diff --git a/application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateService.java b/application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateService.java index e4eaa18074..1becc88cf4 100644 --- a/application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateService.java +++ b/application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateService.java @@ -329,9 +329,12 @@ public class DefaultDevicePackageUpdateService extends AbstractTbEntityService i state.set("metadata", metadata); } installState.put(step.getId() != null ? step.getId() : step.getName(), state); - if (!"use-existing".equals(resolution)) { + // As at install, an entity used as it was is listed too, so a delete removes the metadata entries the + // package added to it; usedExistingEntityIds keeps it the user's + if (!stepEntityIds.contains(result.entityId())) { stepEntityIds.add(result.entityId()); - } else { + } + if ("use-existing".equals(resolution)) { usedExistingIds.putIfAbsent(result.entityId().getId(), result.entityId()); } if (result.entityId() instanceof DashboardId id) { diff --git a/application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateServiceTest.java b/application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateServiceTest.java index 03e138b296..9c30892737 100644 --- a/application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateServiceTest.java +++ b/application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateServiceTest.java @@ -421,6 +421,40 @@ class DefaultDevicePackageUpdateServiceTest { .isEqualTo(JacksonUtil.newObjectNode().put("profile.milesight-iot-am308", "AM308")); } + @Test + void integrationUsedByAStepNewToThePackage_isListedAsUsedAsItWas() { + // The installed version has no integration step; the new one takes the tenant's integration as it is + DeviceInstalledItemDescriptor installed = (DeviceInstalledItemDescriptor) installedItem.getDescriptor(); + installed.setCreatedEntityIds(List.of(profileId, deviceId, dashboardId)); + installed.setInstallState(Map.of("Profile", stateOf(profileId, "created"), "AM308", stateOf(deviceId, "created"), + "Dashboard", stateOf(dashboardId, "created"))); + IntegrationId tenantsIntegrationId = new IntegrationId(UUID.randomUUID()); + Integration current = new Integration(); + current.setId(tenantsIntegrationId); + current.setTenantId(tenantId); + current.setName("TTN my-app"); + ObjectNode configuration = JacksonUtil.newObjectNode(); + configuration.set("metadata", JacksonUtil.newObjectNode().put("profile.other-model", "Other")); + current.setConfiguration(configuration); + when(integrationService.findIntegrationById(tenantId, tenantsIntegrationId)).thenReturn(current); + when(integrationService.saveIntegration(any())).thenAnswer(i -> i.getArgument(0)); + DeviceUpdateRequest.Step step = keep("TTN", "INTEGRATION", tenantsIntegrationId); + step.setResolution("use-existing"); + step.setMetadata(JacksonUtil.newObjectNode().put("profile.milesight-iot-am308", "AM308")); + + DeviceUpdateResult result = service.apply(user, installedItem.getId(), request(step)); + + assertThat(result.isSuccess()).isTrue(); + DeviceInstalledItemDescriptor descriptor = (DeviceInstalledItemDescriptor) installedItem.getDescriptor(); + // A delete walks createdEntityIds and keeps what usedExistingEntityIds lists, removing only the package's + // entries from it (DefaultIotHubServiceTest#deleteInstalledItem_devicePackage_removesItsMetadataEntriesFromTheIntegrationItTookAsItWas) + assertThat(descriptor.getCreatedEntityIds()).containsOnlyOnce(tenantsIntegrationId); + assertThat(descriptor.getUsedExistingEntityIds()).containsExactly(tenantsIntegrationId); + JsonNode state = descriptor.getInstallState().get("TTN"); + assertThat(state.get("resolution").asText()).isEqualTo("use-existing"); + assertThat(state.get("metadata")).isEqualTo(JacksonUtil.newObjectNode().put("profile.milesight-iot-am308", "AM308")); + } + @Test void keptIntegrationWithTheEntriesItHas_isNotSaved() { Integration current = new Integration();