Commit 4e229ea24e156a99c8b35f4938e698705ea1ca29

Authored by Igor Kulikov
Committed by GitHub
2 parents 93b0cfec 70097c6b

Merge pull request #4900 from AndrewVolosytnykhThingsboard/lwm2m-tests-fix

Lwm2m tests fix
@@ -274,14 +274,18 @@ public class DefaultOtaPackageStateService implements OtaPackageStateService { @@ -274,14 +274,18 @@ public class DefaultOtaPackageStateService implements OtaPackageStateService {
274 @Override 274 @Override
275 public void onSuccess(@Nullable Void tmp) { 275 public void onSuccess(@Nullable Void tmp) {
276 log.trace("[{}] Success save telemetry with target firmware for device!", deviceId); 276 log.trace("[{}] Success save telemetry with target firmware for device!", deviceId);
  277 + updateAttributes(device, otaPackage, ts, tenantId, deviceId, otaPackageType);
277 } 278 }
278 279
279 @Override 280 @Override
280 public void onFailure(Throwable t) { 281 public void onFailure(Throwable t) {
281 log.error("[{}] Failed to save telemetry with target firmware for device!", deviceId, t); 282 log.error("[{}] Failed to save telemetry with target firmware for device!", deviceId, t);
  283 + updateAttributes(device, otaPackage, ts, tenantId, deviceId, otaPackageType);
282 } 284 }
283 }); 285 });
  286 + }
284 287
  288 + private void updateAttributes(Device device, OtaPackageInfo otaPackage, long ts, TenantId tenantId, DeviceId deviceId, OtaPackageType otaPackageType) {
285 List<AttributeKvEntry> attributes = new ArrayList<>(); 289 List<AttributeKvEntry> attributes = new ArrayList<>();
286 attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, TITLE), otaPackage.getTitle()))); 290 attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, TITLE), otaPackage.getTitle())));
287 attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, VERSION), otaPackage.getVersion()))); 291 attributes.add(new BaseAttributeKvEntry(ts, new StringDataEntry(getAttributeKey(otaPackageType, VERSION), otaPackage.getVersion())));
@@ -372,38 +372,44 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest { @@ -372,38 +372,44 @@ public class AbstractLwM2MIntegrationTest extends AbstractWebsocketTest {
372 LwM2MClientCredentials credentials, 372 LwM2MClientCredentials credentials,
373 NetworkConfig coapConfig, 373 NetworkConfig coapConfig,
374 String endpoint) throws Exception { 374 String endpoint) throws Exception {
375 - createDeviceProfile(TRANSPORT_CONFIGURATION);  
376 - Device device = createDevice(credentials);  
377 -  
378 - SingleEntityFilter sef = new SingleEntityFilter();  
379 - sef.setSingleEntity(device.getId());  
380 - LatestValueCmd latestCmd = new LatestValueCmd();  
381 - latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "batteryLevel")));  
382 - EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null),  
383 - Collections.emptyList(), Collections.emptyList(), Collections.emptyList());  
384 -  
385 - EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null);  
386 - TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper();  
387 - wrapper.setEntityDataCmds(Collections.singletonList(cmd));  
388 -  
389 - wsClient.send(mapper.writeValueAsString(wrapper));  
390 - wsClient.waitForReply();  
391 -  
392 - wsClient.registerWaitForUpdate();  
393 - LwM2MTestClient client = new LwM2MTestClient(executor, endpoint);  
394 -  
395 - client.init(security, coapConfig);  
396 - String msg = wsClient.waitForUpdate();  
397 -  
398 - EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);  
399 - Assert.assertEquals(1, update.getCmdId());  
400 - List<EntityData> eData = update.getUpdate();  
401 - Assert.assertNotNull(eData);  
402 - Assert.assertEquals(1, eData.size());  
403 - Assert.assertEquals(device.getId(), eData.get(0).getEntityId());  
404 - Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));  
405 - var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("batteryLevel");  
406 - Assert.assertEquals(42, Long.parseLong(tsValue.getValue()));  
407 - client.destroy(); 375 + LwM2MTestClient client = null;
  376 + try {
  377 + createDeviceProfile(TRANSPORT_CONFIGURATION);
  378 + Device device = createDevice(credentials);
  379 +
  380 + SingleEntityFilter sef = new SingleEntityFilter();
  381 + sef.setSingleEntity(device.getId());
  382 + LatestValueCmd latestCmd = new LatestValueCmd();
  383 + latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "batteryLevel")));
  384 + EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null),
  385 + Collections.emptyList(), Collections.emptyList(), Collections.emptyList());
  386 +
  387 + EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null);
  388 + TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper();
  389 + wrapper.setEntityDataCmds(Collections.singletonList(cmd));
  390 +
  391 + wsClient.send(mapper.writeValueAsString(wrapper));
  392 + wsClient.waitForReply();
  393 +
  394 + wsClient.registerWaitForUpdate();
  395 + client = new LwM2MTestClient(executor, endpoint);
  396 +
  397 + client.init(security, coapConfig);
  398 + String msg = wsClient.waitForUpdate();
  399 +
  400 + EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);
  401 + Assert.assertEquals(1, update.getCmdId());
  402 + List<EntityData> eData = update.getUpdate();
  403 + Assert.assertNotNull(eData);
  404 + Assert.assertEquals(1, eData.size());
  405 + Assert.assertEquals(device.getId(), eData.get(0).getEntityId());
  406 + Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));
  407 + var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("batteryLevel");
  408 + Assert.assertEquals(42, Long.parseLong(tsValue.getValue()));
  409 + } finally {
  410 + if(client != null) {
  411 + client.destroy();
  412 + }
  413 + }
