Browse Source

Device package update keeps an entity used by a new step

An entity a new step of the package uses as it was goes into createdEntityIds as well as usedExistingEntityIds, as at install, so deleting the package removes the metadata entries it added to that entity.
pull/16617/head
dshvaika 3 weeks ago
parent
commit
682fe8cd37
  1. 7
      application/src/main/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateService.java
  2. 34
      application/src/test/java/org/thingsboard/server/service/iot_hub/DefaultDevicePackageUpdateServiceTest.java

7
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) {

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

Loading…
Cancel
Save