Browse Source

Refactor: rewrite old tests and add some new

pull/8661/head
Dmytro Skarzhynets 4 years ago
parent
commit
58f5145564
  1. 13
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/CalculateDeltaNode.java
  2. 12
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/CalculateDeltaNodeConfiguration.java
  3. 2
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/FetchTo.java
  4. 2
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbAbstractGetAttributesNode.java
  5. 2
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbAbstractNodeWithFetchTo.java
  6. 1
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbFetchDeviceCredentialsNode.java
  7. 2
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbFetchDeviceCredentialsNodeConfiguration.java
  8. 2
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbGetAttributesNode.java
  9. 3
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbGetOriginatorFieldsNode.java
  10. 2
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbGetTenantDetailsNodeConfiguration.java
  11. 25
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/EntitiesCustomerIdAsyncLoader.java
  12. 49
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/EntitiesRelatedDeviceIdAsyncLoader.java
  13. 39
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/EntitiesRelatedEntityIdAsyncLoader.java
  14. 90
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/OldTbAbstractAttributeNodeTest.java
  15. 184
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/OldTbGetCustomerAttributeNodeTest.java
  16. 235
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/OldTbGetRelatedAttributeNodeTest.java
  17. 177
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/OldTbGetTenantAttributeNodeTest.java
  18. 115
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbFetchDeviceCredentialsNodeTest.java
  19. 281
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbGetAttributesNodeTest.java
  20. 421
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbGetCustomerAttributeNodeTest.java
  21. 52
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbGetOriginatorFieldsNodeTest.java
  22. 490
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbGetRelatedAttributeNodeTest.java
  23. 370
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbGetTenantAttributeNodeTest.java
  24. 134
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/util/EntitiesCustomerIdAsyncLoaderTest.java
  25. 44
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/util/EntitiesFieldsAsyncLoaderTest.java
  26. 12
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/util/TenantIdLoaderTest.java

13
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/CalculateDeltaNode.java

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2023 The Thingsboard Authors * Copyright © 2016-2023 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -21,6 +21,7 @@ import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture; import com.google.common.util.concurrent.ListenableFuture;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.thingsboard.common.util.DonAsynchron; import org.thingsboard.common.util.DonAsynchron;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.rule.engine.api.RuleNode; import org.thingsboard.rule.engine.api.RuleNode;
import org.thingsboard.rule.engine.api.TbContext; import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNode; import org.thingsboard.rule.engine.api.TbNode;
@ -34,7 +35,6 @@ import org.thingsboard.server.common.data.plugin.ComponentType;
import org.thingsboard.server.common.msg.TbMsg; import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.session.SessionMsgType; import org.thingsboard.server.common.msg.session.SessionMsgType;
import org.thingsboard.server.dao.timeseries.TimeseriesService; import org.thingsboard.server.dao.timeseries.TimeseriesService;
import org.thingsboard.common.util.JacksonUtil;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
@ -54,6 +54,7 @@ import java.util.concurrent.ConcurrentHashMap;
uiResources = {"static/rulenode/rulenode-core-config.js"}, uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbEnrichmentNodeCalculateDeltaConfig") configDirective = "tbEnrichmentNodeCalculateDeltaConfig")
public class CalculateDeltaNode implements TbNode { public class CalculateDeltaNode implements TbNode {
private Map<EntityId, ValueWithTs> cache; private Map<EntityId, ValueWithTs> cache;
private CalculateDeltaNodeConfiguration config; private CalculateDeltaNodeConfiguration config;
private TbContext ctx; private TbContext ctx;
@ -94,7 +95,6 @@ public class CalculateDeltaNode implements TbNode {
return; return;
} }
if (config.getRound() != null) { if (config.getRound() != null) {
delta = delta.setScale(config.getRound(), RoundingMode.HALF_UP); delta = delta.setScale(config.getRound(), RoundingMode.HALF_UP);
} }
@ -181,4 +181,5 @@ public class CalculateDeltaNode implements TbNode {
this.value = value; this.value = value;
} }
} }
} }

12
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/CalculateDeltaNodeConfiguration.java

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2023 The Thingsboard Authors * Copyright © 2016-2023 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -22,6 +22,7 @@ import org.thingsboard.rule.engine.api.NodeConfiguration;
@Data @Data
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class CalculateDeltaNodeConfiguration implements NodeConfiguration<CalculateDeltaNodeConfiguration> { public class CalculateDeltaNodeConfiguration implements NodeConfiguration<CalculateDeltaNodeConfiguration> {
private String inputValueKey; private String inputValueKey;
private String outputValueKey; private String outputValueKey;
private boolean useCache; private boolean useCache;
@ -32,13 +33,14 @@ public class CalculateDeltaNodeConfiguration implements NodeConfiguration<Calcul
@Override @Override
public CalculateDeltaNodeConfiguration defaultConfiguration() { public CalculateDeltaNodeConfiguration defaultConfiguration() {
CalculateDeltaNodeConfiguration configuration = new CalculateDeltaNodeConfiguration(); var configuration = new CalculateDeltaNodeConfiguration();
configuration.setInputValueKey("pulseCounter"); configuration.setInputValueKey("pulseCounter");
configuration.setOutputValueKey("delta"); configuration.setOutputValueKey("delta");
configuration.setUseCache(true); configuration.setUseCache(true);
configuration.setAddPeriodBetweenMsgs(false); configuration.setAddPeriodBetweenMsgs(false);
configuration.setPeriodValueKey("periodInMs"); configuration.setPeriodValueKey("periodInMs");
configuration.setTellFailureIfDeltaIsNegative(true); configuration.setTellFailureIfDeltaIsNegative(true);
configuration.setRound(0);
return configuration; return configuration;
} }

2
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/FetchTo.java

@ -16,6 +16,8 @@
package org.thingsboard.rule.engine.metadata; package org.thingsboard.rule.engine.metadata;
public enum FetchTo { public enum FetchTo {
DATA, DATA,
METADATA METADATA
} }

2
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbAbstractGetAttributesNode.java

@ -51,6 +51,7 @@ import static org.thingsboard.server.common.data.DataConstants.SHARED_SCOPE;
@Slf4j @Slf4j
public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeConfiguration, T extends EntityId> extends TbAbstractNodeWithFetchTo<C> { public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeConfiguration, T extends EntityId> extends TbAbstractNodeWithFetchTo<C> {
private static final String VALUE = "value"; private static final String VALUE = "value";
private static final String TS = "ts"; private static final String TS = "ts";
private boolean isTellFailureIfAbsent; private boolean isTellFailureIfAbsent;
@ -188,4 +189,5 @@ public abstract class TbAbstractGetAttributesNode<C extends TbGetAttributesNodeC
failuresPairSet.clear(); failuresPairSet.clear();
return new RuntimeException(errorMessage.toString()); return new RuntimeException(errorMessage.toString());
} }
} }

2
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbAbstractNodeWithFetchTo.java

@ -60,7 +60,7 @@ public abstract class TbAbstractNodeWithFetchTo<C extends TbAbstractFetchToNodeC
} }
protected ObjectNode getMsgDataAsObjectNode(TbMsg msg) { protected ObjectNode getMsgDataAsObjectNode(TbMsg msg) {
JsonNode msgDataNode = JacksonUtil.toJsonNode(msg.getData()); var msgDataNode = JacksonUtil.toJsonNode(msg.getData());
if (msgDataNode == null || !msgDataNode.isObject()) { if (msgDataNode == null || !msgDataNode.isObject()) {
throw new IllegalArgumentException("Message body is not an object!"); throw new IllegalArgumentException("Message body is not an object!");
} }

1
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbFetchDeviceCredentialsNode.java

@ -15,7 +15,6 @@
*/ */
package org.thingsboard.rule.engine.metadata; package org.thingsboard.rule.engine.metadata;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.rule.engine.api.RuleNode; import org.thingsboard.rule.engine.api.RuleNode;

2
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbFetchDeviceCredentialsNodeConfiguration.java

@ -24,10 +24,12 @@ import org.thingsboard.rule.engine.api.NodeConfiguration;
@EqualsAndHashCode(callSuper = true) @EqualsAndHashCode(callSuper = true)
@JsonIgnoreProperties(ignoreUnknown = true) @JsonIgnoreProperties(ignoreUnknown = true)
public class TbFetchDeviceCredentialsNodeConfiguration extends TbAbstractFetchToNodeConfiguration implements NodeConfiguration<TbFetchDeviceCredentialsNodeConfiguration> { public class TbFetchDeviceCredentialsNodeConfiguration extends TbAbstractFetchToNodeConfiguration implements NodeConfiguration<TbFetchDeviceCredentialsNodeConfiguration> {
@Override @Override
public TbFetchDeviceCredentialsNodeConfiguration defaultConfiguration() { public TbFetchDeviceCredentialsNodeConfiguration defaultConfiguration() {
var configuration = new TbFetchDeviceCredentialsNodeConfiguration(); var configuration = new TbFetchDeviceCredentialsNodeConfiguration();
configuration.setFetchTo(FetchTo.METADATA); configuration.setFetchTo(FetchTo.METADATA);
return configuration; return configuration;
} }
} }

2
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbGetAttributesNode.java

@ -42,6 +42,7 @@ import org.thingsboard.server.common.msg.TbMsg;
uiResources = {"static/rulenode/rulenode-core-config.js"}, uiResources = {"static/rulenode/rulenode-core-config.js"},
configDirective = "tbEnrichmentNodeOriginatorAttributesConfig") configDirective = "tbEnrichmentNodeOriginatorAttributesConfig")
public class TbGetAttributesNode extends TbAbstractGetAttributesNode<TbGetAttributesNodeConfiguration, EntityId> { public class TbGetAttributesNode extends TbAbstractGetAttributesNode<TbGetAttributesNodeConfiguration, EntityId> {
@Override @Override
protected TbGetAttributesNodeConfiguration loadNodeConfiguration(TbNodeConfiguration configuration) throws TbNodeException { protected TbGetAttributesNodeConfiguration loadNodeConfiguration(TbNodeConfiguration configuration) throws TbNodeException {
return TbNodeUtils.convert(configuration, TbGetAttributesNodeConfiguration.class); return TbNodeUtils.convert(configuration, TbGetAttributesNodeConfiguration.class);
@ -51,4 +52,5 @@ public class TbGetAttributesNode extends TbAbstractGetAttributesNode<TbGetAttrib
protected ListenableFuture<EntityId> findEntityIdAsync(TbContext ctx, TbMsg msg) { protected ListenableFuture<EntityId> findEntityIdAsync(TbContext ctx, TbMsg msg) {
return Futures.immediateFuture(msg.getOriginator()); return Futures.immediateFuture(msg.getOriginator());
} }
} }

3
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbGetOriginatorFieldsNode.java

@ -50,7 +50,7 @@ public class TbGetOriginatorFieldsNode extends TbAbstractNodeWithFetchTo<TbGetOr
@Override @Override
protected TbGetOriginatorFieldsConfiguration loadNodeConfiguration(TbNodeConfiguration configuration) throws TbNodeException { protected TbGetOriginatorFieldsConfiguration loadNodeConfiguration(TbNodeConfiguration configuration) throws TbNodeException {
var getOriginatorFieldsConfiguration = TbNodeUtils.convert(configuration, TbGetOriginatorFieldsConfiguration.class); var getOriginatorFieldsConfiguration = TbNodeUtils.convert(configuration, TbGetOriginatorFieldsConfiguration.class);
if (config.getFieldsMapping().isEmpty()) { if (getOriginatorFieldsConfiguration.getFieldsMapping().isEmpty()) {
throw new TbNodeException("At least one field mapping should be specified!"); throw new TbNodeException("At least one field mapping should be specified!");
} }
return getOriginatorFieldsConfiguration; return getOriginatorFieldsConfiguration;
@ -95,4 +95,5 @@ public class TbGetOriginatorFieldsNode extends TbAbstractNodeWithFetchTo<TbGetOr
}, ctx.getDbCallbackExecutor() }, ctx.getDbCallbackExecutor()
); );
} }
} }

2
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/metadata/TbGetTenantDetailsNodeConfiguration.java

