From b53f656f72cc76bc761ec055a6348d61c1e771c7 Mon Sep 17 00:00:00 2001 From: YevhenBondarenko Date: Thu, 23 Mar 2023 23:39:57 +0100 Subject: [PATCH] added notification entities to the TenantIdLoader --- .../server/actors/ActorSystemContext.java | 22 ++++++++- .../actors/ruleChain/DefaultTbContext.java | 24 ++++++++++ .../rule/engine/api/TbContext.java | 12 +++++ .../rule/engine/util/TenantIdLoader.java | 18 +++++++ .../rule/engine/util/TenantIdLoaderTest.java | 47 +++++++++++++++++++ 5 files changed, 122 insertions(+), 1 deletion(-) diff --git a/application/src/main/java/org/thingsboard/server/actors/ActorSystemContext.java b/application/src/main/java/org/thingsboard/server/actors/ActorSystemContext.java index 9ec8020544..c0fff5e387 100644 --- a/application/src/main/java/org/thingsboard/server/actors/ActorSystemContext.java +++ b/application/src/main/java/org/thingsboard/server/actors/ActorSystemContext.java @@ -70,6 +70,11 @@ import org.thingsboard.server.dao.entityview.EntityViewService; import org.thingsboard.server.dao.event.EventService; import org.thingsboard.server.dao.nosql.CassandraBufferedRateReadExecutor; import org.thingsboard.server.dao.nosql.CassandraBufferedRateWriteExecutor; +import org.thingsboard.server.dao.notification.NotificationRequestService; +import org.thingsboard.server.dao.notification.NotificationRuleProcessingService; +import org.thingsboard.server.dao.notification.NotificationRuleService; +import org.thingsboard.server.dao.notification.NotificationTargetService; +import org.thingsboard.server.dao.notification.NotificationTemplateService; import org.thingsboard.server.dao.ota.OtaPackageService; import org.thingsboard.server.dao.queue.QueueService; import org.thingsboard.server.dao.relation.RelationService; @@ -95,7 +100,6 @@ import org.thingsboard.server.service.executors.ExternalCallExecutorService; import org.thingsboard.server.service.executors.NotificationExecutorService; import org.thingsboard.server.service.executors.SharedEventLoopGroupService; import org.thingsboard.server.service.mail.MailExecutorService; -import org.thingsboard.server.dao.notification.NotificationRuleProcessingService; import org.thingsboard.server.service.profile.TbAssetProfileCache; import org.thingsboard.server.service.profile.TbDeviceProfileCache; import org.thingsboard.server.service.rpc.TbCoreDeviceRpcService; @@ -339,6 +343,22 @@ public class ActorSystemContext { @Getter private NotificationRuleProcessingService notificationRuleProcessingService; + @Autowired + @Getter + private NotificationTargetService notificationTargetService; + + @Autowired + @Getter + private NotificationTemplateService notificationTemplateService; + + @Autowired + @Getter + private NotificationRequestService notificationRequestService; + + @Autowired + @Getter + private NotificationRuleService notificationRuleService; + @Autowired @Getter private SlackService slackService; diff --git a/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java b/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java index c3e18abbb8..59d633540e 100644 --- a/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java +++ b/application/src/main/java/org/thingsboard/server/actors/ruleChain/DefaultTbContext.java @@ -88,6 +88,10 @@ import org.thingsboard.server.dao.edge.EdgeService; import org.thingsboard.server.dao.entityview.EntityViewService; import org.thingsboard.server.dao.nosql.CassandraStatementTask; import org.thingsboard.server.dao.nosql.TbResultSetFuture; +import org.thingsboard.server.dao.notification.NotificationRequestService; +import org.thingsboard.server.dao.notification.NotificationRuleService; +import org.thingsboard.server.dao.notification.NotificationTargetService; +import org.thingsboard.server.dao.notification.NotificationTemplateService; import org.thingsboard.server.dao.ota.OtaPackageService; import org.thingsboard.server.dao.queue.QueueService; import org.thingsboard.server.dao.relation.RelationService; @@ -698,6 +702,26 @@ class DefaultTbContext implements TbContext { return mainCtx.getNotificationCenter(); } + @Override + public NotificationTargetService getNotificationTargetService() { + return mainCtx.getNotificationTargetService(); + } + + @Override + public NotificationTemplateService getNotificationTemplateService() { + return mainCtx.getNotificationTemplateService(); + } + + @Override + public NotificationRequestService getNotificationRequestService() { + return mainCtx.getNotificationRequestService(); + } + + @Override + public NotificationRuleService getNotificationRuleService() { + return mainCtx.getNotificationRuleService(); + } + @Override public SlackService getSlackService() { return mainCtx.getSlackService(); diff --git a/rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/TbContext.java b/rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/TbContext.java index 654c3bbc6d..1561dac05e 100644 --- a/rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/TbContext.java +++ b/rule-engine/rule-engine-api/src/main/java/org/thingsboard/rule/engine/api/TbContext.java @@ -58,6 +58,10 @@ import org.thingsboard.server.dao.edge.EdgeService; import org.thingsboard.server.dao.entityview.EntityViewService; import org.thingsboard.server.dao.nosql.CassandraStatementTask; import org.thingsboard.server.dao.nosql.TbResultSetFuture; +import org.thingsboard.server.dao.notification.NotificationRequestService; +import org.thingsboard.server.dao.notification.NotificationRuleService; +import org.thingsboard.server.dao.notification.NotificationTargetService; +import org.thingsboard.server.dao.notification.NotificationTemplateService; import org.thingsboard.server.dao.ota.OtaPackageService; import org.thingsboard.server.dao.queue.QueueService; import org.thingsboard.server.dao.relation.RelationService; @@ -288,6 +292,14 @@ public interface TbContext { NotificationCenter getNotificationCenter(); + NotificationTargetService getNotificationTargetService(); + + NotificationTemplateService getNotificationTemplateService(); + + NotificationRequestService getNotificationRequestService(); + + NotificationRuleService getNotificationRuleService(); + SlackService getSlackService(); /** diff --git a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/TenantIdLoader.java b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/TenantIdLoader.java index 7afc017bf0..fe11b25ac0 100644 --- a/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/TenantIdLoader.java +++ b/rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/TenantIdLoader.java @@ -29,6 +29,10 @@ import org.thingsboard.server.common.data.id.DeviceProfileId; import org.thingsboard.server.common.data.id.EdgeId; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityViewId; +import org.thingsboard.server.common.data.id.NotificationRequestId; +import org.thingsboard.server.common.data.id.NotificationRuleId; +import org.thingsboard.server.common.data.id.NotificationTargetId; +import org.thingsboard.server.common.data.id.NotificationTemplateId; import org.thingsboard.server.common.data.id.OtaPackageId; import org.thingsboard.server.common.data.id.QueueId; import org.thingsboard.server.common.data.id.RpcId; @@ -123,6 +127,20 @@ public class TenantIdLoader { tenantEntity = null; } break; + case NOTIFICATION_TARGET: + tenantEntity = ctx.getNotificationTargetService().findNotificationTargetById(ctxTenantId, new NotificationTargetId(id)); + break; + case NOTIFICATION_TEMPLATE: + tenantEntity = ctx.getNotificationTemplateService().findNotificationTemplateById(ctxTenantId, new NotificationTemplateId(id)); + break; + case NOTIFICATION_REQUEST: + tenantEntity = ctx.getNotificationRequestService().findNotificationRequestById(ctxTenantId, new NotificationRequestId(id)); + break; + case NOTIFICATION: + return ctxTenantId; + case NOTIFICATION_RULE: + tenantEntity = ctx.getNotificationRuleService().findNotificationRuleById(ctxTenantId, new NotificationRuleId(id)); + break; default: throw new RuntimeException("Unexpected entity type: " + entityId.getEntityType()); } diff --git a/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/util/TenantIdLoaderTest.java b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/util/TenantIdLoaderTest.java index 6f937d7e18..2cd19eef9a 100644 --- a/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/util/TenantIdLoaderTest.java +++ b/rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/util/TenantIdLoaderTest.java @@ -48,8 +48,13 @@ import org.thingsboard.server.common.data.id.AssetProfileId; import org.thingsboard.server.common.data.id.DeviceProfileId; import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityIdFactory; +import org.thingsboard.server.common.data.id.NotificationId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantProfileId; +import org.thingsboard.server.common.data.notification.NotificationRequest; +import org.thingsboard.server.common.data.notification.rule.NotificationRule; +import org.thingsboard.server.common.data.notification.targets.NotificationTarget; +import org.thingsboard.server.common.data.notification.template.NotificationTemplate; import org.thingsboard.server.common.data.queue.Queue; import org.thingsboard.server.common.data.rpc.Rpc; import org.thingsboard.server.common.data.rule.RuleChain; @@ -63,6 +68,11 @@ import org.thingsboard.server.dao.dashboard.DashboardService; import org.thingsboard.server.dao.device.DeviceService; import org.thingsboard.server.dao.edge.EdgeService; import org.thingsboard.server.dao.entityview.EntityViewService; +import org.thingsboard.server.dao.notification.NotificationRequestService; +import org.thingsboard.server.dao.notification.NotificationRuleService; +import org.thingsboard.server.dao.notification.NotificationService; +import org.thingsboard.server.dao.notification.NotificationTargetService; +import org.thingsboard.server.dao.notification.NotificationTemplateService; import org.thingsboard.server.dao.ota.OtaPackageService; import org.thingsboard.server.dao.queue.QueueService; import org.thingsboard.server.dao.resource.ResourceService; @@ -121,9 +131,20 @@ public class TenantIdLoaderTest { private RuleEngineRpcService rpcService; @Mock private RuleEngineApiUsageStateService ruleEngineApiUsageStateService; + @Mock + private NotificationTargetService notificationTargetService; + @Mock + private NotificationTemplateService notificationTemplateService; + @Mock + private NotificationRequestService notificationRequestService; + @Mock + private NotificationService notificationService; + @Mock + private NotificationRuleService notificationRuleService; private TenantId tenantId; private TenantProfileId tenantProfileId; + private NotificationId notificationId; private AbstractListeningExecutor dbExecutor; @Before @@ -137,6 +158,7 @@ public class TenantIdLoaderTest { dbExecutor.init(); this.tenantId = new TenantId(UUID.randomUUID()); this.tenantProfileId = new TenantProfileId(UUID.randomUUID()); + this.notificationId = new NotificationId(UUID.randomUUID()); when(ctx.getTenantId()).thenReturn(tenantId); @@ -153,6 +175,7 @@ public class TenantIdLoaderTest { private void initMocks(EntityType entityType, TenantId tenantId) { switch (entityType) { case TENANT: + case NOTIFICATION: break; case CUSTOMER: Customer customer = new Customer(); @@ -310,6 +333,30 @@ public class TenantIdLoaderTest { when(ctx.getTenantProfile()).thenReturn(tenantProfile); + break; + case NOTIFICATION_TARGET: + NotificationTarget notificationTarget = new NotificationTarget(); + notificationTarget.setTenantId(tenantId); + when(ctx.getNotificationTargetService()).thenReturn(notificationTargetService); + doReturn(notificationTarget).when(notificationTargetService).findNotificationTargetById(eq(tenantId), any()); + break; + case NOTIFICATION_TEMPLATE: + NotificationTemplate notificationTemplate = new NotificationTemplate(); + notificationTemplate.setTenantId(tenantId); + when(ctx.getNotificationTemplateService()).thenReturn(notificationTemplateService); + doReturn(notificationTemplate).when(notificationTemplateService).findNotificationTemplateById(eq(tenantId), any()); + break; + case NOTIFICATION_REQUEST: + NotificationRequest notificationRequest = new NotificationRequest(); + notificationRequest.setTenantId(tenantId); + when(ctx.getNotificationRequestService()).thenReturn(notificationRequestService); + doReturn(notificationRequest).when(notificationRequestService).findNotificationRequestById(eq(tenantId), any()); + break; + case NOTIFICATION_RULE: + NotificationRule notificationRule = new NotificationRule(); + notificationRule.setTenantId(tenantId); + when(ctx.getNotificationRuleService()).thenReturn(notificationRuleService); + doReturn(notificationRule).when(notificationRuleService).findNotificationRuleById(eq(tenantId), any()); break; default: throw new RuntimeException("Unexpected original EntityType " + entityType);