|
| 1 | +package com.iluwatar.transactionaloutbox; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | +import static org.mockito.ArgumentMatchers.any; |
| 7 | +import static org.mockito.ArgumentMatchers.anyString; |
| 8 | +import static org.mockito.ArgumentMatchers.eq; |
| 9 | +import static org.mockito.Mockito.doThrow; |
| 10 | +import static org.mockito.Mockito.never; |
| 11 | +import static org.mockito.Mockito.times; |
| 12 | +import static org.mockito.Mockito.verify; |
| 13 | +import static org.mockito.Mockito.when; |
| 14 | + |
| 15 | +import jakarta.persistence.EntityManager; |
| 16 | +import jakarta.persistence.EntityTransaction; |
| 17 | +import jakarta.persistence.LockModeType; |
| 18 | +import jakarta.persistence.TypedQuery; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.concurrent.TimeUnit; |
| 21 | +import org.junit.jupiter.api.BeforeEach; |
| 22 | +import org.junit.jupiter.api.Test; |
| 23 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 24 | +import org.mockito.Mock; |
| 25 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 26 | + |
| 27 | +/** Tests for {@link EventPoller}. */ |
| 28 | +@ExtendWith(MockitoExtension.class) |
| 29 | +class EventPollerTests { |
| 30 | + |
| 31 | + private static final String TEST_EVENT = "TEST_EVENT"; |
| 32 | + private static final String TEST_PAYLOAD = "payload"; |
| 33 | + |
| 34 | + @Mock private EntityManager entityManager; |
| 35 | + @Mock private EntityTransaction transaction; |
| 36 | + @Mock private MessageBroker messageBroker; |
| 37 | + @Mock private TypedQuery<OutboxEvent> query; |
| 38 | + private EventPoller eventPoller; |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + void setup() { |
| 42 | + when(entityManager.getTransaction()).thenReturn(transaction); |
| 43 | + when(entityManager.createQuery(anyString(), eq(OutboxEvent.class))).thenReturn(query); |
| 44 | + when(query.setMaxResults(any(Integer.class))).thenReturn(query); |
| 45 | + when(query.setLockMode(any(LockModeType.class))).thenReturn(query); |
| 46 | + eventPoller = new EventPoller(entityManager, messageBroker); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + void shouldProcessEventsSuccessfully() { |
| 51 | + var event = new OutboxEvent("EVENT_1", "payload1"); |
| 52 | + when(query.getResultList()).thenReturn(Collections.singletonList(event)); |
| 53 | + |
| 54 | + eventPoller.processOutboxEvents(); |
| 55 | + |
| 56 | + verify(messageBroker).sendMessage(event); |
| 57 | + verify(transaction).begin(); |
| 58 | + verify(transaction).commit(); |
| 59 | + assertEquals(1, eventPoller.getProcessedEventsCount()); |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + void shouldHandleFailureAndRetry() { |
| 64 | + var event = new OutboxEvent(TEST_EVENT, TEST_PAYLOAD); |
| 65 | + when(query.getResultList()).thenReturn(Collections.singletonList(event)); |
| 66 | + doThrow(new RuntimeException("First attempt")) |
| 67 | + .doNothing() |
| 68 | + .when(messageBroker) |
| 69 | + .sendMessage(any(OutboxEvent.class)); |
| 70 | + |
| 71 | + eventPoller.processOutboxEvents(); |
| 72 | + |
| 73 | + verify(messageBroker, times(2)).sendMessage(event); |
| 74 | + assertEquals(1, eventPoller.getProcessedEventsCount()); |
| 75 | + assertEquals(0, eventPoller.getFailedEventsCount()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void shouldHandleInterruptedThread() { |
| 80 | + var event = new OutboxEvent(TEST_EVENT, TEST_PAYLOAD); |
| 81 | + when(query.getResultList()).thenReturn(Collections.singletonList(event)); |
| 82 | + doThrow(new RuntimeException("Processing fails")) |
| 83 | + .when(messageBroker) |
| 84 | + .sendMessage(any(OutboxEvent.class)); |
| 85 | + Thread.currentThread().interrupt(); |
| 86 | + |
| 87 | + eventPoller.processOutboxEvents(); |
| 88 | + |
| 89 | + verify(transaction).begin(); |
| 90 | + verify(transaction).commit(); |
| 91 | + verify(messageBroker).sendMessage(event); |
| 92 | + assertEquals(0, eventPoller.getProcessedEventsCount()); |
| 93 | + assertEquals( |
| 94 | + 0, eventPoller.getFailedEventsCount(), "Interrupted events are not counted as failures"); |
| 95 | + assertTrue(Thread.interrupted(), "Interrupt flag should be preserved"); |
| 96 | + } |
| 97 | + |
| 98 | + @Test |
| 99 | + void shouldHandleEmptyEventList() { |
| 100 | + when(query.getResultList()).thenReturn(Collections.emptyList()); |
| 101 | + |
| 102 | + eventPoller.processOutboxEvents(); |
| 103 | + |
| 104 | + verify(transaction).begin(); |
| 105 | + verify(transaction).commit(); |
| 106 | + assertEquals(0, eventPoller.getProcessedEventsCount()); |
| 107 | + assertEquals(0, eventPoller.getFailedEventsCount()); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void shouldHandleMaxRetryAttempts() { |
| 112 | + var event = new OutboxEvent(TEST_EVENT, TEST_PAYLOAD); |
| 113 | + when(query.getResultList()).thenReturn(Collections.singletonList(event)); |
| 114 | + doThrow(new RuntimeException("Failed processing")) |
| 115 | + .when(messageBroker) |
| 116 | + .sendMessage(any(OutboxEvent.class)); |
| 117 | + |
| 118 | + eventPoller.processOutboxEvents(); |
| 119 | + |
| 120 | + verify(messageBroker, times(3)).sendMessage(event); |
| 121 | + assertEquals(0, eventPoller.getProcessedEventsCount()); |
| 122 | + assertEquals(1, eventPoller.getFailedEventsCount()); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + void shouldProcessMultipleEvents() { |
| 127 | + var event1 = new OutboxEvent("EVENT_1", "payload1"); |
| 128 | + var event2 = new OutboxEvent("EVENT_2", "payload2"); |
| 129 | + when(query.getResultList()).thenReturn(java.util.Arrays.asList(event1, event2)); |
| 130 | + |
| 131 | + eventPoller.processOutboxEvents(); |
| 132 | + |
| 133 | + verify(messageBroker).sendMessage(event1); |
| 134 | + verify(messageBroker).sendMessage(event2); |
| 135 | + verify(transaction).begin(); |
| 136 | + verify(transaction).commit(); |
| 137 | + assertEquals(2, eventPoller.getProcessedEventsCount()); |
| 138 | + assertEquals(0, eventPoller.getFailedEventsCount()); |
| 139 | + } |
| 140 | + |
| 141 | + @Test |
| 142 | + void shouldRollbackTransactionWhenRepositoryFails() { |
| 143 | + var dbException = new RuntimeException("Database connection failed"); |
| 144 | + when(query.getResultList()).thenThrow(dbException); |
| 145 | + |
| 146 | + eventPoller.processOutboxEvents(); |
| 147 | + |
| 148 | + verify(transaction).begin(); |
| 149 | + verify(transaction).rollback(); |
| 150 | + verify(transaction, never()).commit(); |
| 151 | + |
| 152 | + assertEquals(1, eventPoller.getFailedEventsCount()); |
| 153 | + assertEquals(0, eventPoller.getProcessedEventsCount()); |
| 154 | + } |
| 155 | + |
| 156 | + @Test |
| 157 | + void shouldStartAndStopCorrectly() throws Exception { |
| 158 | + eventPoller.start(); |
| 159 | + |
| 160 | + assertFalse(eventPoller.getScheduler().isShutdown()); |
| 161 | + assertFalse(eventPoller.getScheduler().isTerminated()); |
| 162 | + |
| 163 | + eventPoller.stop(); |
| 164 | + |
| 165 | + assertTrue(eventPoller.getScheduler().isShutdown()); |
| 166 | + assertTrue(eventPoller.getScheduler().awaitTermination(1, TimeUnit.SECONDS)); |
| 167 | + assertTrue(eventPoller.getScheduler().isTerminated()); |
| 168 | + } |
| 169 | +} |
0 commit comments