Search in sources :

Example 16 with AssetId

use of org.thingsboard.server.common.data.id.AssetId in project thingsboard by thingsboard.

the class BaseRelationServiceTest method testRecursiveRelation.

@Test
public void testRecursiveRelation() throws ExecutionException, InterruptedException {
    // A -> B -> [C,D]
    AssetId assetA = new AssetId(UUIDs.timeBased());
    AssetId assetB = new AssetId(UUIDs.timeBased());
    AssetId assetC = new AssetId(UUIDs.timeBased());
    DeviceId deviceD = new DeviceId(UUIDs.timeBased());
    EntityRelation relationAB = new EntityRelation(assetA, assetB, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationBC = new EntityRelation(assetB, assetC, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationBD = new EntityRelation(assetB, deviceD, EntityRelation.CONTAINS_TYPE);
    saveRelation(relationAB);
    saveRelation(relationBC);
    saveRelation(relationBD);
    EntityRelationsQuery query = new EntityRelationsQuery();
    query.setParameters(new RelationsSearchParameters(assetA, EntitySearchDirection.FROM, -1));
    query.setFilters(Collections.singletonList(new EntityTypeFilter(EntityRelation.CONTAINS_TYPE, Collections.singletonList(EntityType.ASSET))));
    List<EntityRelation> relations = relationService.findByQuery(query).get();
    Assert.assertEquals(2, relations.size());
    Assert.assertTrue(relations.contains(relationAB));
    Assert.assertTrue(relations.contains(relationBC));
}
Also used : EntityTypeFilter(org.thingsboard.server.common.data.relation.EntityTypeFilter) EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) EntityRelationsQuery(org.thingsboard.server.common.data.relation.EntityRelationsQuery) DeviceId(org.thingsboard.server.common.data.id.DeviceId) RelationsSearchParameters(org.thingsboard.server.common.data.relation.RelationsSearchParameters) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Example 17 with AssetId

use of org.thingsboard.server.common.data.id.AssetId in project thingsboard by thingsboard.

the class BaseRelationServiceTest method testSaveRelationWithEmptyFrom.

@Test(expected = DataValidationException.class)
public void testSaveRelationWithEmptyFrom() throws ExecutionException, InterruptedException {
    EntityRelation relation = new EntityRelation();
    relation.setTo(new AssetId(UUIDs.timeBased()));
    relation.setType(EntityRelation.CONTAINS_TYPE);
    Assert.assertTrue(saveRelation(relation));
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Example 18 with AssetId

use of org.thingsboard.server.common.data.id.AssetId in project thingsboard by thingsboard.

the class BaseRelationServiceTest method testSaveRelationWithEmptyType.

@Test(expected = DataValidationException.class)
public void testSaveRelationWithEmptyType() throws ExecutionException, InterruptedException {
    EntityRelation relation = new EntityRelation();
    relation.setFrom(new AssetId(UUIDs.timeBased()));
    relation.setTo(new AssetId(UUIDs.timeBased()));
    Assert.assertTrue(saveRelation(relation));
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Example 19 with AssetId

use of org.thingsboard.server.common.data.id.AssetId in project thingsboard by thingsboard.

the class BaseAlarmServiceTest method testFindAlarm.

@Test
public void testFindAlarm() throws ExecutionException, InterruptedException {
    AssetId parentId = new AssetId(UUIDs.timeBased());
    AssetId childId = new AssetId(UUIDs.timeBased());
    EntityRelation relation = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
    Assert.assertTrue(relationService.saveRelationAsync(relation).get());
    long ts = System.currentTimeMillis();
    Alarm alarm = Alarm.builder().tenantId(tenantId).originator(childId).type(TEST_ALARM).propagate(false).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    Alarm created = alarmService.createOrUpdateAlarm(alarm);
    // Check child relation
    TimePageData<AlarmInfo> alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(childId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    // Check parent relation
    alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(parentId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(0, alarms.getData().size());
    created.setPropagate(true);
    created = alarmService.createOrUpdateAlarm(created);
    // Check child relation
    alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(childId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    // Check parent relation
    alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(parentId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    alarmService.ackAlarm(created.getId(), System.currentTimeMillis()).get();
    created = alarmService.findAlarmByIdAsync(created.getId()).get();
    alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(childId).status(AlarmStatus.ACTIVE_ACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    // Check not existing relation
    alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(childId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(0, alarms.getData().size());
    alarmService.clearAlarm(created.getId(), System.currentTimeMillis()).get();
    created = alarmService.findAlarmByIdAsync(created.getId()).get();
    alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(childId).status(AlarmStatus.CLEARED_ACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Example 20 with AssetId

use of org.thingsboard.server.common.data.id.AssetId in project thingsboard by thingsboard.

the class AssetController method assignAssetToCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/{customerId}/asset/{assetId}", method = RequestMethod.POST)
@ResponseBody
public Asset assignAssetToCustomer(@PathVariable("customerId") String strCustomerId, @PathVariable(ASSET_ID) String strAssetId) throws ThingsboardException {
    checkParameter("customerId", strCustomerId);
    checkParameter(ASSET_ID, strAssetId);
    try {
        CustomerId customerId = new CustomerId(toUUID(strCustomerId));
        Customer customer = checkCustomerId(customerId);
        AssetId assetId = new AssetId(toUUID(strAssetId));
        checkAssetId(assetId);
        Asset savedAsset = checkNotNull(assetService.assignAssetToCustomer(assetId, customerId));
        logEntityAction(assetId, savedAsset, savedAsset.getCustomerId(), ActionType.ASSIGNED_TO_CUSTOMER, null, strAssetId, strCustomerId, customer.getName());
        return savedAsset;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ASSET), null, null, ActionType.ASSIGNED_TO_CUSTOMER, e, strAssetId, strCustomerId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) AssetId(org.thingsboard.server.common.data.id.AssetId) ThingsboardException(org.thingsboard.server.exception.ThingsboardException) IncorrectParameterException(org.thingsboard.server.dao.exception.IncorrectParameterException) PreAuthorize(org.springframework.security.access.prepost.PreAuthorize)

Aggregations

AssetId (org.thingsboard.server.common.data.id.AssetId)21 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)13 Test (org.junit.Test)12 Asset (org.thingsboard.server.common.data.asset.Asset)8 CustomerId (org.thingsboard.server.common.data.id.CustomerId)6 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)5 TenantId (org.thingsboard.server.common.data.id.TenantId)5 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)5 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)5 Customer (org.thingsboard.server.common.data.Customer)4 EntityRelationsQuery (org.thingsboard.server.common.data.relation.EntityRelationsQuery)2 EntityTypeFilter (org.thingsboard.server.common.data.relation.EntityTypeFilter)2 RelationsSearchParameters (org.thingsboard.server.common.data.relation.RelationsSearchParameters)2 Function (com.google.common.base.Function)1 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)1 Futures (com.google.common.util.concurrent.Futures)1 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1