Search in sources :

Example 26 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project Gaffer by gchq.

the class OperationChainTest method shouldDetermineOperationChainOutputType.

@Test
public void shouldDetermineOperationChainOutputType() {
    // Given
    final Operation operation1 = mock(Operation.class);
    final Operation operation2 = mock(Operation.class);
    final TypeReference typeRef = mock(TypeReference.class);
    given(operation2.getOutputTypeReference()).willReturn(typeRef);
    // When
    final OperationChain opChain = new OperationChain.Builder().first(operation1).then(operation2).build();
    // When / Then
    assertSame(typeRef, opChain.getOutputTypeReference());
}
Also used : Builder(uk.gov.gchq.gaffer.operation.OperationChain.Builder) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 27 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project Gaffer by gchq.

the class GeneratorsIT method shouldConvertToDomainObjects.

@Test
public void shouldConvertToDomainObjects() throws OperationException, UnsupportedEncodingException {
    // Given
    final OperationChain<CloseableIterable<DomainObject>> opChain = new OperationChain.Builder().first(new GetElements.Builder<>().addSeed(new EntitySeed(SOURCE_1)).build()).then(new GenerateObjects.Builder<Element, DomainObject>().generator(new BasicGenerator()).outputType(new TypeReference<CloseableIterable<DomainObject>>() {
    }).build()).build();
    // When
    final List<DomainObject> results = Lists.newArrayList(graph.execute(opChain, getUser()));
    final EntityDomainObject entityDomainObject = new EntityDomainObject(SOURCE_1, "3", null);
    final EdgeDomainObject edgeDomainObject = new EdgeDomainObject(SOURCE_1, DEST_1, false, 1, 1L);
    // Then
    assertNotNull(results);
    assertEquals(2, results.size());
    assertThat(results, IsCollectionContaining.hasItems(entityDomainObject, edgeDomainObject));
}
Also used : BasicGenerator(uk.gov.gchq.gaffer.integration.generators.BasicGenerator) CloseableIterable(uk.gov.gchq.gaffer.commonutil.iterable.CloseableIterable) Element(uk.gov.gchq.gaffer.data.element.Element) EntityDomainObject(uk.gov.gchq.gaffer.integration.domain.EntityDomainObject) GetElements(uk.gov.gchq.gaffer.operation.impl.get.GetElements) EdgeDomainObject(uk.gov.gchq.gaffer.integration.domain.EdgeDomainObject) GenerateObjects(uk.gov.gchq.gaffer.operation.impl.generate.GenerateObjects) OperationChain(uk.gov.gchq.gaffer.operation.OperationChain) EntitySeed(uk.gov.gchq.gaffer.operation.data.EntitySeed) EdgeDomainObject(uk.gov.gchq.gaffer.integration.domain.EdgeDomainObject) DomainObject(uk.gov.gchq.gaffer.integration.domain.DomainObject) EntityDomainObject(uk.gov.gchq.gaffer.integration.domain.EntityDomainObject) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 28 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project riposte by Nike-Inc.

the class RequestInfoImplTest method setupContentDeserializer_and_getContent_work_as_expected.

