From 5539d47001261df38b49b38cfdb74cc4c776fcdb Mon Sep 17 00:00:00 2001 From: Viacheslav Klimov Date: Thu, 12 Nov 2026 02:41:40 +0200 Subject: [PATCH] Map tenant references of IoT Hub alarm rules to the installing tenant --- .../service/iot_hub/DefaultIotHubService.java | 2 + .../iot_hub/DefaultIotHubServiceTest.java | 105 +++++++++++++++++- 2 files changed, 102 insertions(+), 5 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java b/application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java index 9868f14bde..9552b5495f 100644 --- a/application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java +++ b/application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java @@ -341,6 +341,7 @@ public class DefaultIotHubService implements IotHubService { } alarmRule.setId(null); alarmRule.setTenantId(tenantId); + mapTenantReferences(alarmRule, tenantId); if (data != null && data.has("entityId")) { EntityId entityId = JacksonUtil.treeToValue(data.get("entityId"), EntityId.class); alarmRule.setEntityId(entityId); @@ -621,6 +622,7 @@ public class DefaultIotHubService implements IotHubService { existing.setName(newCf.getName()); existing.setType(newCf.getType()); existing.setConfiguration(newCf.getConfiguration()); + mapTenantReferences(existing, tenantId); tbCalculatedFieldService.save(existing, user); } diff --git a/application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultIotHubServiceTest.java b/application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultIotHubServiceTest.java index 8631244a8e..8349b5ef0e 100644 --- a/application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultIotHubServiceTest.java +++ b/application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultIotHubServiceTest.java @@ -1,5 +1,6 @@ // SPDX-FileCopyrightText: Copyright The ThingsBoard Authors -// SPDX-License-Identifier: Apache-2.0 +// SPDX-FileCopyrightText: Modifications Copyright ThingsBoard, Inc. +// SPDX-License-Identifier: Apache-2.0 AND BUSL-1.1 package org.thingsboard.server.service.iot_hub; import com.fasterxml.jackson.databind.JsonNode; @@ -16,15 +17,23 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.server.common.data.cf.CalculatedField; import org.thingsboard.server.common.data.cf.CalculatedFieldType; +import org.thingsboard.server.common.data.cf.configuration.AlarmCalculatedFieldConfiguration; import org.thingsboard.server.common.data.cf.configuration.Argument; import org.thingsboard.server.common.data.cf.configuration.ArgumentType; import org.thingsboard.server.common.data.cf.configuration.ArgumentsBasedCalculatedFieldConfiguration; +import org.thingsboard.server.common.data.cf.configuration.CalculatedFieldConfiguration; import org.thingsboard.server.common.data.cf.configuration.ReferencedEntityKey; import org.thingsboard.server.common.data.cf.configuration.SimpleCalculatedFieldConfiguration; +import org.thingsboard.server.common.data.cf.configuration.TimeSeriesOutput; +import org.thingsboard.server.common.data.cf.configuration.geofencing.EntityCoordinates; +import org.thingsboard.server.common.data.cf.configuration.geofencing.GeofencingCalculatedFieldConfiguration; +import org.thingsboard.server.common.data.cf.configuration.geofencing.GeofencingReportStrategy; +import org.thingsboard.server.common.data.cf.configuration.geofencing.ZoneGroupConfiguration; import org.thingsboard.server.common.data.id.CalculatedFieldId; import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.IotHubInstalledItemId; import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.iot_hub.AlarmRuleInstalledItemDescriptor; import org.thingsboard.server.common.data.iot_hub.CalculatedFieldInstalledItemDescriptor; import org.thingsboard.server.common.data.iot_hub.DashboardInstalledItemDescriptor; import org.thingsboard.server.common.data.iot_hub.IotHubInstalledItem; @@ -475,16 +484,102 @@ class DefaultIotHubServiceTest { assertThat(((ArgumentsBasedCalculatedFieldConfiguration) captor.getValue().getConfiguration()).getArguments().get("T").getRefEntityId()).isEqualTo(tenantId); } + @Test + void doInstallVersion_alarmRule_mapsTenantReferencesToInstallingTenant() throws Exception { + mockTenant(); + String versionId = UUID.randomUUID().toString(); + when(iotHubRestClient.getVersionInfo(versionId)) + .thenReturn(version(versionId, UUID.randomUUID().toString(), "ALARM_RULE", "Alarm", "1.0")); + when(iotHubRestClient.getVersionFileData(versionId)).thenReturn(alarmRuleFileData()); + when(tbCalculatedFieldService.save(any(), eq(user))).thenAnswer(invocation -> invocation.getArgument(0)); + when(iotHubInstalledItemService.save(eq(tenantId), any())).thenAnswer(invocation -> invocation.getArgument(1)); + + service.doInstallVersion(user, versionId, null, null); + + ArgumentCaptor captor = ArgumentCaptor.forClass(CalculatedField.class); + verify(tbCalculatedFieldService).save(captor.capture(), eq(user)); + assertThat(((ArgumentsBasedCalculatedFieldConfiguration) captor.getValue().getConfiguration()).getArguments().get("T").getRefEntityId()).isEqualTo(tenantId); + } + + @Test + void updateItemVersion_alarmRule_mapsTenantReferencesToInstallingTenant() throws Exception { + mockTenant(); + String versionId = UUID.randomUUID().toString(); + CalculatedFieldId calculatedFieldId = new CalculatedFieldId(UUID.randomUUID()); + AlarmRuleInstalledItemDescriptor descriptor = new AlarmRuleInstalledItemDescriptor(); + descriptor.setCalculatedFieldId(calculatedFieldId); + IotHubInstalledItem item = installedItem(UUID.randomUUID()); + item.setItemType("ALARM_RULE"); + item.setItemVersionId(UUID.randomUUID()); + item.setDescriptor(descriptor); + when(iotHubInstalledItemService.findById(tenantId, item.getId())).thenReturn(item); + when(iotHubRestClient.getVersionInfo(anyString())) + .thenReturn(version(versionId, UUID.randomUUID().toString(), "ALARM_RULE", "Alarm", "2.0")); + when(iotHubRestClient.getVersionFileData(versionId)).thenReturn(alarmRuleFileData()); + CalculatedField existing = new CalculatedField(calculatedFieldId); + existing.setTenantId(tenantId); + when(calculatedFieldService.findById(tenantId, calculatedFieldId)).thenReturn(existing); + + UpdateItemVersionResult result = service.updateItemVersion(user, item.getId(), versionId, true, null); + + assertThat(result.isSuccess()).isTrue(); + ArgumentCaptor captor = ArgumentCaptor.forClass(CalculatedField.class); + verify(tbCalculatedFieldService).save(captor.capture(), eq(user)); + assertThat(((ArgumentsBasedCalculatedFieldConfiguration) captor.getValue().getConfiguration()).getArguments().get("T").getRefEntityId()).isEqualTo(tenantId); + } + + @Test + void doInstallVersion_geofencingCalculatedField_mapsZoneGroupTenantReferencesToInstallingTenant() throws Exception { + mockTenant(); + String versionId = UUID.randomUUID().toString(); + when(iotHubRestClient.getVersionInfo(versionId)) + .thenReturn(version(versionId, UUID.randomUUID().toString(), "CALCULATED_FIELD", "Geofencing", "1.0")); + when(iotHubRestClient.getVersionFileData(versionId)).thenReturn(geofencingFileData()); + when(tbCalculatedFieldService.save(any(), eq(user))).thenAnswer(invocation -> invocation.getArgument(0)); + when(iotHubInstalledItemService.save(eq(tenantId), any())).thenAnswer(invocation -> invocation.getArgument(1)); + + service.doInstallVersion(user, versionId, null, null); + + ArgumentCaptor captor = ArgumentCaptor.forClass(CalculatedField.class); + verify(tbCalculatedFieldService).save(captor.capture(), eq(user)); + assertThat(((GeofencingCalculatedFieldConfiguration) captor.getValue().getConfiguration()).getZoneGroups().get("zones").getRefEntityId()).isEqualTo(tenantId); + } + private byte[] calculatedFieldFileData() { + SimpleCalculatedFieldConfiguration configuration = new SimpleCalculatedFieldConfiguration(); + configuration.setArguments(Map.of("T", otherTenantArgument())); + configuration.setExpression("T"); + return fileData(CalculatedFieldType.SIMPLE, configuration); + } + + private byte[] alarmRuleFileData() { + AlarmCalculatedFieldConfiguration configuration = new AlarmCalculatedFieldConfiguration(); + configuration.setArguments(Map.of("T", otherTenantArgument())); + configuration.setCreateRules(Map.of()); + return fileData(CalculatedFieldType.ALARM, configuration); + } + + private byte[] geofencingFileData() { + ZoneGroupConfiguration zoneGroup = new ZoneGroupConfiguration("perimeter", GeofencingReportStrategy.REPORT_PRESENCE_STATUS_ONLY, false); + zoneGroup.setRefEntityId(TenantId.fromUUID(UUID.randomUUID())); + GeofencingCalculatedFieldConfiguration configuration = new GeofencingCalculatedFieldConfiguration(); + configuration.setEntityCoordinates(new EntityCoordinates("latitude", "longitude")); + configuration.setZoneGroups(Map.of("zones", zoneGroup)); + configuration.setOutput(new TimeSeriesOutput()); + return fileData(CalculatedFieldType.GEOFENCING, configuration); + } + + private Argument otherTenantArgument() { Argument argument = new Argument(); argument.setRefEntityId(TenantId.fromUUID(UUID.randomUUID())); argument.setRefEntityKey(new ReferencedEntityKey("temperature", ArgumentType.TS_LATEST, null)); - SimpleCalculatedFieldConfiguration configuration = new SimpleCalculatedFieldConfiguration(); - configuration.setArguments(Map.of("T", argument)); - configuration.setExpression("T"); + return argument; + } + + private byte[] fileData(CalculatedFieldType type, CalculatedFieldConfiguration configuration) { CalculatedField calculatedField = new CalculatedField(); calculatedField.setEntityId(new DeviceId(UUID.randomUUID())); - calculatedField.setType(CalculatedFieldType.SIMPLE); + calculatedField.setType(type); calculatedField.setName("CF"); calculatedField.setConfiguration(configuration); return JacksonUtil.toString(calculatedField).getBytes(StandardCharsets.UTF_8);