Skip to content

Commit f0fe72f

Browse files
committed
minor formatting log aggregator test
1 parent c02dcea commit f0fe72f

File tree

1 file changed

+41
-59
lines changed

1 file changed

+41
-59
lines changed

microservices-log-aggregation/src/test/java/com/iluwatar/logaggregation/LogAggregatorTest.java

Lines changed: 41 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -24,32 +24,31 @@
2424
*/
2525
package com.iluwatar.logaggregation;
2626

27+
import static org.junit.jupiter.api.Assertions.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertFalse;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
2730
import static org.mockito.ArgumentMatchers.any;
2831
import static org.mockito.Mockito.times;
2932
import static org.mockito.Mockito.verify;
30-
// CRITICAL MISSING IMPORTS - FIXED!
31-
import static org.junit.jupiter.api.Assertions.*;
3233

3334
import java.time.LocalDateTime;
34-
import java.util.concurrent.TimeUnit;
35-
import org.junit.jupiter.api.BeforeEach;
3635
import org.junit.jupiter.api.AfterEach;
36+
import org.junit.jupiter.api.BeforeEach;
3737
import org.junit.jupiter.api.Test;
3838
import org.junit.jupiter.api.extension.ExtendWith;
3939
import org.mockito.Mock;
4040
import org.mockito.junit.jupiter.MockitoExtension;
4141

