Search in sources :

Example 16 with TimePageLink

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

the class BaseAlarmServiceTest method testFindAlarm.

@Test
public void testFindAlarm() throws ExecutionException, InterruptedException {
    AssetId parentId = new AssetId(UUIDs.timeBased());
    AssetId childId = new AssetId(UUIDs.timeBased());
    EntityRelation relation = new EntityRelation(parentId, childId, EntityRelation.CONTAINS_TYPE);
    Assert.assertTrue(relationService.saveRelationAsync(relation).get());
    long ts = System.currentTimeMillis();
    Alarm alarm = Alarm.builder().tenantId(tenantId).originator(childId).type(TEST_ALARM).propagate(false).severity(AlarmSeverity.CRITICAL).status(AlarmStatus.ACTIVE_UNACK).startTs(ts).build();
    Alarm created = alarmService.createOrUpdateAlarm(alarm);
    // Check child relation
    TimePageData<AlarmInfo> alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(childId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    // Check parent relation
    alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(parentId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(0, alarms.getData().size());
    created.setPropagate(true);
    created = alarmService.createOrUpdateAlarm(created);
    // Check child relation
    alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(childId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    // Check parent relation
    alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(parentId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    alarmService.ackAlarm(created.getId(), System.currentTimeMillis()).get();
    created = alarmService.findAlarmByIdAsync(created.getId()).get();
    alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(childId).status(AlarmStatus.ACTIVE_ACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
    // Check not existing relation
    alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(childId).status(AlarmStatus.ACTIVE_UNACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(0, alarms.getData().size());
    alarmService.clearAlarm(created.getId(), System.currentTimeMillis()).get();
    created = alarmService.findAlarmByIdAsync(created.getId()).get();
    alarms = alarmService.findAlarms(AlarmQuery.builder().affectedEntityId(childId).status(AlarmStatus.CLEARED_ACK).pageLink(new TimePageLink(1, 0L, System.currentTimeMillis(), false)).build()).get();
    Assert.assertNotNull(alarms.getData());
    Assert.assertEquals(1, alarms.getData().size());
    Assert.assertEquals(created, alarms.getData().get(0));
}
Also used : EntityRelation(org.thingsboard.server.common.data.relation.EntityRelation) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) AssetId(org.thingsboard.server.common.data.id.AssetId) Test(org.junit.Test)

Example 17 with TimePageLink

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

the class JpaBaseEventDaoTest method findEventsByEntityIdAndEventTypeAndPageLink.

@Test
public void findEventsByEntityIdAndEventTypeAndPageLink() {
    UUID tenantId = UUIDs.timeBased();
    UUID entityId1 = UUIDs.timeBased();
    UUID entityId2 = UUIDs.timeBased();
    long startTime = System.currentTimeMillis();
    long endTime = createEventsTwoEntitiesTwoTypes(tenantId, entityId1, entityId2, startTime, 20);
    TimePageLink pageLink1 = new TimePageLink(30, null, null, true);
    List<Event> events1 = eventDao.findEvents(tenantId, new DeviceId(entityId1), ALARM, pageLink1);
    assertEquals(5, events1.size());
    TimePageLink pageLink2 = new TimePageLink(30, startTime, null, true);
    List<Event> events2 = eventDao.findEvents(tenantId, new DeviceId(entityId1), ALARM, pageLink2);
    assertEquals(5, events2.size());
    TimePageLink pageLink3 = new TimePageLink(30, startTime, endTime, true);
    List<Event> events3 = eventDao.findEvents(tenantId, new DeviceId(entityId1), ALARM, pageLink3);
    assertEquals(5, events3.size());
    TimePageLink pageLink4 = new TimePageLink(4, startTime, endTime, true);
    List<Event> events4 = eventDao.findEvents(tenantId, new DeviceId(entityId1), ALARM, pageLink4);
    assertEquals(4, events4.size());
    UUID idOffset = events3.get(2).getId().getId();
    TimePageLink pageLink5 = new TimePageLink(10, startTime, endTime, true, idOffset);
    List<Event> events5 = eventDao.findEvents(tenantId, new DeviceId(entityId1), ALARM, pageLink5);
    assertEquals(2, events5.size());
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) Event(org.thingsboard.server.common.data.Event) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Example 18 with TimePageLink

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

the class JpaBaseEventDaoTest method findEventsByEntityIdAndPageLink.

@Test
public void findEventsByEntityIdAndPageLink() {
    UUID tenantId = UUIDs.timeBased();
    UUID entityId1 = UUIDs.timeBased();
    UUID entityId2 = UUIDs.timeBased();
    long startTime = System.currentTimeMillis();
    long endTime = createEventsTwoEntities(tenantId, entityId1, entityId2, startTime, 20);
    TimePageLink pageLink1 = new TimePageLink(30, null, null, true);
    List<Event> events1 = eventDao.findEvents(tenantId, new DeviceId(entityId1), pageLink1);
    assertEquals(10, events1.size());
    TimePageLink pageLink2 = new TimePageLink(30, startTime, null, true);
    List<Event> events2 = eventDao.findEvents(tenantId, new DeviceId(entityId1), pageLink2);
    assertEquals(10, events2.size());
    TimePageLink pageLink3 = new TimePageLink(30, startTime, endTime, true);
    List<Event> events3 = eventDao.findEvents(tenantId, new DeviceId(entityId1), pageLink3);
    assertEquals(10, events3.size());
    TimePageLink pageLink4 = new TimePageLink(5, startTime, endTime, true);
    List<Event> events4 = eventDao.findEvents(tenantId, new DeviceId(entityId1), pageLink4);
    assertEquals(5, events4.size());
    UUID idOffset = events4.get(4).getId().getId();
    TimePageLink pageLink5 = new TimePageLink(10, startTime, endTime, true, idOffset);
    List<Event> events5 = eventDao.findEvents(tenantId, new DeviceId(entityId1), pageLink5);
    assertEquals(5, events5.size());
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) Event(org.thingsboard.server.common.data.Event) TimePageLink(org.thingsboard.server.common.data.page.TimePageLink) UUID(java.util.UUID) AbstractJpaDaoTest(org.thingsboard.server.dao.AbstractJpaDaoTest) Test(org.junit.Test)

Example 19 with TimePageLink

use of org.thingsboard.server.common.data.page.TimePageLink 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)

Example 20 with TimePageLink

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

the class BaseAuditLogControllerTest method testAuditLogs_byTenantIdAndEntityId.

@Test
public void testAuditLogs_byTenantIdAndEntityId() throws Exception {
    Device device = new Device();
    device.setName("Device name");
    device.setType("default");
    Device savedDevice = doPost("/api/device", device, Device.class);
    for (int i = 0; i < 178; i++) {
        savedDevice.setName("Device name" + i);
        doPost("/api/device", savedDevice, Device.class);
    }
    List<AuditLog> loadedAuditLogs = new ArrayList<>();
    TimePageLink pageLink = new TimePageLink(23);
    TimePageData<AuditLog> pageData;
    do {
        pageData = doGetTypedWithTimePageLink("/api/audit/logs/entity/DEVICE/" + savedDevice.getId().getId() + "?", new TypeReference<TimePageData<AuditLog>>() {
        }, pageLink);
        loadedAuditLogs.addAll(pageData.getData());
        if (pageData.hasNext()) {
            pageLink = pageData.getNextPageLink();
        }
    } while (pageData.hasNext());
    Assert.assertEquals(179, 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

TimePageLink (org.thingsboard.server.common.data.page.TimePageLink)20 Test (org.junit.Test)9 TenantId (org.thingsboard.server.common.data.id.TenantId)8 PreAuthorize (org.springframework.security.access.prepost.PreAuthorize)7 ThingsboardException (org.thingsboard.server.exception.ThingsboardException)7 ArrayList (java.util.ArrayList)6 CustomerId (org.thingsboard.server.common.data.id.CustomerId)6 UUID (java.util.UUID)5 Event (org.thingsboard.server.common.data.Event)4 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 DashboardInfo (org.thingsboard.server.common.data.DashboardInfo)3 EntityRelation (org.thingsboard.server.common.data.relation.EntityRelation)3 AsyncFunction (com.google.common.util.concurrent.AsyncFunction)2 Futures (com.google.common.util.concurrent.Futures)2 ListenableFuture (com.google.common.util.concurrent.ListenableFuture)2 List (java.util.List)2 Slf4j (lombok.extern.slf4j.Slf4j)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Component (org.springframework.stereotype.Component)2 Device (org.thingsboard.server.common.data.Device)2