Search in sources :

Example 11 with Event

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

the class BaseEventServiceTest method saveEventWithProvidedTime.

private Event saveEventWithProvidedTime(long time, EntityId entityId, TenantId tenantId) throws IOException {
    Event event = generateEvent(tenantId, entityId, DataConstants.STATS, null);
    event.setId(new EventId(UUIDs.startOf(time)));
    return eventService.save(event);
}
Also used : Event(org.thingsboard.server.common.data.Event) EventId(org.thingsboard.server.common.data.id.EventId)

Example 12 with Event

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

the class BaseEventServiceTest method findEventsByTypeAndTimeAscOrder.

@Test
public void findEventsByTypeAndTimeAscOrder() 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, true));
    Assert.assertNotNull(events.getData());
    Assert.assertTrue(events.getData().size() == 2);
    Assert.assertTrue(events.getData().get(0).getUuidId().equals(savedEvent.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(savedEvent3.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 13 with Event

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

the class BaseEventServiceTest method saveEventIfNotExists.

@Test
public void saveEventIfNotExists() throws Exception {
    DeviceId devId = new DeviceId(UUIDs.timeBased());
    Event event = generateEvent(null, devId, "ALARM", UUIDs.timeBased().toString());
    Optional<Event> saved = eventService.saveIfNotExists(event);
    Assert.assertTrue(saved.isPresent());
    saved = eventService.saveIfNotExists(event);
    Assert.assertFalse(saved.isPresent());
}
Also used : DeviceId(org.thingsboard.server.common.data.id.DeviceId) Event(org.thingsboard.server.common.data.Event) Test(org.junit.Test) AbstractServiceTest(org.thingsboard.server.dao.service.AbstractServiceTest)

Example 14 with Event

use of org.thingsboard.server.common.data.Event 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 15 with Event

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

the class JpaBaseEventDaoTest method createEventsTwoEntitiesTwoTypes.

private long createEventsTwoEntitiesTwoTypes(UUID tenantId, UUID entityId1, UUID entityId2, long startTime, int count) {
    for (int i = 0; i < count / 2; i++) {
        String type = i % 2 == 0 ? STATS : ALARM;
        UUID eventId1 = UUIDs.timeBased();
        Event event1 = getEvent(eventId1, tenantId, entityId1, type);
        eventDao.save(event1);
        UUID eventId2 = UUIDs.timeBased();
        Event event2 = getEvent(eventId2, tenantId, entityId2, type);
        eventDao.save(event2);
    }
    return System.currentTimeMillis();
}
Also used : Event(org.thingsboard.server.common.data.Event) UUID(java.util.UUID)

Aggregations

Event (org.thingsboard.server.common.data.Event)20 Test (org.junit.Test)8 UUID (java.util.UUID)6 DeviceId (org.thingsboard.server.common.data.id.DeviceId)6 TenantId (org.thingsboard.server.common.data.id.TenantId)6 EventId (org.thingsboard.server.common.data.id.EventId)4 TimePageLink (org.thingsboard.server.common.data.page.TimePageLink)4 AbstractJpaDaoTest (org.thingsboard.server.dao.AbstractJpaDaoTest)4 AbstractServiceTest (org.thingsboard.server.dao.service.AbstractServiceTest)4 RuleId (org.thingsboard.server.common.data.id.RuleId)2 ComponentLifecycleEvent (org.thingsboard.server.common.data.plugin.ComponentLifecycleEvent)2 JsonNode (com.fasterxml.jackson.databind.JsonNode)1 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 DatabaseSetup (com.github.springtestdbunit.annotation.DatabaseSetup)1 IOException (java.io.IOException)1 VelocityContext (org.apache.velocity.VelocityContext)1 EntityId (org.thingsboard.server.common.data.id.EntityId)1