From bed8881d93b9a1ce90f2e9dd0546a5a506a5175f Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Mon, 30 Aug 2021 20:48:36 +0300 Subject: [PATCH] lwm2m: abstract test create --- ...a => AbstractRpcLwM2MIntegrationTest.java} | 12 +- .../sql/RpcLwm2mIntegrationCreateTest.java | 4 +- .../AbstractLwM2MIntegrationTest.java | 80 ++++++------ .../security/sql/OtaLwM2MIntegrationTest.java | 117 +++++++----------- .../DefaultLwM2MAttributesService.java | 2 +- 5 files changed, 93 insertions(+), 122 deletions(-) rename application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/{RpcAbstractLwM2MIntegrationTest.java => AbstractRpcLwM2MIntegrationTest.java} (99%) diff --git a/application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/RpcAbstractLwM2MIntegrationTest.java b/application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/AbstractRpcLwM2MIntegrationTest.java similarity index 99% rename from application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/RpcAbstractLwM2MIntegrationTest.java rename to application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/AbstractRpcLwM2MIntegrationTest.java index 36158b2640..f635de509b 100644 --- a/application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/RpcAbstractLwM2MIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/AbstractRpcLwM2MIntegrationTest.java @@ -60,19 +60,19 @@ import static org.eclipse.leshan.core.LwM2mId.DEVICE; import static org.eclipse.leshan.core.LwM2mId.FIRMWARE; import static org.eclipse.leshan.core.LwM2mId.SERVER; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.BINARY_APP_DATA_CONTAINER; import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.COAP_CONFIG; +import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.HOST; import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.HOST_BS; +import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.PORT; import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.PORT_BS; import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.SECURE_PORT; import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.SECURE_PORT_BS; import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.SECURITY; import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.SHORT_SERVER_ID; -import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.HOST; -import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.PORT; import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.SHORT_SERVER_ID_BS; -import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.objectInstanceId_0; -import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.BINARY_APP_DATA_CONTAINER; import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.TEMPERATURE_SENSOR; +import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.objectInstanceId_0; import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.objectInstanceId_1; import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.resourceIdName_19_0_0; import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.resourceIdName_19_1_0; @@ -84,7 +84,7 @@ import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.resourceId_ import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.resources; @DaoSqlTest -public class RpcAbstractLwM2MIntegrationTest extends AbstractWebsocketTest { +public abstract class AbstractRpcLwM2MIntegrationTest extends AbstractWebsocketTest { protected final String RPC_TRANSPORT_CONFIGURATION = "{\n" + " \"type\": \"LWM2M\",\n" + @@ -165,7 +165,7 @@ public class RpcAbstractLwM2MIntegrationTest extends AbstractWebsocketTest { protected String objectInstanceIdVer_5; protected String objectIdVer_19; - public RpcAbstractLwM2MIntegrationTest(){ } + public AbstractRpcLwM2MIntegrationTest(){ } @Before public void beforeTest() throws Exception { diff --git a/application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/sql/RpcLwm2mIntegrationCreateTest.java b/application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/sql/RpcLwm2mIntegrationCreateTest.java index a5672be133..5997449d1f 100644 --- a/application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/sql/RpcLwm2mIntegrationCreateTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/lwm2m/rpc/sql/RpcLwm2mIntegrationCreateTest.java @@ -19,7 +19,7 @@ import com.fasterxml.jackson.databind.node.ObjectNode; import org.eclipse.leshan.core.ResponseCode; import org.junit.Test; import org.thingsboard.common.util.JacksonUtil; -import org.thingsboard.server.transport.lwm2m.rpc.RpcAbstractLwM2MIntegrationTest; +import org.thingsboard.server.transport.lwm2m.rpc.AbstractRpcLwM2MIntegrationTest; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -28,7 +28,7 @@ import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.objectInsta import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.resourceId_0; -public class RpcLwm2mIntegrationCreateTest extends RpcAbstractLwM2MIntegrationTest { +public class RpcLwm2mIntegrationCreateTest extends AbstractRpcLwM2MIntegrationTest { /** diff --git a/application/src/test/java/org/thingsboard/server/transport/lwm2m/security/AbstractLwM2MIntegrationTest.java b/application/src/test/java/org/thingsboard/server/transport/lwm2m/security/AbstractLwM2MIntegrationTest.java index 2c47d5cc80..522abba65a 100644 --- a/application/src/test/java/org/thingsboard/server/transport/lwm2m/security/AbstractLwM2MIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/lwm2m/security/AbstractLwM2MIntegrationTest.java @@ -17,7 +17,6 @@ package org.thingsboard.server.transport.lwm2m.security; import com.fasterxml.jackson.core.type.TypeReference; import org.apache.commons.io.IOUtils; -import org.eclipse.californium.core.CoapClient; import org.eclipse.californium.core.network.config.NetworkConfig; import org.eclipse.leshan.client.object.Security; import org.eclipse.leshan.core.util.Hex; @@ -84,12 +83,9 @@ import java.util.List; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; -import static org.eclipse.leshan.client.object.Security.noSec; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.thingsboard.server.common.data.ota.OtaPackageType.FIRMWARE; import static org.thingsboard.server.common.data.ota.OtaPackageType.SOFTWARE; -import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.COAP_CONFIG; -import static org.thingsboard.server.transport.lwm2m.Lwm2mTestHelper.SECURITY; @DaoSqlTest public abstract class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest { @@ -175,7 +171,7 @@ public abstract class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest protected static final String ENDPOINT = "deviceAEndpoint"; - protected CoapClient client; + protected LwM2MTestClient client; public AbstractLwM2MIntegrationTest() { // create client credentials @@ -366,50 +362,46 @@ public abstract class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest public void after() { executor.shutdownNow(); wsClient.close(); + if (client != null) { + client.destroy(); + } } public void basicTestConnectionObserveTelemetry(Security security, LwM2MClientCredentials credentials, NetworkConfig coapConfig, String endpoint) throws Exception { - LwM2MTestClient client = null; - try { - createDeviceProfile(TRANSPORT_CONFIGURATION); - Device device = createDevice(credentials); - - SingleEntityFilter sef = new SingleEntityFilter(); - sef.setSingleEntity(device.getId()); - LatestValueCmd latestCmd = new LatestValueCmd(); - latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "batteryLevel"))); - EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null), - Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); - - EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null); - TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper(); - wrapper.setEntityDataCmds(Collections.singletonList(cmd)); - - wsClient.send(mapper.writeValueAsString(wrapper)); - wsClient.waitForReply(); - - wsClient.registerWaitForUpdate(); - client = new LwM2MTestClient(executor, endpoint); - int clientPort = SocketUtils.findAvailableTcpPort(); - client.init(security, coapConfig, clientPort); - String msg = wsClient.waitForUpdate(); - - EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class); - Assert.assertEquals(1, update.getCmdId()); - List eData = update.getUpdate(); - Assert.assertNotNull(eData); - Assert.assertEquals(1, eData.size()); - Assert.assertEquals(device.getId(), eData.get(0).getEntityId()); - Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES)); - var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("batteryLevel"); - Assert.assertEquals(42, Long.parseLong(tsValue.getValue())); - } finally { - if(client != null) { - client.destroy(); - } - } + createDeviceProfile(TRANSPORT_CONFIGURATION); + Device device = createDevice(credentials); + + SingleEntityFilter sef = new SingleEntityFilter(); + sef.setSingleEntity(device.getId()); + LatestValueCmd latestCmd = new LatestValueCmd(); + latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "batteryLevel"))); + EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null), + Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); + + EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null); + TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper(); + wrapper.setEntityDataCmds(Collections.singletonList(cmd)); + + wsClient.send(mapper.writeValueAsString(wrapper)); + wsClient.waitForReply(); + + wsClient.registerWaitForUpdate(); + client = new LwM2MTestClient(executor, endpoint); + int clientPort = SocketUtils.findAvailableTcpPort(); + client.init(security, coapConfig, clientPort); + String msg = wsClient.waitForUpdate(); + + EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class); + Assert.assertEquals(1, update.getCmdId()); + List eData = update.getUpdate(); + Assert.assertNotNull(eData); + Assert.assertEquals(1, eData.size()); + Assert.assertEquals(device.getId(), eData.get(0).getEntityId()); + Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES)); + var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("batteryLevel"); + Assert.assertEquals(42, Long.parseLong(tsValue.getValue())); } } diff --git a/application/src/test/java/org/thingsboard/server/transport/lwm2m/security/sql/OtaLwM2MIntegrationTest.java b/application/src/test/java/org/thingsboard/server/transport/lwm2m/security/sql/OtaLwM2MIntegrationTest.java index a8a23707c5..ffaeb3f5c6 100644 --- a/application/src/test/java/org/thingsboard/server/transport/lwm2m/security/sql/OtaLwM2MIntegrationTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/lwm2m/security/sql/OtaLwM2MIntegrationTest.java @@ -134,14 +134,11 @@ public class OtaLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { @Test public void testFirmwareUpdateWithClientWithoutFirmwareInfo() throws Exception { - LwM2MTestClient client = null; String endpoint = "WithoutFirmwareInfoDevice"; - try { createDeviceProfile(TRANSPORT_CONFIGURATION); NoSecClientCredentials clientCredentials = new NoSecClientCredentials(); clientCredentials.setEndpoint(endpoint); Device device = createDevice(clientCredentials); - client = new LwM2MTestClient(executor, endpoint); int clientPort = SocketUtils.findAvailableTcpPort(); client.init(SECURITY, COAP_CONFIG, clientPort); @@ -161,48 +158,35 @@ public class OtaLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { List expectedStatuses = Collections.singletonList(FAILED); Assert.assertEquals(expectedStatuses, statuses); - } finally { - if (client != null) { - client.destroy(); - } - } } @Test public void testFirmwareUpdateByObject5() throws Exception { - LwM2MTestClient client = null; String endpoint = "Ota5_Device"; List expectedStatuses = Arrays.asList(QUEUED, INITIATED, DOWNLOADING, DOWNLOADED, UPDATING, UPDATED); - try { - createDeviceProfile(OTA_TRANSPORT_CONFIGURATION); - NoSecClientCredentials clientCredentials = new NoSecClientCredentials(); - clientCredentials.setEndpoint(endpoint); - Device device = createDevice(clientCredentials); - - device.setFirmwareId(createFirmware().getId()); - Device saveDevice = doPost("/api/device", device, Device.class); - Thread.sleep(1000); + createDeviceProfile(OTA_TRANSPORT_CONFIGURATION); + NoSecClientCredentials clientCredentials = new NoSecClientCredentials(); + clientCredentials.setEndpoint(endpoint); + Device device = createDevice(clientCredentials); - client = new LwM2MTestClient(executor, endpoint); - int clientPort = SocketUtils.findAvailableTcpPort(); - client.init(SECURITY, COAP_CONFIG, clientPort); + device.setFirmwareId(createFirmware().getId()); + Device saveDevice = doPost("/api/device", device, Device.class); + Thread.sleep(1000); - List ts = await("await on timeseries") - .atMost(30, TimeUnit.SECONDS) - .until(()-> toTimeseries(doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + saveDevice.getId().getId() + "/values/timeseries?orderBy=ASC&keys=fw_state&startTs=0&endTs=" + System.currentTimeMillis(), new TypeReference<>() { - })), hasSize(expectedStatuses.size())); + client = new LwM2MTestClient(executor, endpoint); + int clientPort = SocketUtils.findAvailableTcpPort(); + client.init(SECURITY, COAP_CONFIG, clientPort); + List ts = await("await on timeseries") + .atMost(30, TimeUnit.SECONDS) + .until(() -> toTimeseries(doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + saveDevice.getId().getId() + "/values/timeseries?orderBy=ASC&keys=fw_state&startTs=0&endTs=" + System.currentTimeMillis(), new TypeReference<>() { + })), hasSize(expectedStatuses.size())); - List statuses = ts.stream().sorted(Comparator.comparingLong(TsKvEntry::getTs)).map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList()); + List statuses = ts.stream().sorted(Comparator.comparingLong(TsKvEntry::getTs)).map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList()); - Assert.assertEquals(expectedStatuses, statuses); - } finally { - if (client != null) { - client.destroy(); - } - } + Assert.assertEquals(expectedStatuses, statuses); } /** @@ -215,44 +199,39 @@ public class OtaLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { //given final List expectedStatuses = List.of( QUEUED, INITIATED, DOWNLOADING, DOWNLOADING, DOWNLOADING, DOWNLOADED, VERIFIED, UPDATED); - LwM2MTestClient client = null; String endpoint = "Ota9_Device"; - try { - createDeviceProfile(OTA_TRANSPORT_CONFIGURATION); - NoSecClientCredentials clientCredentials = new NoSecClientCredentials(); - clientCredentials.setEndpoint(endpoint); - final Device device = createDevice(clientCredentials); - device.setSoftwareId(createSoftware().getId()); - - final Device savedDevice = doPost("/api/device", device, Device.class); //sync call - assertThat(savedDevice).as("saved device").isNotNull(); - assertThat(getDeviceFromAPI(device.getId().getId())).as("fetched device").isEqualTo(savedDevice); - - //when - log.warn("Init the client..."); - client = new LwM2MTestClient(executor, endpoint); - int clientPort = SocketUtils.findAvailableTcpPort(); - client.init(SECURITY, COAP_CONFIG, clientPort); - - log.warn("AWAIT atMost {} SECONDS on timeseries List by API with list size {}...", TIMEOUT, expectedStatuses.size()); - List ts = await("await on timeseries") - .atMost(30, TimeUnit.SECONDS) - .until(() -> getSwStateTelemetryFromAPI(device.getId().getId()), hasSize(expectedStatuses.size())); - log.warn("Got the ts: {}", ts); - - ts.sort(Comparator.comparingLong(TsKvEntry::getTs)); - log.warn("Ts ordered: {}", ts); - ts.forEach((x) -> log.warn("ts: {} ", x)); - - List statuses = ts.stream().map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList()); - log.warn("Converted ts to statuses: {}", statuses); - - assertThat(statuses).isEqualTo(expectedStatuses); - } finally { - if (client != null) { - client.destroy(); - } - } + createDeviceProfile(OTA_TRANSPORT_CONFIGURATION); + NoSecClientCredentials clientCredentials = new NoSecClientCredentials(); + clientCredentials.setEndpoint(endpoint); + final Device device = createDevice(clientCredentials); + device.setSoftwareId(createSoftware().getId()); + + final Device savedDevice = doPost("/api/device", device, Device.class); //sync call + Thread.sleep(1000); + + assertThat(savedDevice).as("saved device").isNotNull(); + assertThat(getDeviceFromAPI(device.getId().getId())).as("fetched device").isEqualTo(savedDevice); + + //when + log.warn("Init the client..."); + client = new LwM2MTestClient(executor, endpoint); + int clientPort = SocketUtils.findAvailableTcpPort(); + client.init(SECURITY, COAP_CONFIG, clientPort); + + log.warn("AWAIT atMost {} SECONDS on timeseries List by API with list size {}...", TIMEOUT, expectedStatuses.size()); + List ts = await("await on timeseries") + .atMost(30, TimeUnit.SECONDS) + .until(() -> getSwStateTelemetryFromAPI(device.getId().getId()), hasSize(expectedStatuses.size())); + log.warn("Got the ts: {}", ts); + + ts.sort(Comparator.comparingLong(TsKvEntry::getTs)); + log.warn("Ts ordered: {}", ts); + ts.forEach((x) -> log.warn("ts: {} ", x)); + + List statuses = ts.stream().map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList()); + log.warn("Converted ts to statuses: {}", statuses); + + assertThat(statuses).isEqualTo(expectedStatuses); } private Device getDeviceFromAPI(UUID deviceId) throws Exception { diff --git a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/attributes/DefaultLwM2MAttributesService.java b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/attributes/DefaultLwM2MAttributesService.java index 3d27621c00..f70af7bf0e 100644 --- a/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/attributes/DefaultLwM2MAttributesService.java +++ b/common/transport/lwm2m/src/main/java/org/thingsboard/server/transport/lwm2m/server/attributes/DefaultLwM2MAttributesService.java @@ -198,7 +198,7 @@ public class DefaultLwM2MAttributesService implements LwM2MAttributesService { } } } catch (IllegalArgumentException e){ - log.error("Failed update resource [{}] onAttributesUpdate [{}]", lwM2MClient.getEndpoint(), e.getMessage()); + log.error("Failed update resource ["+lwM2MClient.getEndpoint()+"] onAttributesUpdate:", e); String logMsg = String.format("%s: Failed update resource onAttributesUpdate %s.", LOG_LWM2M_ERROR, e.getMessage()); logService.log(lwM2MClient, logMsg);