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..b4d2d710cc 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; @@ -115,16 +116,19 @@ public void delete_created_object_instance(Protocol givenProtocol, String givenC DeleteResponse response = server.send(currentRegistration, new DeleteRequest(2, 0)); // verify result - assertThat(response).hasCode(DELETED); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(DELETED) // + .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)))); @@ -154,10 +158,9 @@ public void cannot_delete_unknown_object_instance(Protocol givenProtocol, String DeleteResponse response = server.send(currentRegistration, new DeleteRequest(2, 0)); // verify result - assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -167,10 +170,9 @@ public void cannot_delete_device_object_instance(Protocol givenProtocol, String DeleteResponse response = server.send(currentRegistration, new DeleteRequest(3, 0)); // verify result - assertThat(response).hasCode(METHOD_NOT_ALLOWED); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(METHOD_NOT_ALLOWED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -179,9 +181,8 @@ public void cannot_delete_security_object_instance(Protocol givenProtocol, Strin DeleteResponse response = server.send(currentRegistration, new DeleteRequest(0, 0)); // verify result - assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(NOT_FOUND) // + .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..bff6a6c7b7 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; @@ -100,13 +98,12 @@ public void can_discover_object(Protocol givenProtocol, String givenClientEndpoi DiscoverResponse response = server.send(currentRegistration, new DiscoverRequest(3)); // verify result - assertThat(response).hasCode(CONTENT); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider) // */ + .hasObjectLinksLike( + ";ver=1.1,,,,,,,,"); - assertThat(response.getObjectLinks()).isLikeLwM2mLinks( - ";ver=1.1,,,,,,,,"); } @TestAllTransportLayer @@ -116,10 +113,9 @@ public void cant_discover_non_existent_object(Protocol givenProtocol, String giv DiscoverResponse response = server.send(currentRegistration, new DiscoverRequest(4)); // verify result - assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -129,13 +125,10 @@ public void can_discover_object_instance(Protocol givenProtocol, String givenCli DiscoverResponse response = server.send(currentRegistration, new DiscoverRequest(3, 0)); // verify result - assertThat(response).hasCode(CONTENT); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); - - assertThat(response.getObjectLinks()) - .isLikeLwM2mLinks(",,,,,,,"); + assertThat(response) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider) // + .hasObjectLinksLike(",,,,,,,"); } @TestAllTransportLayer @@ -145,10 +138,9 @@ public void cant_discover_non_existent_instance(Protocol givenProtocol, String g DiscoverResponse response = server.send(currentRegistration, new DiscoverRequest(3, 1)); // verify result - assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -158,10 +150,10 @@ public void can_discover_resource(Protocol givenProtocol, String givenClientEndp DiscoverResponse response = server.send(currentRegistration, new DiscoverRequest(3, 0, 0)); // verify result - assertThat(response).hasCode(CONTENT); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider) // + .hasObjectLinksLike(""); assertThat(response.getObjectLinks()).isLikeLwM2mLinks(""); } @@ -173,10 +165,9 @@ public void cant_discover_resource_of_non_existent_object(Protocol givenProtocol DiscoverResponse response = server.send(currentRegistration, new DiscoverRequest(4, 0, 0)); // verify result - assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -186,10 +177,9 @@ public void cant_discover_resource_of_non_existent_instance(Protocol givenProtoc DiscoverResponse response = server.send(currentRegistration, new DiscoverRequest(3, 1, 0)); // verify result - assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -199,10 +189,9 @@ public void cant_discover_resource_of_non_existent_instance_and_resource(Protoco DiscoverResponse response = server.send(currentRegistration, new DiscoverRequest(3, 1, 20)); // verify result - assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -212,9 +201,8 @@ public void cant_discover_resource_of_non_existent_resource(Protocol givenProtoc DiscoverResponse response = server.send(currentRegistration, new DiscoverRequest(3, 0, 42)); // verify result - assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(NOT_FOUND) // + .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..400d675450 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; @@ -103,10 +101,9 @@ public void cannot_execute_read_only_resource(Protocol givenProtocol, String giv ExecuteResponse response = server.send(currentRegistration, new ExecuteRequest(3, 0, 0)); // verify result - assertThat(response).hasCode(METHOD_NOT_ALLOWED); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(METHOD_NOT_ALLOWED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -116,10 +113,9 @@ public void cannot_execute_read_write_resource(Protocol givenProtocol, String gi ExecuteResponse response = server.send(currentRegistration, new ExecuteRequest(3, 0, 13)); // verify result - assertThat(response).hasCode(METHOD_NOT_ALLOWED); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(METHOD_NOT_ALLOWED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -130,10 +126,9 @@ public void cannot_execute_nonexisting_resource_on_existing_object(Protocol give ExecuteResponse response = server.send(currentRegistration, new ExecuteRequest(3, 0, nonExistingResourceId)); // verify result - assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -143,10 +138,9 @@ public void cannot_execute_nonexisting_resource_on_non_existing_object(Protocol ExecuteResponse response = server.send(currentRegistration, new ExecuteRequest(nonExistingObjectId, 0, 0)); // verify result - assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -155,10 +149,9 @@ public void cannot_execute_security_object(Protocol givenProtocol, String givenC ExecuteResponse response = server.send(currentRegistration, new ExecuteRequest(0, 0, 0)); // verify result - assertThat(response).hasCode(NOT_FOUND); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -168,10 +161,9 @@ public void can_execute_resource(Protocol givenProtocol, String givenClientEndpo ExecuteResponse response = server.send(currentRegistration, new ExecuteRequest(3, 0, 4)); // verify result - assertThat(response).hasCode(CHANGED); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -181,10 +173,8 @@ public void can_execute_resource_with_parameters(Protocol givenProtocol, String ExecuteResponse response = server.send(currentRegistration, new ExecuteRequest(3, 0, 4, "6")); // verify result - assertThat(response).hasCode(CHANGED); - assertThat(response.getCoapResponse())// - .isNotNull()// - .isInstanceOf(Response.class); - + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/PresenceCounter.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/PresenceCounter.java deleted file mode 100644 index 25199e2237..0000000000 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/PresenceCounter.java +++ /dev/null @@ -1,55 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2018 Sierra Wireless and others. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v2.0 - * and Eclipse Distribution License v1.0 which accompany this distribution. - * - * The Eclipse Public License is available at - * http://www.eclipse.org/legal/epl-v20.html - * and the Eclipse Distribution License is available at - * http://www.eclipse.org/org/documents/edl-v10.html. - * - * Contributors: - * Sierra Wireless - initial API and implementation - *******************************************************************************/ -package org.eclipse.leshan.integration.tests; - -import java.util.concurrent.atomic.AtomicInteger; - -import org.eclipse.leshan.server.queue.PresenceListener; -import org.eclipse.leshan.server.registration.Registration; - -public class PresenceCounter implements PresenceListener { - private AtomicInteger nbAwake = new AtomicInteger(0); - private AtomicInteger nbSleeping = new AtomicInteger(0); - - @Override - public void onAwake(Registration registration) { - if (accept(registration)) - nbAwake.incrementAndGet(); - } - - @Override - public void onSleeping(Registration registration) { - if (accept(registration)) - nbSleeping.incrementAndGet(); - } - - public void resetCounter() { - nbAwake.set(0); - nbSleeping.set(0); - } - - public int getNbAwake() { - return nbAwake.get(); - } - - public int getNbSleeping() { - return nbSleeping.get(); - } - - public boolean accept(Registration registration) { - return true; - } -} 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..c1d563fa59 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; @@ -280,6 +281,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); @@ -295,10 +301,9 @@ 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) // + .hasCode(CONTENT) // + .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/create/CreateFailedTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/create/CreateFailedTest.java index 43a5b4f1aa..d1dcfdab06 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/create/CreateFailedTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/create/CreateFailedTest.java @@ -15,21 +15,17 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.create; +import static org.eclipse.leshan.core.ResponseCode.BAD_REQUEST; +import static org.eclipse.leshan.core.ResponseCode.NOT_FOUND; import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing; import static org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder.givenServerUsing; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import static org.junit.jupiter.params.provider.Arguments.arguments; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.stream.Stream; -import org.eclipse.californium.core.coap.Response; -import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.node.LwM2mResource; import org.eclipse.leshan.core.node.LwM2mSingleResource; @@ -103,9 +99,9 @@ public void cannot_create_mandatory_single_object(Protocol givenProtocol, String new CreateRequest(3, new LwM2mResource[] { LwM2mSingleResource.newStringResource(3, "v123") })); // verify result - assertEquals(ResponseCode.BAD_REQUEST, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(BAD_REQUEST) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -115,9 +111,9 @@ public void cannot_create_instance_of_security_object(Protocol givenProtocol, St new CreateRequest(0, new LwM2mResource[] { LwM2mSingleResource.newStringResource(0, "new.dest") })); // verify result - assertEquals(ResponseCode.NOT_FOUND, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -127,8 +123,8 @@ public void cannot_create_instance_of_absent_object(Protocol givenProtocol, Stri CreateResponse response = server.send(currentRegistration, new CreateRequest(50, new LwM2mResource[0])); // verify result - assertEquals(ResponseCode.NOT_FOUND, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/create/CreateTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/create/CreateTest.java index 7d60563bcf..e18fccb30b 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/create/CreateTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/create/CreateTest.java @@ -18,22 +18,21 @@ package org.eclipse.leshan.integration.tests.create; +import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.leshan.core.ResponseCode.BAD_REQUEST; +import static org.eclipse.leshan.core.ResponseCode.CONTENT; +import static org.eclipse.leshan.core.ResponseCode.CREATED; +import static org.eclipse.leshan.core.ResponseCode.NOT_FOUND; import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing; import static org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder.givenServerUsing; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertNotEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.stream.Stream; -import org.eclipse.californium.core.coap.Response; import org.eclipse.leshan.core.LwM2mId; -import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.node.LwM2mObject; import org.eclipse.leshan.core.node.LwM2mObjectInstance; @@ -71,9 +70,9 @@ public class CreateTest { static Stream transports() { - Object[][] transports = new Object[][] - // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - { { Protocol.COAP, "Californium", "Californium" } }; + Object[][] transports = new Object[][] { + // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider + { Protocol.COAP, "Californium", "Californium" } }; Object[] contentFormats = new Object[] { // ContentFormat.TLV, // @@ -127,27 +126,27 @@ public void can_create_instance_without_instance_id(ContentFormat contentFormat, new CreateRequest(contentFormat, 2, LwM2mSingleResource.newIntegerResource(3, 33))); // verify result - assertEquals(ResponseCode.CREATED, response.getCode()); - assertEquals("2/0", response.getLocation()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CREATED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); + assertThat(response.getLocation()).isEqualTo("2/0"); // create a second ACL instance response = server.send(currentRegistration, new CreateRequest(contentFormat, 2, LwM2mSingleResource.newIntegerResource(3, 34))); // verify result - assertEquals(ResponseCode.CREATED, response.getCode()); - assertEquals("2/1", response.getLocation()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CREATED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); + assertThat(response.getLocation()).isEqualTo("2/1"); // read object 2 ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(2)); - assertEquals(ResponseCode.CONTENT, readResponse.getCode()); + assertThat(readResponse).hasCode(CONTENT); LwM2mObject object = (LwM2mObject) readResponse.getContent(); - assertEquals(33l, object.getInstance(0).getResource(3).getValue()); - assertEquals(34l, object.getInstance(1).getResource(3).getValue()); + assertThat(object.getInstance(0).getResource(3).getValue()).isEqualTo(33l); + assertThat(object.getInstance(1).getResource(3).getValue()).isEqualTo(34l); } catch (InvalidRequestException e) { // only TLV support create instance without instance id assertNotEquals(ContentFormat.TLV, contentFormat); @@ -162,16 +161,16 @@ public void can_create_instance_with_id(ContentFormat contentFormat, Protocol gi CreateResponse response = server.send(currentRegistration, new CreateRequest(contentFormat, 2, instance)); // verify result - assertEquals(ResponseCode.CREATED, response.getCode()); - assertEquals(null, response.getLocation()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CREATED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); + assertThat(response.getLocation()).isNull(); // read object 2 ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(2)); - assertEquals(ResponseCode.CONTENT, readResponse.getCode()); + assertThat(readResponse).hasCode(CONTENT); LwM2mObject object = (LwM2mObject) readResponse.getContent(); - assertEquals(object.getInstance(12).getResource(3).getValue(), 123l); + assertThat(object.getInstance(12).getResource(3).getValue()).isEqualTo(123l); } @TestAllCases @@ -184,17 +183,17 @@ public void can_create_2_instances_of_object(ContentFormat contentFormat, Protoc new CreateRequest(contentFormat, 2, instance1, instance2)); // verify result - assertEquals(ResponseCode.CREATED, response.getCode()); - assertEquals(null, response.getLocation()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CREATED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); + assertThat(response.getLocation()).isNull(); // read object 2 ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(2)); - assertEquals(ResponseCode.CONTENT, readResponse.getCode()); + assertThat(readResponse).hasCode(CONTENT); LwM2mObject object = (LwM2mObject) readResponse.getContent(); - assertEquals(object.getInstance(12).getResource(3).getValue(), 123l); - assertEquals(object.getInstance(13).getResource(3).getValue(), 124l); + assertThat(object.getInstance(12).getResource(3).getValue()).isEqualTo(123l); + assertThat(object.getInstance(13).getResource(3).getValue()).isEqualTo(124l); } @TestAllCases @@ -206,9 +205,9 @@ public void cannot_create_instance_without_all_required_resources(ContentFormat LwM2mId.ACCESS_CONTROL, new LwM2mObjectInstance(0, new LwM2mResource[0]))); // verify result - assertEquals(ResponseCode.BAD_REQUEST, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(BAD_REQUEST) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // create ACL instance with only 1 mandatory resources (2 missing) CreateResponse response2 = server.send(currentRegistration, @@ -216,7 +215,9 @@ public void cannot_create_instance_without_all_required_resources(ContentFormat new LwM2mObjectInstance(0, LwM2mSingleResource.newIntegerResource(LwM2mId.ACL_OBJECT_ID, 12)))); // verify result - assertEquals(ResponseCode.BAD_REQUEST, response2.getCode()); + assertThat(response2) // + .hasCode(BAD_REQUEST) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // create 2 ACL instances, one with missing mandatory resource and the other with all mandatory resources LwM2mObjectInstance instance0 = new LwM2mObjectInstance(0, @@ -230,13 +231,15 @@ public void cannot_create_instance_without_all_required_resources(ContentFormat new CreateRequest(contentFormat, LwM2mId.ACCESS_CONTROL, instance0, instance1)); // verify result - assertEquals(ResponseCode.BAD_REQUEST, response3.getCode()); + assertThat(response3) // + .hasCode(BAD_REQUEST) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // try to read to check if the instance is not created // client registration ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(2, 0)); - assertEquals(ResponseCode.NOT_FOUND, readResponse.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(readResponse) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/lockstep/LockStepTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/lockstep/LockStepTest.java index e854c338c9..1764a10d3c 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/lockstep/LockStepTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/lockstep/LockStepTest.java @@ -81,7 +81,7 @@ public class LockStepTest { static Stream transports() { return Stream.of(// - // ProtocolUsed - Server Endpoint Provider + // Server Endpoint Provider arguments("Californium")); } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/ObserveCompositeTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/ObserveCompositeTest.java index 15cc767daa..405120b9bf 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/ObserveCompositeTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/ObserveCompositeTest.java @@ -16,14 +16,12 @@ package org.eclipse.leshan.integration.tests.observe; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; +import static org.eclipse.leshan.core.ResponseCode.CHANGED; +import static org.eclipse.leshan.core.ResponseCode.CONTENT; import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing; import static org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder.givenServerUsing; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import static org.junit.jupiter.params.provider.Arguments.arguments; import java.lang.annotation.Retention; @@ -34,8 +32,6 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Stream; -import org.eclipse.californium.core.coap.Response; -import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.node.LwM2mNode; import org.eclipse.leshan.core.node.LwM2mPath; @@ -121,37 +117,30 @@ public void can_composite_observe_on_single_resource(Protocol givenProtocol, Str new ObserveCompositeRequest(ContentFormat.SENML_JSON, ContentFormat.SENML_JSON, "/3/0/15")); // Assert that ObserveCompositeResponse is valid - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class))); - - CompositeObservation observation = observeResponse.getObservation(); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // Assert that CompositeObservation contains expected paths - assertNotNull(observation); - assertEquals(1, observation.getPaths().size()); - assertEquals("/3/0/15", observation.getPaths().get(0).toString()); + CompositeObservation observation = observeResponse.getObservation(); + assertThat(observation).isNotNull(); + assertThat(observation.getPaths()).containsOnly(new LwM2mPath("/3/0/15")); // Assert that there is one valid observation - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + server.waitForNewObservation(observation); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // Write single example value LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/Paris")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // Assert that response contains expected paths - server.waitForNewObservation(observation); ObserveCompositeResponse response = server.waitForNotificationOf(observation); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); - Map content = response.getContent(); - assertEquals(1, content.size()); - assertTrue(content.containsKey(new LwM2mPath("/3/0/15"))); - - // Assert that listener response contains expected values - assertEquals(LwM2mSingleResource.newStringResource(15, "Europe/Paris"), content.get(new LwM2mPath("/3/0/15"))); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); + assertThat(response.getContent()).containsOnly( + entry(new LwM2mPath("/3/0/15"), LwM2mSingleResource.newStringResource(15, "Europe/Paris"))); } @TestAllTransportLayer @@ -162,26 +151,24 @@ public void should_not_get_response_if_modified_other_resource_than_observed(Pro new ObserveCompositeRequest(ContentFormat.SENML_JSON, ContentFormat.SENML_JSON, "/3/0/14")); // Assert that ObserveCompositeResponse is valid - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class))); - CompositeObservation observation = observeResponse.getObservation(); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // Assert that CompositeObservation contains expected paths - assertNotNull(observation); - assertEquals(1, observation.getPaths().size()); - assertEquals("/3/0/14", observation.getPaths().get(0).toString()); + CompositeObservation observation = observeResponse.getObservation(); + assertThat(observation).isNotNull(); + assertThat(observation.getPaths()).containsOnly(new LwM2mPath("/3/0/14")); // Assert that there is one valid observation - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); - Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); server.waitForNewObservation(observation); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); + Set observations = server.getObservationService().getObservations(currentRegistration); + assertThat(observations).containsExactly(observation); // Write single example value LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/Paris")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // Assert that listener has no response server.ensureNoNotification(observation, 1, TimeUnit.SECONDS); @@ -195,42 +182,33 @@ public void can_composite_observe_on_multiple_resources(Protocol givenProtocol, new ObserveCompositeRequest(ContentFormat.SENML_JSON, ContentFormat.SENML_JSON, "/3/0/15", "/3/0/14")); // Assert that ObserveCompositeResponse is valid - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); LwM2mNode previousOffset = observeResponse.getContent("/3/0/14"); - CompositeObservation observation = observeResponse.getObservation(); - // Assert that CompositeObservation contains expected paths - assertNotNull(observation); - assertEquals(2, observation.getPaths().size()); - assertEquals("/3/0/15", observation.getPaths().get(0).toString()); - assertEquals("/3/0/14", observation.getPaths().get(1).toString()); + CompositeObservation observation = observeResponse.getObservation(); + assertThat(observation).isNotNull(); + assertThat(observation.getPaths()).containsOnly(new LwM2mPath("/3/0/15"), new LwM2mPath("/3/0/14")); // Assert that there is one valid observation - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + server.waitForNewObservation(observation); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // Write single example value LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/Paris")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // Assert that response contains exactly the same paths ObserveCompositeResponse response = server.waitForNotificationOf(observation); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); - Map content = response.getContent(); - assertEquals(2, content.size()); - assertTrue(content.containsKey(new LwM2mPath("/3/0/15"))); - assertTrue(content.containsKey(new LwM2mPath("/3/0/14"))); - - // Assert that listener response contains expected values - assertEquals(LwM2mSingleResource.newStringResource(new LwM2mPath("/3/0/15").getResourceId(), "Europe/Paris"), - content.get(new LwM2mPath("/3/0/15"))); - assertEquals(previousOffset, content.get(new LwM2mPath("/3/0/14"))); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); + assertThat(response.getContent()).containsOnly( + entry(new LwM2mPath("/3/0/15"), LwM2mSingleResource.newStringResource(15, "Europe/Paris")), + entry(new LwM2mPath("/3/0/14"), previousOffset)); } @TestAllTransportLayer @@ -241,23 +219,20 @@ public void can_composite_observe_on_multiple_resources_with_write_composite(Pro new ObserveCompositeRequest(ContentFormat.SENML_JSON, ContentFormat.SENML_JSON, "/3/0/15", "/3/0/14")); // Assert that ObserveCompositeResponse is valid - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class))); - - CompositeObservation observation = observeResponse.getObservation(); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // Assert that CompositeObservation contains expected paths - assertNotNull(observation); - assertEquals(2, observation.getPaths().size()); - assertEquals("/3/0/15", observation.getPaths().get(0).toString()); - assertEquals("/3/0/14", observation.getPaths().get(1).toString()); + CompositeObservation observation = observeResponse.getObservation(); + assertThat(observation).isNotNull(); + assertThat(observation.getPaths()).containsOnly(new LwM2mPath("/3/0/15"), new LwM2mPath("/3/0/14")); // Assert that there is one valid observation - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + server.waitForNewObservation(observation); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // Write example composite values Map nodes = new HashMap<>(); @@ -265,22 +240,14 @@ public void can_composite_observe_on_multiple_resources_with_write_composite(Pro nodes.put("/3/0/14", "+11"); WriteCompositeResponse writeResponse = server.send(currentRegistration, new WriteCompositeRequest(ContentFormat.SENML_JSON, nodes)); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // Assert that response contains expected paths - server.waitForNewObservation(observation); ObserveCompositeResponse response = server.waitForNotificationOf(observation); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); - Map content = response.getContent(); - assertEquals(2, content.size()); - assertTrue(content.containsKey(new LwM2mPath("/3/0/15"))); - assertTrue(content.containsKey(new LwM2mPath("/3/0/14"))); - - // Assert that listener response contains expected values - assertEquals(LwM2mSingleResource.newStringResource(new LwM2mPath("/3/0/15").getResourceId(), "Europe/Paris"), - content.get(new LwM2mPath("/3/0/15"))); - assertEquals(LwM2mSingleResource.newStringResource(new LwM2mPath("/3/0/14").getResourceId(), "+11"), - content.get(new LwM2mPath("/3/0/14"))); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); + assertThat(response.getContent()).containsOnly( + entry(new LwM2mPath("/3/0/15"), LwM2mSingleResource.newStringResource(15, "Europe/Paris")), + entry(new LwM2mPath("/3/0/14"), LwM2mSingleResource.newStringResource(14, "+11"))); } @TestAllTransportLayer @@ -291,38 +258,31 @@ public void can_observe_instance(Protocol givenProtocol, String givenClientEndpo new ObserveCompositeRequest(ContentFormat.SENML_JSON, ContentFormat.SENML_JSON, "/3/0")); // Assert that ObserveCompositeResponse is valid - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class))); - - CompositeObservation observation = observeResponse.getObservation(); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // Assert that CompositeObservation contains expected paths - assertNotNull(observation); - assertEquals(1, observation.getPaths().size()); - assertEquals("/3/0", observation.getPaths().get(0).toString()); + CompositeObservation observation = observeResponse.getObservation(); + assertThat(observation).isNotNull(); + assertThat(observation.getPaths()).containsOnly(new LwM2mPath("/3/0")); // Assert that there is one valid observation - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + server.waitForNewObservation(observation); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // Write single example value LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/Paris")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // Assert that response contains expected paths - server.waitForNewObservation(observation); ObserveCompositeResponse response = server.waitForNotificationOf(observation); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); - Map content = response.getContent(); - assertEquals(1, content.size()); - assertTrue(content.containsKey(new LwM2mPath("/3/0"))); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); - // Assert that listener response equals to ReadResponse ReadResponse readResp = server.send(currentRegistration, new ReadRequest(ContentFormat.SENML_JSON, "/3/0")); - assertEquals(readResp.getContent(), content.get(new LwM2mPath("/3/0"))); + assertThat(response.getContent()).containsOnly(entry(new LwM2mPath("/3/0"), readResp.getContent())); } @TestAllTransportLayer @@ -333,38 +293,31 @@ public void can_observe_object(Protocol givenProtocol, String givenClientEndpoin new ObserveCompositeRequest(ContentFormat.SENML_JSON, ContentFormat.SENML_JSON, "/3")); // Assert that ObserveCompositeResponse is valid - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class))); - - CompositeObservation observation = observeResponse.getObservation(); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // Assert that CompositeObservation contains expected paths - assertNotNull(observation); - assertEquals(1, observation.getPaths().size()); - assertEquals("/3", observation.getPaths().get(0).toString()); + CompositeObservation observation = observeResponse.getObservation(); + assertThat(observation).isNotNull(); + assertThat(observation.getPaths()).containsOnly(new LwM2mPath("/3")); // Assert that there is one valid observation - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + server.waitForNewObservation(observation); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // Write single example value LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/Paris")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // Assert that response contains expected paths - server.waitForNewObservation(observation); ObserveCompositeResponse response = server.waitForNotificationOf(observation); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); - Map content = response.getContent(); - assertEquals(1, content.size()); - assertTrue(content.containsKey(new LwM2mPath("/3"))); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); - // Assert that listener response equals to ReadResponse ReadResponse readResp = server.send(currentRegistration, new ReadRequest(ContentFormat.SENML_JSON, "/3")); - assertEquals(readResp.getContent(), content.get(new LwM2mPath("/3"))); + assertThat(response.getContent()).containsOnly(entry(new LwM2mPath("/3"), readResp.getContent())); } @TestAllTransportLayer @@ -378,18 +331,18 @@ public void can_passive_cancel_composite_observation(Protocol givenProtocol, Str // Write single example value LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/Paris")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); server.waitForNotificationOf(observation); // cancel observation : passive way server.getObservationService().cancelObservation(observation); Set observations = server.getObservationService().getObservations(currentRegistration); - assertTrue(observations.isEmpty(), "Observation should be removed"); + assertThat(observations).isEmpty(); server.waitForCancellationOf(observation, 500, TimeUnit.MILLISECONDS); // Write single value writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/London")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); server.ensureNoNotification(observation, 500, TimeUnit.MILLISECONDS); } @@ -405,36 +358,30 @@ public void can_active_cancel_composite_observation(Protocol givenProtocol, Stri // Write single example value LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/Paris")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); - - // cancel observation : active way - CancelCompositeObservationResponse cancelResponse = server.send(currentRegistration, - new CancelCompositeObservationRequest(observation)); - assertTrue(cancelResponse.isSuccess()); - assertEquals(ResponseCode.CONTENT, cancelResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // Assert that response contains exactly the same paths ObserveCompositeResponse response = server.waitForNotificationOf(observation); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); - Map content = response.getContent(); - assertEquals(1, content.size()); - assertTrue(content.containsKey(new LwM2mPath("/3/0/15"))); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); + assertThat(response.getContent()).containsOnly( + entry(new LwM2mPath("/3/0/15"), LwM2mSingleResource.newStringResource(15, "Europe/Paris"))); - // Assert that listener response contains exact values - assertEquals(LwM2mSingleResource.newStringResource(new LwM2mPath("/3/0/15").getResourceId(), "Europe/Paris"), - content.get(new LwM2mPath("/3/0/15"))); + // cancel observation : active way + CancelCompositeObservationResponse cancelResponse = server.send(currentRegistration, + new CancelCompositeObservationRequest(observation)); + assertThat(cancelResponse.isSuccess()).isTrue(); + assertThat(cancelResponse).hasCode(CONTENT); // active cancellation does not remove observation from store : it should be done manually using // ObservationService().cancelObservation(observation) // Assert that there is one valid observation - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/London")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); server.ensureNoNotification(observation, 500, TimeUnit.MILLISECONDS); } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/ObserveTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/ObserveTest.java index bf56ab0d25..e9c907ad0f 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/ObserveTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/ObserveTest.java @@ -20,10 +20,10 @@ package org.eclipse.leshan.integration.tests.observe; import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.leshan.core.ResponseCode.CHANGED; +import static org.eclipse.leshan.core.ResponseCode.CONTENT; import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import static org.junit.jupiter.params.provider.Arguments.arguments; import java.lang.annotation.Retention; @@ -32,9 +32,7 @@ import java.util.concurrent.TimeUnit; import java.util.stream.Stream; -import org.eclipse.californium.core.coap.Response; import org.eclipse.californium.elements.Connector; -import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.node.LwM2mObject; import org.eclipse.leshan.core.node.LwM2mObjectInstance; @@ -127,27 +125,26 @@ public void can_observe_resource(Protocol givenProtocol, String givenClientEndpo // observe device timezone ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(3, 0, 15)); - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse()).isInstanceOf(Response.class); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // an observation response should have been sent SingleObservation observation = observeResponse.getObservation(); - assertEquals("/3/0/15", observation.getPath().toString()); - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getPath()).asString().isEqualTo("/3/0/15"); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // write device timezone LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/Paris")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // verify result server.waitForNewObservation(observation); ObserveResponse response = server.waitForNotificationOf(observation); assertThat(response.getContent()).isEqualTo(LwM2mSingleResource.newStringResource(15, "Europe/Paris")); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -156,28 +153,27 @@ public void can_observe_resource_instance(Protocol givenProtocol, String givenCl // multi instance string String expectedPath = "/" + TestLwM2mId.TEST_OBJECT + "/0/" + TestLwM2mId.MULTIPLE_STRING_VALUE + "/0"; ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(expectedPath)); - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse()).isInstanceOf(Response.class); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // an observation response should have been sent SingleObservation observation = observeResponse.getObservation(); - assertEquals(expectedPath, observation.getPath().toString()); - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getPath()).asString().isEqualTo(expectedPath); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertTrue(observations.size() == 1, "We should have only on observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // write a new value LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(Mode.REPLACE, ContentFormat.TLV, expectedPath, LwM2mResourceInstance.newStringInstance(0, "a new string"))); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // verify result server.waitForNewObservation(observation); ObserveResponse response = server.waitForNotificationOf(observation); assertThat(response.getContent()).isEqualTo(LwM2mResourceInstance.newStringInstance(0, "a new string")); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -186,39 +182,38 @@ public void can_observe_resource_instance_then_passive_cancel(Protocol givenProt // multi instance string String expectedPath = "/" + TestLwM2mId.TEST_OBJECT + "/0/" + TestLwM2mId.MULTIPLE_STRING_VALUE + "/0"; ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(expectedPath)); - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse()).isInstanceOf(Response.class); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // an observation response should have been sent SingleObservation observation = observeResponse.getObservation(); - assertEquals(expectedPath, observation.getPath().toString()); - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getPath()).asString().isEqualTo(expectedPath); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertTrue(observations.size() == 1, "We should have only on observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // write a new value LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(Mode.REPLACE, ContentFormat.TLV, expectedPath, LwM2mResourceInstance.newStringInstance(0, "a new string"))); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // verify result server.waitForNewObservation(observation); ObserveResponse response = server.waitForNotificationOf(observation); assertThat(response.getContent()).isEqualTo(LwM2mResourceInstance.newStringInstance(0, "a new string")); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); // cancel observation : passive way server.getObservationService().cancelObservation(observation); server.waitForCancellationOf(observation, 500, TimeUnit.MILLISECONDS); observations = server.getObservationService().getObservations(currentRegistration); - assertTrue(observations.isEmpty(), "Observation should be removed"); + assertThat(observations).isEmpty(); // write device timezone writeResponse = server.send(currentRegistration, new WriteRequest(Mode.REPLACE, ContentFormat.TLV, expectedPath, LwM2mResourceInstance.newStringInstance(0, "a another new string"))); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // verify result server.ensureNoNotification(observation, 1, TimeUnit.SECONDS); @@ -231,45 +226,43 @@ public void can_observe_resource_instance_then_active_cancel(Protocol givenProto // multi instance string String expectedPath = "/" + TestLwM2mId.TEST_OBJECT + "/0/" + TestLwM2mId.MULTIPLE_STRING_VALUE + "/0"; ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(expectedPath)); - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse()).isInstanceOf(Response.class); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // an observation response should have been sent SingleObservation observation = observeResponse.getObservation(); - assertEquals(expectedPath, observation.getPath().toString()); - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getPath()).asString().isEqualTo(expectedPath); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertTrue(observations.size() == 1, "We should have only on observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // write a new value LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(Mode.REPLACE, ContentFormat.TLV, expectedPath, LwM2mResourceInstance.newStringInstance(0, "a new string"))); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // verify result server.waitForNewObservation(observation); ObserveResponse response = server.waitForNotificationOf(observation); assertThat(response.getContent()).isEqualTo(LwM2mResourceInstance.newStringInstance(0, "a new string")); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); // cancel observation : active way CancelObservationResponse cancelResponse = server.send(currentRegistration, new CancelObservationRequest(observation)); - assertTrue(cancelResponse.isSuccess()); - assertEquals(ResponseCode.CONTENT, cancelResponse.getCode()); - assertEquals("a new string", ((LwM2mResourceInstance) cancelResponse.getContent()).getValue()); + assertThat(cancelResponse.isSuccess()).isTrue(); + assertThat(cancelResponse).hasCode(CONTENT); + assertThat(((LwM2mResourceInstance) cancelResponse.getContent()).getValue()).isEqualTo("a new string"); // active cancellation does not remove observation from store : it should be done manually using // ObservationService().cancelObservation(observation) observations = server.getObservationService().getObservations(currentRegistration); - assertTrue(observations.size() == 1, "We should have only on observation"); - assertTrue(observations.contains(observation), "Observation should still be there"); + assertThat(observations).containsExactly(observation); // write device timezone writeResponse = server.send(currentRegistration, new WriteRequest(Mode.REPLACE, ContentFormat.TLV, expectedPath, LwM2mResourceInstance.newStringInstance(0, "a another new string"))); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // verify result server.ensureNoNotification(observation, 1, TimeUnit.SECONDS); @@ -281,37 +274,36 @@ public void can_observe_resource_then_passive_cancel(Protocol givenProtocol, Str // observe device timezone ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(3, 0, 15)); - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse()).isInstanceOf(Response.class); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // an observation response should have been sent SingleObservation observation = observeResponse.getObservation(); - assertEquals("/3/0/15", observation.getPath().toString()); - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getPath()).asString().isEqualTo("/3/0/15"); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // write device timezone LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/Paris")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // verify result server.waitForNewObservation(observation); ObserveResponse response = server.waitForNotificationOf(observation); assertThat(response.getContent()).isEqualTo(LwM2mSingleResource.newStringResource(15, "Europe/Paris")); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); // cancel observation : passive way server.getObservationService().cancelObservation(observation); server.waitForCancellationOf(observation, 500, TimeUnit.MILLISECONDS); observations = server.getObservationService().getObservations(currentRegistration); - assertTrue(observations.isEmpty(), "Observation should be removed"); + assertThat(observations).isEmpty(); // write device timezone writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/London")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // verify result server.ensureNoNotification(observation, 1, TimeUnit.SECONDS); @@ -323,43 +315,41 @@ public void can_observe_resource_then_active_cancel(Protocol givenProtocol, Stri // observe device timezone ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(3, 0, 15)); - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse()).isInstanceOf(Response.class); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // an observation response should have been sent SingleObservation observation = observeResponse.getObservation(); - assertEquals("/3/0/15", observation.getPath().toString()); - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getPath()).asString().isEqualTo("/3/0/15"); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // write device timezone LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/Paris")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // verify result server.waitForNewObservation(observation); ObserveResponse response = server.waitForNotificationOf(observation); assertThat(response.getContent()).isEqualTo(LwM2mSingleResource.newStringResource(15, "Europe/Paris")); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); // cancel observation : active way CancelObservationResponse cancelResponse = server.send(currentRegistration, new CancelObservationRequest(observation)); - assertTrue(cancelResponse.isSuccess()); - assertEquals(ResponseCode.CONTENT, cancelResponse.getCode()); - assertEquals("Europe/Paris", ((LwM2mSingleResource) cancelResponse.getContent()).getValue()); + assertThat(cancelResponse.isSuccess()).isTrue(); + assertThat(cancelResponse).hasCode(CONTENT); + assertThat(((LwM2mSingleResource) cancelResponse.getContent()).getValue()).isEqualTo("Europe/Paris"); // active cancellation does not remove observation from store : it should be done manually using // ObservationService().cancelObservation(observation) observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "Observation should still be there"); + assertThat(observations).containsExactly(observation); // write device timezone writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/London")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // verify result server.ensureNoNotification(observation, 1, TimeUnit.SECONDS); @@ -371,31 +361,30 @@ public void can_observe_instance(Protocol givenProtocol, String givenClientEndpo // observe device timezone ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(3, 0)); - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse()).isInstanceOf(Response.class); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // an observation response should have been sent SingleObservation observation = observeResponse.getObservation(); - assertEquals("/3/0", observation.getPath().toString()); - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getPath()).asString().isEqualTo("/3/0"); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // write device timezone LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/Paris")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // verify result server.waitForNewObservation(observation); ObserveResponse response = server.waitForNotificationOf(observation); assertThat(response.getContent()).isInstanceOf(LwM2mObjectInstance.class); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); // try to read the object instance for comparing ReadResponse readResp = server.send(currentRegistration, new ReadRequest(3, 0)); - assertEquals(readResp.getContent(), response.getContent()); + assertThat(response.getContent()).isEqualTo(readResp.getContent()); } @TestAllTransportLayer @@ -404,31 +393,30 @@ public void can_observe_object(Protocol givenProtocol, String givenClientEndpoin // observe device timezone ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(3)); - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse()).isInstanceOf(Response.class); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // an observation response should have been sent SingleObservation observation = observeResponse.getObservation(); - assertEquals("/3", observation.getPath().toString()); - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getPath()).asString().isEqualTo("/3"); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // write device timezone LwM2mResponse writeResponse = server.send(currentRegistration, new WriteRequest(3, 0, 15, "Europe/Paris")); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse).hasCode(CHANGED); // verify result server.waitForNewObservation(observation); ObserveResponse response = server.waitForNotificationOf(observation); assertThat(response.getContent()).isInstanceOf(LwM2mObject.class); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); // try to read the object for comparing ReadResponse readResp = server.send(currentRegistration, new ReadRequest(3)); - assertEquals(readResp.getContent(), response.getContent()); + assertThat(response.getContent()).isEqualTo(readResp.getContent()); } // TODO seems to be a server test only (lockstep client should be used) @@ -438,27 +426,26 @@ public void can_handle_error_on_notification(Protocol givenProtocol, String give // observe device timezone ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(3, 0, 15)); - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse()).isInstanceOf(Response.class); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // an observation response should have been sent SingleObservation observation = observeResponse.getObservation(); - assertEquals("/3/0/15", observation.getPath().toString()); - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getPath()).asString().isEqualTo("/3/0/15"); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertEquals(1, observations.size(), "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // *** HACK send a notification with unsupported content format *** // byte[] payload = new LwM2mNodeJsonEncoder().encode(LwM2mSingleResource.newStringResource(15, "Paris"), new LwM2mPath("/3/0/15"), client.getObjectTree().getModel(), new LwM2mValueChecker()); - Response firstCoapResponse = (Response) observeResponse.getCoapResponse(); + // 666 is not a supported content format. Connector connector = client .getClientConnector(client.getServerIdForRegistrationId("/rd/" + currentRegistration.getId())); TestObserveUtil.sendNotification(connector, server.getEndpoint(Protocol.COAP).getURI(), payload, - firstCoapResponse, ContentFormat.fromCode(666)); + observeResponse.getObservation().getId().getBytes(), 2, ContentFormat.fromCode(666)); // *** Hack End *** // // verify result diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/ObserveTimeStampTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/ObserveTimeStampTest.java index 2922214e18..db688e0598 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/ObserveTimeStampTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/ObserveTimeStampTest.java @@ -16,15 +16,10 @@ package org.eclipse.leshan.integration.tests.observe; import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.leshan.core.ResponseCode.CONTENT; 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.TestUtil.assertContentFormat; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -34,8 +29,6 @@ import java.util.Set; import java.util.stream.Stream; -import org.eclipse.californium.core.coap.Response; -import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.node.LwM2mObject; import org.eclipse.leshan.core.node.LwM2mObjectInstance; @@ -67,7 +60,7 @@ public class ObserveTimeStampTest { /*---------------------------------/ * Parameterized Tests * -------------------------------*/ - @ParameterizedTest(name = "{0} over COAP - Client using Californium - Server using {3}") + @ParameterizedTest(name = "{0} over COAP - Client using Californium - Server using {1}") @MethodSource("transports") @Retention(RetentionPolicy.RUNTIME) private @interface TestAllCases { @@ -75,9 +68,9 @@ public class ObserveTimeStampTest { static Stream transports() { - Object[][] transports = new Object[][] - // Server Endpoint Provider - { { "Californium" } }; + Object[][] transports = new Object[][] { + // Server Endpoint Provider + { "Californium" } }; Object[] contentFormats = new Object[] { // ContentFormat.JSON, // @@ -125,18 +118,17 @@ public void stop() throws InterruptedException { public void can_observe_timestamped_resource(ContentFormat contentFormat, String givenServerEndpointProvider) throws InterruptedException { // observe device timezone - ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(3, 0, 15)); - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class))); + ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(contentFormat, 3, 0, 15)); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // an observation response should have been sent SingleObservation observation = observeResponse.getObservation(); - assertEquals("/3/0/15", observation.getPath().toString()); - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getPath()).asString().isEqualTo("/3/0/15"); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertTrue(observations.size() == 1, "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // *** HACK send time-stamped notification as Leshan client does not support it *** // // create time-stamped nodes @@ -148,37 +140,36 @@ public void can_observe_timestamped_resource(ContentFormat contentFormat, String LwM2mSingleResource.newStringResource(15, "Londres"))); byte[] payload = encoder.encodeTimestampedData(timestampedNodes, contentFormat, new LwM2mPath("/3/0/15"), client.getObjectTree().getModel()); - Response firstCoapResponse = (Response) observeResponse.getCoapResponse(); + TestObserveUtil.sendNotification( client.getClientConnector(client.getServerIdForRegistrationId("/rd/" + currentRegistration.getId())), - server.getEndpoint(Protocol.COAP).getURI(), payload, firstCoapResponse, contentFormat); + server.getEndpoint(Protocol.COAP).getURI(), payload, + observeResponse.getObservation().getId().getBytes(), 2, contentFormat); // *** Hack End *** // // verify result server.waitForNewObservation(observation); ObserveResponse response = server.waitForNotificationOf(observation); + assertThat(response).hasContentFormat(contentFormat, givenServerEndpointProvider); assertThat(response.getContent()).isEqualTo(mostRecentNode.getNode()); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); - assertEquals(timestampedNodes, response.getTimestampedLwM2mNode()); - assertContentFormat(contentFormat, response); + assertThat(response.getTimestampedLwM2mNode()).isEqualTo(timestampedNodes); } @TestAllCases public void can_observe_timestamped_instance(ContentFormat contentFormat, String givenServerEndpointProvider) throws InterruptedException { // observe device timezone - ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(3, 0)); - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class))); + ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(contentFormat, 3, 0)); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // an observation response should have been sent SingleObservation observation = observeResponse.getObservation(); - assertEquals("/3/0", observation.getPath().toString()); - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getPath()).asString().isEqualTo("/3/0"); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertTrue(observations.size() == 1, "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // *** HACK send time-stamped notification as Leshan client does not support it *** // // create time-stamped nodes @@ -190,37 +181,36 @@ public void can_observe_timestamped_instance(ContentFormat contentFormat, String new LwM2mObjectInstance(0, LwM2mSingleResource.newStringResource(15, "Londres")))); byte[] payload = encoder.encodeTimestampedData(timestampedNodes, contentFormat, new LwM2mPath("/3/0"), client.getObjectTree().getModel()); - Response firstCoapResponse = (Response) observeResponse.getCoapResponse(); + TestObserveUtil.sendNotification( client.getClientConnector(client.getServerIdForRegistrationId("/rd/" + currentRegistration.getId())), - server.getEndpoint(Protocol.COAP).getURI(), payload, firstCoapResponse, contentFormat); + server.getEndpoint(Protocol.COAP).getURI(), payload, + observeResponse.getObservation().getId().getBytes(), 2, contentFormat); // *** Hack End *** // // verify result server.waitForNewObservation(observation); ObserveResponse response = server.waitForNotificationOf(observation); + assertThat(response).hasContentFormat(contentFormat, givenServerEndpointProvider); assertThat(response.getContent()).isEqualTo(mostRecentNode.getNode()); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); - assertEquals(timestampedNodes, response.getTimestampedLwM2mNode()); - assertContentFormat(contentFormat, response); + assertThat(response.getTimestampedLwM2mNode()).isEqualTo(timestampedNodes); } @TestAllCases public void can_observe_timestamped_object(ContentFormat contentFormat, String givenServerEndpointProvider) throws InterruptedException { // observe device timezone - ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(3)); - assertEquals(ResponseCode.CONTENT, observeResponse.getCode()); - assertNotNull(observeResponse.getCoapResponse()); - assertThat(observeResponse.getCoapResponse(), is(instanceOf(Response.class))); + ObserveResponse observeResponse = server.send(currentRegistration, new ObserveRequest(contentFormat, 3)); + assertThat(observeResponse) // + .hasCode(CONTENT) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // an observation response should have been sent SingleObservation observation = observeResponse.getObservation(); - assertEquals("/3", observation.getPath().toString()); - assertEquals(currentRegistration.getId(), observation.getRegistrationId()); + assertThat(observation.getPath()).asString().isEqualTo("/3"); + assertThat(observation.getRegistrationId()).isEqualTo(currentRegistration.getId()); Set observations = server.getObservationService().getObservations(currentRegistration); - assertTrue(observations.size() == 1, "We should have only one observation"); - assertTrue(observations.contains(observation), "New observation is not there"); + assertThat(observations).containsExactly(observation); // *** HACK send time-stamped notification as Leshan client does not support it *** // // create time-stamped nodes @@ -233,18 +223,17 @@ public void can_observe_timestamped_object(ContentFormat contentFormat, String g byte[] payload = encoder.encodeTimestampedData(timestampedNodes, contentFormat, new LwM2mPath("/3"), client.getObjectTree().getModel()); - Response firstCoapResponse = (Response) observeResponse.getCoapResponse(); TestObserveUtil.sendNotification( client.getClientConnector(client.getServerIdForRegistrationId("/rd/" + currentRegistration.getId())), - server.getEndpoint(Protocol.COAP).getURI(), payload, firstCoapResponse, contentFormat); + server.getEndpoint(Protocol.COAP).getURI(), payload, + observeResponse.getObservation().getId().getBytes(), 2, contentFormat); // *** Hack End *** // // verify result server.waitForNewObservation(observation); ObserveResponse response = server.waitForNotificationOf(observation); + assertThat(response).hasContentFormat(contentFormat, givenServerEndpointProvider); assertThat(response.getContent()).isEqualTo(mostRecentNode.getNode()); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); - assertEquals(timestampedNodes, response.getTimestampedLwM2mNode()); - assertContentFormat(contentFormat, response); + assertThat(response.getTimestampedLwM2mNode()).isEqualTo(timestampedNodes); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/TestObserveUtil.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/TestObserveUtil.java index abc3d17029..68c8b9b56b 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/TestObserveUtil.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/observe/TestObserveUtil.java @@ -16,6 +16,7 @@ package org.eclipse.leshan.integration.tests.observe; import java.net.URI; +import java.util.Random; import org.eclipse.californium.core.coap.CoAP.Type; import org.eclipse.californium.core.coap.OptionSet; @@ -28,17 +29,18 @@ import org.eclipse.leshan.core.request.ContentFormat; public class TestObserveUtil { - public static void sendNotification(Connector connector, URI destination, byte[] payload, - Response firstCoapResponse, ContentFormat contentFormat) { + public static Random r = new Random(); + + public static void sendNotification(Connector connector, URI destination, byte[] payload, byte[] token, int observe, + ContentFormat contentFormat) { // create observe response Response response = new Response(org.eclipse.californium.core.coap.CoAP.ResponseCode.CONTENT); response.setType(Type.NON); response.setPayload(payload); - response.setMID(firstCoapResponse.getMID() + 1); - response.setToken(firstCoapResponse.getToken()); - OptionSet options = new OptionSet().setContentFormat(contentFormat.getCode()) - .setObserve(firstCoapResponse.getOptions().getObserve() + 1); + response.setMID(r.nextInt(Short.MAX_VALUE)); + response.setToken(token); + OptionSet options = new OptionSet().setContentFormat(contentFormat.getCode()).setObserve(observe); response.setOptions(options); EndpointContext context = new AddressEndpointContext(destination.getHost(), destination.getPort()); response.setDestinationContext(context); diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadCompositeTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadCompositeTest.java index d00a2c4ba3..11646e2109 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadCompositeTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadCompositeTest.java @@ -15,11 +15,11 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.read; +import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.leshan.core.ResponseCode.CONTENT; 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.TestUtil.assertContentFormat; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -116,17 +116,17 @@ public void can_read_resources(ContentFormat requestContentFormat, ContentFormat new ReadCompositeRequest(requestContentFormat, responseContentFormat, "/3/0/0", "/1/0/1")); // verify result - assertEquals(CONTENT, response.getCode()); - assertContentFormat(responseContentFormat, response); + assertThat(response) // + .hasCode(CONTENT) // + .hasContentFormat(responseContentFormat, givenServerEndpointProvider); LwM2mSingleResource resource = (LwM2mSingleResource) response.getContent("/3/0/0"); - assertEquals(0, resource.getId()); - assertEquals(Type.STRING, resource.getType()); + assertThat(resource.getId()).isEqualTo(0); + assertThat(resource.getType()).isEqualTo(Type.STRING); resource = (LwM2mSingleResource) response.getContent("/1/0/1"); - assertEquals(1, resource.getId()); - assertEquals(Type.INTEGER, resource.getType()); - + assertThat(resource.getId()).isEqualTo(1); + assertThat(resource.getType()).isEqualTo(Type.INTEGER); } @TestAllCases @@ -139,12 +139,13 @@ public void can_read_resource_instance(ContentFormat requestContentFormat, Conte new ReadCompositeRequest(requestContentFormat, responseContentFormat, path)); // verify result - assertEquals(CONTENT, response.getCode()); - assertContentFormat(responseContentFormat, response); + assertThat(response) // + .hasCode(CONTENT) // + .hasContentFormat(responseContentFormat, givenServerEndpointProvider); LwM2mResourceInstance resource = (LwM2mResourceInstance) response.getContent(path); - assertEquals(0, resource.getId()); - assertEquals(Type.STRING, resource.getType()); + assertThat(resource.getId()).isEqualTo(0); + assertThat(resource.getType()).isEqualTo(Type.STRING); } @@ -157,15 +158,16 @@ public void can_read_resource_and_instance(ContentFormat requestContentFormat, C new ReadCompositeRequest(requestContentFormat, responseContentFormat, "/3/0/0", "/1")); // verify result - assertEquals(CONTENT, response.getCode()); - assertContentFormat(responseContentFormat, response); + assertThat(response) // + .hasCode(CONTENT) // + .hasContentFormat(responseContentFormat, givenServerEndpointProvider); LwM2mSingleResource resource = (LwM2mSingleResource) response.getContent("/3/0/0"); - assertEquals(0, resource.getId()); - assertEquals(Type.STRING, resource.getType()); + assertThat(resource.getId()).isEqualTo(0); + assertThat(resource.getType()).isEqualTo(Type.STRING); LwM2mObject object = (LwM2mObject) response.getContent("/1"); - assertEquals(1, object.getId()); - assertEquals(1, object.getInstances().size()); + assertThat(object.getId()).isEqualTo(1); + assertThat(object.getInstances()).hasSize(1); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadFailedTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadFailedTest.java index 59a6a00365..812cd77bc5 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadFailedTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadFailedTest.java @@ -19,18 +19,13 @@ import static org.eclipse.leshan.core.ResponseCode.NOT_FOUND; import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing; import static org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder.givenServerUsing; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import static org.junit.jupiter.params.provider.Arguments.arguments; import java.lang.annotation.Retention; 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.ReadRequest; import org.eclipse.leshan.core.response.ReadResponse; @@ -101,9 +96,9 @@ public void cannot_read_non_readable_resource(Protocol givenProtocol, String giv ReadResponse response = server.send(currentRegistration, new ReadRequest(3, 0, 4)); // verify result - assertEquals(METHOD_NOT_ALLOWED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(METHOD_NOT_ALLOWED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -113,9 +108,9 @@ public void cannot_read_non_existent_object(Protocol givenProtocol, String given ReadResponse response = server.send(currentRegistration, new ReadRequest(50)); // verify result - assertEquals(NOT_FOUND, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -125,9 +120,9 @@ public void cannot_read_non_existent_instance(Protocol givenProtocol, String giv ReadResponse response = server.send(currentRegistration, new ReadRequest(3, 1)); // verify result - assertEquals(NOT_FOUND, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -137,9 +132,9 @@ public void cannot_read_non_existent_resource(Protocol givenProtocol, String giv ReadResponse response = server.send(currentRegistration, new ReadRequest(3, 0, 50)); // verify result - assertEquals(NOT_FOUND, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -149,8 +144,8 @@ public void cannot_read_security_resource(Protocol givenProtocol, String givenCl ReadResponse response = server.send(currentRegistration, new ReadRequest(0, 0, 0)); // verify result - assertEquals(NOT_FOUND, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadMultiValueTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadMultiValueTest.java index 7a5ce2155e..a932fdbe2d 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadMultiValueTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadMultiValueTest.java @@ -17,12 +17,11 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.read; +import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.leshan.core.ResponseCode.CONTENT; 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.TestUtil.assertContentFormat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -60,9 +59,9 @@ public class ReadMultiValueTest { static Stream transports() { - Object[][] transports = new Object[][] - // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - { { Protocol.COAP, "Californium", "Californium" } }; + Object[][] transports = new Object[][] { + // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider + { Protocol.COAP, "Californium", "Californium" } }; Object[] contentFormats = new Object[] { // ContentFormat.TLV, // @@ -115,13 +114,13 @@ public void can_read_empty_object(ContentFormat contentFormat, Protocol givenPro ReadResponse response = server.send(currentRegistration, new ReadRequest(contentFormat, 2)); // verify result - assertEquals(CONTENT, response.getCode()); - assertContentFormat(contentFormat, response); + assertThat(response) // + .hasCode(CONTENT) // + .hasContentFormat(contentFormat, givenServerEndpointProvider); LwM2mObject object = (LwM2mObject) response.getContent(); - assertEquals(2, object.getId()); - assertTrue(object.getInstances().isEmpty()); - + assertThat(object.getId()).isEqualTo(2); + assertThat(object.getInstances()).isEmpty(); } @TestAllCases @@ -131,14 +130,13 @@ public void can_read_object(ContentFormat contentFormat, Protocol givenProtocol, ReadResponse response = server.send(currentRegistration, new ReadRequest(contentFormat, 3)); // verify result - assertEquals(CONTENT, response.getCode()); - assertContentFormat(contentFormat, response); + assertThat(response) // + .hasCode(CONTENT) // + .hasContentFormat(contentFormat, givenServerEndpointProvider); LwM2mObject object = (LwM2mObject) response.getContent(); - assertEquals(3, object.getId()); - - LwM2mObjectInstance instance = object.getInstance(0); - assertEquals(0, instance.getId()); + assertThat(object.getId()).isEqualTo(3); + assertThat(object.getInstance(0).getId()).isEqualTo(0); } @TestAllCases @@ -148,10 +146,11 @@ public void can_read_object_instance(ContentFormat contentFormat, Protocol given ReadResponse response = server.send(currentRegistration, new ReadRequest(contentFormat, 3, 0)); // verify result - assertEquals(CONTENT, response.getCode()); - assertContentFormat(contentFormat, response); + assertThat(response) // + .hasCode(CONTENT) // + .hasContentFormat(contentFormat, givenServerEndpointProvider); LwM2mObjectInstance instance = (LwM2mObjectInstance) response.getContent(); - assertEquals(0, instance.getId()); + assertThat(instance.getId()).isEqualTo(0); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadOpaqueValueTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadOpaqueValueTest.java index dbdcb428f6..2226d4d35c 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadOpaqueValueTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadOpaqueValueTest.java @@ -15,12 +15,11 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.read; +import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.leshan.core.ResponseCode.CONTENT; 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.TestUtil.assertContentFormat; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -61,9 +60,9 @@ public class ReadOpaqueValueTest { static Stream transports() { - Object[][] transports = new Object[][] - // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - { { Protocol.COAP, "Californium", "Californium" } }; + Object[][] transports = new Object[][] { + // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider + { Protocol.COAP, "Californium", "Californium" } }; Object[] contentFormats = new Object[] { // ContentFormat.OPAQUE, // @@ -117,18 +116,19 @@ public void can_read_empty_opaque_resource(ContentFormat contentFormat, Protocol // clear value of TEST object ExecuteResponse clearResponse = server.send(currentRegistration, new ExecuteRequest(TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.CLEAR_VALUES)); - assertTrue(clearResponse.isSuccess()); + assertThat(clearResponse.isSuccess()).isTrue(); // read device model number ReadResponse response = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.OPAQUE_VALUE)); // verify result - assertEquals(CONTENT, response.getCode()); - assertContentFormat(contentFormat, response); + assertThat(response) // + .hasCode(CONTENT) // + .hasContentFormat(contentFormat, givenServerEndpointProvider); LwM2mResource resource = (LwM2mResource) response.getContent(); - assertEquals(Type.OPAQUE, resource.getType()); - assertEquals(0, ((byte[]) resource.getValue()).length); + assertThat(resource.getType()).isEqualTo(Type.OPAQUE); + assertThat((byte[]) resource.getValue()).isEmpty(); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadSingleValueTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadSingleValueTest.java index c9f399f742..9b57fea411 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadSingleValueTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/read/ReadSingleValueTest.java @@ -15,12 +15,12 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.read; +import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.leshan.core.ResponseCode.BAD_REQUEST; import static org.eclipse.leshan.core.ResponseCode.CONTENT; 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.TestUtil.assertContentFormat; -import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -60,9 +60,9 @@ public class ReadSingleValueTest { static Stream transports() { - Object[][] transports = new Object[][] - // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - { { Protocol.COAP, "Californium", "Californium" } }; + Object[][] transports = new Object[][] { + // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider + { Protocol.COAP, "Californium", "Californium" } }; Object[] contentFormats = new Object[] { // ContentFormat.TEXT, // @@ -116,13 +116,13 @@ public void can_read_resource(ContentFormat contentFormat, Protocol givenProtoco ReadResponse response = server.send(currentRegistration, new ReadRequest(contentFormat, 3, 0, 1)); // verify result - assertEquals(CONTENT, response.getCode()); - assertContentFormat(contentFormat, response); + assertThat(response) // + .hasCode(CONTENT) // + .hasContentFormat(contentFormat, givenServerEndpointProvider); LwM2mResource resource = (LwM2mResource) response.getContent(); - assertEquals(1, resource.getId()); - assertEquals("IT-TEST-123", resource.getValue()); - + assertThat(resource.getId()).isEqualTo(1); + assertThat(resource.getValue()).isEqualTo("IT-TEST-123"); } @TestAllCases @@ -133,12 +133,13 @@ public void can_read_resource_instance(ContentFormat contentFormat, Protocol giv new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_STRING_VALUE, 0)); // verify result - assertEquals(CONTENT, response.getCode()); - assertContentFormat(contentFormat, response); + assertThat(response) // + .hasCode(CONTENT) // + .hasContentFormat(contentFormat, givenServerEndpointProvider); LwM2mResourceInstance resourceInstance = (LwM2mResourceInstance) response.getContent(); - assertEquals(0, resourceInstance.getId()); - assertEquals(LwM2mTestObject.INITIAL_STRING_VALUE, resourceInstance.getValue()); + assertThat(resourceInstance.getId()).isEqualTo(0); + assertThat(resourceInstance.getValue()).isEqualTo(LwM2mTestObject.INITIAL_STRING_VALUE); } @TestAllCases @@ -149,7 +150,8 @@ public void cannot_read_non_multiple_resource_instance(ContentFormat contentForm new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.INTEGER_VALUE, 0)); // verify result - assertEquals(BAD_REQUEST, response.getCode()); - assertEquals("invalid path : resource is not multiple", response.getErrorMessage()); + assertThat(response) // + .hasCode(BAD_REQUEST) // + .hasErrorMessage("invalid path : resource is not multiple"); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/send/SendTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/send/SendTest.java index b03187aafa..14b03f2ecb 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/send/SendTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/send/SendTest.java @@ -17,8 +17,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.never; @@ -49,7 +47,6 @@ import org.eclipse.leshan.integration.tests.util.assertion.Assertions; import org.eclipse.leshan.integration.tests.util.junit5.extensions.ArgumentsUtil; import org.eclipse.leshan.integration.tests.util.junit5.extensions.BeforeEachParameterizedResolver; -import org.eclipse.leshan.server.send.SendListener; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.extension.ExtendWith; @@ -71,9 +68,9 @@ public class SendTest { static Stream transports() { - Object[][] transports = new Object[][] - // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - { { Protocol.COAP, "Californium", "Californium" } }; + Object[][] transports = new Object[][] { + // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider + { Protocol.COAP, "Californium", "Californium" } }; Object[] contentFormats = new Object[] { // ContentFormat.SENML_JSON, // @@ -120,26 +117,22 @@ public void stop() throws InterruptedException { public void can_send_resources(ContentFormat contentformat, Protocol givenProtocol, String givenClientEndpointProvider, String givenServerEndpointProvider) throws InterruptedException, TimeoutException { - // Define send listener - SendListener listener = mock(SendListener.class); - server.getSendService().addListener(listener); - // Send Data ServerIdentity serverIdentity = client.getRegisteredServers().values().iterator().next(); SendResponse response = client.getSendService().sendData(serverIdentity, contentformat, Arrays.asList("/3/0/1", "/3/0/2"), 1000); - assertTrue(response.isSuccess()); + assertThat(response.isSuccess()).isTrue(); // wait for data and check result TimestampedLwM2mNodes data = server.waitForData(client.getEndpointName(), 1, TimeUnit.SECONDS); Map nodes = data.getNodes(); LwM2mResource modelnumber = (LwM2mResource) nodes.get(new LwM2mPath("/3/0/1")); - assertEquals(modelnumber.getId(), 1); - assertEquals(modelnumber.getValue(), "IT-TEST-123"); + assertThat(modelnumber.getId()).isEqualTo(1); + assertThat(modelnumber.getValue()).isEqualTo("IT-TEST-123"); LwM2mResource serialnumber = (LwM2mResource) nodes.get(new LwM2mPath("/3/0/2")); - assertEquals(serialnumber.getId(), 2); - assertEquals(serialnumber.getValue(), "12345"); + assertThat(serialnumber.getId()).isEqualTo(2); + assertThat(serialnumber.getValue()).isEqualTo("12345"); } @TestAllCases @@ -164,12 +157,12 @@ public void can_send_resources_asynchronously(ContentFormat contentformat, Proto TimestampedLwM2mNodes data = server.waitForData(client.getEndpointName(), 1, TimeUnit.SECONDS); Map nodes = data.getNodes(); LwM2mResource modelnumber = (LwM2mResource) nodes.get(new LwM2mPath("/3/0/1")); - assertEquals(modelnumber.getId(), 1); - assertEquals(modelnumber.getValue(), "IT-TEST-123"); + assertThat(modelnumber.getId()).isEqualTo(1); + assertThat(modelnumber.getValue()).isEqualTo("IT-TEST-123"); LwM2mResource serialnumber = (LwM2mResource) nodes.get(new LwM2mPath("/3/0/2")); - assertEquals(serialnumber.getId(), 2); - assertEquals(serialnumber.getValue(), "12345"); + assertThat(serialnumber.getId()).isEqualTo(2); + assertThat(serialnumber.getValue()).isEqualTo("12345"); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/send/SendTimestampedTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/send/SendTimestampedTest.java index 1dcffb4af8..0ccdeda858 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/send/SendTimestampedTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/send/SendTimestampedTest.java @@ -15,11 +15,10 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.send; +import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing; import static org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder.givenServerUsing; -import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -65,9 +64,9 @@ public class SendTimestampedTest { static Stream transports() { - Object[][] transports = new Object[][] - // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - { { Protocol.COAP, "Californium", "Californium" } }; + Object[][] transports = new Object[][] { + // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider + { Protocol.COAP, "Californium", "Californium" } }; Object[] contentFormats = new Object[] { // ContentFormat.SENML_JSON, // @@ -121,17 +120,17 @@ public void server_handle_multiple_timestamped_node(ContentFormat contentFormat, TimestampedLwM2mNodes data = server.waitForData(client.getEndpointName(), 1, TimeUnit.SECONDS); // Verify SendListener data received - assertEquals(2, data.getTimestamps().size()); + assertThat(data.getTimestamps()).hasSize(2); for (Instant ts : data.getTimestamps()) { assertNotNull(ts); Map pathNodeMap = data.getNodesAt(ts); - assertTrue(pathNodeMap.containsKey(getExamplePath())); + assertThat(pathNodeMap).containsKey(getExamplePath()); LwM2mNode node = pathNodeMap.get(getExamplePath()); - assertEquals(TestLwM2mId.FLOAT_VALUE, node.getId()); - assertTrue(node instanceof LwM2mSingleResource); - assertTrue(((LwM2mSingleResource) node).getType() == Type.FLOAT); + assertThat(node.getId()).isEqualTo(TestLwM2mId.FLOAT_VALUE); + assertThat(node).isInstanceOfSatisfying(LwM2mSingleResource.class, + n -> assertThat(n.getType()).isEqualTo(Type.FLOAT)); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/TestUtil.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/TestUtil.java deleted file mode 100644 index 04d0093a1b..0000000000 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/TestUtil.java +++ /dev/null @@ -1,37 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2020 Sierra Wireless and others. - * - * All rights reserved. This program and the accompanying materials - * are made available under the terms of the Eclipse Public License v2.0 - * and Eclipse Distribution License v1.0 which accompany this distribution. - * - * The Eclipse Public License is available at - * http://www.eclipse.org/legal/epl-v20.html - * and the Eclipse Distribution License is available at - * http://www.eclipse.org/org/documents/edl-v10.html. - * - * Contributors: - * Sierra Wireless - initial API and implementation - *******************************************************************************/ -package org.eclipse.leshan.integration.tests.util; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertTrue; - -import org.eclipse.californium.core.coap.Response; -import org.eclipse.leshan.core.request.ContentFormat; -import org.eclipse.leshan.core.response.LwM2mResponse; - -public class TestUtil { - public static void assertContentFormat(ContentFormat expectedContentFormat, LwM2mResponse response) { - assertNotNull(response, "response must not be null"); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); - Response coapResponse = (Response) response.getCoapResponse(); - assertTrue(coapResponse.getOptions().hasContentFormat(), "response must have content format"); - assertEquals(expectedContentFormat.getCode(), coapResponse.getOptions().getContentFormat()); - } -} diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/AbstractLwM2mResponseAssert.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/AbstractLwM2mResponseAssert.java new file mode 100644 index 0000000000..2147e31a6b --- /dev/null +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/AbstractLwM2mResponseAssert.java @@ -0,0 +1,86 @@ +/******************************************************************************* + * Copyright (c) 2023 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.html. + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.integration.tests.util.assertion; + +import org.assertj.core.api.AbstractObjectAssert; +import org.assertj.core.api.ObjectAssert; +import org.eclipse.californium.core.coap.Response; +import org.eclipse.leshan.core.ResponseCode; +import org.eclipse.leshan.core.request.ContentFormat; +import org.eclipse.leshan.core.response.LwM2mResponse; + +public abstract class AbstractLwM2mResponseAssert, ACTUAL extends LwM2mResponse> + extends AbstractObjectAssert { + + protected AbstractLwM2mResponseAssert(ACTUAL actual, Class selfType) { + super(actual, selfType); + } + + @SuppressWarnings("unchecked") + protected SELF mySelf() { + return (SELF) this; + } + + public SELF hasCode(ResponseCode expectedCode) { + isNotNull(); + + if (actual.getCode() == null) { + failWithMessage("Response MUST NOT have response code"); + } + if (!actual.getCode().equals(expectedCode)) { + failWithMessage("Expected <%s> ResponseCode for <%s> response", expectedCode, actual); + } + return mySelf(); + } + + public SELF hasValidUnderlyingResponseFor(String givenEndpointProvider) { + ObjectAssert assertThatUnderlyingResponse = Assertions.assertThat(actual.getCoapResponse()) + .as("Underlying Response"); + switch (givenEndpointProvider) { + case "Californium": + case "Californium-OSCORE": + assertThatUnderlyingResponse.isExactlyInstanceOf(Response.class); + break; + default: + throw new IllegalStateException(String.format("Unsupported endpoint provider : %s", givenEndpointProvider)); + } + return mySelf(); + } + + public SELF hasContentFormat(ContentFormat format, String givenEndpointProvider) { + ObjectAssert assertThatUnderlyingResponse = Assertions.assertThat(actual.getCoapResponse()) + .as("Underlying Response"); + switch (givenEndpointProvider) { + case "Californium": + case "Californium-OSCORE": + assertThatUnderlyingResponse.isInstanceOfSatisfying(Response.class, r -> { + Assertions.assertThat(r.getOptions().hasContentFormat()); + Assertions.assertThat(r.getOptions().getContentFormat()).as("Content Format") + .isEqualTo(format.getCode()); + }); + break; + default: + throw new IllegalStateException(String.format("Unsupported endpoint provider : %s", givenEndpointProvider)); + } + return mySelf(); + } + + public SELF hasErrorMessage(String expectedErrorMessage) { + isNotNull(); + Assertions.assertThat(actual.getErrorMessage()).as("Error Message").isEqualTo(expectedErrorMessage); + return mySelf(); + } +} diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/Assertions.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/Assertions.java index 1a923b326b..a5907e179c 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/Assertions.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/Assertions.java @@ -19,6 +19,7 @@ import org.assertj.core.matcher.AssertionMatcher; import org.eclipse.leshan.core.link.Link; +import org.eclipse.leshan.core.response.DiscoverResponse; import org.eclipse.leshan.core.response.LwM2mResponse; import org.eclipse.leshan.integration.tests.util.LeshanTestClient; import org.mockito.hamcrest.MockitoHamcrest; @@ -42,6 +43,10 @@ public static LwM2mResponseAssert assertThat(LwM2mResponse actual) { return new LwM2mResponseAssert(actual); } + public static DiscoverResponseAssert assertThat(DiscoverResponse actual) { + return new DiscoverResponseAssert(actual); + } + public static LinkArrayAssert assertThat(Link[] actual) { return new LinkArrayAssert(actual); } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/DiscoverResponseAssert.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/DiscoverResponseAssert.java new file mode 100644 index 0000000000..d935ea5656 --- /dev/null +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/util/assertion/DiscoverResponseAssert.java @@ -0,0 +1,34 @@ +/******************************************************************************* + * Copyright (c) 2023 Sierra Wireless and others. + * + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v2.0 + * and Eclipse Distribution License v1.0 which accompany this distribution. + * + * The Eclipse Public License is available at + * http://www.eclipse.org/legal/epl-v20.html + * and the Eclipse Distribution License is available at + * http://www.eclipse.org/org/documents/edl-v10.html. + * + * Contributors: + * Sierra Wireless - initial API and implementation + *******************************************************************************/ +package org.eclipse.leshan.integration.tests.util.assertion; + +import org.eclipse.leshan.core.response.DiscoverResponse; + +public class DiscoverResponseAssert extends AbstractLwM2mResponseAssert { + + public DiscoverResponseAssert(DiscoverResponse actual) { + super(actual, DiscoverResponseAssert.class); + } + + public DiscoverResponseAssert hasObjectLinksLike(String links) { + LinkArrayAssert.assertThat(actual.getObjectLinks()).isLikeLwM2mLinks(links); + return this; + } + + public static DiscoverResponseAssert assertThat(DiscoverResponse actual) { + return new DiscoverResponseAssert(actual); + } +} 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..a6af6f7c8b 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 @@ -15,11 +15,9 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.util.assertion; -import org.assertj.core.api.AbstractAssert; -import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.response.LwM2mResponse; -public class LwM2mResponseAssert extends AbstractAssert { +public class LwM2mResponseAssert extends AbstractLwM2mResponseAssert { public LwM2mResponseAssert(LwM2mResponse actual) { super(actual, LwM2mResponseAssert.class); @@ -28,16 +26,4 @@ public LwM2mResponseAssert(LwM2mResponse actual) { public static LwM2mResponseAssert assertThat(LwM2mResponse actual) { return new LwM2mResponseAssert(actual); } - - public LwM2mResponseAssert hasCode(ResponseCode expectedCode) { - isNotNull(); - - if (actual.getCode() == null) { - failWithMessage("Response MUST NOT have response code"); - } - if (!actual.getCode().equals(expectedCode)) { - failWithMessage("Expected <%s> ResponseCode for <%s> response", expectedCode, actual); - } - return this; - } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteCompositeTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteCompositeTest.java index 53a1930600..7246e6c347 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteCompositeTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteCompositeTest.java @@ -16,8 +16,11 @@ package org.eclipse.leshan.integration.tests.write; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; +import static org.eclipse.leshan.core.ResponseCode.CHANGED; 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.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsInstanceOf.instanceOf; @@ -134,15 +137,15 @@ public void can_write_resources(ContentFormat contentFormat, Protocol givenProto new WriteCompositeRequest(contentFormat, nodes)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read resource to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(3, 0, 14)); - assertEquals("+02", ((LwM2mSingleResource) readResponse.getContent()).getValue()); + assertThat(((LwM2mSingleResource) readResponse.getContent()).getValue()).isEqualTo("+02"); readResponse = server.send(currentRegistration, new ReadRequest(1, 0, 2)); - assertEquals(100l, ((LwM2mSingleResource) readResponse.getContent()).getValue()); + assertThat(((LwM2mSingleResource) readResponse.getContent()).getValue()).isEqualTo(100l); } @TestAllCases @@ -164,16 +167,16 @@ public void can_write_resource_and_instance(ContentFormat contentFormat, Protoco new WriteCompositeRequest(contentFormat, nodes, null)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read resource to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(3, 0, 14)); - assertEquals(utcOffset, readResponse.getContent()); + assertThat(readResponse.getContent()).isEqualTo(utcOffset); readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, resourceInstancePath, null)); - assertEquals(testStringResourceInstance, readResponse.getContent()); + assertThat(readResponse.getContent()).isEqualTo(testStringResourceInstance); } @TestAllCases @@ -192,17 +195,18 @@ public void can_add_resource_instances(ContentFormat contentFormat, Protocol giv new WriteCompositeRequest(contentFormat, nodes, null)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read resource to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, resourceInstancePath.toResourcePath(), null)); LwM2mMultipleResource multiResource = (LwM2mMultipleResource) readResponse.getContent(); - assertEquals(2, multiResource.getInstances().size()); - assertEquals(testStringResourceInstance, - multiResource.getInstance(resourceInstancePath.getResourceInstanceId())); + + assertThat(multiResource.getInstances()) // + .hasSize(2) // + .containsEntry(resourceInstancePath.getResourceInstanceId(), testStringResourceInstance); } @TestAllCases @@ -226,17 +230,21 @@ public void can_observe_instance_with_composite_write(ContentFormat contentForma LwM2mResponse writeResponse = server.send(currentRegistration, new WriteCompositeRequest(ContentFormat.SENML_CBOR, nodes)); - assertEquals(ResponseCode.CHANGED, writeResponse.getCode()); + assertThat(writeResponse) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // verify result both resource must have new value server.waitForNewObservation(observation); ObserveResponse response = server.waitForNotificationOf(observation); - assertThat(response.getContent()).isInstanceOf(LwM2mObjectInstance.class); - assertThat(response.getCoapResponse()).isInstanceOf(Response.class); - - LwM2mObjectInstance instance = (LwM2mObjectInstance) response.getContent(); - assertEquals("+11", instance.getResource(14).getValue()); - assertEquals("Moon", instance.getResource(15).getValue()); + assertThat(response).hasValidUnderlyingResponseFor(givenServerEndpointProvider); + + assertThat(response.getContent()).isInstanceOfSatisfying(LwM2mObjectInstance.class, instance -> { + assertThat(instance.getResources()) // + .contains( // + entry(14, LwM2mSingleResource.newStringResource(14, "+11")), + entry(15, LwM2mSingleResource.newStringResource(15, "Moon"))); + }); // Ensure we received only one notification. server.ensureNoNotification(observation, 1, TimeUnit.SECONDS); diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteFailedTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteFailedTest.java index 66a1207c79..58ef6662ae 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteFailedTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteFailedTest.java @@ -15,21 +15,18 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.write; +import static org.eclipse.leshan.core.ResponseCode.BAD_REQUEST; +import static org.eclipse.leshan.core.ResponseCode.METHOD_NOT_ALLOWED; +import static org.eclipse.leshan.core.ResponseCode.NOT_FOUND; import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing; import static org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder.givenServerUsing; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import static org.junit.jupiter.params.provider.Arguments.arguments; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.stream.Stream; -import org.eclipse.californium.core.coap.Response; -import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.node.LwM2mResource; import org.eclipse.leshan.core.node.LwM2mSingleResource; @@ -104,9 +101,9 @@ public void cannot_write_non_writable_resource(Protocol givenProtocol, String gi WriteResponse response = server.send(currentRegistration, new WriteRequest(3, 0, 0, manufacturer)); // verify result - assertEquals(ResponseCode.METHOD_NOT_ALLOWED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(METHOD_NOT_ALLOWED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -117,9 +114,9 @@ public void cannot_write_security_resource(Protocol givenProtocol, String givenC WriteResponse response = server.send(currentRegistration, new WriteRequest(0, 0, 0, uri)); // verify result - assertEquals(ResponseCode.NOT_FOUND, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(NOT_FOUND) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } @TestAllTransportLayer @@ -132,8 +129,8 @@ public void cannot_write_replacing_incomplete_object_instance(Protocol givenProt new WriteRequest(Mode.REPLACE, 1, 0, lifetime, defaultMinPeriod)); // verify result - assertEquals(ResponseCode.BAD_REQUEST, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(BAD_REQUEST) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteMultiValueTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteMultiValueTest.java index 399827ed5d..bdbd570c78 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteMultiValueTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteMultiValueTest.java @@ -15,16 +15,13 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.write; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.entry; +import static org.eclipse.leshan.core.ResponseCode.CHANGED; import static org.eclipse.leshan.core.ResponseCode.CONTENT; 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.TestUtil.assertContentFormat; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; -import static org.junit.jupiter.api.Assertions.assertNull; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -32,13 +29,12 @@ import java.util.Map; import java.util.stream.Stream; -import org.eclipse.californium.core.coap.Response; -import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.model.ResourceModel.Type; import org.eclipse.leshan.core.node.LwM2mMultipleResource; import org.eclipse.leshan.core.node.LwM2mObjectInstance; import org.eclipse.leshan.core.node.LwM2mResource; +import org.eclipse.leshan.core.node.LwM2mResourceInstance; import org.eclipse.leshan.core.node.LwM2mSingleResource; import org.eclipse.leshan.core.node.ObjectLink; import org.eclipse.leshan.core.request.ContentFormat; @@ -74,9 +70,9 @@ public class WriteMultiValueTest { static Stream transports() { - Object[][] transports = new Object[][] - // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - { { Protocol.COAP, "Californium", "Californium" } }; + Object[][] transports = new Object[][] { + // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider + { Protocol.COAP, "Californium", "Californium" } }; Object[] contentFormats = new Object[] { // ContentFormat.TLV, // @@ -134,15 +130,17 @@ public void can_write_object_instance(ContentFormat contentFormat, Protocol give new WriteRequest(Mode.REPLACE, contentFormat, 3, 0, utcOffset, timeZone)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read the timezone to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(3, 0)); - LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent(); - assertEquals(utcOffset, instance.getResource(14)); - assertEquals(timeZone, instance.getResource(15)); + assertThat(readResponse.getContent()).isInstanceOfSatisfying(LwM2mObjectInstance.class, instance -> { + assertThat(instance.getResources()).contains( // + entry(14, utcOffset), // + entry(15, timeZone)); + }); } @TestAllCases @@ -152,9 +150,9 @@ public void can_write_replacing_object_instance(ContentFormat contentFormat, Pro WriteResponse response = server.send(currentRegistration, new WriteRequest(contentFormat, 1, 0, 3, 60)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // write server object LwM2mResource lifetime = LwM2mSingleResource.newIntegerResource(1, 120); @@ -165,18 +163,21 @@ public void can_write_replacing_object_instance(ContentFormat contentFormat, Pro defaultMinPeriod, notificationStoring, binding)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read the values to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(1, 0)); - LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent(); - assertEquals(lifetime, instance.getResource(1)); - assertEquals(defaultMinPeriod, instance.getResource(2)); - assertEquals(notificationStoring, instance.getResource(6)); - assertEquals(binding, instance.getResource(7)); - assertNull(instance.getResource(3)); // removed not contained optional writable resource + assertThat(readResponse.getContent()).isInstanceOfSatisfying(LwM2mObjectInstance.class, instance -> { + assertThat(instance.getResources()) // + .contains( // + entry(1, lifetime), // + entry(2, defaultMinPeriod), // + entry(6, notificationStoring), // + entry(7, binding)) // + .doesNotContainKey(3); // removed not contained optional writable resource + }); } @TestAllCases @@ -186,9 +187,10 @@ public void can_write_updating_object_instance(ContentFormat contentFormat, Prot WriteResponse response = server.send(currentRegistration, new WriteRequest(contentFormat, 1, 0, 3, 60)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); + // write server object LwM2mResource lifetime = LwM2mSingleResource.newIntegerResource(1, 120); LwM2mResource defaultMinPeriod = LwM2mSingleResource.newIntegerResource(2, 10); @@ -196,19 +198,19 @@ public void can_write_updating_object_instance(ContentFormat contentFormat, Prot new WriteRequest(Mode.UPDATE, contentFormat, 1, 0, lifetime, defaultMinPeriod)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read the values to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(1, 0)); - LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent(); - assertEquals(lifetime, instance.getResource(1)); - assertEquals(defaultMinPeriod, instance.getResource(2)); - // no resources are removed when updating - assertNotNull(instance.getResource(3)); - assertNotNull(instance.getResource(6)); - assertNotNull(instance.getResource(7)); + assertThat(readResponse.getContent()).isInstanceOfSatisfying(LwM2mObjectInstance.class, instance -> { + assertThat(instance.getResources()) // + .contains( // + entry(1, lifetime), // + entry(2, defaultMinPeriod)) // + .containsKeys(3, 6, 7); // no resources are removed when updating + }); } @TestAllCases @@ -225,22 +227,21 @@ public void can_write_multi_instance_objlnk_resource(ContentFormat contentFormat TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_OBJLINK_VALUE, neighbourCellReport, Type.OBJLNK)); // Verify Write result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // Reading back the written OBJLNK value ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_OBJLINK_VALUE)); - LwM2mMultipleResource resource = (LwM2mMultipleResource) readResponse.getContent(); - // verify read value - assertEquals(((ObjectLink) resource.getValue(0)).getObjectId(), 10245); - assertEquals(((ObjectLink) resource.getValue(0)).getObjectInstanceId(), 1); - assertEquals(((ObjectLink) resource.getValue(1)).getObjectId(), 10242); - assertEquals(((ObjectLink) resource.getValue(1)).getObjectInstanceId(), 2); - assertEquals(((ObjectLink) resource.getValue(2)).getObjectId(), 10244); - assertEquals(((ObjectLink) resource.getValue(2)).getObjectInstanceId(), 3); + assertThat(readResponse.getContent()).isInstanceOfSatisfying(LwM2mMultipleResource.class, instance -> { + assertThat(instance.getInstances()) // + .contains( // + entry(0, LwM2mResourceInstance.newObjectLinkInstance(0, new ObjectLink(10245, 1))), // + entry(1, LwM2mResourceInstance.newObjectLinkInstance(1, new ObjectLink(10242, 2))), // + entry(2, LwM2mResourceInstance.newObjectLinkInstance(2, new ObjectLink(10244, 3)))); + }); } @TestAllCases @@ -269,17 +270,19 @@ public void can_write_object_instance_with_empty_multi_resource(ContentFormat co TestLwM2mId.TEST_OBJECT, 0, stringMultiResource, intMultiResource)); // Verify Write result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // Reading back the instance ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(TestLwM2mId.TEST_OBJECT, 0)); - LwM2mObjectInstance instance = (LwM2mObjectInstance) readResponse.getContent(); - // verify read value - assertEquals(stringMultiResource, instance.getResource(TestLwM2mId.MULTIPLE_STRING_VALUE)); - assertEquals(intMultiResource, instance.getResource(TestLwM2mId.MULTIPLE_INTEGER_VALUE)); + assertThat(readResponse.getContent()).isInstanceOfSatisfying(LwM2mObjectInstance.class, instance -> { + assertThat(instance.getResources()) // + .contains( // + entry(TestLwM2mId.MULTIPLE_STRING_VALUE, stringMultiResource), // + entry(TestLwM2mId.MULTIPLE_INTEGER_VALUE, intMultiResource)); + }); // =============== Now try to replace with ================== // /3441/0/1110 : { 3 = "newString"} @@ -287,24 +290,25 @@ public void can_write_object_instance_with_empty_multi_resource(ContentFormat co // create initial value strings = new HashMap<>(); strings.put(3, "newString"); - stringMultiResource = LwM2mMultipleResource.newStringResource(TestLwM2mId.MULTIPLE_STRING_VALUE, strings); + LwM2mMultipleResource stringMultiResource2 = LwM2mMultipleResource + .newStringResource(TestLwM2mId.MULTIPLE_STRING_VALUE, strings); // Write new instance response = server.send(currentRegistration, - new WriteRequest(Mode.REPLACE, contentFormat, TestLwM2mId.TEST_OBJECT, 0, stringMultiResource)); + new WriteRequest(Mode.REPLACE, contentFormat, TestLwM2mId.TEST_OBJECT, 0, stringMultiResource2)); // Verify Write result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // Reading back the instance readResponse = server.send(currentRegistration, new ReadRequest(TestLwM2mId.TEST_OBJECT, 0)); - instance = (LwM2mObjectInstance) readResponse.getContent(); - - // verify read value - assertEquals(stringMultiResource, instance.getResource(TestLwM2mId.MULTIPLE_STRING_VALUE)); - assertNull(instance.getResource(TestLwM2mId.MULTIPLE_INTEGER_VALUE)); + assertThat(readResponse.getContent()).isInstanceOfSatisfying(LwM2mObjectInstance.class, instance -> { + assertThat(instance.getResources()) // + .contains(entry(TestLwM2mId.MULTIPLE_STRING_VALUE, stringMultiResource2)) // + .doesNotContainKey(TestLwM2mId.MULTIPLE_INTEGER_VALUE); + }); } @TestAllCases @@ -322,21 +326,25 @@ public void can_write_object_resource_instance(ContentFormat contentFormat, Prot TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_STRING_VALUE, values, Type.STRING)); // Verify Write result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read multi instance ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_STRING_VALUE)); // verify result - assertEquals(CONTENT, readResponse.getCode()); - assertContentFormat(contentFormat, readResponse); + assertThat(readResponse) // + .hasCode(CONTENT) // + .hasContentFormat(contentFormat, givenServerEndpointProvider); - LwM2mMultipleResource resourceInstance = (LwM2mMultipleResource) readResponse.getContent(); - assertEquals("value10", resourceInstance.getInstance(10).getValue()); - assertEquals("value20", resourceInstance.getInstance(20).getValue()); + assertThat(readResponse.getContent()).isInstanceOfSatisfying(LwM2mMultipleResource.class, instance -> { + assertThat(instance.getInstances()) // + .contains( // + entry(10, LwM2mResourceInstance.newStringInstance(10, "value10")), // + entry(20, LwM2mResourceInstance.newStringInstance(20, "value20"))); + }); // -------------------------------- // write (update) multi instance @@ -348,22 +356,26 @@ public void can_write_object_resource_instance(ContentFormat contentFormat, Prot TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_STRING_VALUE, newValues, Type.STRING)); // Verify Write result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read multi instance readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_STRING_VALUE)); // verify result - assertEquals(CONTENT, readResponse.getCode()); - assertContentFormat(contentFormat, readResponse); - - resourceInstance = (LwM2mMultipleResource) readResponse.getContent(); - assertEquals("value200", resourceInstance.getInstance(20).getValue()); - assertEquals("value30", resourceInstance.getInstance(30).getValue()); - assertEquals("value10", resourceInstance.getInstance(10).getValue()); + assertThat(readResponse) // + .hasCode(CONTENT) // + .hasContentFormat(contentFormat, givenServerEndpointProvider); + + assertThat(readResponse.getContent()).isInstanceOfSatisfying(LwM2mMultipleResource.class, instance -> { + assertThat(instance.getInstances()) // + .contains( // + entry(20, LwM2mResourceInstance.newStringInstance(20, "value200")), // + entry(30, LwM2mResourceInstance.newStringInstance(30, "value30")), // + entry(10, LwM2mResourceInstance.newStringInstance(10, "value10"))); + }); // -------------------------------- // write (replace) multi instance @@ -374,20 +386,23 @@ public void can_write_object_resource_instance(ContentFormat contentFormat, Prot TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_STRING_VALUE, newValues, Type.STRING)); // Verify Write result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read multi instance readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_STRING_VALUE)); // verify result - assertEquals(CONTENT, readResponse.getCode()); - assertContentFormat(contentFormat, readResponse); - - resourceInstance = (LwM2mMultipleResource) readResponse.getContent(); - assertEquals(1, resourceInstance.getInstances().size()); - assertEquals("value1", resourceInstance.getInstance(1).getValue()); + assertThat(readResponse) // + .hasCode(CONTENT) // + .hasContentFormat(contentFormat, givenServerEndpointProvider); + + assertThat(readResponse.getContent()).isInstanceOfSatisfying(LwM2mMultipleResource.class, instance -> { + assertThat(instance.getInstances()) // + .containsExactly( // + entry(1, LwM2mResourceInstance.newStringInstance(1, "value1"))); + }); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteOpaqueValueTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteOpaqueValueTest.java index 1e12de4ac9..e646b03c99 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteOpaqueValueTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteOpaqueValueTest.java @@ -15,14 +15,11 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.write; +import static org.assertj.core.api.Assertions.assertThat; +import static org.eclipse.leshan.core.ResponseCode.CHANGED; import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing; import static org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder.givenServerUsing; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; @@ -30,8 +27,6 @@ import java.util.Map; import java.util.stream.Stream; -import org.eclipse.californium.core.coap.Response; -import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.model.ResourceModel.Type; import org.eclipse.leshan.core.node.LwM2mResource; @@ -68,9 +63,9 @@ public class WriteOpaqueValueTest { static Stream transports() { - Object[][] transports = new Object[][] - // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - { { Protocol.COAP, "Californium", "Californium" } }; + Object[][] transports = new Object[][] { + // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider + { Protocol.COAP, "Californium", "Californium" } }; Object[] contentFormats = new Object[] { // ContentFormat.OPAQUE, // @@ -130,15 +125,16 @@ public void write_opaque_resource(ContentFormat contentFormat, Protocol givenPro new WriteRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.OPAQUE_VALUE, expectedvalue)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read resource to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.OPAQUE_VALUE)); + LwM2mResource resource = (LwM2mResource) readResponse.getContent(); - assertArrayEquals(expectedvalue, (byte[]) resource.getValue()); + assertThat((byte[]) resource.getValue()).isEqualTo(expectedvalue); } @TestAllCases @@ -155,21 +151,21 @@ public void write_opaque_resource_instance(ContentFormat contentFormat, Protocol TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_OPAQUE_VALUE, values, Type.OPAQUE)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read first instance using parameter content format ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_OPAQUE_VALUE, 2)); LwM2mResourceInstance resource = (LwM2mResourceInstance) readResponse.getContent(); - assertArrayEquals(firstExpectedvalue, (byte[]) resource.getValue()); + assertThat((byte[]) resource.getValue()).isEqualTo(firstExpectedvalue); // read second instance using parameter content format readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_OPAQUE_VALUE, 3)); resource = (LwM2mResourceInstance) readResponse.getContent(); - assertArrayEquals(secondExpectedvalue, (byte[]) resource.getValue()); + assertThat((byte[]) resource.getValue()).isEqualTo(secondExpectedvalue); // write second resource instance using parameter content format byte[] newExpectedvalue = new byte[] { 7, 8, 9, 10 }; @@ -177,14 +173,14 @@ public void write_opaque_resource_instance(ContentFormat contentFormat, Protocol TestLwM2mId.MULTIPLE_OPAQUE_VALUE, 3, newExpectedvalue, Type.OPAQUE)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read second instance using parameter content format readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_OPAQUE_VALUE, 3)); resource = (LwM2mResourceInstance) readResponse.getContent(); - assertArrayEquals(newExpectedvalue, (byte[]) resource.getValue()); + assertThat((byte[]) resource.getValue()).isEqualTo(newExpectedvalue); } } diff --git a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteSingleValueTest.java b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteSingleValueTest.java index 7ba83b000c..3b4ec2dc15 100644 --- a/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteSingleValueTest.java +++ b/leshan-integration-tests/src/test/java/org/eclipse/leshan/integration/tests/write/WriteSingleValueTest.java @@ -15,15 +15,11 @@ *******************************************************************************/ package org.eclipse.leshan.integration.tests.write; +import static org.assertj.core.api.Assertions.assertThat; import static org.eclipse.leshan.core.ResponseCode.CHANGED; import static org.eclipse.leshan.integration.tests.util.LeshanTestClientBuilder.givenClientUsing; import static org.eclipse.leshan.integration.tests.util.LeshanTestServerBuilder.givenServerUsing; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.core.Is.is; -import static org.hamcrest.core.IsInstanceOf.instanceOf; -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.eclipse.leshan.integration.tests.util.assertion.Assertions.assertThat; import static org.junit.jupiter.api.Assertions.assertThrowsExactly; import java.lang.annotation.Retention; @@ -31,9 +27,7 @@ import java.util.Date; import java.util.stream.Stream; -import org.eclipse.californium.core.coap.Response; import org.eclipse.leshan.core.LwM2m; -import org.eclipse.leshan.core.ResponseCode; import org.eclipse.leshan.core.endpoint.Protocol; import org.eclipse.leshan.core.link.Link; import org.eclipse.leshan.core.link.attributes.QuotedStringAttribute; @@ -82,9 +76,9 @@ public class WriteSingleValueTest { static Stream transports() { - Object[][] transports = new Object[][] - // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider - { { Protocol.COAP, "Californium", "Californium" } }; + Object[][] transports = new Object[][] { + // ProtocolUsed - Client Endpoint Provider - Server Endpoint Provider + { Protocol.COAP, "Californium", "Californium" } }; Object[] contentFormats = new Object[] { // ContentFormat.TEXT, // @@ -143,15 +137,15 @@ public void write_string_resource(ContentFormat contentFormat, Protocol givenPro new WriteRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.STRING_VALUE, expectedvalue)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read resource to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.STRING_VALUE)); LwM2mResource resource = (LwM2mResource) readResponse.getContent(); - assertEquals(expectedvalue, resource.getValue()); + assertThat(resource.getValue()).isEqualTo(expectedvalue); } @TestAllCases @@ -163,15 +157,15 @@ public void write_boolean_resource(ContentFormat contentFormat, Protocol givenPr new WriteRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.BOOLEAN_VALUE, expectedvalue)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read resource to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.BOOLEAN_VALUE)); LwM2mResource resource = (LwM2mResource) readResponse.getContent(); - assertEquals(expectedvalue, resource.getValue()); + assertThat(resource.getValue()).isEqualTo(expectedvalue); } @TestAllCases @@ -183,154 +177,149 @@ public void write_integer_resource(ContentFormat contentFormat, Protocol givenPr new WriteRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.INTEGER_VALUE, expectedvalue)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read resource to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.INTEGER_VALUE)); LwM2mResource resource = (LwM2mResource) readResponse.getContent(); - assertEquals(expectedvalue, resource.getValue()); + assertThat(resource.getValue()).isEqualTo(expectedvalue); } @TestAllCases public void can_write_string_resource_instance(ContentFormat contentFormat, Protocol givenProtocol, String givenClientEndpointProvider, String givenServerEndpointProvider) throws InterruptedException { - write_string_resource_instance(contentFormat, 0); - } - private void write_string_resource_instance(ContentFormat format, int resourceInstance) - throws InterruptedException { // read device model number - String valueToWrite = "newValue"; - WriteResponse response = server.send(currentRegistration, new WriteRequest(format, TestLwM2mId.TEST_OBJECT, 0, - TestLwM2mId.MULTIPLE_STRING_VALUE, resourceInstance, valueToWrite, Type.STRING)); + int resourceInstance = 0; + String expectedValue = "newValue"; + WriteResponse response = server.send(currentRegistration, + new WriteRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_STRING_VALUE, + resourceInstance, expectedValue, Type.STRING)); // verify result - assertEquals(CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read resource to check the value changed - ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(format, TestLwM2mId.TEST_OBJECT, 0, - TestLwM2mId.MULTIPLE_STRING_VALUE, resourceInstance)); + ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, + TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_STRING_VALUE, resourceInstance)); // verify result LwM2mResourceInstance resource = (LwM2mResourceInstance) readResponse.getContent(); - assertEquals(valueToWrite, resource.getValue()); + assertThat(resource.getValue()).isEqualTo(expectedValue); } @TestAllCases public void write_float_resource(ContentFormat contentFormat, Protocol givenProtocol, String givenClientEndpointProvider, String givenServerEndpointProvider) throws InterruptedException { // write resource - double expectedvalue = 999.99; + double expectedValue = 999.99; WriteResponse response = server.send(currentRegistration, - new WriteRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.FLOAT_VALUE, expectedvalue)); + new WriteRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.FLOAT_VALUE, expectedValue)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read resource to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.FLOAT_VALUE)); LwM2mResource resource = (LwM2mResource) readResponse.getContent(); - assertEquals(expectedvalue, resource.getValue()); + assertThat(resource.getValue()).isEqualTo(expectedValue); } @TestAllCases public void write_time_resource(ContentFormat contentFormat, Protocol givenProtocol, String givenClientEndpointProvider, String givenServerEndpointProvider) throws InterruptedException { // write resource - Date expectedvalue = new Date(946681000l); // second accuracy + Date expectedValue = new Date(946681000l); // second accuracy WriteResponse response = server.send(currentRegistration, - new WriteRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.TIME_VALUE, expectedvalue)); + new WriteRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.TIME_VALUE, expectedValue)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read resource to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.TIME_VALUE)); LwM2mResource resource = (LwM2mResource) readResponse.getContent(); - assertEquals(expectedvalue, resource.getValue()); + assertThat(resource.getValue()).isEqualTo(expectedValue); } @TestAllCases public void write_corelnk_resource(ContentFormat contentFormat, Protocol givenProtocol, String givenClientEndpointProvider, String givenServerEndpointProvider) throws InterruptedException { // write resource - Link[] expectedvalue = new Link[3]; - expectedvalue[0] = new MixedLwM2mLink(null, new LwM2mPath(3), + Link[] expectedValue = new Link[3]; + expectedValue[0] = new MixedLwM2mLink(null, new LwM2mPath(3), LwM2mAttributes.create(LwM2mAttributes.OBJECT_VERSION, new LwM2m.Version("1.2"))); - expectedvalue[1] = new MixedLwM2mLink(null, new LwM2mPath(3, 1)); - expectedvalue[2] = new MixedLwM2mLink(null, new LwM2mPath(3, 1, 0), + expectedValue[1] = new MixedLwM2mLink(null, new LwM2mPath(3, 1)); + expectedValue[2] = new MixedLwM2mLink(null, new LwM2mPath(3, 1, 0), new QuotedStringAttribute("attr1", "attr1Value"), new UnquotedStringAttribute("attr2", "attr2Value")); WriteResponse response = server.send(currentRegistration, - new WriteRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.CORELNK_VALUE, expectedvalue)); + new WriteRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.CORELNK_VALUE, expectedValue)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read resource to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.CORELNK_VALUE)); LwM2mResource resource = (LwM2mResource) readResponse.getContent(); - assertArrayEquals(expectedvalue, (Link[]) resource.getValue()); + assertThat((Link[]) resource.getValue()).isEqualTo(expectedValue); } @TestAllCases public void write_unsigned_integer_resource(ContentFormat contentFormat, Protocol givenProtocol, String givenClientEndpointProvider, String givenServerEndpointProvider) throws InterruptedException { // write resource - ULong expectedvalue = ULong.valueOf("18446744073709551615"); // this unsigned integer can not be stored in a + ULong expectedValue = ULong.valueOf("18446744073709551615"); // this unsigned integer can not be stored in a WriteResponse response = server.send(currentRegistration, new WriteRequest(contentFormat, - TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.UNSIGNED_INTEGER_VALUE, expectedvalue)); + TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.UNSIGNED_INTEGER_VALUE, expectedValue)); // verify result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // read resource to check the value changed ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.UNSIGNED_INTEGER_VALUE)); LwM2mResource resource = (LwM2mResource) readResponse.getContent(); - assertEquals(expectedvalue, resource.getValue()); + assertThat(resource.getValue()).isEqualTo(expectedValue); } @TestAllCases public void can_write_single_instance_objlnk_resource(ContentFormat contentFormat, Protocol givenProtocol, String givenClientEndpointProvider, String givenServerEndpointProvider) throws InterruptedException { - ObjectLink data = new ObjectLink(10245, 1); + ObjectLink expectedValue = new ObjectLink(10245, 1); // Write objlnk resource - WriteResponse response = server.send(currentRegistration, - new WriteRequest(contentFormat, TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_OBJLINK_VALUE, data)); + WriteResponse response = server.send(currentRegistration, new WriteRequest(contentFormat, + TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_OBJLINK_VALUE, expectedValue)); // Verify Write result - assertEquals(ResponseCode.CHANGED, response.getCode()); - assertNotNull(response.getCoapResponse()); - assertThat(response.getCoapResponse(), is(instanceOf(Response.class))); + assertThat(response) // + .hasCode(CHANGED) // + .hasValidUnderlyingResponseFor(givenServerEndpointProvider); // Reading back the written OBJLNK value ReadResponse readResponse = server.send(currentRegistration, new ReadRequest(TestLwM2mId.TEST_OBJECT, 0, TestLwM2mId.MULTIPLE_OBJLINK_VALUE)); LwM2mSingleResource resource = (LwM2mSingleResource) readResponse.getContent(); - - // verify read value - assertEquals(((ObjectLink) resource.getValue()).getObjectId(), 10245); - assertEquals(((ObjectLink) resource.getValue()).getObjectInstanceId(), 1); + assertThat(resource.getValue()).isEqualTo(expectedValue); } @TestAllCases