Search in sources :

Example 26 with TenantId

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);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) ArrayList(java.util.ArrayList) WidgetsBundle(org.thingsboard.server.common.data.widget.WidgetsBundle) Test(org.junit.Test)

Example 27 with 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());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) RuleId(org.thingsboard.server.common.data.id.RuleId) Event(org.thingsboard.server.common.data.Event) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) Test(org.junit.Test) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest)

Example 28 with TenantId

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));
    }
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) RestRequest(org.thingsboard.server.extensions.api.plugins.rest.RestRequest) DeviceId(org.thingsboard.server.common.data.id.DeviceId) RpcRequest(org.thingsboard.server.extensions.core.plugin.rpc.cmd.RpcRequest) ToDeviceRpcRequest(org.thingsboard.server.extensions.api.plugins.msg.ToDeviceRpcRequest) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException)

Example 29 with TenantId

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());
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) DeviceId(org.thingsboard.server.common.data.id.DeviceId) Alarm(org.thingsboard.server.common.data.alarm.Alarm) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Example 30 with TenantId

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

Aggregations

TenantId (org.thingsboard.server.common.data.id.TenantId)119 Test (org.junit.Test)44 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)38 CustomerId (org.thingsboard.server.common.data.id.CustomerId)30 ArrayList (java.util.ArrayList)26 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)24 Tenant (org.thingsboard.server.common.data.Tenant)23 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)23 PluginMetaData (org.thingsboard.server.common.data.plugin.PluginMetaData)16 Customer (org.thingsboard.server.common.data.Customer)14 User (org.thingsboard.server.common.data.User)14 WidgetsBundle (org.thingsboard.server.common.data.widget.WidgetsBundle)13 DeviceId (org.thingsboard.server.common.data.id.DeviceId)10 RuleMetaData (org.thingsboard.server.common.data.rule.RuleMetaData)10 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)10 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)10 UserId (org.thingsboard.server.common.data.id.UserId)8 TimePageLink (org.thingsboard.server.common.data.page.TimePageLink)8 IOException (java.io.IOException)7 PluginId (org.thingsboard.server.common.data.id.PluginId)7