Skip to content

Commit 2c081d0

Browse files
author
Vali (Vasile Baluta)
committed
restructure flow tests
1 parent a8e1704 commit 2c081d0

File tree

5 files changed

+263
-307
lines changed

5 files changed

+263
-307
lines changed

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

Lines changed: 2 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -41,151 +41,11 @@
4141

4242
@RunWith(SpringJUnit4ClassRunner.class)
4343
@SpringBootTest
44-
public class FlowTest {
44+
public class FlowTest extends FlowTestBase {
4545

4646
private static Logger log = LoggerFactory.getLogger(FlowTest.class);
4747

48-
public static File qpidConfig = null;
49-
static AMQPBrokerManager amqpBrocker;
50-
private static MongodForTestsFactory testsFactory;
51-
static MongoClient mongoClient = null;
52-
static Queue queue = null;
53-
static RabbitAdmin admin;
54-
55-
@Autowired
56-
private MongoDBHandler mongoDBHandler;
57-
58-
@Autowired
59-
RmqHandler rmqHandler;
60-
61-
@Autowired
62-
ObjectHandler objectHandler;
63-
64-
public static class AMQPBrokerManager {
65-
private String path;
66-
private static final String PORT = "8672";
67-
private final Broker broker = new Broker();
68-
69-
public AMQPBrokerManager(String path) {
70-
super();
71-
this.path = path;
72-
}
73-
74-
public void startBroker() throws Exception {
75-
final BrokerOptions brokerOptions = new BrokerOptions();
76-
brokerOptions.setConfigProperty("qpid.amqp_port", PORT);
77-
brokerOptions.setConfigProperty("qpid.pass_file", "src/test/resources/configs/password.properties");
78-
brokerOptions.setInitialConfigurationLocation(path);
79-
80-
broker.startup(brokerOptions);
81-
}
82-
83-
public void stopBroker() {
84-
broker.shutdown();
85-
}
86-
}
87-
88-
static ConnectionFactory cf;
89-
static Connection conn;
90-
private static String jsonFileContent;
91-
private static JsonNode parsedJason;
92-
private static String jsonFilePath = "src/test/resources/test_events.json";
93-
static private final String inputFilePath = "src/test/resources/AggregatedDocument.json";
94-
95-
@BeforeClass
96-
public static void setup() throws Exception {
97-
System.setProperty("flow.test", "true");
98-
System.setProperty("eiffel.intelligence.processedEventsCount", "0");
99-
System.setProperty("eiffel.intelligence.waitListEventsCount", "0");
100-
setUpMessageBus();
101-
setUpEmbeddedMongo();
102-
}
103-
104-
@PostConstruct
105-
public void initMocks() {
106-
mongoDBHandler.setMongoClient(mongoClient);
107-
}
108-
109-
public static void setUpMessageBus() throws Exception {
110-
System.setProperty("rabbitmq.port", "8672");
111-
System.setProperty("rabbitmq.user", "guest");
112-
System.setProperty("rabbitmq.password", "guest");
113-
114-
115-
String config = "src/test/resources/configs/qpidConfig.json";
116-
jsonFileContent = FileUtils.readFileToString(new File(jsonFilePath));
117-
ObjectMapper objectmapper = new ObjectMapper();
118-
parsedJason = objectmapper.readTree(jsonFileContent);
119-
qpidConfig = new File(config);
120-
amqpBrocker = new AMQPBrokerManager(qpidConfig.getAbsolutePath());
121-
amqpBrocker.startBroker();
122-
cf = new ConnectionFactory();
123-
cf.setUsername("guest");
124-
cf.setPassword("guest");
125-
cf.setPort(8672);
126-
conn = cf.newConnection();
127-
}
128-
129-
public static void setUpEmbeddedMongo() throws Exception {
130-
testsFactory = MongodForTestsFactory.with(Version.V3_4_1);
131-
mongoClient = testsFactory.newMongo();
132-
}
133-
134-
@AfterClass
135-
public static void tearDown() throws Exception {
136-
if (amqpBrocker != null)
137-
amqpBrocker.stopBroker();
138-
139-
try {
140-
conn.close();
141-
} catch (Exception e) {
142-
//We try to close the connection but if
143-
//the connection is closed we just receive the
144-
//exception and go on
145-
}
146-
}
147-
148-
@Test
149-
public void flowTest() {
150-
try {
151-
String queueName = rmqHandler.getQueueName();
152-
Channel channel = conn.createChannel();
153-
String exchangeName = "ei-poc-4";
154-
createExchange();
155-
156-
ArrayList<String> eventNames = getEventNamesToSend();
157-
int eventsCount = eventNames.size();
158-
for(String eventName : eventNames) {
159-
JsonNode eventJson = parsedJason.get(eventName);
160-
String event = eventJson.toString();
161-
channel.basicPublish(exchangeName, queueName, null, event.getBytes());
162-
}
163-
164-
// wait for all events to be processed
165-
int processedEvents = 0;
166-
while (processedEvents < eventsCount) {
167-
String countStr = System.getProperty("eiffel.intelligence.processedEventsCount");
168-
String waitingCountStr = System.getProperty("eiffel.intelligence.waitListEventsCount");
169-
if (waitingCountStr == null)
170-
waitingCountStr = "0";
171-
Properties props = admin.getQueueProperties(queue.getName());
172-
int messageCount = Integer.parseInt(props.get("QUEUE_MESSAGE_COUNT").toString());
173-
processedEvents = Integer.parseInt(countStr) - Integer.parseInt(waitingCountStr) - messageCount;
174-
}
175-
176-
String document = objectHandler.findObjectById("6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43");
177-
String expectedDocument = FileUtils.readFileToString(new File(inputFilePath));
178-
ObjectMapper objectmapper = new ObjectMapper();
179-
JsonNode expectedJson = objectmapper.readTree(expectedDocument);
180-
JsonNode actualJson = objectmapper.readTree(document);
181-
String breakString = "breakHere";
182-
assertEquals(expectedJson.toString().length(), actualJson.toString().length());
183-
} catch (Exception e) {
184-
log.info(e.getMessage(),e);
185-
}
186-
}
187-
188-
private ArrayList<String> getEventNamesToSend() {
48+
protected ArrayList<String> getEventNamesToSend() {
18949
ArrayList<String> eventNames = new ArrayList<>();
19050
eventNames.add("event_EiffelConfidenceLevelModifiedEvent_3_2");
19151
eventNames.add("event_EiffelArtifactPublishedEvent_3");
@@ -195,20 +55,4 @@ private ArrayList<String> getEventNamesToSend() {
19555

19656
return eventNames;
19757
}
198-
199-
private void createExchange() {
200-
String queueName = rmqHandler.getQueueName();
201-
String waitlistQueueName = rmqHandler.getWaitlistQueueName();
202-
String exchangeName = "ei-poc-4";
203-
final CachingConnectionFactory ccf = new CachingConnectionFactory(cf);
204-
admin = new RabbitAdmin(ccf);
205-
queue = new Queue(queueName, true);
206-
Queue waitlistQueue = new Queue(waitlistQueueName, true);
207-
admin.declareQueue(queue);
208-
admin.declareQueue(waitlistQueue);
209-
final TopicExchange exchange = new TopicExchange(exchangeName);
210-
admin.declareExchange(exchange);
211-
admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("#"));
212-
ccf.destroy();
213-
}
21458
}

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

Lines changed: 26 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -38,140 +38,45 @@
3838

3939
@RunWith(SpringJUnit4ClassRunner.class)
4040
@SpringBootTest
41-
public class FlowTest2 {
41+
public class FlowTest2 extends FlowTestBase {
4242

4343
private static Logger log = LoggerFactory.getLogger(FlowTest2.class);
4444

45-
public static File qpidConfig = null;
46-
static AMQPBrokerManager amqpBrocker;
47-
private static MongodForTestsFactory testsFactory;
48-
static MongoClient mongoClient = null;
49-
static Queue queue = null;
50-
static RabbitAdmin admin;
51-
52-
@Autowired
53-
private MongoDBHandler mongoDBHandler;
54-
55-
@Autowired
56-
RmqHandler rmqHandler;
57-
58-
@Autowired
59-
ObjectHandler objectHandler;
60-
61-
public static class AMQPBrokerManager {
62-
private String path;
63-
private static final String PORT = "8672";
64-
private final Broker broker = new Broker();
65-
66-
public AMQPBrokerManager(String path) {
67-
super();
68-
this.path = path;
69-
}
70-
71-
public void startBroker() throws Exception {
72-
final BrokerOptions brokerOptions = new BrokerOptions();
73-
brokerOptions.setConfigProperty("qpid.amqp_port", PORT);
74-
brokerOptions.setConfigProperty("qpid.pass_file", "src/test/resources/configs/password.properties");
75-
brokerOptions.setInitialConfigurationLocation(path);
76-
77-
broker.startup(brokerOptions);
78-
}
79-
80-
public void stopBroker() {
81-
broker.shutdown();
82-
}
83-
}
84-
85-
static ConnectionFactory cf;
86-
static Connection conn;
87-
private static String jsonFileContent;
88-
private static JsonNode parsedJason;
89-
private static String jsonFilePath = "src/test/resources/test_events.json";
90-
static private final String inputFilePath = "src/test/resources/AggregatedDocument.json";
9145
static private final String inputFilePath2 = "src/test/resources/AggregatedDocument2.json";
9246

93-
@BeforeClass
94-
public static void setup() throws Exception {
95-
System.setProperty("flow.test", "true");
96-
System.setProperty("eiffel.intelligence.processedEventsCount", "0");
97-
System.setProperty("eiffel.intelligence.waitListEventsCount", "0");
98-
setUpMessageBus();
99-
setUpEmbeddedMongo();
100-
}
101-
102-
@PostConstruct
103-
public void initMocks() {
104-
mongoDBHandler.setMongoClient(mongoClient);
105-
}
106-
107-
public static void setUpMessageBus() throws Exception {
108-
System.setProperty("rabbitmq.port", "8672");
109-
System.setProperty("rabbitmq.user", "guest");
110-
System.setProperty("rabbitmq.password", "guest");
111-
47+
protected ArrayList<String> getEventNamesToSend() {
48+
ArrayList<String> eventNames = new ArrayList<>();
49+
eventNames.add("event_EiffelArtifactCreatedEvent_3");
50+
eventNames.add("event_EiffelArtifactPublishedEvent_3");
51+
eventNames.add("event_EiffelConfidenceLevelModifiedEvent_3_2");
52+
eventNames.add("event_EiffelTestCaseStartedEvent_3");
53+
eventNames.add("event_EiffelTestCaseFinishedEvent_3");
11254

113-
String config = "src/test/resources/configs/qpidConfig.json";
114-
jsonFileContent = FileUtils.readFileToString(new File(jsonFilePath));
115-
ObjectMapper objectmapper = new ObjectMapper();
116-
parsedJason = objectmapper.readTree(jsonFileContent);
117-
qpidConfig = new File(config);
118-
amqpBrocker = new AMQPBrokerManager(qpidConfig.getAbsolutePath());
119-
amqpBrocker.startBroker();
120-
cf = new ConnectionFactory();
121-
cf.setUsername("guest");
122-
cf.setPassword("guest");
123-
cf.setPort(8672);
124-
conn = cf.newConnection();
125-
}
55+
eventNames.add("event_EiffelArtifactCreatedEvent_1");
56+
eventNames.add("event_EiffelArtifactPublishedEvent_1");
57+
eventNames.add("event_EiffelConfidenceLevelModifiedEvent_1");
58+
eventNames.add("event_EiffelTestCaseStartedEvent_1");
59+
eventNames.add("event_EiffelTestCaseFinishedEvent_1");
12660

127-
public static void setUpEmbeddedMongo() throws Exception {
128-
testsFactory = MongodForTestsFactory.with(Version.V3_4_1);
129-
mongoClient = testsFactory.newMongo();
61+
return eventNames;
13062
}
13163

132-
@AfterClass
133-
public static void tearDown() throws Exception {
134-
if (amqpBrocker != null)
135-
amqpBrocker.stopBroker();
136-
137-
try {
138-
conn.close();
139-
} catch (Exception e) {
140-
//We try to close the connection but if
141-
//the connection is closed we just receive the
142-
//exception and go on
64+
protected void waitForEventsToBeProcessed(int eventsCount) {
65+
// wait for all events to be processed
66+
int processedEvents = 0;
67+
while (processedEvents < eventsCount) {
68+
String countStr = System.getProperty("eiffel.intelligence.processedEventsCount");
69+
String waitingCountStr = System.getProperty("eiffel.intelligence.waitListEventsCount");
70+
if (waitingCountStr == null)
71+
waitingCountStr = "0";
72+
Properties props = admin.getQueueProperties(queue.getName());
73+
int messageCount = Integer.parseInt(props.get("QUEUE_MESSAGE_COUNT").toString());
74+
processedEvents = Integer.parseInt(countStr) - Integer.parseInt(waitingCountStr) - messageCount;
14375
}
14476
}
14577

146-
@Test
147-
public void test2AgregatedObjects() {
78+
protected void checkResult() {
14879
try {
149-
String queueName = rmqHandler.getQueueName();
150-
Channel channel = conn.createChannel();
151-
String exchange = "ei-poc-4";
152-
createExchange(exchange, queueName);
153-
154-
155-
ArrayList<String> eventNames = getEventNamesToSend();
156-
int eventsCount = eventNames.size();
157-
for(String eventName : eventNames) {
158-
JsonNode eventJson = parsedJason.get(eventName);
159-
String event = eventJson.toString();
160-
channel.basicPublish(exchange, queueName, null, event.getBytes());
161-
}
162-
163-
// wait for all events to be processed
164-
int processedEvents = 0;
165-
while (processedEvents < eventsCount) {
166-
String countStr = System.getProperty("eiffel.intelligence.processedEventsCount");
167-
String waitingCountStr = System.getProperty("eiffel.intelligence.waitListEventsCount");
168-
if (waitingCountStr == null)
169-
waitingCountStr = "0";
170-
Properties props = admin.getQueueProperties(queue.getName());
171-
int messageCount = Integer.parseInt(props.get("QUEUE_MESSAGE_COUNT").toString());
172-
processedEvents = Integer.parseInt(countStr) - Integer.parseInt(waitingCountStr) - messageCount;
173-
}
174-
17580
String document = objectHandler.findObjectById("6acc3c87-75e0-4b6d-88f5-b1a5d4e62b43");
17681
String expectedDocument = FileUtils.readFileToString(new File(inputFilePath));
17782
ObjectMapper objectmapper = new ObjectMapper();
@@ -188,32 +93,4 @@ public void test2AgregatedObjects() {
18893
log.info(e.getMessage(),e);
18994
}
19095
}
191-
192-
private ArrayList<String> getEventNamesToSend() {
193-
ArrayList<String> eventNames = new ArrayList<>();
194-
eventNames.add("event_EiffelArtifactCreatedEvent_3");
195-
eventNames.add("event_EiffelArtifactPublishedEvent_3");
196-
eventNames.add("event_EiffelConfidenceLevelModifiedEvent_3_2");
197-
eventNames.add("event_EiffelTestCaseStartedEvent_3");
198-
eventNames.add("event_EiffelTestCaseFinishedEvent_3");
199-
200-
eventNames.add("event_EiffelArtifactCreatedEvent_1");
201-
eventNames.add("event_EiffelArtifactPublishedEvent_1");
202-
eventNames.add("event_EiffelConfidenceLevelModifiedEvent_1");
203-
eventNames.add("event_EiffelTestCaseStartedEvent_1");
204-
eventNames.add("event_EiffelTestCaseFinishedEvent_1");
205-
206-
return eventNames;
207-
}
208-
209-
private void createExchange(final String exchangeName, final String queueName) {
210-
final CachingConnectionFactory ccf = new CachingConnectionFactory(cf);
211-
admin = new RabbitAdmin(ccf);
212-
queue = new Queue(queueName, false);
213-
admin.declareQueue(queue);
214-
final TopicExchange exchange = new TopicExchange(exchangeName);
215-
admin.declareExchange(exchange);
216-
admin.declareBinding(BindingBuilder.bind(queue).to(exchange).with("#"));
217-
ccf.destroy();
218-
}
21996
}

0 commit comments

Comments
 (0)