From e879f63376786fb1affd55fbcab7e00288f898bf Mon Sep 17 00:00:00 2001 From: IrynaMatveieva Date: Fri, 22 Mar 2024 10:50:21 +0200 Subject: [PATCH] fixed error message for problem with external id --- .../server/dao/rule/BaseRuleChainService.java | 22 ++-- .../dao/rule/BaseRuleChainServiceTest.java | 110 ++++++++++++++++++ 2 files changed, 123 insertions(+), 9 deletions(-) create mode 100644 dao/src/test/java/org/thingsboard/server/dao/rule/BaseRuleChainServiceTest.java diff --git a/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java b/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java index 2857436fc8..a636ccf879 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java @@ -118,16 +118,20 @@ public class BaseRuleChainService extends AbstractEntityService implements RuleC @Transactional public RuleChain saveRuleChain(RuleChain ruleChain, boolean publishSaveEvent) { ruleChainValidator.validate(ruleChain, RuleChain::getTenantId); + RuleChain savedRuleChain = saveRuleChainInternal(ruleChain); + if (ruleChain.getId() == null) { + entityCountService.publishCountEntityEvictEvent(ruleChain.getTenantId(), EntityType.RULE_CHAIN); + } + if (publishSaveEvent) { + eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(savedRuleChain.getTenantId()) + .entity(savedRuleChain).entityId(savedRuleChain.getId()).created(ruleChain.getId() == null).build()); + } + return savedRuleChain; + } + + private RuleChain saveRuleChainInternal(RuleChain ruleChain) { try { - RuleChain savedRuleChain = ruleChainDao.save(ruleChain.getTenantId(), ruleChain); - if (ruleChain.getId() == null) { - entityCountService.publishCountEntityEvictEvent(ruleChain.getTenantId(), EntityType.RULE_CHAIN); - } - if (publishSaveEvent) { - eventPublisher.publishEvent(SaveEntityEvent.builder().tenantId(savedRuleChain.getTenantId()) - .entity(savedRuleChain).entityId(savedRuleChain.getId()).created(ruleChain.getId() == null).build()); - } - return savedRuleChain; + return ruleChainDao.saveAndFlush(ruleChain.getTenantId(), ruleChain); } catch (Exception e) { checkConstraintViolation(e, "rule_chain_external_id_unq_key", "Rule Chain with such external id already exists!"); throw e; diff --git a/dao/src/test/java/org/thingsboard/server/dao/rule/BaseRuleChainServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/rule/BaseRuleChainServiceTest.java new file mode 100644 index 0000000000..c9a91853ac --- /dev/null +++ b/dao/src/test/java/org/thingsboard/server/dao/rule/BaseRuleChainServiceTest.java @@ -0,0 +1,110 @@ +/** + * Copyright © 2016-2024 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.dao.rule; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.jupiter.api.Assertions; +import org.springframework.beans.factory.annotation.Autowired; +import org.thingsboard.common.util.JacksonUtil; +import org.thingsboard.server.common.data.id.RuleChainId; +import org.thingsboard.server.common.data.rule.RuleChain; +import org.thingsboard.server.dao.exception.DataValidationException; +import org.thingsboard.server.dao.service.AbstractServiceTest; +import org.thingsboard.server.dao.service.DaoSqlTest; + +import java.util.UUID; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; + +@DaoSqlTest +public class BaseRuleChainServiceTest extends AbstractServiceTest { + + @Autowired + private BaseRuleChainService ruleChainService; + + @Test + public void givenRuleChain_whenSave_thenReturnsSavedRuleChain() { + RuleChain ruleChain = getRuleChain(ruleChainWithoutId); + ruleChain.setTenantId(tenantId); + RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain); + + Assert.assertNotNull(savedRuleChain); + Assert.assertNotNull(savedRuleChain.getId()); + Assert.assertTrue(savedRuleChain.getCreatedTime() > 0); + Assert.assertEquals(ruleChain.getTenantId(), savedRuleChain.getTenantId()); + + + RuleChain foundRuleChain = ruleChainService.findRuleChainById(tenantId, savedRuleChain.getId()); + Assertions.assertEquals(foundRuleChain.getName(), savedRuleChain.getName()); + + ruleChainService.deleteRuleChainsByTenantId(tenantId); + } + + @Test + public void givenRuleChainWithExistingExternalId_whenSave_thenThrowsException() { + RuleChainId externalRuleChainId = new RuleChainId(UUID.fromString("2675d180-e1e5-11ee-9f06-71b6c7dc2cbf")); + + RuleChain ruleChain = getRuleChain(ruleChainWithoutId); + ruleChain.setTenantId(tenantId); + ruleChain.setExternalId(externalRuleChainId); + RuleChain savedRuleChain = ruleChainService.saveRuleChain(ruleChain); + + RuleChain ruleChainForSave = getRuleChain(ruleChainWithExternalId); + ruleChainForSave.setTenantId(tenantId); + + String expectedMsg = "Rule Chain with such external id already exists!"; + + assertEquals(savedRuleChain.getExternalId(), ruleChainForSave.getExternalId()); + Exception exception = assertThrows(DataValidationException.class, () -> ruleChainService.saveRuleChain(ruleChainForSave)); + assertEquals(expectedMsg, exception.getMessage()); + + ruleChainService.deleteRuleChainsByTenantId(tenantId); + } + + private RuleChain getRuleChain(String ruleChainString) { + return JacksonUtil.fromString(ruleChainString, RuleChain.class); + } + + private final String ruleChainWithoutId = "{\n" + + " \"name\": \"Root Rule Chain\",\n" + + " \"type\": \"CORE\",\n" + + " \"firstRuleNodeId\": {\n" + + " \"entityType\": \"RULE_NODE\",\n" + + " \"id\": \"91ad0b00-e779-11ee-9cf0-15d8b6079fdb\"\n" + + " },\n" + + " \"debugMode\": false,\n" + + " \"configuration\": null,\n" + + " \"additionalInfo\": null\n" + + "}"; + + private final String ruleChainWithExternalId = "{\n" + + " \"name\": \"Root Rule Chain\",\n" + + " \"type\": \"CORE\",\n" + + " \"firstRuleNodeId\": {\n" + + " \"entityType\": \"RULE_NODE\",\n" + + " \"id\": \"91ad0b00-e779-11ee-9cf0-15d8b6079fdb\"\n" + + " },\n" + + " \"debugMode\": false,\n" + + " \"externalId\": {\n" + + " \"entityType\": \"RULE_CHAIN\",\n" + + " \"id\": \"2675d180-e1e5-11ee-9f06-71b6c7dc2cbf\"\n" + + " },\n" + + " \"configuration\": null,\n" + + " \"additionalInfo\": null\n" + + "}"; +} \ No newline at end of file