Browse Source

fixed error message for problem with external id

pull/10414/head
IrynaMatveieva 2 years ago
parent
commit
e879f63376
  1. 22
      dao/src/main/java/org/thingsboard/server/dao/rule/BaseRuleChainService.java
  2. 110
      dao/src/test/java/org/thingsboard/server/dao/rule/BaseRuleChainServiceTest.java

22
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;

110
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" +
"}";
}
Loading…
Cancel
Save