Browse Source

Merge pull request #5082 from smatvienko-tb/test-lwm2m-refactor-awaitility

[3.3.1] Test lwm2m refactor awaitility
pull/5085/head
Igor Kulikov 5 years ago
committed by GitHub
parent
commit
5834bbd75d
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 45
      application/src/test/java/org/thingsboard/server/transport/lwm2m/sql/NoSecLwM2MIntegrationTest.java
  2. 10
      dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java

45
application/src/test/java/org/thingsboard/server/transport/lwm2m/sql/NoSecLwM2MIntegrationTest.java

@ -19,7 +19,6 @@ import com.fasterxml.jackson.core.type.TypeReference;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.device.credentials.lwm2m.NoSecClientCredentials; import org.thingsboard.server.common.data.device.credentials.lwm2m.NoSecClientCredentials;
@ -37,8 +36,9 @@ import java.util.UUID;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await; import static org.awaitility.Awaitility.await;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.hasSize;
import static org.thingsboard.rest.client.utils.RestJsonConverter.toTimeseries; import static org.thingsboard.rest.client.utils.RestJsonConverter.toTimeseries;
import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADED; import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADED;
import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADING; import static org.thingsboard.server.common.data.ota.OtaPackageUpdateStatus.DOWNLOADING;
@ -174,7 +174,7 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
Assert.assertEquals(expectedStatuses, statuses); Assert.assertEquals(expectedStatuses, statuses);
} finally { } finally {
if(client != null) { if (client != null) {
client.destroy(); client.destroy();
} }
} }
@ -219,11 +219,10 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
* Check the detailed log output to learn how Awaitility polling the API and when exactly expected result appears * Check the detailed log output to learn how Awaitility polling the API and when exactly expected result appears
* */ * */
@Test @Test
@Ignore
public void testSoftwareUpdateByObject9() throws Exception { public void testSoftwareUpdateByObject9() throws Exception {
//given //given
final List<OtaPackageUpdateStatus> expectedStatuses = Collections.unmodifiableList(Arrays.asList( final List<OtaPackageUpdateStatus> expectedStatuses = List.of(
QUEUED, INITIATED, DOWNLOADING, DOWNLOADING, DOWNLOADING, DOWNLOADED, VERIFIED, UPDATED)); QUEUED, INITIATED, DOWNLOADING, DOWNLOADING, DOWNLOADING, DOWNLOADED, VERIFIED, UPDATED);
createDeviceProfile(OTA_TRANSPORT_CONFIGURATION); createDeviceProfile(OTA_TRANSPORT_CONFIGURATION);
NoSecClientCredentials clientCredentials = new NoSecClientCredentials(); NoSecClientCredentials clientCredentials = new NoSecClientCredentials();
@ -231,39 +230,29 @@ public class NoSecLwM2MIntegrationTest extends AbstractLwM2MIntegrationTest {
final Device device = createDevice(clientCredentials); final Device device = createDevice(clientCredentials);
device.setSoftwareId(createSoftware().getId()); device.setSoftwareId(createSoftware().getId());
log.warn("Saving by API " + device); final Device savedDevice = doPost("/api/device", device, Device.class); //sync call
final Device savedDevice = doPost("/api/device", device, Device.class); assertThat(savedDevice).as("saved device").isNotNull();
Assert.assertNotNull(savedDevice); assertThat(getDeviceFromAPI(device.getId().getId())).as("fetched device").isEqualTo(savedDevice);
log.warn("Device saved by API {}", savedDevice);
log.warn("AWAIT atMost {} SECONDS on get device by API...", TIMEOUT);
await()
.atMost(TIMEOUT, TimeUnit.SECONDS)
.until(() -> getDeviceFromAPI(device.getId().getId()), is(savedDevice));
log.warn("Got device by API.");
//when //when
log.warn("Init the client..."); log.warn("Init the client...");
client = new LwM2MTestClient(executor, "OTA_" + ENDPOINT); client = new LwM2MTestClient(executor, "OTA_" + ENDPOINT);
client.init(SECURITY, COAP_CONFIG); client.init(SECURITY, COAP_CONFIG);
log.warn("Init done");
log.warn("AWAIT atMost {} SECONDS on timeseries List<TsKvEntry> by API with list size {}...", TIMEOUT, expectedStatuses.size()); log.warn("AWAIT atMost {} SECONDS on timeseries List<TsKvEntry> by API with list size {}...", TIMEOUT, expectedStatuses.size());
await() List<TsKvEntry> ts = await("await on timeseries")
.atMost(30, TimeUnit.SECONDS) .atMost(30, TimeUnit.SECONDS)
.until(() -> getSwStateTelemetryFromAPI(device.getId().getId()) .until(() -> getSwStateTelemetryFromAPI(device.getId().getId()), hasSize(expectedStatuses.size()));
.size(), is(expectedStatuses.size())); log.warn("Got the ts: {}", ts);
log.warn("Got an expected await condition!");
//then ts.sort(Comparator.comparingLong(TsKvEntry::getTs));
log.warn("Fetching ts for the final asserts"); log.warn("Ts ordered: {}", ts);
List<TsKvEntry> ts = getSwStateTelemetryFromAPI(device.getId().getId()); ts.forEach((x) -> log.warn("ts: {} ", x));
log.warn("Got an ts {}", ts);
List<OtaPackageUpdateStatus> statuses = ts.stream().sorted(Comparator.comparingLong(TsKvEntry::getTs)).map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList()); List<OtaPackageUpdateStatus> statuses = ts.stream().map(KvEntry::getValueAsString).map(OtaPackageUpdateStatus::valueOf).collect(Collectors.toList());
log.warn("Converted ts to statuses {}", statuses); log.warn("Converted ts to statuses: {}", statuses);
Assert.assertEquals(expectedStatuses, statuses); assertThat(statuses).isEqualTo(expectedStatuses);
} }
private Device getDeviceFromAPI(UUID deviceId) throws Exception { private Device getDeviceFromAPI(UUID deviceId) throws Exception {

10
dao/src/main/java/org/thingsboard/server/dao/device/DeviceServiceImpl.java

@ -184,6 +184,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"), @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"),
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}") @CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}")
}) })
@Transactional
@Override @Override
public Device saveDeviceWithAccessToken(Device device, String accessToken) { public Device saveDeviceWithAccessToken(Device device, String accessToken) {
return doSaveDevice(device, accessToken, true); return doSaveDevice(device, accessToken, true);
@ -207,7 +208,11 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
return doSaveDevice(device, null, true); return doSaveDevice(device, null, true);
} }
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}") @Caching(evict= {
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.name}"),
@CacheEvict(cacheNames = DEVICE_CACHE, key = "{#device.tenantId, #device.id}")
})
@Transactional
@Override @Override
public Device saveDeviceWithCredentials(Device device, DeviceCredentials deviceCredentials) { public Device saveDeviceWithCredentials(Device device, DeviceCredentials deviceCredentials) {
if (device.getId() == null) { if (device.getId() == null) {
@ -631,7 +636,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name()); throw new ProvisionFailedException(ProvisionResponseStatus.FAILURE.name());
} }
} }
removeDeviceFromCacheById(savedDevice.getTenantId(), savedDevice.getId()); removeDeviceFromCacheById(savedDevice.getTenantId(), savedDevice.getId()); // eviction by name is described as annotation @CacheEvict above
return savedDevice; return savedDevice;
} }
@ -721,6 +726,7 @@ public class DeviceServiceImpl extends AbstractEntityService implements DeviceSe
} }
if (!old.getName().equals(device.getName())) { if (!old.getName().equals(device.getName())) {
removeDeviceFromCacheByName(tenantId, old.getName()); removeDeviceFromCacheByName(tenantId, old.getName());
removeDeviceFromCacheById(tenantId, device.getId());
} }
} }

Loading…
Cancel
Save