From 663615d191ffd08fe5c0c87b24b44c729fe0a4d7 Mon Sep 17 00:00:00 2001 From: Nikita Mazurenko Date: Sat, 24 Oct 2026 14:31:13 +0200 Subject: [PATCH] Adapt edge mail/SMS/notification uplink handling to the 4.4 codebase --- .../wl/WhiteLabelingEdgeProcessor.java | 12 ++++++ .../server/service/mail/EdgeMailRequest.java | 2 +- .../notification/EdgeNotificationRequest.java | 2 +- .../server/service/sms/EdgeSmsRequest.java | 2 +- .../server/edge/SendEmailEdgeTest.java | 21 +++++----- .../server/edge/SendNotificationEdgeTest.java | 30 ++++++-------- .../server/edge/SendSmsEdgeTest.java | 7 ++-- .../AdminSettingsEdgeProcessorTest.java | 2 +- .../wl/WhiteLabelingEdgeProcessorTest.java | 40 +++++++++++++++++++ 9 files changed, 83 insertions(+), 35 deletions(-) create mode 100644 application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/wl/WhiteLabelingEdgeProcessorTest.java diff --git a/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/wl/WhiteLabelingEdgeProcessor.java b/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/wl/WhiteLabelingEdgeProcessor.java index ebed77616f..26841f770e 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/rpc/processor/wl/WhiteLabelingEdgeProcessor.java +++ b/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; diff --git a/application/src/main/java/org/thingsboard/server/service/mail/EdgeMailRequest.java b/application/src/main/java/org/thingsboard/server/service/mail/EdgeMailRequest.java index 2f2c020ab4..0115ec7069 100644 --- a/application/src/main/java/org/thingsboard/server/service/mail/EdgeMailRequest.java +++ b/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; diff --git a/application/src/main/java/org/thingsboard/server/service/notification/EdgeNotificationRequest.java b/application/src/main/java/org/thingsboard/server/service/notification/EdgeNotificationRequest.java index 50a06aad49..03ded08ed9 100644 --- a/application/src/main/java/org/thingsboard/server/service/notification/EdgeNotificationRequest.java +++ b/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; diff --git a/application/src/main/java/org/thingsboard/server/service/sms/EdgeSmsRequest.java b/application/src/main/java/org/thingsboard/server/service/sms/EdgeSmsRequest.java index c3696b21be..ac6ca8c7ff 100644 --- a/application/src/main/java/org/thingsboard/server/service/sms/EdgeSmsRequest.java +++ b/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; diff --git a/application/src/test/java/org/thingsboard/server/edge/SendEmailEdgeTest.java b/application/src/test/java/org/thingsboard/server/edge/SendEmailEdgeTest.java index d8c35b1676..287d12ddbb 100644 --- a/application/src/test/java/org/thingsboard/server/edge/SendEmailEdgeTest.java +++ b/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 { diff --git a/application/src/test/java/org/thingsboard/server/edge/SendNotificationEdgeTest.java b/application/src/test/java/org/thingsboard/server/edge/SendNotificationEdgeTest.java index 3d4fffd693..0c5731ccf5 100644 --- a/application/src/test/java/org/thingsboard/server/edge/SendNotificationEdgeTest.java +++ b/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 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 systemConfigs = new HashMap<>(); - systemConfigs.put(NotificationDeliveryMethod.MOBILE_APP, mobile); - systemSettings.setDeliveryMethodsConfigs(systemConfigs); - notificationSettingsService.saveNotificationSettings(TenantId.SYS_TENANT_ID, systemSettings); + + NotificationSettings settings = new NotificationSettings(); + Map 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)); } diff --git a/application/src/test/java/org/thingsboard/server/edge/SendSmsEdgeTest.java b/application/src/test/java/org/thingsboard/server/edge/SendSmsEdgeTest.java index 408bbfc43d..e61838c47f 100644 --- a/application/src/test/java/org/thingsboard/server/edge/SendSmsEdgeTest.java +++ b/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 { diff --git a/application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/settings/AdminSettingsEdgeProcessorTest.java b/application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/settings/AdminSettingsEdgeProcessorTest.java index 70598f6c67..3c66d7bf06 100644 --- a/application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/settings/AdminSettingsEdgeProcessorTest.java +++ b/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; diff --git a/application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/wl/WhiteLabelingEdgeProcessorTest.java b/application/src/test/java/org/thingsboard/server/service/edge/rpc/processor/wl/WhiteLabelingEdgeProcessorTest.java new file mode 100644 index 0000000000..eb1933654f --- /dev/null +++ b/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)); + } + +}