Skip to content

Commit 43fb7f9

Browse files
author
Anders Breid
authored
Improve reliability of integration tests (#400)
* Improve reliability of integration tests
1 parent 7d18e38 commit 43fb7f9

File tree

5 files changed

+463
-231
lines changed

5 files changed

+463
-231
lines changed

src/integrationtests/java/com/ericsson/ei/integrationtests/flow/FlowStepsIT.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.util.Map;
1515
import java.util.concurrent.TimeUnit;
1616

17+
import org.apache.commons.lang3.StringUtils;
1718
import org.json.JSONArray;
1819
import org.json.JSONObject;
1920
import org.junit.Ignore;
@@ -239,19 +240,16 @@ public void jenkinsJobShouldBeDeleted() throws Throwable {
239240
public void mongodbShouldContainMails(int amountOfMails) throws Exception {
240241
long stopTime = System.currentTimeMillis() + 30000;
241242
Boolean mailHasBeenDelivered = false;
242-
long createdDateInMillis = 0;
243243

244244
while (mailHasBeenDelivered == false && stopTime > System.currentTimeMillis()) {
245245
JsonNode newestMailJson = getNewestMailFromDatabase();
246246

247247
if (newestMailJson != null) {
248-
JsonNode to = newestMailJson.get("to");
249-
assertEquals("Sent mails " + to.size() + ". Expected " + amountOfMails,
250-
amountOfMails, to.size());
251-
252-
String createdDate = newestMailJson.get("created").get("$date").asText();
248+
JsonNode recipients = newestMailJson.get("to");
249+
assertEquals("Number of recipients for test email",
250+
amountOfMails, recipients.size());
253251

254-
createdDateInMillis = ZonedDateTime.parse(createdDate).toInstant().toEpochMilli();
252+
long createdDateInMillis = getDateAsEpochMillis(newestMailJson);
255253
mailHasBeenDelivered = createdDateInMillis >= startTime;
256254
}
257255

@@ -350,6 +348,17 @@ private String extractValueForKeyInJobData(String key) {
350348
return value;
351349
}
352350

351+
private long getDateAsEpochMillis(JsonNode newestMailJson) {
352+
long createdDateInMillis = 0;
353+
String createdDate = newestMailJson.get("created").get("$date").asText();
354+
if (StringUtils.isNumeric(createdDate)) {
355+
createdDateInMillis = Long.parseLong(createdDate);
356+
} else {
357+
createdDateInMillis = ZonedDateTime.parse(createdDate).toInstant().toEpochMilli();
358+
}
359+
return createdDateInMillis;
360+
}
361+
353362
/**
354363
* Iterates the parameter array and returns the value if the key is found in given parameter
355364
* array.

src/integrationtests/java/util/IntegrationTestBase.java

Lines changed: 54 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
*/
1414
package util;
1515

16+
import static org.junit.Assert.fail;
17+
1618
import java.io.File;
1719
import java.io.IOException;
1820
import java.net.URISyntaxException;
@@ -44,12 +46,17 @@
4446
import com.mongodb.client.MongoCollection;
4547
import com.mongodb.client.MongoDatabase;
4648

49+
import lombok.Setter;
50+
4751
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+
4857
private static final Logger LOGGER = LoggerFactory.getLogger(IntegrationTestBase.class);
49-
private static final String EIFFEL_INTELLIGENCE_DATABASE_NAME = "eiffel_intelligence";
5058

5159
protected RabbitTemplate rabbitTemplate;
52-
protected static final String MAILHOG_DATABASE_NAME = "mailhog";
5360
@Autowired
5461
protected MongoDBHandler mongoDBHandler;
5562
@Value("${ei.host:localhost}")
@@ -91,6 +98,13 @@ public abstract class IntegrationTestBase extends AbstractTestExecutionListener
9198
@Value("${sessions.collection.name}")
9299
private String sessionsCollectionName;
93100

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+
94108
private static ObjectMapper objectMapper = new ObjectMapper();
95109

96110
@PostConstruct
@@ -100,30 +114,18 @@ public void init() {
100114
}
101115

102116
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);
121124
}
122125

123126
/**
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
127129
*
128130
* @return
129131
*/
@@ -153,13 +155,19 @@ protected void sendEventsAndConfirm() throws Exception {
153155
TimeUnit.MILLISECONDS.sleep(firstEventWaitTime);
154156
alreadyExecuted = true;
155157
}
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);
156163
}
157164

