Skip to content

Commit

Permalink
added some more ts unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytro-landiak committed Dec 11, 2024
1 parent 687f983 commit 069d860
Showing 1 changed file with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.thingsboard.mqtt.broker.common.data.BrokerConstants.ASC_ORDER;
import static org.thingsboard.mqtt.broker.common.data.BrokerConstants.DESC_ORDER;

Expand Down Expand Up @@ -75,6 +76,23 @@ public void testFindAllLatest() throws Exception {
assertEquals(TS, tsList.get(0).getTs());
}

@Test
public void testRemoveLatest() throws Exception {
String entityId = RandomStringUtils.randomAlphabetic(20);

saveEntries(entityId, TS - 2);
saveEntries(entityId, TS - 1);
saveEntries(entityId, TS);

List<TsKvEntry> tsList = tsService.findAllLatest(entityId).get(MAX_TIMEOUT, TimeUnit.SECONDS);
assertNotNull(tsList);

tsService.removeLatest(entityId, List.of(LONG_KEY)).get(MAX_TIMEOUT, TimeUnit.SECONDS);

tsList = tsService.findAllLatest(entityId).get(MAX_TIMEOUT, TimeUnit.SECONDS);
assertTrue(tsList.isEmpty());
}

@Test
public void testFindLatest() throws Exception {
String entityId = RandomStringUtils.randomAlphabetic(20);
Expand Down Expand Up @@ -328,6 +346,96 @@ public void testFindDeviceTsData() throws Exception {
assertEquals(Optional.of(2L), list.get(2).getLongValue());
}

@Test
public void testFindDeviceTsDataDesc() throws Exception {
String entityId = RandomStringUtils.randomAlphabetic(20);

save(entityId, 5000, 100);
save(entityId, 15000, 200);

save(entityId, 25000, 300);
save(entityId, 35000, 400);

save(entityId, 45000, 500);
save(entityId, 55000, 600);

List<TsKvEntry> list = tsService.findAll(entityId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 0,
60000, 20000, 3, Aggregation.NONE))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
assertEquals(3, list.size());
assertEquals(55000, list.get(0).getTs());
assertEquals(Optional.of(600L), list.get(0).getLongValue());

assertEquals(45000, list.get(1).getTs());
assertEquals(Optional.of(500L), list.get(1).getLongValue());

assertEquals(35000, list.get(2).getTs());
assertEquals(Optional.of(400L), list.get(2).getLongValue());

list = tsService.findAll(entityId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 0,
60000, 20000, 3, Aggregation.AVG, DESC_ORDER))).get(MAX_TIMEOUT, TimeUnit.SECONDS);
assertEquals(3, list.size());
assertEquals(10000, list.get(2).getTs());
assertEquals(Optional.of(150.0), list.get(2).getDoubleValue());

assertEquals(30000, list.get(1).getTs());
assertEquals(Optional.of(350.0), list.get(1).getDoubleValue());

assertEquals(50000, list.get(0).getTs());
assertEquals(Optional.of(550.0), list.get(0).getDoubleValue());

list = tsService.findAll(entityId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 0,
60000, 20000, 3, Aggregation.SUM, DESC_ORDER))).get(MAX_TIMEOUT, TimeUnit.SECONDS);

assertEquals(3, list.size());
assertEquals(10000, list.get(2).getTs());
assertEquals(Optional.of(300L), list.get(2).getLongValue());

assertEquals(30000, list.get(1).getTs());
assertEquals(Optional.of(700L), list.get(1).getLongValue());

assertEquals(50000, list.get(0).getTs());
assertEquals(Optional.of(1100L), list.get(0).getLongValue());

list = tsService.findAll(entityId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 0,
60000, 20000, 3, Aggregation.MIN, DESC_ORDER))).get(MAX_TIMEOUT, TimeUnit.SECONDS);

assertEquals(3, list.size());
assertEquals(10000, list.get(2).getTs());
assertEquals(Optional.of(100L), list.get(2).getLongValue());

assertEquals(30000, list.get(1).getTs());
assertEquals(Optional.of(300L), list.get(1).getLongValue());

assertEquals(50000, list.get(0).getTs());
assertEquals(Optional.of(500L), list.get(0).getLongValue());

list = tsService.findAll(entityId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 0,
60000, 20000, 3, Aggregation.MAX, DESC_ORDER))).get(MAX_TIMEOUT, TimeUnit.SECONDS);

assertEquals(3, list.size());
assertEquals(10000, list.get(2).getTs());
assertEquals(Optional.of(200L), list.get(2).getLongValue());

assertEquals(30000, list.get(1).getTs());
assertEquals(Optional.of(400L), list.get(1).getLongValue());

assertEquals(50000, list.get(0).getTs());
assertEquals(Optional.of(600L), list.get(0).getLongValue());

list = tsService.findAll(entityId, Collections.singletonList(new BaseReadTsKvQuery(LONG_KEY, 0,
60000, 20000, 3, Aggregation.COUNT, DESC_ORDER))).get(MAX_TIMEOUT, TimeUnit.SECONDS);

assertEquals(3, list.size());
assertEquals(10000, list.get(2).getTs());
assertEquals(Optional.of(2L), list.get(2).getLongValue());

assertEquals(30000, list.get(1).getTs());
assertEquals(Optional.of(2L), list.get(1).getLongValue());

assertEquals(50000, list.get(0).getTs());
assertEquals(Optional.of(2L), list.get(0).getLongValue());
}

@Test
public void testSaveTs_RemoveTs_AndSaveTsAgain() throws Exception {
String entityId = RandomStringUtils.randomAlphabetic(20);
Expand Down

0 comments on commit 069d860

Please sign in to comment.