Relation for Edge (#12353)

* Relation for Edge

- Removed the relation filtering for Edge.

* Relation for Edge

- add test for relation Edge

* Relation for Edge

- refactoring

* Relation for Edge

- Naming fixed
This commit is contained in:
yevhenii_zahrebelnyi 2024-12-30 17:46:03 +02:00 committed by GitHub
parent d9f37cd2f2
commit c0f4440bd9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 62 additions and 15 deletions

View File

@ -73,7 +73,7 @@ public abstract class RelationEdgeProcessor extends BaseRelationProcessor implem
public ListenableFuture<Void> processRelationNotification(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
EntityRelation relation = JacksonUtil.fromString(edgeNotificationMsg.getBody(), EntityRelation.class);
if (relation == null || (relation.getFrom().getEntityType().equals(EntityType.EDGE) || relation.getTo().getEntityType().equals(EntityType.EDGE))) {
if (relation == null) {
return Futures.immediateFuture(null);
}
EdgeId originatorEdgeId = safeGetEdgeId(edgeNotificationMsg.getOriginatorEdgeIdMSB(), edgeNotificationMsg.getOriginatorEdgeIdLSB());

View File

@ -21,8 +21,10 @@ import org.junit.Assert;
import org.junit.Test;
import org.thingsboard.common.util.JacksonUtil;
import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.asset.Asset;
import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.EdgeId;
import org.thingsboard.server.common.data.relation.EntityRelation;
import org.thingsboard.server.common.data.relation.RelationTypeGroup;
import org.thingsboard.server.dao.service.DaoSqlTest;
@ -31,6 +33,8 @@ import org.thingsboard.server.gen.edge.v1.RelationUpdateMsg;
import org.thingsboard.server.gen.edge.v1.UpdateMsgType;
import org.thingsboard.server.gen.edge.v1.UplinkMsg;
import java.util.UUID;
@DaoSqlTest
public class RelationEdgeTest extends AbstractEdgeTest {
@ -58,13 +62,14 @@ public class RelationEdgeTest extends AbstractEdgeTest {
// delete relation
edgeImitator.expectMessageAmount(1);
var deletedRelation = doDelete("/api/v2/relation?" +
"fromId=" + relation.getFrom().getId().toString() +
"&fromType=" + relation.getFrom().getEntityType().name() +
"&relationType=" + relation.getType() +
"&relationTypeGroup=" + relation.getTypeGroup().name() +
"&toId=" + relation.getTo().getId().toString() +
"&toType=" + relation.getTo().getEntityType().name(), EntityRelation.class);
String deleteUrl = String.format("/api/v2/relation?fromId=%s&fromType=%s&relationType=%s&relationTypeGroup=%s&toId=%s&toType=%s",
device.getId().toString(), EntityType.DEVICE.name(), "test",
RelationTypeGroup.COMMON.name(), asset.getId().toString(), EntityType.ASSET.name()
);
var deletedRelation = doDelete(deleteUrl, EntityRelation.class);
Assert.assertTrue(edgeImitator.waitForMessages());
latestMessage = edgeImitator.getLatestMessage();
Assert.assertTrue(latestMessage instanceof RelationUpdateMsg);
@ -94,13 +99,13 @@ public class RelationEdgeTest extends AbstractEdgeTest {
edgeImitator.sendUplinkMsg(uplinkMsgBuilder.build());
Assert.assertTrue(edgeImitator.waitForResponses());
EntityRelation relation = doGet("/api/relation?" +
"&fromId=" + device2.getUuidId() +
"&fromType=" + device2.getId().getEntityType().name() +
"&relationType=" + "test" +
"&relationTypeGroup=" + RelationTypeGroup.COMMON.name() +
"&toId=" + device1.getUuidId() +
"&toType=" + device1.getId().getEntityType().name(), EntityRelation.class);
String getUrl = String.format("/api/relation?fromId=%s&fromType=%s&relationType=%s&relationTypeGroup=%s&toId=%s&toType=%s",
device2.getUuidId(), EntityType.DEVICE.name(), "test",
RelationTypeGroup.COMMON.name(), device1.getUuidId(), EntityType.DEVICE.name()
);
var relation = doGet(getUrl, EntityRelation.class);
Assert.assertNotNull(relation);
}
@ -154,6 +159,47 @@ public class RelationEdgeTest extends AbstractEdgeTest {
Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, relationUpdateMsg.getMsgType());
}
@Test
public void testRelationFromEdgeToDevice() throws Exception {
// create relation
Device device = findDeviceByName("Edge Device 1");
EdgeId edgeId = new EdgeId(new UUID(edgeImitator.getConfiguration().getEdgeIdMSB(), edgeImitator.getConfiguration().getEdgeIdLSB()));
EntityRelation relation = new EntityRelation();
relation.setType("test");
relation.setFrom(edgeId);
relation.setTo(device.getId());
relation.setTypeGroup(RelationTypeGroup.COMMON);
edgeImitator.expectMessageAmount(1);
relation = doPost("/api/v2/relation", relation, EntityRelation.class);
Assert.assertTrue(edgeImitator.waitForMessages());
AbstractMessage latestMessage = edgeImitator.getLatestMessage();
Assert.assertTrue(latestMessage instanceof RelationUpdateMsg);
RelationUpdateMsg relationUpdateMsg = (RelationUpdateMsg) latestMessage;
EntityRelation entityRelation = JacksonUtil.fromString(relationUpdateMsg.getEntity(), EntityRelation.class, true);
Assert.assertNotNull(entityRelation);
Assert.assertEquals(relation, entityRelation);
Assert.assertEquals(UpdateMsgType.ENTITY_CREATED_RPC_MESSAGE, relationUpdateMsg.getMsgType());
// delete relation
edgeImitator.expectMessageAmount(1);
String deleteUrl = String.format("/api/v2/relation?fromId=%s&fromType=%s&relationType=%s&relationTypeGroup=%s&toId=%s&toType=%s",
edgeId, EntityType.EDGE.name(), "test",
RelationTypeGroup.COMMON.name(), device.getId().toString(), EntityType.DEVICE.name()
);
var deletedRelation = doDelete(deleteUrl, EntityRelation.class);
Assert.assertTrue(edgeImitator.waitForMessages());
latestMessage = edgeImitator.getLatestMessage();
Assert.assertTrue(latestMessage instanceof RelationUpdateMsg);
relationUpdateMsg = (RelationUpdateMsg) latestMessage;
entityRelation = JacksonUtil.fromString(relationUpdateMsg.getEntity(), EntityRelation.class, true);
Assert.assertNotNull(entityRelation);
Assert.assertEquals(deletedRelation, entityRelation);
Assert.assertEquals(UpdateMsgType.ENTITY_DELETED_RPC_MESSAGE, relationUpdateMsg.getMsgType());
}
private EntityRelation buildEntityRelationForUplinkMsg(DeviceId deviceId1, DeviceId deviceId2) {
EntityRelation relation = new EntityRelation();
relation.setType("test");
@ -163,4 +209,5 @@ public class RelationEdgeTest extends AbstractEdgeTest {
relation.setAdditionalInfo(TextNode.valueOf("{}"));
return relation;
}
}