Search in sources :

Example 1 with PageData

use of org.thingsboard.server.common.data.page.PageData in project thingsboard by thingsboard.

the class BaseTenantControllerTest method testFindTenants.

@Test
public void testFindTenants() throws Exception {
    loginSysAdmin();
    List<Tenant> tenants = new ArrayList<>();
    PageLink pageLink = new PageLink(17);
    PageData<Tenant> pageData = doGetTypedWithPageLink("/api/tenants?", new TypeReference<PageData<Tenant>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(1, pageData.getData().size());
    tenants.addAll(pageData.getData());
    for (int i = 0; i < 56; i++) {
        Tenant tenant = new Tenant();
        tenant.setTitle("Tenant" + i);
        tenants.add(doPost("/api/tenant", tenant, Tenant.class));
    }
    List<Tenant> loadedTenants = new ArrayList<>();
    pageLink = new PageLink(17);
    do {
        pageData = doGetTypedWithPageLink("/api/tenants?", new TypeReference<PageData<Tenant>>() {
        }, pageLink);
        loadedTenants.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(tenants, idComparator);
    Collections.sort(loadedTenants, idComparator);
    Assert.assertEquals(tenants, loadedTenants);
    for (Tenant tenant : loadedTenants) {
        if (!tenant.getTitle().equals(TEST_TENANT_NAME)) {
            doDelete("/api/tenant/" + tenant.getId().getId().toString()).andExpect(status().isOk());
        }
    }
    pageLink = new PageLink(17);
    pageData = doGetTypedWithPageLink("/api/tenants?", new TypeReference<PageData<Tenant>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(1, pageData.getData().size());
}
Also used : Tenant(org.thingsboard.server.common.data.Tenant) PageData(org.thingsboard.server.common.data.page.PageData) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 2 with PageData

use of org.thingsboard.server.common.data.page.PageData in project thingsboard by thingsboard.

the class BaseDeviceProfileControllerTest method testFindDeviceProfiles.

@Test
public void testFindDeviceProfiles() throws Exception {
    List<DeviceProfile> deviceProfiles = new ArrayList<>();
    PageLink pageLink = new PageLink(17);
    PageData<DeviceProfile> pageData = doGetTypedWithPageLink("/api/deviceProfiles?", new TypeReference<PageData<DeviceProfile>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(1, pageData.getTotalElements());
    deviceProfiles.addAll(pageData.getData());
    for (int i = 0; i < 28; i++) {
        DeviceProfile deviceProfile = this.createDeviceProfile("Device Profile" + i, null);
        deviceProfiles.add(doPost("/api/deviceProfile", deviceProfile, DeviceProfile.class));
    }
    List<DeviceProfile> loadedDeviceProfiles = new ArrayList<>();
    pageLink = new PageLink(17);
    do {
        pageData = doGetTypedWithPageLink("/api/deviceProfiles?", new TypeReference<PageData<DeviceProfile>>() {
        }, pageLink);
        loadedDeviceProfiles.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageLink.nextPageLink();
        }
    } while (pageData.hasNext());
    Collections.sort(deviceProfiles, idComparator);
    Collections.sort(loadedDeviceProfiles, idComparator);
    Assert.assertEquals(deviceProfiles, loadedDeviceProfiles);
    for (DeviceProfile deviceProfile : loadedDeviceProfiles) {
        if (!deviceProfile.isDefault()) {
            doDelete("/api/deviceProfile/" + deviceProfile.getId().getId().toString()).andExpect(status().isOk());
        }
    }
    pageLink = new PageLink(17);
    pageData = doGetTypedWithPageLink("/api/deviceProfiles?", new TypeReference<PageData<DeviceProfile>>() {
    }, pageLink);
    Assert.assertFalse(pageData.hasNext());
    Assert.assertEquals(1, pageData.getTotalElements());
}
Also used : DeviceProfile(org.thingsboard.server.common.data.DeviceProfile) PageData(org.thingsboard.server.common.data.page.PageData) ArrayList(java.util.ArrayList) PageLink(org.thingsboard.server.common.data.page.PageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 3 with PageData

use of org.thingsboard.server.common.data.page.PageData in project thingsboard by thingsboard.

the class BaseEntityViewControllerTest method testGetTenantEntityViewsByName.

@Test
public void testGetTenantEntityViewsByName() throws Exception {
    String name1 = "Entity view name1";
    List<EntityView> namesOfView1 = fillListOf(143, name1);
    List<EntityView> loadedNamesOfView1 = loadListOf(new PageLink(15, 0, name1), "/api/tenant/entityViews?");
    Collections.sort(namesOfView1, idComparator);
    Collections.sort(loadedNamesOfView1, idComparator);
    assertEquals(namesOfView1, loadedNamesOfView1);
    String name2 = "Entity view name2";
    List<EntityView> NamesOfView2 = fillListOf(75, name2);
    List<EntityView> loadedNamesOfView2 = loadListOf(new PageLink(4, 0, name2), "/api/tenant/entityViews?");
    Collections.sort(NamesOfView2, idComparator);
    Collections.sort(loadedNamesOfView2, idComparator);
    assertEquals(NamesOfView2, loadedNamesOfView2);
    for (EntityView view : loadedNamesOfView1) {
        doDelete("/api/entityView/" + view.getId().getId().toString()).andExpect(status().isOk());
    }
    PageData<EntityView> pageData = doGetTypedWithPageLink("/api/tenant/entityViews?", new TypeReference<PageData<EntityView>>() {
    }, new PageLink(4, 0, name1));
    Assert.assertFalse(pageData.hasNext());
    assertEquals(0, pageData.getData().size());
    for (EntityView view : loadedNamesOfView2) {
        doDelete("/api/entityView/" + view.getId().getId().toString()).andExpect(status().isOk());
    }
    pageData = doGetTypedWithPageLink("/api/tenant/entityViews?", new TypeReference<PageData<EntityView>>() {
    }, new PageLink(4, 0, name2));
    Assert.assertFalse(pageData.hasNext());
    assertEquals(0, pageData.getData().size());
}
Also used : PageData(org.thingsboard.server.common.data.page.PageData) AttributesEntityView(org.thingsboard.server.common.data.objects.AttributesEntityView) TelemetryEntityView(org.thingsboard.server.common.data.objects.TelemetryEntityView) EntityView(org.thingsboard.server.common.data.EntityView) PageLink(org.thingsboard.server.common.data.page.PageLink) Matchers.containsString(org.hamcrest.Matchers.containsString) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 4 with PageData

use of org.thingsboard.server.common.data.page.PageData in project thingsboard by thingsboard.

the class BaseEntityQueryControllerTest method testSimpleFindEntityDataByQuery.

@Test
public void testSimpleFindEntityDataByQuery() throws Exception {
    List<Device> devices = new ArrayList<>();
    for (int i = 0; i < 97; i++) {
        Device device = new Device();
        device.setName("Device" + i);
        device.setType("default");
        device.setLabel("testLabel" + (int) (Math.random() * 1000));
        devices.add(doPost("/api/device", device, Device.class));
        Thread.sleep(1);
    }
    DeviceTypeFilter filter = new DeviceTypeFilter();
    filter.setDeviceType("default");
    filter.setDeviceNameFilter("");
    EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
    EntityDataPageLink pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
    List<EntityKey> entityFields = Collections.singletonList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
    EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, null, null);
    PageData<EntityData> data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
    });
    Assert.assertEquals(97, data.getTotalElements());
    Assert.assertEquals(10, data.getTotalPages());
    Assert.assertTrue(data.hasNext());
    Assert.assertEquals(10, data.getData().size());
    List<EntityData> loadedEntities = new ArrayList<>(data.getData());
    while (data.hasNext()) {
        query = query.next();
        data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
        });
        loadedEntities.addAll(data.getData());
    }
    Assert.assertEquals(97, loadedEntities.size());
    List<EntityId> loadedIds = loadedEntities.stream().map(EntityData::getEntityId).collect(Collectors.toList());
    List<EntityId> deviceIds = devices.stream().map(Device::getId).collect(Collectors.toList());
    Assert.assertEquals(deviceIds, loadedIds);
    List<String> loadedNames = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue()).collect(Collectors.toList());
    List<String> deviceNames = devices.stream().map(Device::getName).collect(Collectors.toList());
    Assert.assertEquals(deviceNames, loadedNames);
    sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"), EntityDataSortOrder.Direction.DESC);
    pageLink = new EntityDataPageLink(10, 0, "device1", sortOrder);
    query = new EntityDataQuery(filter, pageLink, entityFields, null, null);
    data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
    });
    Assert.assertEquals(11, data.getTotalElements());
    Assert.assertEquals("Device19", data.getData().get(0).getLatest().get(EntityKeyType.ENTITY_FIELD).get("name").getValue());
    EntityTypeFilter filter2 = new EntityTypeFilter();
    filter2.setEntityType(EntityType.DEVICE);
    EntityDataSortOrder sortOrder2 = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
    EntityDataPageLink pageLink2 = new EntityDataPageLink(10, 0, null, sortOrder2);
    List<EntityKey> entityFields2 = Collections.singletonList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
    EntityDataQuery query2 = new EntityDataQuery(filter2, pageLink2, entityFields2, null, null);
    PageData<EntityData> data2 = doPostWithTypedResponse("/api/entitiesQuery/find", query2, new TypeReference<PageData<EntityData>>() {
    });
    Assert.assertEquals(97, data2.getTotalElements());
    Assert.assertEquals(10, data2.getTotalPages());
    Assert.assertTrue(data2.hasNext());
    Assert.assertEquals(10, data2.getData().size());
}
Also used : Device(org.thingsboard.server.common.data.Device) Tenant(org.thingsboard.server.common.data.Tenant) EntityTypeFilter(org.thingsboard.server.common.data.query.EntityTypeFilter) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) ArrayList(java.util.ArrayList) User(org.thingsboard.server.common.data.User) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) After(org.junit.After) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Before(org.junit.Before) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) DeviceId(org.thingsboard.server.common.data.id.DeviceId) DataConstants(org.thingsboard.server.common.data.DataConstants) EntityData(org.thingsboard.server.common.data.query.EntityData) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) Test(org.junit.Test) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Authority(org.thingsboard.server.common.data.security.Authority) Collectors(java.util.stream.Collectors) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) List(java.util.List) PageData(org.thingsboard.server.common.data.page.PageData) Assert(org.junit.Assert) Collections(java.util.Collections) Device(org.thingsboard.server.common.data.Device) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) ArrayList(java.util.ArrayList) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityTypeFilter(org.thingsboard.server.common.data.query.EntityTypeFilter) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) PageData(org.thingsboard.server.common.data.page.PageData) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Test(org.junit.Test)

