Browse Source

fixed exceptions

pull/11121/head
IrynaMatveieva 2 years ago
parent
commit
c806685734
  1. 6
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbChangeOriginatorNode.java
  2. 2
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/EntitiesByNameAndTypeLoader.java
  3. 33
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbChangeOriginatorNodeTest.java

6
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/transform/TbChangeOriginatorNode.java

@ -114,7 +114,7 @@ public class TbChangeOriginatorNode extends TbAbstractTransformNode<TbChangeOrig
private void validateConfig(TbChangeOriginatorNodeConfiguration conf) { private void validateConfig(TbChangeOriginatorNodeConfiguration conf) {
HashSet<String> knownSources = Sets.newHashSet(CUSTOMER_SOURCE, TENANT_SOURCE, RELATED_SOURCE, ALARM_ORIGINATOR_SOURCE, ENTITY_SOURCE); HashSet<String> knownSources = Sets.newHashSet(CUSTOMER_SOURCE, TENANT_SOURCE, RELATED_SOURCE, ALARM_ORIGINATOR_SOURCE, ENTITY_SOURCE);
if (!knownSources.contains(conf.getOriginatorSource())) { if (!knownSources.contains(conf.getOriginatorSource())) {
log.error("Unsupported source type '[{}]'! Only {} types are allowed.", conf.getOriginatorSource(), supportedOriginatorSourcesStr); log.error("Unsupported source type '{}'! Only {} types are allowed.", conf.getOriginatorSource(), supportedOriginatorSourcesStr);
throw new IllegalArgumentException("Unsupported source type '" + conf.getOriginatorSource() + throw new IllegalArgumentException("Unsupported source type '" + conf.getOriginatorSource() +
"'! Only " + supportedOriginatorSourcesStr + " types are allowed."); "'! Only " + supportedOriginatorSourcesStr + " types are allowed.");
} }
@ -128,11 +128,11 @@ public class TbChangeOriginatorNode extends TbAbstractTransformNode<TbChangeOrig
if (conf.getOriginatorSource().equals(ENTITY_SOURCE)) { if (conf.getOriginatorSource().equals(ENTITY_SOURCE)) {
if (conf.getEntityType() == null) { if (conf.getEntityType() == null) {
log.error("Entity type should be specified if '[{}]' source is selected.", ENTITY_SOURCE); log.error("Entity type should be specified if '{}' source is selected.", ENTITY_SOURCE);
throw new IllegalArgumentException("Entity type should be specified if 'Entity by name pattern' source is selected."); throw new IllegalArgumentException("Entity type should be specified if 'Entity by name pattern' source is selected.");
} }
if (StringUtils.isEmpty(conf.getEntityNamePattern())) { if (StringUtils.isEmpty(conf.getEntityNamePattern())) {
log.error("Name pattern should be specified if '[{}]' source is selected.", ENTITY_SOURCE); log.error("Name pattern should be specified if '{}' source is selected.", ENTITY_SOURCE);
throw new IllegalArgumentException("Name pattern should be specified if 'Entity by name pattern' source is selected."); throw new IllegalArgumentException("Name pattern should be specified if 'Entity by name pattern' source is selected.");
} }
EntitiesByNameAndTypeLoader.checkEntityType(EntityType.valueOf(conf.getEntityType())); EntitiesByNameAndTypeLoader.checkEntityType(EntityType.valueOf(conf.getEntityType()));

2
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/EntitiesByNameAndTypeLoader.java

@ -53,7 +53,7 @@ public class EntitiesByNameAndTypeLoader {
throw new IllegalStateException("Unexpected entity type " + entityType.name()); throw new IllegalStateException("Unexpected entity type " + entityType.name());
} }
if (targetEntity == null) { if (targetEntity == null) {
throw new IllegalStateException("Failed to found " + entityType.name() + " entity by name: '" + entityName + "'!"); throw new IllegalStateException("Failed to found " + entityType.name().toLowerCase() + " with name '" + entityName + "'!");
} }
return targetEntity.getId(); return targetEntity.getId();
} }

33
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/transform/TbChangeOriginatorNodeTest.java

@ -15,7 +15,6 @@
*/ */
package org.thingsboard.rule.engine.transform; package org.thingsboard.rule.engine.transform;
import com.datastax.oss.driver.api.core.uuid.Uuids;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -45,8 +44,6 @@ import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.RuleChainId;
import org.thingsboard.server.common.data.id.RuleNodeId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.msg.TbMsgType; import org.thingsboard.server.common.data.msg.TbMsgType;
import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.EntityRelation;
@ -55,7 +52,6 @@ import org.thingsboard.server.common.data.relation.EntitySearchDirection;
import org.thingsboard.server.common.data.relation.RelationEntityTypeFilter; import org.thingsboard.server.common.data.relation.RelationEntityTypeFilter;
import org.thingsboard.server.common.data.relation.RelationsSearchParameters; import org.thingsboard.server.common.data.relation.RelationsSearchParameters;
import org.thingsboard.server.common.msg.TbMsg; import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgDataType;
import org.thingsboard.server.common.msg.TbMsgMetaData; import org.thingsboard.server.common.msg.TbMsgMetaData;
import org.thingsboard.server.dao.asset.AssetService; import org.thingsboard.server.dao.asset.AssetService;
import org.thingsboard.server.dao.device.DeviceService; import org.thingsboard.server.dao.device.DeviceService;
@ -115,29 +111,6 @@ public class TbChangeOriginatorNodeTest {
config = new TbChangeOriginatorNodeConfiguration().defaultConfiguration(); config = new TbChangeOriginatorNodeConfiguration().defaultConfiguration();
} }
@Test
public void newChainCanBeStarted() throws TbNodeException {
Asset asset = new Asset();
asset.setCustomerId(CUSTOMER_ID);
RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased());
TbMsg msg = TbMsg.newMsg(TbMsgType.POST_TELEMETRY_REQUEST, ASSET_ID, TbMsgMetaData.EMPTY, TbMsgDataType.JSON, TbMsg.EMPTY_JSON_OBJECT, ruleChainId, ruleNodeId);
given(ctxMock.getDbCallbackExecutor()).willReturn(dbExecutor);
given(ctxMock.getAssetService()).willReturn(assetServiceMock);
given(assetServiceMock.findAssetByIdAsync(any(), eq(ASSET_ID))).willReturn(Futures.immediateFuture(asset));
node.init(ctxMock, new TbNodeConfiguration(JacksonUtil.valueToTree(config)));
node.onMsg(ctxMock, msg);
ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class);
then(ctxMock).should().transformMsgOriginator(msgCaptor.capture(), originatorCaptor.capture());
assertThat(originatorCaptor.getValue()).isEqualTo(CUSTOMER_ID);
}
@Test @Test
public void verifyDefaultConfig() { public void verifyDefaultConfig() {
var config = new TbChangeOriginatorNodeConfiguration().defaultConfiguration(); var config = new TbChangeOriginatorNodeConfiguration().defaultConfiguration();
@ -222,6 +195,7 @@ public class TbChangeOriginatorNodeTest {
node.onMsg(ctxMock, msg); node.onMsg(ctxMock, msg);
then(deviceServiceMock).should().findDeviceById(TENANT_ID, DEVICE_ID); then(deviceServiceMock).should().findDeviceById(TENANT_ID, DEVICE_ID);
then(ctxMock).should().transformMsgOriginator(msg, CUSTOMER_ID);
ArgumentCaptor<TbMsg> actualMsg = ArgumentCaptor.forClass(TbMsg.class); ArgumentCaptor<TbMsg> actualMsg = ArgumentCaptor.forClass(TbMsg.class);
then(ctxMock).should().tellSuccess(actualMsg.capture()); then(ctxMock).should().tellSuccess(actualMsg.capture());
assertThat(actualMsg.getValue()).usingRecursiveComparison().ignoringFields("ctx").isEqualTo(expectedMsg); assertThat(actualMsg.getValue()).usingRecursiveComparison().ignoringFields("ctx").isEqualTo(expectedMsg);
@ -241,6 +215,7 @@ public class TbChangeOriginatorNodeTest {
node.init(ctxMock, new TbNodeConfiguration(JacksonUtil.valueToTree(config))); node.init(ctxMock, new TbNodeConfiguration(JacksonUtil.valueToTree(config)));
node.onMsg(ctxMock, msg); node.onMsg(ctxMock, msg);
then(ctxMock).should().transformMsgOriginator(msg, TENANT_ID);
ArgumentCaptor<TbMsg> actualMsg = ArgumentCaptor.forClass(TbMsg.class); ArgumentCaptor<TbMsg> actualMsg = ArgumentCaptor.forClass(TbMsg.class);
then(ctxMock).should().tellSuccess(actualMsg.capture()); then(ctxMock).should().tellSuccess(actualMsg.capture());
assertThat(actualMsg.getValue()).usingRecursiveComparison().ignoringFields("ctx").isEqualTo(expectedMsg); assertThat(actualMsg.getValue()).usingRecursiveComparison().ignoringFields("ctx").isEqualTo(expectedMsg);
@ -297,6 +272,7 @@ public class TbChangeOriginatorNodeTest {
node.onMsg(ctxMock, msg); node.onMsg(ctxMock, msg);
then(alarmServiceMock).should().findAlarmByIdAsync(TENANT_ID, alarmId); then(alarmServiceMock).should().findAlarmByIdAsync(TENANT_ID, alarmId);
then(ctxMock).should().transformMsgOriginator(msg, DEVICE_ID);
ArgumentCaptor<TbMsg> actualMsg = ArgumentCaptor.forClass(TbMsg.class); ArgumentCaptor<TbMsg> actualMsg = ArgumentCaptor.forClass(TbMsg.class);
then(ctxMock).should().tellSuccess(actualMsg.capture()); then(ctxMock).should().tellSuccess(actualMsg.capture());
assertThat(actualMsg.getValue()).usingRecursiveComparison().ignoringFields("ctx").isEqualTo(expectedMsg); assertThat(actualMsg.getValue()).usingRecursiveComparison().ignoringFields("ctx").isEqualTo(expectedMsg);
@ -323,6 +299,7 @@ public class TbChangeOriginatorNodeTest {
String expectedEntityName = TbNodeUtils.processPattern(entityNamePattern, msg); String expectedEntityName = TbNodeUtils.processPattern(entityNamePattern, msg);
then(assetServiceMock).should().findAssetByTenantIdAndName(TENANT_ID, expectedEntityName); then(assetServiceMock).should().findAssetByTenantIdAndName(TENANT_ID, expectedEntityName);
then(ctxMock).should().transformMsgOriginator(msg, ASSET_ID);
ArgumentCaptor<TbMsg> actualMsg = ArgumentCaptor.forClass(TbMsg.class); ArgumentCaptor<TbMsg> actualMsg = ArgumentCaptor.forClass(TbMsg.class);
then(ctxMock).should().tellSuccess(actualMsg.capture()); then(ctxMock).should().tellSuccess(actualMsg.capture());
assertThat(actualMsg.getValue()).usingRecursiveComparison().ignoringFields("ctx").isEqualTo(expectedMsg); assertThat(actualMsg.getValue()).usingRecursiveComparison().ignoringFields("ctx").isEqualTo(expectedMsg);
@ -356,7 +333,7 @@ public class TbChangeOriginatorNodeTest {
ArgumentCaptor<Throwable> throwable = ArgumentCaptor.forClass(Throwable.class); ArgumentCaptor<Throwable> throwable = ArgumentCaptor.forClass(Throwable.class);
then(ctxMock).should().tellFailure(eq(msg), throwable.capture()); then(ctxMock).should().tellFailure(eq(msg), throwable.capture());
assertThat(throwable.getValue()).isInstanceOf(IllegalStateException.class).hasMessage("Failed to found ASSET entity by name: 'test-asset'!"); assertThat(throwable.getValue()).isInstanceOf(IllegalStateException.class).hasMessage("Failed to found asset with name 'test-asset'!");
} }
} }

Loading…
Cancel
Save