package com.common.service; import com.common.entity.IngressStatsEvent; import org.junit.jupiter.api.Test; import org.springframework.test.util.ReflectionTestUtils; import org.springframework.transaction.PlatformTransactionManager; import org.springframework.transaction.TransactionDefinition; import org.springframework.transaction.support.SimpleTransactionStatus; import org.springframework.jdbc.core.JdbcTemplate; import java.time.LocalDateTime; import java.util.Arrays; import java.util.ArrayList; import java.util.List; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verifyNoInteractions; import static org.mockito.Mockito.when; class IngressStatsProjectionWriterTest { @Test void aggregatesBatchByDeviceDateAndCollectorUsingStableMinMax() { LocalDateTime t1 = LocalDateTime.of(2026, 8, 13, 10, 0); LocalDateTime t2 = t1.plusMinutes(2); LocalDateTime previousDay = t1.minusDays(1); IngressStatsProjectionWriter.Aggregation result = IngressStatsProjectionWriter.aggregate(Arrays.asList( new IngressStatsEvent("a", 7L, 3L, t2), new IngressStatsEvent("b", 7L, 3L, t1), new IngressStatsEvent("a", 7L, 3L, t2), new IngressStatsEvent("c", 7L, 3L, previousDay), new IngressStatsEvent("d", 8L, 4L, t1))); assertEquals(3, result.devices.size()); assertEquals(2, result.collects.size()); assertEquals(previousDay, result.collects.get(3L).min); assertEquals(t2, result.collects.get(3L).max); } @Test void usesSameLockOrderRegardlessOfKafkaRecordOrder() { LocalDateTime time = LocalDateTime.of(2026, 8, 13, 10, 0); IngressStatsProjectionWriter.Aggregation result = IngressStatsProjectionWriter.aggregate(Arrays.asList( new IngressStatsEvent("z", 9L, 7L, time), new IngressStatsEvent("a", 2L, 3L, time), new IngressStatsEvent("b", 8L, 4L, time.minusDays(1)))); List devices = new ArrayList<>(); for (IngressStatsProjectionWriter.DeviceKey key : result.devices.keySet()) devices.add(key.deviceId); assertEquals(Arrays.asList(8L, 2L, 9L), devices); assertEquals(Arrays.asList(3L, 4L, 7L), new ArrayList<>(result.collects.keySet())); } @Test void directAsyncModeWritesOnlyOneOutboxEvent() { JdbcTemplate jdbc = mock(JdbcTemplate.class); PlatformTransactionManager manager = mock(PlatformTransactionManager.class); when(manager.getTransaction(any(TransactionDefinition.class))) .thenReturn(new SimpleTransactionStatus()); StatsProjectionOutboxWriter outbox = mock(StatsProjectionOutboxWriter.class); when(outbox.isEnabled()).thenReturn(true); IngressStatsProjectionWriter writer = new IngressStatsProjectionWriter( jdbc, manager, new StatsWriteModeProvider("DIRECT"), mock(StatsMaterializationPublisher.class), outbox); ReflectionTestUtils.setField(writer, "enabled", true); ReflectionTestUtils.setField(writer, "transactionTimeoutSeconds", 45); int groups = writer.writeBatch(Arrays.asList( new IngressStatsEvent("a", 7L, 3L, LocalDateTime.of(2026, 9, 12, 8, 0)), new IngressStatsEvent("b", 7L, 3L, LocalDateTime.of(2026, 9, 12, 8, 1)))); assertEquals(2, groups); verify(outbox).append(org.mockito.ArgumentMatchers.any(StatsProjectionOutboxEvent.class)); verifyNoInteractions(jdbc); verify(manager).commit(any()); } }