Project

General

Profile

1
package eu.dnetlib.enabling.is.store; // NOPMD
2

    
3
import static org.junit.Assert.assertArrayEquals;
4
import static org.junit.Assert.assertEquals;
5
import static org.junit.Assert.assertNotNull;
6
import static org.junit.Assert.assertTrue; // NOPMD
7
import static org.mockito.Matchers.anyObject;
8
import static org.mockito.Mockito.doAnswer;
9
import static org.mockito.Mockito.mock;
10
import static org.mockito.Mockito.verify;
11
import static org.mockito.Mockito.when; // NOPMD
12

    
13
import java.util.ArrayList;
14
import java.util.Arrays;
15
import java.util.Iterator;
16
import java.util.List;
17

    
18
import javax.xml.namespace.QName;
19

    
20
import org.apache.cxf.endpoint.Endpoint;
21
import org.apache.cxf.endpoint.ServerImpl;
22
import org.apache.cxf.jaxws.EndpointImpl;
23
import org.apache.cxf.service.Service;
24
import org.apache.cxf.service.model.EndpointInfo;
25
import org.junit.After;
26
import org.junit.Before;
27
import org.junit.Test;
28
import org.junit.runner.RunWith;
29
import org.mockito.Mock;
30
import org.mockito.invocation.InvocationOnMock;
31
import org.mockito.junit.MockitoJUnitRunner;
32
import org.mockito.stubbing.Answer;
33
import org.xmldb.api.base.XMLDBException;
34

    
35
import eu.dnetlib.enabling.is.store.rmi.ISStoreException;
36
import eu.dnetlib.enabling.is.store.rmi.ISStoreService;
37
import eu.dnetlib.enabling.resultset.LocalResultSetFactoryImpl;
38
import eu.dnetlib.enabling.resultset.LocalResultSetImpl;
39
import eu.dnetlib.enabling.resultset.ResultSet;
40
import eu.dnetlib.enabling.resultset.ResultSetRegistry;
41
import eu.dnetlib.enabling.resultset.ResultSetServiceImpl;
42
import eu.dnetlib.soap.cxf.JaxwsEndpointReferenceBuilder;
43
import eu.dnetlib.soap.cxf.StandaloneCxfEndpointReferenceBuilder;
44
import eu.dnetlib.xml.database.XMLDatabase;
45

    
46
/**
47
 * test the ISStore service.
48
 * 
49
 * @author marko
50
 * 
51
 */
