diff --git a/application/src/main/java/org/thingsboard/server/service/solutions/DefaultSolutionService.java b/application/src/main/java/org/thingsboard/server/service/solutions/DefaultSolutionService.java index cdeb9c06af..352d4d4e17 100644 --- a/application/src/main/java/org/thingsboard/server/service/solutions/DefaultSolutionService.java +++ b/application/src/main/java/org/thingsboard/server/service/solutions/DefaultSolutionService.java @@ -150,7 +150,6 @@ import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; import java.time.ZonedDateTime; import java.util.ArrayList; -import java.util.Collection; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; @@ -183,6 +182,7 @@ public class DefaultSolutionService implements SolutionService { private static final String CONFLICTS_INTRO = "Some entities of the solution template already exist. Rename or delete them and install the template again:"; private static final String RANDOM_PLACEHOLDER = "$random"; + private static final int MAX_REPORTED_CONFLICTS = 10; @Value("${ui.solution_templates.docs_base_url:https://thingsboard.io/docs}") private String docsBaseUrl; @@ -338,7 +338,8 @@ public class DefaultSolutionService implements SolutionService { return null; } StringBuilder details = new StringBuilder(CONFLICTS_INTRO).append(BLANK_LINE); - conflicts.forEach((entityType, entities) -> appendConflicts(details, entityType, entities)); + conflicts.forEach((entityType, entities) -> appendConflicts(details, entityType, + entities.stream().map(entity -> "'" + entity.getName() + "'").toList())); SolutionInstallResponse solutionInstallResponse = new SolutionInstallResponse(); solutionInstallResponse.setSuccess(false); @@ -427,10 +428,13 @@ public class DefaultSolutionService implements SolutionService { * One markdown list item per entity type, so that the names of the conflicting entities are what the user reads, * instead of the same sentence repeated for every type. */ - private void appendConflicts(StringBuilder details, EntityType entityType, List entities) { + private void appendConflicts(StringBuilder details, EntityType entityType, List conflictDescriptions) { details.append("- **").append(entityType.getNormalName()).append("**: ") - .append(entities.stream().map(entity -> "'" + entity.getName() + "'").collect(Collectors.joining(", "))) - .append(System.lineSeparator()); + .append(conflictDescriptions.stream().limit(MAX_REPORTED_CONFLICTS).collect(Collectors.joining(", "))); + if (conflictDescriptions.size() > MAX_REPORTED_CONFLICTS) { + details.append(" and ").append(conflictDescriptions.size() - MAX_REPORTED_CONFLICTS).append(" more"); + } + details.append(System.lineSeparator()); } private SolutionInstallResponse doInstallSolution(User user, TenantId tenantId, String solutionId, Path tempDir, HttpServletRequest request) { diff --git a/application/src/test/java/org/thingsboard/server/service/solutions/DefaultSolutionServiceTest.java b/application/src/test/java/org/thingsboard/server/service/solutions/DefaultSolutionServiceTest.java index d89012057b..d28955e742 100644 --- a/application/src/test/java/org/thingsboard/server/service/solutions/DefaultSolutionServiceTest.java +++ b/application/src/test/java/org/thingsboard/server/service/solutions/DefaultSolutionServiceTest.java @@ -25,6 +25,7 @@ import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.DashboardInfo; import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.DeviceProfile; +import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.asset.AssetProfile; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.id.TenantId; @@ -46,6 +47,8 @@ import java.nio.file.Path; import java.util.List; import java.util.Optional; import java.util.UUID; +import java.util.stream.Collectors; +import java.util.stream.IntStream; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.times; @@ -88,7 +91,7 @@ class DefaultSolutionServiceTest { void testValidateSolutionNamesEveryConflictingEntity() throws IOException { writeEntitiesFile("customers.json", "[{\"name\": \"Existing customer\"}, {\"name\": \"Customer $random\"}]"); writeEntitiesFile("devices.json", "[{\"name\": \"Existing device\"}, {\"name\": \"New device\"}]"); - writeEntitiesFile("assets.json", "[{\"name\": \"New asset\"}]"); + writeEntitiesFile("assets.json", "[{\"name\": \"Existing asset\"}, {\"name\": \"New asset\"}]"); Customer customer = new Customer(); customer.setTitle("Existing customer"); @@ -96,6 +99,9 @@ class DefaultSolutionServiceTest { device.setName("Existing device"); when(customerService.findCustomerByTenantIdAndTitle(tenantId, "Existing customer")).thenReturn(Optional.of(customer)); + Asset asset = new Asset(); + asset.setName("Existing asset"); + when(assetService.findAssetByTenantIdAndName(tenantId, "Existing asset")).thenReturn(asset); when(assetService.findAssetByTenantIdAndName(tenantId, "New asset")).thenReturn(null); when(deviceService.findDeviceByTenantIdAndName(tenantId, "Existing device")).thenReturn(device); when(deviceService.findDeviceByTenantIdAndName(tenantId, "New device")).thenReturn(null); @@ -108,6 +114,7 @@ class DefaultSolutionServiceTest { .containsSubsequence( CONFLICTS_INTRO, "- **Customer**: 'Existing customer'", + "- **Asset**: 'Existing asset'", "- **Device**: 'Existing device'"); // the randomized customer title and the entities that do not exist yet are not reported assertThat(result.getDetails()) @@ -220,6 +227,25 @@ class DefaultSolutionServiceTest { verifyNoInteractions(deviceService); } + @Test + void testValidateSolutionCapsTheNamesItLists() throws IOException { + String definitions = IntStream.rangeClosed(1, 12) + .mapToObj(i -> "{\"name\": \"Sensor " + i + "\"}") + .collect(Collectors.joining(",")); + writeEntitiesFile("devices.json", "[" + definitions + "]"); + for (int i = 1; i <= 12; i++) { + Device device = new Device(); + device.setName("Sensor " + i); + when(deviceService.findDeviceByTenantIdAndName(tenantId, "Sensor " + i)).thenReturn(device); + } + + SolutionInstallResponse result = service.validateSolution(tenantId, tempDir); + + assertThat(result.getDetails().lines().toList()) + .contains("- **Device**: 'Sensor 1', 'Sensor 2', 'Sensor 3', 'Sensor 4', 'Sensor 5', 'Sensor 6', " + + "'Sensor 7', 'Sensor 8', 'Sensor 9', 'Sensor 10' and 2 more"); + } + @Test void testValidateSolutionWithoutConflicts() throws IOException { writeEntitiesFile("devices.json", "[{\"name\": \"New device\"}]"); @@ -231,7 +257,8 @@ class DefaultSolutionServiceTest { @Test void testValidateSolutionWithoutEntityFiles() { assertThat(service.validateSolution(tenantId, tempDir)).isNull(); - verifyNoInteractions(customerService, deviceService, assetService, dashboardService, ruleChainService); + verifyNoInteractions(customerService, deviceService, assetService, dashboardService, ruleChainService, + deviceProfileService, assetProfileService, edgeService); } private void writeFile(String relativePath, String content) throws IOException { diff --git a/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.html b/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.html index 25c5ca2089..d267ccf57e 100644 --- a/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.html +++ b/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.html @@ -141,12 +141,14 @@ @case ('error') {

{{ 'iot-hub.install-error-title' | translate }}

{{ 'iot-hub.install-error-message' | translate:{ name: item.name } }}

- @if (item.type === ItemType.SOLUTION_TEMPLATE) { - - } @else { -
- {{ errorMessage }} -
+ @if (errorMessage) { + @if (item.type === ItemType.SOLUTION_TEMPLATE) { + + } @else { +
+ {{ errorMessage }} +
+ } } } } diff --git a/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.ts b/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.ts index f777a4da31..5fe6879e5e 100644 --- a/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.ts @@ -308,7 +308,7 @@ export class TbIotHubInstallDialogComponent extends DialogComponent { if (!result.success) { this.state = 'error'; - this.errorMessage = result.errorMessage || this.translate.instant('iot-hub.install-error', { name: this.item.name }); + this.errorMessage = result.errorMessage ?? ''; return; } this.handleInstalledDescriptor(result.descriptor); @@ -331,18 +331,19 @@ export class TbIotHubInstallDialogComponent extends DialogComponent !entry.root && entry.errorMessage && entry.status === InstallPlanEntryStatus.WILL_INSTALL) : undefined; if (failedDependency) { @@ -354,7 +355,8 @@ export class TbIotHubInstallDialogComponent extends DialogComponent 0) { - message += '\n\n' + this.translate.instant('iot-hub.install-rollback-partial'); + const warning = this.translate.instant('iot-hub.install-rollback-partial'); + message = message ? message + '\n\n' + warning : warning; } this.errorMessage = message; return;