]> git.somenet.org - pub/jan/dst18.git/blob - ass1-doc/src/test/java/dst/ass1/doc/MongoService.java
Add template for assignment 1
[pub/jan/dst18.git] / ass1-doc / src / test / java / dst / ass1 / doc / MongoService.java
1 package dst.ass1.doc;
2
3 import java.util.Collection;
4 import java.util.List;
5 import java.util.Map;
6 import java.util.function.Function;
7 import java.util.stream.Collectors;
8 import java.util.stream.Stream;
9 import java.util.stream.StreamSupport;
10
11 import org.bson.Document;
12 import org.junit.rules.ExternalResource;
13
14 import com.mongodb.MongoClient;
15 import com.mongodb.client.MongoCollection;
16 import com.mongodb.client.MongoDatabase;
17
18 import dst.ass1.doc.impl.DocumentServiceFactory;
19 import dst.ass1.jpa.util.Constants;
20
21 // DO NOT MODIFY THIS CLASS.
22
23 /**
24  * The MongoService class is used as a JUnit rule, that fulfills the same tasks as the ORMService in ass1-jpa, and works
25  * very similarly.
26  */
27 public class MongoService extends ExternalResource {
28
29     private IDocumentTestData testData;
30     private boolean insertTestData;
31     private boolean clearTestData;
32
33     private MongoClient mongoClient;
34     private MongoDatabase mongoDatabase;
35
36     private IDocumentServiceFactory documentServiceFactory;
37     private IDocumentRepository documentRepository;
38     private IDocumentQuery documentQuery;
39
40     public MongoService() {
41         this(null, false, true);
42     }
43
44     public MongoService(IDocumentTestData testData) {
45         this(testData, true, true);
46     }
47
48     public MongoService(IDocumentTestData testData, boolean insertTestData, boolean clearTestData) {
49         this.testData = testData;
50         this.insertTestData = insertTestData;
51         this.clearTestData = clearTestData;
52     }
53
54     public MongoClient getMongoClient() {
55         return mongoClient;
56     }
57
58     public MongoDatabase getMongoDatabase() {
59         return mongoDatabase;
60     }
61
62     public IDocumentServiceFactory getDocumentServiceFactory() {
63         return documentServiceFactory;
64     }
65
66     public IDocumentRepository getDocumentRepository() {
67         return documentRepository;
68     }
69
70     public IDocumentQuery getDocumentQuery() {
71         return documentQuery;
72     }
73
74     @Override
75     protected void before() throws Throwable {
76         setUpMongo();
77
78         if (insertTestData && testData != null) {
79             insertData(testData);
80         }
81     }
82
83     @Override
84     protected void after() {
85         try {
86             if (clearTestData) {
87                 clearData();
88             }
89         } finally {
90             tearDownMongo();
91         }
92     }
93
94     private void setUpMongo() {
95         mongoClient = new MongoClient("127.0.0.1");
96         mongoDatabase = mongoClient.getDatabase(Constants.MONGO_DB_NAME);
97
98         documentServiceFactory = new DocumentServiceFactory();
99         documentRepository = documentServiceFactory.createDocumentRepository();
100
101         if (documentRepository == null) {
102             throw new IllegalStateException("DocumentRepository returned from factory is null");
103         }
104
105         documentQuery = documentServiceFactory.createDocumentQuery(mongoDatabase);
106     }
107
108     private void tearDownMongo() {
109         mongoClient.close();
110     }
111
112     private void insertData(IDocumentTestData testData) throws Exception {
113         testData.insertTestData(getMongoDatabase());
114     }
115
116     private void clearData() {
117         getMongoDatabase().drop();
118     }
119
120     public static Stream<Document> stream(MongoCollection<Document> collection) {
121         return StreamSupport.stream(collection.find().spliterator(), false);
122     }
123
124     public static <T> Map<T, Document> idMap(MongoCollection<Document> collection, Function<Document, T> idFunction) {
125         return stream(collection).collect(Collectors.toMap(idFunction, Function.identity()));
126     }
127
128 }