1
1
package com .ericsson .ei .flowtests ;
2
2
3
+ import static org .junit .Assert .assertEquals ;
4
+
3
5
import java .io .File ;
4
6
import java .io .IOException ;
7
+ import java .net .UnknownHostException ;
5
8
import java .util .ArrayList ;
6
9
7
10
import org .apache .commons .io .FileUtils ;
14
17
import org .junit .BeforeClass ;
15
18
import org .junit .Test ;
16
19
import org .junit .runner .RunWith ;
20
+ import org .slf4j .Logger ;
21
+ import org .slf4j .LoggerFactory ;
17
22
import org .springframework .beans .factory .annotation .Autowired ;
18
23
import org .springframework .boot .test .context .SpringBootTest ;
19
24
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
20
25
21
26
import com .ericsson .ei .handlers .EventHandler ;
27
+ import com .ericsson .ei .handlers .ObjectHandler ;
22
28
import com .ericsson .ei .rmq .consumer .RmqConsumer ;
23
29
import com .fasterxml .jackson .databind .JsonNode ;
24
30
import com .fasterxml .jackson .databind .ObjectMapper ;
29
35
import com .rabbitmq .client .DefaultConsumer ;
30
36
import com .rabbitmq .client .Envelope ;
31
37
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
+
32
47
@ RunWith (SpringJUnit4ClassRunner .class )
33
48
@ SpringBootTest
34
49
public class FlowTest {
35
50
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 ;
38
56
39
- @ Autowired
40
- RmqConsumer rmqConsumer ;
57
+ @ Autowired
58
+ RmqConsumer rmqConsumer ;
41
59
42
- @ Autowired
43
- EventHandler eventHandler ;
60
+ @ Autowired
61
+ ObjectHandler objectHandler ;
44
62
45
63
public static class AMQPBrokerManager {
46
64
private String path ;
@@ -71,13 +89,22 @@ public void stopBroker() {
71
89
private static String jsonFileContent ;
72
90
private static JsonNode parsedJason ;
73
91
private static String jsonFilePath = "src/test/resources/test_events.json" ;
92
+ static private final String inputFilePath = "src/test/resources/AggregatedDocument.json" ;
74
93
75
94
@ BeforeClass
76
95
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 {
77
103
System .setProperty ("rabbitmq.port" , "8672" );
78
104
System .setProperty ("rabbitmq.user" , "guest" );
79
105
System .setProperty ("rabbitmq.password" , "guest" );
80
106
107
+
81
108
String config = "src/test/resources/configs/qpidConfig.json" ;
82
109
jsonFileContent = FileUtils .readFileToString (new File (jsonFilePath ));
83
110
ObjectMapper objectmapper = new ObjectMapper ();
@@ -90,20 +117,44 @@ public static void setup() throws Exception {
90
117
cf .setPassword ("guest" );
91
118
cf .setPort (8672 );
92
119
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 ();
93
134
135
+
136
+ try {
137
+ mongodExecutable = starter .prepare (mongodConfig );
138
+ MongodProcess mongod = mongodExecutable .start ();
139
+ } catch (Exception e ) {
140
+ log .info (e .getMessage (),e );
141
+ }
94
142
}
95
143
96
144
@ AfterClass
97
145
public static void tearDown () throws Exception {
98
146
amqpBrocker .stopBroker ();
147
+
148
+ if (mongodExecutable != null )
149
+ mongodExecutable .stop ();
150
+
99
151
try {
100
152
conn .close ();
101
153
} catch (Exception e ) {
102
154
//We try to close the connection but if
103
155
//the connection is closed we just receive the
104
156
//exception and go on
105
157
}
106
-
107
158
}
108
159
109
160
@ Test
@@ -113,25 +164,38 @@ public void test() {
113
164
String queueName = rmqConsumer .getQueueName ();
114
165
String exchange = "ei-poc-4" ;
115
166
116
- long count = channel .consumerCount (queueName );
117
167
ArrayList <String > eventNames = getEventNamesToSend ();
118
-
168
+ int eventsCount = eventNames . size ();
119
169
for (String eventName : eventNames ) {
120
170
JsonNode eventJson = parsedJason .get (eventName );
121
171
String event = eventJson .toString ();
122
172
channel .basicPublish (exchange , queueName , null , event .getBytes ());
123
173
}
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 );
124
189
} catch (Exception e ) {
125
- // TODO Auto-generated catch block
126
- e .printStackTrace ();
190
+ log .info (e .getMessage (),e );
127
191
}
128
192
}
129
193
130
194
private ArrayList <String > getEventNamesToSend () {
131
195
ArrayList <String > eventNames = new ArrayList <>();
132
196
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" );
135
199
eventNames .add ("event_EiffelTestCaseStartedEvent_3" );
136
200
eventNames .add ("event_EiffelTestCaseFinishedEvent_3" );
137
201
0 commit comments