Skip to content

Commit 2452d3a

Browse files
author
Vali (Vasile Baluta)
committed
another travis test fix
1 parent 6a796fe commit 2452d3a

File tree

3 files changed

+44
-77
lines changed

3 files changed

+44
-77
lines changed

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

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import java.io.File;
66
import java.util.ArrayList;
77

8+
import javax.annotation.PostConstruct;
9+
810
import org.apache.commons.io.FileUtils;
911
import org.apache.qpid.server.Broker;
1012
import org.apache.qpid.server.BrokerOptions;
@@ -24,9 +26,11 @@
2426
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2527

2628
import com.ericsson.ei.handlers.ObjectHandler;
29+
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
2730
import com.ericsson.ei.rmqhandler.RmqHandler;
2831
import com.fasterxml.jackson.databind.JsonNode;
2932
import com.fasterxml.jackson.databind.ObjectMapper;
33+
import com.mongodb.MongoClient;
3034
import com.rabbitmq.client.Channel;
3135
import com.rabbitmq.client.Connection;
3236
import com.rabbitmq.client.ConnectionFactory;
@@ -38,6 +42,7 @@
3842
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
3943
import de.flapdoodle.embed.mongo.config.Net;
4044
import de.flapdoodle.embed.mongo.distribution.Version;
45+
import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory;
4146
import de.flapdoodle.embed.process.runtime.Network;
4247

