|
|
|
@ -21,22 +21,24 @@ import com.google.common.util.concurrent.Futures; |
|
|
|
import com.google.common.util.concurrent.ListenableFuture; |
|
|
|
import com.google.common.util.concurrent.ListeningExecutorService; |
|
|
|
import com.google.common.util.concurrent.MoreExecutors; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.apache.commons.lang3.RandomStringUtils; |
|
|
|
import org.junit.After; |
|
|
|
import org.junit.Assert; |
|
|
|
import org.junit.Before; |
|
|
|
import org.junit.Test; |
|
|
|
import org.mockito.Mockito; |
|
|
|
import org.thingsboard.common.util.ThingsBoardExecutors; |
|
|
|
import org.thingsboard.server.common.data.Customer; |
|
|
|
import org.thingsboard.server.common.data.Device; |
|
|
|
import org.thingsboard.server.common.data.EntitySubtype; |
|
|
|
import org.thingsboard.server.common.data.Tenant; |
|
|
|
import org.thingsboard.server.common.data.User; |
|
|
|
import org.thingsboard.server.common.data.audit.ActionType; |
|
|
|
import org.thingsboard.server.common.data.edge.Edge; |
|
|
|
import org.thingsboard.server.common.data.id.CustomerId; |
|
|
|
import org.thingsboard.server.common.data.id.DeviceCredentialsId; |
|
|
|
import org.thingsboard.server.common.data.id.DeviceId; |
|
|
|
import org.thingsboard.server.common.data.id.EntityId; |
|
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
|
import org.thingsboard.server.common.data.page.PageLink; |
|
|
|
import org.thingsboard.server.common.data.relation.EntityRelation; |
|
|
|
@ -44,6 +46,8 @@ import org.thingsboard.server.common.data.relation.RelationTypeGroup; |
|
|
|
import org.thingsboard.server.common.data.security.Authority; |
|
|
|
import org.thingsboard.server.common.data.security.DeviceCredentials; |
|
|
|
import org.thingsboard.server.common.data.security.DeviceCredentialsType; |
|
|
|
import org.thingsboard.server.dao.exception.DataValidationException; |
|
|
|
import org.thingsboard.server.dao.exception.DeviceCredentialsValidationException; |
|
|
|
import org.thingsboard.server.dao.model.ModelConstants; |
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
@ -55,7 +59,6 @@ import static org.hamcrest.Matchers.containsString; |
|
|
|
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|
|
|
import static org.thingsboard.server.dao.model.ModelConstants.NULL_UUID; |
|
|
|
|
|
|
|
@Slf4j |
|
|
|
public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
static final TypeReference<PageData<Device>> PAGE_DATA_DEVICE_TYPE_REF = new TypeReference<>() { |
|
|
|
}; |
|
|
|
@ -70,7 +73,6 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
|
|
|
|
@Before |
|
|
|
public void beforeTest() throws Exception { |
|
|
|
log.debug("beforeTest"); |
|
|
|
executor = MoreExecutors.listeningDecorator(ThingsBoardExecutors.newWorkStealingPool(8, getClass())); |
|
|
|
|
|
|
|
loginSysAdmin(); |
|
|
|
@ -92,14 +94,12 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
|
|
|
|
@After |
|
|
|
public void afterTest() throws Exception { |
|
|
|
log.debug("afterTest..."); |
|
|
|
executor.shutdownNow(); |
|
|
|
|
|
|
|
loginSysAdmin(); |
|
|
|
|
|
|
|
doDelete("/api/tenant/" + savedTenant.getId().getId()) |
|
|
|
.andExpect(status().isOk()); |
|
|
|
log.debug("afterTest done"); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -107,8 +107,17 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
Device device = new Device(); |
|
|
|
device.setName("My device"); |
|
|
|
device.setType("default"); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
Device savedDevice = doPost("/api/device", device, Device.class); |
|
|
|
|
|
|
|
Device oldDevice = new Device(savedDevice); |
|
|
|
testNotifyEntityOneTimeMsgToEdgeServiceNever(savedDevice, savedDevice.getId(), savedDevice.getId(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.ADDED); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
|
|
|
|
Assert.assertNotNull(savedDevice); |
|
|
|
Assert.assertNotNull(savedDevice.getId()); |
|
|
|
Assert.assertTrue(savedDevice.getCreatedTime() > 0); |
|
|
|
@ -127,9 +136,15 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
Assert.assertNotNull(deviceCredentials.getCredentialsId()); |
|
|
|
Assert.assertEquals(20, deviceCredentials.getCredentialsId().length()); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
savedDevice.setName("My new device"); |
|
|
|
doPost("/api/device", savedDevice, Device.class); |
|
|
|
|
|
|
|
testNotifyEntityAllOneTime(savedDevice, savedDevice.getId(), savedDevice.getId(), savedTenant.getId(), |
|
|
|
tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.UPDATED); |
|
|
|
testNotificationUpdateGatewayOneTime(savedDevice, oldDevice); |
|
|
|
|
|
|
|
Device foundDevice = doGet("/api/device/" + savedDevice.getId().getId(), Device.class); |
|
|
|
Assert.assertEquals(foundDevice.getName(), savedDevice.getName()); |
|
|
|
} |
|
|
|
@ -139,13 +154,41 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
Device device = new Device(); |
|
|
|
device.setName(RandomStringUtils.randomAlphabetic(300)); |
|
|
|
device.setType("default"); |
|
|
|
doPost("/api/device", device).andExpect(statusReason(containsString("length of name must be equal or less than 255"))); |
|
|
|
device.setName("Normal Name"); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
String msgError = "length of name must be equal or less than 255"; |
|
|
|
doPost("/api/device", device) |
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
.andExpect(statusReason(containsString(msgError))); |
|
|
|
|
|
|
|
testNotifyEntityEqualsOneTimeError(device, savedTenant.getId(), |
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
device.setTenantId(savedTenant.getId()); |
|
|
|
msgError = "length of type must be equal or less than 255"; |
|
|
|
device.setType(RandomStringUtils.randomAlphabetic(300)); |
|
|
|
doPost("/api/device", device).andExpect(statusReason(containsString("length of type must be equal or less than 255"))); |
|
|
|
doPost("/api/device", device) |
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
.andExpect(statusReason(containsString(msgError))); |
|
|
|
|
|
|
|
testNotifyEntityEqualsOneTimeError(device, savedTenant.getId(), |
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
msgError = "length of label must be equal or less than 255"; |
|
|
|
device.setType("Normal type"); |
|
|
|
device.setLabel(RandomStringUtils.randomAlphabetic(300)); |
|
|
|
doPost("/api/device", device).andExpect(statusReason(containsString("length of label must be equal or less than 255"))); |
|
|
|
doPost("/api/device", device) |
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
.andExpect(statusReason(containsString(msgError))); |
|
|
|
|
|
|
|
testNotifyEntityEqualsOneTimeError(device, savedTenant.getId(), |
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -155,7 +198,24 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
device.setType("default"); |
|
|
|
Device savedDevice = doPost("/api/device", device, Device.class); |
|
|
|
loginDifferentTenant(); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
doPost("/api/device", savedDevice, Device.class, status().isNotFound()); |
|
|
|
|
|
|
|
testNotifyEntityNever(savedDevice.getId(), savedDevice); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
String savedDeviceIdStr = savedDevice.getId().getId().toString(); |
|
|
|
doDelete("/api/device/" + savedDeviceIdStr) |
|
|
|
.andExpect(status().isNotFound()) |
|
|
|
.andExpect(statusReason(containsString(msgErrorNoFound("Device", savedDeviceIdStr)))); |
|
|
|
|
|
|
|
testNotifyEntityNever(savedDevice.getId(), savedDevice); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
|
|
|
|
deleteDifferentTenant(); |
|
|
|
} |
|
|
|
|
|
|
|
@ -173,12 +233,24 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
@Test |
|
|
|
public void testFindDeviceTypesByTenantId() throws Exception { |
|
|
|
List<Device> devices = new ArrayList<>(); |
|
|
|
for (int i = 0; i < 3; i++) { |
|
|
|
|
|
|
|
int cntEntity = 3; |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
for (int i = 0; i < cntEntity; i++) { |
|
|
|
Device device = new Device(); |
|
|
|
device.setName("My device B" + i); |
|
|
|
device.setType("typeB"); |
|
|
|
devices.add(doPost("/api/device", device, Device.class)); |
|
|
|
} |
|
|
|
|
|
|
|
testNotifyManyEntityManyTimeMsgToEdgeServiceNever(new Device(), new Device(), |
|
|
|
savedTenant.getId(), |
|
|
|
tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.ADDED, cntEntity); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
|
|
|
|
for (int i = 0; i < 7; i++) { |
|
|
|
Device device = new Device(); |
|
|
|
device.setName("My device C" + i); |
|
|
|
@ -192,7 +264,7 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
devices.add(doPost("/api/device", device, Device.class)); |
|
|
|
} |
|
|
|
List<EntitySubtype> deviceTypes = doGetTyped("/api/device/types", |
|
|
|
new TypeReference<List<EntitySubtype>>() { |
|
|
|
new TypeReference<>() { |
|
|
|
}); |
|
|
|
|
|
|
|
Assert.assertNotNull(deviceTypes); |
|
|
|
@ -211,28 +283,52 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
device.setType("default"); |
|
|
|
Device savedDevice = doPost("/api/device", device, Device.class); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
doDelete("/api/device/" + savedDevice.getId().getId()) |
|
|
|
.andExpect(status().isOk()); |
|
|
|
|
|
|
|
doGet("/api/device/" + savedDevice.getId().getId()) |
|
|
|
.andExpect(status().isNotFound()); |
|
|
|
testNotifyEntityOneTimeMsgToEdgeServiceNever(savedDevice, savedDevice.getId(), savedDevice.getId(), savedTenant.getId(), |
|
|
|
tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.DELETED, savedDevice.getId().getId().toString()); |
|
|
|
testNotificationDeleteGatewayOneTime(savedDevice); |
|
|
|
|
|
|
|
EntityId savedDeviceId = savedDevice.getId(); |
|
|
|
doGet("/api/device/" + savedDeviceId) |
|
|
|
.andExpect(status().isNotFound()) |
|
|
|
.andExpect(statusReason(containsString(msgErrorNoFound("Device", savedDeviceId.getId().toString())))); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testSaveDeviceWithEmptyType() throws Exception { |
|
|
|
Device device = new Device(); |
|
|
|
device.setName("My device"); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
Device savedDevice = doPost("/api/device", device, Device.class); |
|
|
|
Assert.assertEquals("default", savedDevice.getType()); |
|
|
|
|
|
|
|
testNotifyEntityOneTimeMsgToEdgeServiceNever(savedDevice, savedDevice.getId(), savedDevice.getId(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.ADDED); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testSaveDeviceWithEmptyName() throws Exception { |
|
|
|
Device device = new Device(); |
|
|
|
device.setType("default"); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
String msgError = "Device name " + msgErrorShouldBeSpecified; |
|
|
|
doPost("/api/device", device) |
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
.andExpect(statusReason(containsString("Device name should be specified"))); |
|
|
|
.andExpect(statusReason(containsString(msgError))); |
|
|
|
|
|
|
|
testNotifyEntityEqualsOneTimeError(device, savedTenant.getId(), |
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -246,17 +342,33 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
customer.setTitle("My customer"); |
|
|
|
Customer savedCustomer = doPost("/api/customer", customer, Customer.class); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
Device assignedDevice = doPost("/api/customer/" + savedCustomer.getId().getId() |
|
|
|
+ "/device/" + savedDevice.getId().getId(), Device.class); |
|
|
|
Assert.assertEquals(savedCustomer.getId(), assignedDevice.getCustomerId()); |
|
|
|
|
|
|
|
testNotifyEntityAllOneTime(assignedDevice, assignedDevice.getId(), assignedDevice.getId(), savedTenant.getId(), |
|
|
|
savedCustomer.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ASSIGNED_TO_CUSTOMER, |
|
|
|
assignedDevice.getId().getId().toString(), savedCustomer.getId().getId().toString(), |
|
|
|
savedCustomer.getTitle()); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
|
|
|
|
Device foundDevice = doGet("/api/device/" + savedDevice.getId().getId(), Device.class); |
|
|
|
Assert.assertEquals(savedCustomer.getId(), foundDevice.getCustomerId()); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
Device unassignedDevice = |
|
|
|
doDelete("/api/customer/device/" + savedDevice.getId().getId(), Device.class); |
|
|
|
Assert.assertEquals(ModelConstants.NULL_UUID, unassignedDevice.getCustomerId().getId()); |
|
|
|
|
|
|
|
testNotifyEntityAllOneTime(unassignedDevice, unassignedDevice.getId(), unassignedDevice.getId(), savedTenant.getId(), |
|
|
|
savedCustomer.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.UNASSIGNED_FROM_CUSTOMER, |
|
|
|
unassignedDevice.getId().getId().toString(), savedCustomer.getId().getId().toString(), |
|
|
|
savedCustomer.getTitle()); |
|
|
|
testNotificationDeleteGatewayNever(); |
|
|
|
|
|
|
|
foundDevice = doGet("/api/device/" + savedDevice.getId().getId(), Device.class); |
|
|
|
Assert.assertEquals(ModelConstants.NULL_UUID, foundDevice.getCustomerId().getId()); |
|
|
|
} |
|
|
|
@ -267,9 +379,17 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
device.setName("My device"); |
|
|
|
device.setType("default"); |
|
|
|
Device savedDevice = doPost("/api/device", device, Device.class); |
|
|
|
doPost("/api/customer/" + Uuids.timeBased().toString() |
|
|
|
+ "/device/" + savedDevice.getId().getId()) |
|
|
|
.andExpect(status().isNotFound()); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
String customerIdStr = savedDevice.getId().toString(); |
|
|
|
doPost("/api/customer/" + customerIdStr |
|
|
|
+ "/device/" + savedDevice.getId().getId()) |
|
|
|
.andExpect(status().isNotFound()) |
|
|
|
.andExpect(statusReason(containsString(msgErrorNoFound("Customer", customerIdStr)))); |
|
|
|
|
|
|
|
testNotifyEntityNever(savedDevice.getId(), savedDevice); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -288,7 +408,7 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
tenantAdmin2.setFirstName("Joe"); |
|
|
|
tenantAdmin2.setLastName("Downs"); |
|
|
|
|
|
|
|
tenantAdmin2 = createUserAndLogin(tenantAdmin2, "testPassword1"); |
|
|
|
createUserAndLogin(tenantAdmin2, "testPassword1"); |
|
|
|
|
|
|
|
Customer customer = new Customer(); |
|
|
|
customer.setTitle("Different customer"); |
|
|
|
@ -301,9 +421,15 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
device.setType("default"); |
|
|
|
Device savedDevice = doPost("/api/device", device, Device.class); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
doPost("/api/customer/" + savedCustomer.getId().getId() |
|
|
|
+ "/device/" + savedDevice.getId().getId()) |
|
|
|
.andExpect(status().isForbidden()); |
|
|
|
.andExpect(status().isForbidden()) |
|
|
|
.andExpect(statusReason(containsString(msgErrorPermission))); |
|
|
|
|
|
|
|
testNotifyEntityNever(savedDevice.getId(), savedDevice); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
|
|
|
|
loginSysAdmin(); |
|
|
|
|
|
|
|
@ -333,9 +459,16 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
Assert.assertEquals(savedDevice.getId(), deviceCredentials.getDeviceId()); |
|
|
|
deviceCredentials.setCredentialsType(DeviceCredentialsType.ACCESS_TOKEN); |
|
|
|
deviceCredentials.setCredentialsId("access_token"); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
doPost("/api/device/credentials", deviceCredentials) |
|
|
|
.andExpect(status().isOk()); |
|
|
|
|
|
|
|
testNotifyEntityMsgToEdgePushMsgToCoreOneTime(savedDevice, savedDevice.getId(), savedDevice.getId(), savedTenant.getId(), |
|
|
|
tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.CREDENTIALS_UPDATED, deviceCredentials); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
|
|
|
|
DeviceCredentials foundDeviceCredentials = |
|
|
|
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class); |
|
|
|
|
|
|
|
@ -345,8 +478,15 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
@Test |
|
|
|
public void testSaveDeviceCredentialsWithEmptyDevice() throws Exception { |
|
|
|
DeviceCredentials deviceCredentials = new DeviceCredentials(); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
doPost("/api/device/credentials", deviceCredentials) |
|
|
|
.andExpect(status().isBadRequest()); |
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
.andExpect(statusReason(containsString("Incorrect deviceId null"))); |
|
|
|
|
|
|
|
testNotifyEntityNever(deviceCredentials.getDeviceId(), new Device()); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -358,9 +498,18 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
DeviceCredentials deviceCredentials = |
|
|
|
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class); |
|
|
|
deviceCredentials.setCredentialsType(null); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
String msgError = "Device credentials type " + msgErrorShouldBeSpecified; |
|
|
|
doPost("/api/device/credentials", deviceCredentials) |
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
.andExpect(statusReason(containsString("Device credentials type should be specified"))); |
|
|
|
.andExpect(statusReason(containsString(msgError))); |
|
|
|
|
|
|
|
testNotifyEntityIsNullOneTimeError(device, savedTenant.getId(), |
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.CREDENTIALS_UPDATED, |
|
|
|
new DataValidationException(msgError), deviceCredentials); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -372,9 +521,18 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
DeviceCredentials deviceCredentials = |
|
|
|
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class); |
|
|
|
deviceCredentials.setCredentialsId(null); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
String msgError = "Device credentials id " + msgErrorShouldBeSpecified; |
|
|
|
doPost("/api/device/credentials", deviceCredentials) |
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
.andExpect(statusReason(containsString("Device credentials id should be specified"))); |
|
|
|
.andExpect(statusReason(containsString(msgError))); |
|
|
|
|
|
|
|
testNotifyEntityIsNullOneTimeError(device, savedTenant.getId(), |
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.CREDENTIALS_UPDATED, |
|
|
|
new DeviceCredentialsValidationException(msgError), deviceCredentials); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -390,9 +548,18 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
newDeviceCredentials.setDeviceId(deviceCredentials.getDeviceId()); |
|
|
|
newDeviceCredentials.setCredentialsType(deviceCredentials.getCredentialsType()); |
|
|
|
newDeviceCredentials.setCredentialsId(deviceCredentials.getCredentialsId()); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
String msgError = "Unable to update non-existent device credentials"; |
|
|
|
doPost("/api/device/credentials", newDeviceCredentials) |
|
|
|
.andExpect(status().isBadRequest()) |
|
|
|
.andExpect(statusReason(containsString("Unable to update non-existent device credentials"))); |
|
|
|
.andExpect(statusReason(containsString(msgError))); |
|
|
|
|
|
|
|
testNotifyEntityIsNullOneTimeError(device, savedTenant.getId(), |
|
|
|
tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.CREDENTIALS_UPDATED, |
|
|
|
new DeviceCredentialsValidationException(msgError), newDeviceCredentials); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -401,29 +568,44 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
device.setName("My device"); |
|
|
|
device.setType("default"); |
|
|
|
Device savedDevice = doPost("/api/device", device, Device.class); |
|
|
|
DeviceId deviceTimeBasedId = new DeviceId(Uuids.timeBased()); |
|
|
|
DeviceCredentials deviceCredentials = |
|
|
|
doGet("/api/device/" + savedDevice.getId().getId() + "/credentials", DeviceCredentials.class); |
|
|
|
deviceCredentials.setDeviceId(new DeviceId(Uuids.timeBased())); |
|
|
|
deviceCredentials.setDeviceId(deviceTimeBasedId); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
doPost("/api/device/credentials", deviceCredentials) |
|
|
|
.andExpect(status().isNotFound()); |
|
|
|
.andExpect(status().isNotFound()) |
|
|
|
.andExpect(statusReason(containsString(msgErrorNoFound("Device", deviceTimeBasedId.toString())))); |
|
|
|
|
|
|
|
testNotifyEntityNever(savedDevice.getId(), savedDevice); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testFindTenantDevices() throws Exception { |
|
|
|
log.debug("testFindTenantDevices"); |
|
|
|
futures = new ArrayList<>(178); |
|
|
|
for (int i = 0; i < 178; i++) { |
|
|
|
int cntEntity = 178; |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
futures = new ArrayList<>(cntEntity); |
|
|
|
for (int i = 0; i < cntEntity; i++) { |
|
|
|
Device device = new Device(); |
|
|
|
device.setName("Device" + i); |
|
|
|
device.setType("default"); |
|
|
|
futures.add(executor.submit(() -> |
|
|
|
doPost("/api/device", device, Device.class))); |
|
|
|
} |
|
|
|
log.debug("await create devices"); |
|
|
|
|
|
|
|
List<Device> devices = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS); |
|
|
|
|
|
|
|
log.debug("start reading"); |
|
|
|
List<Device> loadedDevices = new ArrayList<>(178); |
|
|
|
testNotifyManyEntityManyTimeMsgToEdgeServiceNever(new Device(), new Device(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.ADDED, cntEntity); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
|
|
|
|
List<Device> loadedDevices = new ArrayList<>(cntEntity); |
|
|
|
PageLink pageLink = new PageLink(23); |
|
|
|
do { |
|
|
|
pageData = doGetTypedWithPageLink("/api/tenant/devices?", |
|
|
|
@ -435,11 +617,16 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
} |
|
|
|
} while (pageData.hasNext()); |
|
|
|
|
|
|
|
log.debug("asserting"); |
|
|
|
assertThat(devices).containsExactlyInAnyOrderElementsOf(loadedDevices); |
|
|
|
log.debug("delete devices async"); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
deleteEntitiesAsync("/api/device/", loadedDevices, executor).get(TIMEOUT, TimeUnit.SECONDS); |
|
|
|
log.debug("done"); |
|
|
|
|
|
|
|
testNotifyManyEntityManyTimeMsgToEdgeServiceNeverAdditionalInfoAny(new Device(), new Device(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.DELETED, cntEntity, 1); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -604,9 +791,12 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
customer.setTitle("Test customer"); |
|
|
|
customer = doPost("/api/customer", customer, Customer.class); |
|
|
|
CustomerId customerId = customer.getId(); |
|
|
|
int cntEntity = 128; |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
futures = new ArrayList<>(128); |
|
|
|
for (int i = 0; i < 128; i++) { |
|
|
|
futures = new ArrayList<>(cntEntity); |
|
|
|
for (int i = 0; i < cntEntity; i++) { |
|
|
|
Device device = new Device(); |
|
|
|
device.setName("Device" + i); |
|
|
|
device.setType("default"); |
|
|
|
@ -618,7 +808,12 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
|
|
|
|
List<Device> devices = Futures.allAsList(futures).get(TIMEOUT, TimeUnit.SECONDS); |
|
|
|
|
|
|
|
List<Device> loadedDevices = new ArrayList<>(128); |
|
|
|
testNotifyManyEntityManyTimeMsgToEdgeServiceEntityEqAny(new Device(), new Device(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.ADDED, ActionType.ASSIGNED_TO_CUSTOMER, cntEntity, cntEntity, cntEntity * 2); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
|
|
|
|
List<Device> loadedDevices = new ArrayList<>(cntEntity); |
|
|
|
PageLink pageLink = new PageLink(23); |
|
|
|
do { |
|
|
|
pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId() + "/devices?", |
|
|
|
@ -631,9 +826,14 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
|
|
|
|
assertThat(devices).containsExactlyInAnyOrderElementsOf(loadedDevices); |
|
|
|
|
|
|
|
log.debug("delete devices async"); |
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
deleteEntitiesAsync("/api/customer/device/", loadedDevices, executor).get(TIMEOUT, TimeUnit.SECONDS); |
|
|
|
log.debug("done"); |
|
|
|
|
|
|
|
testNotifyManyEntityManyTimeMsgToEdgeServiceEntityEqAnyAdditionalInfoAny(new Device(), new Device(), |
|
|
|
savedTenant.getId(), customerId, tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.UNASSIGNED_FROM_CUSTOMER, ActionType.UNASSIGNED_FROM_CUSTOMER, cntEntity, cntEntity, 3); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
@ -843,16 +1043,30 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
createUserAndLogin(user, "testPassword1"); |
|
|
|
|
|
|
|
login("tenant2@thingsboard.org", "testPassword1"); |
|
|
|
Device assignedDevice = doPost("/api/tenant/" + savedDifferentTenant.getId().getId() + "/device/" + savedDevice.getId().getId(), Device.class); |
|
|
|
|
|
|
|
doGet("/api/device/" + assignedDevice.getId().getId(), Device.class, status().isNotFound()); |
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
Device assignedDevice = doPost("/api/tenant/" + savedDifferentTenant.getId().getId() + "/device/" |
|
|
|
+ savedDevice.getId().getId(), Device.class); |
|
|
|
|
|
|
|
doGet("/api/device/" + assignedDevice.getId().getId()) |
|
|
|
.andExpect(status().isNotFound()) |
|
|
|
.andExpect(statusReason(containsString(msgErrorNoFound("Device", assignedDevice.getId().getId().toString())))); |
|
|
|
|
|
|
|
testNotifyEntityOneTimeMsgToEdgeServiceNever(assignedDevice, assignedDevice.getId(), assignedDevice.getId(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.ASSIGNED_TO_TENANT, savedDifferentTenant.getId().getId().toString(), savedDifferentTenant.getTitle()); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
|
|
|
|
login("tenant9@thingsboard.org", "testPassword1"); |
|
|
|
|
|
|
|
Device foundDevice1 = doGet("/api/device/" + assignedDevice.getId().getId(), Device.class); |
|
|
|
Assert.assertNotNull(foundDevice1); |
|
|
|
|
|
|
|
doGet("/api/relation?fromId=" + savedDevice.getId().getId() + "&fromType=DEVICE&relationType=Contains&toId=" + savedAnotherDevice.getId().getId() + "&toType=DEVICE", EntityRelation.class, status().isNotFound()); |
|
|
|
doGet("/api/relation?fromId=" + savedDevice.getId().getId() + "&fromType=DEVICE&relationType=Contains&toId=" |
|
|
|
+ savedAnotherDevice.getId().getId() + "&toType=DEVICE") |
|
|
|
.andExpect(status().isNotFound()) |
|
|
|
.andExpect(statusReason(containsString(msgErrorNoFound("Device", savedAnotherDevice.getId().getId().toString())))); |
|
|
|
|
|
|
|
loginSysAdmin(); |
|
|
|
doDelete("/api/tenant/" + savedDifferentTenant.getId().getId()) |
|
|
|
@ -869,17 +1083,32 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { |
|
|
|
device.setType("default"); |
|
|
|
Device savedDevice = doPost("/api/device", device, Device.class); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
doPost("/api/edge/" + savedEdge.getId().getId() |
|
|
|
+ "/device/" + savedDevice.getId().getId(), Device.class); |
|
|
|
|
|
|
|
testNotifyEntityAllOneTime(savedDevice, savedDevice.getId(), savedDevice.getId(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.ASSIGNED_TO_EDGE, |
|
|
|
savedDevice.getId().getId().toString(), savedEdge.getId().getId().toString(), savedEdge.getName()); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
|
|
|
|
pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId() + "/devices?", |
|
|
|
PAGE_DATA_DEVICE_TYPE_REF, new PageLink(100)); |
|
|
|
|
|
|
|
Assert.assertEquals(1, pageData.getData().size()); |
|
|
|
|
|
|
|
Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); |
|
|
|
|
|
|
|
doDelete("/api/edge/" + savedEdge.getId().getId() |
|
|
|
+ "/device/" + savedDevice.getId().getId(), Device.class); |
|
|
|
|
|
|
|
testNotifyEntityAllOneTime(savedDevice, savedDevice.getId(), savedDevice.getId(), |
|
|
|
savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), |
|
|
|
ActionType.UNASSIGNED_FROM_EDGE, savedDevice.getId().getId().toString(), savedEdge.getId().getId().toString(), savedEdge.getName()); |
|
|
|
testNotificationUpdateGatewayNever(); |
|
|
|
|
|
|
|
pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId() + "/devices?", |
|
|
|
PAGE_DATA_DEVICE_TYPE_REF, new PageLink(100)); |
|
|
|
|
|
|
|
|