1
|
package eu.dnetlib.data.mdstore.modular.newmongodb;
|
2
|
|
3
|
import com.mongodb.MongoClient;
|
4
|
import com.mongodb.client.MongoCollection;
|
5
|
import eu.dnetlib.data.mdstore.modular.mongodb.MDRecordParser;
|
6
|
import eu.dnetlib.data.mdstore.modular.newmongodb.model.ConfigurationMongo;
|
7
|
import eu.dnetlib.data.mdstore.modular.newmongodb.model.MDStoreEncoder;
|
8
|
import eu.dnetlib.data.mdstore.modular.newmongodb.model.MDStoreRecord;
|
9
|
import eu.dnetlib.data.mdstore.modular.newmongodb.model.MongoClientConfiguration;
|
10
|
import eu.dnetlib.data.mdstore.modular.newmongodb.repository.MetadataRepository;
|
11
|
import eu.dnetlib.enabling.resultset.listener.ResultSetListener;
|
12
|
import eu.dnetlib.enabling.tools.DnetStreamSupport;
|
13
|
import eu.dnetlib.rmi.data.DocumentNotFoundException;
|
14
|
import org.apache.commons.io.IOUtils;
|
15
|
import org.apache.commons.lang3.StringUtils;
|
16
|
import org.bson.Document;
|
17
|
import org.junit.Assert;
|
18
|
import org.junit.Before;
|
19
|
import org.junit.Test;
|
20
|
import org.junit.runner.RunWith;
|
21
|
import org.springframework.beans.factory.annotation.Autowired;
|
22
|
import org.springframework.test.context.ContextConfiguration;
|
23
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
24
|
|
25
|
import java.io.IOException;
|
26
|
import java.util.*;
|
27
|
|
28
|
/**
|
29
|
* Created by sandro on 5/23/17.
|
30
|
*/
|
31
|
|
32
|
|
33
|
@RunWith(SpringJUnit4ClassRunner.class)
|
34
|
@ContextConfiguration(classes = {MongoClientConfiguration.class, ConfigurationMongo.class})
|
35
|
public class MongoMDStoreTest {
|
36
|
|
37
|
|
38
|
@Autowired
|
39
|
private MongoClient client;
|
40
|
|
41
|
@Autowired
|
42
|
private MetadataRepository metadataRepository;
|
43
|
|
44
|
|
45
|
private MongoCollection<Document> metadata;
|
46
|
private MongoCollection<Document> collection;
|
47
|
private MongoCollection<Document> discardedCollection;
|
48
|
|
49
|
|
50
|
@Before
|
51
|
public void initializeCollections() {
|
52
|
metadata = client.getDatabase("mdstore").getCollection("metadata");
|
53
|
collection = client.getDatabase("mdstore").getCollection("collection");
|
54
|
discardedCollection = client.getDatabase("mdstore").getCollection("discarded");
|
55
|
|
56
|
}
|
57
|
|
58
|
@Test
|
59
|
public void testIterator() throws IOException {
|
60
|
int i = 0;
|
61
|
for (String s : MongoTestUtility.createLongIterable(100)) {
|
62
|
i++;
|
63
|
}
|
64
|
|
65
|
Assert.assertEquals(100, i);
|
66
|
}
|
67
|
|
68
|
|
69
|
@Test
|
70
|
public void testSpeedParsing() throws IOException {
|
71
|
|
72
|
final Map<String, String> mdFormat = new HashMap<>();
|
73
|
mdFormat.put("subject", "//*[local-name()='subject']");
|
74
|
mdFormat.put("date", "//*[local-name()='date']");
|
75
|
final long start = System.currentTimeMillis();
|
76
|
long count = DnetStreamSupport.generateParallelStreamFromIterator(MongoTestUtility.createLongIterable(10000).iterator()).
|
77
|
map(it ->
|
78
|
MDStoreRecord.generateRecord(it, new MDRecordParser(mdFormat))
|
79
|
).count();
|
80
|
final long total = System.currentTimeMillis() - start;
|
81
|
System.out.println("count = " + count + " in " + total);
|
82
|
}
|
83
|
|
84
|
|
85
|
|
86
|
|
87
|
private void feedMDStore(final int numberOfItems, final MongoMDStore mdStore) throws Exception {
|
88
|
long start = System.currentTimeMillis();
|
89
|
mdStore.feed(MongoTestUtility.createLongIterable(numberOfItems));
|
90
|
long end = System.currentTimeMillis();
|
91
|
System.out.println("DONE 100 in " + (end - start) + " ms and size " + collection.count());
|
92
|
}
|
93
|
|
94
|
|
95
|
@Test
|
96
|
public void testFeedRecord() throws Exception {
|
97
|
|
98
|
final MongoMDStore store = new MongoMDStore("collection", collection, discardedCollection, MongoTestUtility.generateMDFormat(),metadataRepository);
|
99
|
feedMDStore(100, store);
|
100
|
MDStoreEncoder.decodeAllMDStoreRecords(collection.find()).forEach(
|
101
|
record ->
|
102
|
{
|
103
|
Assert.assertNotNull(record.getBody());
|
104
|
Assert.assertNotNull(record.getExtractedValues());
|
105
|
Assert.assertNotNull(record.getExtractedValues().get("subject"));
|
106
|
Assert.assertTrue(record.getExtractedValues().get("subject").size() > 0);
|
107
|
Assert.assertNotNull(record.getExtractedValues().get("date"));
|
108
|
Assert.assertTrue(record.getExtractedValues().get("date").size() > 0);
|
109
|
}
|
110
|
|
111
|
|
112
|
);
|
113
|
}
|
114
|
|
115
|
@Test
|
116
|
public void isIndexedTest() throws Exception {
|
117
|
final MongoMDStore store = new MongoMDStore("collection", collection, discardedCollection, MongoTestUtility.generateMDFormat(),metadataRepository);
|
118
|
feedMDStore(3, store);
|
119
|
Assert.assertTrue(store.isIndexed());
|
120
|
}
|
121
|
|
122
|
@Test
|
123
|
public void testReadWithResultSet() throws Exception {
|
124
|
|
125
|
final MongoMDStore store = new MongoMDStore("collection", collection, discardedCollection, MongoTestUtility.generateMDFormat(),metadataRepository);
|
126
|
feedMDStore(3, store);
|
127
|
Map<String, String> query = new HashMap<>();
|
128
|
query.put("date", "1978");
|
129
|
query.put("subject", "Sea");
|
130
|
metadataRepository.bootstrap();
|
131
|
|
132
|
ResultSetListener<String> deliver = store.deliver(null, null, query);
|
133
|
Assert.assertTrue(deliver.getCount()==0);
|
134
|
Assert.assertTrue(deliver.getTotal()>0);
|
135
|
Assert.assertTrue(deliver.hasNext());
|
136
|
while (deliver.hasNext()) {
|
137
|
final String nextRecord = deliver.next();
|
138
|
Assert.assertNotNull(nextRecord);
|
139
|
}
|
140
|
|
141
|
deliver = store.deliver(null, "" + System.currentTimeMillis(), null);
|
142
|
Assert.assertTrue(deliver.hasNext());
|
143
|
int i = 0;
|
144
|
while (deliver.hasNext()) {
|
145
|
final String nextRecord = deliver.next();
|
146
|
Assert.assertNotNull(nextRecord);
|
147
|
i ++;
|
148
|
}
|
149
|
Assert.assertEquals(2, i);
|
150
|
|
151
|
deliver = store.deliver( "" + System.currentTimeMillis(),null, null);
|
152
|
Assert.assertFalse(deliver.hasNext());
|
153
|
}
|
154
|
|
155
|
|
156
|
@Test(expected = DocumentNotFoundException.class)
|
157
|
public void getAndDeleteRecordTest() throws Exception {
|
158
|
|
159
|
final String id = "r3d100010134::0";
|
160
|
final MongoMDStore store = new MongoMDStore("collection", collection, discardedCollection, MongoTestUtility.generateMDFormat(),metadataRepository);
|
161
|
feedMDStore(10, store);
|
162
|
|
163
|
|
164
|
String record = store.getRecord(id);
|
165
|
|
166
|
|
167
|
Assert.assertFalse(StringUtils.isEmpty(record));
|
168
|
store.deleteRecord(id);
|
169
|
//NOW It Should Run an exception
|
170
|
store.getRecord(id);
|
171
|
}
|
172
|
|
173
|
|
174
|
|
175
|
@Test
|
176
|
public void testReadIterable() throws Exception {
|
177
|
final MongoMDStore store = new MongoMDStore("collection", collection, discardedCollection, MongoTestUtility.generateMDFormat(),metadataRepository);
|
178
|
feedMDStore(300, store);
|
179
|
Map<String, String> query = new HashMap<>();
|
180
|
query.put("date", "1978");
|
181
|
query.put("subject", "Sea");
|
182
|
Iterable<String> strings = store.deliverPage(null, null, null, 100, 100);
|
183
|
|
184
|
Assert.assertNotNull(strings);
|
185
|
int i = 0;
|
186
|
for (String s: strings) {
|
187
|
i++;
|
188
|
Assert.assertFalse(StringUtils.isEmpty(s));
|
189
|
}
|
190
|
|
191
|
Assert.assertEquals(100, i);
|
192
|
strings = store.deliverPage(null, null, null,100 , (int) (collection.count()-50));
|
193
|
Assert.assertNotNull(strings);
|
194
|
i = 0;
|
195
|
for (String s: strings) {
|
196
|
i++;
|
197
|
Assert.assertFalse(StringUtils.isEmpty(s));
|
198
|
}
|
199
|
|
200
|
Assert.assertEquals(50, i);
|
201
|
store.iterate().forEach(it -> Assert.assertFalse(StringUtils.isEmpty(it)));
|
202
|
}
|
203
|
|
204
|
|
205
|
@Test
|
206
|
public void testTruncate() throws Exception {
|
207
|
|
208
|
final MongoMDStore store = new MongoMDStore("collection", collection, discardedCollection, MongoTestUtility.generateMDFormat(),metadataRepository);
|
209
|
feedMDStore(300, store);
|
210
|
Assert.assertTrue(store.getSize()> 0);
|
211
|
store.truncate();
|
212
|
Assert.assertTrue(store.getSize()== 0);
|
213
|
}
|
214
|
|
215
|
|
216
|
|
217
|
}
|