Browse Source

code rewiev

pull/7974/head
Yuriy Lytvynchuk 4 years ago
parent
commit
b7dd23157d
  1. 2
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/filter/TbAbstractTypeSwitchNode.java
  2. 7
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/filter/TbAssetTypeSwitchNode.java
  3. 7
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/filter/TbDeviceTypeSwitchNode.java
  4. 26
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/filter/TbAssetTypeSwitchNodeTest.java
  5. 25
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/filter/TbDeviceTypeSwitchNodeTest.java

2
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/filter/TbAbstractTypeSwitchNode.java

@ -28,7 +28,7 @@ import org.thingsboard.server.common.msg.TbMsg;
@Slf4j
public abstract class TbAbstractTypeSwitchNode implements TbNode {
protected EmptyNodeConfiguration config;
private EmptyNodeConfiguration config;
@Override
public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {

7
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/filter/TbAssetTypeSwitchNode.java

@ -20,6 +20,7 @@ import org.thingsboard.rule.engine.api.EmptyNodeConfiguration;
import org.thingsboard.rule.engine.api.RuleNode;
import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.asset.AssetProfile;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.plugin.ComponentType;
@ -41,7 +42,11 @@ public class TbAssetTypeSwitchNode extends TbAbstractTypeSwitchNode {
if (!EntityType.ASSET.equals(originator.getEntityType())) {
throw new RuntimeException("Unsupported originator type: " + originator.getEntityType() + "!");
}
return ctx.getAssetProfileCache().get(ctx.getTenantId(), (AssetId) originator).getName();
AssetProfile assetProfile = ctx.getAssetProfileCache().get(ctx.getTenantId(), (AssetId) originator);
if (assetProfile == null) {
throw new RuntimeException("Asset profile with entity id: " + originator.getId() + " doesn't not found!");
}
return assetProfile.getName();
}
}

7
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/filter/TbDeviceTypeSwitchNode.java

@ -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.server.common.data.DeviceProfile;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EntityId;
@ -41,7 +42,11 @@ public class TbDeviceTypeSwitchNode extends TbAbstractTypeSwitchNode {
if (!EntityType.DEVICE.equals(originator.getEntityType())) {
throw new RuntimeException("Unsupported originator type: " + originator.getEntityType() + "!");
}
return ctx.getDeviceProfileCache().get(ctx.getTenantId(), (DeviceId) originator).getName();
DeviceProfile deviceProfile = ctx.getDeviceProfileCache().get(ctx.getTenantId(), (DeviceId) originator);
if (deviceProfile == null) {
throw new RuntimeException("Device profile with entity id: " + originator.getId() + " doesn't not found!");
}
return deviceProfile.getName();
}
}

26
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/filter/TbAssetTypeSwitchNodeTest.java

