From 87d3013b2202f1a075378099d06a15e56bff286d Mon Sep 17 00:00:00 2001 From: Simon Bernard Date: Fri, 14 Apr 2023 17:34:14 +0200 Subject: [PATCH] Test java-coap in some of our integration tests. --- leshan-integration-tests/pom.xml | 4 ++ .../leshan/integration/tests/DeleteTest.java | 24 +++++------ .../integration/tests/DiscoverTest.java | 41 +++++-------------- .../leshan/integration/tests/ExecuteTest.java | 34 ++++----------- .../integration/tests/QueueModeTest.java | 3 +- .../integration/tests/RegistrationTest.java | 13 ++++-- .../tests/util/LeshanTestServerBuilder.java | 5 +++ .../util/assertion/LwM2mResponseAssert.java | 21 ++++++++++ 8 files changed, 72 insertions(+), 73 deletions(-) diff --git a/leshan-integration-tests/pom.xml b/leshan-integration-tests/pom.xml index 0ade6c6b5f..f964f51f4a 100644 --- a/leshan-integration-tests/pom.xml +++ b/leshan-integration-tests/pom.xml @@ -38,6 +38,10 @@ Contributors: org.eclipse.leshan leshan-server-cf + + org.eclipse.leshan + leshan-tl-javacoap-server + org.eclipse.leshan leshan-server-redis diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/DeleteTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/DeleteTest.java index 2d8621a66f..33610fa70a 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/DeleteTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/DeleteTest.java @@ -26,6 +26,7 @@ import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing; import static org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder.givenServerUsing; import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.junit.jupiter.params.provider.Arguments.arguments; import java.lang.annotation.Retention; @@ -69,7 +70,8 @@ public class DeleteTest { static Stream transports() { return Stream.of(// // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - arguments(Protocol.COAP, "Californium", "Californium")); + arguments(Protocol.COAP, "Californium", "Californium"), // + arguments(Protocol.COAP, "Californium", "java-coap")); } /*---------------------------------/ @@ -116,15 +118,17 @@ public void delete_created_object_instance(Protocol givenProtocol, String givenC // verify result assertThat(response).hasCode(DELETED); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } // TODO should maybe moved as it only tests client @TestAllTransportLayer public void cannot_delete_resource(Protocol givenProtocol, String givenClientEndpointProvider, String givenServerEndpointProvider) throws InterruptedException { + + // TODO should maybe moved as it only tests client + assumeTrue(givenServerEndpointProvider.equals("Californium")); + // create ACL instance server.send(currentRegistration, new CreateRequest(2, new LwM2mObjectInstance(0, LwM2mSingleResource.newIntegerResource(0, 123)))); @@ -155,9 +159,7 @@ public void cannot_delete_unknown_object_instance(Protocol givenProtocol, String // verify result assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -168,9 +170,7 @@ public void cannot_delete_device_object_instance(Protocol givenProtocol, String // verify result assertThat(response).hasCode(METHOD_NOT_ALLOWED); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -180,8 +180,6 @@ public void cannot_delete_security_object_instance(Protocol givenProtocol, Strin // verify result assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/DiscoverTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/DiscoverTest.java index a36a473bc8..f9d471f39f 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/DiscoverTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/DiscoverTest.java @@ -17,7 +17,6 @@ package org.eclipse.leshan.integration.tests; -import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.leshan.core.ResponseCode.CONTENT; import static org.eclipse.leshan.core.ResponseCode.NOT_FOUND; import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing; @@ -29,7 +28,6 @@ import java.lang.annotation.RetentionPolicy; import java.util.stream.Stream; -import org.eclipse.californium.core.coap.Response; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.request.DiscoverRequest; import org.eclipse.leshan.core.response.DiscoverResponse; @@ -58,7 +56,8 @@ public class DiscoverTest { static Stream transports() { return Stream.of(// // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - arguments(Protocol.COAP, "Californium", "Californium")); + arguments(Protocol.COAP, "Californium", "Californium"), // + arguments(Protocol.COAP, "Californium", "java-coap")); } /*---------------------------------/ @@ -101,9 +100,7 @@ public void can_discover_object(Protocol givenProtocol, String givenClientEndpoi // verify result assertThat(response).hasCode(CONTENT); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); assertThat(response.getObjectLinks()).isLikeLwM2mLinks( ";ver=1.1,,,,,,,,"); @@ -117,9 +114,7 @@ public void cant_discover_non_existent_object(Protocol givenProtocol, String giv // verify result assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -130,9 +125,7 @@ public void can_discover_object_instance(Protocol givenProtocol, String givenCli // verify result assertThat(response).hasCode(CONTENT); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); assertThat(response.getObjectLinks()) .isLikeLwM2mLinks(",,,,,,,"); @@ -146,9 +139,7 @@ public void cant_discover_non_existent_instance(Protocol givenProtocol, String g // verify result assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -159,9 +150,7 @@ public void can_discover_resource(Protocol givenProtocol, String givenClientEndp // verify result assertThat(response).hasCode(CONTENT); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); assertThat(response.getObjectLinks()).isLikeLwM2mLinks(""); } @@ -174,9 +163,7 @@ public void cant_discover_resource_of_non_existent_object(Protocol givenProtocol // verify result assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -187,9 +174,7 @@ public void cant_discover_resource_of_non_existent_instance(Protocol givenProtoc // verify result assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -200,9 +185,7 @@ public void cant_discover_resource_of_non_existent_instance_and_resource(Protoco // verify result assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -213,8 +196,6 @@ public void cant_discover_resource_of_non_existent_resource(Protocol givenProtoc // verify result assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/ExecuteTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/ExecuteTest.java index 72f98938ee..9471fa2e99 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/ExecuteTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/ExecuteTest.java @@ -19,7 +19,6 @@ package org.eclipse.leshan.integration.tests; -import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.leshan.core.ResponseCode.CHANGED; import static org.eclipse.leshan.core.ResponseCode.METHOD_NOT_ALLOWED; import static org.eclipse.leshan.core.ResponseCode.NOT_FOUND; @@ -32,7 +31,6 @@ import java.lang.annotation.RetentionPolicy; import java.util.stream.Stream; -import org.eclipse.californium.core.coap.Response; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.request.ExecuteRequest; import org.eclipse.leshan.core.response.ExecuteResponse; @@ -61,7 +59,8 @@ public class ExecuteTest { static Stream transports() { return Stream.of(// // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - arguments(Protocol.COAP, "Californium", "Californium")); + arguments(Protocol.COAP, "Californium", "Californium"), // + arguments(Protocol.COAP, "Californium", "java-coap")); } /*---------------------------------/ @@ -104,9 +103,7 @@ public void cannot_execute_read_only_resource(Protocol givenProtocol, String giv // verify result assertThat(response).hasCode(METHOD_NOT_ALLOWED); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -117,9 +114,7 @@ public void cannot_execute_read_write_resource(Protocol givenProtocol, String gi // verify result assertThat(response).hasCode(METHOD_NOT_ALLOWED); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -131,9 +126,7 @@ public void cannot_execute_nonexisting_resource_on_existing_object(Protocol give // verify result assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -144,9 +137,7 @@ public void cannot_execute_nonexisting_resource_on_non_existing_object(Protocol // verify result assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -156,9 +147,7 @@ public void cannot_execute_security_object(Protocol givenProtocol, String givenC // verify result assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -169,9 +158,7 @@ public void can_execute_resource(Protocol givenProtocol, String givenClientEndpo // verify result assertThat(response).hasCode(CHANGED); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -182,9 +169,6 @@ public void can_execute_resource_with_parameters(Protocol givenProtocol, String // verify result assertThat(response).hasCode(CHANGED); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); - + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/QueueModeTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/QueueModeTest.java index 1eaba8e1a9..12d5fd8eb3 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/QueueModeTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/QueueModeTest.java @@ -58,7 +58,8 @@ public class QueueModeTest { static Stream transports() { return Stream.of(// // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - arguments(Protocol.COAP, "Californium", "Californium")); + arguments(Protocol.COAP, "Californium", "Californium"), // + arguments(Protocol.COAP, "Californium", "java-coap")); } /*---------------------------------/ diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/RegistrationTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/RegistrationTest.java index c9af7708d8..166a2188a2 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/RegistrationTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/RegistrationTest.java @@ -24,6 +24,7 @@ import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertArg; import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrowsExactly; +import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.junit.jupiter.params.provider.Arguments.arguments; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; @@ -97,7 +98,8 @@ public class RegistrationTest { static Stream transports() { return Stream.of(// // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - arguments(Protocol.COAP, "Californium", "Californium")); + arguments(Protocol.COAP, "Californium", "Californium"), // + arguments(Protocol.COAP, "Californium", "java-coap")); } /*---------------------------------/ @@ -280,6 +282,11 @@ public void register_update_reregister(Protocol protocol, String clientEndpointP @TestAllTransportLayer public void register_observe_deregister_observe(Protocol protocol, String clientEndpointProvider, String serverEndpointProvider) throws NonUniqueSecurityInfoException, InterruptedException { + // TODO java-coap does not raise expected SendFailedException at the end of this tests + // But not sure what should be the right behavior. + // Waiting for https://github.com/open-coap/java-coap/issues/36 before to move forward on this. + assumeTrue(serverEndpointProvider.equals("Californium")); + // Check client is not registered client = givenClient.build(); assertThat(client).isNotRegisteredAt(server); @@ -296,9 +303,7 @@ public void register_observe_deregister_observe(Protocol protocol, String client // observe device timezone ObserveResponse observeResponse = server.send(registration, new ObserveRequest(3, 0)); assertThat(observeResponse).hasCode(CONTENT); - assertThat(observeResponse.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(observeResponse).hasValidUnderlyingResponseFor(serverEndpointProvider); // check observation registry is not null Set observations = server.getObservationService().getObservations(registration); diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestServerBuilder.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestServerBuilder.java index 486a403325..a47850a04d 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestServerBuilder.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/LeshanTestServerBuilder.java @@ -51,6 +51,7 @@ import org.eclipse.leshan.server.security.EditableSecurityStore; import org.eclipse.leshan.server.security.SecurityStore; import org.eclipse.leshan.server.security.ServerSecurityInfo; +import org.eclipse.leshan.transport.javacoap.endpoint.JavaCoapServerEndpointsProvider; public class LeshanTestServerBuilder extends LeshanServerBuilder { @@ -97,6 +98,10 @@ protected LeshanTestServer createServer(LwM2mServerEndpointsProvider endpointsPr builder.addEndpoint(new InetSocketAddress(InetAddress.getLoopbackAddress(), 0), protocolToUse); endpointsProvider = builder.build(); break; + case "java-coap": + endpointsProvider = new JavaCoapServerEndpointsProvider( + new InetSocketAddress(InetAddress.getLoopbackAddress(), 0)); + break; default: throw new IllegalStateException( String.format("Unknown endpoint provider : [%s]", endpointProviderName)); diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/LwM2mResponseAssert.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/LwM2mResponseAssert.java index cd34515891..d68e730d47 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/LwM2mResponseAssert.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/LwM2mResponseAssert.java @@ -16,9 +16,13 @@ package org.eclipse.leshan.integration.tests.util.assertion; import org.assertj.core.api.AbstractAssert; +import org.assertj.core.api.ObjectAssert; +import org.eclipse.californium.core.coap.Response; import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.response.LwM2mResponse; +import com.mbed.coap.packet.CoapResponse; + public class LwM2mResponseAssert extends AbstractAssert { public LwM2mResponseAssert(LwM2mResponse actual) { @@ -40,4 +44,21 @@ public LwM2mResponseAssert hasCode(ResponseCode expectedCode) { } return this; } + + public LwM2mResponseAssert hasValidUnderlyingResponseFor(String givenEndpointProvider) { + ObjectAssert assertThatUnderlyingResponse = Assertions.assertThat(actual.getCoapResponse()) + .as("Underlying Response"); + switch (givenEndpointProvider) { + case "Californium": + case "Californium-OSCORE": + assertThatUnderlyingResponse.isExactlyInstanceOf(Response.class); + break; + case "java-coap": + assertThatUnderlyingResponse.isExactlyInstanceOf(CoapResponse.class); + break; + default: + throw new IllegalStateException(String.format("Unsupported endpoint provider : %s", givenEndpointProvider)); + } + return this; + } }