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 db5dc64379..5ab98c932e 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 @@ -204,7 +204,7 @@ public class DefaultDevicePackageUpdateService extends AbstractTbEntityService i if (request.getSteps() == null || request.getSteps().isEmpty()) { throw new IllegalArgumentException("The update has no steps"); } - Set ownedIds = createdIds(devicePackage.descriptor()).keySet(); + Set ownedIds = ownedIds(devicePackage.descriptor()); Set stateKeys = new HashSet<>(); for (DeviceUpdateRequest.Step step : request.getSteps()) { if (step.getName() == null || step.getAction() == null || !isKnownType(step.getType())) { @@ -273,7 +273,7 @@ public class DefaultDevicePackageUpdateService extends AbstractTbEntityService i private Applied applySteps(TenantId tenantId, DevicePackage devicePackage, DeviceUpdateRequest request, JsonNode versionInfo) { IotHubInstalledItem installedItem = lockInstalledItem(tenantId, devicePackage, request); DeviceInstalledItemDescriptor previous = devicePackage.descriptor(); - Set ownedIds = createdIds(previous).keySet(); + Set ownedIds = ownedIds(previous); Map aliasOutputs = new HashMap<>(); Map outputs = new LinkedHashMap<>(); Map installState = new LinkedHashMap<>(); @@ -840,6 +840,26 @@ public class DefaultDevicePackageUpdateService extends AbstractTbEntityService i return type != null && (STEP_ALIASES.containsKey(type) || "GATEWAY_CONNECTOR".equals(type)); } + /** + * The entities the package owns: those its install state records, except the ones the user chose to use as they + * were. Not createdEntityIds: the install adds the use-existing ones to it too. + */ + private static Set ownedIds(DeviceInstalledItemDescriptor descriptor) { + Set ids = new HashSet<>(); + if (descriptor.getInstallState() != null) { + descriptor.getInstallState().values().forEach(state -> { + JsonNode id = state.path("entityOutput").path("id"); + if (id.isTextual() && !"use-existing".equals(state.path("resolution").asText())) { + try { + ids.add(UUID.fromString(id.asText())); + } catch (IllegalArgumentException ignored) { + } + } + }); + } + return ids; + } + private static Map createdIds(DeviceInstalledItemDescriptor descriptor) { Map ids = new HashMap<>(); if (descriptor.getCreatedEntityIds() != null) { 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 7b02a966fa..06d0109b82 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 @@ -135,7 +135,9 @@ class DefaultDevicePackageUpdateServiceTest { descriptor.setSelectedInstallMethod("INTEGRATION_TTN"); descriptor.setCreatedEntityIds(List.of(profileId, deviceId, dashboardId, integrationId)); descriptor.setDashboardId(dashboardId); - descriptor.setInstallState(Map.of()); + // The package created these entities + descriptor.setInstallState(Map.of("Profile", stateOf(profileId, "created"), "AM308", stateOf(deviceId, "created"), + "Dashboard", stateOf(dashboardId, "created"), "Integration", stateOf(integrationId, "created"))); installedItem = new IotHubInstalledItem(); installedItem.setId(new IotHubInstalledItemId(UUID.randomUUID())); installedItem.setTenantId(tenantId); @@ -273,6 +275,32 @@ class DefaultDevicePackageUpdateServiceTest { assertThat(descriptor.getCreatedEntityIds()).contains(dashboardId); } + @Test + void useExistingAtInstall_staysUseExisting() { + // At install the user picked their own dashboard: the install adds it to createdEntityIds too + ((DeviceInstalledItemDescriptor) installedItem.getDescriptor()).setInstallState(Map.of("Dashboard", stateOf(dashboardId, "use-existing"))); + when(dashboardService.findDashboardById(tenantId, dashboardId)).thenReturn(dashboard(9L)); + DeviceUpdateRequest.Step step = keep("Dashboard", "DASHBOARD", dashboardId); + step.setResolution("use-existing"); + + DeviceUpdateResult result = service.apply(user, installedItem.getId(), request(step)); + + assertThat(result.isSuccess()).isTrue(); + DeviceInstalledItemDescriptor descriptor = (DeviceInstalledItemDescriptor) installedItem.getDescriptor(); + assertThat(descriptor.getInstallState().get("Dashboard").get("resolution").asText()).isEqualTo("use-existing"); + } + + @Test + void overwritingAnEntityUsedAtInstall_needsAConfirmation() { + ((DeviceInstalledItemDescriptor) installedItem.getDescriptor()).setInstallState(Map.of("Dashboard", stateOf(dashboardId, "use-existing"))); + DeviceUpdateRequest.Step step = overwrite("Dashboard", "DASHBOARD", dashboardId, 9L, JacksonUtil.newObjectNode().put("title", "Dashboard")); + + DeviceUpdateResult result = service.apply(user, installedItem.getId(), request(step)); + + assertThat(result.isSuccess()).isFalse(); + verify(installedItemService, never()).save(any(), any()); + } + @Test void twoStepsWithOneKey_areRejected() { DeviceUpdateRequest.Step first = keep("Overview", "DASHBOARD", dashboardId); @@ -593,4 +621,11 @@ class DefaultDevicePackageUpdateServiceTest { return step; } + + private static ObjectNode stateOf(EntityId id, String resolution) { + ObjectNode state = JacksonUtil.newObjectNode().put("resolution", resolution); + state.set("entityOutput", JacksonUtil.newObjectNode().put("id", id.getId().toString())); + return state; + } + } diff --git a/ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.ts b/ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.ts index 77fef10a5e..5085d7fc96 100644 --- a/ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/iot-hub/device-install-dialog/device-install-dialog.component.ts @@ -2429,9 +2429,13 @@ export class TbDeviceInstallDialogComponent extends DialogComponent field.key).filter(key => !previousKeys.has(key))); } - /** Update: true when the entity is one the installed package created. */ + /** + * Update: true when the entity is one the installed package owns: its install state records it, and not as one + * the user chose to use as it was. Not createdEntityIds: the install adds the use-existing ones to it too. + */ private isPackageEntity(id: string): boolean { - return !!this.data.update?.descriptor.createdEntityIds?.some(created => created.id === id); + return Object.values(this.data.update?.descriptor.installState ?? {}) + .some(state => state?.entityOutput?.id === id && state.resolution !== 'use-existing'); } /** The keys the install state may keep a step under: its id, or else its name, as written or resolved. */