Commit bdc37d3eb045121645f07c794038a2d173b6a760

Authored by deaflynx
2 parents 978efe60 4621d28e

Merged with BohdanSmetanyuk: added DefaultEdgeNotificationService,BaseEdgeEventC…

…ontrollerTest,EdgeEventControllerNoSqlTest,EdgeEventControllerSqlTest
... ... @@ -15,6 +15,7 @@
15 15 */
16 16 package org.thingsboard.server.service.edge;
17 17
  18 +import com.fasterxml.jackson.core.JsonProcessingException;
18 19 import com.fasterxml.jackson.databind.JsonNode;
19 20 import com.fasterxml.jackson.databind.ObjectMapper;
20 21 import com.google.common.util.concurrent.Futures;
... ... @@ -245,8 +246,8 @@ public class DefaultEdgeNotificationService implements EdgeNotificationService {
245 246 }, dbCallbackExecutorService);
246 247 }
247 248
248   - private void processRelation(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) {
249   - EntityRelation relation = mapper.convertValue(edgeNotificationMsg.getEntityBody(), EntityRelation.class);
  249 + private void processRelation(TenantId tenantId, TransportProtos.EdgeNotificationMsgProto edgeNotificationMsg) throws JsonProcessingException {
  250 + EntityRelation relation = mapper.readValue(edgeNotificationMsg.getEntityBody(), EntityRelation.class);
250 251 if (!relation.getFrom().getEntityType().equals(EntityType.EDGE) &&
251 252 !relation.getTo().getEntityType().equals(EntityType.EDGE)) {
252 253 List<ListenableFuture<List<EdgeId>>> futures = new ArrayList<>();
... ...
  1 +/**
  2 + * Copyright © 2016-2020 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.server.controller;
  17 +
  18 +import com.fasterxml.jackson.core.type.TypeReference;
  19 +import lombok.extern.slf4j.Slf4j;
  20 +import org.apache.commons.lang3.RandomStringUtils;
  21 +import org.junit.After;
  22 +import org.junit.Assert;
  23 +import org.junit.Before;
  24 +import org.junit.Test;
  25 +import org.thingsboard.server.common.data.Device;
  26 +import org.thingsboard.server.common.data.Tenant;
  27 +import org.thingsboard.server.common.data.User;
  28 +import org.thingsboard.server.common.data.asset.Asset;
  29 +import org.thingsboard.server.common.data.edge.Edge;
  30 +import org.thingsboard.server.common.data.edge.EdgeEvent;
  31 +import org.thingsboard.server.common.data.edge.EdgeEventType;
  32 +import org.thingsboard.server.common.data.id.TenantId;
  33 +import org.thingsboard.server.common.data.page.TimePageData;
  34 +import org.thingsboard.server.common.data.page.TimePageLink;
  35 +import org.thingsboard.server.common.data.relation.EntityRelation;
  36 +import org.thingsboard.server.common.data.security.Authority;
  37 +
  38 +import java.util.List;
  39 +
  40 +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
  41 +
  42 +@Slf4j
  43 +public class BaseEdgeEventControllerTest extends AbstractControllerTest {
  44 +
  45 + private Tenant savedTenant;
  46 + private TenantId tenantId;
  47 + private User tenantAdmin;
  48 +
  49 + @Before
  50 + public void beforeTest() throws Exception {
  51 + loginSysAdmin();
  52 +
  53 + Tenant tenant = new Tenant();
  54 + tenant.setTitle("My tenant");
  55 + savedTenant = doPost("/api/tenant", tenant, Tenant.class);
  56 + tenantId = savedTenant.getId();
  57 + Assert.assertNotNull(savedTenant);
  58 +
  59 + tenantAdmin = new User();
  60 + tenantAdmin.setAuthority(Authority.TENANT_ADMIN);
  61 + tenantAdmin.setTenantId(savedTenant.getId());
  62 + tenantAdmin.setEmail("tenant2@thingsboard.org");
  63 + tenantAdmin.setFirstName("Joe");
  64 + tenantAdmin.setLastName("Downs");
  65 +
  66 + tenantAdmin = createUserAndLogin(tenantAdmin, "testPassword1");
  67 + }
  68 +
  69 + @After
  70 + public void afterTest() throws Exception {
  71 + loginSysAdmin();
  72 +
  73 + doDelete("/api/tenant/" + savedTenant.getId().getId().toString())
  74 + .andExpect(status().isOk());
  75 + }
  76 +
  77 + @Test
  78 + public void testGetEdgeEvents() throws Exception {
  79 + Edge edge = constructEdge("TestEdge", "default");
  80 + edge = doPost("/api/edge", edge, Edge.class);
  81 +
  82 + Device device = constructDevice("TestDevice", "default");
  83 + Device savedDevice = doPost("/api/device", device, Device.class);
  84 +
  85 + doPost("/api/edge/" + edge.getId().toString() + "/device/" + savedDevice.getId().toString(), Device.class);
  86 +
  87 + Asset asset = constructAsset("TestAsset", "default");
  88 + Asset savedAsset = doPost("/api/asset", asset, Asset.class);
  89 +
  90 + doPost("/api/edge/" + edge.getId().toString() + "/asset/" + savedAsset.getId().toString(), Asset.class);
  91 +
  92 + EntityRelation relation = new EntityRelation(savedAsset.getId(), savedDevice.getId(), EntityRelation.CONTAINS_TYPE);
  93 +
  94 + doPost("/api/relation", relation);
  95 +
  96 + Thread.sleep(1000);
  97 +
  98 + List<EdgeEvent> edgeEvents = doGetTypedWithTimePageLink("/api/edge/" + edge.getId().toString() + "/events?",
  99 + new TypeReference<TimePageData<EdgeEvent>>() {
  100 + }, new TimePageLink(5)).getData();
  101 +
  102 + Assert.assertFalse(edgeEvents.isEmpty());
  103 + Assert.assertEquals(4, edgeEvents.size());
  104 + Assert.assertEquals(edgeEvents.get(0).getEdgeEventType(), EdgeEventType.RELATION);
  105 + Assert.assertEquals(edgeEvents.get(1).getEdgeEventType(), EdgeEventType.ASSET);
  106 + Assert.assertEquals(edgeEvents.get(2).getEdgeEventType(), EdgeEventType.DEVICE);
  107 + Assert.assertEquals(edgeEvents.get(3).getEdgeEventType(), EdgeEventType.RULE_CHAIN);
  108 + }
  109 +
  110 + private Edge constructEdge(String name, String type) {
  111 + Edge edge = new Edge();
  112 + edge.setTenantId(tenantId);
  113 + edge.setName(name);
  114 + edge.setType(type);
  115 + edge.setSecret(RandomStringUtils.randomAlphanumeric(20));
  116 + edge.setRoutingKey(RandomStringUtils.randomAlphanumeric(20));
  117 + return edge;
  118 + }
  119 +
  120 + private Device constructDevice(String name, String type) {
  121 + Device device = new Device();
  122 + device.setName(name);
  123 + device.setType(type);
  124 + return device;
  125 + }
  126 +
  127 + private Asset constructAsset(String name, String type) {
  128 + Asset asset = new Asset();
  129 + asset.setName(name);
  130 + asset.setType(type);
  131 + return asset;
  132 + }
  133 +
  134 +}
... ...
  1 +/**
  2 + * Copyright © 2016-2020 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.server.controller.nosql;
  17 +
  18 +import org.thingsboard.server.controller.BaseEdgeEventControllerTest;
  19 +import org.thingsboard.server.dao.service.DaoNoSqlTest;
  20 +
  21 +@DaoNoSqlTest
  22 +public class EdgeEventControllerNoSqlTest extends BaseEdgeEventControllerTest {
  23 +}
... ...
  1 +/**
  2 + * Copyright © 2016-2020 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.server.controller.sql;
  17 +
  18 +import org.thingsboard.server.controller.BaseEdgeEventControllerTest;
  19 +import org.thingsboard.server.dao.service.DaoSqlTest;
  20 +
  21 +@DaoSqlTest
  22 +public class EdgeEventControllerSqlTest extends BaseEdgeEventControllerTest {
  23 +}
... ...