158-
// wait for all events to be processed
159165
waitForEventsToBeProcessed(eventsCount);
160166
checkResult(getCheckData());
161167
} 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);
163171
}
164172
}
165173

@@ -180,8 +188,7 @@ protected void sendEventsAndConfirm() throws Exception {
180188
protected abstract List<String> getEventNamesToSend() throws IOException;
181189

182190
/**
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
185192
*
186193
*/
187194
protected abstract Map<String, JsonNode> getCheckData() throws IOException;
@@ -194,42 +201,45 @@ protected JsonNode getJSONFromFile(String filePath) throws IOException {
194201
/**
195202
* Wait for certain amount of events to be processed.
196203
*
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.
199205
* @return
200206
* @throws InterruptedException
201207
*/
202208
protected void waitForEventsToBeProcessed(int eventsCount) throws InterruptedException {
203209
// wait for all events to be processed
204-
long stopTime = System.currentTimeMillis() + 60000;
210+
long stopTime = System.currentTimeMillis() + SECONDS_30;
205211
long processedEvents = 0;
206212
while (processedEvents < eventsCount && stopTime > System.currentTimeMillis()) {
207213
processedEvents = countProcessedEvents(database, event_map);
208214
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));
210222
}
211223
}
212224

213225
/**
214226
* Counts documents that were processed
215227
*
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
218230
* @return amount of processed events
219231
*/
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();
223234
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();
226237
}
227238

228239
/**
229240
* Retrieves the result from EI and checks if it equals the expected data
230241
*
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
233243
* @return
234244
* @throws URISyntaxException
235245
* @throws IOException
@@ -248,24 +258,22 @@ private void checkResult(final Map<String, JsonNode> expectedData)
248258
String id = (String) pair.getKey();
249259
expectedJSON = (JsonNode) pair.getValue();
250260

251-
long maxWaitTime = 300000;
252-
long stopTime = System.currentTimeMillis() + maxWaitTime;
261+
long stopTime = System.currentTimeMillis() + SECONDS_30;
253262
while (!foundMatch && stopTime > System.currentTimeMillis()) {
254263
actualJSON = queryAggregatedObject(id);
255264

256265
/*
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
259268
*/
260269
try {
261270
JSONAssert.assertEquals(expectedJSON.toString(), actualJSON.toString(), false);
262271
foundMatch = true;
263272
} catch (AssertionError e) {
264-
TimeUnit.SECONDS.sleep(1);
273+
TimeUnit.MILLISECONDS.sleep(SECONDS_1);
265274
}
266275
}
267276
}
268-
269277
JSONAssert.assertEquals(expectedJSON.toString(), actualJSON.toString(), false);
270278
}
271279

src/test/java/com/ericsson/ei/flowtests/FlowTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ public class FlowTest extends FlowTestBase {
7171
private static final String UPSTREAM_RESULT_FILE = "upStreamResultFile.json";
7272
private static final String UPSTREAM_INPUT_FILE = "upStreamInput.json";
7373
private static final String EVENTS_FILE_PATH = "src/test/resources/ArtifactFlowTestEvents.json";
74-
private static final String AGGREGATED_OBJECT_FILE_PATH = "src/test/resources/AggregatedDocumentInternalCompositionLatestIT.json";
74+
private static final String AGGREGATED_OBJECT_FILE_PATH = "src/test/resources/AggregatedDocumentInternalCompositionLatestUnitTest.json";
7575
private static final String AGGREGATED_OBJECT_ID = "aacc3c87-75e0-4b6d-88f5-b1a5d4e62b43";
7676

7777
@Autowired

0 commit comments

Comments
 (0)