SimpleRoadClient implements RoadClient<T> { @Override public List<StandardResponse> sendMessages(List<T> messages) { try { String json = objectMapper.writeValueAsString(messages); HttpEntity entity = EntityBuilder.create().setText(json).setContentType(APPLICATION_JSON).gzipCompress().build(); HttpResponse response = handler.post("roads/" + roadName + "/messages", entity); int statusCode = response.getStatusLine().getStatusCode(); if (statusCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR) { throw new OnrampServerException(errorResponseAsString(response)); } else if (statusCode >= HttpStatus.SC_BAD_REQUEST) { StandardResponse standardResponse = objectMapper.readValue(response.getEntity().getContent(), StandardResponse.class); return failedBatchResponse(messages, standardResponse); } else if (statusCode >= HttpStatus.SC_MULTIPLE_CHOICES) { throw new OnrampServerException(errorResponseAsString(response)); } return objectMapper.readValue(response.getEntity().getContent(), RESPONSE_REF); } catch (JsonProcessingException e) { throw new OnrampEncodingException(e); } catch (Exception e) { throw new OnrampException(e); } } @Deprecated /** @deprecated Use {@link #SimpleRoadClient(OnrampOptions)} */  SimpleRoadClient(String host, String username, String password, String roadName); @Deprecated /** @deprecated Use {@link #SimpleRoadClient(OnrampOptions)} */  SimpleRoadClient(String host, String username, String password, String roadName, int threads); @Deprecated /** @deprecated Use {@link #SimpleRoadClient(OnrampOptions)} */  SimpleRoadClient(String host, String username, String password, String roadName, ObjectMapper objectMapper); @Deprecated /** @deprecated Use {@link #SimpleRoadClient(OnrampOptions)} */  SimpleRoadClient(
      String host,
      String username,
      String password,
      String roadName,
      int threads,
      ObjectMapper objectMapper); @Deprecated /** @deprecated Use {@link #SimpleRoadClient(OnrampOptions)} */  SimpleRoadClient(
      String host,
      String username,
      String password,
      String roadName,
      int threads,
      TLSConfig tlsConfig); @Deprecated /** @deprecated Use {@link #SimpleRoadClient(OnrampOptions)} */  SimpleRoadClient(
      String host,
      String username,
      String password,
      String roadName,
      int threads,
      TLSConfig tlsConfig,
      ObjectMapper objectMapper);  SimpleRoadClient(OnrampOptions options); @Override StandardResponse sendMessage(T message); @Override List<StandardResponse> sendMessages(List<T> messages); String errorResponseAsString(HttpResponse response); @Override void close();  }
@Test public void multipleMessages() throws Exception { List<SimpleModel> messages = createMessages(3); stubFor(post(urlEqualTo("/onramp/v1/roads/" + TEST_ROAD + "/messages")).withBasicAuth("user", "pass").willReturn( aResponse().withStatus(HttpStatus.SC_OK).withBody(createStubResponse(3, true, "accepted: ")))); List<StandardResponse> responses = underTest.sendMessages(messages); assertThat(responses.get(0).isSuccess(), is(true)); assertThat(responses.get(0).getTimestamp(), is(100L)); assertThat(responses.get(0).getMessage(), is("accepted: 0")); assertThat(responses.get(1).isSuccess(), is(true)); assertThat(responses.get(1).getTimestamp(), is(101L)); assertThat(responses.get(1).getMessage(), is("accepted: 1")); assertThat(responses.get(2).isSuccess(), is(true)); assertThat(responses.get(2).getTimestamp(), is(102L)); assertThat(responses.get(2).getMessage(), is("accepted: 2")); }
@Test public void badRequestMultiple() throws Exception { List<SimpleModel> messages = createMessages(3); stubFor(post(urlEqualTo("/onramp/v1/roads/" + TEST_ROAD + "/messages")) .willReturn(aResponse().withStatus(SC_BAD_REQUEST).withBody(createSingleResponse(0, false, "error")))); List<StandardResponse> responses = underTest.sendMessages(messages); assertThat(responses.size(), is(3)); assertThat(responses.get(0).isSuccess(), is(false)); assertThat(responses.get(0).getMessage(), is("error")); assertThat(responses.get(1).isSuccess(), is(false)); assertThat(responses.get(1).getMessage(), is("error")); assertThat(responses.get(2).isSuccess(), is(false)); assertThat(responses.get(2).getMessage(), is("error")); }