@ -27,7 +27,7 @@ public class TbGetTenantDetailsNodeConfiguration extends TbAbstractGetEntityDeta
@Override @Override
public TbGetTenantDetailsNodeConfiguration defaultConfiguration() { public TbGetTenantDetailsNodeConfiguration defaultConfiguration() {
TbGetTenantDetailsNodeConfiguration configuration = new TbGetTenantDetailsNodeConfiguration(); var configuration = new TbGetTenantDetailsNodeConfiguration();
configuration.setDetailsList(Collections.emptyList()); configuration.setDetailsList(Collections.emptyList());
configuration.setFetchTo(FetchTo.DATA); configuration.setFetchTo(FetchTo.DATA);
return configuration; return configuration;

25
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/EntitiesCustomerIdAsyncLoader.java

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2023 The Thingsboard Authors * Copyright © 2016-2023 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -29,23 +29,24 @@ import org.thingsboard.server.common.data.id.UserId;
public class EntitiesCustomerIdAsyncLoader { public class EntitiesCustomerIdAsyncLoader {
public static ListenableFuture<CustomerId> findEntityIdAsync(TbContext ctx, EntityId original) { public static ListenableFuture<CustomerId> findEntityIdAsync(TbContext ctx, EntityId originator) {
switch (original.getEntityType()) { switch (originator.getEntityType()) {
case CUSTOMER: case CUSTOMER:
return Futures.immediateFuture((CustomerId) original); return Futures.immediateFuture((CustomerId) originator);
case USER: case USER:
return getCustomerAsync(ctx.getUserService().findUserByIdAsync(ctx.getTenantId(), (UserId) original)); return toCustomerIdAsync(ctx.getUserService().findUserByIdAsync(ctx.getTenantId(), (UserId) originator));
case ASSET: case ASSET:
return getCustomerAsync(ctx.getAssetService().findAssetByIdAsync(ctx.getTenantId(), (AssetId) original)); return toCustomerIdAsync(ctx.getAssetService().findAssetByIdAsync(ctx.getTenantId(), (AssetId) originator));
case DEVICE: case DEVICE:
return getCustomerAsync(ctx.getDeviceService().findDeviceByIdAsync(ctx.getTenantId(), (DeviceId) original)); return toCustomerIdAsync(ctx.getDeviceService().findDeviceByIdAsync(ctx.getTenantId(), (DeviceId) originator));
default: default:
return Futures.immediateFailedFuture(new TbNodeException("Unexpected original EntityType " + original.getEntityType())); return Futures.immediateFailedFuture(new TbNodeException("Unexpected originator EntityType: " + originator.getEntityType()));
} }
} }
private static <T extends HasCustomerId> ListenableFuture<CustomerId> getCustomerAsync(ListenableFuture<T> future) { private static <T extends HasCustomerId> ListenableFuture<CustomerId> toCustomerIdAsync(ListenableFuture<T> future) {
return Futures.transformAsync(future, in -> in != null ? Futures.immediateFuture(in.getCustomerId()) return Futures.transformAsync(future, in -> in != null ? Futures.immediateFuture(in.getCustomerId())
: Futures.immediateFuture(null), MoreExecutors.directExecutor()); : Futures.immediateFuture(null), MoreExecutors.directExecutor());
} }
} }

49
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/EntitiesRelatedDeviceIdAsyncLoader.java

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2023 The Thingsboard Authors * Copyright © 2016-2023 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -21,35 +21,42 @@ import com.google.common.util.concurrent.MoreExecutors;
import org.apache.commons.collections.CollectionUtils; import org.apache.commons.collections.CollectionUtils;
import org.thingsboard.rule.engine.api.TbContext; import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.data.DeviceRelationsQuery; import org.thingsboard.rule.engine.data.DeviceRelationsQuery;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.device.DeviceSearchQuery; import org.thingsboard.server.common.data.device.DeviceSearchQuery;
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.relation.RelationsSearchParameters; import org.thingsboard.server.common.data.relation.RelationsSearchParameters;
import org.thingsboard.server.dao.device.DeviceService;
import java.util.List;
public class EntitiesRelatedDeviceIdAsyncLoader { public class EntitiesRelatedDeviceIdAsyncLoader {
public static ListenableFuture<DeviceId> findDeviceAsync(TbContext ctx, EntityId originator, public static ListenableFuture<DeviceId> findDeviceAsync(
DeviceRelationsQuery deviceRelationsQuery) { TbContext ctx,
DeviceService deviceService = ctx.getDeviceService(); EntityId originator,
DeviceSearchQuery query = buildQuery(originator, deviceRelationsQuery); DeviceRelationsQuery deviceRelationsQuery
) {
ListenableFuture<List<Device>> asyncDevices = deviceService.findDevicesByQuery(ctx.getTenantId(), query); var deviceService = ctx.getDeviceService();
var query = buildQuery(originator, deviceRelationsQuery);
return Futures.transformAsync(asyncDevices, d -> CollectionUtils.isNotEmpty(d) ? Futures.immediateFuture(d.get(0).getId()) var devicesListFuture = deviceService.findDevicesByQuery(ctx.getTenantId(), query);
: Futures.immediateFuture(null), MoreExecutors.directExecutor()); return Futures.transformAsync(devicesListFuture,
deviceList -> CollectionUtils.isNotEmpty(deviceList) ?
Futures.immediateFuture(deviceList.get(0).getId())
: Futures.immediateFuture(null), MoreExecutors.directExecutor());
} }
private static DeviceSearchQuery buildQuery(EntityId originator, DeviceRelationsQuery deviceRelationsQuery) { private static DeviceSearchQuery buildQuery(
DeviceSearchQuery query = new DeviceSearchQuery(); EntityId originator,
RelationsSearchParameters parameters = new RelationsSearchParameters(originator, DeviceRelationsQuery deviceRelationsQuery
deviceRelationsQuery.getDirection(), deviceRelationsQuery.getMaxLevel(), deviceRelationsQuery.isFetchLastLevelOnly()); ) {
var query = new DeviceSearchQuery();
var parameters = new RelationsSearchParameters(
originator,
deviceRelationsQuery.getDirection(),
deviceRelationsQuery.getMaxLevel(),
deviceRelationsQuery.isFetchLastLevelOnly()
);
query.setParameters(parameters); query.setParameters(parameters);
query.setRelationType(deviceRelationsQuery.getRelationType()); query.setRelationType(deviceRelationsQuery.getRelationType());
query.setDeviceTypes(deviceRelationsQuery.getDeviceTypes()); query.setDeviceTypes(deviceRelationsQuery.getDeviceTypes());
return query; return query;
} }
} }

39
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/EntitiesRelatedEntityIdAsyncLoader.java

@ -22,37 +22,44 @@ import org.apache.commons.collections.CollectionUtils;
import org.thingsboard.rule.engine.api.TbContext; import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.data.RelationsQuery; import org.thingsboard.rule.engine.data.RelationsQuery;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.relation.EntityRelationsQuery; import org.thingsboard.server.common.data.relation.EntityRelationsQuery;
import org.thingsboard.server.common.data.relation.EntitySearchDirection; import org.thingsboard.server.common.data.relation.EntitySearchDirection;
import org.thingsboard.server.common.data.relation.RelationsSearchParameters; import org.thingsboard.server.common.data.relation.RelationsSearchParameters;
import org.thingsboard.server.dao.relation.RelationService;
import java.util.List;
public class EntitiesRelatedEntityIdAsyncLoader { public class EntitiesRelatedEntityIdAsyncLoader {
public static ListenableFuture<EntityId> findEntityAsync(TbContext ctx, EntityId originator, public static ListenableFuture<EntityId> findEntityAsync(
RelationsQuery relationsQuery) { TbContext ctx,
RelationService relationService = ctx.getRelationService(); EntityId originator,
EntityRelationsQuery query = buildQuery(originator, relationsQuery); RelationsQuery relationsQuery
ListenableFuture<List<EntityRelation>> asyncRelation = relationService.findByQuery(ctx.getTenantId(), query); ) {
var relationService = ctx.getRelationService();
var query = buildQuery(originator, relationsQuery);
var relationListFuture = relationService.findByQuery(ctx.getTenantId(), query);
if (relationsQuery.getDirection() == EntitySearchDirection.FROM) { if (relationsQuery.getDirection() == EntitySearchDirection.FROM) {
return Futures.transformAsync(asyncRelation, r -> CollectionUtils.isNotEmpty(r) ? Futures.immediateFuture(r.get(0).getTo()) return Futures.transformAsync(relationListFuture,
: Futures.immediateFuture(null), MoreExecutors.directExecutor()); relationList -> CollectionUtils.isNotEmpty(relationList) ?
Futures.immediateFuture(relationList.get(0).getTo())
: Futures.immediateFuture(null), MoreExecutors.directExecutor());
} else if (relationsQuery.getDirection() == EntitySearchDirection.TO) { } else if (relationsQuery.getDirection() == EntitySearchDirection.TO) {
return Futures.transformAsync(asyncRelation, r -> CollectionUtils.isNotEmpty(r) ? Futures.immediateFuture(r.get(0).getFrom()) return Futures.transformAsync(relationListFuture,
: Futures.immediateFuture(null), MoreExecutors.directExecutor()); relationList -> CollectionUtils.isNotEmpty(relationList) ?
Futures.immediateFuture(relationList.get(0).getFrom())
: Futures.immediateFuture(null), MoreExecutors.directExecutor());
} }
return Futures.immediateFailedFuture(new IllegalStateException("Unknown direction")); return Futures.immediateFailedFuture(new IllegalStateException("Unknown direction"));
} }
private static EntityRelationsQuery buildQuery(EntityId originator, RelationsQuery relationsQuery) { private static EntityRelationsQuery buildQuery(EntityId originator, RelationsQuery relationsQuery) {
EntityRelationsQuery query = new EntityRelationsQuery(); var query = new EntityRelationsQuery();
RelationsSearchParameters parameters = new RelationsSearchParameters(originator, var parameters = new RelationsSearchParameters(
relationsQuery.getDirection(), relationsQuery.getMaxLevel(), relationsQuery.isFetchLastLevelOnly()); originator, relationsQuery.getDirection(),
relationsQuery.getMaxLevel(),
relationsQuery.isFetchLastLevelOnly()
);
query.setParameters(parameters); query.setParameters(parameters);
query.setFilters(relationsQuery.getFilters()); query.setFilters(relationsQuery.getFilters());
return query; return query;
} }
} }

90
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbAbstractAttributeNodeTest.java → rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/OldTbAbstractAttributeNodeTest.java

@ -16,7 +16,6 @@
package org.thingsboard.rule.engine.metadata; package org.thingsboard.rule.engine.metadata;
import com.datastax.oss.driver.api.core.uuid.Uuids; import com.datastax.oss.driver.api.core.uuid.Uuids;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.collect.Lists; import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -66,7 +65,7 @@ import static org.mockito.Mockito.when;
import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE; import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public abstract class TbAbstractAttributeNodeTest { public abstract class OldTbAbstractAttributeNodeTest {
final CustomerId customerId = new CustomerId(Uuids.timeBased()); final CustomerId customerId = new CustomerId(Uuids.timeBased());
final TenantId tenantId = TenantId.fromUUID(Uuids.timeBased()); final TenantId tenantId = TenantId.fromUUID(Uuids.timeBased());
final RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased()); final RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased());
@ -74,24 +73,23 @@ public abstract class TbAbstractAttributeNodeTest {
final String keyAttrConf = "${word}"; final String keyAttrConf = "${word}";
final String valueAttrConf = "${result}"; final String valueAttrConf = "${result}";
@Mock @Mock
TbContext ctx; protected TbContext ctxMock;
@Mock @Mock
AttributesService attributesService; protected AttributesService attributesServiceMock;
@Mock @Mock
TimeseriesService timeseriesService; protected TimeseriesService timeseriesServiceMock;
@Mock @Mock
UserService userService; protected UserService userServiceMock;
@Mock @Mock
AssetService assetService; protected AssetService assetServiceMock;
@Mock @Mock
DeviceService deviceService; protected DeviceService deviceServiceMock;
TbMsg msg; TbMsg msg;
Map<String, String> metaData; Map<String, String> metaData;
TbAbstractGetEntityAttrNode node; TbAbstractGetEntityAttrNode<? extends EntityId> node;
void init(TbAbstractGetEntityAttrNode node) throws TbNodeException { protected void init(TbAbstractGetEntityAttrNode node) throws TbNodeException {
ObjectMapper mapper = JacksonUtil.OBJECT_MAPPER; var nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(getTbNodeConfig()));
TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(getTbNodeConfig()));
metaData = new HashMap<>(); metaData = new HashMap<>();
metaData.putIfAbsent("word", "temperature"); metaData.putIfAbsent("word", "temperature");
@ -104,13 +102,13 @@ public abstract class TbAbstractAttributeNodeTest {
void errorThrownIfCannotLoadAttributes(User user) { void errorThrownIfCannotLoadAttributes(User user) {
msg = TbMsg.newMsg("USER", user.getId(), new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); msg = TbMsg.newMsg("USER", user.getId(), new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
when(ctx.getAttributesService()).thenReturn(attributesService); when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
when(attributesService.find(any(), eq(getEntityId()), eq(SERVER_SCOPE), anyCollection())) when(attributesServiceMock.find(any(), eq(getEntityId()), eq(SERVER_SCOPE), anyCollection()))
.thenThrow(new IllegalStateException("something wrong")); .thenThrow(new IllegalStateException("something wrong"));
node.onMsg(ctx, msg); node.onMsg(ctxMock, msg);
final ArgumentCaptor<Throwable> captor = ArgumentCaptor.forClass(Throwable.class); final ArgumentCaptor<Throwable> captor = ArgumentCaptor.forClass(Throwable.class);
verify(ctx).tellFailure(same(msg), captor.capture()); verify(ctxMock).tellFailure(same(msg), captor.capture());
Throwable value = captor.getValue(); Throwable value = captor.getValue();
assertEquals("something wrong", value.getMessage()); assertEquals("something wrong", value.getMessage());
@ -120,13 +118,13 @@ public abstract class TbAbstractAttributeNodeTest {
void errorThrownIfCannotLoadAttributesAsync(User user) { void errorThrownIfCannotLoadAttributesAsync(User user) {
msg = TbMsg.newMsg("USER", user.getId(), new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); msg = TbMsg.newMsg("USER", user.getId(), new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
when(ctx.getAttributesService()).thenReturn(attributesService); when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
when(attributesService.find(any(), eq(getEntityId()), eq(SERVER_SCOPE), anyCollection())) when(attributesServiceMock.find(any(), eq(getEntityId()), eq(SERVER_SCOPE), anyCollection()))
.thenReturn(Futures.immediateFailedFuture(new IllegalStateException("something wrong"))); .thenReturn(Futures.immediateFailedFuture(new IllegalStateException("something wrong")));
node.onMsg(ctx, msg); node.onMsg(ctxMock, msg);
final ArgumentCaptor<Throwable> captor = ArgumentCaptor.forClass(Throwable.class); final ArgumentCaptor<Throwable> captor = ArgumentCaptor.forClass(Throwable.class);
verify(ctx).tellFailure(same(msg), captor.capture()); verify(ctxMock).tellFailure(same(msg), captor.capture());
Throwable value = captor.getValue(); Throwable value = captor.getValue();
assertEquals("something wrong", value.getMessage()); assertEquals("something wrong", value.getMessage());
@ -136,9 +134,9 @@ public abstract class TbAbstractAttributeNodeTest {
void failedChainUsedIfCustomerCannotBeFound(User user) { void failedChainUsedIfCustomerCannotBeFound(User user) {
msg = TbMsg.newMsg("USER", user.getId(), new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); msg = TbMsg.newMsg("USER", user.getId(), new TbMsgMetaData(), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
node.onMsg(ctx, msg); node.onMsg(ctxMock, msg);
var exceptionCaptor = ArgumentCaptor.forClass(NoSuchElementException.class); var exceptionCaptor = ArgumentCaptor.forClass(NoSuchElementException.class);
verify(ctx).tellFailure(eq(msg), exceptionCaptor.capture()); verify(ctxMock).tellFailure(eq(msg), exceptionCaptor.capture());
assertThat(exceptionCaptor.getValue().getMessage()).contains("Did not find entity! Msg ID: "); assertThat(exceptionCaptor.getValue().getMessage()).contains("Did not find entity! Msg ID: ");
assertTrue(msg.getMetaData().getData().isEmpty()); assertTrue(msg.getMetaData().getData().isEmpty());
@ -146,30 +144,29 @@ public abstract class TbAbstractAttributeNodeTest {
void entityAttributeAddedInMetadata(EntityId entityId, String type) { void entityAttributeAddedInMetadata(EntityId entityId, String type) {
msg = TbMsg.newMsg(type, entityId, new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); msg = TbMsg.newMsg(type, entityId, new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
entityAttributeFetched(getEntityId()); entityAttributesFetched(getEntityId());
} }
void usersCustomerAttributesFetched(User user) { void usersCustomerAttributesFetched(User user) {
msg = TbMsg.newMsg("USER", user.getId(), new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); msg = TbMsg.newMsg("USER", user.getId(), new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
entityAttributeFetched(getEntityId()); entityAttributesFetched(getEntityId());
} }
void assetsCustomerAttributesFetched(Asset asset) { void assetsCustomerAttributesFetched(Asset asset) {
msg = TbMsg.newMsg("ASSET", asset.getId(), new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); msg = TbMsg.newMsg("ASSET", asset.getId(), new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
entityAttributeFetched(getEntityId()); entityAttributesFetched(getEntityId());
} }
void deviceCustomerAttributesFetched(Device device) { void deviceCustomerAttributesFetched(Device device) {
msg = TbMsg.newMsg("DEVICE", device.getId(), new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); msg = TbMsg.newMsg("DEVICE", device.getId(), new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
entityAttributeFetched(getEntityId()); entityAttributesFetched(getEntityId());
} }
void deviceCustomerTelemetryFetched(Device device) throws TbNodeException { void deviceCustomerTelemetryFetched(Device device) throws TbNodeException {
ObjectMapper mapper = JacksonUtil.OBJECT_MAPPER; var nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(getTbNodeConfigForTelemetry()));
TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(getTbNodeConfigForTelemetry()));
TbAbstractGetEntityAttrNode node = getEmptyNode(); TbAbstractGetEntityAttrNode node = getEmptyNode();
node.init(null, nodeConfiguration); node.init(null, nodeConfiguration);
@ -178,32 +175,33 @@ public abstract class TbAbstractAttributeNodeTest {
List<TsKvEntry> timeseries = Lists.newArrayList(new BasicTsKvEntry(1L, new StringDataEntry("temperature", "highest"))); List<TsKvEntry> timeseries = Lists.newArrayList(new BasicTsKvEntry(1L, new StringDataEntry("temperature", "highest")));
when(ctx.getTimeseriesService()).thenReturn(timeseriesService); when(ctxMock.getTimeseriesService()).thenReturn(timeseriesServiceMock);
when(timeseriesService.findLatest(any(), eq(getEntityId()), anyCollection())) when(timeseriesServiceMock.findLatest(any(), eq(getEntityId()), anyCollection()))
.thenReturn(Futures.immediateFuture(timeseries)); .thenReturn(Futures.immediateFuture(timeseries));
node.onMsg(ctx, msg); node.onMsg(ctxMock, msg);
verify(ctx).tellSuccess(msg); verify(ctxMock).tellSuccess(msg);
assertEquals(msg.getMetaData().getValue("answer"), "highest"); assertEquals(msg.getMetaData().getValue("answer"), "highest");
} }
void entityAttributeFetched(EntityId entityId) { protected void entityAttributesFetched(EntityId entityId) {
List<AttributeKvEntry> attributes = Lists.newArrayList(new BaseAttributeKvEntry(new StringDataEntry("temperature", "high"), 1L)); List<AttributeKvEntry> attributes = List.of(new BaseAttributeKvEntry(new StringDataEntry("temperature", "high"), 1L));
when(ctx.getAttributesService()).thenReturn(attributesService); when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
when(attributesService.find(any(), eq(entityId), eq(SERVER_SCOPE), anyCollection())) when(attributesServiceMock.find(any(), eq(entityId), eq(SERVER_SCOPE), anyCollection()))
.thenReturn(Futures.immediateFuture(attributes)); .thenReturn(Futures.immediateFuture(attributes));
node.onMsg(ctx, msg); node.onMsg(ctxMock, msg);
verify(ctx).tellSuccess(msg);
verify(ctxMock).tellSuccess(msg);
assertEquals(msg.getMetaData().getValue("answer"), "high"); assertEquals(msg.getMetaData().getValue("answer"), "high");
} }
TbGetEntityAttrNodeConfiguration getTbNodeConfig() { protected TbGetEntityAttrNodeConfiguration getTbNodeConfig() {
return getConfig(false); return getConfig(false);
} }
TbGetEntityAttrNodeConfiguration getTbNodeConfigForTelemetry() { protected TbGetEntityAttrNodeConfiguration getTbNodeConfigForTelemetry() {
return getConfig(true); return getConfig(true);
} }
@ -222,17 +220,17 @@ public abstract class TbAbstractAttributeNodeTest {
abstract EntityId getEntityId(); abstract EntityId getEntityId();
void mockFindDevice(Device device) { void mockFindDevice(Device device) {
when(ctx.getDeviceService()).thenReturn(deviceService); when(ctxMock.getDeviceService()).thenReturn(deviceServiceMock);
when(deviceService.findDeviceByIdAsync(any(), eq(device.getId()))).thenReturn(Futures.immediateFuture(device)); when(deviceServiceMock.findDeviceByIdAsync(any(), eq(device.getId()))).thenReturn(Futures.immediateFuture(device));
} }
void mockFindAsset(Asset asset) { void mockFindAsset(Asset asset) {
when(ctx.getAssetService()).thenReturn(assetService); when(ctxMock.getAssetService()).thenReturn(assetServiceMock);
when(assetService.findAssetByIdAsync(any(), eq(asset.getId()))).thenReturn(Futures.immediateFuture(asset)); when(assetServiceMock.findAssetByIdAsync(any(), eq(asset.getId()))).thenReturn(Futures.immediateFuture(asset));
} }
void mockFindUser(User user) { void mockFindUser(User user) {
when(ctx.getUserService()).thenReturn(userService); when(ctxMock.getUserService()).thenReturn(userServiceMock);
when(userService.findUserByIdAsync(any(), eq(user.getId()))).thenReturn(Futures.immediateFuture(user)); when(userServiceMock.findUserByIdAsync(any(), eq(user.getId()))).thenReturn(Futures.immediateFuture(user));
} }
} }

184
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/OldTbGetCustomerAttributeNodeTest.java

@ -0,0 +1,184 @@
/**
* Copyright © 2016-2023 The Thingsboard Authors
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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.rule.engine.metadata;
import com.datastax.oss.driver.api.core.uuid.Uuids;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.junit.MockitoJUnitRunner;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
import org.thingsboard.server.common.data.kv.StringDataEntry;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgDataType;
import org.thingsboard.server.common.msg.TbMsgMetaData;
import java.util.List;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE;
@RunWith(MockitoJUnitRunner.class)
public class OldTbGetCustomerAttributeNodeTest extends OldTbAbstractAttributeNodeTest {
User user = new User();
Asset asset = new Asset();
Device device = new Device();
@Before
public void initDataForTests() throws TbNodeException {
init(new TbGetCustomerAttributeNode());
user.setCustomerId(customerId);
user.setId(new UserId(UUID.randomUUID()));
asset.setCustomerId(customerId);
asset.setId(new AssetId(UUID.randomUUID()));
device.setCustomerId(customerId);
device.setId(new DeviceId(Uuids.timeBased()));
}
@Override
protected TbAbstractGetEntityAttrNode<CustomerId> getEmptyNode() {
return new TbGetCustomerAttributeNode();
}
@Override
protected EntityId getEntityId() {
return customerId;
}
@Test
public void errorThrownIfFetchToIsNull() {
var node = new TbGetCustomerAttributeNode();
var config = new TbGetEntityAttrNodeConfiguration().defaultConfiguration();
config.setFetchTo(null);
var nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
var exception = assertThrows(TbNodeException.class, () -> node.init(ctxMock, nodeConfiguration));
assertThat(exception.getMessage()).isEqualTo("FetchTo cannot be NULL!");
verify(ctxMock, never()).tellSuccess(any());
}
@Test
public void errorThrownIfMsgDataIsNotAnObjectAndFetchToData() {
node.fetchTo = FetchTo.DATA;
node.config.setFetchTo(FetchTo.DATA);
msg = TbMsg.newMsg("SOME_MESSAGE_TYPE", new CustomerId(UUID.randomUUID()), new TbMsgMetaData(), "[]");
var exception = assertThrows(IllegalArgumentException.class, () -> node.onMsg(ctxMock, msg));
assertThat(exception.getMessage()).isEqualTo("Message body is not an object!");
verify(ctxMock, never()).tellSuccess(any());
}
@Test
public void errorThrownIfCannotLoadAttributes() {
mockFindUser(user);
errorThrownIfCannotLoadAttributes(user);
}
@Test
public void errorThrownIfCannotLoadAttributesAsync() {
mockFindUser(user);
errorThrownIfCannotLoadAttributesAsync(user);
}
@Test
public void failedChainUsedIfCustomerCannotBeFound() {
when(ctxMock.getUserService()).thenReturn(userServiceMock);
when(userServiceMock.findUserByIdAsync(any(), eq(user.getId()))).thenReturn(Futures.immediateFuture(null));
failedChainUsedIfCustomerCannotBeFound(user);
}
@Test
public void customerAttributeAddedInMetadata() {
entityAttributeAddedInMetadata(customerId, "CUSTOMER");
}
@Test
public void customerAttributeAddedInData() {
node.fetchTo = FetchTo.DATA;
node.config.setFetchTo(FetchTo.DATA);
msg = TbMsg.newMsg("CUSTOMER", customerId, new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
List<AttributeKvEntry> attributes = Lists.newArrayList(new BaseAttributeKvEntry(new StringDataEntry("temperature", "high"), 1L));
when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
when(attributesServiceMock.find(any(), eq(customerId), eq(SERVER_SCOPE), anyCollection()))
.thenReturn(Futures.immediateFuture(attributes));
node.onMsg(ctxMock, msg);
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
var expectedMsgData = "{\"answer\":\"high\"}";
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(expectedMsgData);
}
@Test
public void usersCustomerAttributesFetched() {
mockFindUser(user);
usersCustomerAttributesFetched(user);
}
@Test
public void assetsCustomerAttributesFetched() {
mockFindAsset(asset);
assetsCustomerAttributesFetched(asset);
}
@Test
public void deviceCustomerAttributesFetched() {
mockFindDevice(device);
deviceCustomerAttributesFetched(device);
}
@Test
public void deviceCustomerTelemetryFetched() throws TbNodeException {
mockFindDevice(device);
deviceCustomerTelemetryFetched(device);
}
}

235
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/OldTbGetRelatedAttributeNodeTest.java

@ -0,0 +1,235 @@
/**
* Copyright © 2016-2023 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.rule.engine.metadata;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
import org.thingsboard.server.common.data.kv.StringDataEntry;
import org.thingsboard.server.common.data.relation.EntityRelation;
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.dao.relation.RelationService;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE;
@RunWith(MockitoJUnitRunner.class)
public class OldTbGetRelatedAttributeNodeTest extends OldTbAbstractAttributeNodeTest {
User user = new User();
Asset asset = new Asset();
Device device = new Device();
@Mock
private RelationService relationService;
private EntityRelation entityRelation;
@Before
public void initDataForTests() throws TbNodeException {
init(new TbGetRelatedAttributeNode());
entityRelation = new EntityRelation();
entityRelation.setTo(customerId);
entityRelation.setType(EntityRelation.CONTAINS_TYPE);
when(ctxMock.getRelationService()).thenReturn(relationService);
user.setCustomerId(customerId);
user.setId(new UserId(UUID.randomUUID()));
entityRelation.setFrom(user.getId());
asset.setCustomerId(customerId);
asset.setId(new AssetId(UUID.randomUUID()));
device.setCustomerId(customerId);
device.setId(new DeviceId(UUID.randomUUID()));
}
@Override
protected TbAbstractGetEntityAttrNode getEmptyNode() {
return new TbGetRelatedAttributeNode();
}
@Override
protected TbGetEntityAttrNodeConfiguration getTbNodeConfig() {
return getConfig(false);
}
@Override
protected TbGetEntityAttrNodeConfiguration getTbNodeConfigForTelemetry() {
return getConfig(true);
}
private TbGetEntityAttrNodeConfiguration getConfig(boolean isTelemetry) {
TbGetRelatedAttrNodeConfiguration config = new TbGetRelatedAttrNodeConfiguration();
config = config.defaultConfiguration();
Map<String, String> conf = new HashMap<>();
conf.put(keyAttrConf, valueAttrConf);
config.setAttrMapping(conf);
config.setTelemetry(isTelemetry);
config.setFetchTo(FetchTo.METADATA);
return config;
}
@Override
EntityId getEntityId() {
return customerId;
}
@Test
public void errorThrownIfFetchToIsNull() {
var node = new TbGetRelatedAttributeNode();
var config = new TbGetRelatedAttrNodeConfiguration().defaultConfiguration();
config.setFetchTo(null);
var nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
var exception = assertThrows(TbNodeException.class, () -> node.init(ctxMock, nodeConfiguration));
assertThat(exception.getMessage()).isEqualTo("FetchTo cannot be NULL!");
verify(ctxMock, never()).tellSuccess(any());
}
@Test
public void errorThrownIfMsgDataIsNotAnObjectAndFetchToData() {
node.fetchTo = FetchTo.DATA;
node.config.setFetchTo(FetchTo.DATA);
msg = TbMsg.newMsg("SOME_MESSAGE_TYPE", new DeviceId(UUID.randomUUID()), new TbMsgMetaData(), "[]");
var exception = assertThrows(IllegalArgumentException.class, () -> node.onMsg(ctxMock, msg));
assertThat(exception.getMessage()).isEqualTo("Message body is not an object!");
verify(ctxMock, never()).tellSuccess(any());
}
@Test
public void errorThrownIfCannotLoadAttributes() {
entityRelation.setFrom(user.getId());
entityRelation.setTo(customerId);
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation)));
errorThrownIfCannotLoadAttributes(user);
}
@Test
public void errorThrownIfCannotLoadAttributesAsync() {
entityRelation.setFrom(user.getId());
entityRelation.setTo(customerId);
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation)));
errorThrownIfCannotLoadAttributesAsync(user);
}
@Test
public void failedChainUsedIfCustomerCannotBeFound() {
entityRelation.setFrom(customerId);
entityRelation.setTo(null);
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation)));
failedChainUsedIfCustomerCannotBeFound(user);
}
@Test
public void customerAttributeAddedInMetadata() {
entityRelation.setFrom(customerId);
entityRelation.setTo(customerId);
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation)));
entityAttributeAddedInMetadata(customerId, "CUSTOMER");
}
@Test
public void customerAttributeAddedInData() {
node.fetchTo = FetchTo.DATA;
node.config.setFetchTo(FetchTo.DATA);
entityRelation.setFrom(customerId);
entityRelation.setTo(customerId);
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation)));
msg = TbMsg.newMsg("CUSTOMER", customerId, new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
List<AttributeKvEntry> attributes = Lists.newArrayList(new BaseAttributeKvEntry(new StringDataEntry("temperature", "high"), 1L));
when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
when(attributesServiceMock.find(any(), eq(customerId), eq(SERVER_SCOPE), anyCollection()))
.thenReturn(Futures.immediateFuture(attributes));
node.onMsg(ctxMock, msg);
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
var expectedMsgData = "{\"answer\":\"high\"}";
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(expectedMsgData);
}
@Test
public void usersCustomerAttributesFetched() {
entityRelation.setFrom(user.getId());
entityRelation.setTo(customerId);
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation)));
usersCustomerAttributesFetched(user);
}
@Test
public void assetsCustomerAttributesFetched() {
entityRelation.setFrom(asset.getId());
entityRelation.setTo(customerId);
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation)));
assetsCustomerAttributesFetched(asset);
}
@Test
public void deviceCustomerAttributesFetched() {
entityRelation.setFrom(device.getId());
entityRelation.setTo(customerId);
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation)));
deviceCustomerAttributesFetched(device);
}
@Test
public void deviceCustomerTelemetryFetched() throws TbNodeException {
entityRelation.setFrom(device.getId());
entityRelation.setTo(customerId);
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation)));
deviceCustomerTelemetryFetched(device);
}
}

177
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/OldTbGetTenantAttributeNodeTest.java

@ -0,0 +1,177 @@
/**
* Copyright © 2016-2023 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.rule.engine.metadata;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.junit.MockitoJUnitRunner;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
import org.thingsboard.server.common.data.kv.StringDataEntry;
import org.thingsboard.server.common.msg.TbMsg;
import org.thingsboard.server.common.msg.TbMsgDataType;
import org.thingsboard.server.common.msg.TbMsgMetaData;
import java.util.List;
import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE;
@RunWith(MockitoJUnitRunner.class)
public class OldTbGetTenantAttributeNodeTest extends OldTbAbstractAttributeNodeTest {
User user = new User();
Asset asset = new Asset();
Device device = new Device();
@Before
public void initDataForTests() throws TbNodeException {
init(new TbGetTenantAttributeNode());
user.setTenantId(tenantId);
user.setId(new UserId(UUID.randomUUID()));
asset.setTenantId(tenantId);
asset.setId(new AssetId(UUID.randomUUID()));
device.setTenantId(tenantId);
device.setId(new DeviceId(UUID.randomUUID()));
when(ctxMock.getTenantId()).thenReturn(tenantId);
}
@Override
protected TbAbstractGetEntityAttrNode getEmptyNode() {
return new TbGetTenantAttributeNode();
}
@Override
EntityId getEntityId() {
return tenantId;
}
@Test
public void errorThrownIfFetchToIsNull() {
var node = new TbGetTenantAttributeNode();
var config = new TbGetEntityAttrNodeConfiguration().defaultConfiguration();
config.setFetchTo(null);
var nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
var exception = assertThrows(TbNodeException.class, () -> node.init(ctxMock, nodeConfiguration));
assertThat(exception.getMessage()).isEqualTo("FetchTo cannot be NULL!");
verify(ctxMock, never()).tellSuccess(any());
}
@Test
public void errorThrownIfMsgDataIsNotAnObjectAndFetchToData() {
node.fetchTo = FetchTo.DATA;
node.config.setFetchTo(FetchTo.DATA);
msg = TbMsg.newMsg("SOME_MESSAGE_TYPE", new TenantId(UUID.randomUUID()), new TbMsgMetaData(), "[]");
var exception = assertThrows(IllegalArgumentException.class, () -> node.onMsg(ctxMock, msg));
assertThat(exception.getMessage()).isEqualTo("Message body is not an object!");
verify(ctxMock, never()).tellSuccess(any());
}
@Test
public void errorThrownIfCannotLoadAttributes() {
errorThrownIfCannotLoadAttributes(user);
}
@Test
public void errorThrownIfCannotLoadAttributesAsync() {
errorThrownIfCannotLoadAttributesAsync(user);
}
@Test
public void failedChainUsedIfTenantIdFromCtxCannotBeFound() {
when(ctxMock.getTenantId()).thenReturn(null);
failedChainUsedIfCustomerCannotBeFound(user);
}
@Test
public void customerAttributeAddedInMetadata() {
entityAttributeAddedInMetadata(tenantId, "TENANT");
}
@Test
public void customerAttributeAddedInData() {
node.fetchTo = FetchTo.DATA;
node.config.setFetchTo(FetchTo.DATA);
msg = TbMsg.newMsg("TENANT", tenantId, new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId);
List<AttributeKvEntry> attributes = Lists.newArrayList(new BaseAttributeKvEntry(new StringDataEntry("temperature", "high"), 1L));
when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
when(attributesServiceMock.find(any(), eq(tenantId), eq(SERVER_SCOPE), anyCollection()))
.thenReturn(Futures.immediateFuture(attributes));
node.onMsg(ctxMock, msg);
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
var expectedMsgData = "{\"answer\":\"high\"}";
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(expectedMsgData);
}
@Test
public void usersCustomerAttributesFetched() {
usersCustomerAttributesFetched(user);
}
@Test
public void assetsCustomerAttributesFetched() {
assetsCustomerAttributesFetched(asset);
}
@Test
public void deviceCustomerAttributesFetched() {
deviceCustomerAttributesFetched(device);
}
@Test
public void deviceCustomerTelemetryFetched() throws TbNodeException {
deviceCustomerTelemetryFetched(device);
}
}

115
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbFetchDeviceCredentialsNodeTest.java

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2023 The Thingsboard Authors * Copyright © 2016-2023 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -15,11 +15,14 @@
*/ */
package org.thingsboard.rule.engine.metadata; package org.thingsboard.rule.engine.metadata;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.rule.engine.api.TbContext; import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNodeConfiguration; import org.thingsboard.rule.engine.api.TbNodeConfiguration;
@ -39,46 +42,40 @@ import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.willAnswer; import static org.mockito.BDDMockito.willAnswer;
import static org.mockito.BDDMockito.willReturn; import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.thingsboard.server.common.data.security.DeviceCredentialsType.ACCESS_TOKEN; import static org.thingsboard.server.common.data.security.DeviceCredentialsType.ACCESS_TOKEN;
@ExtendWith(MockitoExtension.class)
public class TbFetchDeviceCredentialsNodeTest { public class TbFetchDeviceCredentialsNodeTest {
final ObjectMapper mapper = new ObjectMapper();
DeviceId deviceId; @Mock
TbFetchDeviceCredentialsNode node; private TbContext ctxMock;
TbFetchDeviceCredentialsNodeConfiguration config; @Mock
TbNodeConfiguration nodeConfiguration; private TbMsgCallback callbackMock;
TbContext ctx; @Mock
TbMsgCallback callback; private DeviceCredentialsService deviceCredentialsServiceMock;
DeviceCredentialsService deviceCredentialsService; @Spy
private TbFetchDeviceCredentialsNode node;
private DeviceId deviceId;
private TbFetchDeviceCredentialsNodeConfiguration config;
@BeforeEach @BeforeEach
void setUp() throws TbNodeException { void setUp() throws TbNodeException {
deviceId = new DeviceId(UUID.randomUUID()); deviceId = new DeviceId(UUID.randomUUID());
callback = mock(TbMsgCallback.class);
ctx = mock(TbContext.class);
config = new TbFetchDeviceCredentialsNodeConfiguration().defaultConfiguration(); config = new TbFetchDeviceCredentialsNodeConfiguration().defaultConfiguration();
config.setFetchTo(FetchTo.METADATA); config.setFetchTo(FetchTo.METADATA);
nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config)); node.init(ctxMock, new TbNodeConfiguration(JacksonUtil.valueToTree(config)));
node = spy(new TbFetchDeviceCredentialsNode());
node.init(ctx, nodeConfiguration);
deviceCredentialsService = mock(DeviceCredentialsService.class);
willReturn(deviceCredentialsService).given(ctx).getDeviceCredentialsService(); lenient().doReturn(deviceCredentialsServiceMock).when(ctxMock).getDeviceCredentialsService();
willAnswer(invocation -> { lenient().doAnswer(invocation -> {
DeviceCredentials deviceCredentials = new DeviceCredentials(); DeviceCredentials deviceCredentials = new DeviceCredentials();
deviceCredentials.setCredentialsType(ACCESS_TOKEN); deviceCredentials.setCredentialsType(ACCESS_TOKEN);
return deviceCredentials; return deviceCredentials;
}).given(deviceCredentialsService).findDeviceCredentialsByDeviceId(any(), any()); }).when(deviceCredentialsServiceMock).findDeviceCredentialsByDeviceId(any(), any());
willAnswer(invocation -> { lenient().doAnswer(invocation -> JacksonUtil.newObjectNode()).when(deviceCredentialsServiceMock).toCredentialsInfo(any());
return JacksonUtil.newObjectNode();
}).given(deviceCredentialsService).toCredentialsInfo(any());
} }
@AfterEach @AfterEach
@ -94,20 +91,24 @@ public class TbFetchDeviceCredentialsNodeTest {
@Test @Test
void givenDefaultConfig_whenVerify_thenOK() { void givenDefaultConfig_whenVerify_thenOK() {
TbFetchDeviceCredentialsNodeConfiguration defaultConfig = new TbFetchDeviceCredentialsNodeConfiguration().defaultConfiguration(); var defaultConfig = new TbFetchDeviceCredentialsNodeConfiguration().defaultConfiguration();
assertThat(defaultConfig.getFetchTo()).isEqualTo(FetchTo.METADATA); assertThat(defaultConfig.getFetchTo()).isEqualTo(FetchTo.METADATA);
} }
@Test @Test
void givenMsg_whenOnMsg_thenVerifyOutput() throws Exception { void givenValidMsg_whenOnMsg_thenVerifyOutput() throws Exception {
node.onMsg(ctx, getTbMsg(deviceId)); // GIVEN
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); // WHEN
verify(ctx, times(1)).tellSuccess(newMsgCaptor.capture()); node.onMsg(ctxMock, getTbMsg(deviceId));
verify(ctx, never()).tellFailure(any(), any());
verify(deviceCredentialsService, times(1)).findDeviceCredentialsByDeviceId(any(), any());
TbMsg newMsg = newMsgCaptor.getValue(); // THEN
var newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(newMsgCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
verify(deviceCredentialsServiceMock, times(1)).findDeviceCredentialsByDeviceId(any(), any());
var newMsg = newMsgCaptor.getValue();
assertThat(newMsg).isNotNull(); assertThat(newMsg).isNotNull();
assertThat(newMsg.getMetaData().getData().containsKey("credentials")).isEqualTo(true); assertThat(newMsg.getMetaData().getData().containsKey("credentials")).isEqualTo(true);
@ -115,29 +116,36 @@ public class TbFetchDeviceCredentialsNodeTest {
} }
@Test @Test
void givenUnsupportedOriginatorType_whenOnMsg_thenTellFailure() throws Exception { void givenUnsupportedOriginatorType_whenOnMsg_thenShouldTellFailure() throws Exception {
node.onMsg(ctx, getTbMsg(new CustomerId(UUID.randomUUID()))); // GIVEN
var randomCustomerId = new CustomerId(UUID.randomUUID());
// WHEN
node.onMsg(ctxMock, getTbMsg(randomCustomerId));
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); // THEN
ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class); var newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctx, never()).tellSuccess(any()); var exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
verify(ctx, times(1)).tellFailure(newMsgCaptor.capture(), exceptionCaptor.capture()); verify(ctxMock, never()).tellSuccess(any());
verify(ctxMock, times(1)).tellFailure(newMsgCaptor.capture(), exceptionCaptor.capture());
assertThat(exceptionCaptor.getValue()).isInstanceOf(RuntimeException.class); assertThat(exceptionCaptor.getValue()).isInstanceOf(RuntimeException.class);
} }
@Test @Test
void givenGetDeviceCredentials_whenOnMsg_thenTellFailure() throws Exception { void givenGetDeviceCredentials_whenOnMsg_thenShouldTellFailure() throws Exception {
willAnswer(invocation -> { // GIVEN
return null; willAnswer(invocation -> null)
}).given(deviceCredentialsService).findDeviceCredentialsByDeviceId(any(), any()); .given(deviceCredentialsServiceMock).findDeviceCredentialsByDeviceId(any(), any());
node.onMsg(ctx, getTbMsg(deviceId)); // WHEN
node.onMsg(ctxMock, getTbMsg(deviceId));
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); // THEN
ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(Exception.class); var newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctx, never()).tellSuccess(any()); var exceptionCaptor = ArgumentCaptor.forClass(Exception.class);
verify(ctx, times(1)).tellFailure(newMsgCaptor.capture(), exceptionCaptor.capture()); verify(ctxMock, never()).tellSuccess(any());
verify(ctxMock, times(1)).tellFailure(newMsgCaptor.capture(), exceptionCaptor.capture());
assertThat(exceptionCaptor.getValue()).isInstanceOf(RuntimeException.class); assertThat(exceptionCaptor.getValue()).isInstanceOf(RuntimeException.class);
} }
@ -148,9 +156,10 @@ public class TbFetchDeviceCredentialsNodeTest {
"city", "NY" "city", "NY"
); );
final TbMsgMetaData metaData = new TbMsgMetaData(mdMap); final var metaData = new TbMsgMetaData(mdMap);
final String data = "{\"TestAttribute_1\": \"humidity\", \"TestAttribute_2\": \"voltage\"}"; final String data = "{\"TestAttribute_1\": \"humidity\", \"TestAttribute_2\": \"voltage\"}";
return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, metaData, data, callback); return TbMsg.newMsg("POST_ATTRIBUTES_REQUEST", entityId, metaData, data, callbackMock);
} }
} }

281
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbAbstractGetAttributesNodeTest.java → rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbGetAttributesNodeTest.java

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2023 The Thingsboard Authors * Copyright © 2016-2023 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -16,18 +16,13 @@
package org.thingsboard.rule.engine.metadata; package org.thingsboard.rule.engine.metadata;
import com.datastax.oss.driver.api.core.uuid.Uuids; import com.datastax.oss.driver.api.core.uuid.Uuids;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import org.junit.After; import org.junit.After;
import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.MockitoJUnitRunner;
import org.thingsboard.common.util.AbstractListeningExecutor; import org.thingsboard.common.util.AbstractListeningExecutor;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
@ -53,31 +48,37 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class) @RunWith(MockitoJUnitRunner.class)
public class TbAbstractGetAttributesNodeTest { public class TbGetAttributesNodeTest {
final ObjectMapper mapper = new ObjectMapper(); private static final EntityId ORIGINATOR = new DeviceId(Uuids.timeBased());
private static final TenantId TENANT_ID = TenantId.fromUUID(Uuids.timeBased());
private EntityId originator = new DeviceId(Uuids.timeBased()); private AbstractListeningExecutor dbExecutor;
private TenantId tenantId = TenantId.fromUUID(Uuids.timeBased());
@Mock @Mock
private TbContext ctx; private TbContext ctxMock;
@Mock @Mock
private AttributesService attributesService; private AttributesService attributesServiceMock;
@Mock @Mock
private TimeseriesService tsService; private TimeseriesService timeseriesServiceMock;
private AbstractListeningExecutor dbExecutor;
private List<String> clientAttributes; private List<String> clientAttributes;
private List<String> serverAttributes; private List<String> serverAttributes;
private List<String> sharedAttributes; private List<String> sharedAttributes;
private List<String> tsKeys; private List<String> tsKeys;
private long ts; private long ts;
private TbGetAttributesNode node;
@Before @Before
public void before() throws TbNodeException { public void before() throws TbNodeException {
@ -89,18 +90,10 @@ public class TbAbstractGetAttributesNodeTest {
}; };
dbExecutor.init(); dbExecutor.init();
Mockito.reset(ctx); when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
Mockito.reset(attributesService); when(ctxMock.getTimeseriesService()).thenReturn(timeseriesServiceMock);
Mockito.reset(tsService); when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(ctxMock.getDbCallbackExecutor()).thenReturn(dbExecutor);
Mockito.reset(ctx);
Mockito.reset(attributesService);
Mockito.reset(tsService);
lenient().when(ctx.getAttributesService()).thenReturn(attributesService);
lenient().when(ctx.getTimeseriesService()).thenReturn(tsService);
lenient().when(ctx.getTenantId()).thenReturn(tenantId);
lenient().when(ctx.getDbCallbackExecutor()).thenReturn(dbExecutor);
clientAttributes = getAttributeNames("client"); clientAttributes = getAttributeNames("client");
serverAttributes = getAttributeNames("server"); serverAttributes = getAttributeNames("server");
@ -108,18 +101,16 @@ public class TbAbstractGetAttributesNodeTest {
tsKeys = List.of("temperature", "humidity", "unknown"); tsKeys = List.of("temperature", "humidity", "unknown");
ts = System.currentTimeMillis(); ts = System.currentTimeMillis();
Mockito.when(attributesService.find(tenantId, originator, DataConstants.CLIENT_SCOPE, clientAttributes)) when(attributesServiceMock.find(TENANT_ID, ORIGINATOR, DataConstants.CLIENT_SCOPE, clientAttributes))
.thenReturn(Futures.immediateFuture(getListAttributeKvEntry(clientAttributes, ts))); .thenReturn(Futures.immediateFuture(getListAttributeKvEntry(clientAttributes, ts)));
when(attributesServiceMock.find(TENANT_ID, ORIGINATOR, DataConstants.SERVER_SCOPE, serverAttributes))
Mockito.when(attributesService.find(tenantId, originator, DataConstants.SERVER_SCOPE, serverAttributes))
.thenReturn(Futures.immediateFuture(getListAttributeKvEntry(serverAttributes, ts))); .thenReturn(Futures.immediateFuture(getListAttributeKvEntry(serverAttributes, ts)));
when(attributesServiceMock.find(TENANT_ID, ORIGINATOR, DataConstants.SHARED_SCOPE, sharedAttributes))
Mockito.when(attributesService.find(tenantId, originator, DataConstants.SHARED_SCOPE, sharedAttributes))
.thenReturn(Futures.immediateFuture(getListAttributeKvEntry(sharedAttributes, ts))); .thenReturn(Futures.immediateFuture(getListAttributeKvEntry(sharedAttributes, ts)));
Mockito.when(tsService.findLatest(tenantId, originator, tsKeys)) when(timeseriesServiceMock.findLatest(TENANT_ID, ORIGINATOR, tsKeys))
.thenReturn(Futures.immediateFuture(getListTsKvEntry(tsKeys, ts))); .thenReturn(Futures.immediateFuture(getListTsKvEntry(tsKeys, ts)));
} }
@ -129,192 +120,197 @@ public class TbAbstractGetAttributesNodeTest {
} }
@Test @Test
public void fetchToMetadata_whenOnMsg_then_success() throws Exception { public void givenFetchAttributesToMetadata_whenOnMsg_thenShouldTellSuccess() throws Exception {
TbGetAttributesNode node = initNode(FetchTo.METADATA, false, false); // GIVEN
TbMsg msg = getTbMsg(originator); node = initNode(FetchTo.METADATA, false, false);
node.onMsg(ctx, msg); var msg = getTbMsg(ORIGINATOR);
// check msg // WHEN
TbMsg resultMsg = checkMsg(true); node.onMsg(ctxMock, msg);
// THEN
var resultMsg = checkMsg(true);
//check attributes
checkAttributes(resultMsg, FetchTo.METADATA, "cs_", clientAttributes); checkAttributes(resultMsg, FetchTo.METADATA, "cs_", clientAttributes);
checkAttributes(resultMsg, FetchTo.METADATA, "ss_", serverAttributes); checkAttributes(resultMsg, FetchTo.METADATA, "ss_", serverAttributes);
checkAttributes(resultMsg, FetchTo.METADATA, "shared_", sharedAttributes); checkAttributes(resultMsg, FetchTo.METADATA, "shared_", sharedAttributes);
//check timeseries checkTs(resultMsg, FetchTo.METADATA, false, tsKeys);
checkTs(resultMsg, false, false, tsKeys);
} }
@Test @Test
public void fetchToMetadata_latestWithTs_whenOnMsg_then_success() throws Exception { public void givenFetchLatestTimeseriesToMetadata_whenOnMsg_thenShouldTellSuccess() throws Exception {
TbGetAttributesNode node = initNode(FetchTo.METADATA, true, false); // GIVEN
TbMsg msg = getTbMsg(originator); node = initNode(FetchTo.METADATA, true, false);
node.onMsg(ctx, msg); var msg = getTbMsg(ORIGINATOR);
// WHEN
node.onMsg(ctxMock, msg);
// check msg // THEN
TbMsg resultMsg = checkMsg(true); var resultMsg = checkMsg(true);
//check attributes
checkAttributes(resultMsg, FetchTo.METADATA, "cs_", clientAttributes); checkAttributes(resultMsg, FetchTo.METADATA, "cs_", clientAttributes);
checkAttributes(resultMsg, FetchTo.METADATA, "ss_", serverAttributes); checkAttributes(resultMsg, FetchTo.METADATA, "ss_", serverAttributes);
checkAttributes(resultMsg, FetchTo.METADATA, "shared_", sharedAttributes); checkAttributes(resultMsg, FetchTo.METADATA, "shared_", sharedAttributes);
//check timeseries with ts checkTs(resultMsg, FetchTo.METADATA, true, tsKeys);
checkTs(resultMsg, false, true, tsKeys);
} }
@Test @Test
public void fetchToData_whenOnMsg_then_success() throws Exception { public void givenFetchAttributesToData_whenOnMsg_thenShouldTellSuccess() throws Exception {
TbGetAttributesNode node = initNode(FetchTo.DATA, false, false); // GIVEN
TbMsg msg = getTbMsg(originator); node = initNode(FetchTo.DATA, false, false);
node.onMsg(ctx, msg); var msg = getTbMsg(ORIGINATOR);
// check msg // WHEN
TbMsg resultMsg = checkMsg(true); node.onMsg(ctxMock, msg);
// THEN
var resultMsg = checkMsg(true);
//check attributes
checkAttributes(resultMsg, FetchTo.DATA, "cs_", clientAttributes); checkAttributes(resultMsg, FetchTo.DATA, "cs_", clientAttributes);
checkAttributes(resultMsg, FetchTo.DATA, "ss_", serverAttributes); checkAttributes(resultMsg, FetchTo.DATA, "ss_", serverAttributes);
checkAttributes(resultMsg, FetchTo.DATA, "shared_", sharedAttributes); checkAttributes(resultMsg, FetchTo.DATA, "shared_", sharedAttributes);
//check timeseries checkTs(resultMsg, FetchTo.DATA, false, tsKeys);
checkTs(resultMsg, true, false, tsKeys);
} }
@Test @Test
public void fetchToData_latestWithTs_whenOnMsg_then_success() throws Exception { public void givenFetchLatestTimeseriesToData_whenOnMsg_thenShouldTellSuccess() throws Exception {
TbGetAttributesNode node = initNode(FetchTo.DATA, true, false); // GIVEN
TbMsg msg = getTbMsg(originator); node = initNode(FetchTo.DATA, true, false);
node.onMsg(ctx, msg); var msg = getTbMsg(ORIGINATOR);
// WHEN
node.onMsg(ctxMock, msg);
// check msg // THEN
TbMsg resultMsg = checkMsg(true); var resultMsg = checkMsg(true);
//check attributes
checkAttributes(resultMsg, FetchTo.DATA, "cs_", clientAttributes); checkAttributes(resultMsg, FetchTo.DATA, "cs_", clientAttributes);
checkAttributes(resultMsg, FetchTo.DATA, "ss_", serverAttributes); checkAttributes(resultMsg, FetchTo.DATA, "ss_", serverAttributes);
checkAttributes(resultMsg, FetchTo.DATA, "shared_", sharedAttributes); checkAttributes(resultMsg, FetchTo.DATA, "shared_", sharedAttributes);
//check timeseries with ts checkTs(resultMsg, FetchTo.DATA, true, tsKeys);
checkTs(resultMsg, true, true, tsKeys);
} }
@Test @Test
public void fetchToMetadata_whenOnMsg_then_failure() throws Exception { public void givenFetchAttributesToMetadata_whenOnMsg_thenShouldTellFailure() throws Exception {
TbGetAttributesNode node = initNode(FetchTo.METADATA, false, true); // GIVEN
TbMsg msg = getTbMsg(originator); node = initNode(FetchTo.METADATA, false, true);
node.onMsg(ctx, msg); var msg = getTbMsg(ORIGINATOR);
// check msg // WHEN
TbMsg actualMsg = checkMsg(false); node.onMsg(ctxMock, msg);
// THEN
var actualMsg = checkMsg(false);
//check attributes
checkAttributes(actualMsg, FetchTo.METADATA, "cs_", clientAttributes); checkAttributes(actualMsg, FetchTo.METADATA, "cs_", clientAttributes);
checkAttributes(actualMsg, FetchTo.METADATA, "ss_", serverAttributes); checkAttributes(actualMsg, FetchTo.METADATA, "ss_", serverAttributes);
checkAttributes(actualMsg, FetchTo.METADATA, "shared_", sharedAttributes); checkAttributes(actualMsg, FetchTo.METADATA, "shared_", sharedAttributes);
//check timeseries with ts checkTs(actualMsg, FetchTo.METADATA, false, tsKeys);
checkTs(actualMsg, false, false, tsKeys);
} }
@Test @Test
public void fetchToData_whenOnMsg_then_failure() throws Exception { public void givenFetchLatestTimeseriesToData_whenOnMsg_thenShouldTellFailure() throws Exception {
TbGetAttributesNode node = initNode(FetchTo.DATA, true, true); // GIVEN
TbMsg msg = getTbMsg(originator); node = initNode(FetchTo.DATA, true, true);
node.onMsg(ctx, msg); var msg = getTbMsg(ORIGINATOR);
// WHEN
node.onMsg(ctxMock, msg);
// check msg // THEN
TbMsg actualMsg = checkMsg(false); var actualMsg = checkMsg(false);
//check attributes
checkAttributes(actualMsg, FetchTo.DATA, "cs_", clientAttributes); checkAttributes(actualMsg, FetchTo.DATA, "cs_", clientAttributes);
checkAttributes(actualMsg, FetchTo.DATA, "ss_", serverAttributes); checkAttributes(actualMsg, FetchTo.DATA, "ss_", serverAttributes);
checkAttributes(actualMsg, FetchTo.DATA, "shared_", sharedAttributes); checkAttributes(actualMsg, FetchTo.DATA, "shared_", sharedAttributes);
//check timeseries with ts checkTs(actualMsg, FetchTo.DATA, true, tsKeys);
checkTs(actualMsg, true, true, tsKeys);
} }
@Test @Test
public void fetchToData_whenOnMsg_and_data_is_not_object_then_failure() throws Exception { public void givenFetchLatestTimeseriesToDataAndDataIsNotJsonObject_whenOnMsg_thenException() throws Exception {
TbGetAttributesNode node = initNode(FetchTo.DATA, true, true); // GIVEN
TbMsg msg = TbMsg.newMsg("TEST", originator, new TbMsgMetaData(), "[]"); node = initNode(FetchTo.DATA, true, true);
node.onMsg(ctx, msg); var msg = TbMsg.newMsg("TEST", ORIGINATOR, new TbMsgMetaData(), "[]");
ArgumentCaptor<TbMsg> newMsgCaptor = ArgumentCaptor.forClass(TbMsg.class); // WHEN
ArgumentCaptor<Exception> exceptionCaptor = ArgumentCaptor.forClass(IllegalArgumentException.class); var exception = assertThrows(IllegalArgumentException.class, () -> node.onMsg(ctxMock, msg));
Mockito.verify(ctx, never()).tellSuccess(any());
Mockito.verify(ctx, Mockito.timeout(5000)).tellFailure(newMsgCaptor.capture(), exceptionCaptor.capture()); // THEN
verify(ctxMock, never()).tellSuccess(any());
Assert.assertSame(msg, newMsgCaptor.getValue()); assertThat(exception.getMessage()).isEqualTo("Message body is not an object!");
Assert.assertNotNull(exceptionCaptor.getValue());
} }
private TbMsg checkMsg(boolean checkSuccess) { private TbMsg checkMsg(boolean checkSuccess) {
ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class); var msgCaptor = ArgumentCaptor.forClass(TbMsg.class);
if (checkSuccess) { if (checkSuccess) {
Mockito.verify(ctx, Mockito.timeout(5000)).tellSuccess(msgCaptor.capture()); verify(ctxMock, timeout(5000)).tellSuccess(msgCaptor.capture());
} else { } else {
ArgumentCaptor<RuntimeException> exceptionCaptor = ArgumentCaptor.forClass(RuntimeException.class); var exceptionCaptor = ArgumentCaptor.forClass(RuntimeException.class);
Mockito.verify(ctx, never()).tellSuccess(any()); verify(ctxMock, never()).tellSuccess(any());
Mockito.verify(ctx, Mockito.timeout(5000)).tellFailure(msgCaptor.capture(), exceptionCaptor.capture()); verify(ctxMock, timeout(5000)).tellFailure(msgCaptor.capture(), exceptionCaptor.capture());
RuntimeException exception = exceptionCaptor.getValue(); var exception = exceptionCaptor.getValue();
Assert.assertNotNull(exception); assertNotNull(exception);
Assert.assertNotNull(exception.getMessage()); assertNotNull(exception.getMessage());
Assert.assertTrue(exception.getMessage().startsWith("The following attribute/telemetry keys is not present in the DB:")); assertTrue(exception.getMessage().startsWith("The following attribute/telemetry keys is not present in the DB:"));
} }
TbMsg resultMsg = msgCaptor.getValue(); var resultMsg = msgCaptor.getValue();
Assert.assertNotNull(resultMsg); assertNotNull(resultMsg);
Assert.assertNotNull(resultMsg.getMetaData()); assertNotNull(resultMsg.getMetaData());
Assert.assertNotNull(resultMsg.getData()); assertNotNull(resultMsg.getData());
return resultMsg; return resultMsg;
} }
private void checkAttributes(TbMsg actualMsg, FetchTo fetchTo, String prefix, List<String> attributes) { private void checkAttributes(TbMsg actualMsg, FetchTo fetchTo, String prefix, List<String> attributes) {
JsonNode msgData = JacksonUtil.toJsonNode(actualMsg.getData()); var msgData = JacksonUtil.toJsonNode(actualMsg.getData());
attributes.stream() attributes.stream()
.filter(attribute -> !attribute.equals("unknown")) .filter(attribute -> !attribute.equals("unknown"))
.forEach(attribute -> { .forEach(attribute -> {
String result; String result = null;
if (FetchTo.DATA.equals(fetchTo)) { if (FetchTo.DATA.equals(fetchTo)) {
result = msgData.get(prefix + attribute).asText(); result = msgData.get(prefix + attribute).asText();
} else { } else if (FetchTo.METADATA.equals(fetchTo)) {
result = actualMsg.getMetaData().getValue(prefix + attribute); result = actualMsg.getMetaData().getValue(prefix + attribute);
} }
Assert.assertNotNull(result); assertNotNull(result);
Assert.assertEquals(attribute + "_value", result); assertEquals(attribute + "_value", result);
}); });
} }
private void checkTs(TbMsg actualMsg, boolean fetchToData, boolean getLatestValueWithTs, List<String> tsKeys) { private void checkTs(TbMsg actualMsg, FetchTo fetchTo, boolean getLatestValueWithTs, List<String> tsKeys) {
JsonNode msgData = JacksonUtil.toJsonNode(actualMsg.getData()); var msgData = JacksonUtil.toJsonNode(actualMsg.getData());
long value = 1L; long value = 1L;
for (String key : tsKeys) { for (var key : tsKeys) {
if (key.equals("unknown")) { if (key.equals("unknown")) {
continue; continue;
} }
String actualValue; String actualValue = null;
String expectedValue; String expectedValue;
if (getLatestValueWithTs) { if (getLatestValueWithTs) {
expectedValue = "{\"ts\":" + ts + ",\"value\":{\"data\":" + value + "}}"; expectedValue = "{\"ts\":" + ts + ",\"value\":{\"data\":" + value + "}}";
} else { } else {
expectedValue = "{\"data\":" + value + "}"; expectedValue = "{\"data\":" + value + "}";
} }
if (fetchToData) { if (FetchTo.DATA.equals(fetchTo)) {
actualValue = JacksonUtil.toString(msgData.get(key)); actualValue = JacksonUtil.toString(msgData.get(key));
} else { } else if (FetchTo.METADATA.equals(fetchTo)) {
actualValue = actualMsg.getMetaData().getValue(key); actualValue = actualMsg.getMetaData().getValue(key);
} }
Assert.assertNotNull(actualValue); assertNotNull(actualValue);
Assert.assertEquals(expectedValue, actualValue); assertEquals(expectedValue, actualValue);
value++; value++;
} }
} }
private TbGetAttributesNode initNode(FetchTo fetchTo, boolean getLatestValueWithTs, boolean isTellFailureIfAbsent) throws TbNodeException { private TbGetAttributesNode initNode(FetchTo fetchTo, boolean getLatestValueWithTs, boolean isTellFailureIfAbsent) throws TbNodeException {
TbGetAttributesNodeConfiguration config = new TbGetAttributesNodeConfiguration(); var config = new TbGetAttributesNodeConfiguration();
config.setClientAttributeNames(List.of("client_attr_1", "client_attr_2", "${client_attr_metadata}", "unknown")); config.setClientAttributeNames(List.of("client_attr_1", "client_attr_2", "${client_attr_metadata}", "unknown"));
config.setServerAttributeNames(List.of("server_attr_1", "server_attr_2", "${server_attr_metadata}", "unknown")); config.setServerAttributeNames(List.of("server_attr_1", "server_attr_2", "${server_attr_metadata}", "unknown"));
config.setSharedAttributeNames(List.of("shared_attr_1", "shared_attr_2", "$[shared_attr_data]", "unknown")); config.setSharedAttributeNames(List.of("shared_attr_1", "shared_attr_2", "$[shared_attr_data]", "unknown"));
@ -322,17 +318,18 @@ public class TbAbstractGetAttributesNodeTest {
config.setFetchTo(fetchTo); config.setFetchTo(fetchTo);
config.setGetLatestValueWithTs(getLatestValueWithTs); config.setGetLatestValueWithTs(getLatestValueWithTs);
config.setTellFailureIfAbsent(isTellFailureIfAbsent); config.setTellFailureIfAbsent(isTellFailureIfAbsent);
TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(config));
TbGetAttributesNode node = new TbGetAttributesNode(); var nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
node.init(ctx, nodeConfiguration); var node = new TbGetAttributesNode();
node.init(ctxMock, nodeConfiguration);
return node; return node;
} }
private TbMsg getTbMsg(EntityId entityId) { private TbMsg getTbMsg(EntityId entityId) {
ObjectNode msgData = JacksonUtil.newObjectNode(); var msgData = JacksonUtil.newObjectNode();
msgData.put("shared_attr_data", "shared_attr_3"); msgData.put("shared_attr_data", "shared_attr_3");
TbMsgMetaData msgMetaData = new TbMsgMetaData(); var msgMetaData = new TbMsgMetaData();
msgMetaData.putValue("client_attr_metadata", "client_attr_3"); msgMetaData.putValue("client_attr_metadata", "client_attr_3");
msgMetaData.putValue("server_attr_metadata", "server_attr_3"); msgMetaData.putValue("server_attr_metadata", "server_attr_3");
@ -343,8 +340,8 @@ public class TbAbstractGetAttributesNodeTest {
return List.of(prefix + "_attr_1", prefix + "_attr_2", prefix + "_attr_3", "unknown"); return List.of(prefix + "_attr_1", prefix + "_attr_2", prefix + "_attr_3", "unknown");
} }
private List<AttributeKvEntry> getListAttributeKvEntry(List<String> attributes, long ts) { private List<AttributeKvEntry> getListAttributeKvEntry(List<String> attributesList, long ts) {
return attributes.stream() return attributesList.stream()
.filter(attribute -> !attribute.equals("unknown")) .filter(attribute -> !attribute.equals("unknown"))
.map(attribute -> toAttributeKvEntry(ts, attribute)) .map(attribute -> toAttributeKvEntry(ts, attribute))
.collect(Collectors.toList()); .collect(Collectors.toList());
@ -354,18 +351,18 @@ public class TbAbstractGetAttributesNodeTest {
return new BaseAttributeKvEntry(ts, new StringDataEntry(attribute, attribute + "_value")); return new BaseAttributeKvEntry(ts, new StringDataEntry(attribute, attribute + "_value"));
} }
private List<TsKvEntry> getListTsKvEntry(List<String> keys, long ts) { private List<TsKvEntry> getListTsKvEntry(List<String> keysList, long ts) {
long value = 1L; long value = 1L;
List<TsKvEntry> kvEntries = new ArrayList<>(); var kvEntriesList = new ArrayList<TsKvEntry>();
for (String key : keys) { for (var key : keysList) {
if (key.equals("unknown")) { if (key.equals("unknown")) {
continue; continue;
} }
String dataValue = "{\"data\":" + value + "}"; String dataValue = "{\"data\":" + value + "}";
kvEntries.add(new BasicTsKvEntry(ts, new JsonDataEntry(key, dataValue))); kvEntriesList.add(new BasicTsKvEntry(ts, new JsonDataEntry(key, dataValue)));
value++; value++;
} }
return kvEntries; return kvEntriesList;
} }
} }

421
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbGetCustomerAttributeNodeTest.java

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2023 The Thingsboard Authors * Copyright © 2016-2023 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -15,17 +15,21 @@
*/ */
package org.thingsboard.rule.engine.metadata; package org.thingsboard.rule.engine.metadata;
import com.datastax.oss.driver.api.core.uuid.Uuids;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import org.junit.Before; import com.google.common.util.concurrent.ListenableFuture;
import org.junit.Test; import org.jetbrains.annotations.NotNull;
import org.junit.runner.RunWith; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.common.util.ListeningExecutor;
import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNodeConfiguration; import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException; import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.asset.Asset;
@ -33,150 +37,401 @@ 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.TenantId;
import org.thingsboard.server.common.data.id.UserId; import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
import org.thingsboard.server.common.data.kv.BasicTsKvEntry;
import org.thingsboard.server.common.data.kv.StringDataEntry; import org.thingsboard.server.common.data.kv.StringDataEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry;
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.attributes.AttributesService;
import org.thingsboard.server.dao.device.DeviceService;
import org.thingsboard.server.dao.timeseries.TimeseriesService;
import org.thingsboard.server.dao.user.UserService;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.NoSuchElementException;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Callable;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection; import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE; import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class TbGetCustomerAttributeNodeTest extends TbAbstractAttributeNodeTest { public class TbGetCustomerAttributeNodeTest {
User user = new User();
Asset asset = new Asset();
Device device = new Device();
@Before private static final EntityId DUMMY_ENTITY_ID = new DeviceId(UUID.randomUUID());
public void initDataForTests() throws TbNodeException { private static final TenantId TENANT_ID = new TenantId(UUID.randomUUID());
init(new TbGetCustomerAttributeNode()); private static final ListeningExecutor DB_EXECUTOR = new ListeningExecutor() {
user.setCustomerId(customerId); @Override
user.setId(new UserId(UUID.randomUUID())); public <T> ListenableFuture<T> executeAsync(Callable<T> task) {
try {
return Futures.immediateFuture(task.call());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
asset.setCustomerId(customerId); @Override
asset.setId(new AssetId(UUID.randomUUID())); public void execute(@NotNull Runnable command) {
command.run();
}
};
@Mock
private TbContext ctxMock;
@Mock
private AttributesService attributesServiceMock;
@Mock
private TimeseriesService timeseriesServiceMock;
@Mock
private UserService userServiceMock;
@Mock
private AssetService assetServiceMock;
@Mock
private DeviceService deviceServiceMock;
private TbGetCustomerAttributeNode node;
private TbGetEntityAttrNodeConfiguration config;
private TbNodeConfiguration nodeConfiguration;
private TbMsg msg;
device.setCustomerId(customerId); @BeforeEach
device.setId(new DeviceId(Uuids.timeBased())); public void setUp() {
node = new TbGetCustomerAttributeNode();
config = new TbGetEntityAttrNodeConfiguration().defaultConfiguration();
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
} }
@Override @Test
protected TbAbstractGetEntityAttrNode getEmptyNode() { public void givenConfigWithNullFetchTo_whenInit_thenException() {
return new TbGetCustomerAttributeNode(); // GIVEN
} config.setFetchTo(null);
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
// WHEN
var exception = assertThrows(TbNodeException.class, () -> node.init(ctxMock, nodeConfiguration));
@Override // THEN
EntityId getEntityId() { assertThat(exception.getMessage()).isEqualTo("FetchTo cannot be null!");
return customerId; verify(ctxMock, never()).tellSuccess(any());
} }
@Test @Test
public void errorThrownIfFetchToIsNull() { public void givenDefaultConfig_whenInit_thenOK() throws TbNodeException {
var node = new TbGetCustomerAttributeNode(); // GIVEN
var config = new TbGetEntityAttrNodeConfiguration().defaultConfiguration();
config.setFetchTo(null);
var nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
var exception = assertThrows(TbNodeException.class, () -> node.init(ctx, nodeConfiguration)); // WHEN
node.init(ctxMock, nodeConfiguration);
assertThat(exception.getMessage()).isEqualTo("FetchTo cannot be NULL!"); // THEN
verify(ctx, never()).tellSuccess(any()); assertThat(node.config).isEqualTo(config);
assertThat(config.getAttrMapping()).isEqualTo(Map.of("alarmThreshold", "threshold"));
assertThat(config.isTelemetry()).isEqualTo(false);
assertThat(node.fetchTo).isEqualTo(FetchTo.METADATA);
} }
@Test @Test
public void errorThrownIfMsgDataIsNotAnObjectAndFetchToData() { public void givenCustomConfig_whenInit_thenOK() throws TbNodeException {
node.fetchTo = FetchTo.DATA; // GIVEN
node.config.setFetchTo(FetchTo.DATA); config.setAttrMapping(Map.of(
msg = TbMsg.newMsg("SOME_MESSAGE_TYPE", new CustomerId(UUID.randomUUID()), new TbMsgMetaData(), "[]"); "sourceAttr1", "targetKey1",
"sourceAttr2", "targetKey2",
"sourceAttr3", "targetKey3"));
config.setTelemetry(true);
config.setFetchTo(FetchTo.DATA);
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
var exception = assertThrows(IllegalArgumentException.class, () -> node.onMsg(ctx, msg)); // WHEN
node.init(ctxMock, nodeConfiguration);
assertThat(exception.getMessage()).isEqualTo("Message body is not an object!"); // THEN
verify(ctx, never()).tellSuccess(any()); assertThat(node.config).isEqualTo(config);
assertThat(config.getAttrMapping()).isEqualTo(Map.of(
"sourceAttr1", "targetKey1",
"sourceAttr2", "targetKey2",
"sourceAttr3", "targetKey3"));
assertThat(config.isTelemetry()).isEqualTo(true);
assertThat(node.fetchTo).isEqualTo(FetchTo.DATA);
} }
@Test @Test
public void errorThrownIfCannotLoadAttributes() { public void givenEmptyAttributesMapping_whenInit_thenException() {
mockFindUser(user); // SETUP
errorThrownIfCannotLoadAttributes(user); var expectedExceptionMessage = "At least one attribute mapping should be specified!";
// GIVEN
config.setAttrMapping(Collections.emptyMap());
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
// WHEN
var exception = assertThrows(TbNodeException.class, () -> node.init(ctxMock, nodeConfiguration));
// THEN
assertThat(exception.getMessage()).isEqualTo(expectedExceptionMessage);
verify(ctxMock, never()).tellSuccess(any());
} }
@Test @Test
public void errorThrownIfCannotLoadAttributesAsync() { public void givenMsgDataIsNotAnJsonObjectAndFetchToData_whenOnMsg_thenException() {
mockFindUser(user); // GIVEN
errorThrownIfCannotLoadAttributesAsync(user); node.fetchTo = FetchTo.DATA;
msg = TbMsg.newMsg("POST_TELEMETRY_REQUEST", DUMMY_ENTITY_ID, new TbMsgMetaData(), "[]");
// WHEN
var exception = assertThrows(IllegalArgumentException.class, () -> node.onMsg(ctxMock, msg));
// THEN
assertThat(exception.getMessage()).isEqualTo("Message body is not an object!");
verify(ctxMock, never()).tellSuccess(any());
} }
@Test @Test
public void failedChainUsedIfCustomerCannotBeFound() { public void givenEntityThatDoesNotBelongToTheCurrentTenant_whenOnMsg_thenException() {
when(ctx.getUserService()).thenReturn(userService); // SETUP
when(userService.findUserByIdAsync(any(), eq(user.getId()))).thenReturn(Futures.immediateFuture(null)); var expectedExceptionMessage = "Entity with id: '" + DUMMY_ENTITY_ID +
failedChainUsedIfCustomerCannotBeFound(user); "' specified in the configuration doesn't belong to the current tenant.";
// GIVEN
doThrow(new RuntimeException(expectedExceptionMessage)).when(ctxMock).checkTenantEntity(DUMMY_ENTITY_ID);
msg = TbMsg.newMsg("POST_TELEMETRY_REQUEST", DUMMY_ENTITY_ID, new TbMsgMetaData(), "{}");
// WHEN
var exception = assertThrows(RuntimeException.class, () -> node.onMsg(ctxMock, msg));
// THEN
assertThat(exception.getMessage()).isEqualTo(expectedExceptionMessage);
verify(ctxMock, never()).tellSuccess(any());
} }
@Test @Test
public void customerAttributeAddedInMetadata() { public void givenDidNotFindEntity_whenOnMsg_thenShouldTellFailure() {
entityAttributeAddedInMetadata(customerId, "CUSTOMER"); // GIVEN
var userId = new UserId(UUID.randomUUID());
msg = TbMsg.newMsg("POST_TELEMETRY_REQUEST", userId, new TbMsgMetaData(), "{}");
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
when(ctxMock.getUserService()).thenReturn(userServiceMock);
doReturn(Futures.immediateFuture(null)).when(userServiceMock).findUserByIdAsync(any(), any());
// WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
var actualExceptionCaptor = ArgumentCaptor.forClass(Throwable.class);
verify(ctxMock, never()).tellSuccess(any());
verify(ctxMock, times(1))
.tellFailure(actualMessageCaptor.capture(), actualExceptionCaptor.capture());
var actualMessage = actualMessageCaptor.getValue();
var actualException = actualExceptionCaptor.getValue();
var expectedExceptionMessage = String.format(
"Failed to find customer for entity with id %s and type %s",
userId.getId(), userId.getEntityType().getNormalName());
assertEquals(msg, actualMessage);
assertEquals(expectedExceptionMessage, actualException.getMessage());
assertInstanceOf(NoSuchElementException.class, actualException);
} }
@Test @Test
public void customerAttributeAddedInData() { public void givenFetchAttributesToData_whenOnMsg_thenShouldFetchAttributesToData() {
node.fetchTo = FetchTo.DATA; // GIVEN
node.config.setFetchTo(FetchTo.DATA); var device = new Device(new DeviceId(UUID.randomUUID()));
device.setCustomerId(new CustomerId(UUID.randomUUID()));
msg = TbMsg.newMsg("CUSTOMER", customerId, new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); prepareMsgAndConfig(FetchTo.DATA, false, device.getId());
List<AttributeKvEntry> attributes = Lists.newArrayList(new BaseAttributeKvEntry(new StringDataEntry("temperature", "high"), 1L)); when(ctxMock.getDeviceService()).thenReturn(deviceServiceMock);
doReturn(Futures.immediateFuture(device)).when(deviceServiceMock).findDeviceByIdAsync(any(), any());
when(ctx.getAttributesService()).thenReturn(attributesService); when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(attributesService.find(any(), eq(customerId), eq(SERVER_SCOPE), anyCollection())) when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
List<AttributeKvEntry> attributes = List.of(
new BaseAttributeKvEntry(new StringDataEntry("sourceKey1", "sourceValue1"), 1L),
new BaseAttributeKvEntry(new StringDataEntry("sourceKey2", "sourceValue2"), 2L),
new BaseAttributeKvEntry(new StringDataEntry("sourceKey3", "sourceValue3"), 3L)
);
when(attributesServiceMock.find(eq(TENANT_ID), eq(device.getCustomerId()), eq(SERVER_SCOPE), anyList()))
.thenReturn(Futures.immediateFuture(attributes)); .thenReturn(Futures.immediateFuture(attributes));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
node.onMsg(ctx, msg); // WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class); var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctx, times(1)).tellSuccess(actualMessageCaptor.capture()); verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgData = "{\"answer\":\"high\"}"; var expectedMsgData = "{\"temp\":42," +
"\"humidity\":77," +
"\"messageBodyPattern1\":\"targetKey2\"," +
"\"messageBodyPattern2\":\"sourceKey3\"," +
"\"targetKey1\":\"sourceValue1\"," +
"\"targetKey2\":\"sourceValue2\"," +
"\"targetKey3\":\"sourceValue3\"}";
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(expectedMsgData); assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(expectedMsgData);
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(msg.getMetaData());
} }
@Test @Test
public void usersCustomerAttributesFetched() { public void givenFetchAttributesToMetaData_whenOnMsg_thenShouldFetchAttributesToMetaData() {
mockFindUser(user); // GIVEN
usersCustomerAttributesFetched(user); var user = new User(new UserId(UUID.randomUUID()));
user.setCustomerId(new CustomerId(UUID.randomUUID()));
prepareMsgAndConfig(FetchTo.METADATA, false, user.getId());
when(ctxMock.getUserService()).thenReturn(userServiceMock);
doReturn(Futures.immediateFuture(user)).when(userServiceMock).findUserByIdAsync(any(), any());
when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
List<AttributeKvEntry> attributes = List.of(
new BaseAttributeKvEntry(new StringDataEntry("sourceKey1", "sourceValue1"), 1L),
new BaseAttributeKvEntry(new StringDataEntry("sourceKey2", "sourceValue2"), 2L),
new BaseAttributeKvEntry(new StringDataEntry("sourceKey3", "sourceValue3"), 3L)
);
when(attributesServiceMock.find(eq(TENANT_ID), eq(user.getCustomerId()), eq(SERVER_SCOPE), anyList()))
.thenReturn(Futures.immediateFuture(attributes));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
// WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgMetaData = new TbMsgMetaData(Map.of(
"metaDataPattern1", "sourceKey2",
"metaDataPattern2", "targetKey3",
"targetKey1", "sourceValue1",
"targetKey2", "sourceValue2",
"targetKey3", "sourceValue3"
));
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(msg.getData());
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(expectedMsgMetaData);
} }
@Test @Test
public void assetsCustomerAttributesFetched() { public void givenFetchTelemetryToData_whenOnMsg_thenShouldFetchTelemetryToData() {
mockFindAsset(asset); // GIVEN
assetsCustomerAttributesFetched(asset); var customer = new Customer(new CustomerId(UUID.randomUUID()));
prepareMsgAndConfig(FetchTo.DATA, true, customer.getId());
when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(ctxMock.getTimeseriesService()).thenReturn(timeseriesServiceMock);
List<TsKvEntry> timeseries = List.of(
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey1", "sourceValue1")),
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey2", "sourceValue2")),
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey3", "sourceValue3"))
);
when(timeseriesServiceMock.findLatest(eq(TENANT_ID), eq(customer.getId()), anyList()))
.thenReturn(Futures.immediateFuture(timeseries));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
// WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgData = "{\"temp\":42," +
"\"humidity\":77," +
"\"messageBodyPattern1\":\"targetKey2\"," +
"\"messageBodyPattern2\":\"sourceKey3\"," +
"\"targetKey1\":\"sourceValue1\"," +
"\"targetKey2\":\"sourceValue2\"," +
"\"targetKey3\":\"sourceValue3\"}";
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(expectedMsgData);
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(msg.getMetaData());
} }
@Test @Test
public void deviceCustomerAttributesFetched() { public void givenFetchTelemetryToMetaData_whenOnMsg_thenShouldFetchTelemetryToMetaData() {
mockFindDevice(device); // GIVEN
deviceCustomerAttributesFetched(device); var asset = new Asset(new AssetId(UUID.randomUUID()));
asset.setCustomerId(new CustomerId(UUID.randomUUID()));
prepareMsgAndConfig(FetchTo.METADATA, true, asset.getId());
when(ctxMock.getAssetService()).thenReturn(assetServiceMock);
doReturn(Futures.immediateFuture(asset)).when(assetServiceMock).findAssetByIdAsync(any(), any());
when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(ctxMock.getTimeseriesService()).thenReturn(timeseriesServiceMock);
List<TsKvEntry> timeseries = List.of(
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey1", "sourceValue1")),
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey2", "sourceValue2")),
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey3", "sourceValue3"))
);
when(timeseriesServiceMock.findLatest(eq(TENANT_ID), eq(asset.getCustomerId()), anyList()))
.thenReturn(Futures.immediateFuture(timeseries));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
// WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgMetaData = new TbMsgMetaData(Map.of(
"metaDataPattern1", "sourceKey2",
"metaDataPattern2", "targetKey3",
"targetKey1", "sourceValue1",
"targetKey2", "sourceValue2",
"targetKey3", "sourceValue3"
));
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(msg.getData());
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(expectedMsgMetaData);
} }
@Test private void prepareMsgAndConfig(FetchTo fetchTo, boolean isTelemetry, EntityId entityId) {
public void deviceCustomerTelemetryFetched() throws TbNodeException { config.setAttrMapping(Map.of(
mockFindDevice(device); "sourceKey1", "targetKey1",
deviceCustomerTelemetryFetched(device); "${metaDataPattern1}", "$[messageBodyPattern1]",
"$[messageBodyPattern2]", "${metaDataPattern2}"));
config.setTelemetry(isTelemetry);
config.setFetchTo(fetchTo);
node.config = config;
node.fetchTo = fetchTo;
var msgMetaData = new TbMsgMetaData();
msgMetaData.putValue("metaDataPattern1", "sourceKey2");
msgMetaData.putValue("metaDataPattern2", "targetKey3");
var msgData = "{\"temp\":42,\"humidity\":77,\"messageBodyPattern1\":\"targetKey2\",\"messageBodyPattern2\":\"sourceKey3\"}";
msg = TbMsg.newMsg("POST_TELEMETRY_REQUEST", entityId, msgMetaData, msgData);
} }
} }

52
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbGetOriginatorFieldsNodeTest.java

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2023 The Thingsboard Authors * Copyright © 2016-2023 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -54,8 +54,9 @@ import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
public class TbGetOriginatorFieldsNodeTest { public class TbGetOriginatorFieldsNodeTest {
private static final EntityId DUMMY_ENTITY_ID = new DeviceId(UUID.randomUUID()); private static final EntityId DUMMY_ENTITY_ID = new DeviceId(UUID.randomUUID());
public static final ListeningExecutor DB_EXECUTOR = new ListeningExecutor() { private static final ListeningExecutor DB_EXECUTOR = new ListeningExecutor() {
@Override @Override
public <T> ListenableFuture<T> executeAsync(Callable<T> task) { public <T> ListenableFuture<T> executeAsync(Callable<T> task) {
try { try {
@ -81,15 +82,14 @@ public class TbGetOriginatorFieldsNodeTest {
@BeforeEach @BeforeEach
public void setUp() { public void setUp() {
config = new TbGetOriginatorFieldsConfiguration();
node = new TbGetOriginatorFieldsNode(); node = new TbGetOriginatorFieldsNode();
config = new TbGetOriginatorFieldsConfiguration().defaultConfiguration();
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
} }
@Test @Test
public void givenConfigWithNullFetchTo_whenOnInit_thenException() { public void givenConfigWithNullFetchTo_whenInit_thenException() {
// GIVEN // GIVEN
config = config.defaultConfiguration();
config.setFetchTo(null); config.setFetchTo(null);
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
@ -97,15 +97,13 @@ public class TbGetOriginatorFieldsNodeTest {
var exception = assertThrows(TbNodeException.class, () -> node.init(ctxMock, nodeConfiguration)); var exception = assertThrows(TbNodeException.class, () -> node.init(ctxMock, nodeConfiguration));
// THEN // THEN
assertThat(exception.getMessage()).isEqualTo("FetchTo cannot be NULL!"); assertThat(exception.getMessage()).isEqualTo("FetchTo cannot be null!");
verify(ctxMock, never()).tellSuccess(any()); verify(ctxMock, never()).tellSuccess(any());
} }
@Test @Test
public void givenDefaultConfig_whenInit_thenOK() throws TbNodeException { public void givenDefaultConfig_whenInit_thenOK() throws TbNodeException {
// GIVEN // GIVEN
config = config.defaultConfiguration();
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
// WHEN // WHEN
node.init(ctxMock, nodeConfiguration); node.init(ctxMock, nodeConfiguration);
@ -311,35 +309,18 @@ public class TbGetOriginatorFieldsNodeTest {
} }
@Test @Test
public void givenEmptyFieldsMapping_whenOnMsg_thenShouldTellSuccessWithSameMsg() { public void givenEmptyFieldsMapping_whenInit_thenException() {
// GIVEN // GIVEN
config = config.defaultConfiguration();
config.setFieldsMapping(Collections.emptyMap()); config.setFieldsMapping(Collections.emptyMap());
config.setIgnoreNullStrings(false); nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
config.setFetchTo(FetchTo.METADATA);
node.config = config;
node.fetchTo = FetchTo.METADATA;
var msgMetaData = new TbMsgMetaData(Map.of(
"testKey1", "testValue1",
"testKey2", "123"));
var msgData = "[\"value1\",\"value2\"]";
msg = TbMsg.newMsg("POST_TELEMETRY_REQUEST", DUMMY_ENTITY_ID, msgMetaData, msgData);
// WHEN // WHEN
node.onMsg(ctxMock, msg); var exception = assertThrows(TbNodeException.class, () -> node.init(ctxMock, nodeConfiguration));
// THEN // THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class); assertThat(exception.getMessage()).isEqualTo("At least one field mapping should be specified!");
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture()); verify(ctxMock, never()).tellSuccess(any());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgMetaData = new TbMsgMetaData(Map.of(
"testKey1", "testValue1",
"testKey2", "123"
));
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(msgData);
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(expectedMsgMetaData);
} }
@Test @Test
@ -373,4 +354,5 @@ public class TbGetOriginatorFieldsNodeTest {
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(msgData); assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(msgData);
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(msgMetaData); assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(msgMetaData);
} }
}
}

490
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbGetRelatedAttributeNodeTest.java

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2023 The Thingsboard Authors * Copyright © 2016-2023 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -15,221 +15,459 @@
*/ */
package org.thingsboard.rule.engine.metadata; package org.thingsboard.rule.engine.metadata;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import org.junit.Before; import com.google.common.util.concurrent.ListenableFuture;
import org.junit.Test; import org.jetbrains.annotations.NotNull;
import org.junit.runner.RunWith; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.junit.jupiter.MockitoExtension;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.common.util.ListeningExecutor;
import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNodeConfiguration; import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException; import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.rule.engine.data.RelationsQuery;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.AssetId; import org.thingsboard.server.common.data.id.AssetId;
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.TenantId;
import org.thingsboard.server.common.data.id.UserId; import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
import org.thingsboard.server.common.data.kv.BasicTsKvEntry;
import org.thingsboard.server.common.data.kv.StringDataEntry; import org.thingsboard.server.common.data.kv.StringDataEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry;
import org.thingsboard.server.common.data.relation.EntityRelation; import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.relation.EntitySearchDirection;
import org.thingsboard.server.common.data.relation.RelationEntityTypeFilter;
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.attributes.AttributesService;
import org.thingsboard.server.dao.relation.RelationService; import org.thingsboard.server.dao.relation.RelationService;
import org.thingsboard.server.dao.timeseries.TimeseriesService;
import java.util.HashMap; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.NoSuchElementException;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Callable;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection; import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE; import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class TbGetRelatedAttributeNodeTest extends TbAbstractAttributeNodeTest { public class TbGetRelatedAttributeNodeTest {
User user = new User();
Asset asset = new Asset(); private static final EntityId DUMMY_ENTITY_ID = new DeviceId(UUID.randomUUID());
Device device = new Device(); private static final TenantId TENANT_ID = new TenantId(UUID.randomUUID());
private static final ListeningExecutor DB_EXECUTOR = new ListeningExecutor() {
@Override
public <T> ListenableFuture<T> executeAsync(Callable<T> task) {
try {
return Futures.immediateFuture(task.call());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void execute(@NotNull Runnable command) {
command.run();
}
};
@Mock @Mock
private RelationService relationService; private TbContext ctxMock;
@Mock
private AttributesService attributesServiceMock;
@Mock
private TimeseriesService timeseriesServiceMock;
@Mock
private RelationService relationServiceMock;
private TbGetRelatedAttributeNode node;
private TbGetRelatedAttrNodeConfiguration config;
private TbNodeConfiguration nodeConfiguration;
private EntityRelation entityRelation; private EntityRelation entityRelation;
private TbMsg msg;
@Before @BeforeEach
public void initDataForTests() throws TbNodeException { public void setUp() {
init(new TbGetRelatedAttributeNode()); node = new TbGetRelatedAttributeNode();
config = new TbGetRelatedAttrNodeConfiguration().defaultConfiguration();
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
entityRelation = new EntityRelation(); entityRelation = new EntityRelation();
entityRelation.setTo(customerId);
entityRelation.setType(EntityRelation.CONTAINS_TYPE);
when(ctx.getRelationService()).thenReturn(relationService);
user.setCustomerId(customerId);
user.setId(new UserId(UUID.randomUUID()));
entityRelation.setFrom(user.getId());
asset.setCustomerId(customerId);
asset.setId(new AssetId(UUID.randomUUID()));
device.setCustomerId(customerId);
device.setId(new DeviceId(UUID.randomUUID()));
} }
@Override @Test
protected TbAbstractGetEntityAttrNode getEmptyNode() { public void givenConfigWithNullFetchTo_whenInit_thenException() {
return new TbGetRelatedAttributeNode(); // GIVEN
} config.setFetchTo(null);
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
@Override // WHEN
TbGetEntityAttrNodeConfiguration getTbNodeConfig() { var exception = assertThrows(TbNodeException.class, () -> node.init(ctxMock, nodeConfiguration));
return getConfig(false);
}
@Override // THEN
TbGetEntityAttrNodeConfiguration getTbNodeConfigForTelemetry() { assertThat(exception.getMessage()).isEqualTo("FetchTo cannot be null!");
return getConfig(true); verify(ctxMock, never()).tellSuccess(any());
} }
private TbGetEntityAttrNodeConfiguration getConfig(boolean isTelemetry) { @Test
TbGetRelatedAttrNodeConfiguration config = new TbGetRelatedAttrNodeConfiguration(); public void givenDefaultConfig_whenInit_thenOK() throws TbNodeException {
config = config.defaultConfiguration(); // GIVEN
Map<String, String> conf = new HashMap<>();
conf.put(keyAttrConf, valueAttrConf); // WHEN
config.setAttrMapping(conf); node.init(ctxMock, nodeConfiguration);
config.setTelemetry(isTelemetry);
config.setFetchTo(FetchTo.METADATA); // THEN
return config; var nodeConfig = (TbGetRelatedAttrNodeConfiguration) node.config;
assertThat(nodeConfig).isEqualTo(config);
assertThat(nodeConfig.getAttrMapping()).isEqualTo(Map.of("serialNumber", "sn"));
assertThat(nodeConfig.isTelemetry()).isEqualTo(false);
assertThat(node.fetchTo).isEqualTo(FetchTo.METADATA);
var relationsQuery = new RelationsQuery();
var relationEntityTypeFilter = new RelationEntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.emptyList());
relationsQuery.setDirection(EntitySearchDirection.FROM);
relationsQuery.setMaxLevel(1);
relationsQuery.setFilters(Collections.singletonList(relationEntityTypeFilter));
assertThat(nodeConfig.getRelationsQuery()).isEqualTo(relationsQuery);
} }
@Override @Test
EntityId getEntityId() { public void givenCustomConfig_whenInit_thenOK() throws TbNodeException {
return customerId; // GIVEN
config.setAttrMapping(Map.of(
"sourceAttr1", "targetKey1",
"sourceAttr2", "targetKey2",
"sourceAttr3", "targetKey3"));
config.setTelemetry(true);
config.setFetchTo(FetchTo.DATA);
var relationsQuery = new RelationsQuery();
var relationEntityTypeFilter = new RelationEntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.emptyList());
relationsQuery.setDirection(EntitySearchDirection.FROM);
relationsQuery.setMaxLevel(1);
relationsQuery.setFilters(Collections.singletonList(relationEntityTypeFilter));
config.setRelationsQuery(relationsQuery);
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
// WHEN
node.init(ctxMock, nodeConfiguration);
// THEN
var nodeConfig = (TbGetRelatedAttrNodeConfiguration) node.config;
assertThat(nodeConfig).isEqualTo(config);
assertThat(nodeConfig.getAttrMapping()).isEqualTo(Map.of(
"sourceAttr1", "targetKey1",
"sourceAttr2", "targetKey2",
"sourceAttr3", "targetKey3"
));
assertThat(nodeConfig.isTelemetry()).isEqualTo(true);
assertThat(node.fetchTo).isEqualTo(FetchTo.DATA);
assertThat(nodeConfig.getRelationsQuery()).isEqualTo(relationsQuery);
} }
@Test @Test
public void errorThrownIfFetchToIsNull() { public void givenEmptyAttributesMapping_whenInit_thenException() {
var node = new TbGetRelatedAttributeNode(); // SETUP
var config = new TbGetRelatedAttrNodeConfiguration().defaultConfiguration(); var expectedExceptionMessage = "At least one attribute mapping should be specified!";
config.setFetchTo(null);
var nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config)); // GIVEN
config.setAttrMapping(Collections.emptyMap());
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
var exception = assertThrows(TbNodeException.class, () -> node.init(ctx, nodeConfiguration)); // WHEN
var exception = assertThrows(TbNodeException.class, () -> node.init(ctxMock, nodeConfiguration));
assertThat(exception.getMessage()).isEqualTo("FetchTo cannot be NULL!"); // THEN
verify(ctx, never()).tellSuccess(any()); assertThat(exception.getMessage()).isEqualTo(expectedExceptionMessage);
verify(ctxMock, never()).tellSuccess(any());
} }
@Test @Test
public void errorThrownIfMsgDataIsNotAnObjectAndFetchToData() { public void givenMsgDataIsNotAnJsonObjectAndFetchToData_whenOnMsg_thenException() {
// GIVEN
node.fetchTo = FetchTo.DATA; node.fetchTo = FetchTo.DATA;
node.config.setFetchTo(FetchTo.DATA); msg = TbMsg.newMsg("POST_TELEMETRY_REQUEST", DUMMY_ENTITY_ID, new TbMsgMetaData(), "[]");
msg = TbMsg.newMsg("SOME_MESSAGE_TYPE", new DeviceId(UUID.randomUUID()), new TbMsgMetaData(), "[]");
var exception = assertThrows(IllegalArgumentException.class, () -> node.onMsg(ctx, msg)); // WHEN
var exception = assertThrows(IllegalArgumentException.class, () -> node.onMsg(ctxMock, msg));
// THEN
assertThat(exception.getMessage()).isEqualTo("Message body is not an object!"); assertThat(exception.getMessage()).isEqualTo("Message body is not an object!");
verify(ctx, never()).tellSuccess(any()); verify(ctxMock, never()).tellSuccess(any());
} }
@Test @Test
public void errorThrownIfCannotLoadAttributes() { public void givenEntityThatDoesNotBelongToTheCurrentTenant_whenOnMsg_thenException() {
entityRelation.setFrom(user.getId()); // SETUP
entityRelation.setTo(customerId); var expectedExceptionMessage = "Entity with id: '" + DUMMY_ENTITY_ID +
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation))); "' specified in the configuration doesn't belong to the current tenant.";
errorThrownIfCannotLoadAttributes(user);
}
@Test // GIVEN
public void errorThrownIfCannotLoadAttributesAsync() { doThrow(new RuntimeException(expectedExceptionMessage)).when(ctxMock).checkTenantEntity(DUMMY_ENTITY_ID);
entityRelation.setFrom(user.getId()); msg = TbMsg.newMsg("POST_TELEMETRY_REQUEST", DUMMY_ENTITY_ID, new TbMsgMetaData(), "{}");
entityRelation.setTo(customerId);
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation)));
errorThrownIfCannotLoadAttributesAsync(user);
}
@Test // WHEN
public void failedChainUsedIfCustomerCannotBeFound() { var exception = assertThrows(RuntimeException.class, () -> node.onMsg(ctxMock, msg));
entityRelation.setFrom(customerId);
entityRelation.setTo(null); // THEN
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation))); assertThat(exception.getMessage()).isEqualTo(expectedExceptionMessage);
failedChainUsedIfCustomerCannotBeFound(user); verify(ctxMock, never()).tellSuccess(any());
} }
@Test @Test
public void customerAttributeAddedInMetadata() { public void givenDidNotFindEntity_whenOnMsg_thenShouldTellFailure() {
entityRelation.setFrom(customerId); // GIVEN
entityRelation.setTo(customerId); prepareMsgAndConfig(FetchTo.METADATA, false, DUMMY_ENTITY_ID);
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation)));
entityAttributeAddedInMetadata(customerId, "CUSTOMER"); when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
when(ctxMock.getRelationService()).thenReturn(relationServiceMock);
doReturn(Futures.immediateFuture(null)).when(relationServiceMock).findByQuery(any(), any());
// WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
var actualExceptionCaptor = ArgumentCaptor.forClass(Throwable.class);
verify(ctxMock, never()).tellSuccess(any());
verify(ctxMock, times(1))
.tellFailure(actualMessageCaptor.capture(), actualExceptionCaptor.capture());
var actualMessage = actualMessageCaptor.getValue();
var actualException = actualExceptionCaptor.getValue();
var expectedExceptionMessage = "Failed to find related entity to message originator using relation query specified in the configuration!";
assertEquals(msg, actualMessage);
assertEquals(expectedExceptionMessage, actualException.getMessage());
assertInstanceOf(NoSuchElementException.class, actualException);
} }
@Test @Test
public void customerAttributeAddedInData() { public void givenFetchAttributesToData_whenOnMsg_thenShouldFetchAttributesToData() {
node.fetchTo = FetchTo.DATA; // GIVEN
node.config.setFetchTo(FetchTo.DATA); var customer = new Customer(new CustomerId(UUID.randomUUID()));
var user = new User(new UserId(UUID.randomUUID()));
prepareMsgAndConfig(FetchTo.DATA, false, user.getId());
entityRelation.setFrom(customerId); entityRelation.setFrom(user.getId());
entityRelation.setTo(customerId); entityRelation.setTo(customer.getId());
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation))); entityRelation.setType(EntityRelation.CONTAINS_TYPE);
msg = TbMsg.newMsg("CUSTOMER", customerId, new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); when(ctxMock.getRelationService()).thenReturn(relationServiceMock);
doReturn(Futures.immediateFuture(List.of(entityRelation))).when(relationServiceMock).findByQuery(eq(TENANT_ID), any());
List<AttributeKvEntry> attributes = Lists.newArrayList(new BaseAttributeKvEntry(new StringDataEntry("temperature", "high"), 1L)); when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
when(ctx.getAttributesService()).thenReturn(attributesService); List<AttributeKvEntry> attributes = List.of(
when(attributesService.find(any(), eq(customerId), eq(SERVER_SCOPE), anyCollection())) new BaseAttributeKvEntry(new StringDataEntry("sourceKey1", "sourceValue1"), 1L),
new BaseAttributeKvEntry(new StringDataEntry("sourceKey2", "sourceValue2"), 2L),
new BaseAttributeKvEntry(new StringDataEntry("sourceKey3", "sourceValue3"), 3L)
);
when(attributesServiceMock.find(eq(TENANT_ID), eq(customer.getId()), eq(SERVER_SCOPE), anyList()))
.thenReturn(Futures.immediateFuture(attributes)); .thenReturn(Futures.immediateFuture(attributes));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
node.onMsg(ctx, msg); // WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class); var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctx, times(1)).tellSuccess(actualMessageCaptor.capture()); verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgData = "{\"answer\":\"high\"}"; var expectedMsgData = "{\"temp\":42," +
"\"humidity\":77," +
"\"messageBodyPattern1\":\"targetKey2\"," +
"\"messageBodyPattern2\":\"sourceKey3\"," +
"\"targetKey1\":\"sourceValue1\"," +
"\"targetKey2\":\"sourceValue2\"," +
"\"targetKey3\":\"sourceValue3\"}";
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(expectedMsgData); assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(expectedMsgData);
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(msg.getMetaData());
} }
@Test @Test
public void usersCustomerAttributesFetched() { public void givenFetchAttributesToMetaData_whenOnMsg_thenShouldFetchAttributesToMetaData() {
entityRelation.setFrom(user.getId()); // GIVEN
entityRelation.setTo(customerId); var customer = new Customer(new CustomerId(UUID.randomUUID()));
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation)));
usersCustomerAttributesFetched(user); prepareMsgAndConfig(FetchTo.METADATA, false, customer.getId());
entityRelation.setFrom(customer.getId());
entityRelation.setTo(customer.getId());
entityRelation.setType(EntityRelation.CONTAINS_TYPE);
when(ctxMock.getRelationService()).thenReturn(relationServiceMock);
doReturn(Futures.immediateFuture(List.of(entityRelation))).when(relationServiceMock).findByQuery(eq(TENANT_ID), any());
when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
List<AttributeKvEntry> attributes = List.of(
new BaseAttributeKvEntry(new StringDataEntry("sourceKey1", "sourceValue1"), 1L),
new BaseAttributeKvEntry(new StringDataEntry("sourceKey2", "sourceValue2"), 2L),
new BaseAttributeKvEntry(new StringDataEntry("sourceKey3", "sourceValue3"), 3L)
);
when(attributesServiceMock.find(eq(TENANT_ID), eq(customer.getId()), eq(SERVER_SCOPE), anyList()))
.thenReturn(Futures.immediateFuture(attributes));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
// WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgMetaData = new TbMsgMetaData(Map.of(
"metaDataPattern1", "sourceKey2",
"metaDataPattern2", "targetKey3",
"targetKey1", "sourceValue1",
"targetKey2", "sourceValue2",
"targetKey3", "sourceValue3"
));
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(msg.getData());
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(expectedMsgMetaData);
} }
@Test @Test
public void assetsCustomerAttributesFetched() { public void givenFetchTelemetryToData_whenOnMsg_thenShouldFetchTelemetryToData() {
// GIVEN
var customer = new Customer(new CustomerId(UUID.randomUUID()));
var asset = new Asset(new AssetId(UUID.randomUUID()));
prepareMsgAndConfig(FetchTo.DATA, true, asset.getId());
entityRelation.setFrom(asset.getId()); entityRelation.setFrom(asset.getId());
entityRelation.setTo(customerId); entityRelation.setTo(customer.getId());
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation))); entityRelation.setType(EntityRelation.CONTAINS_TYPE);
assetsCustomerAttributesFetched(asset);
when(ctxMock.getRelationService()).thenReturn(relationServiceMock);
doReturn(Futures.immediateFuture(List.of(entityRelation))).when(relationServiceMock).findByQuery(eq(TENANT_ID), any());
when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(ctxMock.getTimeseriesService()).thenReturn(timeseriesServiceMock);
List<TsKvEntry> timeseries = List.of(
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey1", "sourceValue1")),
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey2", "sourceValue2")),
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey3", "sourceValue3"))
);
when(timeseriesServiceMock.findLatest(eq(TENANT_ID), eq(customer.getId()), anyList()))
.thenReturn(Futures.immediateFuture(timeseries));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
// WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgData = "{\"temp\":42," +
"\"humidity\":77," +
"\"messageBodyPattern1\":\"targetKey2\"," +
"\"messageBodyPattern2\":\"sourceKey3\"," +
"\"targetKey1\":\"sourceValue1\"," +
"\"targetKey2\":\"sourceValue2\"," +
"\"targetKey3\":\"sourceValue3\"}";
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(expectedMsgData);
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(msg.getMetaData());
} }
@Test @Test
public void deviceCustomerAttributesFetched() { public void givenFetchTelemetryToMetaData_whenOnMsg_thenShouldFetchTelemetryToMetaData() {
// GIVEN
var customer = new Customer(new CustomerId(UUID.randomUUID()));
var device = new Device(new DeviceId(UUID.randomUUID()));
prepareMsgAndConfig(FetchTo.METADATA, true, device.getId());
entityRelation.setFrom(device.getId()); entityRelation.setFrom(device.getId());
entityRelation.setTo(customerId); entityRelation.setTo(customer.getId());
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation))); entityRelation.setType(EntityRelation.CONTAINS_TYPE);
deviceCustomerAttributesFetched(device);
when(ctxMock.getRelationService()).thenReturn(relationServiceMock);
doReturn(Futures.immediateFuture(List.of(entityRelation))).when(relationServiceMock).findByQuery(eq(TENANT_ID), any());
when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(ctxMock.getTimeseriesService()).thenReturn(timeseriesServiceMock);
List<TsKvEntry> timeseries = List.of(
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey1", "sourceValue1")),
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey2", "sourceValue2")),
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey3", "sourceValue3"))
);
when(timeseriesServiceMock.findLatest(eq(TENANT_ID), eq(customer.getId()), anyList()))
.thenReturn(Futures.immediateFuture(timeseries));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
// WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgMetaData = new TbMsgMetaData(Map.of(
"metaDataPattern1", "sourceKey2",
"metaDataPattern2", "targetKey3",
"targetKey1", "sourceValue1",
"targetKey2", "sourceValue2",
"targetKey3", "sourceValue3"
));
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(msg.getData());
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(expectedMsgMetaData);
} }
@Test private void prepareMsgAndConfig(FetchTo fetchTo, boolean isTelemetry, EntityId entityId) {
public void deviceCustomerTelemetryFetched() throws TbNodeException { config.setAttrMapping(Map.of(
entityRelation.setFrom(device.getId()); "sourceKey1", "targetKey1",
entityRelation.setTo(customerId); "${metaDataPattern1}", "$[messageBodyPattern1]",
when(relationService.findByQuery(any(), any())).thenReturn(Futures.immediateFuture(List.of(entityRelation))); "$[messageBodyPattern2]", "${metaDataPattern2}"));
deviceCustomerTelemetryFetched(device); config.setTelemetry(isTelemetry);
config.setFetchTo(fetchTo);
node.config = config;
node.fetchTo = fetchTo;
var msgMetaData = new TbMsgMetaData();
msgMetaData.putValue("metaDataPattern1", "sourceKey2");
msgMetaData.putValue("metaDataPattern2", "targetKey3");
var msgData = "{\"temp\":42,\"humidity\":77,\"messageBodyPattern1\":\"targetKey2\",\"messageBodyPattern2\":\"sourceKey3\"}";
msg = TbMsg.newMsg("POST_TELEMETRY_REQUEST", entityId, msgMetaData, msgData);
} }
} }

