@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")); }
@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); } }
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); } } }
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); }
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(); }
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();  }