|
|
|
@ -16,6 +16,7 @@ |
|
|
|
package org.thingsboard.server.controller; |
|
|
|
|
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.mockito.ArgumentMatcher; |
|
|
|
import org.mockito.Mockito; |
|
|
|
import org.springframework.boot.test.mock.mockito.SpyBean; |
|
|
|
import org.thingsboard.server.cluster.TbClusterService; |
|
|
|
@ -33,6 +34,7 @@ import org.thingsboard.server.dao.audit.AuditLogService; |
|
|
|
import org.thingsboard.server.dao.model.ModelConstants; |
|
|
|
|
|
|
|
import java.util.Locale; |
|
|
|
import java.util.Objects; |
|
|
|
|
|
|
|
import static org.mockito.Mockito.never; |
|
|
|
import static org.mockito.Mockito.times; |
|
|
|
@ -50,9 +52,10 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { |
|
|
|
protected void testNotifyEntityAllOneTime(HasName entity, EntityId entityId, EntityId originatorId, |
|
|
|
TenantId tenantId, CustomerId customerId, UserId userId, String userName, |
|
|
|
ActionType actionType, Object... additionalInfo) { |
|
|
|
testSendNotificationMsgToEdgeServiceTime(entityId, tenantId, actionType, 1); |
|
|
|
testLogEntityActionOneTime(entity, originatorId, tenantId, customerId, userId, userName, actionType, additionalInfo); |
|
|
|
testPushMsgToRuleEngineTime(originatorId, tenantId, 1); |
|
|
|
int cntTime = 1; |
|
|
|
testSendNotificationMsgToEdgeServiceTime(entityId, tenantId, actionType, cntTime); |
|
|
|
logEntityAction(entity, originatorId, tenantId, customerId, userId, userName, actionType, cntTime, additionalInfo); |
|
|
|
testPushMsgToRuleEngineTime(originatorId, tenantId, cntTime); |
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
} |
|
|
|
|
|
|
|
@ -67,20 +70,23 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { |
|
|
|
protected void testNotifyEntityOneTimeMsgToEdgeServiceNever(HasName entity, EntityId entityId, EntityId originatorId, |
|
|
|
TenantId tenantId, CustomerId customerId, UserId userId, String userName, |
|
|
|
ActionType actionType, Object... additionalInfo) { |
|
|
|
int cntTime = 1; |
|
|
|
testNotificationMsgToEdgeServiceNever(entityId); |
|
|
|
testLogEntityActionOneTime(entity, originatorId, tenantId, customerId, userId, userName, actionType, additionalInfo); |
|
|
|
testPushMsgToRuleEngineTime(originatorId, tenantId, 1); |
|
|
|
logEntityAction(entity, originatorId, tenantId, customerId, userId, userName, actionType, cntTime, additionalInfo); |
|
|
|
testPushMsgToRuleEngineTime(originatorId, tenantId, cntTime); |
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
} |
|
|
|
|
|
|
|
protected void testNotifyEntityManyTimeMsgToEdgeServiceNever(HasName entity, HasName originator, |
|
|
|
TenantId tenantId, CustomerId customerId, UserId userId, String userName, |
|
|
|
ActionType actionType, int cntTime, Object... additionalInfo) { |
|
|
|
protected void testNotifyManyEntityManyTimeMsgToEdgeServiceNever(HasName entity, HasName originator, |
|
|
|
TenantId tenantId, CustomerId customerId, UserId userId, String userName, |
|
|
|
ActionType actionType, int cntTime, Object... additionalInfo) { |
|
|
|
EntityId entityId = createEntityId_NULL_UUID(entity); |
|
|
|
EntityId originatorId = createEntityId_NULL_UUID(originator); |
|
|
|
testNotificationMsgToEdgeServiceNever(entityId); |
|
|
|
testLogEntityActionManyTime(entity,originatorId, tenantId, customerId, |
|
|
|
userId, userName, actionType, cntTime, additionalInfo); |
|
|
|
ArgumentMatcher<HasName> matcherEntityClassEquals = argument -> argument.getClass().equals(entity.getClass()); |
|
|
|
ArgumentMatcher<EntityId> matcherOriginatorId = argument -> argument.getClass().equals(originatorId.getClass()); |
|
|
|
logEntityActionAdditionalInfo(matcherEntityClassEquals, matcherOriginatorId, tenantId, customerId, userId, userName, actionType, cntTime, |
|
|
|
additionalInfo); |
|
|
|
testPushMsgToRuleEngineTime(originatorId, tenantId, cntTime); |
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
} |
|
|
|
@ -88,37 +94,37 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { |
|
|
|
protected void testNotifyEntityBroadcastEntityStateChangeEventOneTimeMsgToEdgeServiceNever(HasName entity, EntityId entityId, EntityId originatorId, |
|
|
|
TenantId tenantId, CustomerId customerId, UserId userId, String userName, |
|
|
|
ActionType actionType, Object... additionalInfo) { |
|
|
|
int cntTime = 1; |
|
|
|
testNotificationMsgToEdgeServiceNever(entityId); |
|
|
|
testLogEntityActionOneTime(entity, originatorId, tenantId, customerId, userId, userName, actionType, additionalInfo); |
|
|
|
testPushMsgToRuleEngineTime(originatorId, tenantId, 1); |
|
|
|
testBroadcastEntityStateChangeEventTime(entityId, tenantId, 1); |
|
|
|
logEntityAction(entity, originatorId, tenantId, customerId, userId, userName, actionType, cntTime, additionalInfo); |
|
|
|
testPushMsgToRuleEngineTime(originatorId, tenantId, cntTime); |
|
|
|
testBroadcastEntityStateChangeEventTime(entityId, tenantId, cntTime); |
|
|
|
Mockito.reset(tbClusterService, auditLogService); |
|
|
|
} |
|
|
|
|
|
|
|
protected void testNotifyEntityOneTimeError(HasName entity, TenantId tenantId, |
|
|
|
UserId userId, String userName, ActionType actionType, Exception exp, |
|
|
|
Object... additionalInfo) { |
|
|
|
protected void testNotifyEntityEqualsOneTimeError(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_NULL_UUID = createEntityId_NULL_UUID(entity); |
|
|
|
testNotificationMsgToEdgeServiceNever(entity_NULL_UUID); |
|
|
|
if (additionalInfo.length > 0) { |
|
|
|
Mockito.verify(auditLogService, times(1)) |
|
|
|
.logEntityAction(Mockito.eq(tenantId), |
|
|
|
Mockito.eq(customer_NULL_UUID), Mockito.eq(userId), Mockito.eq(userName), |
|
|
|
Mockito.eq(entity_NULL_UUID), Mockito.any(entity.getClass()), Mockito.eq(actionType), |
|
|
|
Mockito.argThat(argument -> |
|
|
|
argument.getMessage().contains(exp.getMessage())), |
|
|
|
Mockito.eq(additionalInfo) |
|
|
|
); |
|
|
|
} else { |
|
|
|
Mockito.verify(auditLogService, times(1)) |
|
|
|
.logEntityAction(Mockito.eq(tenantId), |
|
|
|
Mockito.eq(customer_NULL_UUID), Mockito.eq(userId), Mockito.eq(userName), |
|
|
|
Mockito.eq(entity_NULL_UUID), Mockito.any(entity.getClass()), Mockito.eq(actionType), |
|
|
|
Mockito.argThat((argument -> argument.getMessage().contains(exp.getMessage()) & argument.getClass().equals(exp.getClass()))) |
|
|
|
); |
|
|
|
} |
|
|
|
testPushMsgToRuleEngineNever(entity_NULL_UUID); |
|
|
|
EntityId entity_originator_NULL_UUID = createEntityId_NULL_UUID(entity); |
|
|
|
testNotificationMsgToEdgeServiceNever(entity_originator_NULL_UUID); |
|
|
|
ArgumentMatcher<HasName> matcherEntityEquals = argument -> argument.getClass().equals(entity.getClass()); |
|
|
|
ArgumentMatcher<Exception> matcherError = argument -> argument.getMessage().contains(exp.getMessage()); |
|
|
|
logEntityActionErrorAdditionalInfo(matcherEntityEquals, entity_originator_NULL_UUID, tenantId, customer_NULL_UUID, userId, |
|
|
|
userName, actionType, 1, matcherError, additionalInfo); |
|
|
|
testPushMsgToRuleEngineNever(entity_originator_NULL_UUID); |
|
|
|
} |
|
|
|
|
|
|
|
protected void testNotifyEntityIsNullOneTimeError(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); |
|
|
|
ArgumentMatcher<HasName> matcherEntityIsNull = Objects::isNull; |
|
|
|
ArgumentMatcher<Exception> matcherError = argument -> argument.getMessage().contains(exp.getMessage()) & argument.getClass().equals(exp.getClass()); |
|
|
|
logEntityActionErrorAdditionalInfo(matcherEntityIsNull, entity_originator_NULL_UUID, tenantId, customer_NULL_UUID, userId, userName, actionType, 1, matcherError, additionalInfo); |
|
|
|
testPushMsgToRuleEngineNever(entity_originator_NULL_UUID); |
|
|
|
} |
|
|
|
|
|
|
|
protected void testNotifyEntityNever(EntityId entityId, HasName entity) { |
|
|
|
@ -145,47 +151,16 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { |
|
|
|
Mockito.any(entityId.getClass()), Mockito.any(), Mockito.any()); |
|
|
|
} |
|
|
|
|
|
|
|
private void testLogEntityActionOneTime(HasName entity, EntityId originatorId, TenantId tenantId, CustomerId customerId, |
|
|
|
UserId userId, String userName, ActionType actionType, Object... additionalInfo) { |
|
|
|
if (additionalInfo.length == 0) { |
|
|
|
Mockito.verify(auditLogService, times(1)).logEntityAction(Mockito.eq(tenantId), Mockito.eq(customerId), |
|
|
|
Mockito.eq(userId), Mockito.eq(userName), Mockito.eq(originatorId), |
|
|
|
Mockito.eq(entity), Mockito.eq(actionType), Mockito.isNull()); |
|
|
|
} else { |
|
|
|
String additionalInfoStr = extractParameter(String.class, 0, additionalInfo); |
|
|
|
Mockito.verify(auditLogService, times(1)).logEntityAction(Mockito.eq(tenantId), Mockito.eq(customerId), |
|
|
|
Mockito.eq(userId), Mockito.eq(userName), Mockito.eq(originatorId), |
|
|
|
Mockito.eq(entity), Mockito.eq(actionType), Mockito.isNull(), Mockito.eq(additionalInfoStr)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void testLogEntityActionManyTime(HasName entity, EntityId originatorId, TenantId tenantId, CustomerId customerId, |
|
|
|
UserId userId, String userName, ActionType actionType, int cntTime, Object... additionalInfo) { |
|
|
|
if (additionalInfo.length == 0) { |
|
|
|
Mockito.verify(auditLogService, times(cntTime)).logEntityAction(Mockito.eq(tenantId), Mockito.eq(customerId), |
|
|
|
Mockito.eq(userId), Mockito.eq(userName), Mockito.any(originatorId.getClass()), |
|
|
|
Mockito.any(entity.getClass()), Mockito.eq(actionType), Mockito.isNull()); |
|
|
|
} else { |
|
|
|
String additionalInfoStr = extractParameter(String.class, 0, additionalInfo); |
|
|
|
Mockito.verify(auditLogService, times(cntTime)).logEntityAction(Mockito.eq(tenantId), Mockito.eq(customerId), |
|
|
|
Mockito.eq(userId), Mockito.eq(userName), Mockito.any(originatorId.getClass()), |
|
|
|
Mockito.any(entity.getClass()), Mockito.eq(actionType), Mockito.isNull(), Mockito.eq(additionalInfoStr)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void testPushMsgToRuleEngineTime(EntityId originatorId, TenantId tenantId, int cntTime) { |
|
|
|
if (cntTime ==1) { |
|
|
|
Mockito.verify(tbClusterService, times(cntTime)).pushMsgToRuleEngine(Mockito.eq(tenantId), |
|
|
|
Mockito.eq(originatorId), Mockito.any(TbMsg.class), Mockito.isNull()); |
|
|
|
} else { |
|
|
|
Mockito.verify(tbClusterService, times(cntTime)).pushMsgToRuleEngine(Mockito.eq(tenantId), |
|
|
|
Mockito.any(originatorId.getClass()), Mockito.any(TbMsg.class), Mockito.isNull()); |
|
|
|
} |
|
|
|
ArgumentMatcher<EntityId> matcherOriginatorId = cntTime == 1 ? argument -> argument.equals(originatorId) : |
|
|
|
argument -> argument.getClass().equals(originatorId.getClass()); |
|
|
|
Mockito.verify(tbClusterService, times(cntTime)).pushMsgToRuleEngine(Mockito.eq(tenantId), |
|
|
|
Mockito.argThat(matcherOriginatorId), Mockito.any(TbMsg.class), Mockito.isNull()); |
|
|
|
} |
|
|
|
|
|
|
|
private void testSendNotificationMsgToEdgeServiceTime(EntityId entityId, TenantId tenantId, ActionType actionType, int cntTime) { |
|
|
|
Mockito.verify(tbClusterService, times(cntTime)).sendNotificationMsgToEdgeService(Mockito.eq(tenantId), |
|
|
|
Mockito.isNull(), Mockito.eq(entityId), Mockito.isNull(), Mockito.isNull(), |
|
|
|
Mockito.any(), Mockito.eq(entityId), Mockito.any(), Mockito.isNull(), |
|
|
|
Mockito.eq(edgeTypeByActionType(actionType))); |
|
|
|
} |
|
|
|
|
|
|
|
@ -194,6 +169,112 @@ public abstract class AbstractNotifyEntityTest extends AbstractWebTest { |
|
|
|
Mockito.any(entityId.getClass()), Mockito.any(ComponentLifecycleEvent.class)); |
|
|
|
} |
|
|
|
|
|
|
|
private void logEntityAction(HasName entity, EntityId originatorId, TenantId tenantId, |
|
|
|
CustomerId customerId, UserId userId, String userName, |
|
|
|
ActionType actionType, int cntTime, Object... additionalInfo) { |
|
|
|
ArgumentMatcher<HasName> matcherEntityEquals = argument -> argument.equals(entity); |
|
|
|
ArgumentMatcher<EntityId> matcherOriginatorId = argument -> argument.equals(originatorId); |
|
|
|
logEntityActionAdditionalInfo(matcherEntityEquals, matcherOriginatorId, tenantId, customerId, userId, userName, actionType, cntTime, additionalInfo); |
|
|
|
} |
|
|
|
|
|
|
|
private void logEntityActionAdditionalInfo(ArgumentMatcher<HasName> matcherEntity, ArgumentMatcher<EntityId> matcherOriginatorId, TenantId tenantId, |
|
|
|
CustomerId customerId, UserId userId, String userName, |
|
|
|
ActionType actionType, int cntTime, Object... additionalInfo) { |
|
|
|
switch (additionalInfo.length) { |
|
|
|
case 1: |
|
|
|
Mockito.verify(auditLogService, times(cntTime)) |
|
|
|
.logEntityAction(Mockito.eq(tenantId), |
|
|
|
Mockito.eq(customerId), |
|
|
|
Mockito.eq(userId), |
|
|
|
Mockito.eq(userName), |
|
|
|
Mockito.argThat(matcherOriginatorId), |
|
|
|
Mockito.argThat(matcherEntity), |
|
|
|
Mockito.eq(actionType), |
|
|
|
Mockito.isNull(), |
|
|
|
Mockito.eq(extractParameter(String.class, 0, additionalInfo))); |
|
|
|
break; |
|
|
|
case 2: |
|
|
|
Mockito.verify(auditLogService, times(cntTime)) |
|
|
|
.logEntityAction(Mockito.eq(tenantId), |
|
|
|
Mockito.eq(customerId), |
|
|
|
Mockito.eq(userId), |
|
|
|
Mockito.eq(userName), |
|
|
|
Mockito.argThat(matcherOriginatorId), |
|
|
|
Mockito.argThat(matcherEntity), |
|
|
|
Mockito.eq(actionType), |
|
|
|
Mockito.isNull(), |
|
|
|
Mockito.eq(extractParameter(String.class, 0, additionalInfo)), |
|
|
|
Mockito.eq(extractParameter(String.class, 1, additionalInfo))); |
|
|
|
break; |
|
|
|
case 3: |
|
|
|
Mockito.verify(auditLogService, times(cntTime)) |
|
|
|
.logEntityAction(Mockito.eq(tenantId), |
|
|
|
Mockito.eq(customerId), |
|
|
|
Mockito.eq(userId), |
|
|
|
Mockito.eq(userName), |
|
|
|
Mockito.argThat(matcherOriginatorId), |
|
|
|
Mockito.argThat(matcherEntity), |
|
|
|
Mockito.eq(actionType), |
|
|
|
Mockito.isNull(), |
|
|
|
Mockito.eq(extractParameter(String.class, 0, additionalInfo)), |
|
|
|
Mockito.eq(extractParameter(String.class, 1, additionalInfo)), |
|
|
|
Mockito.eq(extractParameter(String.class, 2, additionalInfo))); |
|
|
|
break; |
|
|
|
default: |
|
|
|
Mockito.verify(auditLogService, times(cntTime)) |
|
|
|
.logEntityAction(Mockito.eq(tenantId), |
|
|
|
Mockito.eq(customerId), |
|
|
|
Mockito.eq(userId), |
|
|
|
Mockito.eq(userName), |
|
|
|
Mockito.argThat(matcherOriginatorId), |
|
|
|
Mockito.argThat(matcherEntity), |
|
|
|
Mockito.eq(actionType), |
|
|
|
Mockito.isNull()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private void logEntityActionErrorAdditionalInfo(ArgumentMatcher<HasName> matcherEntity, EntityId originatorId, TenantId tenantId, |
|
|
|
CustomerId customerId, UserId userId, String userName, |
|
|
|
ActionType actionType, int cntTime, ArgumentMatcher<Exception> matcherError, Object... additionalInfo) { |
|
|
|
switch (additionalInfo.length) { |
|
|
|
case 1: |
|
|
|
Mockito.verify(auditLogService, times(cntTime)) |
|
|
|
.logEntityAction(Mockito.eq(tenantId), |
|
|
|
Mockito.eq(customerId), |
|
|
|
Mockito.eq(userId), |
|
|
|
Mockito.eq(userName), |
|
|
|
Mockito.eq(originatorId), |
|
|
|
Mockito.argThat(matcherEntity), |
|
|
|
Mockito.eq(actionType), |
|
|
|
Mockito.argThat(matcherError), |
|
|
|
Mockito.eq(extractParameter(String.class, 0, additionalInfo))); |
|
|
|
break; |
|
|
|
case 2: |
|
|
|
Mockito.verify(auditLogService, times(cntTime)) |
|
|
|
.logEntityAction(Mockito.eq(tenantId), |
|
|
|
Mockito.eq(customerId), |
|
|
|
Mockito.eq(userId), |
|
|
|
Mockito.eq(userName), |
|
|
|
Mockito.eq(originatorId), |
|
|
|
Mockito.argThat(matcherEntity), |
|
|
|
Mockito.eq(actionType), |
|
|
|
Mockito.argThat(matcherError), |
|
|
|
Mockito.eq(extractParameter(String.class, 0, additionalInfo)), |
|
|
|
Mockito.eq(extractParameter(String.class, 1, additionalInfo))); |
|
|
|
break; |
|
|
|
default: |
|
|
|
Mockito.verify(auditLogService, times(cntTime)) |
|
|
|
.logEntityAction(Mockito.eq(tenantId), |
|
|
|
Mockito.eq(customerId), |
|
|
|
Mockito.eq(userId), |
|
|
|
Mockito.eq(userName), |
|
|
|
Mockito.eq(originatorId), |
|
|
|
Mockito.argThat(matcherEntity), |
|
|
|
Mockito.eq(actionType), |
|
|
|
Mockito.argThat(matcherError)); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
private <T> T extractParameter(Class<T> clazz, int index, Object... additionalInfo) { |
|
|
|
T result = null; |
|
|
|
if (additionalInfo != null && additionalInfo.length > index) { |
|
|
|
|