52
@RunWith(MockitoJUnitRunner.class)
53
public class ISStoreServiceTest { // NOPMD by marko on 11/24/08 4:54 PM
54

    
55
	/**
56
	 * root collection name for mocked xmldb.
57
	 */
58
	private static final String ROOT_COLLECTION = "/db";
59
	/**
60
	 * test resultset id.
61
	 */
62
	private static final String SOME_RS_ID = "someRsId";
63
	/**
64
	 * some test query.
65
	 */
66
	private static final String SOME_QUERY = "someQuery";
67
	/**
68
	 * a test collection name.
69
	 */
70
	private static final String TEST_COLLECTION = "testCollection";
71
	/**
72
	 * a test string value.
73
	 */
74
	private static final String ONE = "one";
75
	/**
76
	 * another test resultset value.
77
	 */
78
	private static final String TWO = "two";
79
	/**
80
	 * name of the a test file.
81
	 */
82
	private static final String TEST_FILE = "testFile";
83

    
84
	/**
85
	 * the ISStoreService under test.
86
	 */
87
	private transient ISStoreService store = null;
88
	/**
89
	 * the ISStoreService under test. Used only in setUp/tearDown().
90
	 */
91
	private transient ISStoreServiceImpl ismo = null;
92
	/**
93
	 * the XMLDatabase injected into the ISStore.
94
	 */
95
	@Mock
96
	private transient XMLDatabase xdb;
97

    
98
	/**
99
	 * resultset service mock. needed only for resultset testing.
100
	 */
101
	@Mock
102
	private transient ResultSetServiceImpl resultSetService;
103

    
104
	/**
105
	 * jaxws endpoint mock. needed for resultset testing.
106
	 */
107
	@Mock
108
	private transient EndpointImpl jaxwsEndpoint;
109

    
110
	/**
111
	 * cxf server mock. needed for resultset testing.
112
	 */
113
	@Mock
114
	private transient ServerImpl cxfServer;
115

    
116
	/**
117
	 * cxf endpoint mock. needed for resultset testing.
118
	 */
119
	@Mock
120
	private transient Endpoint endpoint;
121

    
122
	/**
123
	 * cxf service mock. needed for resultset testing.
124
	 */
125
	@Mock
126
	private transient Service service;
127

    
128
	/**
129
	 * cxf endpoint info mock. needed for resultset testing.
130
	 */
131
	@Mock
132
	private transient EndpointInfo endpointInfo;
133

    
134
	/**
135
	 * resultset registry mock. needed for resultset testing.
136
	 */
137
	@Mock
138
	private transient ResultSetRegistry resultSetRegistry;
139

    
140
	/**
141
	 * hook for inspecting the created resultset object.
142
	 */
143
	private transient LocalResultSetImpl resultSetImpl;
144

    
145
	/**
146
	 * setup the ISStore instance, injecting all the dependencies.
147
	 * 
148
	 * @throws Exception
149
	 *             shouldn't throw
150
	 */
151
	@Before
152
	public void setUp() throws Exception {
153
		ismo = new ISStoreServiceImpl();
154

    
155
		ismo.setXmlDatabase(xdb);
156
		ismo.start();
157

    
158
		store = ismo;
159
	}
160

    
161
	/**
162
	 * Setup a fake resultset with all the necessary endpoint mocks used to generate an actual W3CEndpoint.
163
	 */
164
	public void setUpResultSet() {
165
		final StandaloneCxfEndpointReferenceBuilder cxfEprBuilder = new StandaloneCxfEndpointReferenceBuilder();
166
		cxfEprBuilder.setBaseAddress("http://dnetlib.eu");
167
		final JaxwsEndpointReferenceBuilder eprBuilder = new JaxwsEndpointReferenceBuilder();
168
		eprBuilder.setBuilder(cxfEprBuilder);
169

    
170
		when(resultSetService.getEprBuilder()).thenReturn(eprBuilder);
171

    
172
		final LocalResultSetFactoryImpl resultSetFactory = new LocalResultSetFactoryImpl();
173
		resultSetFactory.setResultSetService(resultSetService);
174

    
175
		setUpRSEndpoint();
176

    
177
		when(resultSetService.getEndpoint()).thenReturn(jaxwsEndpoint);
178
		when(jaxwsEndpoint.getServer()).thenReturn(cxfServer);
179
		when(cxfServer.getEndpoint()).thenReturn(endpoint);
180

    
181
		when(resultSetService.getResultsetRegistry()).thenReturn(resultSetRegistry);
182
		doAnswer(new Answer<Void>() {
183

    
184
			@Override
185
			public Void answer(final InvocationOnMock invocation) throws Throwable {
186
				resultSetImpl = (LocalResultSetImpl) invocation.getArguments()[0];
187
				resultSetImpl.setIdentifier(SOME_RS_ID);
188
				return null;
189
			}
190
		}).when(resultSetRegistry).addResultSet((ResultSet) anyObject());
191

    
192
		ismo.setResultSetFactory(resultSetFactory);
193
	}
194

    
195
	/**
196
	 * create resultset endpoint.
197
	 */
198
	private void setUpRSEndpoint() {
199
		when(service.getName()).thenReturn(new QName("http://my.test", "TestService"));
200

    
201
		when(endpoint.getEndpointInfo()).thenReturn(endpointInfo);
202
		when(endpoint.getService()).thenReturn(service);
203
		when(endpointInfo.getAddress()).thenReturn("http://localhost/something");
204
		when(endpointInfo.getName()).thenReturn(new QName("http://my.test", "TestServiceEndpoint"));
205
	}
206

    
207
	/**
208
	 * respect the service lifecycle.
209
	 */
210
	@After
211
	public void tearDown() {
212
		ismo.stop();
213
	}
214

    
215
	/**
216
	 * create collection.
217
	 * 
218
	 * @throws ISStoreException
219
	 *             shouldn't happen
220
	 * @throws XMLDBException
221
	 *             shouldn't happen
222
	 */
223
	@Test
224
	public void testCreateFileColl() throws ISStoreException, XMLDBException {
225
		assertTrue("create collection", store.createFileColl(TEST_COLLECTION));
226
		verify(xdb).createCollection(TEST_COLLECTION);
227
	}
228

    
229
	/**
230
	 * delete collection.
231
	 * 
232
	 * @throws XMLDBException
233
	 *             shouldn't happen
234
	 * @throws ISStoreException
235
	 *             shouldn't happen
236
	 */
237
	@Test
238
	public void testDeleteFileColl() throws XMLDBException, ISStoreException {
239
		assertTrue("delete collection", store.deleteFileColl(TEST_COLLECTION));
240
		verify(xdb).removeCollection(TEST_COLLECTION);
241
	}
242

    
243
	/**
244
	 * delete an xml file.
245
	 * 
246
	 * @throws ISStoreException
247
	 *             shouldn't happen
248
	 * @throws XMLDBException
249
	 *             shouldn't happen
250
	 */
251
	@Test
252
	public void testDeleteXML() throws ISStoreException, XMLDBException {
253
		when(xdb.remove(TEST_FILE, TEST_COLLECTION)).thenReturn(true);
254

    
255
		assertTrue("delete xml", store.deleteXML(TEST_FILE, TEST_COLLECTION));
256
		verify(xdb).remove(TEST_FILE, TEST_COLLECTION);
257
	}
258

    
259
	/**
260
	 * execute an XUpdate.
261
	 */
262
	// @Test
263
	// public void testExecuteXUpdate() {
264
	// fail(NYI);
265
	// }
266
	/**
267
	 * get collection names.
268
	 * 
269
	 * @throws ISStoreException
270
	 *             shouldn't happen
271
	 * @throws XMLDBException
272
	 *             shouldn't happen
273
	 */
274
	@Test
275
	public void testGetFileColls() throws ISStoreException, XMLDBException {
276
		final String[] expectedNames = new String[] { "col1", "col2" };
277

    
278
		when(xdb.getRootCollection()).thenReturn(ROOT_COLLECTION);
279
		when(xdb.listChildCollections(ROOT_COLLECTION)).thenReturn(Arrays.asList(expectedNames));
280

    
281
		final List<String> names = store.getFileColls();
282
		verify(xdb).getRootCollection();
283
		assertArrayEquals(expectedNames, names.toArray());
284
	}
285

    
286
	/**
287
	 * list file names.
288
	 * 
289
	 * @throws ISStoreException
290
	 *             shouldn't happen
291
	 * @throws XMLDBException
292
	 *             shouldn't happen
293
	 */
294
	@Test
295
	public void testGetFileNames() throws ISStoreException, XMLDBException {
296
		final String[] expectedNames = new String[] { "fil1", "file2", "file3" };
297

    
298
		when(xdb.list(ROOT_COLLECTION)).thenReturn(Arrays.asList(expectedNames));
299

    
300
		final List<String> names = store.getFileNames(ROOT_COLLECTION);
301
		assertArrayEquals(expectedNames, names.toArray());
302
	}
303

    
304
	/**
305
	 * read an xml file.
306
	 * 
307
	 * @throws ISStoreException
308
	 *             shouldn't happen
309
	 * @throws XMLDBException
310
	 *             shouldn't happen
311
	 */
312
	@Test
313
	public void testGetXML() throws ISStoreException, XMLDBException {
314
		when(xdb.read(TEST_FILE, TEST_COLLECTION)).thenReturn("testContent");
315
		final String res = store.getXML(TEST_FILE, TEST_COLLECTION);
316
		assertEquals("get xml", "testContent", res);
317
	}
318

    
319
	/**
320
	 * read an xml file using a xquery.
321
	 * 
322
	 * @throws XMLDBException
323
	 *             shouldn't happen
324
	 * @throws ISStoreException
325
	 *             shouldn't happen
326
	 */
327
	@Test
328
	public void testGetXMLbyQuery() throws XMLDBException, ISStoreException {
329
		prepareXMLDBResultSet();
330

    
331
		final String res = store.getXMLbyQuery(SOME_QUERY);
332
		assertEquals("get xml by query", ONE, res);
333
	}
334

    
335
	/**
336
	 * helper method. Prepares a common result mock.
337
	 * 
338
	 * @throws XMLDBException
339
	 *             the declaration of this exception is just a syntactic necessity
340
	 */
341
	private void prepareXMLDBResultSet() throws XMLDBException {
342
		List<String> myList = Arrays.asList(ONE,TWO);
343
		when(xdb.xquery(SOME_QUERY)).thenReturn(myList.iterator());
344
	}
345

    
346
	/**
347
	 * insert a new xml file.
348
	 * 
349
	 * @throws ISStoreException
350
	 *             shouldn't happen
351
	 * @throws XMLDBException
352
	 *             shouldn't happen
353
	 */
354
	@Test
355
	public void testInsertXML() throws ISStoreException, XMLDBException {
356
		assertTrue("insert xml", store.insertXML("example", ROOT_COLLECTION, "<hello/>"));
357
		verify(xdb).create("example", ROOT_COLLECTION, "<hello/>");
358
	}
359

    
360
	/**
361
	 * test quick search xml.
362
	 * 
363
	 * @throws ISStoreException
364
	 *             happens
365
	 * @throws XMLDBException
366
	 *             happens
367
	 */
368
	@Test
369
	public void testQuickSearchXML() throws ISStoreException, XMLDBException {
370
		final List<String> ans = new ArrayList<String>();
371
		ans.add(ONE);
372
		ans.add(TWO);
373

    
374
		prepareXMLDBResultSet();
375

    
376
		final ArrayList<String> res = (ArrayList<String>) store.quickSearchXML(SOME_QUERY);
377
		assertNotNull("query returned", res);
378
		assertEquals("correct size", 2, res.size());
379
		assertEquals("first value", ONE, res.get(0));
380
		assertEquals("second value", TWO, res.get(1));
381
	}
382

    
383
}
(2-2/2)