Browse Source

Merge pull request #16630 from pinkevmladchy/fix/iot-hub-device-package-delete-order-4.4.1

IoT Hub device package delete: each entity before what it refers to
pull/16633/head
Viacheslav Klimov 3 weeks ago
committed by GitHub
parent
commit
33d3bbb1de
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 13
      application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultIotHubService.java
  2. 44
      application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultIotHubServiceTest.java

13
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<EntityType, Integer> 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<UUID> usedExisting = usedExistingIds(descriptor);
List<DeviceInstalledItemDescriptor> otherPackages = new ArrayList<>();
Map<UUID, Set<IotHubInstalledItemId>> usedByOtherPackages = findEntitiesOfOtherDevicePackages(tenantId, installedItemId, otherPackages);
List<EntityId> 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<EntityId> 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);

44
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();

Loading…
Cancel
Save