JmapResponseWriterImpl implements JmapResponseWriter { @Override public Flux<InvocationResponse> formatMethodResponse(Flux<JmapResponse> jmapResponses) { return jmapResponses.map(jmapResponse -> { ObjectMapper objectMapper = newConfiguredObjectMapper(jmapResponse); return new InvocationResponse( jmapResponse.getResponseName(), objectMapper.valueToTree(jmapResponse.getResponse()), jmapResponse.getMethodCallId()); }); } @Inject  JmapResponseWriterImpl(ObjectMapperFactory objectMapperFactory); @Override Flux<InvocationResponse> formatMethodResponse(Flux<JmapResponse> jmapResponses); static final String PROPERTIES_FILTER; }
@Ignore @Test(expected = IllegalStateException.class) public void formatMethodResponseShouldWorkWhenNullJmapResponse() { String expectedMethod = "nwonMethod"; String expectedMethodCallId = "#1"; String expectedId = "myId"; Stream<InvocationResponse> response = testee.formatMethodResponse(Flux.just(JmapResponse .builder() .methodCallId(MethodCallId.of(expectedMethodCallId)) .response(null) .build())) .toStream(); List<InvocationResponse> responseList = response.collect(Collectors.toList()); assertThat(responseList).hasSize(1) .extracting(InvocationResponse::getResponseName, x -> x.getResults().get("id").asText(), InvocationResponse::getMethodCallId) .containsExactly(tuple(expectedMethod, expectedId, expectedMethodCallId)); }
@Test public void formatMethodResponseShouldWork() { String expectedMethodCallId = "#1"; String expectedId = "myId"; ResponseClass responseClass = new ResponseClass(); responseClass.id = expectedId; List<InvocationResponse> response = testee.formatMethodResponse( Flux.just(JmapResponse .builder() .responseName(Method.Response.name("unknownMethod")) .methodCallId(MethodCallId.of(expectedMethodCallId)) .response(responseClass) .build())) .collectList() .block(); assertThat(response).hasSize(1) .extracting(InvocationResponse::getResponseName, x -> x.getResults().get("id").asText(), InvocationResponse::getMethodCallId) .containsExactly(tuple(Method.Response.name("unknownMethod"), expectedId, MethodCallId.of(expectedMethodCallId))); }
@Test public void formatMethodResponseShouldFilterFieldsWhenProperties() { ObjectResponseClass responseClass = new ObjectResponseClass(); responseClass.list = ImmutableList.of(new ObjectResponseClass.Foo("id", "name")); Property property = () -> "id"; List<InvocationResponse> response = testee.formatMethodResponse( Flux.just(JmapResponse .builder() .responseName(Method.Response.name("unknownMethod")) .methodCallId(MethodCallId.of("#1")) .properties(ImmutableSet.of(property)) .response(responseClass) .build())) .collectList() .block(); assertThat(response).hasSize(1); JsonNode firstObject = Iterables.getOnlyElement(response).getResults().get("list").elements().next(); assertThat(firstObject.get("id").asText()).isEqualTo("id"); assertThat(firstObject.get("name")).isNull(); }
@Test public void formatMethodResponseShouldNotFilterFieldsWhenSecondCallWithoutProperties() { ObjectResponseClass responseClass = new ObjectResponseClass(); responseClass.list = ImmutableList.of(new ObjectResponseClass.Foo("id", "name")); Property property = () -> "id"; @SuppressWarnings("unused") Stream<InvocationResponse> ignoredResponse = testee.formatMethodResponse( Flux.just(JmapResponse .builder() .responseName(Method.Response.name("unknownMethod")) .methodCallId(MethodCallId.of("#1")) .properties(ImmutableSet.of(property)) .response(responseClass) .build())) .toStream(); List<InvocationResponse> response = testee.formatMethodResponse( Flux.just(JmapResponse .builder() .responseName(Method.Response.name("unknownMethod")) .methodCallId(MethodCallId.of("#1")) .response(responseClass) .build())) .collect(Collectors.toList()) .block(); assertThat(response).hasSize(1); JsonNode firstObject = Iterables.getOnlyElement(response).getResults().get("list").elements().next(); assertThat(firstObject.get("id").asText()).isEqualTo("id"); assertThat(firstObject.get("name").asText()).isEqualTo("name"); }
@Test @SuppressWarnings("unchecked") public void formatMethodResponseShouldFilterRightFieldsForEachResponse() { ObjectResponseClass responseClass = new ObjectResponseClass(); responseClass.list = ImmutableList.of(new ObjectResponseClass.Foo("id", "name")); Property idProperty = () -> "id"; Property nameProperty = () -> "name"; List<InvocationResponse> response = testee.formatMethodResponse( Flux.just(JmapResponse .builder() .responseName(Method.Response.name("unknownMethod")) .methodCallId(MethodCallId.of("#1")) .properties(ImmutableSet.of(idProperty, nameProperty)) .response(responseClass) .build(), JmapResponse .builder() .responseName(Method.Response.name("unknownMethod")) .methodCallId(MethodCallId.of("#1")) .properties(ImmutableSet.of(idProperty)) .response(responseClass) .build())) .collectList() .block(); assertThat(response).hasSize(2) .extracting(x -> x.getResults().get("list").elements().next()) .extracting( x -> x.get("id").asText(), x -> Optional.ofNullable(x.get("name")).map(JsonNode::asText).orElse(null)) .containsExactly(tuple("id", "name"), tuple("id", null)); }
@Test public void formatErrorResponseShouldWork() { String expectedMethodCallId = "#1"; ObjectNode parameters = new ObjectNode(new JsonNodeFactory(false)); parameters.put("id", "myId"); JsonNode[] nodes = new JsonNode[]{new ObjectNode(new JsonNodeFactory(false)).textNode("unknwonMethod"), parameters, new ObjectNode(new JsonNodeFactory(false)).textNode(expectedMethodCallId)}; List<InvocationResponse> response = testee.formatMethodResponse( Flux.just(JmapResponse .builder() .methodCallId(InvocationRequest.deserialize(nodes).getMethodCallId()) .error() .build())) .collectList() .block(); assertThat(response).hasSize(1) .extracting(InvocationResponse::getResponseName, x -> x.getResults().get("type").asText(), InvocationResponse::getMethodCallId) .containsExactly(tuple(ErrorResponse.ERROR_METHOD, ErrorResponse.DEFAULT_ERROR_MESSAGE, MethodCallId.of(expectedMethodCallId))); }
@Test public void formatErrorResponseShouldWorkWithTypeAndDescription() { String expectedMethodCallId = "#1"; ObjectNode parameters = new ObjectNode(new JsonNodeFactory(false)); parameters.put("id", "myId"); JsonNode[] nodes = new JsonNode[]{new ObjectNode(new JsonNodeFactory(false)).textNode("unknwonMethod"), parameters, new ObjectNode(new JsonNodeFactory(false)).textNode(expectedMethodCallId)}; List<InvocationResponse> response = testee.formatMethodResponse( Flux.just(JmapResponse .builder() .methodCallId(InvocationRequest.deserialize(nodes).getMethodCallId()) .error(ErrorResponse .builder() .type("errorType") .description("complete description") .build()) .build())) .collectList() .block(); assertThat(response).hasSize(1) .extracting(InvocationResponse::getResponseName, x -> x.getResults().get("type").asText(), x -> x.getResults().get("description").asText(), InvocationResponse::getMethodCallId) .containsExactly(tuple(ErrorResponse.ERROR_METHOD, "errorType", "complete description", MethodCallId.of(expectedMethodCallId))); }