@Test
@DataProvider(value = { "UTF-8", "UTF-16", "UTF-16BE", "UTF-16LE", "ISO-8859-1", "US-ASCII" })
public void setupContentDeserializer_and_getContent_work_as_expected(String charsetName) throws IOException {
    // given
    ObjectMapper objectMapper = new ObjectMapper();
    TypeReference<TestContentObject> typeRef = new TypeReference<TestContentObject>() {
    };
    TestContentObject contentObj = new TestContentObject(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    String contentString = objectMapper.writeValueAsString(contentObj);
    RequestInfoImpl<TestContentObject> requestInfo = (RequestInfoImpl<TestContentObject>) RequestInfoImpl.dummyInstanceForUnknownRequests();
    requestInfo.rawContentBytes = contentString.getBytes(Charset.forName(charsetName));
    // when
    requestInfo.setupContentDeserializer(objectMapper, typeRef);
    // then
    assertThat(requestInfo.getContent(), notNullValue());
    assertThat(requestInfo.getContent().foo, is(contentObj.foo));
    assertThat(requestInfo.getContent().bar, is(contentObj.bar));
}
Also used : TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DataProvider(com.tngtech.java.junit.dataprovider.DataProvider) Test(org.junit.Test)

Example 29 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project riposte by Nike-Inc.

the class RequestInfoImplTest method getContent_throws_RequestContentDeserializationException_if_an_error_occurs_during_deserialization.

@Test
public void getContent_throws_RequestContentDeserializationException_if_an_error_occurs_during_deserialization() throws IOException {
    // given
    RequestInfo<TestContentObject> requestInfoSpy = spy((RequestInfo<TestContentObject>) RequestInfoImpl.dummyInstanceForUnknownRequests());
    ObjectMapper objectMapperSpy = spy(new ObjectMapper());
    byte[] rawBytes = new byte[] {};
    doReturn(rawBytes).when(requestInfoSpy).getRawContentBytes();
    RuntimeException expectedRootCause = new RuntimeException("splat");
    doThrow(expectedRootCause).when(objectMapperSpy).readValue(any(byte[].class), any(TypeReference.class));
    HttpMethod method = HttpMethod.CONNECT;
    String path = UUID.randomUUID().toString();
    TypeReference<TestContentObject> typeRef = new TypeReference<TestContentObject>() {
    };
    doReturn(method).when(requestInfoSpy).getMethod();
    doReturn(path).when(requestInfoSpy).getPath();
    // when
    requestInfoSpy.setupContentDeserializer(objectMapperSpy, typeRef);
    Throwable actualEx = catchThrowable(() -> requestInfoSpy.getContent());
    // then
    assertThat(actualEx, notNullValue());
    assertThat(actualEx, instanceOf(RequestContentDeserializationException.class));
    RequestContentDeserializationException rcde = (RequestContentDeserializationException) actualEx;
    assertThat(rcde.desiredObjectType, sameInstance(typeRef));
    assertThat(rcde.httpMethod, is(String.valueOf(method)));
    assertThat(rcde.requestPath, is(path));
    assertThat(actualEx.getCause(), is(expectedRootCause));
}
Also used : RequestContentDeserializationException(com.nike.riposte.server.error.exception.RequestContentDeserializationException) Assertions.catchThrowable(org.assertj.core.api.Assertions.catchThrowable) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpMethod(io.netty.handler.codec.http.HttpMethod) Test(org.junit.Test)

Example 30 with TypeReference

use of com.fasterxml.jackson.core.type.TypeReference in project riposte by Nike-Inc.

the class RequestInfoImplTest method getContent_returns_object_deserialized_from_raw_bytes_if_content_type_is_not_CharSequence_or_String.

@Test
public void getContent_returns_object_deserialized_from_raw_bytes_if_content_type_is_not_CharSequence_or_String() throws IOException {
    // given
    RequestInfo<TestContentObject> requestInfoSpy = spy((RequestInfo<TestContentObject>) RequestInfoImpl.dummyInstanceForUnknownRequests());
    ObjectMapper objectMapper = new ObjectMapper();
    TestContentObject expectedTco = new TestContentObject(UUID.randomUUID().toString(), UUID.randomUUID().toString());
    byte[] rawBytes = objectMapper.writeValueAsString(expectedTco).getBytes(CharsetUtil.UTF_8);
    doReturn(rawBytes).when(requestInfoSpy).getRawContentBytes();
    ObjectMapper objectMapperSpy = spy(objectMapper);
    TypeReference<TestContentObject> typeRef = new TypeReference<TestContentObject>() {
    };
    // when
    requestInfoSpy.setupContentDeserializer(objectMapperSpy, typeRef);
    TestContentObject result = requestInfoSpy.getContent();
    // then
    assertThat(result, notNullValue());
    assertThat(result.foo, is(expectedTco.foo));
    assertThat(result.bar, is(expectedTco.bar));
    verify(requestInfoSpy).getRawContentBytes();
    verify(requestInfoSpy, never()).getRawContent();
    verify(objectMapperSpy).readValue(rawBytes, typeRef);
}
Also used : TypeReference(com.fasterxml.jackson.core.type.TypeReference) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

TypeReference (com.fasterxml.jackson.core.type.TypeReference)67 IOException (java.io.IOException)27 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)25 Test (org.junit.Test)23 Map (java.util.Map)13 BaseMandrillAnonymousListResponse (com.cribbstechnologies.clients.mandrill.model.response.BaseMandrillAnonymousListResponse)7 StringWriter (java.io.StringWriter)7 ArrayList (java.util.ArrayList)7 ImmutableMap (com.google.common.collect.ImmutableMap)6 HashMap (java.util.HashMap)6 InputStream (java.io.InputStream)5 List (java.util.List)5 SimpleModule (com.fasterxml.jackson.databind.module.SimpleModule)4 AuditInfo (io.druid.audit.AuditInfo)4 Interval (org.joda.time.Interval)4 JsonParseException (com.fasterxml.jackson.core.JsonParseException)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 JsonMappingException (com.fasterxml.jackson.databind.JsonMappingException)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)3 DefaultObjectMapper (io.druid.jackson.DefaultObjectMapper)3