24
24
*/
25
25
package com .iluwatar .logaggregation ;
26
26
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 ;
27
30
import static org .mockito .ArgumentMatchers .any ;
28
31
import static org .mockito .Mockito .times ;
29
32
import static org .mockito .Mockito .verify ;
30
- // CRITICAL MISSING IMPORTS - FIXED!
31
- import static org .junit .jupiter .api .Assertions .*;
32
33
33
34
import java .time .LocalDateTime ;
34
- import java .util .concurrent .TimeUnit ;
35
- import org .junit .jupiter .api .BeforeEach ;
36
35
import org .junit .jupiter .api .AfterEach ;
36
+ import org .junit .jupiter .api .BeforeEach ;
37
37
import org .junit .jupiter .api .Test ;
38
38
import org .junit .jupiter .api .extension .ExtendWith ;
39
39
import org .mockito .Mock ;
40
40
import org .mockito .junit .jupiter .MockitoExtension ;
41
41
42
42
/**
43
- * FIXED Championship Test Suite - LogAggregator
44
- *
43
+ * FIXED Championship Test Suite - LogAggregator.
44
+ *
45
45
* Fixed to work with the actual LogAggregator API and proper imports
46
46
*/
47
47
@ ExtendWith (MockitoExtension .class )
48
48
class LogAggregatorTest {
49
-
50
- @ Mock
51
- private CentralLogStore centralLogStore ;
52
-
49
+
50
+ @ Mock private CentralLogStore centralLogStore ;
51
+
53
52
private LogAggregator logAggregator ;
54
53
55
54
@ BeforeEach
@@ -59,149 +58,132 @@ void setUp() {
59
58
60
59
@ AfterEach
61
60
void tearDown () throws InterruptedException {
62
- // 🚀 CHAMPIONSHIP CLEANUP - Properly shutdown the event-driven aggregator
63
61
if (logAggregator != null && logAggregator .isRunning ()) {
64
62
logAggregator .stop ();
65
63
logAggregator .awaitShutdown ();
66
64
}
67
65
}
68
66
69
67
@ Test
70
- void whenThreeInfoLogsAreCollected_thenCentralLogStoreShouldStoreAllOfThem () throws InterruptedException {
71
- // ELITE FIX: Account for asynchronous threshold-based flushing
68
+ void whenThreeInfoLogsAreCollected_thenCentralLogStoreShouldStoreAllOfThem ()
69
+ throws InterruptedException {
72
70
logAggregator .collectLog (createLogEntry (LogLevel .INFO , "Sample log message 1" ));
73
71
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
+
76
73
assertEquals (2 , logAggregator .getLogCount ());
77
74
verifyNoInteractionsWithCentralLogStore ();
78
-
79
- // Third log should trigger immediate flush (threshold = 3)
75
+
80
76
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
+
85
80
verifyCentralLogStoreInvokedTimes (3 );
86
- assertEquals (0 , logAggregator .getLogCount ()); // Buffer should be empty after flush
81
+ assertEquals (0 , logAggregator .getLogCount ());
87
82
}
88
83
89
84
@ Test
90
85
void whenDebugLogIsCollected_thenNoLogsShouldBeStored () throws InterruptedException {
91
86
logAggregator .collectLog (createLogEntry (LogLevel .DEBUG , "Sample debug log message" ));
92
-
93
- // Debug log should be filtered out before reaching buffer
87
+
94
88
assertEquals (0 , logAggregator .getLogCount ());
95
89
assertEquals (0 , logAggregator .getBufferSize ());
96
-
97
- // Wait a bit to ensure no delayed processing
90
+
98
91
Thread .sleep (500 );
99
-
92
+
100
93
verifyNoInteractionsWithCentralLogStore ();
101
94
}
102
95
103
96
@ Test
104
97
void whenTwoLogsCollected_thenBufferShouldContainThem () {
105
- // 🎯 NEW TEST: Verify buffer state management
106
98
logAggregator .collectLog (createLogEntry (LogLevel .INFO , "Message 1" ));
107
99
logAggregator .collectLog (createLogEntry (LogLevel .INFO , "Message 2" ));
108
-
100
+
109
101
assertEquals (2 , logAggregator .getLogCount ());
110
102
assertEquals (2 , logAggregator .getBufferSize ());
111
-
112
- // Should not trigger flush yet (threshold is 3)
103
+
113
104
verifyNoInteractionsWithCentralLogStore ();
114
105
}
115
106
116
107
@ Test
117
108
void whenScheduledFlushOccurs_thenBufferedLogsShouldBeStored () throws InterruptedException {
118
- // 🏆 NEW TEST: Verify scheduled periodic flushing
119
109
logAggregator .collectLog (createLogEntry (LogLevel .INFO , "Scheduled flush test" ));
120
-
110
+
121
111
assertEquals (1 , logAggregator .getLogCount ());
122
112
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
+
127
116
verifyCentralLogStoreInvokedTimes (1 );
128
117
assertEquals (0 , logAggregator .getLogCount ());
129
118
}
130
119
131
120
@ Test
132
121
void whenLogAggregatorStopped_thenRemainingLogsShouldBeStored () throws InterruptedException {
133
- // 🚀 NEW TEST: Verify graceful shutdown flushes remaining logs
134
122
logAggregator .collectLog (createLogEntry (LogLevel .INFO , "Final message 1" ));
135
123
logAggregator .collectLog (createLogEntry (LogLevel .INFO , "Final message 2" ));
136
-
124
+
137
125
assertEquals (2 , logAggregator .getLogCount ());
138
126
verifyNoInteractionsWithCentralLogStore ();
139
-
140
- // Stop should trigger final flush
127
+
141
128
logAggregator .stop ();
142
129
logAggregator .awaitShutdown ();
143
-
130
+
144
131
verifyCentralLogStoreInvokedTimes (2 );
145
132
assertEquals (0 , logAggregator .getLogCount ());
146
133
assertFalse (logAggregator .isRunning ());
147
134
}
148
135
149
136
@ Test
150
137
void whenLogLevelBelowThreshold_thenLogShouldBeFiltered () {
151
- // FIXED TEST: Only use available log levels
152
138
logAggregator .collectLog (createLogEntry (LogLevel .DEBUG , "Debug message" ));
153
-
139
+
154
140
assertEquals (0 , logAggregator .getLogCount ());
155
141
assertEquals (0 , logAggregator .getBufferSize ());
156
142
verifyNoInteractionsWithCentralLogStore ();
157
143
}
158
144
159
145
@ Test
160
146
void whenLogLevelAtOrAboveThreshold_thenLogShouldBeAccepted () {
161
- // FIXED TEST: Use only available log levels (INFO, DEBUG, ERROR)
162
147
logAggregator .collectLog (createLogEntry (LogLevel .INFO , "Info message" ));
163
148
logAggregator .collectLog (createLogEntry (LogLevel .ERROR , "Error message" ));
164
-
149
+
165
150
assertEquals (2 , logAggregator .getLogCount ());
166
151
assertEquals (2 , logAggregator .getBufferSize ());
167
152
}
168
153
169
154
@ Test
170
155
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
+
174
159
logAggregator .collectLog (nullLevelEntry );
175
-
160
+
176
161
assertEquals (0 , logAggregator .getLogCount ());
177
162
verifyNoInteractionsWithCentralLogStore ();
178
163
}
179
164
180
165
@ Test
181
166
void whenLogAggregatorIsShutdown_thenNewLogsShouldBeRejected () throws InterruptedException {
182
- // NEW TEST: Verify shutdown behavior
183
167
logAggregator .stop ();
184
168
logAggregator .awaitShutdown ();
185
-
169
+
186
170
assertFalse (logAggregator .isRunning ());
187
-
188
- // Try to add log after shutdown
171
+
189
172
logAggregator .collectLog (createLogEntry (LogLevel .INFO , "Post-shutdown message" ));
190
-
173
+
191
174
assertEquals (0 , logAggregator .getLogCount ());
192
175
verifyNoInteractionsWithCentralLogStore ();
193
176
}
194
177
195
178
@ Test
196
179
void testBasicFunctionality () throws InterruptedException {
197
- // SIMPLIFIED TEST: Basic functionality without advanced features
198
180
assertTrue (logAggregator .isRunning ());
199
-
181
+
200
182
logAggregator .collectLog (createLogEntry (LogLevel .INFO , "Basic test" ));
201
183
assertEquals (1 , logAggregator .getLogCount ());
202
184
}
203
185
204
- private static LogEntry createLogEntry (LogLevel logLevel , String message ) {
186
+ private static LogEntry createLogEntry (LogLevel logLevel , String message ) {
205
187
return new LogEntry ("ServiceA" , logLevel , message , LocalDateTime .now ());
206
188
}
207
189
@@ -212,4 +194,4 @@ private void verifyNoInteractionsWithCentralLogStore() {
212
194
private void verifyCentralLogStoreInvokedTimes (int times ) {
213
195
verify (centralLogStore , times (times )).storeLog (any ());
214
196
}
215
- }
197
+ }
0 commit comments