370
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/metadata/TbGetTenantAttributeNodeTest.java

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2023 The Thingsboard Authors * Copyright © 2016-2023 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -15,163 +15,357 @@
*/ */
package org.thingsboard.rule.engine.metadata; package org.thingsboard.rule.engine.metadata;
import com.google.common.collect.Lists;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import org.junit.Before; import com.google.common.util.concurrent.ListenableFuture;
import org.junit.Test; import org.jetbrains.annotations.NotNull;
import org.junit.runner.RunWith; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor; import org.mockito.ArgumentCaptor;
import org.mockito.junit.MockitoJUnitRunner; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.common.util.ListeningExecutor;
import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNodeConfiguration; import org.thingsboard.rule.engine.api.TbNodeConfiguration;
import org.thingsboard.rule.engine.api.TbNodeException; import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.AssetId;
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.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.common.data.kv.AttributeKvEntry;
import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry;
import org.thingsboard.server.common.data.kv.BasicTsKvEntry;
import org.thingsboard.server.common.data.kv.StringDataEntry; import org.thingsboard.server.common.data.kv.StringDataEntry;
import org.thingsboard.server.common.data.kv.TsKvEntry;
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.attributes.AttributesService;
import org.thingsboard.server.dao.timeseries.TimeseriesService;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Callable;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyCollection; import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE; import static org.thingsboard.server.common.data.DataConstants.SERVER_SCOPE;
@RunWith(MockitoJUnitRunner.class) @ExtendWith(MockitoExtension.class)
public class TbGetTenantAttributeNodeTest extends TbAbstractAttributeNodeTest { public class TbGetTenantAttributeNodeTest {
User user = new User();
Asset asset = new Asset(); private static final EntityId DUMMY_ENTITY_ID = new DeviceId(UUID.randomUUID());
Device device = new Device(); private static final TenantId TENANT_ID = new TenantId(UUID.randomUUID());
private static final ListeningExecutor DB_EXECUTOR = new ListeningExecutor() {
@Override
public <T> ListenableFuture<T> executeAsync(Callable<T> task) {
try {
return Futures.immediateFuture(task.call());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void execute(@NotNull Runnable command) {
command.run();
}
};
@Mock
private TbContext ctxMock;
@Mock
private AttributesService attributesServiceMock;
@Mock
private TimeseriesService timeseriesServiceMock;
private TbGetTenantAttributeNode node;
private TbGetEntityAttrNodeConfiguration config;
private TbNodeConfiguration nodeConfiguration;
private TbMsg msg;
@BeforeEach
public void setUp() {
node = new TbGetTenantAttributeNode();
config = new TbGetEntityAttrNodeConfiguration().defaultConfiguration();
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
}
@Before @Test
public void initDataForTests() throws TbNodeException { public void givenConfigWithNullFetchTo_whenInit_thenException() {
init(new TbGetTenantAttributeNode()); // GIVEN
config.setFetchTo(null);
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
user.setTenantId(tenantId); // WHEN
user.setId(new UserId(UUID.randomUUID())); var exception = assertThrows(TbNodeException.class, () -> node.init(ctxMock, nodeConfiguration));
asset.setTenantId(tenantId); // THEN
asset.setId(new AssetId(UUID.randomUUID())); assertThat(exception.getMessage()).isEqualTo("FetchTo cannot be null!");
verify(ctxMock, never()).tellSuccess(any());
}
device.setTenantId(tenantId); @Test
device.setId(new DeviceId(UUID.randomUUID())); public void givenDefaultConfig_whenInit_thenOK() throws TbNodeException {
// GIVEN
when(ctx.getTenantId()).thenReturn(tenantId); // WHEN
} node.init(ctxMock, nodeConfiguration);
@Override // THEN
protected TbAbstractGetEntityAttrNode getEmptyNode() { assertThat(node.config).isEqualTo(config);
return new TbGetTenantAttributeNode(); assertThat(config.getAttrMapping()).isEqualTo(Map.of("alarmThreshold", "threshold"));
assertThat(config.isTelemetry()).isEqualTo(false);
assertThat(node.fetchTo).isEqualTo(FetchTo.METADATA);
} }
@Override @Test
EntityId getEntityId() { public void givenCustomConfig_whenInit_thenOK() throws TbNodeException {
return tenantId; // GIVEN
config.setAttrMapping(Map.of(
"sourceAttr1", "targetKey1",
"sourceAttr2", "targetKey2",
"sourceAttr3", "targetKey3"));
config.setTelemetry(true);
config.setFetchTo(FetchTo.DATA);
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
// WHEN
node.init(ctxMock, nodeConfiguration);
// THEN
assertThat(node.config).isEqualTo(config);
assertThat(config.getAttrMapping()).isEqualTo(Map.of(
"sourceAttr1", "targetKey1",
"sourceAttr2", "targetKey2",
"sourceAttr3", "targetKey3"));
assertThat(config.isTelemetry()).isEqualTo(true);
assertThat(node.fetchTo).isEqualTo(FetchTo.DATA);
} }
@Test @Test
public void errorThrownIfFetchToIsNull() { public void givenEmptyAttributesMapping_whenInit_thenException() {
var node = new TbGetTenantAttributeNode(); // SETUP
var config = new TbGetEntityAttrNodeConfiguration().defaultConfiguration(); var expectedExceptionMessage = "At least one attribute mapping should be specified!";
config.setFetchTo(null);
var nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
var exception = assertThrows(TbNodeException.class, () -> node.init(ctx, nodeConfiguration)); // GIVEN
config.setAttrMapping(Collections.emptyMap());
nodeConfiguration = new TbNodeConfiguration(JacksonUtil.valueToTree(config));
assertThat(exception.getMessage()).isEqualTo("FetchTo cannot be NULL!"); // WHEN
verify(ctx, never()).tellSuccess(any()); var exception = assertThrows(TbNodeException.class, () -> node.init(ctxMock, nodeConfiguration));
// THEN
assertThat(exception.getMessage()).isEqualTo(expectedExceptionMessage);
verify(ctxMock, never()).tellSuccess(any());
} }
@Test @Test
public void errorThrownIfMsgDataIsNotAnObjectAndFetchToData() { public void givenMsgDataIsNotAnJsonObjectAndFetchToData_whenOnMsg_thenException() {
// GIVEN
node.fetchTo = FetchTo.DATA; node.fetchTo = FetchTo.DATA;
node.config.setFetchTo(FetchTo.DATA); msg = TbMsg.newMsg("POST_TELEMETRY_REQUEST", DUMMY_ENTITY_ID, new TbMsgMetaData(), "[]");
msg = TbMsg.newMsg("SOME_MESSAGE_TYPE", new TenantId(UUID.randomUUID()), new TbMsgMetaData(), "[]");
var exception = assertThrows(IllegalArgumentException.class, () -> node.onMsg(ctx, msg)); // WHEN
var exception = assertThrows(IllegalArgumentException.class, () -> node.onMsg(ctxMock, msg));
// THEN
assertThat(exception.getMessage()).isEqualTo("Message body is not an object!"); assertThat(exception.getMessage()).isEqualTo("Message body is not an object!");
verify(ctx, never()).tellSuccess(any()); verify(ctxMock, never()).tellSuccess(any());
} }
@Test @Test
public void errorThrownIfCannotLoadAttributes() { public void givenEntityThatDoesNotBelongToTheCurrentTenant_whenOnMsg_thenException() {
errorThrownIfCannotLoadAttributes(user); // SETUP
} var expectedExceptionMessage = "Entity with id: '" + DUMMY_ENTITY_ID +
"' specified in the configuration doesn't belong to the current tenant.";
@Test // GIVEN
public void errorThrownIfCannotLoadAttributesAsync() { doThrow(new RuntimeException(expectedExceptionMessage)).when(ctxMock).checkTenantEntity(DUMMY_ENTITY_ID);
errorThrownIfCannotLoadAttributesAsync(user); msg = TbMsg.newMsg("POST_TELEMETRY_REQUEST", DUMMY_ENTITY_ID, new TbMsgMetaData(), "{}");
}
@Test // WHEN
public void failedChainUsedIfTenantIdFromCtxCannotBeFound() { var exception = assertThrows(RuntimeException.class, () -> node.onMsg(ctxMock, msg));
when(ctx.getTenantId()).thenReturn(null);
failedChainUsedIfCustomerCannotBeFound(user);
}
@Test // THEN
public void customerAttributeAddedInMetadata() { assertThat(exception.getMessage()).isEqualTo(expectedExceptionMessage);
entityAttributeAddedInMetadata(tenantId, "TENANT"); verify(ctxMock, never()).tellSuccess(any());
} }
@Test @Test
public void customerAttributeAddedInData() { public void givenFetchAttributesToData_whenOnMsg_thenShouldFetchAttributesToData() {
node.fetchTo = FetchTo.DATA; // GIVEN
node.config.setFetchTo(FetchTo.DATA); var deviceId = new DeviceId(UUID.randomUUID());
msg = TbMsg.newMsg("TENANT", tenantId, new TbMsgMetaData(metaData), TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); prepareMsgAndConfig(FetchTo.DATA, false, deviceId);
List<AttributeKvEntry> attributes = Lists.newArrayList(new BaseAttributeKvEntry(new StringDataEntry("temperature", "high"), 1L)); when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
when(ctx.getAttributesService()).thenReturn(attributesService); List<AttributeKvEntry> attributes = List.of(
when(attributesService.find(any(), eq(tenantId), eq(SERVER_SCOPE), anyCollection())) new BaseAttributeKvEntry(new StringDataEntry("sourceKey1", "sourceValue1"), 1L),
new BaseAttributeKvEntry(new StringDataEntry("sourceKey2", "sourceValue2"), 2L),
new BaseAttributeKvEntry(new StringDataEntry("sourceKey3", "sourceValue3"), 3L)
);
when(attributesServiceMock.find(eq(TENANT_ID), eq(TENANT_ID), eq(SERVER_SCOPE), anyList()))
.thenReturn(Futures.immediateFuture(attributes)); .thenReturn(Futures.immediateFuture(attributes));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
node.onMsg(ctx, msg); // WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class); var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctx, times(1)).tellSuccess(actualMessageCaptor.capture()); verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgData = "{\"answer\":\"high\"}"; var expectedMsgData = "{\"temp\":42," +
"\"humidity\":77," +
"\"messageBodyPattern1\":\"targetKey2\"," +
"\"messageBodyPattern2\":\"sourceKey3\"," +
"\"targetKey1\":\"sourceValue1\"," +
"\"targetKey2\":\"sourceValue2\"," +
"\"targetKey3\":\"sourceValue3\"}";
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(expectedMsgData); assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(expectedMsgData);
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(msg.getMetaData());
} }
@Test @Test
public void usersCustomerAttributesFetched() { public void givenFetchAttributesToMetaData_whenOnMsg_thenShouldFetchAttributesToMetaData() {
usersCustomerAttributesFetched(user); // GIVEN
prepareMsgAndConfig(FetchTo.METADATA, false, TENANT_ID);
when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(ctxMock.getAttributesService()).thenReturn(attributesServiceMock);
List<AttributeKvEntry> attributes = List.of(
new BaseAttributeKvEntry(new StringDataEntry("sourceKey1", "sourceValue1"), 1L),
new BaseAttributeKvEntry(new StringDataEntry("sourceKey2", "sourceValue2"), 2L),
new BaseAttributeKvEntry(new StringDataEntry("sourceKey3", "sourceValue3"), 3L)
);
when(attributesServiceMock.find(eq(TENANT_ID), eq(TENANT_ID), eq(SERVER_SCOPE), anyList()))
.thenReturn(Futures.immediateFuture(attributes));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
// WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgMetaData = new TbMsgMetaData(Map.of(
"metaDataPattern1", "sourceKey2",
"metaDataPattern2", "targetKey3",
"targetKey1", "sourceValue1",
"targetKey2", "sourceValue2",
"targetKey3", "sourceValue3"
));
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(msg.getData());
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(expectedMsgMetaData);
} }
@Test @Test
public void assetsCustomerAttributesFetched() { public void givenFetchTelemetryToData_whenOnMsg_thenShouldFetchTelemetryToData() {
assetsCustomerAttributesFetched(asset); // GIVEN
var customerId = new CustomerId(UUID.randomUUID());
prepareMsgAndConfig(FetchTo.DATA, true, customerId);
when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(ctxMock.getTimeseriesService()).thenReturn(timeseriesServiceMock);
List<TsKvEntry> timeseries = List.of(
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey1", "sourceValue1")),
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey2", "sourceValue2")),
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey3", "sourceValue3"))
);
when(timeseriesServiceMock.findLatest(eq(TENANT_ID), eq(TENANT_ID), anyList()))
.thenReturn(Futures.immediateFuture(timeseries));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
// WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgData = "{\"temp\":42," +
"\"humidity\":77," +
"\"messageBodyPattern1\":\"targetKey2\"," +
"\"messageBodyPattern2\":\"sourceKey3\"," +
"\"targetKey1\":\"sourceValue1\"," +
"\"targetKey2\":\"sourceValue2\"," +
"\"targetKey3\":\"sourceValue3\"}";
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(expectedMsgData);
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(msg.getMetaData());
} }
@Test @Test
public void deviceCustomerAttributesFetched() { public void givenFetchTelemetryToMetaData_whenOnMsg_thenShouldFetchTelemetryToMetaData() {
deviceCustomerAttributesFetched(device); // GIVEN
var ruleChainId = new RuleChainId(UUID.randomUUID());
prepareMsgAndConfig(FetchTo.METADATA, true, ruleChainId);
when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
when(ctxMock.getTimeseriesService()).thenReturn(timeseriesServiceMock);
List<TsKvEntry> timeseries = List.of(
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey1", "sourceValue1")),
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey2", "sourceValue2")),
new BasicTsKvEntry(1L, new StringDataEntry("sourceKey3", "sourceValue3"))
);
when(timeseriesServiceMock.findLatest(eq(TENANT_ID), eq(TENANT_ID), anyList()))
.thenReturn(Futures.immediateFuture(timeseries));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
// WHEN
node.onMsg(ctxMock, msg);
// THEN
var actualMessageCaptor = ArgumentCaptor.forClass(TbMsg.class);
verify(ctxMock, times(1)).tellSuccess(actualMessageCaptor.capture());
verify(ctxMock, never()).tellFailure(any(), any());
var expectedMsgMetaData = new TbMsgMetaData(Map.of(
"metaDataPattern1", "sourceKey2",
"metaDataPattern2", "targetKey3",
"targetKey1", "sourceValue1",
"targetKey2", "sourceValue2",
"targetKey3", "sourceValue3"
));
assertThat(actualMessageCaptor.getValue().getData()).isEqualTo(msg.getData());
assertThat(actualMessageCaptor.getValue().getMetaData()).isEqualTo(expectedMsgMetaData);
} }
@Test private void prepareMsgAndConfig(FetchTo fetchTo, boolean isTelemetry, EntityId entityId) {
public void deviceCustomerTelemetryFetched() throws TbNodeException { config.setAttrMapping(Map.of(
deviceCustomerTelemetryFetched(device); "sourceKey1", "targetKey1",
"${metaDataPattern1}", "$[messageBodyPattern1]",
"$[messageBodyPattern2]", "${metaDataPattern2}"));
config.setTelemetry(isTelemetry);
config.setFetchTo(fetchTo);
node.config = config;
node.fetchTo = fetchTo;
var msgMetaData = new TbMsgMetaData();
msgMetaData.putValue("metaDataPattern1", "sourceKey2");
msgMetaData.putValue("metaDataPattern2", "targetKey3");
var msgData = "{\"temp\":42,\"humidity\":77,\"messageBodyPattern1\":\"targetKey2\",\"messageBodyPattern2\":\"sourceKey3\"}";
msg = TbMsg.newMsg("POST_TELEMETRY_REQUEST", entityId, msgMetaData, msgData);
} }
} }