@ -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;
@ -44,7 +43,6 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ -53,6 +51,7 @@ class TbAssetTypeSwitchNodeTest {
TenantId tenantId;
AssetId assetId;
AssetId assetIdDeleted;
AssetProfile assetProfile;
TbContext ctx;
TbAssetTypeSwitchNode node;
@ -64,6 +63,7 @@ class TbAssetTypeSwitchNodeTest {
void setUp() throws TbNodeException {
tenantId = new TenantId(UUID.randomUUID());
assetId = new AssetId(UUID.randomUUID());
assetIdDeleted = new AssetId(UUID.randomUUID());
assetProfile = new AssetProfile();
assetProfile.setTenantId(tenantId);
@ -71,7 +71,7 @@ class TbAssetTypeSwitchNodeTest {
//node
config = new EmptyNodeConfiguration();
node = spy(new TbAssetTypeSwitchNode());
node = new TbAssetTypeSwitchNode();
node.init(ctx, new TbNodeConfiguration(JacksonUtil.valueToTree(config)));
//init mock
@ -83,6 +83,7 @@ class TbAssetTypeSwitchNodeTest {
when(ctx.getAssetProfileCache()).thenReturn(assetProfileCache);
doReturn(assetProfile).when(assetProfileCache).get(tenantId, assetId);
doReturn(null).when(assetProfileCache).get(tenantId, assetIdDeleted);
}
@AfterEach
@ -93,12 +94,17 @@ 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(RuntimeException.class);
}
@Test
void givenMsg_whenOnMsg_EntityIdDeleted_then_Fail() {
assertThatThrownBy(() -> node.onMsg(ctx, getTbMsg(assetIdDeleted))).isInstanceOf(RuntimeException.class);
}
@Test
void givenMsg_whenOnMsg_then_Success() {
TbMsg msg = getTbMsg(assetId, "{}");
TbMsg msg = getTbMsg(assetId);
node.onMsg(ctx, msg);
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
@ -110,13 +116,7 @@ class TbAssetTypeSwitchNodeTest {
assertThat(newMsg).isSameAs(msg);
}
private TbMsg getTbMsg(EntityId entityId, String data) {
final Map<String, String> mdMap = Map.of(
"TestKey_1", "Test",
"country", "US",
"voltageDataValue", "220",
"city", "NY"
);
return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, new TbMsgMetaData(mdMap), data, callback);
private TbMsg getTbMsg(EntityId entityId) {
return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, new TbMsgMetaData(), "{}", callback);
}
}

25
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/filter/TbDeviceTypeSwitchNodeTest.java

@ -44,7 +44,6 @@ import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ -53,6 +52,7 @@ class TbDeviceTypeSwitchNodeTest {
TenantId tenantId;
DeviceId deviceId;
DeviceId deviceIdDeleted;
DeviceProfile deviceProfile;
TbContext ctx;
TbDeviceTypeSwitchNode node;
@ -64,6 +64,7 @@ class TbDeviceTypeSwitchNodeTest {
void setUp() throws TbNodeException {
tenantId = new TenantId(UUID.randomUUID());
deviceId = new DeviceId(UUID.randomUUID());
deviceIdDeleted = new DeviceId(UUID.randomUUID());
deviceProfile = new DeviceProfile();
deviceProfile.setTenantId(tenantId);
@ -71,7 +72,7 @@ class TbDeviceTypeSwitchNodeTest {
//node
config = new EmptyNodeConfiguration();
node = spy(new TbDeviceTypeSwitchNode());
node = new TbDeviceTypeSwitchNode();
node.init(ctx, new TbNodeConfiguration(JacksonUtil.valueToTree(config)));
//init mock
@ -83,6 +84,7 @@ class TbDeviceTypeSwitchNodeTest {
when(ctx.getDeviceProfileCache()).thenReturn(deviceProfileCache);
doReturn(deviceProfile).when(deviceProfileCache).get(tenantId, deviceId);
doReturn(null).when(deviceProfileCache).get(tenantId, deviceIdDeleted);
}
@AfterEach
@ -93,12 +95,17 @@ 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(RuntimeException.class);
}
@Test
void givenMsg_whenOnMsg_EntityIdDeleted_then_Fail() {
assertThatThrownBy(() -> node.onMsg(ctx, getTbMsg(deviceIdDeleted))).isInstanceOf(RuntimeException.class);
}
@Test
void givenMsg_whenOnMsg_then_Success() {
TbMsg msg = getTbMsg(deviceId, "{}");
TbMsg msg = getTbMsg(deviceId);
node.onMsg(ctx, msg);
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
@ -110,13 +117,7 @@ class TbDeviceTypeSwitchNodeTest {
assertThat(newMsg).isSameAs(msg);
}
private TbMsg getTbMsg(EntityId entityId, String data) {
final Map<String, String> mdMap = Map.of(
"TestKey_1", "Test",
"country", "US",
"voltageDataValue", "220",
"city", "NY"
);
return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, new TbMsgMetaData(mdMap), data, callback);
private TbMsg getTbMsg(EntityId entityId) {
return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, new TbMsgMetaData(), "{}", callback);
}
}

Loading…
Cancel
Save