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());
}
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);
}
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);
}
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);
}
}
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);
}
}
Aggregations