4348
@RunWith(SpringJUnit4ClassRunner.class)
@@ -48,7 +53,11 @@ public class FlowTest {
4853

4954
public static File qpidConfig = null;
5055
static AMQPBrokerManager amqpBrocker;
51-
static MongodExecutable mongodExecutable = null;
56+
private static MongodForTestsFactory testsFactory;
57+
static MongoClient mongoClient = null;
58+
59+
@Autowired
60+
private MongoDBHandler mongoDBHandler;
5261

5362
@Autowired
5463
RmqHandler rmqHandler;
@@ -95,6 +104,11 @@ public static void setup() throws Exception {
95104
setUpEmbeddedMongo();
96105
}
97106

107+
@PostConstruct
108+
public void initMocks() {
109+
mongoDBHandler.setMongoClient(mongoClient);
110+
}
111+
98112
public static void setUpMessageBus() throws Exception {
99113
System.setProperty("rabbitmq.port", "8672");
100114
System.setProperty("rabbitmq.user", "guest");
@@ -116,34 +130,14 @@ public static void setUpMessageBus() throws Exception {
116130
}
117131

118132
public static void setUpEmbeddedMongo() throws Exception {
119-
int port = 12349;
120-
System.setProperty("mongodb.port", ""+port);
121-
122-
MongodStarter starter = MongodStarter.getDefaultInstance();
123-
124-
String bindIp = "localhost";
125-
126-
IMongodConfig mongodConfig = new MongodConfigBuilder()
127-
.version(Version.Main.PRODUCTION)
128-
.net(new Net(bindIp, port, Network.localhostIsIPv6()))
129-
.build();
130-
131-
132-
try {
133-
mongodExecutable = starter.prepare(mongodConfig);
134-
MongodProcess mongod = mongodExecutable.start();
135-
} catch (Exception e) {
136-
log.info(e.getMessage(),e);
137-
}
133+
testsFactory = MongodForTestsFactory.with(Version.V3_4_1);
134+
mongoClient = testsFactory.newMongo();
138135
}
139136

140137
@AfterClass
141138
public static void tearDown() throws Exception {
142139
amqpBrocker.stopBroker();
143140

144-
if (mongodExecutable != null)
145-
mongodExecutable.stop();
146-
147141
try {
148142
conn.close();
149143
} catch (Exception e) {

src/test/java/com/ericsson/ei/handlers/test/ObjectHandlerTest.java

Lines changed: 15 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import java.io.File;
88

9+
import javax.annotation.PostConstruct;
10+
911
import org.apache.commons.io.FileUtils;
1012
import org.junit.AfterClass;
1113
import org.junit.Before;
@@ -16,6 +18,7 @@
1618
import org.mockito.MockitoAnnotations;
1719
import org.slf4j.Logger;
1820
import org.slf4j.LoggerFactory;
21+
import org.springframework.beans.factory.annotation.Autowired;
1922

2023
import com.ericsson.ei.handlers.EventToObjectMapHandler;
2124
import com.ericsson.ei.handlers.ObjectHandler;
@@ -26,6 +29,7 @@
2629
import com.ericsson.ei.rules.test.TestRulesObject;
2730
import com.fasterxml.jackson.databind.JsonNode;
2831
import com.fasterxml.jackson.databind.ObjectMapper;
32+
import com.mongodb.MongoClient;
2933

3034
import de.flapdoodle.embed.mongo.MongodExecutable;
3135
import de.flapdoodle.embed.mongo.MongodProcess;
@@ -34,24 +38,27 @@
3438
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
3539
import de.flapdoodle.embed.mongo.config.Net;
3640
import de.flapdoodle.embed.mongo.distribution.Version;
41+
import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory;
3742
import de.flapdoodle.embed.process.runtime.Network;
3843

3944
public class ObjectHandlerTest {
4045

4146
static Logger log = (Logger) LoggerFactory.getLogger(ObjectHandlerTest.class);
4247

4348
static ObjectHandler objHandler = new ObjectHandler();
44-
static MongodExecutable mongodExecutable = null;
45-
static MongoDBHandler mongoDBHandler = null;
49+
50+
private static MongodForTestsFactory testsFactory;
51+
static MongoClient mongoClient = null;
52+
53+
54+
static MongoDBHandler mongoDBHandler = new MongoDBHandler();
55+
4656
static JmesPathInterface jmesPathInterface = new JmesPathInterface();
4757

4858
static private RulesObject rulesObject;
4959
static private final String inputFilePath = "src/test/resources/RulesHandlerOutput2.json";
5060
static private JsonNode rulesJson;
5161

52-
static String host = "localhost";
53-
static int port = 27017;
54-
5562
static String dataBaseName = "EventStorageDBbbb";
5663
static String collectionName = "SampleEvents";
5764
static String input = "{\"id\":\"eventId\",\"type\":\"eventType11\",\"test_cases\" : [{\"event_id\" : \"testcaseid1\", \"test_data\" : \"testcase1data\"},{\"event_id\" : \"testcaseid2\", \"test_data\" : \"testcase2data\"}]}";
@@ -60,32 +67,15 @@ public class ObjectHandlerTest {
6067
static String event = "{\"meta\":{\"id\":\"eventId\"}}";
6168

6269
public static void setUpEmbeddedMongo() throws Exception {
63-
System.setProperty("mongodb.port", ""+port);
64-
65-
MongodStarter starter = MongodStarter.getDefaultInstance();
66-
67-
String bindIp = "localhost";
68-
69-
IMongodConfig mongodConfig = new MongodConfigBuilder()
70-
.version(Version.Main.PRODUCTION)
71-
.net(new Net(bindIp, port, Network.localhostIsIPv6()))
72-
.build();
73-
74-
75-
try {
76-
mongodExecutable = starter.prepare(mongodConfig);
77-
MongodProcess mongod = mongodExecutable.start();
78-
} catch (Exception e) {
79-
log.info(e.getMessage(),e);
80-
}
70+
testsFactory = MongodForTestsFactory.with(Version.V3_4_1);
71+
mongoClient = testsFactory.newMongo();
8172
}
8273

8374
@BeforeClass
8475
public static void init() throws Exception
8576
{
8677
setUpEmbeddedMongo();
87-
mongoDBHandler = new MongoDBHandler();
88-
mongoDBHandler.createConnection(host,port);
78+
mongoDBHandler.setMongoClient(mongoClient);
8979
EventToObjectMapHandler eventToObjectMapHandler = mock(EventToObjectMapHandler.class);
9080
objHandler.setEventToObjectMap(eventToObjectMapHandler);
9181
objHandler.setMongoDbHandler(mongoDBHandler);
@@ -115,8 +105,5 @@ public void test() {
115105
public static void dropCollection()
116106
{
117107
mongoDBHandler.dropDocument(dataBaseName, collectionName, condition);
118-
119-
if (mongodExecutable != null)
120-
mongodExecutable.stop();
121108
}
122109
}

src/test/java/com/ericsson/ei/mongoDBHandler/test/MongoDBHandlerTest.java

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@
55
import org.junit.Test;
66
import org.slf4j.Logger;
77
import org.slf4j.LoggerFactory;
8+
import org.springframework.beans.factory.annotation.Autowired;
89

910
import com.ericsson.ei.handlers.test.ObjectHandlerTest;
1011
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
12+
import com.mongodb.MongoClient;
1113

1214
import de.flapdoodle.embed.mongo.MongodExecutable;
1315
import de.flapdoodle.embed.mongo.MongodProcess;
@@ -16,6 +18,7 @@
1618
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
1719
import de.flapdoodle.embed.mongo.config.Net;
1820
import de.flapdoodle.embed.mongo.distribution.Version;
21+
import de.flapdoodle.embed.mongo.tests.MongodForTestsFactory;
1922
import de.flapdoodle.embed.process.runtime.Network;
2023

2124
import static org.junit.Assert.assertTrue;
@@ -29,11 +32,10 @@ public class MongoDBHandlerTest {
2932

3033
static Logger log = (Logger) LoggerFactory.getLogger(MongoDBHandlerTest.class);
3134

32-
static MongodExecutable mongodExecutable = null;
33-
static MongoDBHandler mongoDBHandler = null;
34-
35-
static String host = "localhost";
36-
static int port = 27017;
35+
@Autowired
36+
static MongoDBHandler mongoDBHandler;
37+
private static MongodForTestsFactory testsFactory;
38+
static MongoClient mongoClient = null;
3739

3840
static String dataBaseName = "EventStorageDBbbb";
3941
static String collectionName = "SampleEvents";
@@ -42,32 +44,16 @@ public class MongoDBHandlerTest {
4244
static String condition = "{\"test_cases.event_id\" : \"testcaseid1\"}";
4345

4446
public static void setUpEmbeddedMongo() throws Exception {
45-
System.setProperty("mongodb.port", ""+port);
46-
47-
MongodStarter starter = MongodStarter.getDefaultInstance();
48-
49-
String bindIp = "localhost";
50-
51-
IMongodConfig mongodConfig = new MongodConfigBuilder()
52-
.version(Version.Main.PRODUCTION)
53-
.net(new Net(bindIp, port, Network.localhostIsIPv6()))
54-
.build();
55-
56-
57-
try {
58-
mongodExecutable = starter.prepare(mongodConfig);
59-
MongodProcess mongod = mongodExecutable.start();
60-
} catch (Exception e) {
61-
log.info(e.getMessage(),e);
62-
}
47+
testsFactory = MongodForTestsFactory.with(Version.V3_4_1);
48+
mongoClient = testsFactory.newMongo();
6349
}
6450

6551
@BeforeClass
6652
public static void init() throws Exception
6753
{
6854
setUpEmbeddedMongo();
6955
mongoDBHandler = new MongoDBHandler();
70-
mongoDBHandler.createConnection(host,port);
56+
mongoDBHandler.setMongoClient(mongoClient);
7157
assertTrue(mongoDBHandler.insertDocument(dataBaseName, collectionName, input));
7258
}
7359

@@ -100,7 +86,7 @@ public void testUpdateDocument(){
10086
public static void dropCollection()
10187
{
10288
assertTrue(mongoDBHandler.dropDocument(dataBaseName, collectionName, condition));
103-
if (mongodExecutable != null)
104-
mongodExecutable.stop();
89+
testsFactory.shutdown();
90+
mongoClient.close();
10591
}
10692
}

0 commit comments

Comments
 (0)