### Question:
CoapTcpMessaging extends CoapMessaging { @Override public void makeRequest(CoapPacket packet, Callback<CoapPacket> callback, TransportContext transContext) { int payloadLen = packet.getPayload().length; int maxMessageSize = csmStorage.getOrDefault(packet.getRemoteAddress()).getMaxMessageSizeInt(); if (payloadLen > maxMessageSize) { callback.callException( new CoapException("Request payload size is too big and no block transfer support is enabled for " + packet.getRemoteAddress() + ": " + payloadLen) ); return; } RequestCallback requestCallback = wrapCallback(callback); DelayedTransactionId transId = new DelayedTransactionId(packet.getToken(), packet.getRemoteAddress()); transactions.put(transId, requestCallback); sendPacket(packet, packet.getRemoteAddress(), transContext) .whenComplete((wasSent, maybeError) -> { if (maybeError == null) { requestCallback.onSent(); } else { removeTransactionExceptionally(transId, (Exception) maybeError); } }); } CoapTcpMessaging(CoapTransport coapTransport, CoapTcpCSMStorage csmStorage, boolean useBlockWiseTransfer, int maxMessageSize); @Override void ping(InetSocketAddress destination, Callback<CoapPacket> callback); @Override void makeRequest(CoapPacket packet, Callback<CoapPacket> callback, TransportContext transContext); @Override void sendResponse(CoapPacket request, CoapPacket response, TransportContext transContext); @Override void onConnected(InetSocketAddress remoteAddress); @Override void onDisconnected(InetSocketAddress remoteAddress); @Override void makePrioritisedRequest(CoapPacket packet, Callback<CoapPacket> callback, TransportContext transContext);  }### Answer:
@Test public void should_receive_response_to_request() throws Exception { CompletableFuture<CoapPacket> resp = makeRequest(newCoapPacket(LOCAL_1_5683).token(2001).con().get().uriPath("/test")); receive(newCoapPacket(LOCAL_1_5683).token(2001).ack(Code.C205_CONTENT)); assertNotNull(resp.get()); }
@Test public void should_ignore_non_matching_response() throws Exception { CompletableFuture<CoapPacket> resp = makeRequest(newCoapPacket(LOCAL_1_5683).token(2001).con().get().uriPath("/test")); receive(newCoapPacket(LOCAL_1_5683).token(1002).ack(Code.C205_CONTENT)); assertFalse(resp.isDone()); }
@Test public void should_fail_to_make_request_when_transport_fails() throws Exception { given(coapTransport.sendPacket(any(), any(), any())).willReturn(completedFuture(new IOException())); CompletableFuture<CoapPacket> resp = makeRequest(newCoapPacket(LOCAL_1_5683).token(2001).con().get().uriPath("/test")); assertTrue(resp.isCompletedExceptionally()); }
@Test public void should_call_exception_when_abort_signal_received() throws Exception { CompletableFuture<CoapPacket> resp = makeRequest(newCoapPacket(LOCAL_1_5683).token(2001).con().get().uriPath("/test")); reset(coapTransport); receive(newCoapPacket(LOCAL_1_5683).con(Code.C705_ABORT)); assertTrue(resp.isCompletedExceptionally()); assertThatThrownBy(resp::get).hasCauseExactlyInstanceOf(IOException.class); assertNothingSent(); }
@Test public void shouldThrowExceptionWhenTooLargePayload() { CompletableFuture<CoapPacket> resp = makeRequest(newCoapPacket(LOCAL_5683).get().payload(new byte[1200])); assertTrue(resp.isCompletedExceptionally()); assertThatThrownBy(resp::get).hasCauseExactlyInstanceOf(CoapException.class); }