Search in sources :

Example 11 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class BaseDeviceServiceTest method testSaveDeviceWithEmptyTenant.

@Test(expected = DataValidationException.class)
public void testSaveDeviceWithEmptyTenant() {
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    deviceService.saveDevice(device);
}
Also used : Device(org.thingsboard.server.common.data.Device) Test(org.junit.Test)

Example 12 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class BaseDeviceServiceTest method testFindDevicesByTenantIdAndType.

@Test
public void testFindDevicesByTenantIdAndType() {
    String title1 = "Device title 1";
    String type1 = "typeA";
    List<Device> devicesType1 = new ArrayList<>();
    for (int i = 0; i < 143; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title1 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        device.setName(name);
        device.setType(type1);
        devicesType1.add(deviceService.saveDevice(device));
    }
    String title2 = "Device title 2";
    String type2 = "typeB";
    List<Device> devicesType2 = new ArrayList<>();
    for (int i = 0; i < 175; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        String suffix = RandomStringUtils.randomAlphanumeric(15);
        String name = title2 + suffix;
        name = i % 2 == 0 ? name.toLowerCase() : name.toUpperCase();
        device.setName(name);
        device.setType(type2);
        devicesType2.add(deviceService.saveDevice(device));
    }
    List<Device> loadedDevicesType1 = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(15);
    TextPageData<Device> pageData = null;
    do {
        pageData = deviceService.findDevicesByTenantIdAndType(tenantId, type1, pageLink);
        loadedDevicesType1.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesType1, idComparator);
    Collections.sort(loadedDevicesType1, idComparator);
    Assert.assertEquals(devicesType1, loadedDevicesType1);
    List<Device> loadedDevicesType2 = new ArrayList<>();
    pageLink = new TextPageLink(4);
    do {
        pageData = deviceService.findDevicesByTenantIdAndType(tenantId, type2, pageLink);
        loadedDevicesType2.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devicesType2, idComparator);
    Collections.sort(loadedDevicesType2, idComparator);
    Assert.assertEquals(devicesType2, loadedDevicesType2);
    for (Device device : loadedDevicesType1) {
        deviceService.deleteDevice(device.getId());
    }
    pageLink = new TextPageLink(4);
    pageData = deviceService.findDevicesByTenantIdAndType(tenantId, type1, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
    for (Device device : loadedDevicesType2) {
        deviceService.deleteDevice(device.getId());
    }
    pageLink = new TextPageLink(4);
    pageData = deviceService.findDevicesByTenantIdAndType(tenantId, type2, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(0, pageData.getData().size());
}
Also used : TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 13 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class BaseDeviceServiceTest method testAssignDeviceToCustomerFromDifferentTenant.

@Test(expected = DataValidationException.class)
public void testAssignDeviceToCustomerFromDifferentTenant() {
    Device device = new Device();
    device.setName("My device");
    device.setType("default");
    device.setTenantId(tenantId);
    device = deviceService.saveDevice(device);
    Tenant tenant = new Tenant();
    tenant.setTitle("Test different tenant");
    tenant = tenantService.saveTenant(tenant);
    Customer customer = new Customer();
    customer.setTenantId(tenant.getId());
    customer.setTitle("Test different customer");
    customer = customerService.saveCustomer(customer);
    try {
        deviceService.assignDeviceToCustomer(device.getId(), customer.getId());
    } finally {
        deviceService.deleteDevice(device.getId());
        tenantService.deleteTenant(tenant.getId());
    }
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) Customer(org.thingsboard.server.common.data.Customer) Device(org.thingsboard.server.common.data.Device) Test(org.junit.Test)

Example 14 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class DeviceActorMessageProcessor method initAttributes.

private void initAttributes() {
    // TODO: add invalidation of deviceType cache.
    Device device = systemContext.getDeviceService().findDeviceById(deviceId);
    this.deviceName = device.getName();
    this.deviceType = device.getType();
    this.deviceAttributes = new DeviceAttributes(fetchAttributes(DataConstants.CLIENT_SCOPE), fetchAttributes(DataConstants.SERVER_SCOPE), fetchAttributes(DataConstants.SHARED_SCOPE));
}
Also used : Device(org.thingsboard.server.common.data.Device)

Example 15 with Device

use of org.thingsboard.server.common.data.Device in project thingsboard by thingsboard.

the class DefaultActorServiceTest method testBasicPostWithSyncSession.

@Test
public void testBasicPostWithSyncSession() throws Exception {
    SessionContext ssnCtx = mock(SessionContext.class);
    KvEntry entry1 = new StringDataEntry("key1", "value1");
    KvEntry entry2 = new StringDataEntry("key2", "value2");
    BasicTelemetryUploadRequest telemetry = new BasicTelemetryUploadRequest();
    long ts = 42;
    telemetry.add(ts, entry1);
    telemetry.add(ts, entry2);
    BasicAdaptorToSessionActorMsg msg = new BasicAdaptorToSessionActorMsg(ssnCtx, telemetry);
    DeviceId deviceId = new DeviceId(UUID.randomUUID());
    DeviceCredentialsFilter filter = new DeviceTokenCredentials("token1");
    Device device = mock(Device.class);
    when(device.getId()).thenReturn(deviceId);
    when(device.getTenantId()).thenReturn(tenantId);
    when(ssnCtx.getSessionId()).thenReturn(new DummySessionID("session1"));
    when(ssnCtx.getSessionType()).thenReturn(SessionType.SYNC);
    when(deviceAuthService.process(filter)).thenReturn(DeviceAuthResult.of(deviceId));
    when(deviceService.findDeviceById(deviceId)).thenReturn(device);
    ObjectMapper ruleMapper = new ObjectMapper();
    when(ruleMock.getFilters()).thenReturn(ruleMapper.readTree(FILTERS_CONFIGURATION));
    when(ruleMock.getAction()).thenReturn(ruleMapper.readTree(ACTION_CONFIGURATION));
    ComponentDescriptor filterComp = new ComponentDescriptor();
    filterComp.setClazz("org.thingsboard.server.extensions.core.filter.MsgTypeFilter");
    filterComp.setType(ComponentType.FILTER);
    when(componentService.getComponent("org.thingsboard.server.extensions.core.filter.MsgTypeFilter")).thenReturn(Optional.of(filterComp));
    ComponentDescriptor actionComp = new ComponentDescriptor();
    actionComp.setClazz("org.thingsboard.server.extensions.core.action.telemetry.TelemetryPluginAction");
    actionComp.setType(ComponentType.ACTION);
    when(componentService.getComponent("org.thingsboard.server.extensions.core.action.telemetry.TelemetryPluginAction")).thenReturn(Optional.of(actionComp));
    ObjectMapper pluginMapper = new ObjectMapper();
    JsonNode pluginAdditionalInfo = pluginMapper.readTree(PLUGIN_CONFIGURATION);
    when(pluginMock.getConfiguration()).thenReturn(pluginAdditionalInfo);
    when(pluginMock.getClazz()).thenReturn(TelemetryStoragePlugin.class.getName());
    when(attributesService.findAll(deviceId, DataConstants.CLIENT_SCOPE)).thenReturn(Futures.immediateFuture(Collections.emptyList()));
    when(attributesService.findAll(deviceId, DataConstants.SHARED_SCOPE)).thenReturn(Futures.immediateFuture(Collections.emptyList()));
    when(attributesService.findAll(deviceId, DataConstants.SERVER_SCOPE)).thenReturn(Futures.immediateFuture(Collections.emptyList()));
    initActorSystem();
    Thread.sleep(1000);
    actorService.process(new BasicToDeviceActorSessionMsg(device, msg));
    // Check that device data was saved to DB;
    List<TsKvEntry> expected = new ArrayList<>();
    expected.add(new BasicTsKvEntry(ts, entry1));
    expected.add(new BasicTsKvEntry(ts, entry2));
    verify(tsService, Mockito.timeout(5000)).save(deviceId, expected, 0L);
}
Also used : BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) Device(org.thingsboard.server.common.data.Device) ComponentDescriptor(org.thingsboard.server.common.data.plugin.ComponentDescriptor) BasicTsKvEntry(org.thingsboard.server.common.data.kv.BasicTsKvEntry) TsKvEntry(org.thingsboard.server.common.data.kv.TsKvEntry) KvEntry(org.thingsboard.server.common.data.kv.KvEntry) TelemetryStoragePlugin(org.thingsboard.server.extensions.core.plugin.telemetry.TelemetryStoragePlugin) JsonNode(com.fasterxml.jackson.databind.JsonNode) DeviceCredentialsFilter(org.thingsboard.server.common.data.security.DeviceCredentialsFilter) StringDataEntry(org.thingsboard.server.common.data.kv.StringDataEntry) BasicTelemetryUploadRequest(org.thingsboard.server.common.msg.core.BasicTelemetryUploadRequest) DeviceTokenCredentials(org.thingsboard.server.common.data.security.DeviceTokenCredentials) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Aggregations

Device (org.thingsboard.server.common.data.Device)53 Test (org.junit.Test)31 DeviceCredentials (org.thingsboard.server.common.data.security.DeviceCredentials)18 ArrayList (java.util.ArrayList)11 DeviceId (org.thingsboard.server.common.data.id.DeviceId)11 CustomerId (org.thingsboard.server.common.data.id.CustomerId)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 Customer (org.thingsboard.server.common.data.Customer)7 TextPageLink (org.thingsboard.server.common.data.page.TextPageLink)7 IncorrectParameterException (org.thingsboard.server.dao.exception.IncorrectParameterException)7 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)7 TenantId (org.thingsboard.server.common.data.id.TenantId)6 UUID (java.util.UUID)5 AbstractControllerTest (org.thingsboard.server.controller.AbstractControllerTest)4 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)4 Tenant (org.thingsboard.server.common.data.Tenant)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 List (java.util.List)2 Before (org.junit.Before)2