Browse Source

address the PE review notes on the install report

- drop the java.util.Collection import that stayed behind when firstOrNull
  was inlined
- cap each report section at ten names plus "and N more": a template can
  hold hundreds of devices, and the dialog renders these details without
  the height cap the plain-text branch has
- leave the details empty when the server reported no message of its own,
  since the description above them already names the item that failed
- cover the asset conflict, the capped report and the remaining mocks in
  the validation test
pull/16109/head
dpinkevych 3 months ago
parent
commit
f97bc57694
Failed to extract signature
  1. 14
      application/src/main/java/org/thingsboard/server/service/solutions/DefaultSolutionService.java
  2. 31
      application/src/test/java/org/thingsboard/server/service/solutions/DefaultSolutionServiceTest.java
  3. 14
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.html
  4. 18
      ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.ts

14
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<HasName> entities) {
private void appendConflicts(StringBuilder details, EntityType entityType, List<String> 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) {

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

14
ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.html

@ -141,12 +141,14 @@
@case ('error') {
<h2 class="tb-iot-hub-install-title">{{ 'iot-hub.install-error-title' | translate }}</h2>
<p class="tb-iot-hub-install-desc">{{ 'iot-hub.install-error-message' | translate:{ name: item.name } }}</p>
@if (item.type === ItemType.SOLUTION_TEMPLATE) {
<tb-iot-hub-markdown class="tb-iot-hub-error-details-markdown" [data]="errorMessage"></tb-iot-hub-markdown>
} @else {
<div class="tb-iot-hub-error-details">
{{ errorMessage }}
</div>
@if (errorMessage) {
@if (item.type === ItemType.SOLUTION_TEMPLATE) {
<tb-iot-hub-markdown class="tb-iot-hub-error-details-markdown" [data]="errorMessage"></tb-iot-hub-markdown>
} @else {
<div class="tb-iot-hub-error-details">
{{ errorMessage }}
</div>
}
}
}
}

18
ui-ngx/src/app/modules/home/components/iot-hub/iot-hub-install-dialog.component.ts

@ -308,7 +308,7 @@ export class TbIotHubInstallDialogComponent extends DialogComponent<TbIotHubInst
next: (result) => {
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<TbIotHubInst
private handleApiError(err: any): void {
this.state = 'error';
this.errorMessage = err?.error?.message || err?.message ||
this.translate.instant('iot-hub.install-error', { name: this.item.name });
this.errorMessage = err?.error?.message || err?.message || '';
}
private handlePlanResult(result: InstallPlanResult): void {
if (!result.success) {
this.state = 'error';
const serverError = result.errorMessage;
let message = serverError || this.translate.instant('iot-hub.install-error', { name: this.item.name });
// The title already names the item being installed, so only a failing dependency has to be named here, and
// only when the server reported the failure - the fallback above names the root. A missing entry carries an
// error message of its own, so the failing one is the entry the cascade actually tried to install.
// The description above the details already names the item, so nothing is rendered here when the server
// reported no message of its own.
let message = serverError ?? '';
// The description above already names the item being installed, so only a failing dependency has to be
// named here, and only when the server reported the failure. A missing entry carries an error message of
// its own, so the failing one is the entry the cascade actually tried to install.
const failedDependency = serverError ? (result.entries ?? []).find(
entry => !entry.root && entry.errorMessage && entry.status === InstallPlanEntryStatus.WILL_INSTALL) : undefined;
if (failedDependency) {
@ -354,7 +355,8 @@ export class TbIotHubInstallDialogComponent extends DialogComponent<TbIotHubInst
// behind — tell the admin so they know manual cleanup may be needed. The willInstall guard
// avoids the misleading warning on the "empty plan" failure, where nothing was installed.
if (!result.rolledBack && this.planSummary.willInstall > 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;

Loading…
Cancel
Save