408 } 414 }
409 } 415 }
@@ -19,31 +19,24 @@ import com.fasterxml.jackson.core.type.TypeReference; @@ -19,31 +19,24 @@ import com.fasterxml.jackson.core.type.TypeReference;
19 import org.junit.Assert; 19 import org.junit.Assert;
20 import org.junit.Test; 20 import org.junit.Test;
21 import org.thingsboard.server.common.data.Device; 21 import org.thingsboard.server.common.data.Device;
22 -import org.thingsboard.server.common.data.OtaPackageInfo;  
23 import org.thingsboard.server.common.data.device.credentials.lwm2m.NoSecClientCredentials; 22 import org.thingsboard.server.common.data.device.credentials.lwm2m.NoSecClientCredentials;
24 import org.thingsboard.server.common.data.kv.KvEntry; 23 import org.thingsboard.server.common.data.kv.KvEntry;
25 import org.thingsboard.server.common.data.kv.TsKvEntry; 24 import org.thingsboard.server.common.data.kv.TsKvEntry;
26 import org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus; 25 import org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus;
27 -import org.thingsboard.server.common.data.query.EntityData;  
28 -import org.thingsboard.server.common.data.query.EntityDataPageLink;  
29 -import org.thingsboard.server.common.data.query.EntityDataQuery;  
30 import org.thingsboard.server.common.data.query.EntityKey; 26 import org.thingsboard.server.common.data.query.EntityKey;
31 import org.thingsboard.server.common.data.query.EntityKeyType; 27 import org.thingsboard.server.common.data.query.EntityKeyType;
32 -import org.thingsboard.server.common.data.query.SingleEntityFilter;  
33 -import org.thingsboard.server.service.telemetry.cmd.TelemetryPluginCmdsWrapper;  
34 -import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataCmd;  
35 -import org.thingsboard.server.service.telemetry.cmd.v2.EntityDataUpdate;  
36 -import org.thingsboard.server.service.telemetry.cmd.v2.LatestValueCmd;  
37 import org.thingsboard.server.transport.lwm2m.client.LwM2MTestClient; 28 import org.thingsboard.server.transport.lwm2m.client.LwM2MTestClient;
38 29
39 import java.util.Arrays; 30 import java.util.Arrays;
40 import java.util.Collections; 31 import java.util.Collections;
  32 +import java.util.Comparator;
41 import java.util.List; 33 import java.util.List;
42 import java.util.stream.Collectors; 34 import java.util.stream.Collectors;
43 35
44 import static org.thingsboard.rest.client.utils.RestJsonConverter.toTimeseries; 36 import static org.thingsboard.rest.client.utils.RestJsonConverter.toTimeseries;
45 import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADED; 37 import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADED;
46 import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADING; 38 import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADING;
  39 +import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.FAILED;
