@Test public void sendPartyInfo() { MockWebTarget webTarget = restClient.getWebTarget(); Invocation.Builder m = webTarget.getMockInvocationBuilder(); byte[] responseData = "Result".getBytes(); Response response = mock(Response.class); when(response.readEntity(byte[].class)).thenReturn(responseData); when(response.getStatus()).thenReturn(200); List<Entity> postedEntities = new ArrayList<>(); doAnswer( (invocation) -> { postedEntities.add(invocation.getArgument(0)); return response; }) .when(m) .post(any(Entity.class)); String targetUrl = "http: byte[] data = "Some Data".getBytes(); boolean outcome = client.sendPartyInfo(targetUrl, data); assertThat(outcome).isTrue(); assertThat(postedEntities).hasSize(1); Entity entity = postedEntities.get(0); assertThat(entity.getMediaType()).isEqualTo(javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM_TYPE); assertThat(entity.getEntity()).isSameAs(data); verify(response).readEntity(byte[].class); }
@Override public boolean sendPartyInfo(final String targetUrl, final byte[] data) { LOGGER.debug("Sending PartyInfo to peer {}", targetUrl); try (Response response = client.target(targetUrl) .path("/partyinfo") .request() .post(Entity.entity(data, MediaType.APPLICATION_OCTET_STREAM_TYPE))) { final int returnStatusCode = response.getStatus(); if (Status.OK.getStatusCode() != returnStatusCode && Status.CREATED.getStatusCode() != returnStatusCode) { LOGGER.warn("PartyInfo returned status code for peer {} was {}", targetUrl, returnStatusCode); return false; } LOGGER.debug("Successful PartyInfo call to {}", targetUrl); return Objects.nonNull(response.readEntity(byte[].class)); } }
RestP2pClient implements P2pClient { @Override public boolean sendPartyInfo(final String targetUrl, final byte[] data) { LOGGER.debug("Sending PartyInfo to peer {}", targetUrl); try (Response response = client.target(targetUrl) .path("/partyinfo") .request() .post(Entity.entity(data, MediaType.APPLICATION_OCTET_STREAM_TYPE))) { final int returnStatusCode = response.getStatus(); if (Status.OK.getStatusCode() != returnStatusCode && Status.CREATED.getStatusCode() != returnStatusCode) { LOGGER.warn("PartyInfo returned status code for peer {} was {}", targetUrl, returnStatusCode); return false; } LOGGER.debug("Successful PartyInfo call to {}", targetUrl); return Objects.nonNull(response.readEntity(byte[].class)); } } }
RestP2pClient implements P2pClient { @Override public boolean sendPartyInfo(final String targetUrl, final byte[] data) { LOGGER.debug("Sending PartyInfo to peer {}", targetUrl); try (Response response = client.target(targetUrl) .path("/partyinfo") .request() .post(Entity.entity(data, MediaType.APPLICATION_OCTET_STREAM_TYPE))) { final int returnStatusCode = response.getStatus(); if (Status.OK.getStatusCode() != returnStatusCode && Status.CREATED.getStatusCode() != returnStatusCode) { LOGGER.warn("PartyInfo returned status code for peer {} was {}", targetUrl, returnStatusCode); return false; } LOGGER.debug("Successful PartyInfo call to {}", targetUrl); return Objects.nonNull(response.readEntity(byte[].class)); } } RestP2pClient(final Client client); }
RestP2pClient implements P2pClient { @Override public boolean sendPartyInfo(final String targetUrl, final byte[] data) { LOGGER.debug("Sending PartyInfo to peer {}", targetUrl); try (Response response = client.target(targetUrl) .path("/partyinfo") .request() .post(Entity.entity(data, MediaType.APPLICATION_OCTET_STREAM_TYPE))) { final int returnStatusCode = response.getStatus(); if (Status.OK.getStatusCode() != returnStatusCode && Status.CREATED.getStatusCode() != returnStatusCode) { LOGGER.warn("PartyInfo returned status code for peer {} was {}", targetUrl, returnStatusCode); return false; } LOGGER.debug("Successful PartyInfo call to {}", targetUrl); return Objects.nonNull(response.readEntity(byte[].class)); } } RestP2pClient(final Client client); @Override boolean sendPartyInfo(final String targetUrl, final byte[] data); }
RestP2pClient implements P2pClient { @Override public boolean sendPartyInfo(final String targetUrl, final byte[] data) { LOGGER.debug("Sending PartyInfo to peer {}", targetUrl); try (Response response = client.target(targetUrl) .path("/partyinfo") .request() .post(Entity.entity(data, MediaType.APPLICATION_OCTET_STREAM_TYPE))) { final int returnStatusCode = response.getStatus(); if (Status.OK.getStatusCode() != returnStatusCode && Status.CREATED.getStatusCode() != returnStatusCode) { LOGGER.warn("PartyInfo returned status code for peer {} was {}", targetUrl, returnStatusCode); return false; } LOGGER.debug("Successful PartyInfo call to {}", targetUrl); return Objects.nonNull(response.readEntity(byte[].class)); } } RestP2pClient(final Client client); @Override boolean sendPartyInfo(final String targetUrl, final byte[] data);  }