1
|
package eu.dnetlib.data.mdstore.modular.mongodb;
|
2
|
|
3
|
import java.net.UnknownHostException;
|
4
|
import java.util.ArrayList;
|
5
|
import java.util.List;
|
6
|
import java.util.UUID;
|
7
|
|
8
|
import com.mongodb.DB;
|
9
|
import com.mongodb.DBCollection;
|
10
|
import com.mongodb.DBCursor;
|
11
|
import com.mongodb.Mongo;
|
12
|
import com.mongodb.client.MongoDatabase;
|
13
|
import eu.dnetlib.data.mdstore.MDStoreServiceException;
|
14
|
import eu.dnetlib.data.mdstore.modular.connector.MDStore;
|
15
|
import eu.dnetlib.data.mdstore.modular.connector.MDStoreDao;
|
16
|
import eu.dnetlib.data.mdstore.modular.connector.MDStoreTransactionManager;
|
17
|
import org.junit.Assert;
|
18
|
import org.junit.Ignore;
|
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
|
// TODO: reimplement tests
|
26
|
|
27
|
@Ignore
|
28
|
@RunWith(SpringJUnit4ClassRunner.class)
|
29
|
@ContextConfiguration(classes = ConfigurationTestConfig.class)
|
30
|
public class MDStoreTransactionManagerTest {
|
31
|
|
32
|
@Autowired
|
33
|
private MongoDatabase db;
|
34
|
|
35
|
@Autowired
|
36
|
private MDStoreTransactionManager manager;
|
37
|
|
38
|
@Autowired
|
39
|
private MDStoreDao dao;
|
40
|
|
41
|
@Test
|
42
|
public void testCreateandRetrieve() throws MDStoreServiceException {
|
43
|
UUID idCreation = UUID.randomUUID();
|
44
|
db.getCollection("metadataManager").drop();
|
45
|
((MDStoreTransactionManagerImpl) manager).setManagerTable(null);
|
46
|
|
47
|
String mdId = idCreation.toString() + "_TURTdG9yZURTUmVzb3VyY2VzL01EU3RvcmVEU1Jlc291cmNlVHlwZQ==";
|
48
|
manager.createMDStore(mdId);
|
49
|
|
50
|
Assert.assertNotNull(manager.startTransaction(mdId, true));
|
51
|
Assert.assertNotNull(manager.startTransaction(mdId, true));
|
52
|
|
53
|
String s = manager.getMDStoreCollection(mdId);
|
54
|
Assert.assertNotNull(s);
|
55
|
this.manager.dropMDStore(mdId);
|
56
|
s = manager.getMDStoreCollection(mdId);
|
57
|
Assert.assertNull(s);
|
58
|
db.getCollection("metadataManager").drop();
|
59
|
((MDStoreTransactionManagerImpl) manager).setManagerTable(null);
|
60
|
}
|
61
|
|
62
|
@Test
|
63
|
public void testReadMdStore() throws MDStoreServiceException {
|
64
|
UUID idCreation = UUID.randomUUID();
|
65
|
String mdId = idCreation.toString() + "_TURTdG9yZURTUmVzb3VyY2VzL01EU3RvcmVEU1Jlc291cmNlVHlwZQ==";
|
66
|
manager.createMDStore(mdId);
|
67
|
Assert.assertNotNull(manager.readMdStore(mdId));
|
68
|
Assert.assertNotNull(manager.startTransaction(mdId, true));
|
69
|
Assert.assertNotNull(manager.readMdStore(mdId));
|
70
|
db.getCollection("metadataManager").drop();
|
71
|
((MDStoreTransactionManagerImpl) manager).setManagerTable(null);
|
72
|
db.getCollection("metadataManager").drop();
|
73
|
((MDStoreTransactionManagerImpl) manager).setManagerTable(null);
|
74
|
|
75
|
}
|
76
|
|
77
|
@Test
|
78
|
public void testCommit() throws MDStoreServiceException {
|
79
|
UUID idCreation = UUID.randomUUID();
|
80
|
String mdId = idCreation.toString() + "_TURTdG9yZURTUmVzb3VyY2VzL01EU3RvcmVEU1Jlc291cmNlVHlwZQ==";
|
81
|
manager.createMDStore(mdId);
|
82
|
String idCurrent = manager.readMdStore(mdId);
|
83
|
String transaction = manager.startTransaction(mdId, true);
|
84
|
// Assert.assertTrue(manager.commit(transaction, mdId));
|
85
|
Assert.assertNotSame(idCurrent, manager.readMdStore(mdId));
|
86
|
|
87
|
}
|
88
|
|
89
|
@Ignore
|
90
|
@Test
|
91
|
public void testDateTime() throws MDStoreServiceException, UnknownHostException {
|
92
|
Mongo mongo = new Mongo("localhost", 27017);
|
93
|
DB dbinput = mongo.getDB("mdstore");
|
94
|
DBCollection inputCollection = dbinput.getCollection("70e07e9f-b3bf-4423-8777-b159819e0c6a");
|
95
|
|
96
|
Assert.assertNotNull(inputCollection.findOne().get("body"));
|
97
|
UUID idCreation = UUID.randomUUID();
|
98
|
|
99
|
String mdId = idCreation.toString() + "_TURTdG9yZURTUmVzb3VyY2VzL01EU3RvcmVEU1Jlc291cmNlVHlwZQ==";
|
100
|
|
101
|
manager.createMDStore(mdId);
|
102
|
dao.createMDStore(mdId, "a", "a", "a");
|
103
|
|
104
|
String transId = manager.startTransaction(mdId, true);
|
105
|
|
106
|
ArrayList<String> data = new ArrayList<String>();
|
107
|
DBCursor cursor = inputCollection.find();
|
108
|
|
109
|
for (int i = 0; i < 1000; i++) {
|
110
|
data.add((String) cursor.next().get("body"));
|
111
|
}
|
112
|
dao.getMDStore(transId).feed(data, true);
|
113
|
// manager.commit(transId, mdId);
|
114
|
|
115
|
cursor = inputCollection.find();
|
116
|
transId = manager.startTransaction(mdId, false);
|
117
|
|
118
|
data.clear();
|
119
|
for (int i = 0; i < 10; i++) {
|
120
|
data.add(cursor.next().get("body").toString().replace("oai:pumaoai.isti.cnr.it:", "SUUUCAAA"));
|
121
|
}
|
122
|
|
123
|
String currentId = manager.readMdStore(mdId);
|
124
|
|
125
|
final MDStore newMdstore = dao.getMDStore(currentId);
|
126
|
|
127
|
new Thread(() -> {
|
128
|
List<String> dataInput = null;
|
129
|
try {
|
130
|
dataInput = newMdstore.deliver("", "", null).getResult(0, 10);
|
131
|
for (int i = 0; i < 10; i++) {
|
132
|
try {
|
133
|
Thread.sleep(1000);
|
134
|
} catch (InterruptedException e) {
|
135
|
// TODO Auto-generated catch block
|
136
|
e.printStackTrace();
|
137
|
}
|
138
|
System.out.println(dataInput.get(i));
|
139
|
}
|
140
|
} catch (MDStoreServiceException e) {
|
141
|
e.printStackTrace();
|
142
|
}
|
143
|
}).start();
|
144
|
|
145
|
dao.getMDStore(transId).feed(data, true);
|
146
|
// manager.commit(transId, mdId);
|
147
|
|
148
|
}
|
149
|
}
|