|
@@ -1,102 +1,99 @@
|
|
|
-package cz.senslog.analyzer.ws.manager;
|
|
|
|
|
-
|
|
|
|
|
-import cz.senslog.analyzer.domain.*;
|
|
|
|
|
-import cz.senslog.analyzer.storage.permanent.repository.StatisticsConfigRepository;
|
|
|
|
|
-import cz.senslog.analyzer.storage.permanent.repository.StatisticsRepository;
|
|
|
|
|
-import cz.senslog.analyzer.ws.dto.SensorStatisticsData;
|
|
|
|
|
-import cz.senslog.common.util.Tuple;
|
|
|
|
|
-import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
-import org.junit.jupiter.api.Test;
|
|
|
|
|
-
|
|
|
|
|
-import java.time.Instant;
|
|
|
|
|
-import java.time.LocalDateTime;
|
|
|
|
|
-import java.time.OffsetDateTime;
|
|
|
|
|
-import java.time.ZoneOffset;
|
|
|
|
|
-import java.time.temporal.ChronoUnit;
|
|
|
|
|
-import java.util.Arrays;
|
|
|
|
|
-import java.util.Collections;
|
|
|
|
|
-import java.util.HashSet;
|
|
|
|
|
-import java.util.List;
|
|
|
|
|
-
|
|
|
|
|
-import static cz.senslog.analyzer.domain.AggregationType.DOUBLE;
|
|
|
|
|
-import static java.time.temporal.ChronoUnit.*;
|
|
|
|
|
-import static java.util.Collections.emptySet;
|
|
|
|
|
-import static java.util.Collections.singletonList;
|
|
|
|
|
-import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
-import static org.mockito.ArgumentMatchers.*;
|
|
|
|
|
-import static org.mockito.Mockito.mock;
|
|
|
|
|
-import static org.mockito.Mockito.when;
|
|
|
|
|
-
|
|
|
|
|
-class WSStatisticsManagerTest {
|
|
|
|
|
-
|
|
|
|
|
- @BeforeEach
|
|
|
|
|
- void setUp() {
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
- private static WSStatisticsManager createManager(Group group, List<DoubleStatistics> doubleStatistics) {
|
|
|
|
|
- StatisticsConfigRepository.Special specialRepo = mock(StatisticsConfigRepository.Special.class);
|
|
|
|
|
- when(specialRepo.getGroupIdByUnitSensor(anyLong(), anyLong(), anyLong())).thenReturn(group.getId());
|
|
|
|
|
- StatisticsConfigRepository configRepo = mock(StatisticsConfigRepository.class);
|
|
|
|
|
- when(configRepo.special()).thenReturn(specialRepo);
|
|
|
|
|
-
|
|
|
|
|
- StatisticsRepository statisticsRepo = mock(StatisticsRepository.class);
|
|
|
|
|
- when(statisticsRepo.getByTimeRange(anyLong(), any(Tuple.class))).thenReturn(doubleStatistics);
|
|
|
|
|
- return new WSStatisticsManager(statisticsRepo, configRepo);
|
|
|
|
|
- }
|
|
|
|
|
-
|
|
|
|
|
-
|
|
|
|
|
- @Test
|
|
|
|
|
- void loadData() {
|
|
|
|
|
-
|
|
|
|
|
- long unitId = 0;
|
|
|
|
|
- long sensorId = 0;
|
|
|
|
|
-
|
|
|
|
|
- Group group = new Group(0, 3600, true, DOUBLE,
|
|
|
|
|
- new HashSet<>(singletonList(new Sensor(unitId, sensorId)))
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- LocalDateTime startDate = LocalDateTime.of(2020, 1, 1, 1, 1);
|
|
|
|
|
- Timestamp from = Timestamp.of(OffsetDateTime.of(startDate, ZoneOffset.UTC));
|
|
|
|
|
- Timestamp to = from.plus(1, ChronoUnit.MONTHS);
|
|
|
|
|
- IntervalGroup intervalGroup = IntervalGroup.DAY;
|
|
|
|
|
-
|
|
|
|
|
- WSStatisticsManager manager = createManager(group, Arrays.asList(
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(1, DAYS).plus(1, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(1, DAYS).plus(22, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(2, DAYS).plus(1, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(2, DAYS).plus(22, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(3, DAYS).plus(1, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(3, DAYS).plus(22, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(4, DAYS).plus(1, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(4, DAYS).plus(22, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(5, DAYS).plus(1, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(5, DAYS).plus(22, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(10, DAYS).plus(1, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(10, DAYS).plus(22, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(11, DAYS).plus(1, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(11, DAYS).plus(22, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(12, DAYS).plus(1, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(12, DAYS).plus(22, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(13, DAYS).plus(1, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(13, DAYS).plus(22, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(14, DAYS).plus(1, HOURS)),
|
|
|
|
|
- new DoubleStatistics(group, 2, 10, 20, 30, from.plus(14, DAYS).plus(22, HOURS))
|
|
|
|
|
- )
|
|
|
|
|
- );
|
|
|
|
|
-
|
|
|
|
|
- List<SensorStatisticsData> dataList = manager.loadData(unitId, sensorId, from, to, intervalGroup);
|
|
|
|
|
-
|
|
|
|
|
- assertEquals(1, dataList.size());
|
|
|
|
|
-
|
|
|
|
|
- SensorStatisticsData sensorData = dataList.get(0);
|
|
|
|
|
- assertEquals(10, sensorData.getData().size());
|
|
|
|
|
-
|
|
|
|
|
- SensorStatisticsData.Data statisticsAggr = sensorData.getStatistics();
|
|
|
|
|
- assertEquals(600, statisticsAggr.getSum());
|
|
|
|
|
- assertEquals(10, statisticsAggr.getMin());
|
|
|
|
|
- assertEquals(20, statisticsAggr.getMax());
|
|
|
|
|
- assertEquals(15, statisticsAggr.getAvg());
|
|
|
|
|
- }
|
|
|
|
|
|
|
+package cz.senslog.analyzer.ws.manager;
|
|
|
|
|
+
|
|
|
|
|
+import cz.senslog.analyzer.domain.*;
|
|
|
|
|
+import cz.senslog.analyzer.storage.permanent.repository.StatisticsConfigRepository;
|
|
|
|
|
+import cz.senslog.analyzer.storage.permanent.repository.StatisticsRepository;
|
|
|
|
|
+import cz.senslog.analyzer.ws.dto.SensorStatisticsData;
|
|
|
|
|
+import cz.senslog.analyzer.util.Tuple;
|
|
|
|
|
+import org.junit.jupiter.api.BeforeEach;
|
|
|
|
|
+import org.junit.jupiter.api.Test;
|
|
|
|
|
+
|
|
|
|
|
+import java.time.LocalDateTime;
|
|
|
|
|
+import java.time.OffsetDateTime;
|
|
|
|
|
+import java.time.ZoneOffset;
|
|
|
|
|
+import java.time.temporal.ChronoUnit;
|
|
|
|
|
+import java.util.Arrays;
|
|
|
|
|
+import java.util.HashSet;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+import static cz.senslog.analyzer.domain.AggregationType.DOUBLE;
|
|
|
|
|
+import static java.time.temporal.ChronoUnit.*;
|
|
|
|
|
+import static java.util.Collections.singletonList;
|
|
|
|
|
+import static org.junit.jupiter.api.Assertions.assertEquals;
|
|
|
|
|
+import static org.mockito.ArgumentMatchers.*;
|
|
|
|
|
+import static org.mockito.Mockito.mock;
|
|
|
|
|
+import static org.mockito.Mockito.when;
|
|
|
|
|
+
|
|
|
|
|
+class WSStatisticsManagerTest {
|
|
|
|
|
+
|
|
|
|
|
+ @BeforeEach
|
|
|
|
|
+ void setUp() {
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ private static WSStatisticsManager createManager(Group group, List<DoubleStatistics> doubleStatistics) {
|
|
|
|
|
+ StatisticsConfigRepository.Special specialRepo = mock(StatisticsConfigRepository.Special.class);
|
|
|
|
|
+ when(specialRepo.getGroupIdByUnitSensor(anyLong(), anyLong(), anyLong())).thenReturn(group.getId());
|
|
|
|
|
+ StatisticsConfigRepository configRepo = mock(StatisticsConfigRepository.class);
|
|
|
|
|
+ when(configRepo.special()).thenReturn(specialRepo);
|
|
|
|
|
+
|
|
|
|
|
+ StatisticsRepository statisticsRepo = mock(StatisticsRepository.class);
|
|
|
|
|
+ when(statisticsRepo.getByTimeRange(anyLong(), any(Tuple.class))).thenReturn(doubleStatistics);
|
|
|
|
|
+ return new WSStatisticsManager(statisticsRepo, configRepo);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @Test
|
|
|
|
|
+ void loadData() {
|
|
|
|
|
+
|
|
|
|
|
+ long unitId = 0;
|
|
|
|
|
+ long sensorId = 0;
|
|
|
|
|
+
|
|
|
|
|
+ Group group = new Group(0, 3600, true, DOUBLE,
|
|
|
|
|
+ new HashSet<>(singletonList(new Sensor(unitId, sensorId)))
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ LocalDateTime startDate = LocalDateTime.of(2020, 1, 1, 1, 1);
|
|
|
|
|
+ Timestamp from = Timestamp.of(OffsetDateTime.of(startDate, ZoneOffset.UTC));
|
|
|
|
|
+ Timestamp to = from.plus(1, ChronoUnit.MONTHS);
|
|
|
|
|
+ IntervalGroup intervalGroup = IntervalGroup.DAY;
|
|
|
|
|
+
|
|
|
|
|
+ WSStatisticsManager manager = createManager(group, Arrays.asList(
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(1, DAYS).plus(1, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(1, DAYS).plus(22, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(2, DAYS).plus(1, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(2, DAYS).plus(22, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(3, DAYS).plus(1, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(3, DAYS).plus(22, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(4, DAYS).plus(1, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(4, DAYS).plus(22, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(5, DAYS).plus(1, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(5, DAYS).plus(22, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(10, DAYS).plus(1, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(10, DAYS).plus(22, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(11, DAYS).plus(1, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(11, DAYS).plus(22, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(12, DAYS).plus(1, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(12, DAYS).plus(22, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(13, DAYS).plus(1, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(13, DAYS).plus(22, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(14, DAYS).plus(1, HOURS)),
|
|
|
|
|
+ new DoubleStatistics(group, 2, 10, 20, 30, from.plus(14, DAYS).plus(22, HOURS))
|
|
|
|
|
+ )
|
|
|
|
|
+ );
|
|
|
|
|
+
|
|
|
|
|
+ List<SensorStatisticsData> dataList = manager.loadData(unitId, sensorId, from, to, intervalGroup);
|
|
|
|
|
+
|
|
|
|
|
+ assertEquals(1, dataList.size());
|
|
|
|
|
+
|
|
|
|
|
+ SensorStatisticsData sensorData = dataList.get(0);
|
|
|
|
|
+ assertEquals(10, sensorData.getData().size());
|
|
|
|
|
+
|
|
|
|
|
+ SensorStatisticsData.Data statisticsAggr = sensorData.getStatistics();
|
|
|
|
|
+ assertEquals(600, statisticsAggr.getSum());
|
|
|
|
|
+ assertEquals(10, statisticsAggr.getMin());
|
|
|
|
|
+ assertEquals(20, statisticsAggr.getMax());
|
|
|
|
|
+ assertEquals(15, statisticsAggr.getAvg());
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|