Browse Source

Adapt edge mail/SMS/notification uplink handling to the 4.4 codebase

pull/16143/head
Nikita Mazurenko 1 month ago
parent
commit
663615d191
  1. 12
      application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/wl/WhiteLabelingEdgeProcessor.java
  2. 2
      application/src/main/java/org/thingsboard/server/service/mail/EdgeMailRequest.java
  3. 2
      application/src/main/java/org/thingsboard/server/service/notification/EdgeNotificationRequest.java
  4. 2
      application/src/main/java/org/thingsboard/server/service/sms/EdgeSmsRequest.java
  5. 21
      application/src/test/java/org/thingsboard/server/edge/SendEmailEdgeTest.java
  6. 30
      application/src/test/java/org/thingsboard/server/edge/SendNotificationEdgeTest.java
  7. 7
      application/src/test/java/org/thingsboard/server/edge/SendSmsEdgeTest.java
  8. 2
      application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/settings/AdminSettingsEdgeProcessorTest.java
  9. 40
      application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/wl/WhiteLabelingEdgeProcessorTest.java

12
application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/wl/WhiteLabelingEdgeProcessor.java

@ -65,6 +65,9 @@ public class WhiteLabelingEdgeProcessor extends BaseEdgeProcessor {
if (whiteLabeling == null) {
return null;
}
if (isSystemMailTemplates(whiteLabeling.getType(), tenantId)) {
return null;
}
UpdateMsgType msgType = getUpdateMsgType(edgeEvent.getAction());
WhiteLabelingProto whiteLabelingProto = EdgeMsgConstructorUtils.constructWhiteLabeling(msgType, whiteLabeling);
DownlinkMsg.Builder builder = DownlinkMsg.newBuilder()
@ -80,6 +83,9 @@ public class WhiteLabelingEdgeProcessor extends BaseEdgeProcessor {
}
case DELETED -> {
WhiteLabeling whiteLabeling = JacksonUtil.convertValue(edgeEvent.getBody(), WhiteLabeling.class);
if (whiteLabeling != null && isSystemMailTemplates(whiteLabeling.getType(), whiteLabeling.getTenantId())) {
return null;
}
WhiteLabelingProto whiteLabelingProto = EdgeMsgConstructorUtils.constructWhiteLabeling(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, whiteLabeling);
return DownlinkMsg.newBuilder()
.setDownlinkMsgId(EdgeUtils.nextPositiveInt())
@ -92,6 +98,12 @@ public class WhiteLabelingEdgeProcessor extends BaseEdgeProcessor {
return null;
}
private boolean isSystemMailTemplates(WhiteLabelingType type, TenantId tenantId) {
// System mail templates are not synced to the Edge. The Edge is a thin mail client: it
// delegates every send to the Cloud, which renders with its own templates.
return WhiteLabelingType.MAIL_TEMPLATES.equals(type) && TenantId.SYS_TENANT_ID.equals(tenantId);
}
private WhiteLabelingType getWhiteLabelingType(EdgeEventType type) {
return switch (type) {
case WHITE_LABELING -> WhiteLabelingType.GENERAL;

2
application/src/main/java/org/thingsboard/server/service/mail/EdgeMailRequest.java

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
// SPDX-License-Identifier: Apache-2.0
package org.thingsboard.server.service.mail;

2
application/src/main/java/org/thingsboard/server/service/notification/EdgeNotificationRequest.java

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
// SPDX-License-Identifier: Apache-2.0
package org.thingsboard.server.service.notification;

2
application/src/main/java/org/thingsboard/server/service/sms/EdgeSmsRequest.java

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
// SPDX-License-Identifier: Apache-2.0
package org.thingsboard.server.service.sms;

21
application/src/test/java/org/thingsboard/server/edge/SendEmailEdgeTest.java

@ -1,5 +1,6 @@
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc.
// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1
package org.thingsboard.server.edge;
import com.fasterxml.jackson.databind.JsonNode;
@ -79,7 +80,7 @@ public class SendEmailEdgeTest extends AbstractEdgeTest {
sendEmailUplink(request);
verify(mailService, timeout(30000))
.sendActivationEmail(eq("http://localhost/activate?token=abc"), eq(60000L), eq("recipient@thingsboard.io"));
.sendActivationEmail(eq(tenantId), eq("http://localhost/activate?token=abc"), eq(60000L), eq("recipient@thingsboard.io"));
}
@Test
@ -93,7 +94,7 @@ public class SendEmailEdgeTest extends AbstractEdgeTest {
sendEmailUplink(request);
verify(mailService, timeout(30000))
.sendAccountActivatedEmail(eq("http://localhost/login"), eq("recipient@thingsboard.io"));
.sendAccountActivatedEmail(eq(tenantId), eq("http://localhost/login"), eq("recipient@thingsboard.io"));
}
@Test
@ -108,7 +109,7 @@ public class SendEmailEdgeTest extends AbstractEdgeTest {
sendEmailUplink(request);
verify(mailService, timeout(30000))
.sendResetPasswordEmail(eq("http://localhost/reset?token=abc"), eq(60000L), eq("recipient@thingsboard.io"));
.sendResetPasswordEmail(eq(tenantId), eq("http://localhost/reset?token=abc"), eq(60000L), eq("recipient@thingsboard.io"));
}
@Test
@ -122,7 +123,7 @@ public class SendEmailEdgeTest extends AbstractEdgeTest {
sendEmailUplink(request);
verify(mailService, timeout(30000))
.sendPasswordWasResetEmail(eq("http://localhost/login"), eq("recipient@thingsboard.io"));
.sendPasswordWasResetEmail(eq(tenantId), eq("http://localhost/login"), eq("recipient@thingsboard.io"));
}
@Test
@ -137,7 +138,7 @@ public class SendEmailEdgeTest extends AbstractEdgeTest {
sendEmailUplink(request);
verify(mailService, timeout(30000))
.sendTwoFaVerificationEmail(eq("recipient@thingsboard.io"), eq("123456"), eq(120));
.sendTwoFaVerificationEmail(eq(tenantId), eq("recipient@thingsboard.io"), eq("123456"), eq(120));
}
@Test
@ -152,7 +153,7 @@ public class SendEmailEdgeTest extends AbstractEdgeTest {
sendEmailUplink(request);
verify(mailService, timeout(30000))
.sendAccountLockoutEmail(eq("locked@thingsboard.io"), eq("recipient@thingsboard.io"), eq(5));
.sendAccountLockoutEmail(eq(tenantId), eq("locked@thingsboard.io"), eq("recipient@thingsboard.io"), eq(5));
}
@Test
@ -167,7 +168,7 @@ public class SendEmailEdgeTest extends AbstractEdgeTest {
sendEmailUplink(request);
verify(mailService, timeout(30000))
.sendApiFeatureStateEmail(eq(ApiFeature.EMAIL), eq(ApiUsageStateValue.DISABLED), eq("recipient@thingsboard.io"), isNull());
.sendApiFeatureStateEmail(eq(tenantId), eq(ApiFeature.EMAIL), eq(ApiUsageStateValue.DISABLED), eq("recipient@thingsboard.io"), isNull());
}
@Test
@ -182,7 +183,7 @@ public class SendEmailEdgeTest extends AbstractEdgeTest {
sendEmailUplink(request);
verify(mailService, timeout(30000))
.sendTestMail(any(JsonNode.class), eq("recipient@thingsboard.io"));
.sendTestMail(eq(tenantId), any(JsonNode.class), eq("recipient@thingsboard.io"));
}
private void sendEmailUplink(EdgeMailRequest request) throws Exception {

30
application/src/test/java/org/thingsboard/server/edge/SendNotificationEdgeTest.java

@ -1,5 +1,6 @@
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc.
// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1
package org.thingsboard.server.edge;
import org.junit.Test;
@ -8,7 +9,6 @@ import org.springframework.test.context.bean.override.mockito.MockitoBean;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.rule.engine.api.notification.FirebaseService;
import org.thingsboard.rule.engine.api.notification.SlackService;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.notification.NotificationDeliveryMethod;
import org.thingsboard.server.common.data.notification.settings.MobileAppNotificationDeliveryMethodConfig;
import org.thingsboard.server.common.data.notification.settings.NotificationDeliveryMethodConfig;
@ -48,23 +48,18 @@ public class SendNotificationEdgeTest extends AbstractEdgeTest {
private NotificationSettingsService notificationSettingsService;
private void prepareNotificationSettings() {
// Slack is configured at tenant level.
SlackNotificationDeliveryMethodConfig slack = new SlackNotificationDeliveryMethodConfig();
slack.setBotToken("xoxb-edge-test-token");
NotificationSettings tenantSettings = new NotificationSettings();
Map<NotificationDeliveryMethod, NotificationDeliveryMethodConfig> tenantConfigs = new HashMap<>();
tenantConfigs.put(NotificationDeliveryMethod.SLACK, slack);
tenantSettings.setDeliveryMethodsConfigs(tenantConfigs);
notificationSettingsService.saveNotificationSettings(tenantId, tenantSettings);
// Mobile app credentials can only be configured at system level.
MobileAppNotificationDeliveryMethodConfig mobile = new MobileAppNotificationDeliveryMethodConfig();
mobile.setUseSystemSettings(false);
mobile.setFirebaseServiceAccountCredentials("{\"type\":\"service_account\"}");
NotificationSettings systemSettings = new NotificationSettings();
Map<NotificationDeliveryMethod, NotificationDeliveryMethodConfig> systemConfigs = new HashMap<>();
systemConfigs.put(NotificationDeliveryMethod.MOBILE_APP, mobile);
systemSettings.setDeliveryMethodsConfigs(systemConfigs);
notificationSettingsService.saveNotificationSettings(TenantId.SYS_TENANT_ID, systemSettings);
NotificationSettings settings = new NotificationSettings();
Map<NotificationDeliveryMethod, NotificationDeliveryMethodConfig> configs = new HashMap<>();
configs.put(NotificationDeliveryMethod.SLACK, slack);
configs.put(NotificationDeliveryMethod.MOBILE_APP, mobile);
settings.setDeliveryMethodsConfigs(configs);
notificationSettingsService.saveNotificationSettings(tenantId, settings);
}
@Test
@ -79,7 +74,7 @@ public class SendNotificationEdgeTest extends AbstractEdgeTest {
sendNotificationUplink(request);
verify(slackService, timeout(30000))
.sendMessage(eq(tenantId), anyString(), eq("C0123456789"), eq("Edge Slack notification"));
.sendMessage(eq(tenantId), anyString(), eq("C0123456789"), eq("Edge Slack notification"), any());
}
@Test
@ -105,7 +100,6 @@ public class SendNotificationEdgeTest extends AbstractEdgeTest {
.setTenantIdLSB(tenantId.getId().getLeastSignificantBits())
.setRequest(JacksonUtil.toString(request))
.build();
sendUplinkMsgAndWaitForResponse(builder -> builder.addSendNotificationUplinkMsg(sendNotificationUplinkMsg));
}

7
application/src/test/java/org/thingsboard/server/edge/SendSmsEdgeTest.java

@ -1,5 +1,6 @@
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc.
// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1
package org.thingsboard.server.edge;
import org.junit.Test;
@ -56,7 +57,7 @@ public class SendSmsEdgeTest extends AbstractEdgeTest {
sendSmsUplink(request);
verify(smsService, timeout(30000)).sendTestSms(any(TestSmsRequest.class));
verify(smsService, timeout(30000)).sendTestSms(eq(tenantId), any(TestSmsRequest.class));
}
private void sendSmsUplink(EdgeSmsRequest request) throws Exception {

2
application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/settings/AdminSettingsEdgeProcessorTest.java

@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: Copyright The Thingsboard Authors
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
// SPDX-License-Identifier: Apache-2.0
package org.thingsboard.server.service.edge.rpc.processor.settings;

40
application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/wl/WhiteLabelingEdgeProcessorTest.java

@ -0,0 +1,40 @@
// SPDX-FileCopyrightText: Copyright ThingsBoard, Inc.
// SPDX-License-Identifier: BUSL-1.1
package org.thingsboard.server.service.edge.rpc.processor.wl;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.test.util.ReflectionTestUtils;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.wl.WhiteLabelingType;
import java.util.UUID;
public class WhiteLabelingEdgeProcessorTest {
private final WhiteLabelingEdgeProcessor processor = new WhiteLabelingEdgeProcessor();
private final TenantId tenantId = new TenantId(UUID.randomUUID());
@Test
public void testSystemMailTemplatesAreNotSyncedToEdge() {
// System mail templates are rendered on the cloud, so they must never be pushed to the edge.
Assertions.assertTrue(isSystemMailTemplates(WhiteLabelingType.MAIL_TEMPLATES, TenantId.SYS_TENANT_ID));
}
@Test
public void testTenantMailTemplatesAreStillSyncedToEdge() {
// Only system-scoped mail templates are skipped; tenant-scoped ones keep syncing.
Assertions.assertFalse(isSystemMailTemplates(WhiteLabelingType.MAIL_TEMPLATES, tenantId));
}
@Test
public void testSystemNonMailTemplatesAreStillSyncedToEdge() {
// Other system white-labeling (e.g. GENERAL) is not affected by the mail-templates skip.
Assertions.assertFalse(isSystemMailTemplates(WhiteLabelingType.GENERAL, TenantId.SYS_TENANT_ID));
}
private boolean isSystemMailTemplates(WhiteLabelingType type, TenantId tenantId) {
return Boolean.TRUE.equals(ReflectionTestUtils.invokeMethod(processor, "isSystemMailTemplates", type, tenantId));
}
}
Loading…
Cancel
Save