47 import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.INITIATED; 40 import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.INITIATED;
48 import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.QUEUED; 41 import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.QUEUED;
49 import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.UPDATED; 42 import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.UPDATED;
@@ -138,116 +131,102 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest { @@ -138,116 +131,102 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
138 131
139 @Test 132 @Test
140 public void testFirmwareUpdateWithClientWithoutFirmwareInfo() throws Exception { 133 public void testFirmwareUpdateWithClientWithoutFirmwareInfo() throws Exception {
141 - createDeviceProfile(TRANSPORT_CONFIGURATION);  
142 - NoSecClientCredentials clientCredentials = new NoSecClientCredentials();  
143 - clientCredentials.setEndpoint(ENDPOINT);  
144 - Device device = createDevice(clientCredentials);  
145 -  
146 - OtaPackageInfo firmware = createFirmware();  
147 -  
148 - LwM2MTestClient client = new LwM2MTestClient(executor, ENDPOINT);  
149 - client.init(SECURITY, COAP_CONFIG); 134 + LwM2MTestClient client = null;
  135 + try {
  136 + createDeviceProfile(TRANSPORT_CONFIGURATION);
  137 + NoSecClientCredentials clientCredentials = new NoSecClientCredentials();
  138 + clientCredentials.setEndpoint(ENDPOINT);
  139 + Device device = createDevice(clientCredentials);
150 140
151 - Thread.sleep(1000); 141 + client = new LwM2MTestClient(executor, ENDPOINT);
  142 + client.init(SECURITY, COAP_CONFIG);
152 143
153 - device.setFirmwareId(firmware.getId()); 144 + Thread.sleep(1000);
154 145
155 - device = doPost("/api/device", device, Device.class); 146 + device.setFirmwareId(createFirmware().getId());
  147 + device = doPost("/api/device", device, Device.class);
156 148
157 - Thread.sleep(1000); 149 + Thread.sleep(5000);
158 150
159 - SingleEntityFilter sef = new SingleEntityFilter();  
160 - sef.setSingleEntity(device.getId());  
161 - LatestValueCmd latestCmd = new LatestValueCmd();  
162 - latestCmd.setKeys(Collections.singletonList(new EntityKey(EntityKeyType.TIME_SERIES, "fw_state")));  
163 - EntityDataQuery edq = new EntityDataQuery(sef, new EntityDataPageLink(1, 0, null, null),  
164 - Collections.emptyList(), Collections.emptyList(), Collections.emptyList()); 151 + List<TsKvEntry> ts = toTimeseries(doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + device.getId().getId() + "/values/timeseries?keys=fw_state", new TypeReference<>() {
  152 + }));
165 153
166 - EntityDataCmd cmd = new EntityDataCmd(1, edq, null, latestCmd, null);  
167 - TelemetryPluginCmdsWrapper wrapper = new TelemetryPluginCmdsWrapper();  
168 - wrapper.setEntityDataCmds(Collections.singletonList(cmd)); 154 + List<OtaPackageUpdateStatus> statuses = ts.stream().map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList());
169 155
170 - wsClient.send(mapper.writeValueAsString(wrapper));  
171 - wsClient.waitForReply(); 156 + List<OtaPackageUpdateStatus> expectedStatuses = Collections.singletonList(FAILED);
172 157
173 - wsClient.registerWaitForUpdate();  
174 -  
175 - String msg = wsClient.waitForUpdate();  
176 -  
177 - EntityDataUpdate update = mapper.readValue(msg, EntityDataUpdate.class);  
178 - Assert.assertEquals(1, update.getCmdId());  
179 - List<EntityData> eData = update.getUpdate();  
180 - Assert.assertNotNull(eData);  
181 - Assert.assertEquals(1, eData.size());  
182 - Assert.assertEquals(device.getId(), eData.get(0).getEntityId());  
183 - Assert.assertNotNull(eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES));  
184 - var tsValue = eData.get(0).getLatest().get(EntityKeyType.TIME_SERIES).get("fw_state");  
185 - Assert.assertEquals("FAILED", tsValue.getValue());  
186 - client.destroy(); 158 + Assert.assertEquals(expectedStatuses, statuses);
  159 + } finally {
  160 + if(client != null) {
  161 + client.destroy();
  162 + }
  163 + }
