/** * Parse a JSON entity into the desired type. * @param entity the entity * @param valueType the type reference * @param <T> the intended return type * @return the entity as the desired type */ public static <T> T readEntityAsJson(final Object entity, final TypeReference<T> valueType) { try { return MAPPER.readValue(readEntityAsString(entity), valueType); } catch (final IOException ex) { throw new UncheckedIOException(ex); } }
/** * Parse a JSON entity into the desired type. * @param entity the entity * @param valueType the type reference * @param <T> the intended return type * @return the entity as the desired type */ public static <T> T readEntityAsJson(final Object entity, final TypeReference<T> valueType) { try { return MAPPER.readValue(readEntityAsString(entity), valueType); } catch (final IOException ex) { throw new UncheckedIOException(ex); } }
/** * Test the content of a binary memento resource. */ @Test @DisplayName("Test the content of memento resources") default void testMementoContent() { final Map<String, String> mementos = getMementos(); final Map<String, String> responses = new HashMap<>(); mementos.forEach((memento, date) -> { try (final Response res = target(memento).request().get()) { assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a valid response"); responses.put(memento, readEntityAsString(res.getEntity())); } }); assertEquals(3L, responses.size(), "Check for 3 mementos"); responses.forEach((response, content) -> assertTrue(content.startsWith("This is a text file."), "Check the binary content of the mementos")); assertEquals(3L, responses.values().size(), "Check the number of Memento responses"); final Set<String> values = new HashSet<>(responses.values()); assertEquals(3L, values.size(), "Check the number of distinct Memento responses"); }
/** * Test the content of a binary memento resource. */ @Test @DisplayName("Test the content of memento resources") default void testMementoContent() { final Map<String, String> mementos = getMementos(); final Map<String, String> responses = new HashMap<>(); mementos.forEach((memento, date) -> { try (final Response res = target(memento).request().get()) { assertEquals(SUCCESSFUL, res.getStatusInfo().getFamily(), "Check for a valid response"); responses.put(memento, readEntityAsString(res.getEntity())); } }); assertEquals(3L, responses.size(), "Check for 3 mementos"); responses.forEach((response, content) -> assertTrue(content.startsWith("This is a text file."), "Check the binary content of the mementos")); assertEquals(3L, responses.values().size(), "Check the number of Memento responses"); final Set<String> values = new HashSet<>(responses.values()); assertEquals(3L, values.size(), "Check the number of distinct Memento responses"); }
@Test public void testReadEntityAsStringError() throws IOException { final InputStream mockInputStream = mock(InputStream.class); when(mockInputStream.read(any(), anyInt(), anyInt())).thenThrow(new IOException("Expected")); assertThrows(UncheckedIOException.class, () -> TestUtils.readEntityAsString(mockInputStream), "Check that an expected exception is thrown"); }
@Test public void testReadEntityAsString() { final String text = "some text"; final InputStream is = new ByteArrayInputStream(text.getBytes(UTF_8)); assertEquals(text, TestUtils.readEntityAsString(is), "Verify that the input stream matches"); }
/** * Test fetching a binary resource. */ @Test @DisplayName("Test fetching a binary resource") default void testGetBinary() { // Fetch the resource try (final Response res = target(getResourceLocation()).request().get()) { assertAll("Check binary resource", checkNonRdfResponse(res, TEXT_PLAIN_TYPE)); assertEquals(CONTENT, readEntityAsString(res.getEntity()), "Check for matching content"); assertFalse(res.getEntityTag().isWeak(), "Check for a strong ETag"); } }
/** * Test fetching a binary resource. */ @Test @DisplayName("Test fetching a binary resource") default void testGetBinary() { // Fetch the resource try (final Response res = target(getResourceLocation()).request().get()) { assertAll("Check binary resource", checkNonRdfResponse(res, TEXT_PLAIN_TYPE)); assertEquals(CONTENT, readEntityAsString(res.getEntity()), "Check for matching content"); assertFalse(res.getEntityTag().isWeak(), "Check for a strong ETag"); } }
assertEquals(CONTENT, readEntityAsString(res.getEntity()), "Check for an expected binary content value");
assertEquals(CONTENT, readEntityAsString(res.getEntity()), "Check for an expected binary content value");