Skip to content

Commit 2592443

Browse files
author
Vali (Vasile Baluta)
committed
make test to wait until all events are processed
Change-Id: I7a72f635d7154e92f3dd36b478e69becb27f5716
1 parent dfbaafe commit 2592443

File tree

4 files changed

+93
-13
lines changed

4 files changed

+93
-13
lines changed

pom.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,16 @@
109109
<groupId>org.apache.qpid</groupId>
110110
<artifactId>qpid-broker</artifactId>
111111
<version>6.1.3</version>
112+
<scope>test</scope>
112113
</dependency>
114+
115+
<dependency>
116+
<groupId>de.flapdoodle.embed</groupId>
117+
<artifactId>de.flapdoodle.embed.mongo</artifactId>
118+
<version>2.0.0</version>
119+
<scope>test</scope>
120+
</dependency>
121+
113122
</dependencies>
114123

115124
<properties>

src/main/java/com/ericsson/ei/handlers/EventHandler.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,5 +28,11 @@ public void eventReceived(byte[] message) {
2828
String actualMessage = new String(message);
2929
log.info("Event received <" + actualMessage + ">");
3030
eventReceived(actualMessage);
31+
if (System.getProperty("flow.test") == "true") {
32+
String countStr = System.getProperty("eiffel.intelligence.processedEventsCount");
33+
int count = Integer.parseInt(countStr);
34+
count++;
35+
System.setProperty("eiffel.intelligence.processedEventsCount", "" + count);
36+
}
3137
}
3238
}

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

Lines changed: 77 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
package com.ericsson.ei.flowtests;
22

3+
import static org.junit.Assert.assertEquals;
4+
35
import java.io.File;
46
import java.io.IOException;
7+
import java.net.UnknownHostException;
58
import java.util.ArrayList;
69

710
import org.apache.commons.io.FileUtils;
@@ -14,11 +17,14 @@
1417
import org.junit.BeforeClass;
1518
import org.junit.Test;
1619
import org.junit.runner.RunWith;
20+
import org.slf4j.Logger;
21+
import org.slf4j.LoggerFactory;
1722
import org.springframework.beans.factory.annotation.Autowired;
1823
import org.springframework.boot.test.context.SpringBootTest;
1924
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2025

2126
import com.ericsson.ei.handlers.EventHandler;
27+
import com.ericsson.ei.handlers.ObjectHandler;
2228
import com.ericsson.ei.rmq.consumer.RmqConsumer;
2329
import com.fasterxml.jackson.databind.JsonNode;
2430
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -29,18 +35,30 @@
2935
import com.rabbitmq.client.DefaultConsumer;
3036
import com.rabbitmq.client.Envelope;
3137

