JacksonRequestConverterFunction implements RequestConverterFunction { @Override @Nullable public Object convertRequest( ServiceRequestContext ctx, AggregatedHttpRequest request, Class<?> expectedResultType, @Nullable ParameterizedType expectedParameterizedResultType) throws Exception { final MediaType contentType = request.contentType(); if (contentType != null && (contentType.is(MediaType.JSON) || contentType.subtype().endsWith("+json"))) { if (expectedResultType == TreeNode.class || expectedResultType == JsonNode.class) { try { return mapper.readTree(getContent(request, contentType)); } catch (JsonProcessingException e) { throw newConversionException(e); } } final ObjectReader reader = getObjectReader(expectedResultType, expectedParameterizedResultType); if (reader != null) { final String content = getContent(request, contentType); try { return reader.readValue(content); } catch (JsonProcessingException e) { if (skippableTypes.containsKey(expectedResultType)) { return RequestConverterFunction.fallthrough(); } throw newConversionException(e); } } } return RequestConverterFunction.fallthrough(); } JacksonRequestConverterFunction();  JacksonRequestConverterFunction(ObjectMapper mapper); @Override @Nullable Object convertRequest(
            ServiceRequestContext ctx, AggregatedHttpRequest request, Class<?> expectedResultType,
            @Nullable ParameterizedType expectedParameterizedResultType);  }
@Test(expected = FallthroughException.class) public void jsonTextToByteArray() throws Exception { when(req.contentType()).thenReturn(MediaType.JSON); when(req.content(StandardCharsets.UTF_8)).thenReturn(JSON_TEXT); function.convertRequest(ctx, req, byte[].class, null); }
@Test(expected = FallthroughException.class) public void jsonTextToHttpData() throws Exception { when(req.contentType()).thenReturn(MediaType.JSON); when(req.content(StandardCharsets.UTF_8)).thenReturn(JSON_TEXT); function.convertRequest(ctx, req, HttpData.class, null); }
@Test(expected = FallthroughException.class) public void jsonTextToString() throws Exception { when(req.contentType()).thenReturn(MediaType.JSON); when(req.content(StandardCharsets.UTF_8)).thenReturn(JSON_TEXT); function.convertRequest(ctx, req, String.class, null); }
@Test public void jsonTextToTreeNode() throws Exception { when(req.contentType()).thenReturn(MediaType.JSON); when(req.content(StandardCharsets.UTF_8)).thenReturn(JSON_TEXT); assertThat(function.convertRequest(ctx, req, TreeNode.class, null)) .isEqualTo(new ObjectMapper().readTree(JSON_TEXT)); }
@Test public void jsonTextToJsonRequest() throws Exception { when(req.contentType()).thenReturn(MediaType.JSON); when(req.content(StandardCharsets.UTF_8)).thenReturn(JSON_TEXT); final Object result = function.convertRequest(ctx, req, JsonRequest.class, null); assertThat(result).isInstanceOf(JsonRequest.class); }
@Test public void jsonArrayToListOfInteger() throws Exception { when(req.contentType()).thenReturn(MediaType.JSON); when(req.content(StandardCharsets.UTF_8)).thenReturn(JSON_ARRAY); final Object result = function.convertRequest( ctx, req, List.class, (ParameterizedType) new TypeReference<List<Integer>>() {}.getType()); assertThat(result).isEqualTo(ImmutableList.of(1, 2, 3)); }
@Test public void jsonArrayToListOfLong() throws Exception { when(req.contentType()).thenReturn(MediaType.JSON); when(req.content(StandardCharsets.UTF_8)).thenReturn(JSON_ARRAY); final Object result = function.convertRequest( ctx, req, List.class, (ParameterizedType) new TypeReference<List<Long>>() {}.getType()); assertThat(result).isEqualTo(ImmutableList.of(1L, 2L, 3L)); }
@Test public void jsonArrayToTreeNode() throws Exception { when(req.contentType()).thenReturn(MediaType.JSON); when(req.content(StandardCharsets.UTF_8)).thenReturn(JSON_ARRAY); final Object result = function.convertRequest(ctx, req, TreeNode.class, null); assertThat(result).isEqualTo(new ObjectMapper().readTree(JSON_ARRAY)); }
@Test public void jsonArrayToListOfString() throws Exception { when(req.contentType()).thenReturn(MediaType.JSON); when(req.content(StandardCharsets.UTF_8)).thenReturn(JSON_ARRAY); final Object result = function.convertRequest( ctx, req, List.class, (ParameterizedType) new TypeReference<List<String>>() {}.getType()); assertThat(result).isEqualTo(ImmutableList.of("1", "2", "3")); }