Search in sources :

Example 16 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class DefaultEdgeRequestsService method processEntityViewsRequestMsg.

@Override
public ListenableFuture<Void> processEntityViewsRequestMsg(TenantId tenantId, Edge edge, EntityViewsRequestMsg entityViewsRequestMsg) {
    log.trace("[{}] processEntityViewsRequestMsg [{}][{}]", tenantId, edge.getName(), entityViewsRequestMsg);
    EntityId entityId = EntityIdFactory.getByTypeAndUuid(EntityType.valueOf(entityViewsRequestMsg.getEntityType()), new UUID(entityViewsRequestMsg.getEntityIdMSB(), entityViewsRequestMsg.getEntityIdLSB()));
    SettableFuture<Void> futureToSet = SettableFuture.create();
    Futures.addCallback(entityViewService.findEntityViewsByTenantIdAndEntityIdAsync(tenantId, entityId), new FutureCallback<>() {

        @Override
        public void onSuccess(@Nullable List<EntityView> entityViews) {
            try {
                if (entityViews != null && !entityViews.isEmpty()) {
                    List<ListenableFuture<Boolean>> futures = new ArrayList<>();
                    for (EntityView entityView : entityViews) {
                        ListenableFuture<Boolean> future = relationService.checkRelation(tenantId, edge.getId(), entityView.getId(), EntityRelation.CONTAINS_TYPE, RelationTypeGroup.EDGE);
                        futures.add(future);
                        Futures.addCallback(future, new FutureCallback<>() {

                            @Override
                            public void onSuccess(@Nullable Boolean result) {
                                if (Boolean.TRUE.equals(result)) {
                                    saveEdgeEvent(tenantId, edge.getId(), EdgeEventType.ENTITY_VIEW, EdgeEventActionType.ADDED, entityView.getId(), null);
                                }
                            }

                            @Override
                            public void onFailure(Throwable t) {
                            // Do nothing - error handles in allAsList
                            }
                        }, dbCallbackExecutorService);
                    }
                    Futures.addCallback(Futures.allAsList(futures), new FutureCallback<>() {

                        @Override
                        public void onSuccess(@Nullable List<Boolean> result) {
                            futureToSet.set(null);
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            log.error("Exception during loading relation [{}] to edge on sync!", t, t);
                            futureToSet.setException(t);
                        }
                    }, dbCallbackExecutorService);
                } else {
                    futureToSet.set(null);
                }
            } catch (Exception e) {
                log.error("Exception during loading relation(s) to edge on sync!", e);
                futureToSet.setException(e);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            log.error("[{}] Can't find entity views by entity id [{}]", tenantId, entityId, t);
            futureToSet.setException(t);
        }
    }, dbCallbackExecutorService);
    return futureToSet;
}
Also used : EntityView(org.thingsboard.server.common.data.EntityView) EntityId(org.thingsboard.server.common.data.id.EntityId) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) List(java.util.List) ArrayList(java.util.ArrayList) UUID(java.util.UUID) FutureCallback(com.google.common.util.concurrent.FutureCallback) Nullable(org.checkerframework.checker.nullness.qual.Nullable)

Example 17 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseEntityViewControllerTest method testAssignEntityViewToNonExistentCustomer.

@Test
public void testAssignEntityViewToNonExistentCustomer() throws Exception {
    EntityView savedView = getNewSavedEntityView("Test entity view");
    doPost("/api/customer/" + Uuids.timeBased().toString() + "/device/" + savedView.getId().getId().toString()).andExpect(status().isNotFound());
}
Also used : AttributesEntityView(org.thingsboard.server.common.data.objects.AttributesEntityView) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) Test(org.junit.Test)

Example 18 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseEntityViewControllerTest method testAssignAndUnAssignedEntityViewToCustomer.