134
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/util/EntitiesCustomerIdAsyncLoaderTest.java

@ -0,0 +1,134 @@
package org.thingsboard.rule.engine.util;
import com.google.common.util.concurrent.Futures;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNodeException;
import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.AssetId;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EntityIdFactory;
import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.dao.asset.AssetService;
import org.thingsboard.server.dao.device.DeviceService;
import org.thingsboard.server.dao.user.UserService;
import java.util.EnumSet;
import java.util.UUID;
import java.util.concurrent.ExecutionException;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
public class EntitiesCustomerIdAsyncLoaderTest {
private static final EnumSet<EntityType> SUPPORTED_ENTITY_TYPES = EnumSet.of(
EntityType.CUSTOMER,
EntityType.USER,
EntityType.ASSET,
EntityType.DEVICE
);
@Mock
private TbContext ctxMock;
@Mock
private UserService userServiceMock;
@Mock
private AssetService assetServiceMock;
@Mock
private DeviceService deviceServiceMock;
@Test
public void givenCustomerEntityType_whenFindEntityIdAsync_thenOK() throws ExecutionException, InterruptedException {
// GIVEN
var customer = new Customer(new CustomerId(UUID.randomUUID()));
// WHEN
var actualCustomerId = EntitiesCustomerIdAsyncLoader.findEntityIdAsync(ctxMock, customer.getId()).get();
// THEN
Assertions.assertEquals(customer.getId(), actualCustomerId);
}
@Test
public void givenUserEntityType_whenFindEntityIdAsync_thenOK() throws ExecutionException, InterruptedException {
// GIVEN
var user = new User(new UserId(UUID.randomUUID()));
var expectedCustomerId = new CustomerId(UUID.randomUUID());
user.setCustomerId(expectedCustomerId);
when(ctxMock.getUserService()).thenReturn(userServiceMock);
doReturn(Futures.immediateFuture(user)).when(userServiceMock).findUserByIdAsync(any(), any());
// WHEN
var actualCustomerId = EntitiesCustomerIdAsyncLoader.findEntityIdAsync(ctxMock, user.getId()).get();
// THEN
Assertions.assertEquals(expectedCustomerId, actualCustomerId);
}
@Test
public void givenAssetEntityType_whenFindEntityIdAsync_thenOK() throws ExecutionException, InterruptedException {
// GIVEN
var asset = new Asset(new AssetId(UUID.randomUUID()));
var expectedCustomerId = new CustomerId(UUID.randomUUID());
asset.setCustomerId(expectedCustomerId);
when(ctxMock.getAssetService()).thenReturn(assetServiceMock);
doReturn(Futures.immediateFuture(asset)).when(assetServiceMock).findAssetByIdAsync(any(), any());
// WHEN
var actualCustomerId = EntitiesCustomerIdAsyncLoader.findEntityIdAsync(ctxMock, asset.getId()).get();
// THEN
Assertions.assertEquals(expectedCustomerId, actualCustomerId);
}
@Test
public void givenDeviceEntityType_whenFindEntityIdAsync_thenOK() throws ExecutionException, InterruptedException {
// GIVEN
var device = new Device(new DeviceId(UUID.randomUUID()));
var expectedCustomerId = new CustomerId(UUID.randomUUID());
device.setCustomerId(expectedCustomerId);
when(ctxMock.getDeviceService()).thenReturn(deviceServiceMock);
doReturn(Futures.immediateFuture(device)).when(deviceServiceMock).findDeviceByIdAsync(any(), any());
// WHEN
var actualCustomerId = EntitiesCustomerIdAsyncLoader.findEntityIdAsync(ctxMock, device.getId()).get();
// THEN
Assertions.assertEquals(expectedCustomerId, actualCustomerId);
}
@Test
public void givenUnsupportedEntityTypes_whenFindEntityIdAsync_thenException() {
for (var entityType : EntityType.values()) {
if (!SUPPORTED_ENTITY_TYPES.contains(entityType)) {
var entityId = EntityIdFactory.getByTypeAndUuid(entityType, UUID.randomUUID());
var expectedExceptionMsg = "org.thingsboard.rule.engine.api.TbNodeException: Unexpected originator EntityType: " + entityType;
var exception = assertThrows(ExecutionException.class,
() -> EntitiesCustomerIdAsyncLoader.findEntityIdAsync(ctxMock, entityId).get());
assertInstanceOf(TbNodeException.class, exception.getCause());
assertEquals(expectedExceptionMsg, exception.getMessage());
}
}
}
}

