Skip to content

Commit 9e022ee

Browse files
author
Vali (Vasile Baluta)
committed
add missing embedded mongoDB
Change-Id: I141c429a1bf4c32e49fe992a77f40b2ed9f3ed72
1 parent 36c58b9 commit 9e022ee

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

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

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,22 @@
2727
import com.fasterxml.jackson.databind.JsonNode;
2828
import com.fasterxml.jackson.databind.ObjectMapper;
2929

30+
import de.flapdoodle.embed.mongo.MongodExecutable;
31+
import de.flapdoodle.embed.mongo.MongodProcess;
32+
import de.flapdoodle.embed.mongo.MongodStarter;
33+
import de.flapdoodle.embed.mongo.config.IMongodConfig;
34+
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
35+
import de.flapdoodle.embed.mongo.config.Net;
36+
import de.flapdoodle.embed.mongo.distribution.Version;
37+
import de.flapdoodle.embed.process.runtime.Network;
38+
3039
public class ObjectHandlerTest {
3140

3241
static Logger log = (Logger) LoggerFactory.getLogger(ObjectHandlerTest.class);
3342

3443
static ObjectHandler objHandler = new ObjectHandler();
35-
36-
static MongoDBHandler mongoDBHandler = new MongoDBHandler();
44+
static MongodExecutable mongodExecutable = null;
45+
static MongoDBHandler mongoDBHandler = null;
3746
static JmesPathInterface jmesPathInterface = new JmesPathInterface();
3847

3948
static private RulesObject rulesObject;
@@ -50,10 +59,32 @@ public class ObjectHandlerTest {
5059
static String condition = "{\"_id\" : \"eventId\"}";
5160
static String event = "{\"meta\":{\"id\":\"eventId\"}}";
5261

62+
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+
}
81+
}
5382

5483
@BeforeClass
55-
public static void init()
84+
public static void init() throws Exception
5685
{
86+
setUpEmbeddedMongo();
87+
mongoDBHandler = new MongoDBHandler();
5788
mongoDBHandler.createConnection(host,port);
5889
EventToObjectMapHandler eventToObjectMapHandler = mock(EventToObjectMapHandler.class);
5990
objHandler.setEventToObjectMap(eventToObjectMapHandler);
@@ -84,5 +115,8 @@ public void test() {
84115
public static void dropCollection()
85116
{
86117
mongoDBHandler.dropDocument(dataBaseName, collectionName, condition);
118+
119+
if (mongodExecutable != null)
120+
mongodExecutable.stop();
87121
}
88122
}

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

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,21 @@
33
import org.junit.Before;
44
import org.junit.BeforeClass;
55
import org.junit.Test;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
68

9+
import com.ericsson.ei.handlers.test.ObjectHandlerTest;
710
import com.ericsson.ei.mongodbhandler.MongoDBHandler;
811

12+
import de.flapdoodle.embed.mongo.MongodExecutable;
13+
import de.flapdoodle.embed.mongo.MongodProcess;
14+
import de.flapdoodle.embed.mongo.MongodStarter;
15+
import de.flapdoodle.embed.mongo.config.IMongodConfig;
16+
import de.flapdoodle.embed.mongo.config.MongodConfigBuilder;
17+
import de.flapdoodle.embed.mongo.config.Net;
18+
import de.flapdoodle.embed.mongo.distribution.Version;
19+
import de.flapdoodle.embed.process.runtime.Network;
20+
921
import static org.junit.Assert.assertTrue;
1022
import static org.junit.Assert.assertEquals;
1123

@@ -15,7 +27,10 @@
1527

1628
public class MongoDBHandlerTest {
1729

18-
static MongoDBHandler mongoDBHandler = new MongoDBHandler();
30+
static Logger log = (Logger) LoggerFactory.getLogger(MongoDBHandlerTest.class);
31+
32+
static MongodExecutable mongodExecutable = null;
33+
static MongoDBHandler mongoDBHandler = null;
1934

2035
static String host = "localhost";
2136
static int port = 27017;
@@ -26,9 +41,32 @@ public class MongoDBHandlerTest {
2641
String updateInput = "{\"id\":\"eventId\",\"type\":\"eventType11\",\"test_cases\" : [{\"event_id\" : \"testcaseid1\", \"test_data\" : \"testcase2data\"},{\"event_id\" : \"testcaseid3\", \"test_data\" : \"testcase3data\"}]}";
2742
static String condition = "{\"test_cases.event_id\" : \"testcaseid1\"}";
2843

44+
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+
}
63+
}
64+
2965
@BeforeClass
30-
public static void init()
66+
public static void init() throws Exception
3167
{
68+
setUpEmbeddedMongo();
69+
mongoDBHandler = new MongoDBHandler();
3270
mongoDBHandler.createConnection(host,port);
3371
assertTrue(mongoDBHandler.insertDocument(dataBaseName, collectionName, input));
3472
}
@@ -62,5 +100,7 @@ public void testUpdateDocument(){
62100
public static void dropCollection()
63101
{
64102
assertTrue(mongoDBHandler.dropDocument(dataBaseName, collectionName, condition));
103+
if (mongodExecutable != null)
104+
mongodExecutable.stop();
65105
}
66106
}

0 commit comments

Comments
 (0)