13
13
*/
14
14
package util ;
15
15
16
+ import static org .junit .Assert .fail ;
17
+
16
18
import java .io .File ;
17
19
import java .io .IOException ;
18
20
import java .net .URISyntaxException ;
44
46
import com .mongodb .client .MongoCollection ;
45
47
import com .mongodb .client .MongoDatabase ;
46
48
49
+ import lombok .Setter ;
50
+
47
51
public abstract class IntegrationTestBase extends AbstractTestExecutionListener {
52
+ private static final int SECONDS_1 = 1000 ;
53
+ private static final int SECONDS_30 = 30000 ;
54
+ private static final int DEFAULT_DELAY_BETWEEN_SENDING_EVENTS = 350 ;
55
+ protected static final String MAILHOG_DATABASE_NAME = "mailhog" ;
56
+
48
57
private static final Logger LOGGER = LoggerFactory .getLogger (IntegrationTestBase .class );
49
- private static final String EIFFEL_INTELLIGENCE_DATABASE_NAME = "eiffel_intelligence" ;
50
58
51
59
protected RabbitTemplate rabbitTemplate ;
52
- protected static final String MAILHOG_DATABASE_NAME = "mailhog" ;
53
60
@ Autowired
54
61
protected MongoDBHandler mongoDBHandler ;
55
62
@ Value ("${ei.host:localhost}" )
@@ -91,6 +98,13 @@ public abstract class IntegrationTestBase extends AbstractTestExecutionListener
91
98
@ Value ("${sessions.collection.name}" )
92
99
private String sessionsCollectionName ;
93
100
101
+ /*
102
+ * setFirstEventWaitTime: variable to set the wait time after publishing the first event. So any
103
+ * thread looking for the events don't do it before actually populating events in the database
104
+ */
105
+ @ Setter
106
+ private int firstEventWaitTime = 0 ;
107
+
94
108
private static ObjectMapper objectMapper = new ObjectMapper ();
95
109
96
110
@ PostConstruct
@@ -100,30 +114,18 @@ public void init() {
100
114
}
101
115
102
116
private void cleanDatabases () {
103
- mongoDBHandler .dropCollection (EIFFEL_INTELLIGENCE_DATABASE_NAME , aggregatedCollectionName );
104
- mongoDBHandler .dropCollection (EIFFEL_INTELLIGENCE_DATABASE_NAME , waitlistCollectionName );
105
- mongoDBHandler .dropCollection (EIFFEL_INTELLIGENCE_DATABASE_NAME , subscriptionCollectionName );
106
- mongoDBHandler .dropCollection (EIFFEL_INTELLIGENCE_DATABASE_NAME , eventObjectMapCollectionName );
107
- mongoDBHandler .dropCollection (EIFFEL_INTELLIGENCE_DATABASE_NAME , subscriptionCollectionRepatFlagHandlerName );
108
- mongoDBHandler .dropCollection (EIFFEL_INTELLIGENCE_DATABASE_NAME , failedNotificationCollectionName );
109
- mongoDBHandler .dropCollection (EIFFEL_INTELLIGENCE_DATABASE_NAME , sessionsCollectionName );
110
- }
111
-
112
- /*
113
- * setFirstEventWaitTime: variable to set the wait time after publishing the
114
- * first event. So any thread looking for the events don't do it before actually
115
- * populating events in the database
116
- */
117
- private int firstEventWaitTime = 0 ;
118
-
119
- public void setFirstEventWaitTime (int value ) {
120
- firstEventWaitTime = value ;
117
+ mongoDBHandler .dropCollection (database , aggregatedCollectionName );
118
+ mongoDBHandler .dropCollection (database , waitlistCollectionName );
119
+ mongoDBHandler .dropCollection (database , subscriptionCollectionName );
120
+ mongoDBHandler .dropCollection (database , eventObjectMapCollectionName );
121
+ mongoDBHandler .dropCollection (database , subscriptionCollectionRepatFlagHandlerName );
122
+ mongoDBHandler .dropCollection (database , failedNotificationCollectionName );
123
+ mongoDBHandler .dropCollection (database , sessionsCollectionName );
121
124
}
122
125
123
126
/**
124
- * Override this if you have more events that will be registered to event to
125
- * object map but it is not visible in the test. For example from upstream or
126
- * downstream from event repository
127
+ * Override this if you have more events that will be registered to event to object map but it
128
+ * is not visible in the test. For example from upstream or downstream from event repository
127
129
*
128
130
* @return
129
131
*/
@@ -153,13 +155,19 @@ protected void sendEventsAndConfirm() throws Exception {
153
155
TimeUnit .MILLISECONDS .sleep (firstEventWaitTime );
154
156
alreadyExecuted = true ;
155
157
}
158
+ /**
159
+ * Without a small delay between the sending of 2 events, one may risk to be lost in
160
+ * an empty void if not received by EI
161
+ */
162
+ TimeUnit .MILLISECONDS .sleep (DEFAULT_DELAY_BETWEEN_SENDING_EVENTS );
156
163
}
157
164
158
- // wait for all events to be processed
159
165
waitForEventsToBeProcessed (eventsCount );
160
166
checkResult (getCheckData ());
161
167
} catch (IOException e ) {
162
- LOGGER .error (e .getMessage (), e );
168
+ String message = String .format ("Failed to send Eiffel messages. Reason: %s" , e .getMessage ());
169
+ LOGGER .error (message , e );
170
+ fail (message );
163
171
}
164
172
}
165
173
@@ -180,8 +188,7 @@ protected void sendEventsAndConfirm() throws Exception {
180
188
protected abstract List <String > getEventNamesToSend () throws IOException ;
181
189
182
190
/**
183
- * @return map, where key - _id of expected aggregated object value - expected
184
- * aggregated object
191
+ * @return map, where key - _id of expected aggregated object value - expected aggregated object
185
192
*
186
193
*/
187
194
protected abstract Map <String , JsonNode > getCheckData () throws IOException ;
@@ -194,42 +201,45 @@ protected JsonNode getJSONFromFile(String filePath) throws IOException {
194
201
/**
195
202
* Wait for certain amount of events to be processed.
196
203
*
197
- * @param eventsCount - An int which indicated how many events that should be
198
- * processed.
204
+ * @param eventsCount - An int which indicated how many events that should be processed.
199
205
* @return
200
206
* @throws InterruptedException
201
207
*/
202
208
protected void waitForEventsToBeProcessed (int eventsCount ) throws InterruptedException {
203
209
// wait for all events to be processed
204
- long stopTime = System .currentTimeMillis () + 60000 ;
210
+ long stopTime = System .currentTimeMillis () + SECONDS_30 ;
205
211
long processedEvents = 0 ;
206
212
while (processedEvents < eventsCount && stopTime > System .currentTimeMillis ()) {
207
213
processedEvents = countProcessedEvents (database , event_map );
208
214
LOGGER .debug ("Have gotten: " + processedEvents + " out of: " + eventsCount );
209
- TimeUnit .MILLISECONDS .sleep (1000 );
215
+ TimeUnit .MILLISECONDS .sleep (SECONDS_1 );
216
+ }
217
+
218
+ if (processedEvents < eventsCount ) {
219
+ fail (String .format (
220
+ "EI did not process all sent events. Processed '%s' events out of '%s' sent." ,
221
+ processedEvents , eventsCount ));
210
222
}
211
223
}
212
224
213
225
/**
214
226
* Counts documents that were processed
215
227
*
216
- * @param database - A string with the database to use
217
- * @param collection - A string with the collection to use
228
+ * @param database - A string with the database to use
229
+ * @param collectionName - A string with the collection to use
218
230
* @return amount of processed events
219
231
*/
220
- private long countProcessedEvents (String database , String collection ) {
221
- MongoClient mongoClient = null ;
222
- mongoClient = mongoDBHandler .getMongoClient ();
232
+ private long countProcessedEvents (String database , String collectionName ) {
233
+ MongoClient mongoClient = mongoDBHandler .getMongoClient ();
223
234
MongoDatabase db = mongoClient .getDatabase (database );
224
- MongoCollection table = db .getCollection (collection );
225
- return table .count ();
235
+ MongoCollection collection = db .getCollection (collectionName );
236
+ return collection .count ();
226
237
}
227
238
228
239
/**
229
240
* Retrieves the result from EI and checks if it equals the expected data
230
241
*
231
- * @param expectedData - A Map<String, JsonNode> which contains the expected
232
- * data
242
+ * @param expectedData - A Map<String, JsonNode> which contains the expected data
233
243
* @return
234
244
* @throws URISyntaxException
235
245
* @throws IOException
@@ -248,24 +258,22 @@ private void checkResult(final Map<String, JsonNode> expectedData)
248
258
String id = (String ) pair .getKey ();
249
259
expectedJSON = (JsonNode ) pair .getValue ();
250
260
251
- long maxWaitTime = 300000 ;
252
- long stopTime = System .currentTimeMillis () + maxWaitTime ;
261
+ long stopTime = System .currentTimeMillis () + SECONDS_30 ;
253
262
while (!foundMatch && stopTime > System .currentTimeMillis ()) {
254
263
actualJSON = queryAggregatedObject (id );
255
264
256
265
/*
257
- * This is a workaround for expectedJSON.equals(acutalJSON) as that does not
258
- * work with strict equalization
266
+ * This is a workaround for expectedJSON.equals(acutalJSON) as that does not work
267
+ * with strict equalization
259
268
*/
260
269
try {
261
270
JSONAssert .assertEquals (expectedJSON .toString (), actualJSON .toString (), false );
262
271
foundMatch = true ;
263
272
} catch (AssertionError e ) {
264
- TimeUnit .SECONDS .sleep (1 );
273
+ TimeUnit .MILLISECONDS .sleep (SECONDS_1 );
265
274
}
266
275
}
267
276
}
268
-
269
277
JSONAssert .assertEquals (expectedJSON .toString (), actualJSON .toString (), false );
270
278
}
271
279
0 commit comments