BaseDeviceControllerTest refactored

This commit is contained in:
Sergey Matvienko 2022-04-20 10:40:38 +03:00
parent e61a77af30
commit 8412397fd1

View File

@ -59,12 +59,12 @@ import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID;
@Slf4j @Slf4j
public abstract class BaseDeviceControllerTest extends AbstractControllerTest { public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
static final int TIMEOUT = 30; static final int TIMEOUT = 30;
static final TypeReference<PageData<Device>> PAGE_DATA_DEVICE_TYPE_REF = new TypeReference<>(){}; static final TypeReference<PageData<Device>> PAGE_DATA_DEVICE_TYPE_REF = new TypeReference<>() {
};
ListeningExecutorService executor; ListeningExecutorService executor;
List<ListenableFuture<Device>> createFutures; List<ListenableFuture<Device>> futures;
List<ListenableFuture<ResultActions>> deleteFutures;
PageData<Device> pageData; PageData<Device> pageData;
private Tenant savedTenant; private Tenant savedTenant;
@ -72,8 +72,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
@Before @Before
public void beforeTest() throws Exception { public void beforeTest() throws Exception {
log.warn("beforeTest"); log.debug("beforeTest");
executor = MoreExecutors.listeningDecorator(ThingsBoardExecutors.newWorkStealingPool(12, getClass())); executor = MoreExecutors.listeningDecorator(ThingsBoardExecutors.newWorkStealingPool(8, getClass()));
loginSysAdmin(); loginSysAdmin();
@ -94,14 +94,14 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
@After @After
public void afterTest() throws Exception { public void afterTest() throws Exception {
log.warn("afterTest..."); log.debug("afterTest...");
executor.shutdownNow(); executor.shutdownNow();
loginSysAdmin(); loginSysAdmin();
doDelete("/api/tenant/" + savedTenant.getId().getId()) doDelete("/api/tenant/" + savedTenant.getId().getId())
.andExpect(status().isOk()); .andExpect(status().isOk());
log.warn("afterTest done"); log.debug("afterTest done");
} }
@Test @Test
@ -202,6 +202,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
Assert.assertEquals("typeA", deviceTypes.get(0).getType()); Assert.assertEquals("typeA", deviceTypes.get(0).getType());
Assert.assertEquals("typeB", deviceTypes.get(1).getType()); Assert.assertEquals("typeB", deviceTypes.get(1).getType());
Assert.assertEquals("typeC", deviceTypes.get(2).getType()); Assert.assertEquals("typeC", deviceTypes.get(2).getType());
deleteDevicesAsync("/api/device/", devices);
} }
@Test @Test
@ -410,19 +412,19 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
@Test @Test
public void testFindTenantDevices() throws Exception { public void testFindTenantDevices() throws Exception {
log.warn("testFindTenantDevices"); log.debug("testFindTenantDevices");
createFutures = new ArrayList<>(178); futures = new ArrayList<>(178);
for (int i = 0; i < 178; i++) { for (int i = 0; i < 178; i++) {
Device device = new Device(); Device device = new Device();
device.setName("Device" + i); device.setName("Device" + i);
device.setType("default"); device.setType("default");
createFutures.add(executor.submit(() -> futures.add(executor.submit(() ->
doPost("/api/device", device, Device.class))); doPost("/api/device", device, Device.class)));
} }
log.warn("await create devices"); log.debug("await create devices");
List<Device> devices = Futures.allAsList(createFutures).get(TIMEOUT, TimeUnit.SECONDS); List<Device> devices = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS);
log.warn("start reading"); log.debug("start reading");
List<Device> loadedDevices = new ArrayList<>(178); List<Device> loadedDevices = new ArrayList<>(178);
PageLink pageLink = new PageLink(23); PageLink pageLink = new PageLink(23);
do { do {
@ -435,18 +437,18 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
} }
} while (pageData.hasNext()); } while (pageData.hasNext());
log.warn("asserting"); log.debug("asserting");
assertThat(devices).containsExactlyInAnyOrderElementsOf(loadedDevices); assertThat(devices).containsExactlyInAnyOrderElementsOf(loadedDevices);
log.warn("delete devices async"); log.debug("delete devices async");
deleteDevicesAsync("/api/device/", loadedDevices).get(TIMEOUT, TimeUnit.SECONDS); deleteDevicesAsync("/api/device/", loadedDevices).get(TIMEOUT, TimeUnit.SECONDS);
log.warn("done"); log.debug("done");
} }
@Test @Test
public void testFindTenantDevicesByName() throws Exception { public void testFindTenantDevicesByName() throws Exception {
String title1 = "Device title 1"; String title1 = "Device title 1";
createFutures = new ArrayList<>(143); futures = new ArrayList<>(143);
for (int i = 0; i < 143; i++) { for (int i = 0; i < 143; i++) {
Device device = new Device(); Device device = new Device();
String suffix = RandomStringUtils.randomAlphanumeric(15); String suffix = RandomStringUtils.randomAlphanumeric(15);
@ -454,13 +456,13 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
device.setName(name); device.setName(name);
device.setType("default"); device.setType("default");
createFutures.add(executor.submit(() -> futures.add(executor.submit(() ->
doPost("/api/device", device, Device.class))); doPost("/api/device", device, Device.class)));
} }
List<Device> devicesTitle1 = Futures.allAsList(createFutures).get(TIMEOUT, TimeUnit.SECONDS); List<Device> devicesTitle1 = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS);
String title2 = "Device title 2"; String title2 = "Device title 2";
createFutures = new ArrayList<>(75); futures = new ArrayList<>(75);
for (int i = 0; i < 75; i++) { for (int i = 0; i < 75; i++) {
Device device = new Device(); Device device = new Device();
String suffix = RandomStringUtils.randomAlphanumeric(15); String suffix = RandomStringUtils.randomAlphanumeric(15);
@ -468,10 +470,10 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
device.setName(name); device.setName(name);
device.setType("default"); device.setType("default");
createFutures.add(executor.submit(() -> futures.add(executor.submit(() ->
doPost("/api/device", device, Device.class))); doPost("/api/device", device, Device.class)));
} }
List<Device> devicesTitle2 = Futures.allAsList(createFutures).get(TIMEOUT, TimeUnit.SECONDS); List<Device> devicesTitle2 = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS);
List<Device> loadedDevicesTitle1 = new ArrayList<>(143); List<Device> loadedDevicesTitle1 = new ArrayList<>(143);
PageLink pageLink = new PageLink(15, 0, title1); PageLink pageLink = new PageLink(15, 0, title1);
@ -517,20 +519,20 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
} }
ListenableFuture<List<ResultActions>> deleteDevicesAsync(String urlTemplate, List<Device> loadedDevicesTitle1) { ListenableFuture<List<ResultActions>> deleteDevicesAsync(String urlTemplate, List<Device> loadedDevicesTitle1) {
deleteFutures = new ArrayList<>(loadedDevicesTitle1.size()); List<ListenableFuture<ResultActions>> futures = new ArrayList<>(loadedDevicesTitle1.size());
for (Device device : loadedDevicesTitle1) { for (Device device : loadedDevicesTitle1) {
deleteFutures.add(executor.submit(() -> futures.add(executor.submit(() ->
doDelete(urlTemplate + device.getId().getId()) doDelete(urlTemplate + device.getId().getId())
.andExpect(status().isOk()))); .andExpect(status().isOk())));
} }
return Futures.allAsList(deleteFutures); return Futures.allAsList(futures);
} }
@Test @Test
public void testFindTenantDevicesByType() throws Exception { public void testFindTenantDevicesByType() throws Exception {
String title1 = "Device title 1"; String title1 = "Device title 1";
String type1 = "typeA"; String type1 = "typeA";
createFutures = new ArrayList<>(143); futures = new ArrayList<>(143);
for (int i = 0; i < 143; i++) { for (int i = 0; i < 143; i++) {
Device device = new Device(); Device device = new Device();
String suffix = RandomStringUtils.randomAlphanumeric(15); String suffix = RandomStringUtils.randomAlphanumeric(15);
@ -538,17 +540,17 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
device.setName(name); device.setName(name);
device.setType(type1); device.setType(type1);
createFutures.add(executor.submit(() -> futures.add(executor.submit(() ->
doPost("/api/device", device, Device.class))); doPost("/api/device", device, Device.class)));
if (i == 0) { if (i == 0) {
createFutures.get(0).get(TIMEOUT, TimeUnit.SECONDS); // wait for the device profile created first time futures.get(0).get(TIMEOUT, TimeUnit.SECONDS); // wait for the device profile created first time
} }
} }
List<Device> devicesType1 = Futures.allAsList(createFutures).get(TIMEOUT, TimeUnit.SECONDS); List<Device> devicesType1 = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS);
String title2 = "Device title 2"; String title2 = "Device title 2";
String type2 = "typeB"; String type2 = "typeB";
createFutures = new ArrayList<>(75); futures = new ArrayList<>(75);
for (int i = 0; i < 75; i++) { for (int i = 0; i < 75; i++) {
Device device = new Device(); Device device = new Device();
String suffix = RandomStringUtils.randomAlphanumeric(15); String suffix = RandomStringUtils.randomAlphanumeric(15);
@ -556,14 +558,14 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase(); name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
device.setName(name); device.setName(name);
device.setType(type2); device.setType(type2);
createFutures.add(executor.submit(() -> futures.add(executor.submit(() ->
doPost("/api/device", device, Device.class))); doPost("/api/device", device, Device.class)));
if (i == 0) { if (i == 0) {
createFutures.get(0).get(TIMEOUT, TimeUnit.SECONDS); // wait for the device profile created first time futures.get(0).get(TIMEOUT, TimeUnit.SECONDS); // wait for the device profile created first time
} }
} }
List<Device> devicesType2 = Futures.allAsList(createFutures).get(TIMEOUT, TimeUnit.SECONDS); List<Device> devicesType2 = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS);
List<Device> loadedDevicesType1 = new ArrayList<>(143); List<Device> loadedDevicesType1 = new ArrayList<>(143);
PageLink pageLink = new PageLink(15); PageLink pageLink = new PageLink(15);
@ -615,18 +617,18 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
customer = doPost("/api/customer", customer, Customer.class); customer = doPost("/api/customer", customer, Customer.class);
CustomerId customerId = customer.getId(); CustomerId customerId = customer.getId();
createFutures = new ArrayList<>(128); futures = new ArrayList<>(128);
for (int i = 0; i < 128; i++) { for (int i = 0; i < 128; i++) {
Device device = new Device(); Device device = new Device();
device.setName("Device" + i); device.setName("Device" + i);
device.setType("default"); device.setType("default");
ListenableFuture<Device> future = executor.submit(() -> doPost("/api/device", device, Device.class)); ListenableFuture<Device> future = executor.submit(() -> doPost("/api/device", device, Device.class));
createFutures.add(Futures.transform(future, (dev) -> futures.add(Futures.transform(future, (dev) ->
doPost("/api/customer/" + customerId.getId() doPost("/api/customer/" + customerId.getId()
+ "/device/" + dev.getId().getId(), Device.class), MoreExecutors.directExecutor())); + "/device/" + dev.getId().getId(), Device.class), MoreExecutors.directExecutor()));
} }
List<Device> devices = Futures.allAsList(createFutures).get(TIMEOUT, TimeUnit.SECONDS); List<Device> devices = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS);
List<Device> loadedDevices = new ArrayList<>(128); List<Device> loadedDevices = new ArrayList<>(128);
PageLink pageLink = new PageLink(23); PageLink pageLink = new PageLink(23);
@ -641,9 +643,9 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
assertThat(devices).containsExactlyInAnyOrderElementsOf(loadedDevices); assertThat(devices).containsExactlyInAnyOrderElementsOf(loadedDevices);
log.warn("delete devices async"); log.debug("delete devices async");
deleteDevicesAsync("/api/customer/device/", loadedDevices).get(TIMEOUT, TimeUnit.SECONDS); deleteDevicesAsync("/api/customer/device/", loadedDevices).get(TIMEOUT, TimeUnit.SECONDS);
log.warn("done"); log.debug("done");
} }
@Test @Test
@ -654,7 +656,7 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
CustomerId customerId = customer.getId(); CustomerId customerId = customer.getId();
String title1 = "Device title 1"; String title1 = "Device title 1";
createFutures = new ArrayList<>(125); futures = new ArrayList<>(125);
for (int i = 0; i < 125; i++) { for (int i = 0; i < 125; i++) {
Device device = new Device(); Device device = new Device();
String suffix = RandomStringUtils.randomAlphanumeric(15); String suffix = RandomStringUtils.randomAlphanumeric(15);
@ -663,14 +665,14 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
device.setName(name); device.setName(name);
device.setType("default"); device.setType("default");
ListenableFuture<Device> future = executor.submit(() -> doPost("/api/device", device, Device.class)); ListenableFuture<Device> future = executor.submit(() -> doPost("/api/device", device, Device.class));
createFutures.add(Futures.transform(future, (dev) -> futures.add(Futures.transform(future, (dev) ->
doPost("/api/customer/" + customerId.getId() doPost("/api/customer/" + customerId.getId()
+ "/device/" + dev.getId().getId(), Device.class), MoreExecutors.directExecutor())); + "/device/" + dev.getId().getId(), Device.class), MoreExecutors.directExecutor()));
} }
List<Device> devicesTitle1 = Futures.allAsList(createFutures).get(TIMEOUT, TimeUnit.SECONDS); List<Device> devicesTitle1 = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS);
String title2 = "Device title 2"; String title2 = "Device title 2";
createFutures = new ArrayList<>(143); futures = new ArrayList<>(143);
for (int i = 0; i < 143; i++) { for (int i = 0; i < 143; i++) {
Device device = new Device(); Device device = new Device();
String suffix = RandomStringUtils.randomAlphanumeric(15); String suffix = RandomStringUtils.randomAlphanumeric(15);
@ -679,11 +681,11 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
device.setName(name); device.setName(name);
device.setType("default"); device.setType("default");
ListenableFuture<Device> future = executor.submit(() -> doPost("/api/device", device, Device.class)); ListenableFuture<Device> future = executor.submit(() -> doPost("/api/device", device, Device.class));
createFutures.add(Futures.transform(future, (dev) -> futures.add(Futures.transform(future, (dev) ->
doPost("/api/customer/" + customerId.getId() doPost("/api/customer/" + customerId.getId()
+ "/device/" + dev.getId().getId(), Device.class), MoreExecutors.directExecutor())); + "/device/" + dev.getId().getId(), Device.class), MoreExecutors.directExecutor()));
} }
List<Device> devicesTitle2 = Futures.allAsList(createFutures).get(TIMEOUT, TimeUnit.SECONDS); List<Device> devicesTitle2 = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS);
List<Device> loadedDevicesTitle1 = new ArrayList<>(125); List<Device> loadedDevicesTitle1 = new ArrayList<>(125);
PageLink pageLink = new PageLink(15, 0, title1); PageLink pageLink = new PageLink(15, 0, title1);
@ -737,7 +739,7 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
String title1 = "Device title 1"; String title1 = "Device title 1";
String type1 = "typeC"; String type1 = "typeC";
createFutures = new ArrayList<>(125); futures = new ArrayList<>(125);
for (int i = 0; i < 125; i++) { for (int i = 0; i < 125; i++) {
Device device = new Device(); Device device = new Device();
String suffix = RandomStringUtils.randomAlphanumeric(15); String suffix = RandomStringUtils.randomAlphanumeric(15);
@ -746,18 +748,18 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
device.setName(name); device.setName(name);
device.setType(type1); device.setType(type1);
ListenableFuture<Device> future = executor.submit(() -> doPost("/api/device", device, Device.class)); ListenableFuture<Device> future = executor.submit(() -> doPost("/api/device", device, Device.class));
createFutures.add(Futures.transform(future, (dev) -> futures.add(Futures.transform(future, (dev) ->
doPost("/api/customer/" + customerId.getId() doPost("/api/customer/" + customerId.getId()
+ "/device/" + dev.getId().getId(), Device.class), MoreExecutors.directExecutor())); + "/device/" + dev.getId().getId(), Device.class), MoreExecutors.directExecutor()));
if (i == 0) { if (i == 0) {
createFutures.get(0).get(TIMEOUT, TimeUnit.SECONDS); // wait for the device profile created first time futures.get(0).get(TIMEOUT, TimeUnit.SECONDS); // wait for the device profile created first time
} }
} }
List<Device> devicesType1 = Futures.allAsList(createFutures).get(TIMEOUT, TimeUnit.SECONDS); List<Device> devicesType1 = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS);
String title2 = "Device title 2"; String title2 = "Device title 2";
String type2 = "typeD"; String type2 = "typeD";
createFutures = new ArrayList<>(143); futures = new ArrayList<>(143);
for (int i = 0; i < 143; i++) { for (int i = 0; i < 143; i++) {
Device device = new Device(); Device device = new Device();
String suffix = RandomStringUtils.randomAlphanumeric(15); String suffix = RandomStringUtils.randomAlphanumeric(15);
@ -766,14 +768,14 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
device.setName(name); device.setName(name);
device.setType(type2); device.setType(type2);
ListenableFuture<Device> future = executor.submit(() -> doPost("/api/device", device, Device.class)); ListenableFuture<Device> future = executor.submit(() -> doPost("/api/device", device, Device.class));
createFutures.add(Futures.transform(future, (dev) -> futures.add(Futures.transform(future, (dev) ->
doPost("/api/customer/" + customerId.getId() doPost("/api/customer/" + customerId.getId()
+ "/device/" + dev.getId().getId(), Device.class), MoreExecutors.directExecutor())); + "/device/" + dev.getId().getId(), Device.class), MoreExecutors.directExecutor()));
if (i == 0) { if (i == 0) {
createFutures.get(0).get(TIMEOUT, TimeUnit.SECONDS); // wait for the device profile created first time futures.get(0).get(TIMEOUT, TimeUnit.SECONDS); // wait for the device profile created first time
} }
} }
List<Device> devicesType2 = Futures.allAsList(createFutures).get(TIMEOUT, TimeUnit.SECONDS); List<Device> devicesType2 = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS);
List<Device> loadedDevicesType1 = new ArrayList<>(125); List<Device> loadedDevicesType1 = new ArrayList<>(125);
PageLink pageLink = new PageLink(15); PageLink pageLink = new PageLink(15);
@ -882,9 +884,8 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
doPost("/api/edge/" + savedEdge.getId().getId() doPost("/api/edge/" + savedEdge.getId().getId()
+ "/device/" + savedDevice.getId().getId(), Device.class); + "/device/" + savedDevice.getId().getId(), Device.class);
PageData<Device> pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId() + "/devices?", pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId() + "/devices?",
new TypeReference<PageData<Device>>() { PAGE_DATA_DEVICE_TYPE_REF, new PageLink(100));
}, new PageLink(100));
Assert.assertEquals(1, pageData.getData().size()); Assert.assertEquals(1, pageData.getData().size());
@ -892,8 +893,7 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest {
+ "/device/" + savedDevice.getId().getId(), Device.class); + "/device/" + savedDevice.getId().getId(), Device.class);
pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId() + "/devices?", pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId() + "/devices?",
new TypeReference<PageData<Device>>() { PAGE_DATA_DEVICE_TYPE_REF, new PageLink(100));
}, new PageLink(100));
Assert.assertEquals(0, pageData.getData().size()); Assert.assertEquals(0, pageData.getData().size());
} }