### Question:
CoapServer { public byte[] observe(String uri, InetSocketAddress destination, final Callback<CoapPacket> respCallback, byte[] token, TransportContext transportContext) { CoapPacket request = new CoapPacket(Method.GET, MessageType.Confirmable, uri, destination); request.setToken(token); request.headers().setObserve(0); return observe(request, respCallback, transportContext); } CoapServer(CoapMessaging coapMessaging); static CoapServerBuilder.CoapServerBuilderForUdp builder(); static CoapServerBuilder.CoapServerBuilderForTcp builderForTcp(); synchronized CoapServer start(); final synchronized void stop(); boolean isRunning(); void addRequestHandler(String uri, CoapHandler coapHandler); void removeRequestHandler(CoapHandler requestHandler); InetSocketAddress getLocalSocketAddress(); final CompletableFuture<CoapPacket> makeRequest(CoapPacket requestPacket); final CompletableFuture<CoapPacket> makeRequest(CoapPacket requestPacket, final TransportContext transContext); final void makeRequest(CoapPacket packet, Callback<CoapPacket> callback); void makeRequest(final CoapPacket packet, final Callback<CoapPacket> callback, final TransportContext transContext); void sendNotification(final CoapPacket notifPacket, final Callback<CoapPacket> callback, final TransportContext transContext); void ping(InetSocketAddress destination, final Callback<CoapPacket> callback); void setObservationHandler(ObservationHandler observationHandler); void setConnectHandler(Consumer<InetSocketAddress> disconnectConsumer); void useCriticalOptionTest(boolean enable); CoapHandler getResourceLinkResource(); List<LinkFormat> getResourceLinks(); void sendResponse(CoapExchange exchange); byte[] observe(String uri, InetSocketAddress destination, final Callback<CoapPacket> respCallback, byte[] token, TransportContext transportContext); byte[] observe(CoapPacket request, final Callback<CoapPacket> respCallback, TransportContext transportContext); void setObservationIDGenerator(ObservationIDGenerator observationIDGenerator); CoapMessaging getCoapMessaging();  }### Answer:
@Test public void shouldRespondToObservationRequest() { CoapPacket resp = newCoapPacket().ack(Code.C205_CONTENT).obs(0).build(); RequestCallback respCallback = mock(RequestCallback.class); server.observe("/test", LOCAL_5683, respCallback, "aa".getBytes(), TransportContext.NULL); verifyMakeRequest_andThen().onSent(); verifyMakeRequest_andThen().call(resp); verify(respCallback).onSent(); verify(respCallback).call(eq(resp)); }
@Test public void shouldRespondToObservationRequest_notObserved() { Callback<CoapPacket> respCallback = mock(Callback.class); server.observe("/test", LOCAL_5683, respCallback, "aa".getBytes(), TransportContext.NULL); verifyMakeRequest_andThen().onSent(); verifyMakeRequest_andThen().call(newCoapPacket().ack(Code.C205_CONTENT).build()); verify(respCallback).callException(isA(ObservationNotEstablishedException.class)); }
@Test public void shouldRespondToObservationRequest_errorResponse() { Callback<CoapPacket> respCallback = mock(Callback.class); server.observe("/test", LOCAL_5683, respCallback, "aa".getBytes(), TransportContext.NULL); verifyMakeRequest_andThen().call(newCoapPacket().ack(Code.C404_NOT_FOUND).build()); verify(respCallback).callException(isA(ObservationNotEstablishedException.class)); }
@Test public void shouldRespondToObservationRequest_exception() { Callback<CoapPacket> respCallback = mock(Callback.class); server.observe("/test", LOCAL_5683, respCallback, "aa".getBytes(), TransportContext.NULL); verifyMakeRequest_andThen().callException(new IOException()); verify(respCallback).callException(isA(IOException.class)); }