@Test
public void testAssignAndUnAssignedEntityViewToCustomer() throws Exception {
    EntityView view = getNewSavedEntityView("Test entity view");
    Customer savedCustomer = doPost("/api/customer", getNewCustomer("My customer"), Customer.class);
    view.setCustomerId(savedCustomer.getId());
    EntityView savedView = doPost("/api/entityView", view, EntityView.class);
    EntityView assignedView = doPost("/api/customer/" + savedCustomer.getId().getId().toString() + "/entityView/" + savedView.getId().getId().toString(), EntityView.class);
    assertEquals(savedCustomer.getId(), assignedView.getCustomerId());
    EntityView foundView = doGet("/api/entityView/" + savedView.getId().getId().toString(), EntityView.class);
    assertEquals(savedCustomer.getId(), foundView.getCustomerId());
    EntityView unAssignedView = doDelete("/api/customer/entityView/" + savedView.getId().getId().toString(), EntityView.class);
    assertEquals(ModelConstants.NULL_UUID, unAssignedView.getCustomerId().getId());
    foundView = doGet("/api/entityView/" + savedView.getId().getId().toString(), EntityView.class);
    assertEquals(ModelConstants.NULL_UUID, foundView.getCustomerId().getId());
}
Also used : AttributesEntityView(org.thingsboard.server.common.data.objects.AttributesEntityView) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) Customer(org.thingsboard.server.common.data.Customer) Test(org.junit.Test)

Example 19 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseEntityViewControllerTest method testSaveEntityViewWithViolationOfValidation.

@Test
public void testSaveEntityViewWithViolationOfValidation() throws Exception {
    EntityView entityView = createEntityView(RandomStringUtils.randomAlphabetic(300), 0, 0);
    doPost("/api/entityView", entityView).andExpect(statusReason(containsString("length of name must be equal or less than 255")));
    entityView.setName("Normal name");
    entityView.setType(RandomStringUtils.randomAlphabetic(300));
    doPost("/api/entityView", entityView).andExpect(statusReason(containsString("length of type must be equal or less than 255")));
}
Also used : AttributesEntityView(org.thingsboard.server.common.data.objects.AttributesEntityView) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) Test(org.junit.Test)

Example 20 with EntityView

use of org.thingsboard.server.common.data.EntityView in project thingsboard by thingsboard.

the class BaseEntityViewControllerTest method testSaveEntityView.

@Test
public void testSaveEntityView() throws Exception {
    EntityView savedView = getNewSavedEntityView("Test entity view");
    Assert.assertNotNull(savedView);
    Assert.assertNotNull(savedView.getId());
    Assert.assertTrue(savedView.getCreatedTime() > 0);
    assertEquals(savedTenant.getId(), savedView.getTenantId());
    Assert.assertNotNull(savedView.getCustomerId());
    assertEquals(NULL_UUID, savedView.getCustomerId().getId());
    assertEquals(savedView.getName(), savedView.getName());
    savedView.setName("New test entity view");
    doPost("/api/entityView", savedView, EntityView.class);
    EntityView foundEntityView = doGet("/api/entityView/" + savedView.getId().getId().toString(), EntityView.class);
    assertEquals(foundEntityView.getName(), savedView.getName());
    assertEquals(foundEntityView.getKeys(), telemetry);
}
Also used : AttributesEntityView(org.thingsboard.server.common.data.objects.AttributesEntityView) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) Test(org.junit.Test)

Aggregations

EntityView (org.thingsboard.server.common.data.EntityView)51 TelemetryEntityView (org.thingsboard.server.common.data.objects.TelemetryEntityView)21 Test (org.junit.Test)18 AttributesEntityView (org.thingsboard.server.common.data.objects.AttributesEntityView)17 ExecutionException (java.util.concurrent.ExecutionException)16 EntityViewId (org.thingsboard.server.common.data.id.EntityViewId)15 ArrayList (java.util.ArrayList)13 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)12 Customer (org.thingsboard.server.common.data.Customer)11 ThingsboardException (org.thingsboard.server.common.data.exception.ThingsboardException)11 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)10 ApiOperation (io.swagger.annotations.ApiOperation)9 List (java.util.List)9 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)9 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)9 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)8 Edge (org.thingsboard.server.common.data.edge.Edge)8 PageLink (org.thingsboard.server.common.data.page.PageLink)7 DataValidationException (org.thingsboard.server.dao.exception.DataValidationException)7 Matchers.containsString (org.hamcrest.Matchers.containsString)6