Search in sources :

Example 1 with Edge

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

the class AbstractWebTest method constructEdge.

protected Edge constructEdge(TenantId tenantId, String name, String type) {
    Edge edge = new Edge();
    edge.setTenantId(tenantId);
    edge.setName(name);
    edge.setType(type);
    edge.setSecret(RandomStringUtils.randomAlphanumeric(20));
    edge.setRoutingKey(RandomStringUtils.randomAlphanumeric(20));
    return edge;
}
Also used : Edge(org.thingsboard.server.common.data.edge.Edge)

Example 2 with Edge

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

the class BaseEdgeEventControllerTest method testGetEdgeEvents.

@Test
public void testGetEdgeEvents() throws Exception {
    Edge edge = constructEdge("TestEdge", "default");
    edge = doPost("/api/edge", edge, Edge.class);
    Device device = constructDevice("TestDevice", "default");
    Device savedDevice = doPost("/api/device", device, Device.class);
    final EdgeId edgeId = edge.getId();
    doPost("/api/edge/" + edgeId.toString() + "/device/" + savedDevice.getId().toString(), Device.class);
    Asset asset = constructAsset("TestAsset", "default");
    Asset savedAsset = doPost("/api/asset", asset, Asset.class);
    doPost("/api/edge/" + edgeId.toString() + "/asset/" + savedAsset.getId().toString(), Asset.class);
    EntityRelation relation = new EntityRelation(savedAsset.getId(), savedDevice.getId(), EntityRelation.CONTAINS_TYPE);
    doPost("/api/relation", relation);
    Awaitility.await().atMost(30, TimeUnit.SECONDS).until(() -> {
        List<EdgeEvent> edgeEvents = findEdgeEvents(edgeId);
        return edgeEvents.size() == 4;
    });
    List<EdgeEvent> edgeEvents = findEdgeEvents(edgeId);
    Assert.assertTrue(edgeEvents.stream().anyMatch(ee -> EdgeEventType.RULE_CHAIN.equals(ee.getType())));
    Assert.assertTrue(edgeEvents.stream().anyMatch(ee -> EdgeEventType.DEVICE.equals(ee.getType())));
    Assert.assertTrue(edgeEvents.stream().anyMatch(ee -> EdgeEventType.ASSET.equals(ee.getType())));
    Assert.assertTrue(edgeEvents.stream().anyMatch(ee -> EdgeEventType.RELATION.equals(ee.getType())));
}
Also used : EdgeId(org.thingsboard.server.common.data.id.EdgeId) Edge(org.thingsboard.server.common.data.edge.Edge) Device(org.thingsboard.server.common.data.Device) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) Tenant(org.thingsboard.server.common.data.Tenant) Test(org.junit.Test) Authority(org.thingsboard.server.common.data.security.Authority) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) TimeUnit(java.util.concurrent.TimeUnit) User(org.thingsboard.server.common.data.User) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) After(org.junit.After) PageData(org.thingsboard.server.common.data.page.PageData) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent) EdgeEventType(org.thingsboard.server.common.data.edge.EdgeEventType) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Assert(org.junit.Assert) Awaitility(org.awaitility.Awaitility) Before(org.junit.Before) Asset(org.thingsboard.server.common.data.asset.Asset) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) Device(org.thingsboard.server.common.data.Device) EdgeId(org.thingsboard.server.common.data.id.EdgeId) Asset(org.thingsboard.server.common.data.asset.Asset) EdgeEvent(org.thingsboard.server.common.data.edge.EdgeEvent) Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 3 with Edge

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

the class BaseRuleChainControllerTest method testFindEdgeRuleChainsByTenantIdAndName.