187 } 164 }
188 165
189 @Test 166 @Test
190 public void testFirmwareUpdateByObject5() throws Exception { 167 public void testFirmwareUpdateByObject5() throws Exception {
191 - createDeviceProfile(OTA_TRANSPORT_CONFIGURATION);  
192 - NoSecClientCredentials clientCredentials = new NoSecClientCredentials();  
193 - clientCredentials.setEndpoint("OTA_" + ENDPOINT);  
194 - Device device = createDevice(clientCredentials);  
195 -  
196 - OtaPackageInfo firmware = createFirmware(); 168 + LwM2MTestClient client = null;
  169 + try {
  170 + createDeviceProfile(OTA_TRANSPORT_CONFIGURATION);
  171 + NoSecClientCredentials clientCredentials = new NoSecClientCredentials();
  172 + clientCredentials.setEndpoint("OTA_" + ENDPOINT);
  173 + Device device = createDevice(clientCredentials);
197 174
198 - LwM2MTestClient client = new LwM2MTestClient(executor, "OTA_" + ENDPOINT);  
199 - client.init(SECURITY, COAP_CONFIG); 175 + device.setFirmwareId(createFirmware().getId());
  176 + device = doPost("/api/device", device, Device.class);
  177 + Thread.sleep(1000);
200 178
201 - Thread.sleep(1000); 179 + client = new LwM2MTestClient(executor, "OTA_" + ENDPOINT);
  180 + client.init(SECURITY, COAP_CONFIG);
202 181
203 - device.setFirmwareId(firmware.getId()); 182 + Thread.sleep(3000);
204 183
205 - device = doPost("/api/device", device, Device.class); 184 + List<TsKvEntry> ts = toTimeseries(doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + device.getId().getId() + "/values/timeseries?orderBy=ASC&keys=fw_state&startTs=0&endTs=" + System.currentTimeMillis(), new TypeReference<>() {
  185 + }));
206 186
207 - Thread.sleep(4000); 187 + List<OtaPackageUpdateStatus> statuses = ts.stream().sorted(Comparator.comparingLong(TsKvEntry::getTs)).map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList());
208 188
209 - List<TsKvEntry> ts = toTimeseries(doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + device.getId().getId() + "/values/timeseries?orderBy=ASC&keys=fw_state&startTs=0&endTs=" + System.currentTimeMillis(), new TypeReference<>() {  
210 - })); 189 + List<OtaPackageUpdateStatus> expectedStatuses = Arrays.asList(QUEUED, INITIATED, DOWNLOADING, DOWNLOADED, UPDATING, UPDATED);
211 190
212 - List<OtaPackageUpdateStatus> statuses = ts.stream().map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList());  
213 -  
214 - List<OtaPackageUpdateStatus> expectedStatuses = Arrays.asList(QUEUED, INITIATED, DOWNLOADING, DOWNLOADED, UPDATING, UPDATED);  
215 -  
216 - Assert.assertEquals(expectedStatuses, statuses);  
217 -  
218 - client.destroy(); 191 + Assert.assertEquals(expectedStatuses, statuses);
  192 + } finally {
  193 + if (client != null) {
  194 + client.destroy();
  195 + }
  196 + }
