1
|
package eu.dnetlib.enabling.is.store; // NOPMD
|
2
|
|
3
|
import java.util.ArrayList;
|
4
|
import java.util.Arrays;
|
5
|
import java.util.List;
|
6
|
|
7
|
import eu.dnetlib.xml.database.XMLDatabase;
|
8
|
import org.junit.Before;
|
9
|
import org.junit.Test;
|
10
|
import org.junit.runner.RunWith;
|
11
|
import org.mockito.Mock;
|
12
|
import org.mockito.junit.MockitoJUnitRunner;
|
13
|
import org.xmldb.api.base.XMLDBException;
|
14
|
|
15
|
import static org.junit.Assert.*;
|
16
|
import static org.mockito.Mockito.verify;
|
17
|
import static org.mockito.Mockito.when;
|
18
|
|
19
|
/**
|
20
|
* test the ISStore service.
|
21
|
*
|
22
|
* @author marko
|
23
|
*
|
24
|
*/
|
25
|
@RunWith(MockitoJUnitRunner.class)
|
26
|
public class ISStoreServiceTest { // NOPMD by marko on 11/24/08 4:54 PM
|
27
|
|
28
|
/**
|
29
|
* root collection name for mocked xmldb.
|
30
|
*/
|
31
|
private static final String ROOT_COLLECTION = "/db";
|
32
|
/**
|
33
|
* test resultset id.
|
34
|
*/
|
35
|
private static final String SOME_RS_ID = "someRsId";
|
36
|
/**
|
37
|
* some test query.
|
38
|
*/
|
39
|
private static final String SOME_QUERY = "someQuery";
|
40
|
/**
|
41
|
* a test collection name.
|
42
|
*/
|
43
|
private static final String TEST_COLLECTION = "testCollection";
|
44
|
/**
|
45
|
* a test string value.
|
46
|
*/
|
47
|
private static final String ONE = "one";
|
48
|
/**
|
49
|
* another test resultset value.
|
50
|
*/
|
51
|
private static final String TWO = "two";
|
52
|
/**
|
53
|
* name of the a test file.
|
54
|
*/
|
55
|
private static final String TEST_FILE = "testFile";
|
56
|
|
57
|
/**
|
58
|
* the ISStoreService under test.
|
59
|
*/
|
60
|
private transient ISStore store = null;
|
61
|
/**
|
62
|
* the ISStoreService under test. Used only in setUp/tearDown().
|
63
|
*/
|
64
|
private transient ISStoreImpl ismo = null;
|
65
|
/**
|
66
|
* the XMLDatabase injected into the ISStore.
|
67
|
*/
|
68
|
@Mock
|
69
|
private transient XMLDatabase xdb;
|
70
|
|
71
|
/**
|
72
|
* setup the ISStore instance, injecting all the dependencies.
|
73
|
*
|
74
|
* @throws Exception
|
75
|
* shouldn't throw
|
76
|
*/
|
77
|
@Before
|
78
|
public void setUp() throws Exception {
|
79
|
ismo = new ISStoreImpl();
|
80
|
|
81
|
ismo.setXmlDatabase(xdb);
|
82
|
|
83
|
store = ismo;
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* create collection.
|
88
|
*
|
89
|
* @throws ISStoreException
|
90
|
* shouldn't happen
|
91
|
* @throws XMLDBException
|
92
|
* shouldn't happen
|
93
|
*/
|
94
|
@Test
|
95
|
public void testCreateFileColl() throws ISStoreException, XMLDBException {
|
96
|
assertTrue("create collection", store.createFileColl(TEST_COLLECTION));
|
97
|
verify(xdb).createCollection(TEST_COLLECTION);
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* delete collection.
|
102
|
*
|
103
|
* @throws ISStoreException
|
104
|
* shouldn't happen
|
105
|
* @throws XMLDBException
|
106
|
* shouldn't happen
|
107
|
*/
|
108
|
@Test
|
109
|
public void testDeleteFileColl() throws ISStoreException, XMLDBException {
|
110
|
assertTrue("delete collection", store.deleteFileColl(TEST_COLLECTION));
|
111
|
verify(xdb).removeCollection(TEST_COLLECTION);
|
112
|
}
|
113
|
|
114
|
/**
|
115
|
* delete an xml file.
|
116
|
*
|
117
|
* @throws ISStoreException
|
118
|
* shouldn't happen
|
119
|
* @throws XMLDBException
|
120
|
* shouldn't happen
|
121
|
*/
|
122
|
@Test
|
123
|
public void testDeleteXML() throws ISStoreException, XMLDBException {
|
124
|
when(xdb.remove(TEST_FILE, TEST_COLLECTION)).thenReturn(true);
|
125
|
|
126
|
assertTrue("delete xml", store.deleteXML(TEST_FILE, TEST_COLLECTION));
|
127
|
verify(xdb).remove(TEST_FILE, TEST_COLLECTION);
|
128
|
}
|
129
|
|
130
|
/**
|
131
|
* execute an XUpdate.
|
132
|
*/
|
133
|
// @Test
|
134
|
// public void testExecuteXUpdate() {
|
135
|
// fail(NYI);
|
136
|
// }
|
137
|
/**
|
138
|
* get collection names.
|
139
|
*
|
140
|
* @throws ISStoreException
|
141
|
* shouldn't happen
|
142
|
* @throws XMLDBException
|
143
|
* shouldn't happen
|
144
|
*/
|
145
|
@Test
|
146
|
public void testGetFileColls() throws ISStoreException, XMLDBException {
|
147
|
final String[] expectedNames = new String[] { "col1", "col2" };
|
148
|
|
149
|
when(xdb.getRootCollection()).thenReturn(ROOT_COLLECTION);
|
150
|
when(xdb.listChildCollections(ROOT_COLLECTION)).thenReturn(Arrays.asList(expectedNames));
|
151
|
|
152
|
final List<String> names = store.getFileColls();
|
153
|
verify(xdb).getRootCollection();
|
154
|
assertArrayEquals(expectedNames, names.toArray());
|
155
|
}
|
156
|
|
157
|
/**
|
158
|
* list file names.
|
159
|
*
|
160
|
* @throws ISStoreException
|
161
|
* shouldn't happen
|
162
|
* @throws XMLDBException
|
163
|
* shouldn't happen
|
164
|
*/
|
165
|
@Test
|
166
|
public void testGetFileNames() throws ISStoreException, XMLDBException {
|
167
|
final String[] expectedNames = new String[] { "fil1", "file2", "file3" };
|
168
|
|
169
|
when(xdb.list(ROOT_COLLECTION)).thenReturn(Arrays.asList(expectedNames));
|
170
|
|
171
|
final List<String> names = store.getFileNames(ROOT_COLLECTION);
|
172
|
assertArrayEquals(expectedNames, names.toArray());
|
173
|
}
|
174
|
|
175
|
/**
|
176
|
* read an xml file.
|
177
|
*
|
178
|
* @throws ISStoreException
|
179
|
* shouldn't happen
|
180
|
* @throws XMLDBException
|
181
|
* shouldn't happen
|
182
|
*/
|
183
|
@Test
|
184
|
public void testGetXML() throws ISStoreException, XMLDBException {
|
185
|
when(xdb.read(TEST_FILE, TEST_COLLECTION)).thenReturn("testContent");
|
186
|
final String res = store.getXML(TEST_FILE, TEST_COLLECTION);
|
187
|
assertEquals("get xml", "testContent", res);
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
* read an xml file using a xquery.
|
192
|
*
|
193
|
* @throws ISStoreException
|
194
|
* shouldn't happen
|
195
|
* @throws XMLDBException
|
196
|
* shouldn't happen
|
197
|
*/
|
198
|
@Test
|
199
|
public void testGetXMLbyQuery() throws ISStoreException, XMLDBException {
|
200
|
prepareXMLDBResultSet();
|
201
|
final String res = store.getXMLbyQuery(SOME_QUERY);
|
202
|
assertEquals("get xml by query", ONE, res);
|
203
|
}
|
204
|
|
205
|
/**
|
206
|
* helper method. Prepares a common result mock.
|
207
|
*/
|
208
|
private void prepareXMLDBResultSet() throws XMLDBException {
|
209
|
List<String> myList = Arrays.asList(ONE,TWO);
|
210
|
when(xdb.xquery(SOME_QUERY)).thenReturn(myList.iterator());
|
211
|
}
|
212
|
|
213
|
/**
|
214
|
* insert a new xml file.
|
215
|
*
|
216
|
* @throws ISStoreException
|
217
|
* shouldn't happen
|
218
|
* @throws XMLDBException
|
219
|
* shouldn't happen
|
220
|
*/
|
221
|
@Test
|
222
|
public void testInsertXML() throws ISStoreException, XMLDBException {
|
223
|
assertTrue("insert xml", store.insertXML("example", ROOT_COLLECTION, "<hello/>"));
|
224
|
verify(xdb).create("example", ROOT_COLLECTION, "<hello/>");
|
225
|
}
|
226
|
|
227
|
/**
|
228
|
* test quick search xml.
|
229
|
*
|
230
|
* @throws ISStoreException
|
231
|
* happens
|
232
|
*/
|
233
|
@Test
|
234
|
public void testQuickSearchXML() throws ISStoreException, XMLDBException {
|
235
|
final List<String> ans = new ArrayList<String>();
|
236
|
ans.add(ONE);
|
237
|
ans.add(TWO);
|
238
|
|
239
|
prepareXMLDBResultSet();
|
240
|
|
241
|
final ArrayList<String> res = (ArrayList<String>) store.quickSearchXML(SOME_QUERY);
|
242
|
assertNotNull("query returned", res);
|
243
|
assertEquals("correct size", 2, res.size());
|
244
|
assertEquals("first value", ONE, res.get(0));
|
245
|
assertEquals("second value", TWO, res.get(1));
|
246
|
}
|
247
|
}
|