SyncSender extends AbstractSender { private Response send(String body) throws IOException { HttpURLConnection connection = getConnection(); byte[] bytes = body.getBytes(UTF_8); sendJson(connection, bytes); return readResponse(connection); } SyncSender(Builder builder); @Override Response doSend(Payload payload); @Override void close(); static final String DEFAULT_API_ENDPOINT; }
@Test public void shouldSendThePayloadWithSuccessResult() throws Exception { int responseCode = 200; String responseJson = "simulated_response_json"; when(connection.getResponseCode()).thenReturn(responseCode); when(connection.getInputStream()) .thenReturn(new ByteArrayInputStream(responseJson.getBytes(UTF_8))); when(serializer.resultFrom(responseJson)).thenReturn(result); sut.send(payload); Response expectedResponse = new Response.Builder() .status(responseCode) .result(result) .build(); verifyHttp(); verify(connection).getInputStream(); verify(listener).onResponse(payload, expectedResponse); }
@Test public void shouldSendThePayloadWithResponseError() throws Exception { int responseCode = 400; String responseJson = "simulated_response_json"; String apiMessage = "simulated_api_error_json"; Result result = new Result.Builder() .code(1) .body(apiMessage) .build(); Response response = new Response.Builder() .status(responseCode) .result(result) .build(); ApiException sourceError = new ApiException(response); when(connection.getResponseCode()).thenReturn(responseCode); when(connection.getErrorStream()) .thenReturn(new ByteArrayInputStream(responseJson.getBytes(UTF_8))); when(serializer.resultFrom(responseJson)).thenReturn(result); sut.send(payload); verifyHttp(); verify(connection).getErrorStream(); ArgumentCaptor<SenderException> argument = ArgumentCaptor.forClass(SenderException.class); verify(listener).onError(eq(payload), argument.capture()); assertThat(argument.getValue(), is(instanceOf(SenderException.class))); assertThat(argument.getValue().getCause(), is(sourceError)); }
@Test public void shouldNotifyErrorDuringSend() throws Exception { IOException sourceError = new IOException("Error opening the connection."); byte[] bytes = PAYLOAD_JSON.getBytes(UTF_8); doThrow(sourceError).when(out).write(bytes, 0, bytes.length); sut.send(payload); verifyHttp(); ArgumentCaptor<SenderException> argument = ArgumentCaptor.forClass(SenderException.class); verify(listener).onError(eq(payload), argument.capture()); assertThat(argument.getValue(), is(instanceOf(SenderException.class))); assertThat(argument.getValue().getCause(), is(sourceError)); }
@Test public void shouldSendThePayloadUsingAProxyIfProvided() throws Exception { Proxy proxy = mock(Proxy.class); when(url.openConnection(eq(proxy))).thenReturn(connection); when(connection.getOutputStream()).thenReturn(out); when(serializer.toJson(payload)).thenReturn(PAYLOAD_JSON); sut = new SyncSender.Builder() .url(url) .jsonSerializer(serializer) .proxy(proxy) .build(); sut.addListener(listener); int responseCode = 200; String responseJson = "simulated_response_json"; when(connection.getResponseCode()).thenReturn(responseCode); when(connection.getInputStream()) .thenReturn(new ByteArrayInputStream(responseJson.getBytes(UTF_8))); when(serializer.resultFrom(responseJson)).thenReturn(result); sut.send(payload); Response expectedResponse = new Response.Builder() .status(responseCode) .result(result) .build(); verifyHttp(); verify(connection).getInputStream(); verify(listener).onResponse(payload, expectedResponse); }