38+
import de.flapdoodle.embed.mongo.MongodExecutable;
39+
import de.flapdoodle.embed.mongo.MongodProcess;
40+
import de.flapdoodle.embed.mongo.MongodStarter;
41+
import de.flapdoodle.embed.mongo.config.IMongodConfig;
42+
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
43+
import de.flapdoodle.embed.mongo.config.Net;
44+
import de.flapdoodle.embed.mongo.distribution.Version;
45+
import de.flapdoodle.embed.process.runtime.Network;
46+
3247
@RunWith(SpringJUnit4ClassRunner.class)
3348
@SpringBootTest
3449
public class FlowTest {
3550

36-
public static File qpidConfig = null;
37-
static AMQPBrokerManager amqpBrocker;
51+
private static Logger log = LoggerFactory.getLogger(FlowTest.class);
52+
53+
public static File qpidConfig = null;
54+
static AMQPBrokerManager amqpBrocker;
55+
static MongodExecutable mongodExecutable = null;
3856

39-
@Autowired
40-
RmqConsumer rmqConsumer;
57+
@Autowired
58+
RmqConsumer rmqConsumer;
4159

42-
@Autowired
43-
EventHandler eventHandler;
60+
@Autowired
61+
ObjectHandler objectHandler;
4462

4563
public static class AMQPBrokerManager {
4664
private String path;
@@ -71,13 +89,22 @@ public void stopBroker() {
7189
private static String jsonFileContent;
7290
private static JsonNode parsedJason;
7391
private static String jsonFilePath = "src/test/resources/test_events.json";
92+
static private final String inputFilePath = "src/test/resources/AggregatedDocument.json";
7493

7594
@BeforeClass
7695
public static void setup() throws Exception {
96+
System.setProperty("flow.test", "true");
97+
System.setProperty("eiffel.intelligence.processedEventsCount", "0");
98+
setUpMessageBus();
99+
setUpEmbeddedMongo();
100+
}
101+
102+
public static void setUpMessageBus() throws Exception {
77103
System.setProperty("rabbitmq.port", "8672");
78104
System.setProperty("rabbitmq.user", "guest");
79105
System.setProperty("rabbitmq.password", "guest");
80106

107+
81108
String config = "src/test/resources/configs/qpidConfig.json";
82109
jsonFileContent = FileUtils.readFileToString(new File(jsonFilePath));
83110
ObjectMapper objectmapper = new ObjectMapper();
@@ -90,20 +117,44 @@ public static void setup() throws Exception {
90117
cf.setPassword("guest");
91118
cf.setPort(8672);
92119
conn = cf.newConnection();
120+
}
121+
122+
public static void setUpEmbeddedMongo() throws Exception {
123+
int port = 12349;
124+
System.setProperty("mongodb.port", ""+port);
125+
126+
MongodStarter starter = MongodStarter.getDefaultInstance();
127+
128+
String bindIp = "localhost";
129+
130+
IMongodConfig mongodConfig = new MongodConfigBuilder()
131+
.version(Version.Main.PRODUCTION)
132+
.net(new Net(bindIp, port, Network.localhostIsIPv6()))
133+
.build();
93134

135+
136+
try {
137+
mongodExecutable = starter.prepare(mongodConfig);
138+
MongodProcess mongod = mongodExecutable.start();
139+
} catch (Exception e) {
140+
log.info(e.getMessage(),e);
141+
}
94142
}
95143

96144
@AfterClass
97145
public static void tearDown() throws Exception {
98146
amqpBrocker.stopBroker();
147+
148+
if (mongodExecutable != null)
149+
mongodExecutable.stop();
150+
99151
try {
100152
conn.close();
101153
} catch (Exception e) {
102154
//We try to close the connection but if
103155
//the connection is closed we just receive the
104156
//exception and go on
105157
}
106-
107158
}
108159

109160
@Test
@@ -113,25 +164,38 @@ public void test() {
113164
String queueName = rmqConsumer.getQueueName();
114165
String exchange = "ei-poc-4";
115166

116-
long count = channel.consumerCount(queueName);
117167
ArrayList<String> eventNames = getEventNamesToSend();
118-
168+
int eventsCount = eventNames.size();
119169
for(String eventName : eventNames) {
120170
JsonNode eventJson = parsedJason.get(eventName);
121171
String event = eventJson.toString();
122172
channel.basicPublish(exchange, queueName, null, event.getBytes());
123173
}
174+
175+
// wait for all events to be processed
176+
int processedEvents = 0;
177+
while (processedEvents < eventsCount) {
178+
String countStr = System.getProperty("eiffel.intelligence.processedEventsCount");
179+
processedEvents = Integer.parseInt(countStr);
180+
}
181+
182+
String document = objectHandler.findObjectById("6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43");
183+
String expectedDocument = FileUtils.readFileToString(new File(inputFilePath));
184+
ObjectMapper objectmapper = new ObjectMapper();
185+
JsonNode expectedJson = objectmapper.readTree(expectedDocument);
186+
JsonNode actualJson = objectmapper.readTree(document);
187+
String breakString = "breakHere";
188+
assertEquals(expectedJson, actualJson);
124189
} catch (Exception e) {
125-
// TODO Auto-generated catch block
126-
e.printStackTrace();
190+
log.info(e.getMessage(),e);
127191
}
128192
}
129193

130194
private ArrayList<String> getEventNamesToSend() {
131195
ArrayList<String> eventNames = new ArrayList<>();
132196
eventNames.add("event_EiffelArtifactCreatedEvent_3");
133-
// eventNames.add("event_EiffelArtifactPublishedEvent_3");
134-
// eventNames.add("event_EiffelConfidenceLevelModifiedEvent_3_2");
197+
eventNames.add("event_EiffelArtifactPublishedEvent_3");
198+
eventNames.add("event_EiffelConfidenceLevelModifiedEvent_3_2");
135199
eventNames.add("event_EiffelTestCaseStartedEvent_3");
136200
eventNames.add("event_EiffelTestCaseFinishedEvent_3");
137201

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "_id" : "6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43" , "aggregatedObject" : "{\"fileInformation\":[{\"extension\":\"jar\",\"classifier\":\"\"}],\"buildCommand\":null,\"confidenceLevels\":[{\"eventId\":\"f37d59a3-069e-4f4c-8cc5-a52e73501a75\",\"name\":\"readyForDelivery\",\"time\":1481875944272,\"value\":\"SUCCESS\"}],\"testCaseExecutions\":[{\"testCaseFinishEventId\":\"11109351-41e0-474a-bc1c-f6e81e58a1c9\",\"testCaseStartedTime\":1481875925916,\"testCaseStartedEventId\":\"cb9d64b0-a6e9-4419-8b5d-a650c27c59ca\",\"testCaseFinishedTime\":1481875935919,\"testCase\":{\"conclusion\":\"SUCCESSFUL\",\"verdict\":\"PASSED\",\"tracker\":\"My Other Test Management System\",\"id\":\"TC5\",\"uri\":\"https:\\/\\/other-tm.company.com\\/testCase\\/TC5\"}}],\"id\":\"6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43\",\"time\":1481875891763,\"type\":\"EiffelArtifactCreatedEvent\",\"gav\":{\"groupId\":\"com.mycompany.myproduct\",\"artifactId\":\"sub-system\",\"version\":\"1.1.0\"},\"publications\":[{\"eventId\":\"33d05e6f-9bd9-4138-83b6-e20cc74680a3\",\"locations\":[{\"type\":\"PLAIN\",\"uri\":\"https:\\/\\/myrepository.com\\/mySubSystemArtifact\"}],\"time\":1481875921763}]}"}

0 commit comments

Comments
 (0)