Commit e5ff6aba163ab5dd48700c32eb225e06c37cf8f6
Committed by
Andrew Shvayka
1 parent
d22209ed
added create relation rule node test
Showing
1 changed file
with
184 additions
and
0 deletions
1 | +/** | ||
2 | + * Copyright © 2016-2021 The Thingsboard Authors | ||
3 | + * | ||
4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | + * you may not use this file except in compliance with the License. | ||
6 | + * You may obtain a copy of the License at | ||
7 | + * | ||
8 | + * http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | + * | ||
10 | + * Unless required by applicable law or agreed to in writing, software | ||
11 | + * distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | + * See the License for the specific language governing permissions and | ||
14 | + * limitations under the License. | ||
15 | + */ | ||
16 | +package org.thingsboard.rule.engine.action; | ||
17 | + | ||
18 | +import com.datastax.oss.driver.api.core.uuid.Uuids; | ||
19 | +import com.fasterxml.jackson.databind.ObjectMapper; | ||
20 | +import com.google.common.util.concurrent.Futures; | ||
21 | +import com.google.common.util.concurrent.ListenableFuture; | ||
22 | +import org.junit.Before; | ||
23 | +import org.junit.Test; | ||
24 | +import org.junit.runner.RunWith; | ||
25 | +import org.mockito.Mock; | ||
26 | +import org.mockito.junit.MockitoJUnitRunner; | ||
27 | +import org.thingsboard.common.util.ListeningExecutor; | ||
28 | +import org.thingsboard.rule.engine.api.TbContext; | ||
29 | +import org.thingsboard.rule.engine.api.TbNodeConfiguration; | ||
30 | +import org.thingsboard.rule.engine.api.TbNodeException; | ||
31 | +import org.thingsboard.rule.engine.api.TbRelationTypes; | ||
32 | +import org.thingsboard.server.common.data.DataConstants; | ||
33 | +import org.thingsboard.server.common.data.asset.Asset; | ||
34 | +import org.thingsboard.server.common.data.id.AssetId; | ||
35 | +import org.thingsboard.server.common.data.id.DeviceId; | ||
36 | +import org.thingsboard.server.common.data.id.RuleChainId; | ||
37 | +import org.thingsboard.server.common.data.id.RuleNodeId; | ||
38 | +import org.thingsboard.server.common.data.relation.EntityRelation; | ||
39 | +import org.thingsboard.server.common.data.relation.EntitySearchDirection; | ||
40 | +import org.thingsboard.server.common.data.relation.RelationTypeGroup; | ||
41 | +import org.thingsboard.server.common.msg.TbMsg; | ||
42 | +import org.thingsboard.server.common.msg.TbMsgDataType; | ||
43 | +import org.thingsboard.server.common.msg.TbMsgMetaData; | ||
44 | +import org.thingsboard.server.dao.asset.AssetService; | ||
45 | +import org.thingsboard.server.dao.relation.RelationService; | ||
46 | + | ||
47 | +import java.util.Collections; | ||
48 | +import java.util.concurrent.Callable; | ||
49 | + | ||
50 | +import static org.mockito.ArgumentMatchers.any; | ||
51 | +import static org.mockito.ArgumentMatchers.eq; | ||
52 | +import static org.mockito.Mockito.verify; | ||
53 | +import static org.mockito.Mockito.when; | ||
54 | + | ||
55 | +@RunWith(MockitoJUnitRunner.class) | ||
56 | +public class TbCreateRelationNodeTest { | ||
57 | + | ||
58 | + private static final String RELATION_TYPE_CONTAINS = "Contains"; | ||
59 | + | ||
60 | + private TbCreateRelationNode node; | ||
61 | + | ||
62 | + @Mock | ||
63 | + private TbContext ctx; | ||
64 | + @Mock | ||
65 | + private AssetService assetService; | ||
66 | + @Mock | ||
67 | + private RelationService relationService; | ||
68 | + | ||
69 | + private TbMsg msg; | ||
70 | + | ||
71 | + private RuleChainId ruleChainId = new RuleChainId(Uuids.timeBased()); | ||
72 | + private RuleNodeId ruleNodeId = new RuleNodeId(Uuids.timeBased()); | ||
73 | + | ||
74 | + private ListeningExecutor dbExecutor; | ||
75 | + | ||
76 | + @Before | ||
77 | + public void before() { | ||
78 | + dbExecutor = new ListeningExecutor() { | ||
79 | + @Override | ||
80 | + public <T> ListenableFuture<T> executeAsync(Callable<T> task) { | ||
81 | + try { | ||
82 | + return Futures.immediateFuture(task.call()); | ||
83 | + } catch (Exception e) { | ||
84 | + throw new RuntimeException(e); | ||
85 | + } | ||
86 | + } | ||
87 | + | ||
88 | + @Override | ||
89 | + public void execute(Runnable command) { | ||
90 | + command.run(); | ||
91 | + } | ||
92 | + }; | ||
93 | + } | ||
94 | + | ||
95 | + @Test | ||
96 | + public void testCreateNewRelation() throws TbNodeException { | ||
97 | + init(createRelationNodeConfig()); | ||
98 | + | ||
99 | + DeviceId deviceId = new DeviceId(Uuids.timeBased()); | ||
100 | + | ||
101 | + AssetId assetId = new AssetId(Uuids.timeBased()); | ||
102 | + Asset asset = new Asset(); | ||
103 | + asset.setId(assetId); | ||
104 | + | ||
105 | + when(assetService.findAssetByTenantIdAndName(any(), eq("AssetName"))).thenReturn(asset); | ||
106 | + when(assetService.findAssetByIdAsync(any(), eq(assetId))).thenReturn(Futures.immediateFuture(asset)); | ||
107 | + | ||
108 | + TbMsgMetaData metaData = new TbMsgMetaData(); | ||
109 | + metaData.putValue("name", "AssetName"); | ||
110 | + metaData.putValue("type", "AssetType"); | ||
111 | + msg = TbMsg.newMsg(DataConstants.ENTITY_CREATED, deviceId, metaData, TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); | ||
112 | + | ||
113 | + when(ctx.getRelationService().checkRelation(any(), eq(assetId), eq(deviceId), eq(RELATION_TYPE_CONTAINS), eq(RelationTypeGroup.COMMON))) | ||
114 | + .thenReturn(Futures.immediateFuture(false)); | ||
115 | + when(ctx.getRelationService().saveRelationAsync(any(), eq(new EntityRelation(assetId, deviceId, RELATION_TYPE_CONTAINS, RelationTypeGroup.COMMON)))) | ||
116 | + .thenReturn(Futures.immediateFuture(true)); | ||
117 | + | ||
118 | + node.onMsg(ctx, msg); | ||
119 | + verify(ctx).tellNext(msg, TbRelationTypes.SUCCESS); | ||
120 | + } | ||
121 | + | ||
122 | + @Test | ||
123 | + public void testDeleteCurrentRelationsCreateNewRelation() throws TbNodeException { | ||
124 | + init(createRelationNodeConfigWithRemoveCurrentRelations()); | ||
125 | + | ||
126 | + DeviceId deviceId = new DeviceId(Uuids.timeBased()); | ||
127 | + | ||
128 | + AssetId assetId = new AssetId(Uuids.timeBased()); | ||
129 | + Asset asset = new Asset(); | ||
130 | + asset.setId(assetId); | ||
131 | + | ||
132 | + when(assetService.findAssetByTenantIdAndName(any(), eq("AssetName"))).thenReturn(asset); | ||
133 | + when(assetService.findAssetByIdAsync(any(), eq(assetId))).thenReturn(Futures.immediateFuture(asset)); | ||
134 | + | ||
135 | + TbMsgMetaData metaData = new TbMsgMetaData(); | ||
136 | + metaData.putValue("name", "AssetName"); | ||
137 | + metaData.putValue("type", "AssetType"); | ||
138 | + msg = TbMsg.newMsg(DataConstants.ENTITY_CREATED, deviceId, metaData, TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); | ||
139 | + | ||
140 | + EntityRelation relation = new EntityRelation(); | ||
141 | + when(ctx.getRelationService().findByToAndTypeAsync(any(), eq(msg.getOriginator()), eq(RELATION_TYPE_CONTAINS), eq(RelationTypeGroup.COMMON))) | ||
142 | + .thenReturn(Futures.immediateFuture(Collections.singletonList(relation))); | ||
143 | + when(ctx.getRelationService().deleteRelationAsync(any(), eq(relation))).thenReturn(Futures.immediateFuture(true)); | ||
144 | + when(ctx.getRelationService().checkRelation(any(), eq(assetId), eq(deviceId), eq(RELATION_TYPE_CONTAINS), eq(RelationTypeGroup.COMMON))) | ||
145 | + .thenReturn(Futures.immediateFuture(false)); | ||
146 | + when(ctx.getRelationService().saveRelationAsync(any(), eq(new EntityRelation(assetId, deviceId, RELATION_TYPE_CONTAINS, RelationTypeGroup.COMMON)))) | ||
147 | + .thenReturn(Futures.immediateFuture(true)); | ||
148 | + | ||
149 | + node.onMsg(ctx, msg); | ||
150 | + verify(ctx).tellNext(msg, TbRelationTypes.SUCCESS); | ||
151 | + } | ||
152 | + | ||
153 | + public void init(TbCreateRelationNodeConfiguration configuration) throws TbNodeException { | ||
154 | + ObjectMapper mapper = new ObjectMapper(); | ||
155 | + TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(configuration)); | ||
156 | + | ||
157 | + when(ctx.getDbCallbackExecutor()).thenReturn(dbExecutor); | ||
158 | + when(ctx.getRelationService()).thenReturn(relationService); | ||
159 | + when(ctx.getAssetService()).thenReturn(assetService); | ||
160 | + | ||
161 | + node = new TbCreateRelationNode(); | ||
162 | + node.init(ctx, nodeConfiguration); | ||
163 | + } | ||
164 | + | ||
165 | + private TbCreateRelationNodeConfiguration createRelationNodeConfig() { | ||
166 | + TbCreateRelationNodeConfiguration configuration = new TbCreateRelationNodeConfiguration(); | ||
167 | + configuration.setDirection(EntitySearchDirection.FROM.name()); | ||
168 | + configuration.setRelationType(RELATION_TYPE_CONTAINS); | ||
169 | + configuration.setEntityCacheExpiration(300); | ||
170 | + configuration.setEntityType("ASSET"); | ||
171 | + configuration.setEntityNamePattern("${name}"); | ||
172 | + configuration.setEntityTypePattern("${type}"); | ||
173 | + configuration.setCreateEntityIfNotExists(false); | ||
174 | + configuration.setChangeOriginatorToRelatedEntity(false); | ||
175 | + configuration.setRemoveCurrentRelations(false); | ||
176 | + return configuration; | ||
177 | + } | ||
178 | + | ||
179 | + private TbCreateRelationNodeConfiguration createRelationNodeConfigWithRemoveCurrentRelations() { | ||
180 | + TbCreateRelationNodeConfiguration configuration = createRelationNodeConfig(); | ||
181 | + configuration.setRemoveCurrentRelations(true); | ||
182 | + return configuration; | ||
183 | + } | ||
184 | +} |