44
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/util/EntitiesFieldsAsyncLoaderTest.java

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2023 The Thingsboard Authors * Copyright © 2016-2023 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -16,12 +16,15 @@
package org.thingsboard.rule.engine.util; package org.thingsboard.rule.engine.util;
import com.google.common.util.concurrent.Futures; import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.thingsboard.common.util.ListeningExecutor;
import org.thingsboard.rule.engine.api.RuleEngineAlarmService; import org.thingsboard.rule.engine.api.RuleEngineAlarmService;
import org.thingsboard.rule.engine.api.TbContext; import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.rule.engine.api.TbNodeException; import org.thingsboard.rule.engine.api.TbNodeException;
@ -56,7 +59,9 @@ import org.thingsboard.server.dao.tenant.TenantService;
import org.thingsboard.server.dao.user.UserService; import org.thingsboard.server.dao.user.UserService;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.NoSuchElementException;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException; import java.util.concurrent.ExecutionException;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
@ -69,6 +74,22 @@ import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class) @ExtendWith(MockitoExtension.class)
public class EntitiesFieldsAsyncLoaderTest { public class EntitiesFieldsAsyncLoaderTest {
private static final ListeningExecutor DB_EXECUTOR = new ListeningExecutor() {
@Override
public <T> ListenableFuture<T> executeAsync(Callable<T> task) {
try {
return Futures.immediateFuture(task.call());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void execute(@NotNull Runnable command) {
command.run();
}
};
private static EnumSet<EntityType> SUPPORTED_ENTITY_TYPES; private static EnumSet<EntityType> SUPPORTED_ENTITY_TYPES;
private static UUID RANDOM_UUID; private static UUID RANDOM_UUID;
private static TenantId TENANT_ID; private static TenantId TENANT_ID;
@ -148,12 +169,12 @@ public class EntitiesFieldsAsyncLoaderTest {
initMocks(entityType, true); initMocks(entityType, true);
when(ctxMock.getTenantId()).thenReturn(TENANT_ID); when(ctxMock.getTenantId()).thenReturn(TENANT_ID);
var expectedExceptionMsg = "org.thingsboard.rule.engine.api.TbNodeException: Entity not found!"; var expectedExceptionMsg = "java.util.NoSuchElementException: Entity not found!";
var exception = assertThrows(ExecutionException.class, var exception = assertThrows(ExecutionException.class,
() -> EntitiesFieldsAsyncLoader.findAsync(ctxMock, entityId).get()); () -> EntitiesFieldsAsyncLoader.findAsync(ctxMock, entityId).get());
assertInstanceOf(TbNodeException.class, exception.getCause()); assertInstanceOf(NoSuchElementException.class, exception.getCause());
assertThat(exception.getMessage()).isEqualTo(expectedExceptionMsg); assertThat(exception.getMessage()).isEqualTo(expectedExceptionMsg);
} }
} }
@ -163,6 +184,7 @@ public class EntitiesFieldsAsyncLoaderTest {
case TENANT: case TENANT:
var tenant = Futures.immediateFuture(entityDoesNotExist ? null : new Tenant(new TenantId(RANDOM_UUID))); var tenant = Futures.immediateFuture(entityDoesNotExist ? null : new Tenant(new TenantId(RANDOM_UUID)));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
when(ctxMock.getTenantService()).thenReturn(tenantServiceMock); when(ctxMock.getTenantService()).thenReturn(tenantServiceMock);
doReturn(tenant).when(tenantServiceMock).findTenantByIdAsync(eq(TENANT_ID), any()); doReturn(tenant).when(tenantServiceMock).findTenantByIdAsync(eq(TENANT_ID), any());
@ -170,6 +192,7 @@ public class EntitiesFieldsAsyncLoaderTest {
case CUSTOMER: case CUSTOMER:
var customer = Futures.immediateFuture(entityDoesNotExist ? null : new Customer(new CustomerId(RANDOM_UUID))); var customer = Futures.immediateFuture(entityDoesNotExist ? null : new Customer(new CustomerId(RANDOM_UUID)));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
when(ctxMock.getCustomerService()).thenReturn(customerServiceMock); when(ctxMock.getCustomerService()).thenReturn(customerServiceMock);
doReturn(customer).when(customerServiceMock).findCustomerByIdAsync(eq(TENANT_ID), any()); doReturn(customer).when(customerServiceMock).findCustomerByIdAsync(eq(TENANT_ID), any());
@ -177,6 +200,7 @@ public class EntitiesFieldsAsyncLoaderTest {
case USER: case USER:
var user = Futures.immediateFuture(entityDoesNotExist ? null : new User(new UserId(RANDOM_UUID))); var user = Futures.immediateFuture(entityDoesNotExist ? null : new User(new UserId(RANDOM_UUID)));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
when(ctxMock.getUserService()).thenReturn(userServiceMock); when(ctxMock.getUserService()).thenReturn(userServiceMock);
doReturn(user).when(userServiceMock).findUserByIdAsync(eq(TENANT_ID), any()); doReturn(user).when(userServiceMock).findUserByIdAsync(eq(TENANT_ID), any());
@ -184,6 +208,7 @@ public class EntitiesFieldsAsyncLoaderTest {
case ASSET: case ASSET:
var asset = Futures.immediateFuture(entityDoesNotExist ? null : new Asset(new AssetId(RANDOM_UUID))); var asset = Futures.immediateFuture(entityDoesNotExist ? null : new Asset(new AssetId(RANDOM_UUID)));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
when(ctxMock.getAssetService()).thenReturn(assetServiceMock); when(ctxMock.getAssetService()).thenReturn(assetServiceMock);
doReturn(asset).when(assetServiceMock).findAssetByIdAsync(eq(TENANT_ID), any()); doReturn(asset).when(assetServiceMock).findAssetByIdAsync(eq(TENANT_ID), any());
@ -191,6 +216,7 @@ public class EntitiesFieldsAsyncLoaderTest {
case DEVICE: case DEVICE:
var device = Futures.immediateFuture(entityDoesNotExist ? null : new Device(new DeviceId(RANDOM_UUID))); var device = Futures.immediateFuture(entityDoesNotExist ? null : new Device(new DeviceId(RANDOM_UUID)));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
when(ctxMock.getDeviceService()).thenReturn(deviceServiceMock); when(ctxMock.getDeviceService()).thenReturn(deviceServiceMock);
doReturn(device).when(deviceServiceMock).findDeviceByIdAsync(eq(TENANT_ID), any()); doReturn(device).when(deviceServiceMock).findDeviceByIdAsync(eq(TENANT_ID), any());
@ -198,6 +224,7 @@ public class EntitiesFieldsAsyncLoaderTest {
case ALARM: case ALARM:
var alarm = Futures.immediateFuture(entityDoesNotExist ? null : new Alarm(new AlarmId(RANDOM_UUID))); var alarm = Futures.immediateFuture(entityDoesNotExist ? null : new Alarm(new AlarmId(RANDOM_UUID)));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
when(ctxMock.getAlarmService()).thenReturn(ruleEngineAlarmServiceMock); when(ctxMock.getAlarmService()).thenReturn(ruleEngineAlarmServiceMock);
doReturn(alarm).when(ruleEngineAlarmServiceMock).findAlarmByIdAsync(eq(TENANT_ID), any()); doReturn(alarm).when(ruleEngineAlarmServiceMock).findAlarmByIdAsync(eq(TENANT_ID), any());
@ -205,6 +232,7 @@ public class EntitiesFieldsAsyncLoaderTest {
case RULE_CHAIN: case RULE_CHAIN:
var ruleChain = Futures.immediateFuture(entityDoesNotExist ? null : new RuleChain(new RuleChainId(RANDOM_UUID))); var ruleChain = Futures.immediateFuture(entityDoesNotExist ? null : new RuleChain(new RuleChainId(RANDOM_UUID)));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
when(ctxMock.getRuleChainService()).thenReturn(ruleChainServiceMock); when(ctxMock.getRuleChainService()).thenReturn(ruleChainServiceMock);
doReturn(ruleChain).when(ruleChainServiceMock).findRuleChainByIdAsync(eq(TENANT_ID), any()); doReturn(ruleChain).when(ruleChainServiceMock).findRuleChainByIdAsync(eq(TENANT_ID), any());
@ -212,6 +240,7 @@ public class EntitiesFieldsAsyncLoaderTest {
case ENTITY_VIEW: case ENTITY_VIEW:
var entityView = Futures.immediateFuture(entityDoesNotExist ? null : new EntityView(new EntityViewId(RANDOM_UUID))); var entityView = Futures.immediateFuture(entityDoesNotExist ? null : new EntityView(new EntityViewId(RANDOM_UUID)));
when(ctxMock.getDbCallbackExecutor()).thenReturn(DB_EXECUTOR);
when(ctxMock.getEntityViewService()).thenReturn(entityViewServiceMock); when(ctxMock.getEntityViewService()).thenReturn(entityViewServiceMock);
doReturn(entityView).when(entityViewServiceMock).findEntityViewByIdAsync(eq(TENANT_ID), any()); doReturn(entityView).when(entityViewServiceMock).findEntityViewByIdAsync(eq(TENANT_ID), any());
@ -243,4 +272,5 @@ public class EntitiesFieldsAsyncLoaderTest {
throw new RuntimeException("Unexpected EntityType: " + entityId.getEntityType()); throw new RuntimeException("Unexpected EntityType: " + entityId.getEntityType());
} }
} }
} }

12
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/util/TenantIdLoaderTest.java

@ -1,12 +1,12 @@
/** /**
* Copyright © 2016-2023 The Thingsboard Authors * Copyright © 2016-2023 The Thingsboard Authors
* * <p>
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
* You may obtain a copy of the License at * You may obtain a copy of the License at
* * <p>
* http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
* * <p>
* Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
@ -69,7 +69,6 @@ import org.thingsboard.server.dao.edge.EdgeService;
import org.thingsboard.server.dao.entityview.EntityViewService; import org.thingsboard.server.dao.entityview.EntityViewService;
import org.thingsboard.server.dao.notification.NotificationRequestService; import org.thingsboard.server.dao.notification.NotificationRequestService;
import org.thingsboard.server.dao.notification.NotificationRuleService; import org.thingsboard.server.dao.notification.NotificationRuleService;
import org.thingsboard.server.dao.notification.NotificationService;
import org.thingsboard.server.dao.notification.NotificationTargetService; import org.thingsboard.server.dao.notification.NotificationTargetService;
import org.thingsboard.server.dao.notification.NotificationTemplateService; import org.thingsboard.server.dao.notification.NotificationTemplateService;
import org.thingsboard.server.dao.ota.OtaPackageService; import org.thingsboard.server.dao.ota.OtaPackageService;
@ -135,8 +134,6 @@ public class TenantIdLoaderTest {
@Mock @Mock
private NotificationRequestService notificationRequestService; private NotificationRequestService notificationRequestService;
@Mock @Mock
private NotificationService notificationService;
@Mock
private NotificationRuleService notificationRuleService; private NotificationRuleService notificationRuleService;
private TenantId tenantId; private TenantId tenantId;
@ -393,4 +390,5 @@ public class TenantIdLoaderTest {
public void test_findEntityIdAsync_other_tenant() { public void test_findEntityIdAsync_other_tenant() {
checkTenant(new TenantId(UUID.randomUUID()), false); checkTenant(new TenantId(UUID.randomUUID()), false);
} }
} }

Loading…
Cancel
Save