Search in sources :

Example 6 with AssetId

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

the class BaseRelationServiceTest method testFindFrom.

@Test
public void testFindFrom() throws ExecutionException, InterruptedException {
    AssetId parentA = new AssetId(UUIDs.timeBased());
    AssetId parentB = new AssetId(UUIDs.timeBased());
    AssetId childA = new AssetId(UUIDs.timeBased());
    AssetId childB = new AssetId(UUIDs.timeBased());
    EntityRelation relationA1 = new EntityRelation(parentA, childA, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationA2 = new EntityRelation(parentA, childB, EntityRelation.CONTAINS_TYPE);
    EntityRelation relationB1 = new EntityRelation(parentB, childA, EntityRelation.MANAGES_TYPE);
    EntityRelation relationB2 = new EntityRelation(parentB, childB, EntityRelation.MANAGES_TYPE);
    saveRelation(relationA1);
    saveRelation(relationA2);
    saveRelation(relationB1);
    saveRelation(relationB2);
    List<EntityRelation> relations = relationService.findByFrom(parentA, RelationTypeGroup.COMMON);
    Assert.assertEquals(2, relations.size());
    for (EntityRelation relation : relations) {
        Assert.assertEquals(EntityRelation.CONTAINS_TYPE, relation.getType());
        Assert.assertEquals(parentA, relation.getFrom());
        Assert.assertTrue(childA.equals(relation.getTo()) || childB.equals(relation.getTo()));
    }
    relations = relationService.findByFromAndType(parentA, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(2, relations.size());
    relations = relationService.findByFromAndType(parentA, EntityRelation.MANAGES_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(0, relations.size());
    relations = relationService.findByFrom(parentB, RelationTypeGroup.COMMON);
    Assert.assertEquals(2, relations.size());
    for (EntityRelation relation : relations) {
        Assert.assertEquals(EntityRelation.MANAGES_TYPE, relation.getType());
        Assert.assertEquals(parentB, relation.getFrom());
        Assert.assertTrue(childA.equals(relation.getTo()) || childB.equals(relation.getTo()));
    }
    relations = relationService.findByFromAndType(parentB, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(0, relations.size());
    relations = relationService.findByFromAndType(parentB, EntityRelation.CONTAINS_TYPE, RelationTypeGroup.COMMON);
    Assert.assertEquals(0, relations.size());
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Example 7 with AssetId

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

the class BaseAlarmServiceTest method testSaveAndFetchAlarm.

@Test
public void testSaveAndFetchAlarm() 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).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    Alarm created = alarmService.createOrUpdateAlarm(alarm);
    Assert.assertNotNull(created);
    Assert.assertNotNull(created.getId());
    Assert.assertNotNull(created.getOriginator());
    Assert.assertNotNull(created.getSeverity());
    Assert.assertNotNull(created.getStatus());
    Assert.assertEquals(tenantId, created.getTenantId());
    Assert.assertEquals(childId, created.getOriginator());
    Assert.assertEquals(TEST_ALARM, created.getType());
    Assert.assertEquals(AlarmSeverity.CRITICAL, created.getSeverity());
    Assert.assertEquals(AlarmStatus.ACTIVE_UNACK, created.getStatus());
    Assert.assertEquals(ts, created.getStartTs());
    Assert.assertEquals(ts, created.getEndTs());
    Assert.assertEquals(0L, created.getAckTs());
    Assert.assertEquals(0L, created.getClearTs());
    Alarm fetched = alarmService.findAlarmByIdAsync(created.getId()).get();
    Assert.assertEquals(created, fetched);
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Example 8 with AssetId

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

the class JpaAssetDaoTest method saveAsset.

private void saveAsset(UUID id, UUID tenantId, UUID customerId, String name, String type) {
    Asset asset = new Asset();
    asset.setId(new AssetId(id));
    asset.setTenantId(new TenantId(tenantId));
    asset.setCustomerId(new CustomerId(customerId));
    asset.setName(name);
    asset.setType(type);
    assetDao.save(asset);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Asset(org.thingsboard.server.common.data.asset.Asset) CustomerId(org.thingsboard.server.common.data.id.CustomerId) AssetId(org.thingsboard.server.common.data.id.AssetId)

Example 9 with AssetId

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

the class AssetController method assignAssetToPublicCustomer.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/customer/public/asset/{assetId}", method = RequestMethod.POST)
@ResponseBody
public Asset assignAssetToPublicCustomer(@PathVariable(ASSET_ID) String strAssetId) throws ThingsboardException {
    checkParameter(ASSET_ID, strAssetId);
    try {
        AssetId assetId = new AssetId(toUUID(strAssetId));
        Asset asset = checkAssetId(assetId);
        Customer publicCustomer = customerService.findOrCreatePublicCustomer(asset.getTenantId());
        Asset savedAsset = checkNotNull(assetService.assignAssetToCustomer(assetId, publicCustomer.getId()));
        logEntityAction(assetId, savedAsset, savedAsset.getCustomerId(), ActionType.ASSIGNED_TO_CUSTOMER, null, strAssetId, publicCustomer.getId().toString(), publicCustomer.getName());
        return savedAsset;
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ASSET), null, null, ActionType.ASSIGNED_TO_CUSTOMER, e, strAssetId);
        throw handleException(e);
    }
}
Also used : Customer(org.thingsboard.server.common.data.Customer) Asset(org.thingsboard.server.common.data.asset.Asset) 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)

Example 10 with AssetId

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

the class AssetController method deleteAsset.

@PreAuthorize("hasAuthority('TENANT_ADMIN')")
@RequestMapping(value = "/asset/{assetId}", method = RequestMethod.DELETE)
@ResponseStatus(value = HttpStatus.OK)
public void deleteAsset(@PathVariable(ASSET_ID) String strAssetId) throws ThingsboardException {
    checkParameter(ASSET_ID, strAssetId);
    try {
        AssetId assetId = new AssetId(toUUID(strAssetId));
        Asset asset = checkAssetId(assetId);
        assetService.deleteAsset(assetId);
        logEntityAction(assetId, asset, asset.getCustomerId(), ActionType.DELETED, null, strAssetId);
    } catch (Exception e) {
        logEntityAction(emptyId(EntityType.ASSET), null, null, ActionType.DELETED, e, strAssetId);
        throw handleException(e);
    }
}
Also used : Asset(org.thingsboard.server.common.data.asset.Asset) 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