Search in sources :

Example 46 with Device

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

the class BaseDeviceServiceTest method testFindDeviceById.

@Test
public void testFindDeviceById() {
    Device device = new Device();
    device.setTenantId(tenantId);
    device.setName("My device");
    device.setType("default");
    Device savedDevice = deviceService.saveDevice(device);
    Device foundDevice = deviceService.findDeviceById(savedDevice.getId());
    Assert.assertNotNull(foundDevice);
    Assert.assertEquals(savedDevice, foundDevice);
    deviceService.deleteDevice(savedDevice.getId());
}
Also used : Device(org.thingsboard.server.common.data.Device) Test(org.junit.Test)

Example 47 with Device

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

the class BaseDeviceServiceTest method testDeleteDevice.

@Test
public void testDeleteDevice() {
    Device device = new Device();
    device.setTenantId(tenantId);
    device.setName("My device");
    device.setType("default");
    Device savedDevice = deviceService.saveDevice(device);
    Device foundDevice = deviceService.findDeviceById(savedDevice.getId());
    Assert.assertNotNull(foundDevice);
    deviceService.deleteDevice(savedDevice.getId());
    foundDevice = deviceService.findDeviceById(savedDevice.getId());
    Assert.assertNull(foundDevice);
    DeviceCredentials foundDeviceCredentials = deviceCredentialsService.findDeviceCredentialsByDeviceId(savedDevice.getId());
    Assert.assertNull(foundDeviceCredentials);
}
Also used : Device(org.thingsboard.server.common.data.Device) DeviceCredentials(org.thingsboard.server.common.data.security.DeviceCredentials) Test(org.junit.Test)

Example 48 with Device

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

the class BaseDeviceServiceTest method testFindDevicesByTenantId.

@Test
public void testFindDevicesByTenantId() {
    Tenant tenant = new Tenant();
    tenant.setTitle("Test tenant");
    tenant = tenantService.saveTenant(tenant);
    TenantId tenantId = tenant.getId();
    List<Device> devices = new ArrayList<>();
    for (int i = 0; i < 178; i++) {
        Device device = new Device();
        device.setTenantId(tenantId);
        device.setName("Device" + i);
        device.setType("default");
        devices.add(deviceService.saveDevice(device));
    }
    List<Device> loadedDevices = new ArrayList<>();
    TextPageLink pageLink = new TextPageLink(23);
    TextPageData<Device> pageData = null;
    do {
        pageData = deviceService.findDevicesByTenantId(tenantId, pageLink);
        loadedDevices.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(devices, idComparator);
    Collections.sort(loadedDevices, idComparator);
    Assert.assertEquals(devices, loadedDevices);
    deviceService.deleteDevicesByTenantId(tenantId);
    pageLink = new TextPageLink(33);
    pageData = deviceService.findDevicesByTenantId(tenantId, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertTrue(pageData.getData().isEmpty());
    tenantService.deleteTenant(tenantId);
}
Also used : TenantId(org.thingsboard.server.common.data.id.TenantId) Tenant(org.thingsboard.server.common.data.Tenant) TextPageLink(org.thingsboard.server.common.data.page.TextPageLink) Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) Test(org.junit.Test)

Example 49 with Device

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

the class BaseDeviceServiceTest method testSaveDeviceWithEmptyName.

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

Example 50 with Device

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

the class BaseAuditLogControllerTest method testAuditLogs.

@Test
public void testAuditLogs() throws Exception {
    for (int i = 0; i < 178; i++) {
        Device device = new Device();
        device.setName("Device" + i);
        device.setType("default");
        doPost("/api/device", device, Device.class);
    }
    List<AuditLog> loadedAuditLogs = new ArrayList<>();
    TimePageLink pageLink = new TimePageLink(23);
    TimePageData<AuditLog> pageData;
    do {
        pageData = doGetTypedWithTimePageLink("/api/audit/logs?", new TypeReference<TimePageData<AuditLog>>() {
        }, pageLink);
        loadedAuditLogs.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Assert.assertEquals(178, loadedAuditLogs.size());
    loadedAuditLogs = new ArrayList<>();
    pageLink = new TimePageLink(23);
    do {
        pageData = doGetTypedWithTimePageLink("/api/audit/logs/customer/" + ModelConstants.NULL_UUID + "?", new TypeReference<TimePageData<AuditLog>>() {
        }, pageLink);
        loadedAuditLogs.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Assert.assertEquals(178, loadedAuditLogs.size());
    loadedAuditLogs = new ArrayList<>();
    pageLink = new TimePageLink(23);
    do {
        pageData = doGetTypedWithTimePageLink("/api/audit/logs/user/" + tenantAdmin.getId().getId().toString() + "?", new TypeReference<TimePageData<AuditLog>>() {
        }, pageLink);
        loadedAuditLogs.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Assert.assertEquals(178, loadedAuditLogs.size());
}
Also used : Device(org.thingsboard.server.common.data.Device) ArrayList(java.util.ArrayList) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) AuditLog(org.thingsboard.server.common.data.audit.AuditLog) 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