Project

General

Profile

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.enabling.is.store.rmi.ISStoreException;
8
import eu.dnetlib.enabling.is.store.rmi.ISStoreService;
9
import eu.dnetlib.xml.database.XMLDatabase;
10
import org.junit.After;
11
import org.junit.Before;
12
import org.junit.Test;
13
import org.junit.runner.RunWith;
14
import org.mockito.Mock;
15
import org.mockito.junit.MockitoJUnitRunner;
16
import org.xmldb.api.base.XMLDBException;
17

    
18
import static org.junit.Assert.*;
19
import static org.mockito.Mockito.verify;
20
import static org.mockito.Mockito.when;
21

    
22
/**
23
 * test the ISStore service.
24
 * 
25
 * @author marko
26
 * 
27
 */
28
@RunWith(MockitoJUnitRunner.class)
29
public class ISStoreServiceTest { // NOPMD by marko on 11/24/08 4:54 PM
30

    
31
	/**
32
	 * root collection name for mocked xmldb.
33
	 */
34
	private static final String ROOT_COLLECTION = "/db";
35
	/**
36
	 * test resultset id.
37
	 */
38
	private static final String SOME_RS_ID = "someRsId";
39
	/**
40
	 * some test query.
41
	 */
42
	private static final String SOME_QUERY = "someQuery";
43
	/**
44
	 * a test collection name.
45
	 */
46
	private static final String TEST_COLLECTION = "testCollection";
47
	/**
48
	 * a test string value.
49
	 */
50
	private static final String ONE = "one";
51
	/**
52
	 * another test resultset value.
53
	 */
54
	private static final String TWO = "two";
55
	/**
56
	 * name of the a test file.
57
	 */
58
	private static final String TEST_FILE = "testFile";
59

    
60
	/**
61
	 * the ISStoreService under test.
62
	 */
63
	private transient ISStoreService store = null;
64
	/**
65
	 * the ISStoreService under test. Used only in setUp/tearDown().
66
	 */
67
	private transient ISStoreServiceImpl ismo = null;
68
	/**
69
	 * the XMLDatabase injected into the ISStore.
70
	 */
71
	@Mock
72
	private transient XMLDatabase xdb;
73

    
74
	/**
75
	 * setup the ISStore instance, injecting all the dependencies.
76
	 * 
77
	 * @throws Exception
78
	 *             shouldn't throw
79
	 */
80
	@Before
81
	public void setUp() throws Exception {
82
		ismo = new ISStoreServiceImpl();
83

    
84
		ismo.setXmlDatabase(xdb);
85
		ismo.start();
86

    
87
		store = ismo;
88
	}
89

    
90
	/**
91
	 * respect the service lifecycle.
92
	 */
93
	@After
94
	public void tearDown() {
95
		ismo.stop();
96
	}
97

    
98
	/**
99
	 * create collection.
100
	 * 
101
	 * @throws ISStoreException
102
	 *             shouldn't happen
103
	 * @throws XMLDBException
104
	 *             shouldn't happen
105
	 */
106
	@Test
107
	public void testCreateFileColl() throws ISStoreException, XMLDBException {
108
		assertTrue("create collection", store.createFileColl(TEST_COLLECTION));
109
		verify(xdb).createCollection(TEST_COLLECTION);
110
	}
111

    
112
	/**
113
	 * delete collection.
114
	 * 
115
	 * @throws XMLDBException
116
	 *             shouldn't happen
117
	 * @throws ISStoreException
118
	 *             shouldn't happen
119
	 */
120
	@Test
121
	public void testDeleteFileColl() throws XMLDBException, ISStoreException {
122
		assertTrue("delete collection", store.deleteFileColl(TEST_COLLECTION));
123
		verify(xdb).removeCollection(TEST_COLLECTION);
124
	}
125

    
126
	/**
127
	 * delete an xml file.
128
	 * 
129
	 * @throws ISStoreException
130
	 *             shouldn't happen
131
	 * @throws XMLDBException
132
	 *             shouldn't happen
133
	 */
134
	@Test
135
	public void testDeleteXML() throws ISStoreException, XMLDBException {
136
		when(xdb.remove(TEST_FILE, TEST_COLLECTION)).thenReturn(true);
137

    
138
		assertTrue("delete xml", store.deleteXML(TEST_FILE, TEST_COLLECTION));
139
		verify(xdb).remove(TEST_FILE, TEST_COLLECTION);
140
	}
141

    
142
	/**
143
	 * execute an XUpdate.
144
	 */
145
	// @Test
146
	// public void testExecuteXUpdate() {
147
	// fail(NYI);
148
	// }
149
	/**
150
	 * get collection names.
151
	 * 
152
	 * @throws ISStoreException
153
	 *             shouldn't happen
154
	 * @throws XMLDBException
155
	 *             shouldn't happen
156
	 */
157
	@Test
158
	public void testGetFileColls() throws ISStoreException, XMLDBException {
159
		final String[] expectedNames = new String[] { "col1", "col2" };
160

    
161
		when(xdb.getRootCollection()).thenReturn(ROOT_COLLECTION);
162
		when(xdb.listChildCollections(ROOT_COLLECTION)).thenReturn(Arrays.asList(expectedNames));
163

    
164
		final List<String> names = store.getFileColls();
165
		verify(xdb).getRootCollection();
166
		assertArrayEquals(expectedNames, names.toArray());
167
	}
168

    
169
	/**
170
	 * list file names.
171
	 * 
172
	 * @throws ISStoreException
173
	 *             shouldn't happen
174
	 * @throws XMLDBException
175
	 *             shouldn't happen
176
	 */
177
	@Test
178
	public void testGetFileNames() throws ISStoreException, XMLDBException {
179
		final String[] expectedNames = new String[] { "fil1", "file2", "file3" };
180

    
181
		when(xdb.list(ROOT_COLLECTION)).thenReturn(Arrays.asList(expectedNames));
182

    
183
		final List<String> names = store.getFileNames(ROOT_COLLECTION);
184
		assertArrayEquals(expectedNames, names.toArray());
185
	}
186

    
187
	/**
188
	 * read an xml file.
189
	 * 
190
	 * @throws ISStoreException
191
	 *             shouldn't happen
192
	 * @throws XMLDBException
193
	 *             shouldn't happen
194
	 */
195
	@Test
196
	public void testGetXML() throws ISStoreException, XMLDBException {
197
		when(xdb.read(TEST_FILE, TEST_COLLECTION)).thenReturn("testContent");
198
		final String res = store.getXML(TEST_FILE, TEST_COLLECTION);
199
		assertEquals("get xml", "testContent", res);
200
	}
201

    
202
	/**
203
	 * read an xml file using a xquery.
204
	 * 
205
	 * @throws XMLDBException
206
	 *             shouldn't happen
207
	 * @throws ISStoreException
208
	 *             shouldn't happen
209
	 */
210
	@Test
211
	public void testGetXMLbyQuery() throws XMLDBException, ISStoreException {
212
		prepareXMLDBResultSet();
213

    
214
		final String res = store.getXMLbyQuery(SOME_QUERY);
215
		assertEquals("get xml by query", ONE, res);
216
	}
217

    
218
	/**
219
	 * helper method. Prepares a common result mock.
220
	 * 
221
	 * @throws XMLDBException
222
	 *             the declaration of this exception is just a syntactic necessity
223
	 */
224
	private void prepareXMLDBResultSet() throws XMLDBException {
225
		List<String> myList = Arrays.asList(ONE,TWO);
226
		when(xdb.xquery(SOME_QUERY)).thenReturn(myList.iterator());
227
	}
228

    
229
	/**
230
	 * insert a new xml file.
231
	 * 
232
	 * @throws ISStoreException
233
	 *             shouldn't happen
234
	 * @throws XMLDBException
235
	 *             shouldn't happen
236
	 */
237
	@Test
238
	public void testInsertXML() throws ISStoreException, XMLDBException {
239
		assertTrue("insert xml", store.insertXML("example", ROOT_COLLECTION, "<hello/>"));
240
		verify(xdb).create("example", ROOT_COLLECTION, "<hello/>");
241
	}
242

    
243
	/**
244
	 * test quick search xml.
245
	 * 
246
	 * @throws ISStoreException
247
	 *             happens
248
	 * @throws XMLDBException
249
	 *             happens
250
	 */
251
	@Test
252
	public void testQuickSearchXML() throws ISStoreException, XMLDBException {
253
		final List<String> ans = new ArrayList<String>();
254
		ans.add(ONE);
255
		ans.add(TWO);
256

    
257
		prepareXMLDBResultSet();
258

    
259
		final ArrayList<String> res = (ArrayList<String>) store.quickSearchXML(SOME_QUERY);
260
		assertNotNull("query returned", res);
261
		assertEquals("correct size", 2, res.size());
262
		assertEquals("first value", ONE, res.get(0));
263
		assertEquals("second value", TWO, res.get(1));
264
	}
265

    
266
}
(2-2/2)