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 c071bc61b6..ddbe8ec5d7 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 @@ -84,6 +84,7 @@ import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.util.ArrayList; import java.util.Collections; +import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.HexFormat; @@ -713,6 +714,9 @@ public class DefaultIotHubService implements IotHubService { } } + private static final Map DELETE_ORDER = Map.of(EntityType.DEVICE, 0, EntityType.DEVICE_PROFILE, 1, + EntityType.DASHBOARD, 2, EntityType.RULE_CHAIN, 3, EntityType.INTEGRATION, 4, EntityType.CONVERTER, 5); + /** * Deletes what a device package created, except the entities another installed device package * also uses (e.g. an integration it took over with Overwrite): deleting those would break that package, @@ -728,9 +732,12 @@ public class DefaultIotHubService implements IotHubService { Set usedExisting = usedExistingIds(descriptor); List otherPackages = new ArrayList<>(); Map> usedByOtherPackages = findEntitiesOfOtherDevicePackages(tenantId, installedItemId, otherPackages); - List reversed = new ArrayList<>(createdEntityIds); - Collections.reverse(reversed); - for (EntityId entityId : reversed) { + // An entity goes before what it refers to: a device before its profile, a profile before its default dashboard + // and rule chain, an integration before its converters. Within a type, the reverse of the creation order. + List ordered = new ArrayList<>(createdEntityIds); + Collections.reverse(ordered); + ordered.sort(Comparator.comparingInt(id -> DELETE_ORDER.getOrDefault(id.getEntityType(), DELETE_ORDER.size()))); + for (EntityId entityId : ordered) { if (usedExisting.contains(entityId.getId())) { log.info("[{}] Kept {} {}: the package took it as it was", tenantId, entityId.getEntityType(), entityId.getId()); removeIntegrationMetadata(tenantId, entityId, descriptor, otherPackages); 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 a20a609a15..d63be65c5c 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 @@ -27,14 +27,22 @@ import org.thingsboard.server.common.data.integration.Integration; import org.thingsboard.server.common.data.iot_hub.DeviceInstalledItemDescriptor; import org.thingsboard.server.common.data.iot_hub.IotHubInstalledItem; import org.thingsboard.server.common.data.iot_hub.RuleChainInstalledItemDescriptor; +import org.thingsboard.server.common.data.Dashboard; +import org.thingsboard.server.common.data.DeviceProfile; +import org.thingsboard.server.common.data.converter.Converter; +import org.thingsboard.server.common.data.id.ConverterId; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.dao.dashboard.DashboardService; +import org.thingsboard.server.dao.converter.ConverterService; +import org.thingsboard.server.dao.device.DeviceProfileService; import org.thingsboard.server.dao.device.DeviceService; import org.thingsboard.server.dao.entity.EntityService; import org.thingsboard.server.dao.integration.IntegrationService; import org.thingsboard.server.dao.iot_hub.IotHubInstalledItemService; import org.thingsboard.server.service.entitiy.dashboard.TbDashboardService; +import org.thingsboard.server.service.entitiy.converter.TbConverterService; import org.thingsboard.server.service.entitiy.device.TbDeviceService; +import org.thingsboard.server.service.entitiy.device.profile.TbDeviceProfileService; import org.thingsboard.server.service.security.model.SecurityUser; import java.util.List; @@ -79,6 +87,14 @@ class DefaultIotHubServiceTest { private TbDashboardService tbDashboardService; @Mock private IntegrationService integrationService; + @Mock + private DeviceProfileService deviceProfileService; + @Mock + private TbDeviceProfileService tbDeviceProfileService; + @Mock + private ConverterService converterService; + @Mock + private TbConverterService tbConverterService; @Spy @InjectMocks @@ -443,6 +459,34 @@ class DefaultIotHubServiceTest { assertThat(result.getErrorMessage()).contains("install failed"); } + @Test + void deleteInstalledItem_devicePackage_removesEachEntityBeforeWhatItRefersTo() throws Exception { + mockTenant(); + DeviceProfile profile = new DeviceProfile(new DeviceProfileId(UUID.randomUUID())); + Dashboard dashboard = new Dashboard(new DashboardId(UUID.randomUUID())); + profile.setDefaultDashboardId(dashboard.getId()); + Integration integration = new Integration(new IntegrationId(UUID.randomUUID())); + Converter converter = new Converter(new ConverterId(UUID.randomUUID())); + // The profile and the integration listed first: in reverse order of creation, the dashboard would go before the + // profile that refers to it, and the converter before the integration that uses it. + IotHubInstalledItem deleted = devicePackage(profile.getId(), dashboard.getId(), integration.getId(), converter.getId()); + when(iotHubInstalledItemService.findById(tenantId, deleted.getId())).thenReturn(deleted); + when(iotHubInstalledItemService.findByTenantId(eq(tenantId), eq(List.of("DEVICE")), isNull(), any())) + .thenReturn(new PageData<>(List.of(deleted), 1, 1, false)); + when(deviceProfileService.findDeviceProfileById(tenantId, profile.getId())).thenReturn(profile); + when(dashboardService.findDashboardById(tenantId, dashboard.getId())).thenReturn(dashboard); + when(integrationService.findIntegrationById(tenantId, integration.getId())).thenReturn(integration); + when(converterService.findConverterById(tenantId, converter.getId())).thenReturn(converter); + + service.deleteInstalledItem(user, deleted.getId()); + + InOrder order = inOrder(tbDeviceProfileService, tbDashboardService, integrationService, tbConverterService); + order.verify(tbDeviceProfileService).delete(profile, user); + order.verify(tbDashboardService).delete(dashboard, user); + order.verify(integrationService).deleteIntegration(tenantId, integration.getId()); + order.verify(tbConverterService).delete(converter, user); + } + @Test void deleteInstalledItem_devicePackage_keepsEntitiesAnotherDevicePackageUses() { mockTenant();