committed by
GitHub
93 changed files with 1058 additions and 292 deletions
@ -0,0 +1,43 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.constructor.notification; |
|||
|
|||
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.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.gen.edge.v1.NotificationRuleUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.NotificationTargetUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.NotificationTemplateUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
|||
|
|||
public interface NotificationMsgConstructor { |
|||
|
|||
NotificationRuleUpdateMsg constructNotificationRuleUpdateMsg(UpdateMsgType msgType, NotificationRule notificationRule); |
|||
|
|||
NotificationRuleUpdateMsg constructNotificationRuleDeleteMsg(NotificationRuleId notificationRuleId); |
|||
|
|||
NotificationTargetUpdateMsg constructNotificationTargetUpdateMsg(UpdateMsgType msgType, NotificationTarget notificationTarget); |
|||
|
|||
NotificationTargetUpdateMsg constructNotificationTargetDeleteMsg(NotificationTargetId notificationTargetId); |
|||
|
|||
NotificationTemplateUpdateMsg constructNotificationTemplateUpdateMsg(UpdateMsgType msgType, NotificationTemplate notificationTemplate); |
|||
|
|||
NotificationTemplateUpdateMsg constructNotificationTemplateDeleteMsg(NotificationTemplateId notificationTemplateId); |
|||
|
|||
} |
|||
@ -0,0 +1,75 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.constructor.notification; |
|||
|
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
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.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.gen.edge.v1.NotificationRuleUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.NotificationTargetUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.NotificationTemplateUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
|
|||
@Component |
|||
@TbCoreComponent |
|||
public class NotificationMsgConstructorImpl implements NotificationMsgConstructor { |
|||
|
|||
@Override |
|||
public NotificationRuleUpdateMsg constructNotificationRuleUpdateMsg(UpdateMsgType msgType, NotificationRule notificationRule) { |
|||
return NotificationRuleUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(notificationRule)).build(); |
|||
} |
|||
|
|||
@Override |
|||
public NotificationRuleUpdateMsg constructNotificationRuleDeleteMsg(NotificationRuleId notificationRuleId) { |
|||
return NotificationRuleUpdateMsg.newBuilder() |
|||
.setMsgType(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE) |
|||
.setIdMSB(notificationRuleId.getId().getMostSignificantBits()) |
|||
.setIdLSB(notificationRuleId.getId().getLeastSignificantBits()).build(); |
|||
} |
|||
|
|||
@Override |
|||
public NotificationTargetUpdateMsg constructNotificationTargetUpdateMsg(UpdateMsgType msgType, NotificationTarget notificationTarget) { |
|||
return NotificationTargetUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(notificationTarget)).build(); |
|||
} |
|||
|
|||
@Override |
|||
public NotificationTargetUpdateMsg constructNotificationTargetDeleteMsg(NotificationTargetId notificationTargetId) { |
|||
return NotificationTargetUpdateMsg.newBuilder() |
|||
.setMsgType(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE) |
|||
.setIdMSB(notificationTargetId.getId().getMostSignificantBits()) |
|||
.setIdLSB(notificationTargetId.getId().getLeastSignificantBits()).build(); |
|||
} |
|||
|
|||
@Override |
|||
public NotificationTemplateUpdateMsg constructNotificationTemplateUpdateMsg(UpdateMsgType msgType, NotificationTemplate notificationTemplate) { |
|||
return NotificationTemplateUpdateMsg.newBuilder().setMsgType(msgType).setEntity(JacksonUtil.toString(notificationTemplate)).build(); |
|||
} |
|||
|
|||
@Override |
|||
public NotificationTemplateUpdateMsg constructNotificationTemplateDeleteMsg(NotificationTemplateId notificationTemplateId) { |
|||
return NotificationTemplateUpdateMsg.newBuilder() |
|||
.setMsgType(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE) |
|||
.setIdMSB(notificationTemplateId.getId().getMostSignificantBits()) |
|||
.setIdLSB(notificationTemplateId.getId().getLeastSignificantBits()).build(); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.fetch; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.notification.rule.NotificationRule; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.dao.notification.NotificationRuleService; |
|||
|
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class NotificationRuleEdgeEventFetcher extends BasePageableEdgeEventFetcher<NotificationRule>{ |
|||
|
|||
private NotificationRuleService notificationRuleService; |
|||
|
|||
@Override |
|||
PageData<NotificationRule> fetchEntities(TenantId tenantId, Edge edge, PageLink pageLink) { |
|||
return notificationRuleService.findNotificationRulesByTenantId(tenantId, pageLink); |
|||
} |
|||
|
|||
@Override |
|||
EdgeEvent constructEdgeEvent(TenantId tenantId, Edge edge, NotificationRule notificationRule) { |
|||
return EdgeUtils.constructEdgeEvent(tenantId, edge.getId(), EdgeEventType.NOTIFICATION_RULE, |
|||
EdgeEventActionType.ADDED, notificationRule.getId(), null); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,48 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.fetch; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.notification.targets.NotificationTarget; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.dao.notification.NotificationTargetService; |
|||
|
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class NotificationTargetEdgeEventFetcher extends BasePageableEdgeEventFetcher<NotificationTarget> { |
|||
|
|||
private NotificationTargetService notificationTargetService; |
|||
|
|||
@Override |
|||
PageData<NotificationTarget> fetchEntities(TenantId tenantId, Edge edge, PageLink pageLink) { |
|||
return notificationTargetService.findNotificationTargetsByTenantId(tenantId, pageLink); |
|||
} |
|||
|
|||
@Override |
|||
EdgeEvent constructEdgeEvent(TenantId tenantId, Edge edge, NotificationTarget notificationTarget) { |
|||
return EdgeUtils.constructEdgeEvent(tenantId, edge.getId(), EdgeEventType.NOTIFICATION_TARGET, |
|||
EdgeEventActionType.ADDED, notificationTarget.getId(), null); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,51 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.fetch; |
|||
|
|||
import lombok.AllArgsConstructor; |
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.edge.Edge; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventActionType; |
|||
import org.thingsboard.server.common.data.edge.EdgeEventType; |
|||
import org.thingsboard.server.common.data.id.TenantId; |
|||
import org.thingsboard.server.common.data.notification.NotificationType; |
|||
import org.thingsboard.server.common.data.notification.template.NotificationTemplate; |
|||
import org.thingsboard.server.common.data.page.PageData; |
|||
import org.thingsboard.server.common.data.page.PageLink; |
|||
import org.thingsboard.server.dao.notification.NotificationTemplateService; |
|||
|
|||
import java.util.List; |
|||
|
|||
@AllArgsConstructor |
|||
@Slf4j |
|||
public class NotificationTemplateEdgeEventFetcher extends BasePageableEdgeEventFetcher<NotificationTemplate> { |
|||
|
|||
private NotificationTemplateService notificationTemplateService; |
|||
|
|||
@Override |
|||
PageData<NotificationTemplate> fetchEntities(TenantId tenantId, Edge edge, PageLink pageLink) { |
|||
return notificationTemplateService.findNotificationTemplatesByTenantIdAndNotificationTypes(tenantId, List.of(NotificationType.values()), pageLink); |
|||
} |
|||
|
|||
@Override |
|||
EdgeEvent constructEdgeEvent(TenantId tenantId, Edge edge, NotificationTemplate notificationTemplate) { |
|||
return EdgeUtils.constructEdgeEvent(tenantId, edge.getId(), EdgeEventType.NOTIFICATION_TEMPLATE, |
|||
EdgeEventActionType.ADDED, notificationTemplate.getId(), null); |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,119 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.service.edge.rpc.processor.notification; |
|||
|
|||
import lombok.extern.slf4j.Slf4j; |
|||
import org.springframework.stereotype.Component; |
|||
import org.thingsboard.server.common.data.EdgeUtils; |
|||
import org.thingsboard.server.common.data.edge.EdgeEvent; |
|||
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.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.gen.edge.v1.DownlinkMsg; |
|||
import org.thingsboard.server.gen.edge.v1.NotificationRuleUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.NotificationTargetUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.NotificationTemplateUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
|||
import org.thingsboard.server.queue.util.TbCoreComponent; |
|||
import org.thingsboard.server.service.edge.rpc.processor.BaseEdgeProcessor; |
|||
|
|||
@Slf4j |
|||
@Component |
|||
@TbCoreComponent |
|||
public class NotificationEdgeProcessor extends BaseEdgeProcessor { |
|||
|
|||
public DownlinkMsg convertNotificationRuleToDownlink(EdgeEvent edgeEvent) { |
|||
NotificationRuleId notificationRuleId = new NotificationRuleId(edgeEvent.getEntityId()); |
|||
DownlinkMsg downlinkMsg = null; |
|||
switch (edgeEvent.getAction()) { |
|||
case ADDED, UPDATED -> { |
|||
NotificationRule notificationRule = notificationRuleService.findNotificationRuleById(edgeEvent.getTenantId(), notificationRuleId); |
|||
if (notificationRule != null) { |
|||
UpdateMsgType msgType = getUpdateMsgType(edgeEvent.getAction()); |
|||
NotificationRuleUpdateMsg notificationRuleUpdateMsg = notificationMsgConstructor.constructNotificationRuleUpdateMsg(msgType, notificationRule); |
|||
downlinkMsg = DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addNotificationRuleUpdateMsg(notificationRuleUpdateMsg) |
|||
.build(); |
|||
} |
|||
} |
|||
case DELETED -> { |
|||
NotificationRuleUpdateMsg notificationRuleUpdateMsg = notificationMsgConstructor.constructNotificationRuleDeleteMsg(notificationRuleId); |
|||
downlinkMsg = DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addNotificationRuleUpdateMsg(notificationRuleUpdateMsg) |
|||
.build(); |
|||
} |
|||
} |
|||
return downlinkMsg; |
|||
} |
|||
|
|||
public DownlinkMsg convertNotificationTargetToDownlink(EdgeEvent edgeEvent) { |
|||
NotificationTargetId notificationTargetId = new NotificationTargetId(edgeEvent.getEntityId()); |
|||
DownlinkMsg downlinkMsg = null; |
|||
switch (edgeEvent.getAction()) { |
|||
case ADDED, UPDATED -> { |
|||
NotificationTarget notificationTarget = notificationTargetService.findNotificationTargetById(edgeEvent.getTenantId(), notificationTargetId); |
|||
if (notificationTarget != null) { |
|||
UpdateMsgType msgType = getUpdateMsgType(edgeEvent.getAction()); |
|||
NotificationTargetUpdateMsg notificationTargetUpdateMsg = notificationMsgConstructor.constructNotificationTargetUpdateMsg(msgType, notificationTarget); |
|||
downlinkMsg = DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addNotificationTargetUpdateMsg(notificationTargetUpdateMsg) |
|||
.build(); |
|||
} |
|||
} |
|||
case DELETED -> { |
|||
NotificationTargetUpdateMsg notificationTargetUpdateMsg = notificationMsgConstructor.constructNotificationTargetDeleteMsg(notificationTargetId); |
|||
downlinkMsg = DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addNotificationTargetUpdateMsg(notificationTargetUpdateMsg) |
|||
.build(); |
|||
} |
|||
} |
|||
return downlinkMsg; |
|||
} |
|||
|
|||
public DownlinkMsg convertNotificationTemplateToDownlink(EdgeEvent edgeEvent) { |
|||
NotificationTemplateId notificationTemplateId = new NotificationTemplateId(edgeEvent.getEntityId()); |
|||
DownlinkMsg downlinkMsg = null; |
|||
switch (edgeEvent.getAction()) { |
|||
case ADDED, UPDATED -> { |
|||
NotificationTemplate notificationTemplate = notificationTemplateService.findNotificationTemplateById(edgeEvent.getTenantId(), notificationTemplateId); |
|||
if (notificationTemplate != null) { |
|||
UpdateMsgType msgType = getUpdateMsgType(edgeEvent.getAction()); |
|||
NotificationTemplateUpdateMsg notificationTemplateUpdateMsg = notificationMsgConstructor.constructNotificationTemplateUpdateMsg(msgType, notificationTemplate); |
|||
downlinkMsg = DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addNotificationTemplateUpdateMsg(notificationTemplateUpdateMsg) |
|||
.build(); |
|||
} |
|||
} |
|||
case DELETED -> { |
|||
NotificationTemplateUpdateMsg notificationTemplateUpdateMsg = notificationMsgConstructor.constructNotificationTemplateDeleteMsg(notificationTemplateId); |
|||
downlinkMsg = DownlinkMsg.newBuilder() |
|||
.setDownlinkMsgId(EdgeUtils.nextPositiveInt()) |
|||
.addNotificationTemplateUpdateMsg(notificationTemplateUpdateMsg) |
|||
.build(); |
|||
} |
|||
} |
|||
return downlinkMsg; |
|||
} |
|||
|
|||
} |
|||
@ -0,0 +1,290 @@ |
|||
/** |
|||
* Copyright © 2016-2024 The Thingsboard Authors |
|||
* |
|||
* Licensed under the Apache License, Version 2.0 (the "License"); |
|||
* you may not use this file except in compliance with the License. |
|||
* You may obtain a copy of the License at |
|||
* |
|||
* http://www.apache.org/licenses/LICENSE-2.0
|
|||
* |
|||
* Unless required by applicable law or agreed to in writing, software |
|||
* distributed under the License is distributed on an "AS IS" BASIS, |
|||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|||
* See the License for the specific language governing permissions and |
|||
* limitations under the License. |
|||
*/ |
|||
package org.thingsboard.server.edge; |
|||
|
|||
import com.google.protobuf.AbstractMessage; |
|||
import org.junit.Assert; |
|||
import org.junit.Test; |
|||
import org.springframework.test.context.TestPropertySource; |
|||
import org.thingsboard.common.util.JacksonUtil; |
|||
import org.thingsboard.server.common.data.StringUtils; |
|||
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod; |
|||
import org.thingsboard.server.common.data.notification.NotificationType; |
|||
import org.thingsboard.server.common.data.notification.rule.EscalatedNotificationRuleRecipientsConfig; |
|||
import org.thingsboard.server.common.data.notification.rule.NotificationRule; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.config.AlarmNotificationRuleTriggerConfig; |
|||
import org.thingsboard.server.common.data.notification.rule.trigger.config.NotificationRuleTriggerType; |
|||
import org.thingsboard.server.common.data.notification.targets.NotificationTarget; |
|||
import org.thingsboard.server.common.data.notification.targets.platform.PlatformUsersNotificationTargetConfig; |
|||
import org.thingsboard.server.common.data.notification.targets.platform.TenantAdministratorsFilter; |
|||
import org.thingsboard.server.common.data.notification.template.DeliveryMethodNotificationTemplate; |
|||
import org.thingsboard.server.common.data.notification.template.EmailDeliveryMethodNotificationTemplate; |
|||
import org.thingsboard.server.common.data.notification.template.HasSubject; |
|||
import org.thingsboard.server.common.data.notification.template.MobileAppDeliveryMethodNotificationTemplate; |
|||
import org.thingsboard.server.common.data.notification.template.NotificationTemplate; |
|||
import org.thingsboard.server.common.data.notification.template.NotificationTemplateConfig; |
|||
import org.thingsboard.server.common.data.notification.template.SmsDeliveryMethodNotificationTemplate; |
|||
import org.thingsboard.server.common.data.notification.template.WebDeliveryMethodNotificationTemplate; |
|||
import org.thingsboard.server.dao.service.DaoSqlTest; |
|||
import org.thingsboard.server.gen.edge.v1.NotificationRuleUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.NotificationTargetUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.NotificationTemplateUpdateMsg; |
|||
import org.thingsboard.server.gen.edge.v1.UpdateMsgType; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.HashMap; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
import java.util.Set; |
|||
import java.util.UUID; |
|||
|
|||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
|||
|
|||
@DaoSqlTest |
|||
public class NotificationEdgeTest extends AbstractEdgeTest { |
|||
|
|||
@Test |
|||
public void testNotificationTemplate() throws Exception { |
|||
// create notification template
|
|||
edgeImitator.expectMessageAmount(1); |
|||
NotificationDeliveryMethod[] deliveryMethods = new NotificationDeliveryMethod[]{ |
|||
NotificationDeliveryMethod.WEB |
|||
}; |
|||
NotificationTemplate template = createNotificationTemplate(NotificationType.GENERAL, deliveryMethods); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
AbstractMessage latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof NotificationTemplateUpdateMsg); |
|||
NotificationTemplateUpdateMsg notificationTemplateUpdateMsg = (NotificationTemplateUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, notificationTemplateUpdateMsg.getMsgType()); |
|||
NotificationTemplate notificationTemplate = JacksonUtil.fromString(notificationTemplateUpdateMsg.getEntity(), NotificationTemplate.class, true); |
|||
Assert.assertNotNull(notificationTemplate); |
|||
Assert.assertEquals(template.getId(), notificationTemplate.getId()); |
|||
Assert.assertEquals(template.getName(), notificationTemplate.getName()); |
|||
Assert.assertEquals(template.getNotificationType(), notificationTemplate.getNotificationType()); |
|||
|
|||
// update notification template
|
|||
edgeImitator.expectMessageAmount(1); |
|||
template.setName(StringUtils.randomAlphanumeric(15)); |
|||
saveNotificationTemplate(template); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof NotificationTemplateUpdateMsg); |
|||
notificationTemplateUpdateMsg = (NotificationTemplateUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, notificationTemplateUpdateMsg.getMsgType()); |
|||
notificationTemplate = JacksonUtil.fromString(notificationTemplateUpdateMsg.getEntity(), NotificationTemplate.class, true); |
|||
Assert.assertNotNull(notificationTemplate); |
|||
Assert.assertEquals(template.getId(), notificationTemplate.getId()); |
|||
Assert.assertEquals(template.getName(), notificationTemplate.getName()); |
|||
Assert.assertEquals(template.getNotificationType(), notificationTemplate.getNotificationType()); |
|||
|
|||
// delete notification template
|
|||
edgeImitator.expectMessageAmount(1); |
|||
doDelete("/api/notification/template/" + notificationTemplate.getUuidId()) |
|||
.andExpect(status().isOk()); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof NotificationTemplateUpdateMsg); |
|||
notificationTemplateUpdateMsg = (NotificationTemplateUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, notificationTemplateUpdateMsg.getMsgType()); |
|||
Assert.assertEquals(notificationTemplate.getUuidId().getMostSignificantBits(), notificationTemplateUpdateMsg.getIdMSB()); |
|||
Assert.assertEquals(notificationTemplate.getUuidId().getLeastSignificantBits(), notificationTemplateUpdateMsg.getIdLSB()); |
|||
} |
|||
|
|||
@Test |
|||
public void testNotificationTarget() throws Exception { |
|||
// create notification target
|
|||
edgeImitator.expectMessageAmount(1); |
|||
NotificationTarget target = createNotificationTarget(); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
AbstractMessage latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof NotificationTargetUpdateMsg); |
|||
NotificationTargetUpdateMsg notificationTargetUpdateMsg = (NotificationTargetUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, notificationTargetUpdateMsg.getMsgType()); |
|||
NotificationTarget notificationTarget = JacksonUtil.fromString(notificationTargetUpdateMsg.getEntity(), NotificationTarget.class, true); |
|||
Assert.assertNotNull(notificationTarget); |
|||
Assert.assertEquals(target, notificationTarget); |
|||
|
|||
// update notification target
|
|||
edgeImitator.expectMessageAmount(1); |
|||
target.setName(StringUtils.randomAlphanumeric(15)); |
|||
target = saveNotificationTarget(target); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof NotificationTargetUpdateMsg); |
|||
notificationTargetUpdateMsg = (NotificationTargetUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, notificationTargetUpdateMsg.getMsgType()); |
|||
notificationTarget = JacksonUtil.fromString(notificationTargetUpdateMsg.getEntity(), NotificationTarget.class, true); |
|||
Assert.assertNotNull(notificationTarget); |
|||
Assert.assertEquals(target, notificationTarget); |
|||
|
|||
// delete notification target
|
|||
edgeImitator.expectMessageAmount(1); |
|||
doDelete("/api/notification/target/" + notificationTarget.getUuidId()) |
|||
.andExpect(status().isOk()); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof NotificationTargetUpdateMsg); |
|||
notificationTargetUpdateMsg = (NotificationTargetUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, notificationTargetUpdateMsg.getMsgType()); |
|||
Assert.assertEquals(notificationTarget.getUuidId().getMostSignificantBits(), notificationTargetUpdateMsg.getIdMSB()); |
|||
Assert.assertEquals(notificationTarget.getUuidId().getLeastSignificantBits(), notificationTargetUpdateMsg.getIdLSB()); |
|||
} |
|||
|
|||
@Test |
|||
public void testNotificationRule() throws Exception { |
|||
// create notification template for notification rule
|
|||
edgeImitator.expectMessageAmount(1); |
|||
NotificationDeliveryMethod[] deliveryMethods = new NotificationDeliveryMethod[]{ |
|||
NotificationDeliveryMethod.WEB, NotificationDeliveryMethod.EMAIL |
|||
}; |
|||
NotificationTemplate template = createNotificationTemplate(NotificationType.EDGE_CONNECTION, deliveryMethods); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
AbstractMessage latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof NotificationTemplateUpdateMsg); |
|||
NotificationTemplateUpdateMsg notificationTemplateUpdateMsg = (NotificationTemplateUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, notificationTemplateUpdateMsg.getMsgType()); |
|||
NotificationTemplate notificationTemplate = JacksonUtil.fromString(notificationTemplateUpdateMsg.getEntity(), NotificationTemplate.class, true); |
|||
Assert.assertNotNull(notificationTemplate); |
|||
Assert.assertEquals(template.getId(), notificationTemplate.getId()); |
|||
Assert.assertEquals(template.getName(), notificationTemplate.getName()); |
|||
Assert.assertEquals(template.getNotificationType(), notificationTemplate.getNotificationType()); |
|||
|
|||
// create notification rule
|
|||
edgeImitator.expectMessageAmount(1); |
|||
NotificationRule rule = createNotificationRule(template); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof NotificationRuleUpdateMsg); |
|||
NotificationRuleUpdateMsg notificationRuleUpdateMsg = (NotificationRuleUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, notificationRuleUpdateMsg.getMsgType()); |
|||
NotificationRule notificationRule = JacksonUtil.fromString(notificationRuleUpdateMsg.getEntity(), NotificationRule.class, true); |
|||
Assert.assertNotNull(notificationRule); |
|||
Assert.assertEquals(rule, notificationRule); |
|||
|
|||
// update notification rule
|
|||
edgeImitator.expectMessageAmount(1); |
|||
rule.setEnabled(false); |
|||
rule = saveNotificationRule(rule); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof NotificationRuleUpdateMsg); |
|||
notificationRuleUpdateMsg = (NotificationRuleUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_UPDATED_RPC_MESSAGE, notificationRuleUpdateMsg.getMsgType()); |
|||
notificationRule = JacksonUtil.fromString(notificationRuleUpdateMsg.getEntity(), NotificationRule.class, true); |
|||
Assert.assertNotNull(notificationRule); |
|||
Assert.assertEquals(rule, notificationRule); |
|||
|
|||
// delete notification rule
|
|||
edgeImitator.expectMessageAmount(1); |
|||
doDelete("/api/notification/rule/" + notificationRule.getUuidId()) |
|||
.andExpect(status().isOk()); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof NotificationRuleUpdateMsg); |
|||
notificationRuleUpdateMsg = (NotificationRuleUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, notificationRuleUpdateMsg.getMsgType()); |
|||
Assert.assertEquals(notificationRule.getUuidId().getMostSignificantBits(), notificationRuleUpdateMsg.getIdMSB()); |
|||
Assert.assertEquals(notificationRule.getUuidId().getLeastSignificantBits(), notificationRuleUpdateMsg.getIdLSB()); |
|||
|
|||
// delete notification template
|
|||
edgeImitator.expectMessageAmount(1); |
|||
doDelete("/api/notification/template/" + notificationTemplate.getUuidId()) |
|||
.andExpect(status().isOk()); |
|||
Assert.assertTrue(edgeImitator.waitForMessages()); |
|||
latestMessage = edgeImitator.getLatestMessage(); |
|||
Assert.assertTrue(latestMessage instanceof NotificationTemplateUpdateMsg); |
|||
notificationTemplateUpdateMsg = (NotificationTemplateUpdateMsg) latestMessage; |
|||
Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, notificationTemplateUpdateMsg.getMsgType()); |
|||
Assert.assertEquals(notificationTemplate.getUuidId().getMostSignificantBits(), notificationTemplateUpdateMsg.getIdMSB()); |
|||
Assert.assertEquals(notificationTemplate.getUuidId().getLeastSignificantBits(), notificationTemplateUpdateMsg.getIdLSB()); |
|||
} |
|||
|
|||
private NotificationTemplate createNotificationTemplate(NotificationType notificationType, NotificationDeliveryMethod... deliveryMethods) { |
|||
NotificationTemplate notificationTemplate = new NotificationTemplate(); |
|||
notificationTemplate.setTenantId(tenantId); |
|||
notificationTemplate.setName(StringUtils.randomAlphanumeric(15)); |
|||
notificationTemplate.setNotificationType(notificationType); |
|||
NotificationTemplateConfig config = new NotificationTemplateConfig(); |
|||
config.setDeliveryMethodsTemplates(new HashMap<>()); |
|||
for (NotificationDeliveryMethod deliveryMethod : deliveryMethods) { |
|||
DeliveryMethodNotificationTemplate deliveryMethodNotificationTemplate; |
|||
switch (deliveryMethod) { |
|||
case WEB -> deliveryMethodNotificationTemplate = new WebDeliveryMethodNotificationTemplate(); |
|||
case EMAIL -> deliveryMethodNotificationTemplate = new EmailDeliveryMethodNotificationTemplate(); |
|||
case SMS -> deliveryMethodNotificationTemplate = new SmsDeliveryMethodNotificationTemplate(); |
|||
case MOBILE_APP -> deliveryMethodNotificationTemplate = new MobileAppDeliveryMethodNotificationTemplate(); |
|||
default -> throw new IllegalArgumentException("Unsupported delivery method " + deliveryMethod); |
|||
} |
|||
deliveryMethodNotificationTemplate.setEnabled(true); |
|||
deliveryMethodNotificationTemplate.setBody("Test text"); |
|||
if (deliveryMethodNotificationTemplate instanceof HasSubject) { |
|||
((HasSubject) deliveryMethodNotificationTemplate).setSubject("Test subject"); |
|||
} |
|||
config.getDeliveryMethodsTemplates().put(deliveryMethod, deliveryMethodNotificationTemplate); |
|||
} |
|||
notificationTemplate.setConfiguration(config); |
|||
return saveNotificationTemplate(notificationTemplate); |
|||
} |
|||
|
|||
private NotificationTarget createNotificationTarget() { |
|||
NotificationTarget notificationTarget = new NotificationTarget(); |
|||
notificationTarget.setTenantId(tenantId); |
|||
notificationTarget.setName("Test target"); |
|||
|
|||
PlatformUsersNotificationTargetConfig targetConfig = new PlatformUsersNotificationTargetConfig(); |
|||
TenantAdministratorsFilter tenantAdministratorsFilter = new TenantAdministratorsFilter(); |
|||
tenantAdministratorsFilter.setTenantsIds(Set.of()); |
|||
tenantAdministratorsFilter.setTenantProfilesIds(Set.of()); |
|||
targetConfig.setUsersFilter(tenantAdministratorsFilter); |
|||
notificationTarget.setConfiguration(targetConfig); |
|||
return saveNotificationTarget(notificationTarget); |
|||
} |
|||
|
|||
private NotificationRule createNotificationRule(NotificationTemplate notificationTemplate) { |
|||
NotificationRule notificationRule = new NotificationRule(); |
|||
notificationRule.setName("Web notification on any alarm"); |
|||
notificationRule.setEnabled(true); |
|||
notificationRule.setTemplateId(notificationTemplate.getId()); |
|||
notificationRule.setTriggerType(NotificationRuleTriggerType.ALARM); |
|||
|
|||
AlarmNotificationRuleTriggerConfig triggerConfig = new AlarmNotificationRuleTriggerConfig(); |
|||
triggerConfig.setAlarmTypes(null); |
|||
triggerConfig.setAlarmSeverities(null); |
|||
triggerConfig.setNotifyOn(Set.of(AlarmNotificationRuleTriggerConfig.AlarmAction.CREATED, AlarmNotificationRuleTriggerConfig.AlarmAction.SEVERITY_CHANGED, AlarmNotificationRuleTriggerConfig.AlarmAction.ACKNOWLEDGED, AlarmNotificationRuleTriggerConfig.AlarmAction.CLEARED)); |
|||
notificationRule.setTriggerConfig(triggerConfig); |
|||
|
|||
EscalatedNotificationRuleRecipientsConfig recipientsConfig = new EscalatedNotificationRuleRecipientsConfig(); |
|||
recipientsConfig.setTriggerType(NotificationRuleTriggerType.ALARM); |
|||
Map<Integer, List<UUID>> escalationTable = new HashMap<>(); |
|||
escalationTable.put(Integer.valueOf("1"), new ArrayList<>()); |
|||
recipientsConfig.setEscalationTable(escalationTable); |
|||
notificationRule.setRecipientsConfig(recipientsConfig); |
|||
return saveNotificationRule(notificationRule); |
|||
} |
|||
|
|||
private NotificationTemplate saveNotificationTemplate(NotificationTemplate notificationTemplate) { |
|||
return doPost("/api/notification/template", notificationTemplate, NotificationTemplate.class); |
|||
} |
|||
|
|||
private NotificationTarget saveNotificationTarget(NotificationTarget notificationTarget) { |
|||
return doPost("/api/notification/target", notificationTarget, NotificationTarget.class); |
|||
} |
|||
|
|||
private NotificationRule saveNotificationRule(NotificationRule notificationRule) { |
|||
return doPost("/api/notification/rule", notificationRule, NotificationRule.class); |
|||
} |
|||
|
|||
} |
|||
Loading…
Reference in new issue