use of org.thingsboard.server.common.data.id.TenantId in project thingsboard by thingsboard.
the class BaseWidgetsBundleServiceTest method testFindAllWidgetsBundlesByTenantId.
@Test
public void testFindAllWidgetsBundlesByTenantId() {
List<WidgetsBundle> systemWidgetsBundles = widgetsBundleService.findSystemWidgetsBundles();
Tenant tenant = new Tenant();
tenant.setTitle("Test tenant");
tenant = tenantService.saveTenant(tenant);
TenantId tenantId = tenant.getId();
TenantId systemTenantId = new TenantId(ModelConstants.NULL_UUID);
List<WidgetsBundle> createdWidgetsBundles = new ArrayList<>();
List<WidgetsBundle> createdSystemWidgetsBundles = new ArrayList<>();
for (int i = 0; i < 277; i++) {
WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setTenantId(i % 2 == 0 ? tenantId : systemTenantId);
widgetsBundle.setTitle((i % 2 == 0 ? "Widgets bundle " : "System widget bundle ") + i);
WidgetsBundle savedWidgetsBundle = widgetsBundleService.saveWidgetsBundle(widgetsBundle);
createdWidgetsBundles.add(savedWidgetsBundle);
if (i % 2 == 1) {
createdSystemWidgetsBundles.add(savedWidgetsBundle);
}
}
List<WidgetsBundle> widgetsBundles = new ArrayList<>(createdWidgetsBundles);
widgetsBundles.addAll(systemWidgetsBundles);
List<WidgetsBundle> loadedWidgetsBundles = widgetsBundleService.findAllTenantWidgetsBundlesByTenantId(tenantId);
Collections.sort(widgetsBundles, idComparator);
Collections.sort(loadedWidgetsBundles, idComparator);
Assert.assertEquals(widgetsBundles, loadedWidgetsBundles);
widgetsBundleService.deleteWidgetsBundlesByTenantId(tenantId);
loadedWidgetsBundles = widgetsBundleService.findAllTenantWidgetsBundlesByTenantId(tenantId);
List<WidgetsBundle> allSystemWidgetsBundles = new ArrayList<>(systemWidgetsBundles);
allSystemWidgetsBundles.addAll(createdSystemWidgetsBundles);
Collections.sort(allSystemWidgetsBundles, idComparator);
Collections.sort(loadedWidgetsBundles, idComparator);
Assert.assertEquals(allSystemWidgetsBundles, loadedWidgetsBundles);
for (WidgetsBundle widgetsBundle : createdSystemWidgetsBundles) {
widgetsBundleService.deleteWidgetsBundle(widgetsBundle.getId());
}
loadedWidgetsBundles = widgetsBundleService.findAllTenantWidgetsBundlesByTenantId(tenantId);
Collections.sort(systemWidgetsBundles, idComparator);
Collections.sort(loadedWidgetsBundles, idComparator);
Assert.assertEquals(systemWidgetsBundles, loadedWidgetsBundles);
tenantService.deleteTenant(tenantId);
}
use of org.thingsboard.server.common.data.id.TenantId in project thingsboard by thingsboard.
the class BaseEventServiceTest method findEventsByTypeAndTimeDescOrder.
@Test
public void findEventsByTypeAndTimeDescOrder() throws Exception {
long timeBeforeStartTime = LocalDateTime.of(2016, Month.NOVEMBER, 1, 11, 30).toEpochSecond(ZoneOffset.UTC);
long startTime = LocalDateTime.of(2016, Month.NOVEMBER, 1, 12, 0).toEpochSecond(ZoneOffset.UTC);
long eventTime = LocalDateTime.of(2016, Month.NOVEMBER, 1, 12, 30).toEpochSecond(ZoneOffset.UTC);
long endTime = LocalDateTime.of(2016, Month.NOVEMBER, 1, 13, 0).toEpochSecond(ZoneOffset.UTC);
long timeAfterEndTime = LocalDateTime.of(2016, Month.NOVEMBER, 1, 13, 30).toEpochSecond(ZoneOffset.UTC);
RuleId ruleId = new RuleId(UUIDs.timeBased());
TenantId tenantId = new TenantId(UUIDs.timeBased());
saveEventWithProvidedTime(timeBeforeStartTime, ruleId, tenantId);
Event savedEvent = saveEventWithProvidedTime(eventTime, ruleId, tenantId);
Event savedEvent2 = saveEventWithProvidedTime(eventTime + 1, ruleId, tenantId);
Event savedEvent3 = saveEventWithProvidedTime(eventTime + 2, ruleId, tenantId);
saveEventWithProvidedTime(timeAfterEndTime, ruleId, tenantId);
TimePageData<Event> events = eventService.findEvents(tenantId, ruleId, DataConstants.STATS, new TimePageLink(2, startTime, endTime, false));
Assert.assertNotNull(events.getData());
Assert.assertTrue(events.getData().size() == 2);
Assert.assertTrue(events.getData().get(0).getUuidId().equals(savedEvent3.getUuidId()));
Assert.assertTrue(events.getData().get(1).getUuidId().equals(savedEvent2.getUuidId()));
Assert.assertTrue(events.hasNext());
Assert.assertNotNull(events.getNextPageLink());
events = eventService.findEvents(tenantId, ruleId, DataConstants.STATS, events.getNextPageLink());
Assert.assertNotNull(events.getData());
Assert.assertTrue(events.getData().size() == 1);
Assert.assertTrue(events.getData().get(0).getUuidId().equals(savedEvent.getUuidId()));
Assert.assertFalse(events.hasNext());
Assert.assertNull(events.getNextPageLink());
}
use of org.thingsboard.server.common.data.id.TenantId in project thingsboard by thingsboard.
the class RpcRestMsgHandler method handleHttpPostRequest.
@Override
public void handleHttpPostRequest(PluginContext ctx, PluginRestMsg msg) throws ServletException {
boolean valid = false;
RestRequest request = msg.getRequest();
try {
String[] pathParams = request.getPathParams();
if (pathParams.length == 2) {
String method = pathParams[0].toUpperCase();
if (DataConstants.ONEWAY.equals(method) || DataConstants.TWOWAY.equals(method)) {
final TenantId tenantId = ctx.getSecurityCtx().orElseThrow(() -> new IllegalStateException("Security context is empty!")).getTenantId();
JsonNode rpcRequestBody = jsonMapper.readTree(request.getRequestBody());
RpcRequest cmd = new RpcRequest(rpcRequestBody.get("method").asText(), jsonMapper.writeValueAsString(rpcRequestBody.get("params")));
if (rpcRequestBody.has("timeout")) {
cmd.setTimeout(rpcRequestBody.get("timeout").asLong());
}
boolean oneWay = DataConstants.ONEWAY.equals(method);
DeviceId deviceId = DeviceId.fromString(pathParams[1]);
valid = handleDeviceRPCRequest(ctx, msg, tenantId, deviceId, cmd, oneWay);
}
}
} catch (IOException e) {
log.debug("Failed to process POST request due to IO exception", e);
} catch (RuntimeException e) {
log.debug("Failed to process POST request due to Runtime exception", e);
}
if (!valid) {
msg.getResponseHolder().setResult(new ResponseEntity<>(HttpStatus.BAD_REQUEST));
}
}
use of org.thingsboard.server.common.data.id.TenantId in project thingsboard by thingsboard.
the class JpaAlarmDaoTest method testFindLatestByOriginatorAndType.
@Test
public void testFindLatestByOriginatorAndType() throws ExecutionException, InterruptedException {
System.out.println(System.currentTimeMillis());
UUID tenantId = UUID.fromString("d4b68f40-3e96-11e7-a884-898080180d6b");
UUID originator1Id = UUID.fromString("d4b68f41-3e96-11e7-a884-898080180d6b");
UUID originator2Id = UUID.fromString("d4b68f42-3e96-11e7-a884-898080180d6b");
UUID alarm1Id = UUID.fromString("d4b68f43-3e96-11e7-a884-898080180d6b");
UUID alarm2Id = UUID.fromString("d4b68f44-3e96-11e7-a884-898080180d6b");
UUID alarm3Id = UUID.fromString("d4b68f45-3e96-11e7-a884-898080180d6b");
saveAlarm(alarm1Id, tenantId, originator1Id, "TEST_ALARM");
saveAlarm(alarm2Id, tenantId, originator1Id, "TEST_ALARM");
saveAlarm(alarm3Id, tenantId, originator2Id, "TEST_ALARM");
assertEquals(3, alarmDao.find().size());
ListenableFuture<Alarm> future = alarmDao.findLatestByOriginatorAndType(new TenantId(tenantId), new DeviceId(originator1Id), "TEST_ALARM");
Alarm alarm = future.get();
assertNotNull(alarm);
assertEquals(alarm2Id, alarm.getId().getId());
}
use of org.thingsboard.server.common.data.id.TenantId 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);
}
Aggregations