219 } 197 }
220 198
221 @Test 199 @Test
222 public void testSoftwareUpdateByObject9() throws Exception { 200 public void testSoftwareUpdateByObject9() throws Exception {
223 - createDeviceProfile(OTA_TRANSPORT_CONFIGURATION);  
224 - NoSecClientCredentials clientCredentials = new NoSecClientCredentials();  
225 - clientCredentials.setEndpoint("OTA_" + ENDPOINT);  
226 - Device device = createDevice(clientCredentials); 201 + LwM2MTestClient client = null;
  202 + try {
  203 + createDeviceProfile(OTA_TRANSPORT_CONFIGURATION);
  204 + NoSecClientCredentials clientCredentials = new NoSecClientCredentials();
  205 + clientCredentials.setEndpoint("OTA_" + ENDPOINT);
  206 + Device device = createDevice(clientCredentials);
227 207
228 - OtaPackageInfo software = createSoftware(); 208 + device.setSoftwareId(createSoftware().getId());
  209 + device = doPost("/api/device", device, Device.class);
229 210
230 - LwM2MTestClient client = new LwM2MTestClient(executor, "OTA_" + ENDPOINT);  
231 - client.init(SECURITY, COAP_CONFIG); 211 + Thread.sleep(1000);
232 212
233 - Thread.sleep(1000); 213 + client = new LwM2MTestClient(executor, "OTA_" + ENDPOINT);
  214 + client.init(SECURITY, COAP_CONFIG);
234 215
235 - device.setSoftwareId(software.getId()); 216 + Thread.sleep(3000);
236 217
237 - device = doPost("/api/device", device, Device.class); 218 + List<TsKvEntry> ts = toTimeseries(doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + device.getId().getId() + "/values/timeseries?orderBy=ASC&keys=sw_state&startTs=0&endTs=" + System.currentTimeMillis(), new TypeReference<>() {
  219 + }));
238 220
239 - Thread.sleep(4000); 221 + List<OtaPackageUpdateStatus> statuses = ts.stream().sorted(Comparator.comparingLong(TsKvEntry::getTs)).map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList());
240 222
241 - List<TsKvEntry> ts = toTimeseries(doGetAsyncTyped("/api/plugins/telemetry/DEVICE/" + device.getId().getId() + "/values/timeseries?orderBy=ASC&keys=sw_state&startTs=0&endTs=" + System.currentTimeMillis(), new TypeReference<>() {  
242 - })); 223 + List<OtaPackageUpdateStatus> expectedStatuses = Arrays.asList(QUEUED, INITIATED, DOWNLOADING, DOWNLOADING, DOWNLOADING, DOWNLOADED, VERIFIED, UPDATED);
243 224
244 - List<OtaPackageUpdateStatus> statuses = ts.stream().map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList());  
245 -  
246 - List<OtaPackageUpdateStatus> expectedStatuses = Arrays.asList(QUEUED, INITIATED, DOWNLOADING, DOWNLOADING, DOWNLOADING, DOWNLOADED, VERIFIED, UPDATED);  
247 -  
248 - Assert.assertEquals(expectedStatuses, statuses);  
249 -  
250 - client.destroy(); 225 + Assert.assertEquals(expectedStatuses, statuses);
  226 + } finally {
  227 + if (client != null) {
  228 + client.destroy();
  229 + }
  230 + }
251 } 231 }
252 -  
253 } 232 }
@@ -128,10 +128,14 @@ public class FwLwM2MDevice extends BaseInstanceEnabler implements Destroyable { @@ -128,10 +128,14 @@ public class FwLwM2MDevice extends BaseInstanceEnabler implements Destroyable {
128 128
129 private void startDownloading() { 129 private void startDownloading() {
130 scheduler.schedule(() -> { 130 scheduler.schedule(() -> {
131 - state.set(1);  
132 - fireResourcesChange(3);  
133 - state.set(2);  
134 - fireResourcesChange(3); 131 + try {
  132 + state.set(1);
  133 + fireResourcesChange(3);
  134 + Thread.sleep(100);
  135 + state.set(2);
  136 + fireResourcesChange(3);
  137 + } catch (Exception e) {
  138 + }
135 }, 100, TimeUnit.MILLISECONDS); 139 }, 100, TimeUnit.MILLISECONDS);
136 } 140 }
137 141
@@ -144,7 +148,6 @@ public class FwLwM2MDevice extends BaseInstanceEnabler implements Destroyable { @@ -144,7 +148,6 @@ public class FwLwM2MDevice extends BaseInstanceEnabler implements Destroyable {
144 updateResult.set(1); 148 updateResult.set(1);
145 fireResourcesChange(5); 149 fireResourcesChange(5);
146 } catch (Exception e) { 150 } catch (Exception e) {
147 -  
148 } 151 }
149 }, 100, TimeUnit.MILLISECONDS); 152 }, 100, TimeUnit.MILLISECONDS);
150 } 153 }