Commit e9ba7655653ddc07a6e09a5dcb26d0ebff0ec1ba
Committed by
Andrew Shvayka
1 parent
e5ff6aba
added one more test for create relation node
Showing
1 changed file
with
43 additions
and
0 deletions
... | ... | @@ -22,6 +22,7 @@ import com.google.common.util.concurrent.ListenableFuture; |
22 | 22 | import org.junit.Before; |
23 | 23 | import org.junit.Test; |
24 | 24 | import org.junit.runner.RunWith; |
25 | +import org.mockito.ArgumentCaptor; | |
25 | 26 | import org.mockito.Mock; |
26 | 27 | import org.mockito.junit.MockitoJUnitRunner; |
27 | 28 | import org.thingsboard.common.util.ListeningExecutor; |
... | ... | @@ -33,6 +34,7 @@ import org.thingsboard.server.common.data.DataConstants; |
33 | 34 | import org.thingsboard.server.common.data.asset.Asset; |
34 | 35 | import org.thingsboard.server.common.data.id.AssetId; |
35 | 36 | import org.thingsboard.server.common.data.id.DeviceId; |
37 | +import org.thingsboard.server.common.data.id.EntityId; | |
36 | 38 | import org.thingsboard.server.common.data.id.RuleChainId; |
37 | 39 | import org.thingsboard.server.common.data.id.RuleNodeId; |
38 | 40 | import org.thingsboard.server.common.data.relation.EntityRelation; |
... | ... | @@ -47,6 +49,7 @@ import org.thingsboard.server.dao.relation.RelationService; |
47 | 49 | import java.util.Collections; |
48 | 50 | import java.util.concurrent.Callable; |
49 | 51 | |
52 | +import static org.junit.Assert.assertEquals; | |
50 | 53 | import static org.mockito.ArgumentMatchers.any; |
51 | 54 | import static org.mockito.ArgumentMatchers.eq; |
52 | 55 | import static org.mockito.Mockito.verify; |
... | ... | @@ -150,6 +153,40 @@ public class TbCreateRelationNodeTest { |
150 | 153 | verify(ctx).tellNext(msg, TbRelationTypes.SUCCESS); |
151 | 154 | } |
152 | 155 | |
156 | + @Test | |
157 | + public void testCreateNewRelationAndChangeOriginator() throws TbNodeException { | |
158 | + init(createRelationNodeConfigWithChangeOriginator()); | |
159 | + | |
160 | + DeviceId deviceId = new DeviceId(Uuids.timeBased()); | |
161 | + | |
162 | + AssetId assetId = new AssetId(Uuids.timeBased()); | |
163 | + Asset asset = new Asset(); | |
164 | + asset.setId(assetId); | |
165 | + | |
166 | + when(assetService.findAssetByTenantIdAndName(any(), eq("AssetName"))).thenReturn(asset); | |
167 | + when(assetService.findAssetByIdAsync(any(), eq(assetId))).thenReturn(Futures.immediateFuture(asset)); | |
168 | + | |
169 | + TbMsgMetaData metaData = new TbMsgMetaData(); | |
170 | + metaData.putValue("name", "AssetName"); | |
171 | + metaData.putValue("type", "AssetType"); | |
172 | + msg = TbMsg.newMsg(DataConstants.ENTITY_CREATED, deviceId, metaData, TbMsgDataType.JSON, "{}", ruleChainId, ruleNodeId); | |
173 | + | |
174 | + when(ctx.getRelationService().checkRelation(any(), eq(assetId), eq(deviceId), eq(RELATION_TYPE_CONTAINS), eq(RelationTypeGroup.COMMON))) | |
175 | + .thenReturn(Futures.immediateFuture(false)); | |
176 | + when(ctx.getRelationService().saveRelationAsync(any(), eq(new EntityRelation(assetId, deviceId, RELATION_TYPE_CONTAINS, RelationTypeGroup.COMMON)))) | |
177 | + .thenReturn(Futures.immediateFuture(true)); | |
178 | + | |
179 | + node.onMsg(ctx, msg); | |
180 | + ArgumentCaptor<TbMsg> msgCaptor = ArgumentCaptor.forClass(TbMsg.class); | |
181 | + ArgumentCaptor<String> typeCaptor = ArgumentCaptor.forClass(String.class); | |
182 | + ArgumentCaptor<EntityId> originatorCaptor = ArgumentCaptor.forClass(EntityId.class); | |
183 | + ArgumentCaptor<TbMsgMetaData> metadataCaptor = ArgumentCaptor.forClass(TbMsgMetaData.class); | |
184 | + ArgumentCaptor<String> dataCaptor = ArgumentCaptor.forClass(String.class); | |
185 | + verify(ctx).transformMsg(msgCaptor.capture(), typeCaptor.capture(), originatorCaptor.capture(), metadataCaptor.capture(), dataCaptor.capture()); | |
186 | + | |
187 | + assertEquals(assetId, originatorCaptor.getValue()); | |
188 | + } | |
189 | + | |
153 | 190 | public void init(TbCreateRelationNodeConfiguration configuration) throws TbNodeException { |
154 | 191 | ObjectMapper mapper = new ObjectMapper(); |
155 | 192 | TbNodeConfiguration nodeConfiguration = new TbNodeConfiguration(mapper.valueToTree(configuration)); |
... | ... | @@ -181,4 +218,10 @@ public class TbCreateRelationNodeTest { |
181 | 218 | configuration.setRemoveCurrentRelations(true); |
182 | 219 | return configuration; |
183 | 220 | } |
221 | + | |
222 | + private TbCreateRelationNodeConfiguration createRelationNodeConfigWithChangeOriginator() { | |
223 | + TbCreateRelationNodeConfiguration configuration = createRelationNodeConfig(); | |
224 | + configuration.setChangeOriginatorToRelatedEntity(true); | |
225 | + return configuration; | |
226 | + } | |
184 | 227 | } | ... | ... |