4242
/**
43-
* FIXED Championship Test Suite - LogAggregator
44-
*
43+
* FIXED Championship Test Suite - LogAggregator.
44+
*
4545
* Fixed to work with the actual LogAggregator API and proper imports
4646
*/
4747
@ExtendWith(MockitoExtension.class)
4848
class LogAggregatorTest {
49-
50-
@Mock
51-
private CentralLogStore centralLogStore;
52-
49+
50+
@Mock private CentralLogStore centralLogStore;
51+
5352
private LogAggregator logAggregator;
5453

5554
@BeforeEach
@@ -59,149 +58,132 @@ void setUp() {
5958

6059
@AfterEach
6160
void tearDown() throws InterruptedException {
62-
// 🚀 CHAMPIONSHIP CLEANUP - Properly shutdown the event-driven aggregator
6361
if (logAggregator != null && logAggregator.isRunning()) {
6462
logAggregator.stop();
6563
logAggregator.awaitShutdown();
6664
}
6765
}
6866

6967
@Test
70-
void whenThreeInfoLogsAreCollected_thenCentralLogStoreShouldStoreAllOfThem() throws InterruptedException {
71-
// ELITE FIX: Account for asynchronous threshold-based flushing
68+
void whenThreeInfoLogsAreCollected_thenCentralLogStoreShouldStoreAllOfThem()
69+
throws InterruptedException {
7270
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Sample log message 1"));
7371
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Sample log message 2"));
74-
75-
// At this point, we should have 2 logs in buffer, no flush yet
72+
7673
assertEquals(2, logAggregator.getLogCount());
7774
verifyNoInteractionsWithCentralLogStore();
78-
79-
// Third log should trigger immediate flush (threshold = 3)
75+
8076
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Sample log message 3"));
81-
82-
// CHAMPIONSHIP WAIT: Allow time for ScheduledExecutorService to process
83-
Thread.sleep(1000); // Give executor time to flush
84-
77+
78+
Thread.sleep(1000);
79+
8580
verifyCentralLogStoreInvokedTimes(3);
86-
assertEquals(0, logAggregator.getLogCount()); // Buffer should be empty after flush
81+
assertEquals(0, logAggregator.getLogCount());
8782
}
8883

8984
@Test
9085
void whenDebugLogIsCollected_thenNoLogsShouldBeStored() throws InterruptedException {
9186
logAggregator.collectLog(createLogEntry(LogLevel.DEBUG, "Sample debug log message"));
92-
93-
// Debug log should be filtered out before reaching buffer
87+
9488
assertEquals(0, logAggregator.getLogCount());
9589
assertEquals(0, logAggregator.getBufferSize());
96-
97-
// Wait a bit to ensure no delayed processing
90+
9891
Thread.sleep(500);
99-
92+
10093
verifyNoInteractionsWithCentralLogStore();
10194
}
10295

10396
@Test
10497
void whenTwoLogsCollected_thenBufferShouldContainThem() {
105-
// 🎯 NEW TEST: Verify buffer state management
10698
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Message 1"));
10799
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Message 2"));
108-
100+
109101
assertEquals(2, logAggregator.getLogCount());
110102
assertEquals(2, logAggregator.getBufferSize());
111-
112-
// Should not trigger flush yet (threshold is 3)
103+
113104
verifyNoInteractionsWithCentralLogStore();
114105
}
115106

116107
@Test
117108
void whenScheduledFlushOccurs_thenBufferedLogsShouldBeStored() throws InterruptedException {
118-
// 🏆 NEW TEST: Verify scheduled periodic flushing
119109
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Scheduled flush test"));
120-
110+
121111
assertEquals(1, logAggregator.getLogCount());
122112
verifyNoInteractionsWithCentralLogStore();
123-
124-
// Wait for scheduled flush (FLUSH_INTERVAL_SECONDS = 5)
125-
Thread.sleep(6000); // 5 seconds + buffer
126-
113+
114+
Thread.sleep(6000);
115+
127116
verifyCentralLogStoreInvokedTimes(1);
128117
assertEquals(0, logAggregator.getLogCount());
129118
}
130119

131120
@Test
132121
void whenLogAggregatorStopped_thenRemainingLogsShouldBeStored() throws InterruptedException {
133-
// 🚀 NEW TEST: Verify graceful shutdown flushes remaining logs
134122
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Final message 1"));
135123
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Final message 2"));
136-
124+
137125
assertEquals(2, logAggregator.getLogCount());
138126
verifyNoInteractionsWithCentralLogStore();
139-
140-
// Stop should trigger final flush
127+
141128
logAggregator.stop();
142129
logAggregator.awaitShutdown();
143-
130+
144131
verifyCentralLogStoreInvokedTimes(2);
145132
assertEquals(0, logAggregator.getLogCount());
146133
assertFalse(logAggregator.isRunning());
147134
}
148135

149136
@Test
150137
void whenLogLevelBelowThreshold_thenLogShouldBeFiltered() {
151-
// FIXED TEST: Only use available log levels
152138
logAggregator.collectLog(createLogEntry(LogLevel.DEBUG, "Debug message"));
153-
139+
154140
assertEquals(0, logAggregator.getLogCount());
155141
assertEquals(0, logAggregator.getBufferSize());
156142
verifyNoInteractionsWithCentralLogStore();
157143
}
158144

159145
@Test
160146
void whenLogLevelAtOrAboveThreshold_thenLogShouldBeAccepted() {
161-
// FIXED TEST: Use only available log levels (INFO, DEBUG, ERROR)
162147
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Info message"));
163148
logAggregator.collectLog(createLogEntry(LogLevel.ERROR, "Error message"));
164-
149+
165150
assertEquals(2, logAggregator.getLogCount());
166151
assertEquals(2, logAggregator.getBufferSize());
167152
}
168153

169154
@Test
170155
void whenNullLogLevelProvided_thenLogShouldBeSkipped() {
171-
// EDGE CASE TEST: Null safety
172-
LogEntry nullLevelEntry = new LogEntry("ServiceA", null, "Null level message", LocalDateTime.now());
173-
156+
LogEntry nullLevelEntry =
157+
new LogEntry("ServiceA", null, "Null level message", LocalDateTime.now());
158+
174159
logAggregator.collectLog(nullLevelEntry);
175-
160+
176161
assertEquals(0, logAggregator.getLogCount());
177162
verifyNoInteractionsWithCentralLogStore();
178163
}
179164

180165
@Test
181166
void whenLogAggregatorIsShutdown_thenNewLogsShouldBeRejected() throws InterruptedException {
182-
// NEW TEST: Verify shutdown behavior
183167
logAggregator.stop();
184168
logAggregator.awaitShutdown();
185-
169+
186170
assertFalse(logAggregator.isRunning());
187-
188-
// Try to add log after shutdown
171+
189172
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Post-shutdown message"));
190-
173+
191174
assertEquals(0, logAggregator.getLogCount());
192175
verifyNoInteractionsWithCentralLogStore();
193176
}
194177

195178
@Test
196179
void testBasicFunctionality() throws InterruptedException {
197-
// SIMPLIFIED TEST: Basic functionality without advanced features
198180
assertTrue(logAggregator.isRunning());
199-
181+
200182
logAggregator.collectLog(createLogEntry(LogLevel.INFO, "Basic test"));
201183
assertEquals(1, logAggregator.getLogCount());
202184
}
203185

204-
private static LogEntry createLogEntry(LogLevel logLevel, String message) {
186+
private static LogEntry createLogEntry(LogLevel logLevel, String message) {
205187
return new LogEntry("ServiceA", logLevel, message, LocalDateTime.now());
206188
}
207189

@@ -212,4 +194,4 @@ private void verifyNoInteractionsWithCentralLogStore() {
212194
private void verifyCentralLogStoreInvokedTimes(int times) {
213195
verify(centralLogStore, times(times)).storeLog(any());
214196
}
215-
}
197+
}

0 commit comments

Comments
 (0)