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 b279d693ef9..ad8c6a224bc 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 @@ -111,6 +111,7 @@ import org.thingsboard.server.service.solutions.data.DashboardLinkInfo; import org.thingsboard.server.service.solutions.data.DeviceCredentialsInfo; import org.thingsboard.server.service.solutions.data.EdgeLinkInfo; import org.thingsboard.server.service.solutions.data.SolutionInstallContext; +import org.thingsboard.server.service.solutions.data.SolutionValidationResult; import org.thingsboard.server.service.solutions.data.UserCredentialsInfo; import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.thingsboard.server.service.solutions.data.definition.AssetDefinition; @@ -257,9 +258,9 @@ public class DefaultSolutionService implements SolutionService { throw new IllegalArgumentException("Solution template is missing solution.json or its 'title' field"); } - SolutionInstallResponse validateResult = validateSolution(tenantId, tempDir); - if (validateResult != null && !validateResult.isSuccess()) { - return validateResult; + SolutionValidationResult validation = validateSolution(tenantId, tempDir); + if (!validation.isPassed()) { + return conflictResponse(validation.getConflictReport()); } return doInstallSolution(user, tenantId, solutionId, tempDir, request); } finally { @@ -300,7 +301,7 @@ public class DefaultSolutionService implements SolutionService { } } - SolutionInstallResponse validateSolution(TenantId tenantId, Path tempDir) { + SolutionValidationResult validateSolution(TenantId tenantId, Path tempDir) { //TODO: pre-validate what still only fails at provision time: customer users (unique by email), // alarm rules and calculated fields. @@ -338,7 +339,7 @@ public class DefaultSolutionService implements SolutionService { collectConflicts(conflicts, edges, name -> edgeService.findEdgeByTenantIdAndName(tenantId, name)); if (conflicts.isEmpty()) { - return null; + return SolutionValidationResult.passed(); } StringBuilder details = new StringBuilder(CONFLICTS_INTRO).append(BLANK_LINE); conflicts.forEach((entityType, conflictDescriptions) -> appendConflicts(details, entityType, conflictDescriptions)); @@ -346,10 +347,14 @@ public class DefaultSolutionService implements SolutionService { details.append(System.lineSeparator()).append(TRUNCATION_NOTE); } - SolutionInstallResponse solutionInstallResponse = new SolutionInstallResponse(); - solutionInstallResponse.setSuccess(false); - solutionInstallResponse.setDetails(details.toString()); - return solutionInstallResponse; + return SolutionValidationResult.conflictsFound(details.toString()); + } + + private static SolutionInstallResponse conflictResponse(String conflictReport) { + SolutionInstallResponse response = new SolutionInstallResponse(); + response.setSuccess(false); + response.setDetails(conflictReport); + return response; } /** diff --git a/application/src/main/java/org/thingsboard/server/service/solutions/data/SolutionValidationResult.java b/application/src/main/java/org/thingsboard/server/service/solutions/data/SolutionValidationResult.java new file mode 100644 index 00000000000..407290e487d --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/solutions/data/SolutionValidationResult.java @@ -0,0 +1,43 @@ +/** + * Copyright © 2016-2026 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.service.solutions.data; + +import lombok.AccessLevel; +import lombok.Getter; +import lombok.RequiredArgsConstructor; + +/** + * The outcome of validating a solution template before anything is created: either the install can go ahead, or the + * entities of the tenant the template clashes with, reported as the markdown the install dialog renders. + */ +@Getter +@RequiredArgsConstructor(access = AccessLevel.PRIVATE) +public class SolutionValidationResult { + + private static final SolutionValidationResult PASSED = new SolutionValidationResult(true, ""); + + private final boolean passed; + private final String conflictReport; + + public static SolutionValidationResult passed() { + return PASSED; + } + + public static SolutionValidationResult conflictsFound(String conflictReport) { + return new SolutionValidationResult(false, conflictReport); + } + +} 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 b1b5f1726a4..860a6d3a0cf 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 @@ -41,7 +41,7 @@ import org.thingsboard.server.dao.device.DeviceProfileService; import org.thingsboard.server.dao.device.DeviceService; import org.thingsboard.server.dao.edge.EdgeService; import org.thingsboard.server.dao.rule.RuleChainService; -import org.thingsboard.server.service.solutions.data.solution.SolutionInstallResponse; +import org.thingsboard.server.service.solutions.data.SolutionValidationResult; import java.io.IOException; import java.nio.file.Files; @@ -111,18 +111,17 @@ class DefaultSolutionServiceTest { .thenReturn(existingDevice("Existing device")); when(deviceService.findDeviceByTenantIdAndName(tenantId, "New device")).thenReturn(null); - SolutionInstallResponse result = service.validateSolution(tenantId, tempDir); + SolutionValidationResult result = service.validateSolution(tenantId, tempDir); - assertThat(result).isNotNull(); - assertThat(result.isSuccess()).isFalse(); - assertThat(result.getDetails().lines().toList()) + assertThat(result.isPassed()).isFalse(); + assertThat(result.getConflictReport().lines().toList()) .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()) + assertThat(result.getConflictReport()) .doesNotContain("Customer $random") .doesNotContain("New device") .doesNotContain("New asset"); @@ -136,10 +135,10 @@ class DefaultSolutionServiceTest { dashboard.setTitle("Overview"); when(dashboardService.findFirstDashboardInfoByTenantIdAndName(tenantId, "Overview")).thenReturn(dashboard); - SolutionInstallResponse result = service.validateSolution(tenantId, tempDir); + SolutionValidationResult result = service.validateSolution(tenantId, tempDir); - assertThat(result).isNotNull(); - assertThat(result.getDetails().lines().toList()) + assertThat(result.isPassed()).isFalse(); + assertThat(result.getConflictReport().lines().toList()) .containsSubsequence(CONFLICTS_INTRO, "- **Dashboard**: 'Overview'"); } @@ -153,10 +152,10 @@ class DefaultSolutionServiceTest { when(ruleChainService.findTenantRuleChainsByTypeAndName(tenantId, RuleChainType.CORE, "Name the install creates")) .thenReturn(List.of(ruleChain)); - SolutionInstallResponse result = service.validateSolution(tenantId, tempDir); + SolutionValidationResult result = service.validateSolution(tenantId, tempDir); - assertThat(result).isNotNull(); - assertThat(result.getDetails().lines().toList()) + assertThat(result.isPassed()).isFalse(); + assertThat(result.getConflictReport().lines().toList()) .containsSubsequence(CONFLICTS_INTRO, "- **Rule chain**: 'Name the install creates'"); } @@ -176,10 +175,10 @@ class DefaultSolutionServiceTest { when(assetProfileService.findAssetProfileByName(tenantId, "building")).thenReturn(assetProfile); when(edgeService.findEdgeByTenantIdAndName(tenantId, "Main edge")).thenReturn(edge); - SolutionInstallResponse result = service.validateSolution(tenantId, tempDir); + SolutionValidationResult result = service.validateSolution(tenantId, tempDir); - assertThat(result).isNotNull(); - assertThat(result.getDetails().lines().toList()).containsSubsequence( + assertThat(result.isPassed()).isFalse(); + assertThat(result.getConflictReport().lines().toList()).containsSubsequence( "- **Device profile**: 'thermostat'", "- **Asset profile**: 'building'", "- **Edge**: 'Main edge'"); @@ -191,9 +190,9 @@ class DefaultSolutionServiceTest { when(deviceService.findDeviceByTenantIdAndName(tenantId, "Sensor")).thenReturn(existingDevice("Sensor")); - SolutionInstallResponse result = service.validateSolution(tenantId, tempDir); + SolutionValidationResult result = service.validateSolution(tenantId, tempDir); - assertThat(result.getDetails().lines().toList()).contains("- **Device**: 'Sensor'"); + assertThat(result.getConflictReport().lines().toList()).contains("- **Device**: 'Sensor'"); verify(deviceService, times(1)).findDeviceByTenantIdAndName(tenantId, "Sensor"); } @@ -203,7 +202,7 @@ class DefaultSolutionServiceTest { "{\"name\": \"No name inside\", \"file\": \"nameless.json\"}]"); writeFile("rule_chains/nameless.json", "{\"ruleChain\": {}}"); - assertThat(service.validateSolution(tenantId, tempDir)).isNull(); + assertThat(service.validateSolution(tenantId, tempDir).isPassed()).isTrue(); verifyNoInteractions(ruleChainService); } @@ -217,16 +216,16 @@ class DefaultSolutionServiceTest { when(ruleChainService.findTenantRuleChainsByTypeAndName(tenantId, RuleChainType.EDGE, "Edge chain")) .thenReturn(List.of(ruleChain)); - SolutionInstallResponse result = service.validateSolution(tenantId, tempDir); + SolutionValidationResult result = service.validateSolution(tenantId, tempDir); - assertThat(result.getDetails().lines().toList()).contains("- **Rule chain**: 'Edge chain'"); + assertThat(result.getConflictReport().lines().toList()).contains("- **Rule chain**: 'Edge chain'"); } @Test void testValidateSolutionTreatsAJsonNullFileAsEmpty() throws IOException { writeEntitiesFile("devices.json", "null"); - assertThat(service.validateSolution(tenantId, tempDir)).isNull(); + assertThat(service.validateSolution(tenantId, tempDir).isPassed()).isTrue(); verifyNoInteractions(deviceService); } @@ -240,17 +239,17 @@ class DefaultSolutionServiceTest { names.forEach(name -> when(deviceService.findDeviceByTenantIdAndName(tenantId, name)) .thenReturn(existingDevice(name))); - SolutionInstallResponse result = service.validateSolution(tenantId, tempDir); + SolutionValidationResult result = service.validateSolution(tenantId, tempDir); int hidden = conflictCount - LISTED_NAMES_PER_TYPE; String listed = names.stream().limit(LISTED_NAMES_PER_TYPE) .map(name -> "'" + name + "'").collect(Collectors.joining(", ")); - assertThat(result.getDetails().lines().toList()).contains("- **Device**: " + listed + assertThat(result.getConflictReport().lines().toList()).contains("- **Device**: " + listed + (hidden > 0 ? " and " + hidden + " more (" + conflictCount + " in total)" : "")); if (hidden > 0) { - assertThat(result.getDetails()).contains(TRUNCATION_NOTE); + assertThat(result.getConflictReport()).contains(TRUNCATION_NOTE); } else { - assertThat(result.getDetails()).doesNotContain(TRUNCATION_NOTE); + assertThat(result.getConflictReport()).doesNotContain(TRUNCATION_NOTE); } } @@ -267,9 +266,9 @@ class DefaultSolutionServiceTest { profile.setName("Sensor profile"); when(deviceProfileService.findDeviceProfileByName(tenantId, "Sensor profile")).thenReturn(profile); - SolutionInstallResponse result = service.validateSolution(tenantId, tempDir); + SolutionValidationResult result = service.validateSolution(tenantId, tempDir); - assertThat(result.getDetails().lines().toList()).contains("- **Device profile**: 'Sensor profile'"); + assertThat(result.getConflictReport().lines().toList()).contains("- **Device profile**: 'Sensor profile'"); verify(deviceProfileService, times(1)).findDeviceProfileByName(tenantId, "Sensor profile"); } @@ -278,12 +277,12 @@ class DefaultSolutionServiceTest { writeEntitiesFile("devices.json", "[{\"name\": \"New device\"}]"); when(deviceService.findDeviceByTenantIdAndName(tenantId, "New device")).thenReturn(null); - assertThat(service.validateSolution(tenantId, tempDir)).isNull(); + assertThat(service.validateSolution(tenantId, tempDir).isPassed()).isTrue(); } @Test void testValidateSolutionWithoutEntityFiles() { - assertThat(service.validateSolution(tenantId, tempDir)).isNull(); + assertThat(service.validateSolution(tenantId, tempDir).isPassed()).isTrue(); verifyNoInteractions(customerService, deviceService, assetService, dashboardService, ruleChainService, deviceProfileService, assetProfileService, edgeService); }