61 lines
1.9 KiB
Java
61 lines
1.9 KiB
Java
|
|
package com.common.service;
|
||
|
|
|
||
|
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||
|
|
import static org.mockito.ArgumentMatchers.any;
|
||
|
|
import static org.mockito.ArgumentMatchers.anyString;
|
||
|
|
import static org.mockito.Mockito.mock;
|
||
|
|
import static org.mockito.Mockito.times;
|
||
|
|
import static org.mockito.Mockito.verify;
|
||
|
|
|
||
|
|
import java.time.LocalDateTime;
|
||
|
|
import java.util.Arrays;
|
||
|
|
import java.util.HashMap;
|
||
|
|
import java.util.Map;
|
||
|
|
import org.junit.jupiter.api.Test;
|
||
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||
|
|
import org.springframework.test.util.ReflectionTestUtils;
|
||
|
|
|
||
|
|
class LateAlarmBucketWriterTest {
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void groupsRowsByDeviceAndEventMinute() {
|
||
|
|
JdbcTemplate jdbcTemplate = mock(JdbcTemplate.class);
|
||
|
|
LateAlarmBucketWriter writer = new LateAlarmBucketWriter(jdbcTemplate);
|
||
|
|
ReflectionTestUtils.setField(writer, "enabled", true);
|
||
|
|
|
||
|
|
writer.record(
|
||
|
|
Arrays.asList(
|
||
|
|
row(126, LocalDateTime.of(2026, 9, 4, 10, 1, 2), 3),
|
||
|
|
row(126, LocalDateTime.of(2026, 9, 4, 10, 1, 59), 4),
|
||
|
|
row(126, LocalDateTime.of(2026, 9, 4, 10, 2, 1), 5)));
|
||
|
|
|
||
|
|
verify(jdbcTemplate, times(2))
|
||
|
|
.update(
|
||
|
|
anyString(),
|
||
|
|
any(),
|
||
|
|
any(),
|
||
|
|
any(),
|
||
|
|
any(),
|
||
|
|
any(),
|
||
|
|
any());
|
||
|
|
}
|
||
|
|
|
||
|
|
@Test
|
||
|
|
void rejectsAlarmRowsWithoutStableTimes() {
|
||
|
|
LateAlarmBucketWriter writer = new LateAlarmBucketWriter(mock(JdbcTemplate.class));
|
||
|
|
ReflectionTestUtils.setField(writer, "enabled", true);
|
||
|
|
Map<String, Object> invalid = new HashMap<>();
|
||
|
|
invalid.put("device_id", 126);
|
||
|
|
|
||
|
|
assertThrows(IllegalArgumentException.class, () -> writer.record(Arrays.asList(invalid)));
|
||
|
|
}
|
||
|
|
|
||
|
|
private Map<String, Object> row(int deviceId, LocalDateTime eventTime, int receiveSecond) {
|
||
|
|
Map<String, Object> row = new HashMap<>();
|
||
|
|
row.put("device_id", deviceId);
|
||
|
|
row.put("log_time", eventTime);
|
||
|
|
row.put("created_at", LocalDateTime.of(2026, 9, 5, 1, 0, receiveSecond));
|
||
|
|
return row;
|
||
|
|
}
|
||
|
|
}
|