@Test
public void testFindEdgeRuleChainsByTenantIdAndName() throws Exception {
    Edge edge = constructEdge("My edge", "default");
    Edge savedEdge = doPost("/api/edge", edge, Edge.class);
    List<RuleChain> edgeRuleChains = new ArrayList<>();
    PageLink pageLink = new PageLink(17);
    PageData<RuleChain> pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId() + "/ruleChains?", new TypeReference<>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(1, pageData.getTotalElements());
    edgeRuleChains.addAll(pageData.getData());
    for (int i = 0; i < 28; i++) {
        RuleChain ruleChain = new RuleChain();
        ruleChain.setName("RuleChain " + i);
        ruleChain.setType(RuleChainType.EDGE);
        RuleChain savedRuleChain = doPost("/api/ruleChain", ruleChain, RuleChain.class);
        doPost("/api/edge/" + savedEdge.getId().getId().toString() + "/ruleChain/" + savedRuleChain.getId().getId().toString(), RuleChain.class);
        edgeRuleChains.add(savedRuleChain);
    }
    List<RuleChain> loadedEdgeRuleChains = new ArrayList<>();
    pageLink = new PageLink(17);
    do {
        pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId() + "/ruleChains?", new TypeReference<>() {
        }, pageLink);
        loadedEdgeRuleChains.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(edgeRuleChains, idComparator);
    Collections.sort(loadedEdgeRuleChains, idComparator);
    Assert.assertEquals(edgeRuleChains, loadedEdgeRuleChains);
    for (RuleChain ruleChain : loadedEdgeRuleChains) {
        if (!ruleChain.isRoot()) {
            doDelete("/api/edge/" + savedEdge.getId().getId().toString() + "/ruleChain/" + ruleChain.getId().getId().toString(), RuleChain.class);
        }
    }
    pageLink = new PageLink(17);
    pageData = doGetTypedWithPageLink("/api/edge/" + savedEdge.getId().getId() + "/ruleChains?", new TypeReference<>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(1, pageData.getTotalElements());
}
Also used : RuleChain(org.thingsboard.server.common.data.rule.RuleChain) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 4 with Edge

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

the class BaseEdgeControllerTest method testAssignEdgeToCustomerFromDifferentTenant.

@Test
public void testAssignEdgeToCustomerFromDifferentTenant() throws Exception {
    loginSysAdmin();
    Tenant tenant2 = new Tenant();
    tenant2.setTitle("Different tenant");
    Tenant savedTenant2 = doPost("/api/tenant", tenant2, Tenant.class);
    Assert.assertNotNull(savedTenant2);
    User tenantAdmin2 = new User();
    tenantAdmin2.setAuthority(Authority.TENANT_ADMIN);
    tenantAdmin2.setTenantId(savedTenant2.getId());
    tenantAdmin2.setEmail("tenant3@thingsboard.org");
    tenantAdmin2.setFirstName("Joe");
    tenantAdmin2.setLastName("Downs");
    tenantAdmin2 = createUserAndLogin(tenantAdmin2, "testPassword1");
    Customer customer = new Customer();
    customer.setTitle("Different customer");
    Customer savedCustomer = doPost("/api/customer", customer, Customer.class);
    login(tenantAdmin.getEmail(), "testPassword1");
    Edge edge = constructEdge("My edge", "default");
    Edge savedEdge = doPost("/api/edge", edge, Edge.class);
    doPost("/api/customer/" + savedCustomer.getId().getId().toString() + "/edge/" + savedEdge.getId().getId().toString()).andExpect(status().isForbidden());
    loginSysAdmin();
    doDelete("/api/tenant/" + savedTenant2.getId().getId().toString()).andExpect(status().isOk());
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) User(org.thingsboard.server.common.data.User) Customer(org.thingsboard.server.common.data.Customer) Edge(org.thingsboard.server.common.data.edge.Edge) Test(org.junit.Test)

Example 5 with Edge

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

the class BaseEdgeControllerTest method testFindTenantEdges.

@Test
public void testFindTenantEdges() throws Exception {
    List<Edge> edges = new ArrayList<>();
    for (int i = 0; i < 178; i++) {
        Edge edge = constructEdge("Edge" + i, "default");
        edges.add(doPost("/api/edge", edge, Edge.class));
    }
    List<Edge> loadedEdges = new ArrayList<>();
    PageLink pageLink = new PageLink(23);
    PageData<Edge> pageData = null;
    do {
        pageData = doGetTypedWithPageLink("/api/tenant/edges?", new TypeReference<PageData<Edge>>() {
        }, pageLink);
        loadedEdges.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(edges, idComparator);
    Collections.sort(loadedEdges, idComparator);
    Assert.assertEquals(edges, loadedEdges);
}
Also used : ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) 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