code review #2

This commit is contained in:
Yuriy Lytvynchuk 2023-01-23 15:48:21 +02:00
parent 0a57fc78dd
commit 9b05f7adf1
6 changed files with 36 additions and 35 deletions

View File

@ -36,10 +36,10 @@ public abstract class TbAbstractTypeSwitchNode implements TbNode {
}
@Override
public void onMsg(TbContext ctx, TbMsg msg) {
public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException {
ctx.tellNext(msg, getRelationType(ctx, msg.getOriginator()));
}
protected abstract String getRelationType(TbContext ctx, EntityId originator);
protected abstract String getRelationType(TbContext ctx, EntityId originator) throws TbNodeException;
}

View File

@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
import org.thingsboard.rule.engine.api.EmptyNodeConfiguration;
import org.thingsboard.rule.engine.api.RuleNode;
import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.asset.AssetProfile;
import org.thingsboard.server.common.data.id.AssetId;
@ -32,19 +33,20 @@ import org.thingsboard.server.common.data.plugin.ComponentType;
customRelations = true,
relationTypes = {},
configClazz = EmptyNodeConfiguration.class,
nodeDescription = "Route incoming messages by Asset Type",
nodeDetails = "Routes messages to chain according to the asset type",
nodeDescription = "Route incoming messages based on the name of the asset profile",
nodeDetails = "Route incoming messages based on the name of the asset profile. The asset profile name is case-sensitive",
uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbNodeEmptyConfig")
public class TbAssetTypeSwitchNode extends TbAbstractTypeSwitchNode {
protected String getRelationType(TbContext ctx, EntityId originator) {
@Override
protected String getRelationType(TbContext ctx, EntityId originator) throws TbNodeException {
if (!EntityType.ASSET.equals(originator.getEntityType())) {
throw new RuntimeException("Unsupported originator type: " + originator.getEntityType() + "! Only 'ASSET' type is allowed.");
throw new TbNodeException("Unsupported originator type: " + originator.getEntityType() + "! Only 'ASSET' type is allowed.");
}
AssetProfile assetProfile = ctx.getAssetProfileCache().get(ctx.getTenantId(), (AssetId) originator);
if (assetProfile == null) {
throw new RuntimeException("Asset profile for entity id: " + originator.getId() + " wasn't found!");
throw new TbNodeException("Asset profile for entity id: " + originator.getId() + " wasn't found!");
}
return assetProfile.getName();
}

View File

@ -19,6 +19,7 @@ import lombok.extern.slf4j.Slf4j;
import org.thingsboard.rule.engine.api.EmptyNodeConfiguration;
import org.thingsboard.rule.engine.api.RuleNode;
import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.DeviceId;
@ -30,21 +31,22 @@ import org.thingsboard.server.common.data.plugin.ComponentType;
type = ComponentType.FILTER,
name = "device type switch",
customRelations = true,
relationTypes = {},
relationTypes = {"default"},
configClazz = EmptyNodeConfiguration.class,
nodeDescription = "Route incoming messages by Device Type",
nodeDetails = "Routes messages to chain according to the device type",
nodeDescription = "Route incoming messages based on the name of the device profile",
nodeDetails = "Route incoming messages based on the name of the device profile. The device profile name is case-sensitive",
uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbNodeEmptyConfig")
public class TbDeviceTypeSwitchNode extends TbAbstractTypeSwitchNode {
protected String getRelationType(TbContext ctx, EntityId originator) {
@Override
protected String getRelationType(TbContext ctx, EntityId originator) throws TbNodeException {
if (!EntityType.DEVICE.equals(originator.getEntityType())) {
throw new RuntimeException("Unsupported originator type: " + originator.getEntityType() + "! Only 'DEVICE' type is allowed.");
throw new TbNodeException("Unsupported originator type: " + originator.getEntityType() + "! Only 'DEVICE' type is allowed.");
}
DeviceProfile deviceProfile = ctx.getDeviceProfileCache().get(ctx.getTenantId(), (DeviceId) originator);
if (deviceProfile == null) {
throw new RuntimeException("Device profile for entity id: " + originator.getId() + " wasn't found!");
throw new TbNodeException("Device profile for entity id: " + originator.getId() + " wasn't found!");
}
return deviceProfile.getName();
}

View File

@ -19,13 +19,10 @@ import lombok.extern.slf4j.Slf4j;
import org.thingsboard.rule.engine.api.EmptyNodeConfiguration;
import org.thingsboard.rule.engine.api.RuleNode;
import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNode;
import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.rule.engine.api.util.TbNodeUtils;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg;
@Slf4j
@RuleNode(
@ -37,19 +34,12 @@ import org.thingsboard.server.common.msg.TbMsg;
nodeDetails = "Routes messages to chain according to the entity type ('Device', 'Asset', etc.).",
uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbNodeEmptyConfig")
public class TbOriginatorTypeSwitchNode implements TbNode {
EmptyNodeConfiguration config;
public class TbOriginatorTypeSwitchNode extends TbAbstractTypeSwitchNode {
@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
this.config = TbNodeUtils.convert(configuration, EmptyNodeConfiguration.class);
}
@Override
public void onMsg(TbContext ctx, TbMsg msg) throws TbNodeException {
protected String getRelationType(TbContext ctx, EntityId originator) throws TbNodeException {
String relationType;
EntityType originatorType = msg.getOriginator().getEntityType();
EntityType originatorType = originator.getEntityType();
switch (originatorType) {
case TENANT:
relationType = "Tenant";
@ -87,7 +77,7 @@ public class TbOriginatorTypeSwitchNode implements TbNode {
default:
throw new TbNodeException("Unsupported originator type: " + originatorType);
}
ctx.tellNext(msg, relationType);
return relationType;
}
}

View File

@ -94,16 +94,20 @@ class TbAssetTypeSwitchNodeTest {
@Test
void givenMsg_whenOnMsg_then_Fail() {
CustomerId customerId = new CustomerId(UUID.randomUUID());
assertThatThrownBy(() -> node.onMsg(ctx, getTbMsg(customerId))).isInstanceOf(RuntimeException.class);
assertThatThrownBy(() -> {
node.onMsg(ctx, getTbMsg(customerId));
}).isInstanceOf(TbNodeException.class).hasMessageContaining("Unsupported originator type");
}
@Test
void givenMsg_whenOnMsg_EntityIdDeleted_then_Fail() {
assertThatThrownBy(() -> node.onMsg(ctx, getTbMsg(assetIdDeleted))).isInstanceOf(RuntimeException.class);
assertThatThrownBy(() -> {
node.onMsg(ctx, getTbMsg(assetIdDeleted));
}).isInstanceOf(TbNodeException.class).hasMessageContaining("Asset profile for entity id");
}
@Test
void givenMsg_whenOnMsg_then_Success() {
void givenMsg_whenOnMsg_then_Success() throws TbNodeException {
TbMsg msg = getTbMsg(assetId);
node.onMsg(ctx, msg);

View File

@ -34,7 +34,6 @@ import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgMetaData;
import org.thingsboard.server.common.msg.queue.TbMsgCallback;
import java.util.Map;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
@ -95,16 +94,20 @@ class TbDeviceTypeSwitchNodeTest {
@Test
void givenMsg_whenOnMsg_then_Fail() {
CustomerId customerId = new CustomerId(UUID.randomUUID());
assertThatThrownBy(() -> node.onMsg(ctx, getTbMsg(customerId))).isInstanceOf(RuntimeException.class);
assertThatThrownBy(() -> {
node.onMsg(ctx, getTbMsg(customerId));
}).isInstanceOf(TbNodeException.class).hasMessageContaining("Unsupported originator type");
}
@Test
void givenMsg_whenOnMsg_EntityIdDeleted_then_Fail() {
assertThatThrownBy(() -> node.onMsg(ctx, getTbMsg(deviceIdDeleted))).isInstanceOf(RuntimeException.class);
assertThatThrownBy(() -> {
node.onMsg(ctx, getTbMsg(deviceIdDeleted));
}).isInstanceOf(TbNodeException.class).hasMessageContaining("Device profile for entity id");
}
@Test
void givenMsg_whenOnMsg_then_Success() {
void givenMsg_whenOnMsg_then_Success() throws TbNodeException {
TbMsg msg = getTbMsg(deviceId);
node.onMsg(ctx, msg);