From bbbe6f043ad3467db812b47a51040560f622a066 Mon Sep 17 00:00:00 2001 From: nickAS21 Date: Thu, 30 Jun 2022 17:07:06 +0300 Subject: [PATCH] refactoring: tests DeviceProfile --- .../controller/AbstractNotifyEntityTest.java | 54 +++-- .../controller/BaseAssetControllerTest.java | 12 +- .../BaseCustomerControllerTest.java | 16 +- .../BaseDashboardControllerTest.java | 4 +- .../controller/BaseDeviceControllerTest.java | 20 +- .../BaseDeviceProfileControllerTest.java | 197 ++++++++++++++---- 6 files changed, 217 insertions(+), 86 deletions(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/AbstractNotifyEntityTest.java b/application/src/test/java/org/thingsboard/server/controller/AbstractNotifyEntityTest.java index 617e0637e8..002c13c770 100644 --- a/application/src/test/java/org/thingsboard/server/controller/AbstractNotifyEntityTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/AbstractNotifyEntityTest.java @@ -41,6 +41,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Locale; import java.util.Objects; +import java.util.stream.Collectors; import static org.mockito.Mockito.never; import static org.mockito.Mockito.times; @@ -126,7 +127,7 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { ArgumentMatcher matcherEntityClassEquals = argument -> argument.getClass().equals(entity.getClass()); ArgumentMatcher matcherOriginatorId = argument -> argument.getClass().equals(originatorId.getClass()); testLogEntityActionAdditionalInfo(matcherEntityClassEquals, matcherOriginatorId, tenantId, customerId, userId, userName, actionType, cntTime, - extractMatcherAdditionalInfo(additionalInfo)); + extractMatcherAdditionalInfoClass(additionalInfo)); testPushMsgToRuleEngineTime(matcherOriginatorId, tenantId, cntTimeRuleEngine); Mockito.reset(tbClusterService, auditLogService); } @@ -158,6 +159,18 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { Mockito.reset(tbClusterService, auditLogService); } + protected void testNotifyEntityBroadcastEntityStateChangeEventOneTime(HasName entity, EntityId entityId, EntityId originatorId, + TenantId tenantId, CustomerId customerId, UserId userId, String userName, + ActionType actionType, Object... additionalInfo) { + int cntTime = 1; + testSendNotificationMsgToEdgeServiceTime(entityId, tenantId, actionType, cntTime); + testLogEntityAction(entity, originatorId, tenantId, customerId, userId, userName, actionType, cntTime, additionalInfo); + ArgumentMatcher matcherOriginatorId = argument -> argument.equals(originatorId); + testPushMsgToRuleEngineTime(matcherOriginatorId, tenantId, cntTime); + testBroadcastEntityStateChangeEventTime(entityId, tenantId, cntTime); + Mockito.reset(tbClusterService, auditLogService); + } + protected void testNotifyEntityBroadcastEntityStateChangeEventOneTimeMsgToEdgeServiceNever(HasName entity, EntityId entityId, EntityId originatorId, TenantId tenantId, CustomerId customerId, UserId userId, String userName, ActionType actionType, Object... additionalInfo) { @@ -170,7 +183,6 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { Mockito.reset(tbClusterService, auditLogService); } - protected void testNotifyEntityMsgToEdgePushMsgToCoreOneTime(HasName entity, EntityId entityId, EntityId originatorId, TenantId tenantId, CustomerId customerId, UserId userId, String userName, ActionType actionType, Object... additionalInfo) { @@ -181,10 +193,9 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { Mockito.reset(tbClusterService, auditLogService); } - - protected void testNotifyEntityEqualsOneTimeError(HasName entity, TenantId tenantId, - UserId userId, String userName, ActionType actionType, Exception exp, - Object... additionalInfo) { + protected void testNotifyEntityEqualsOneTimeServiceNeverError(HasName entity, TenantId tenantId, + UserId userId, String userName, ActionType actionType, Exception exp, + Object... additionalInfo) { CustomerId customer_NULL_UUID = (CustomerId) EntityIdFactory.getByTypeAndUuid(EntityType.CUSTOMER, ModelConstants.NULL_UUID); EntityId entity_originator_NULL_UUID = createEntityId_NULL_UUID(entity); testNotificationMsgToEdgeServiceNever(entity_originator_NULL_UUID); @@ -196,9 +207,9 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { testPushMsgToRuleEngineNever(entity_originator_NULL_UUID); } - protected void testNotifyEntityIsNullOneTimeError(HasName entity, TenantId tenantId, - UserId userId, String userName, ActionType actionType, Exception exp, - Object... additionalInfo) { + protected void testNotifyEntityIsNullOneTimeEdgeServiceNeverError(HasName entity, TenantId tenantId, + UserId userId, String userName, ActionType actionType, Exception exp, + Object... additionalInfo) { CustomerId customer_NULL_UUID = (CustomerId) EntityIdFactory.getByTypeAndUuid(EntityType.CUSTOMER, ModelConstants.NULL_UUID); EntityId entity_originator_NULL_UUID = createEntityId_NULL_UUID(entity); testNotificationMsgToEdgeServiceNever(entity_originator_NULL_UUID); @@ -222,10 +233,6 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { Mockito.verify(gatewayNotificationsService, times(1)).onDeviceUpdated(Mockito.eq(device), Mockito.eq(oldDevice)); } - protected void testNotificationUpdateGatewayTime(int cntTimes) { - Mockito.verify(gatewayNotificationsService, times(cntTimes)).onDeviceUpdated(Mockito.any(Device.class), Mockito.isNull()); - } - protected void testNotificationUpdateGatewayNever() { Mockito.verify(gatewayNotificationsService, never()).onDeviceUpdated(Mockito.any(Device.class), Mockito.any(Device.class)); } @@ -476,6 +483,14 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { return matcherAdditionalInfos; } + private List> extractMatcherAdditionalInfoClass(Object... additionalInfos) { + List> matcherAdditionalInfos = new ArrayList<>(additionalInfos.length); + for (Object additionalInfo : additionalInfos) { + matcherAdditionalInfos.add(argument -> argument.getClass().equals(extractParameter(additionalInfo.getClass(), additionalInfo).getClass())); + } + return matcherAdditionalInfos; + } + private T extractParameter(Class clazz, Object additionalInfo) { T result = null; if (additionalInfo != null) { @@ -488,9 +503,7 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { } private EntityId createEntityId_NULL_UUID(HasName entity) { - return EntityIdFactory.getByTypeAndUuid(EntityType.valueOf(entity.getClass().toString() - .substring(entity.getClass().toString().lastIndexOf(".") + 1).toUpperCase(Locale.ENGLISH)), - ModelConstants.NULL_UUID); + return EntityIdFactory.getByTypeAndUuid(entityClassToEntityTypeName(entity), ModelConstants.NULL_UUID); } protected String msgErrorFieldLength(String fieldName){ @@ -500,4 +513,13 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { protected String msgErrorNoFound(String entityClassName, String assetIdStr){ return entityClassName + " with id [" + assetIdStr + "] is not found"; } + + private String entityClassToEntityTypeName(HasName entity) { + String className = entity.getClass().toString() + .substring(entity.getClass().toString().lastIndexOf(".") + 1); + List str = className.chars() + .mapToObj(x -> (Character.isUpperCase(x)) ? "_" + Character.toString(x) : Character.toString(x)) + .collect(Collectors.toList()); + return String.join("", str).toUpperCase(Locale.ENGLISH).substring(1); + } } diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseAssetControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseAssetControllerTest.java index 1e8bff0ee5..d0cdf9adea 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseAssetControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseAssetControllerTest.java @@ -127,7 +127,7 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest { .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(asset, savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(asset, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); Mockito.reset(tbClusterService, auditLogService); @@ -138,7 +138,7 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest { .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(asset, savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(asset, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); Mockito.reset(tbClusterService, auditLogService); @@ -149,7 +149,7 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest { .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(asset, savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(asset, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); } @@ -279,7 +279,7 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest { .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityIsNullOneTimeError(savedAsset1, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), + testNotifyEntityIsNullOneTimeEdgeServiceNeverError(savedAsset1, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.DELETED, new DataValidationException(msgError), savedAsset1.getId().getId().toString()); savedView.setEntityId(savedAsset2.getId()); @@ -307,7 +307,7 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest { .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(asset, savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(asset, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); } @@ -323,7 +323,7 @@ public abstract class BaseAssetControllerTest extends AbstractControllerTest { .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(asset, savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(asset, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); } diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseCustomerControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseCustomerControllerTest.java index 817e8d4ca9..bd0a416ccf 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseCustomerControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseCustomerControllerTest.java @@ -132,7 +132,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest .andExpect(statusReason(containsString(msgError))); customer.setTenantId(savedTenant.getId()); - testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(customer,savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); Mockito.reset(tbClusterService, auditLogService); @@ -143,7 +143,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(customer,savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); Mockito.reset(tbClusterService, auditLogService); @@ -154,7 +154,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(customer,savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); Mockito.reset(tbClusterService, auditLogService); @@ -165,7 +165,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(customer,savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); Mockito.reset(tbClusterService, auditLogService); @@ -176,7 +176,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(customer,savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); Mockito.reset(tbClusterService, auditLogService); @@ -187,7 +187,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(customer,savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); } @@ -271,7 +271,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(customer,savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(customer,savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); } @@ -288,7 +288,7 @@ public abstract class BaseCustomerControllerTest extends AbstractControllerTest .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(customer, savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(customer, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); } diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseDashboardControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseDashboardControllerTest.java index 5a9a3d5578..e51a1f3bff 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseDashboardControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseDashboardControllerTest.java @@ -121,7 +121,7 @@ public abstract class BaseDashboardControllerTest extends AbstractControllerTest .andExpect(statusReason(containsString(msgError))); dashboard.setTenantId(savedTenant.getId()); - testNotifyEntityEqualsOneTimeError(dashboard, savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(dashboard, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); Mockito.reset(tbClusterService, auditLogService); } @@ -184,7 +184,7 @@ public abstract class BaseDashboardControllerTest extends AbstractControllerTest .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(dashboard, savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(dashboard, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); } diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseDeviceControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseDeviceControllerTest.java index 96f1924d11..f372456f8f 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseDeviceControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseDeviceControllerTest.java @@ -157,36 +157,36 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { Mockito.reset(tbClusterService, auditLogService, gatewayNotificationsService); - String msgError = "length of name must be equal or less than 255"; + String msgError = msgErrorFieldLength("name"); doPost("/api/device", device) .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(device, savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(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"; + msgError = msgErrorFieldLength("type"); device.setType(RandomStringUtils.randomAlphabetic(300)); doPost("/api/device", device) .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(device, savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(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"; + msgError = msgErrorFieldLength("label"); device.setType("Normal type"); device.setLabel(RandomStringUtils.randomAlphabetic(300)); doPost("/api/device", device) .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(device, savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(device, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); testNotificationUpdateGatewayNever(); } @@ -326,7 +326,7 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityEqualsOneTimeError(device, savedTenant.getId(), + testNotifyEntityEqualsOneTimeServiceNeverError(device, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); testNotificationUpdateGatewayNever(); } @@ -506,7 +506,7 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityIsNullOneTimeError(device, savedTenant.getId(), + testNotifyEntityIsNullOneTimeEdgeServiceNeverError(device, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.CREDENTIALS_UPDATED, new DataValidationException(msgError), deviceCredentials); testNotificationUpdateGatewayNever(); @@ -529,7 +529,7 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityIsNullOneTimeError(device, savedTenant.getId(), + testNotifyEntityIsNullOneTimeEdgeServiceNeverError(device, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.CREDENTIALS_UPDATED, new DeviceCredentialsValidationException(msgError), deviceCredentials); testNotificationUpdateGatewayNever(); @@ -556,7 +556,7 @@ public abstract class BaseDeviceControllerTest extends AbstractControllerTest { .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(msgError))); - testNotifyEntityIsNullOneTimeError(device, savedTenant.getId(), + testNotifyEntityIsNullOneTimeEdgeServiceNeverError(device, savedTenant.getId(), tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.CREDENTIALS_UPDATED, new DeviceCredentialsValidationException(msgError), newDeviceCredentials); testNotificationUpdateGatewayNever(); diff --git a/application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java index f427fc7826..80266563ef 100644 --- a/application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/BaseDeviceProfileControllerTest.java @@ -26,8 +26,8 @@ import org.apache.commons.lang3.RandomStringUtils; import org.junit.After; import org.junit.Assert; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; +import org.mockito.Mockito; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.DeviceProfile; import org.thingsboard.server.common.data.DeviceProfileInfo; @@ -36,6 +36,7 @@ import org.thingsboard.server.common.data.DeviceProfileType; import org.thingsboard.server.common.data.DeviceTransportType; 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.device.profile.DeviceProfileTransportConfiguration; import org.thingsboard.server.common.data.device.profile.JsonTransportPayloadConfiguration; import org.thingsboard.server.common.data.device.profile.MqttDeviceProfileTransportConfiguration; @@ -44,6 +45,7 @@ import org.thingsboard.server.common.data.device.profile.TransportPayloadTypeCon import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.security.Authority; +import org.thingsboard.server.dao.exception.DataValidationException; import java.util.ArrayList; import java.util.Collections; @@ -95,6 +97,9 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController @Test public void testSaveDeviceProfile() throws Exception { DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"); + + Mockito.reset(tbClusterService, auditLogService); + DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); Assert.assertNotNull(savedDeviceProfile); Assert.assertNotNull(savedDeviceProfile.getId()); @@ -105,23 +110,41 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController Assert.assertEquals(deviceProfile.isDefault(), savedDeviceProfile.isDefault()); Assert.assertEquals(deviceProfile.getDefaultRuleChainId(), savedDeviceProfile.getDefaultRuleChainId()); Assert.assertEquals(DeviceProfileProvisionType.DISABLED, savedDeviceProfile.getProvisionType()); + + testNotifyEntityBroadcastEntityStateChangeEventOneTime(savedDeviceProfile, savedDeviceProfile.getId(), savedDeviceProfile.getId(), + savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), + ActionType.ADDED); + savedDeviceProfile.setName("New device profile"); doPost("/api/deviceProfile", savedDeviceProfile, DeviceProfile.class); - DeviceProfile foundDeviceProfile = doGet("/api/deviceProfile/"+savedDeviceProfile.getId().getId().toString(), DeviceProfile.class); + DeviceProfile foundDeviceProfile = doGet("/api/deviceProfile/" + savedDeviceProfile.getId().getId().toString(), DeviceProfile.class); Assert.assertEquals(savedDeviceProfile.getName(), foundDeviceProfile.getName()); + + testNotifyEntityBroadcastEntityStateChangeEventOneTime(foundDeviceProfile, foundDeviceProfile.getId(), foundDeviceProfile.getId(), + savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), + ActionType.UPDATED); } @Test public void saveDeviceProfileWithViolationOfValidation() throws Exception { - doPost("/api/deviceProfile", this.createDeviceProfile(RandomStringUtils.randomAlphabetic(300))) - .andExpect(statusReason(containsString("length of name must be equal or less than 255"))); + String msgError = msgErrorFieldLength("name"); + + Mockito.reset(tbClusterService, auditLogService); + + DeviceProfile createDeviceProfile = this.createDeviceProfile(RandomStringUtils.randomAlphabetic(300)); + doPost("/api/deviceProfile", createDeviceProfile) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(msgError))); + + testNotifyEntityEqualsOneTimeServiceNeverError(createDeviceProfile, savedTenant.getId(), + tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); } @Test public void testFindDeviceProfileById() throws Exception { DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"); DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); - DeviceProfile foundDeviceProfile = doGet("/api/deviceProfile/"+savedDeviceProfile.getId().getId().toString(), DeviceProfile.class); + DeviceProfile foundDeviceProfile = doGet("/api/deviceProfile/" + savedDeviceProfile.getId().getId().toString(), DeviceProfile.class); Assert.assertNotNull(foundDeviceProfile); Assert.assertEquals(savedDeviceProfile, foundDeviceProfile); } @@ -132,15 +155,17 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController deviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); loginDifferentTenant(); + doGet("/api/deviceProfile/" + deviceProfile.getId()) - .andExpect(status().isForbidden()); + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString(msgErrorPermission))); } @Test public void testFindDeviceProfileInfoById() throws Exception { DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"); DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); - DeviceProfileInfo foundDeviceProfileInfo = doGet("/api/deviceProfileInfo/"+savedDeviceProfile.getId().getId().toString(), DeviceProfileInfo.class); + DeviceProfileInfo foundDeviceProfileInfo = doGet("/api/deviceProfileInfo/" + savedDeviceProfile.getId().getId().toString(), DeviceProfileInfo.class); Assert.assertNotNull(foundDeviceProfileInfo); Assert.assertEquals(savedDeviceProfile.getId(), foundDeviceProfileInfo.getId()); Assert.assertEquals(savedDeviceProfile.getName(), foundDeviceProfileInfo.getName()); @@ -154,7 +179,8 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController loginDifferentTenant(); doGet("/api/deviceProfileInfo/" + deviceProfile.getId()) - .andExpect(status().isForbidden()); + .andExpect(status().isForbidden()) + .andExpect(statusReason(containsString(msgErrorPermission))); } @Test @@ -172,20 +198,35 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController public void testSetDefaultDeviceProfile() throws Exception { DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile 1"); DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); - DeviceProfile defaultDeviceProfile = doPost("/api/deviceProfile/"+savedDeviceProfile.getId().getId().toString()+"/default", DeviceProfile.class); + + Mockito.reset(tbClusterService, auditLogService); + + DeviceProfile defaultDeviceProfile = doPost("/api/deviceProfile/" + savedDeviceProfile.getId().getId().toString() + "/default", DeviceProfile.class); Assert.assertNotNull(defaultDeviceProfile); DeviceProfileInfo foundDefaultDeviceProfile = doGet("/api/deviceProfileInfo/default", DeviceProfileInfo.class); Assert.assertNotNull(foundDefaultDeviceProfile); Assert.assertEquals(savedDeviceProfile.getName(), foundDefaultDeviceProfile.getName()); Assert.assertEquals(savedDeviceProfile.getId(), foundDefaultDeviceProfile.getId()); Assert.assertEquals(savedDeviceProfile.getType(), foundDefaultDeviceProfile.getType()); + + testNotifyEntityOneTimeMsgToEdgeServiceNever(defaultDeviceProfile, defaultDeviceProfile.getId(), defaultDeviceProfile.getId(), + savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), + ActionType.UPDATED); } @Test public void testSaveDeviceProfileWithEmptyName() throws Exception { DeviceProfile deviceProfile = new DeviceProfile(); - doPost("/api/deviceProfile", deviceProfile).andExpect(status().isBadRequest()) - .andExpect(statusReason(containsString("Device profile name should be specified"))); + + Mockito.reset(tbClusterService, auditLogService); + + String msgError = "Device profile name " + msgErrorShouldBeSpecified; + doPost("/api/deviceProfile", deviceProfile) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(msgError))); + + testNotifyEntityEqualsOneTimeServiceNeverError(deviceProfile, savedTenant.getId(), + tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); } @Test @@ -193,8 +234,16 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"); doPost("/api/deviceProfile", deviceProfile).andExpect(status().isOk()); DeviceProfile deviceProfile2 = this.createDeviceProfile("Device Profile"); - doPost("/api/deviceProfile", deviceProfile2).andExpect(status().isBadRequest()) - .andExpect(statusReason(containsString("Device profile with such name already exists"))); + + Mockito.reset(tbClusterService, auditLogService); + + String msgError = "Device profile with such name already exists"; + doPost("/api/deviceProfile", deviceProfile2) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(msgError))); + + testNotifyEntityEqualsOneTimeServiceNeverError(deviceProfile, savedTenant.getId(), + tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); } @Test @@ -204,24 +253,33 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController doPost("/api/deviceProfile", deviceProfile).andExpect(status().isOk()); DeviceProfile deviceProfile2 = this.createDeviceProfile("Device Profile 2"); deviceProfile2.setProvisionDeviceKey("testProvisionDeviceKey"); - doPost("/api/deviceProfile", deviceProfile2).andExpect(status().isBadRequest()) - .andExpect(statusReason(containsString("Device profile with such provision device key already exists"))); + + Mockito.reset(tbClusterService, auditLogService); + + String msgError = "Device profile with such provision device key already exists"; + doPost("/api/deviceProfile", deviceProfile2) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(msgError))); + + testNotifyEntityEqualsOneTimeServiceNeverError(deviceProfile, savedTenant.getId(), + tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(msgError)); } - @Ignore @Test - public void testChangeDeviceProfileTypeWithExistingDevices() throws Exception { + public void testChangeDeviceProfileTypeNull() throws Exception { DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"); DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); - Device device = new Device(); - device.setName("Test device"); - device.setType("default"); - device.setDeviceProfileId(savedDeviceProfile.getId()); - doPost("/api/device", device, Device.class); - //TODO uncomment once we have other device types; - //savedDeviceProfile.setType(DeviceProfileType.LWM2M); - doPost("/api/deviceProfile", savedDeviceProfile).andExpect(status().isBadRequest()) - .andExpect(statusReason(containsString("Can't change device profile type because devices referenced it"))); + + Mockito.reset(tbClusterService, auditLogService); + + savedDeviceProfile.setType(null); + String msgError = "Device profile type " + msgErrorShouldBeSpecified; + doPost("/api/deviceProfile", savedDeviceProfile) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(msgError))); + + testNotifyEntityEqualsOneTimeServiceNeverError(deviceProfile, savedTenant.getId(), + tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.UPDATED, new DataValidationException(msgError)); } @Test @@ -233,9 +291,17 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController device.setType("default"); device.setDeviceProfileId(savedDeviceProfile.getId()); doPost("/api/device", device, Device.class); + + Mockito.reset(tbClusterService, auditLogService); + + String msgError = "Can't change device profile transport type because devices referenced it"; savedDeviceProfile.setTransportType(DeviceTransportType.MQTT); - doPost("/api/deviceProfile", savedDeviceProfile).andExpect(status().isBadRequest()) - .andExpect(statusReason(containsString("Can't change device profile transport type because devices referenced it"))); + doPost("/api/deviceProfile", savedDeviceProfile) + .andExpect(status().isBadRequest()) + .andExpect(statusReason(containsString(msgError))); + + testNotifyEntityEqualsOneTimeServiceNeverError(deviceProfile, savedTenant.getId(), + tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.UPDATED, new DataValidationException(msgError)); } @Test @@ -248,11 +314,15 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController device.setType("default"); device.setDeviceProfileId(savedDeviceProfile.getId()); - Device savedDevice = doPost("/api/device", device, Device.class); + doPost("/api/device", device, Device.class); + + Mockito.reset(tbClusterService, auditLogService); doDelete("/api/deviceProfile/" + savedDeviceProfile.getId().getId().toString()) .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString("The device profile referenced by the devices cannot be deleted"))); + + testNotifyEntityNever(savedDeviceProfile.getId(), savedDeviceProfile); } @Test @@ -260,11 +330,19 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"); DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); + Mockito.reset(tbClusterService, auditLogService); + doDelete("/api/deviceProfile/" + savedDeviceProfile.getId().getId().toString()) .andExpect(status().isOk()); + String savedDeviceProfileIdFtr = savedDeviceProfile.getId().getId().toString(); + testNotifyEntityBroadcastEntityStateChangeEventOneTime(savedDeviceProfile, savedDeviceProfile.getId(), savedDeviceProfile.getId(), + savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), + ActionType.DELETED, savedDeviceProfileIdFtr); + doGet("/api/deviceProfile/" + savedDeviceProfile.getId().getId().toString()) - .andExpect(status().isNotFound()); + .andExpect(status().isNotFound()) + .andExpect(statusReason(containsString(msgErrorNoFound("Device profile", savedDeviceProfileIdFtr)))); } @Test @@ -272,21 +350,30 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController List deviceProfiles = new ArrayList<>(); PageLink pageLink = new PageLink(17); PageData pageData = doGetTypedWithPageLink("/api/deviceProfiles?", - new TypeReference>(){}, pageLink); + new TypeReference<>() { + }, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertEquals(1, pageData.getTotalElements()); deviceProfiles.addAll(pageData.getData()); - for (int i=0;i<28;i++) { - DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"+i); + Mockito.reset(tbClusterService, auditLogService); + + int cntEntity = 28; + for (int i = 0; i < cntEntity; i++) { + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile" + i); deviceProfiles.add(doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class)); } + testNotifyManyEntityManyTimeMsgToEdgeServiceEntityEqAny(new DeviceProfile(), new DeviceProfile(), + savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), + ActionType.ADDED, ActionType.ADDED, cntEntity, cntEntity, cntEntity); + List loadedDeviceProfiles = new ArrayList<>(); pageLink = new PageLink(17); do { pageData = doGetTypedWithPageLink("/api/deviceProfiles?", - new TypeReference>(){}, pageLink); + new TypeReference<>() { + }, pageLink); loadedDeviceProfiles.addAll(pageData.getData()); if (pageData.hasNext()) { pageLink = pageLink.nextPageLink(); @@ -305,9 +392,14 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController } } + testNotifyManyEntityManyTimeMsgToEdgeServiceEntityEqAny(loadedDeviceProfiles.get(0), loadedDeviceProfiles.get(0), + savedTenant.getId(), tenantAdmin.getCustomerId(), tenantAdmin.getId(), tenantAdmin.getEmail(), + ActionType.DELETED, ActionType.DELETED, cntEntity, cntEntity, cntEntity, loadedDeviceProfiles.get(0).getId().getId().toString()); + pageLink = new PageLink(17); pageData = doGetTypedWithPageLink("/api/deviceProfiles?", - new TypeReference>(){}, pageLink); + new TypeReference<>() { + }, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertEquals(1, pageData.getTotalElements()); } @@ -317,13 +409,14 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController List deviceProfiles = new ArrayList<>(); PageLink pageLink = new PageLink(17); PageData deviceProfilePageData = doGetTypedWithPageLink("/api/deviceProfiles?", - new TypeReference>(){}, pageLink); + new TypeReference>() { + }, pageLink); Assert.assertFalse(deviceProfilePageData.hasNext()); Assert.assertEquals(1, deviceProfilePageData.getTotalElements()); deviceProfiles.addAll(deviceProfilePageData.getData()); - for (int i=0;i<28;i++) { - DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile"+i); + for (int i = 0; i < 28; i++) { + DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile" + i); deviceProfiles.add(doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class)); } @@ -332,7 +425,8 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController PageData pageData; do { pageData = doGetTypedWithPageLink("/api/deviceProfileInfos?", - new TypeReference>(){}, pageLink); + new TypeReference<>() { + }, pageLink); loadedDeviceProfileInfos.addAll(pageData.getData()); if (pageData.hasNext()) { pageLink = pageLink.nextPageLink(); @@ -357,7 +451,8 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController pageLink = new PageLink(17); pageData = doGetTypedWithPageLink("/api/deviceProfileInfos?", - new TypeReference>(){}, pageLink); + new TypeReference>() { + }, pageLink); Assert.assertFalse(pageData.hasNext()); Assert.assertEquals(1, pageData.getTotalElements()); } @@ -867,7 +962,7 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController Assert.assertTrue(savedDeviceProfile.getProfileData().getTransportConfiguration() instanceof MqttDeviceProfileTransportConfiguration); MqttDeviceProfileTransportConfiguration transportConfiguration = (MqttDeviceProfileTransportConfiguration) savedDeviceProfile.getProfileData().getTransportConfiguration(); Assert.assertTrue(transportConfiguration.isSendAckOnValidationException()); - DeviceProfile foundDeviceProfile = doGet("/api/deviceProfile/"+ savedDeviceProfile.getId().getId().toString(), DeviceProfile.class); + DeviceProfile foundDeviceProfile = doGet("/api/deviceProfile/" + savedDeviceProfile.getId().getId().toString(), DeviceProfile.class); Assert.assertEquals(savedDeviceProfile, foundDeviceProfile); } @@ -877,7 +972,7 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile", mqttDeviceProfileTransportConfiguration); DeviceProfile savedDeviceProfile = doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class); Assert.assertNotNull(savedDeviceProfile); - DeviceProfile foundDeviceProfile = doGet("/api/deviceProfile/"+ savedDeviceProfile.getId().getId().toString(), DeviceProfile.class); + DeviceProfile foundDeviceProfile = doGet("/api/deviceProfile/" + savedDeviceProfile.getId().getId().toString(), DeviceProfile.class); Assert.assertEquals(savedDeviceProfile, foundDeviceProfile); return savedDeviceProfile; } @@ -886,16 +981,30 @@ public abstract class BaseDeviceProfileControllerTest extends AbstractController ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = this.createProtoTransportPayloadConfiguration(schema, schema, null, null); MqttDeviceProfileTransportConfiguration mqttDeviceProfileTransportConfiguration = this.createMqttDeviceProfileTransportConfiguration(protoTransportPayloadConfiguration, false); DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile", mqttDeviceProfileTransportConfiguration); - doPost("/api/deviceProfile", deviceProfile).andExpect(status().isBadRequest()) + + Mockito.reset(tbClusterService, auditLogService); + + doPost("/api/deviceProfile", deviceProfile) + .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(errorMsg))); + + testNotifyEntityEqualsOneTimeServiceNeverError(deviceProfile,savedTenant.getId(), + tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(errorMsg)); } private void testSaveDeviceProfileWithInvalidRpcRequestProtoSchema(String schema, String errorMsg) throws Exception { ProtoTransportPayloadConfiguration protoTransportPayloadConfiguration = this.createProtoTransportPayloadConfiguration(schema, schema, schema, null); MqttDeviceProfileTransportConfiguration mqttDeviceProfileTransportConfiguration = this.createMqttDeviceProfileTransportConfiguration(protoTransportPayloadConfiguration, false); DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile", mqttDeviceProfileTransportConfiguration); - doPost("/api/deviceProfile", deviceProfile).andExpect(status().isBadRequest()) + + Mockito.reset(tbClusterService, auditLogService); + + doPost("/api/deviceProfile", deviceProfile) + .andExpect(status().isBadRequest()) .andExpect(statusReason(containsString(errorMsg))); + + testNotifyEntityEqualsOneTimeServiceNeverError(deviceProfile,savedTenant.getId(), + tenantAdmin.getId(), tenantAdmin.getEmail(), ActionType.ADDED, new DataValidationException(errorMsg)); } private DynamicSchema getDynamicSchema(String schema) throws Exception {