Example 5 with PageData

use of org.thingsboard.server.common.data.page.PageData in project thingsboard by thingsboard.

the class BaseEntityQueryControllerTest method testFindEntityDataByQueryWithAttributes.

@Test
public void testFindEntityDataByQueryWithAttributes() throws Exception {
    List<Device> devices = new ArrayList<>();
    List<Long> temperatures = new ArrayList<>();
    List<Long> highTemperatures = new ArrayList<>();
    for (int i = 0; i < 67; i++) {
        Device device = new Device();
        String name = "Device" + i;
        device.setName(name);
        device.setType("default");
        device.setLabel("testLabel" + (int) (Math.random() * 1000));
        devices.add(doPost("/api/device?accessToken=" + name, device, Device.class));
        Thread.sleep(1);
        long temperature = (long) (Math.random() * 100);
        temperatures.add(temperature);
        if (temperature > 45) {
            highTemperatures.add(temperature);
        }
    }
    for (int i = 0; i < devices.size(); i++) {
        Device device = devices.get(i);
        String payload = "{\"temperature\":" + temperatures.get(i) + "}";
        doPost("/api/plugins/telemetry/" + device.getId() + "/" + DataConstants.SHARED_SCOPE, payload, String.class, status().isOk());
    }
    Thread.sleep(1000);
    DeviceTypeFilter filter = new DeviceTypeFilter();
    filter.setDeviceType("default");
    filter.setDeviceNameFilter("");
    EntityDataSortOrder sortOrder = new EntityDataSortOrder(new EntityKey(EntityKeyType.ENTITY_FIELD, "createdTime"), EntityDataSortOrder.Direction.ASC);
    EntityDataPageLink pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
    List<EntityKey> entityFields = Collections.singletonList(new EntityKey(EntityKeyType.ENTITY_FIELD, "name"));
    List<EntityKey> latestValues = Collections.singletonList(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
    EntityDataQuery query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, null);
    PageData<EntityData> data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
    });
    List<EntityData> loadedEntities = new ArrayList<>(data.getData());
    while (data.hasNext()) {
        query = query.next();
        data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
        });
        loadedEntities.addAll(data.getData());
    }
    Assert.assertEquals(67, loadedEntities.size());
    List<String> loadedTemperatures = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("temperature").getValue()).collect(Collectors.toList());
    List<String> deviceTemperatures = temperatures.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
    Assert.assertEquals(deviceTemperatures, loadedTemperatures);
    pageLink = new EntityDataPageLink(10, 0, null, sortOrder);
    KeyFilter highTemperatureFilter = new KeyFilter();
    highTemperatureFilter.setKey(new EntityKey(EntityKeyType.ATTRIBUTE, "temperature"));
    NumericFilterPredicate predicate = new NumericFilterPredicate();
    predicate.setValue(FilterPredicateValue.fromDouble(45));
    predicate.setOperation(NumericFilterPredicate.NumericOperation.GREATER);
    highTemperatureFilter.setPredicate(predicate);
    List<KeyFilter> keyFilters = Collections.singletonList(highTemperatureFilter);
    query = new EntityDataQuery(filter, pageLink, entityFields, latestValues, keyFilters);
    data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
    });
    loadedEntities = new ArrayList<>(data.getData());
    while (data.hasNext()) {
        query = query.next();
        data = doPostWithTypedResponse("/api/entitiesQuery/find", query, new TypeReference<PageData<EntityData>>() {
        });
        loadedEntities.addAll(data.getData());
    }
    Assert.assertEquals(highTemperatures.size(), loadedEntities.size());
    List<String> loadedHighTemperatures = loadedEntities.stream().map(entityData -> entityData.getLatest().get(EntityKeyType.ATTRIBUTE).get("temperature").getValue()).collect(Collectors.toList());
    List<String> deviceHighTemperatures = highTemperatures.stream().map(aLong -> Long.toString(aLong)).collect(Collectors.toList());
    Assert.assertEquals(deviceHighTemperatures, loadedHighTemperatures);
}
Also used : Device(org.thingsboard.server.common.data.Device) Tenant(org.thingsboard.server.common.data.Tenant) EntityTypeFilter(org.thingsboard.server.common.data.query.EntityTypeFilter) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) EntityCountQuery(org.thingsboard.server.common.data.query.EntityCountQuery) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) ArrayList(java.util.ArrayList) User(org.thingsboard.server.common.data.User) EntityListFilter(org.thingsboard.server.common.data.query.EntityListFilter) MockMvcResultMatchers.status(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status) After(org.junit.After) EntityId(org.thingsboard.server.common.data.id.EntityId) EntityType(org.thingsboard.server.common.data.EntityType) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) TypeReference(com.fasterxml.jackson.core.type.TypeReference) EntityKey(org.thingsboard.server.common.data.query.EntityKey) Before(org.junit.Before) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) DeviceId(org.thingsboard.server.common.data.id.DeviceId) DataConstants(org.thingsboard.server.common.data.DataConstants) EntityData(org.thingsboard.server.common.data.query.EntityData) FilterPredicateValue(org.thingsboard.server.common.data.query.FilterPredicateValue) EntityKeyType(org.thingsboard.server.common.data.query.EntityKeyType) Test(org.junit.Test) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) Authority(org.thingsboard.server.common.data.security.Authority) Collectors(java.util.stream.Collectors) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) List(java.util.List) PageData(org.thingsboard.server.common.data.page.PageData) Assert(org.junit.Assert) Collections(java.util.Collections) DeviceTypeFilter(org.thingsboard.server.common.data.query.DeviceTypeFilter) ArrayList(java.util.ArrayList) EntityKey(org.thingsboard.server.common.data.query.EntityKey) EntityDataSortOrder(org.thingsboard.server.common.data.query.EntityDataSortOrder) TypeReference(com.fasterxml.jackson.core.type.TypeReference) NumericFilterPredicate(org.thingsboard.server.common.data.query.NumericFilterPredicate) Device(org.thingsboard.server.common.data.Device) EntityDataPageLink(org.thingsboard.server.common.data.query.EntityDataPageLink) EntityData(org.thingsboard.server.common.data.query.EntityData) EntityDataQuery(org.thingsboard.server.common.data.query.EntityDataQuery) PageData(org.thingsboard.server.common.data.page.PageData) KeyFilter(org.thingsboard.server.common.data.query.KeyFilter) Test(org.junit.Test)

Aggregations

PageData (org.thingsboard.server.common.data.page.PageData)43 Test (org.junit.Test)31 ArrayList (java.util.ArrayList)28 List (java.util.List)28 Tenant (org.thingsboard.server.common.data.Tenant)26 PageLink (org.thingsboard.server.common.data.page.PageLink)25 Collectors (java.util.stream.Collectors)24 Edge (org.thingsboard.server.common.data.edge.Edge)23 TenantId (org.thingsboard.server.common.data.id.TenantId)23 After (org.junit.After)21 Assert (org.junit.Assert)21 Device (org.thingsboard.server.common.data.Device)21 EntityType (org.thingsboard.server.common.data.EntityType)21 Collections (java.util.Collections)20 EdgeId (org.thingsboard.server.common.data.id.EdgeId)20 Before (org.junit.Before)19 Autowired (org.springframework.beans.factory.annotation.Autowired)19 DataConstants (org.thingsboard.server.common.data.DataConstants)19 EntityId (org.thingsboard.server.common.data.id.EntityId)19 Slf4j (lombok.extern.slf4j.Slf4j)18