Search in sources :

Example 56 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class BaseEdgeControllerTest method testFindCustomerEdgesByName.

@Test
public void testFindCustomerEdgesByName() throws Exception {
    Customer customer = new Customer();
    customer.setTitle("Test customer");
    customer = doPost("/api/customer", customer, Customer.class);
    CustomerId customerId = customer.getId();
    String title1 = "Edge title 1";
    List<Edge> edgesTitle1 = new ArrayList<>();
    for (int i = 0; i < 125; i++) {
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title1 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        Edge edge = constructEdge(name, "default");
        edge = doPost("/api/edge", edge, Edge.class);
        edgesTitle1.add(doPost("/api/customer/" + customerId.getId().toString() + "/edge/" + edge.getId().getId().toString(), Edge.class));
    }
    String title2 = "Edge title 2";
    List<Edge> edgesTitle2 = new ArrayList<>();
    for (int i = 0; i < 143; i++) {
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title2 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        Edge edge = constructEdge(name, "default");
        edge = doPost("/api/edge", edge, Edge.class);
        edgesTitle2.add(doPost("/api/customer/" + customerId.getId().toString() + "/edge/" + edge.getId().getId().toString(), Edge.class));
    }
    List<Edge> loadedEdgesTitle1 = new ArrayList<>();
    PageLink pageLink = new PageLink(15, 0, title1);
    PageData<Edge> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/edges?", new TypeReference<PageData<Edge>>() {
        }, pageLink);
        loadedEdgesTitle1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(edgesTitle1, idComparator);
    Collections.sort(loadedEdgesTitle1, idComparator);
    Assert.assertEquals(edgesTitle1, loadedEdgesTitle1);
    List<Edge> loadedEdgesTitle2 = new ArrayList<>();
    pageLink = new PageLink(4, 0, title2);
    do {
        pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/edges?", new TypeReference<PageData<Edge>>() {
        }, pageLink);
        loadedEdgesTitle2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(edgesTitle2, idComparator);
    Collections.sort(loadedEdgesTitle2, idComparator);
    Assert.assertEquals(edgesTitle2, loadedEdgesTitle2);
    for (Edge edge : loadedEdgesTitle1) {
        doDelete("/api/customer/edge/" + edge.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new PageLink(4, 0, title1);
    pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/edges?", new TypeReference<PageData<Edge>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Edge edge : loadedEdgesTitle2) {
        doDelete("/api/customer/edge/" + edge.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new PageLink(4, 0, title2);
    pageData = doGetTypedWithPageLink("/api/customer/" + customerId.getId().toString() + "/edges?", new TypeReference<PageData<Edge>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
}
Also used : Customer(org.thingsboard.server.common.data.Customer) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) CustomerId(org.thingsboard.server.common.data.id.CustomerId) Matchers.containsString(org.hamcrest.Matchers.containsString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 57 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class BaseEdgeControllerTest method testSyncEdge.

@Test
public void testSyncEdge() throws Exception {
    Edge edge = doPost("/api/edge", constructEdge("Test Sync Edge", "test"), Edge.class);
    Device device = new Device();
    device.setName("Test Sync Edge Device 1");
    device.setType("default");
    Device savedDevice = doPost("/api/device", device, Device.class);
    doPost("/api/edge/" + edge.getId().getId().toString() + "/device/" + savedDevice.getId().getId().toString(), Device.class);
    Asset asset = new Asset();
    asset.setName("Test Sync Edge Asset 1");
    asset.setType("test");
    Asset savedAsset = doPost("/api/asset", asset, Asset.class);
    doPost("/api/edge/" + edge.getId().getId().toString() + "/asset/" + savedAsset.getId().getId().toString(), Asset.class);
    EdgeImitator edgeImitator = new EdgeImitator(EDGE_HOST, EDGE_PORT, edge.getRoutingKey(), edge.getSecret());
    edgeImitator.ignoreType(UserCredentialsUpdateMsg.class);
    edgeImitator.expectMessageAmount(11);
    edgeImitator.connect();
    assertThat(edgeImitator.waitForMessages()).as("await for messages on first connect").isTrue();
    assertThat(edgeImitator.findAllMessagesByType(RuleChainUpdateMsg.class)).as("one msg during sync process, another from edge creation").hasSize(2);
    assertThat(edgeImitator.findAllMessagesByType(DeviceProfileUpdateMsg.class)).as("one msg during sync process for 'default' device profile").hasSize(1);
    assertThat(edgeImitator.findAllMessagesByType(DeviceUpdateMsg.class)).as("one msg once device assigned to edge").hasSize(1);
    assertThat(edgeImitator.findAllMessagesByType(AssetUpdateMsg.class)).as("two msgs - one during sync process, and one more once asset assigned to edge").hasSize(2);
    assertThat(edgeImitator.findAllMessagesByType(UserUpdateMsg.class)).as("one msg during sync process for tenant admin user").hasSize(1);
    assertThat(edgeImitator.findAllMessagesByType(AdminSettingsUpdateMsg.class)).as("admin setting update").hasSize(4);
    edgeImitator.expectMessageAmount(8);
    doPost("/api/edge/sync/" + edge.getId());
    assertThat(edgeImitator.waitForMessages()).as("await for messages after edge sync rest api call").isTrue();
    assertThat(edgeImitator.findAllMessagesByType(RuleChainUpdateMsg.class)).as("rule chain msg").hasSize(1);
    assertThat(edgeImitator.findAllMessagesByType(DeviceProfileUpdateMsg.class)).as("device profile msg").hasSize(1);
    assertThat(edgeImitator.findAllMessagesByType(AssetUpdateMsg.class)).as("asset update msg").hasSize(1);
    assertThat(edgeImitator.findAllMessagesByType(UserUpdateMsg.class)).as("user update msg").hasSize(1);
    assertThat(edgeImitator.findAllMessagesByType(AdminSettingsUpdateMsg.class)).as("admin setting update msg").hasSize(4);
    edgeImitator.allowIgnoredTypes();
    try {
        edgeImitator.disconnect();
    } catch (Exception ignored) {
    }
    doDelete("/api/device/" + savedDevice.getId().getId().toString()).andExpect(status().isOk());
    doDelete("/api/asset/" + savedAsset.getId().getId().toString()).andExpect(status().isOk());
    doDelete("/api/edge/" + edge.getId().getId().toString()).andExpect(status().isOk());
}
Also used : DeviceProfileUpdateMsg(org.thingsboard.server.gen.edge.v1.DeviceProfileUpdateMsg) Device(org.thingsboard.server.common.data.Device) AssetUpdateMsg(org.thingsboard.server.gen.edge.v1.AssetUpdateMsg) AdminSettingsUpdateMsg(org.thingsboard.server.gen.edge.v1.AdminSettingsUpdateMsg) Asset(org.thingsboard.server.common.data.asset.Asset) DeviceUpdateMsg(org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg) EdgeImitator(org.thingsboard.server.edge.imitator.EdgeImitator) Edge(org.thingsboard.server.common.data.edge.Edge) UserUpdateMsg(org.thingsboard.server.gen.edge.v1.UserUpdateMsg) RuleChainUpdateMsg(org.thingsboard.server.gen.edge.v1.RuleChainUpdateMsg) Test(org.junit.Test)

Example 58 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class BaseEdgeControllerTest method testFindTenantEdgesByName.

@Test
public void testFindTenantEdgesByName() throws Exception {
    String title1 = "Edge title 1";
    List<Edge> edgesTitle1 = new ArrayList<>();
    for (int i = 0; i < 143; i++) {
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title1 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        Edge edge = constructEdge(name, "default");
        edgesTitle1.add(doPost("/api/edge", edge, Edge.class));
    }
    String title2 = "Edge title 2";
    List<Edge> edgesTitle2 = new ArrayList<>();
    for (int i = 0; i < 75; i++) {
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title2 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        Edge edge = constructEdge(name, "default");
        edgesTitle2.add(doPost("/api/edge", edge, Edge.class));
    }
    List<Edge> loadedEdgesTitle1 = new ArrayList<>();
    PageLink pageLink = new PageLink(15, 0, title1);
    PageData<Edge> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/tenant/edges?", new TypeReference<PageData<Edge>>() {
        }, pageLink);
        loadedEdgesTitle1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(edgesTitle1, idComparator);
    Collections.sort(loadedEdgesTitle1, idComparator);
    Assert.assertEquals(edgesTitle1, loadedEdgesTitle1);
    List<Edge> loadedEdgesTitle2 = new ArrayList<>();
    pageLink = new PageLink(4, 0, title2);
    do {
        pageData = doGetTypedWithPageLink("/api/tenant/edges?", new TypeReference<PageData<Edge>>() {
        }, pageLink);
        loadedEdgesTitle2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(edgesTitle2, idComparator);
    Collections.sort(loadedEdgesTitle2, idComparator);
    Assert.assertEquals(edgesTitle2, loadedEdgesTitle2);
    for (Edge edge : loadedEdgesTitle1) {
        doDelete("/api/edge/" + edge.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new PageLink(4, 0, title1);
    pageData = doGetTypedWithPageLink("/api/tenant/edges?", new TypeReference<PageData<Edge>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Edge edge : loadedEdgesTitle2) {
        doDelete("/api/edge/" + edge.getId().getId().toString()).andExpect(status().isOk());
    }
    pageLink = new PageLink(4, 0, title2);
    pageData = doGetTypedWithPageLink("/api/tenant/edges?", new TypeReference<PageData<Edge>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
}
Also used : ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) Matchers.containsString(org.hamcrest.Matchers.containsString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 59 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class BaseEdgeControllerTest method testDeleteEdge.

@Test
public void testDeleteEdge() throws Exception {
    Edge edge = constructEdge("My edge", "default");
    Edge savedEdge = doPost("/api/edge", edge, Edge.class);
    doDelete("/api/edge/" + savedEdge.getId().getId().toString()).andExpect(status().isOk());
    doGet("/api/edge/" + savedEdge.getId().getId().toString()).andExpect(status().isNotFound());
}
Also used : Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 60 with Edge

use of org.thingsboard.server.common.data.edge.Edge in project thingsboard by thingsboard.

the class BaseEdgeControllerTest method testAssignEdgeToNonExistentCustomer.

@Test
public void testAssignEdgeToNonExistentCustomer() throws Exception {
    Edge edge = constructEdge("My edge", "default");
    Edge savedEdge = doPost("/api/edge", edge, Edge.class);
    doPost("/api/customer/" + Uuids.timeBased().toString() + "/edge/" + savedEdge.getId().getId().toString()).andExpect(status().isNotFound());
}
Also used : Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Aggregations

Edge (org.thingsboard.server.common.data.edge.Edge)98 Test (org.junit.Test)46 EdgeId (org.thingsboard.server.common.data.id.EdgeId)32 PageLink (org.thingsboard.server.common.data.page.PageLink)31 ArrayList (java.util.ArrayList)25 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)25 ApiOperation (io.swagger.annotations.ApiOperation)23 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)23 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)23 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)22 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)22 CustomerId (org.thingsboard.server.common.data.id.CustomerId)21 Customer (org.thingsboard.server.common.data.Customer)20 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)20 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)16 TypeReference (com.fasterxml.jackson.core.type.TypeReference)15 TenantId (org.thingsboard.server.common.data.id.TenantId)14 Device (org.thingsboard.server.common.data.Device)13 Asset (org.thingsboard.server.common.data.asset.Asset)13 PageData (org.thingsboard.server.common.data.page.PageData)13