Project

General

Profile

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

    
3
import static org.junit.Assert.assertEquals;
4
import static org.junit.Assert.assertFalse;
5
import static org.junit.Assert.assertNotNull;
6
import static org.junit.Assert.assertTrue;
7
import static org.mockito.Matchers.anyObject;
8
import static org.mockito.Matchers.anyString;
9
import static org.mockito.Matchers.eq;
10
import static org.mockito.Mockito.doThrow;
11
import static org.mockito.Mockito.never;
12
import static org.mockito.Mockito.verify;
13
import static org.mockito.Mockito.when;
14

    
15
import java.io.IOException;
16
import java.io.StringWriter;
17

    
18
import javax.xml.parsers.ParserConfigurationException;
19
import javax.xml.xpath.XPathExpressionException;
20

    
21
import org.apache.commons.io.IOUtils;
22
import org.junit.Before;
23
import org.junit.Test;
24
import org.junit.runner.RunWith;
25
import org.mockito.Mock;
26
import org.mockito.junit.MockitoJUnitRunner;
27
import org.xml.sax.SAXException;
28

    
29
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpException;
30
import eu.dnetlib.enabling.is.lookup.rmi.ISLookUpService;
31
import eu.dnetlib.enabling.is.registry.rmi.ISRegistryException;
32
import eu.dnetlib.enabling.is.registry.schema.OpaqueResourceValidator;
33
import eu.dnetlib.enabling.is.registry.schema.ValidationException;
34
import eu.dnetlib.enabling.is.registry.validation.ProfileValidationStrategy;
35
import eu.dnetlib.enabling.is.registry.validation.RegistrationPhase;
36
import eu.dnetlib.enabling.is.store.rmi.ISStoreException;
37
import eu.dnetlib.enabling.is.store.rmi.ISStoreService;
38
import eu.dnetlib.enabling.tools.CompatResourceIdentifierResolverImpl;
39
import eu.dnetlib.enabling.tools.OpaqueResource;
40
import eu.dnetlib.enabling.tools.ResourceType;
41
import eu.dnetlib.enabling.tools.StringOpaqueResource;
42
import eu.dnetlib.enabling.tools.XQueryUtils;
43
import eu.dnetlib.enabling.tools.XQueryUtilsImpl;
44

    
45
/**
46
 * test registry service.
47
 * 
48
 * @author marko
49
 * 
50
 */
51
@RunWith(MockitoJUnitRunner.class)
52
public class ISRegistryServiceImplTest { // NOPMD
53

    
54
	/**
55
	 * a test resource type.
56
	 */
57
	private static final String REPOSITORY_TYPE = "RepositoryServiceResourceType";
58

    
59
	/**
60
	 * TODO: use a shared constant for this.
61
	 */
62
	private static final String DB_DRIVER = "/db/DRIVER/";
63

    
64
	/**
65
	 * class under test.
66
	 */
67
	private transient ISRegistryServiceImpl registry;
68

    
69
	/**
70
	 * store service mock.
71
	 */
72
	@Mock
73
	private transient ISStoreService storeService;
74

    
75
	/**
76
	 * lookup service mock.
77
	 */
78
	@Mock
79
	private transient ISLookUpService lookUpService;
80

    
81
	/**
82
	 * xml source of a test profile, loaded from test-profile.xml.
83
	 */
84
	private transient String profileSource;
85

    
86
	/**
87
	 * resource identifier resolver, converts IDs to filename/collection pairs.
88
	 */
89
	private transient CompatResourceIdentifierResolverImpl resIdResolver;
90

    
91
	/**
92
	 * schema validator.
93
	 */
94
	@Mock
95
	private transient OpaqueResourceValidator validator;
96

    
97
	/**
98
	 * xquery utils, used for xmldb root path.
99
	 */
100
	private final transient XQueryUtils xqueryUtils = new XQueryUtilsImpl();
101

    
102
	/**
103
	 * used to validate resources w.r.t. a set of defined properties.
104
	 */
105
	@Mock
106
	private ProfileValidationStrategy profileValidationStrategy;
107

    
108
	/**
109
	 * common initialization.
110
	 * 
111
	 * @throws IOException
112
	 *             shouldn't happen
113
	 */
114
	@Before
115
	public void setUp() throws IOException {
116
		registry = new ISRegistryServiceImpl();
117
		registry.setXqueryUtils(new XQueryUtilsImpl());
118
		resIdResolver = new CompatResourceIdentifierResolverImpl();
119
		registry.setResIdResolver(resIdResolver);
120

    
121
		final CompatPendingResourceManagerImpl pendingManager = new CompatPendingResourceManagerImpl();
122
		pendingManager.setResourceKindResolver(new ApplicationProfileResourceKindResolver());
123
		pendingManager.setProfileValidationStrategy(profileValidationStrategy);
124

    
125
		registry.setPendingManager(pendingManager);
126
		pendingManager.setIsRegistry(registry);
127
		registry.setIsStore(storeService);
128
		registry.setIsLookup(lookUpService);
129

    
130
		registry.setResourceValidator(validator);
131

    
132
		final StringWriter writer = new StringWriter();
133
		IOUtils.copy(getClass().getResourceAsStream("test-profile.xml"), writer);
134
		profileSource = writer.getBuffer().toString();
135

    
136
		registry.setProfileValidationStrategy(profileValidationStrategy);
137
	}
138

    
139
	/**
140
	 * test the deletion of a profile.
141
	 * 
142
	 * @throws ISRegistryException
143
	 *             shouldn't happen
144
	 * @throws XPathExpressionException
145
	 *             shouldn't happen
146
	 * @throws SAXException
147
	 *             shouldn't happen
148
	 * @throws IOException
149
	 *             shouldn't happen
150
	 * @throws ParserConfigurationException
151
	 *             shouldn't happen
152
	 * @throws ISStoreException
153
	 *             shouldn't happen
154
	 */
155
	@Test
156
	public void testDeleteProfile() throws ISRegistryException, XPathExpressionException, SAXException, IOException, ParserConfigurationException,
157
			ISStoreException {
158
		final OpaqueResource resource = new StringOpaqueResource(profileSource);
159
		final String resId = resource.getResourceId();
160

    
161
		boolean catched = false; // NOPMD
162
		try {
163
			registry.deleteProfile(resId);
164
		} catch (ISRegistryDocumentNotFoundException e) {
165
			catched = true;
166
		}
167

    
168
		assertTrue("exception raised", catched);
169
		verify(storeService).deleteXML(registry.getResIdResolver().getFileName(resId), DB_DRIVER + registry.getResIdResolver().getCollectionName(resId));
170

    
171
		assertNotNull("dummy", storeService);
172
	}
173

    
174
	/**
175
	 * test the deletion of an inexitent profile. It should throwa specific exception
176
	 * 
177
	 * @throws ISRegistryException
178
	 *             shouldn't happen
179
	 * @throws XPathExpressionException
180
	 *             shouldn't happen
181
	 * @throws SAXException
182
	 *             shouldn't happen
183
	 * @throws IOException
184
	 *             shouldn't happen
185
	 * @throws ParserConfigurationException
186
	 *             shouldn't happen
187
	 * @throws ISStoreException
188
	 *             shouldn't happen
189
	 */
190
	@Test(expected = ISRegistryDocumentNotFoundException.class)
191
	public void testDeleteInexistentProfile() throws ISRegistryException, XPathExpressionException, SAXException, IOException,
192
			ParserConfigurationException, ISStoreException {
193
		final OpaqueResource resource = new StringOpaqueResource(profileSource);
194
		final String resId = resource.getResourceId();
195
		final String wrongId = "xx" + resId;
196

    
197
		when(storeService.deleteXML(registry.getResIdResolver().getFileName(wrongId), DB_DRIVER + registry.getResIdResolver().getCollectionName(wrongId)))
198
				.thenReturn(false);
199

    
200
		registry.deleteProfile(wrongId);
201
	}
202

    
203
	/**
204
	 * test register profile.
205
	 * 
206
	 * @throws ISRegistryException
207
	 *             shouldn't happen
208
	 * @throws ISStoreException
209
	 *             shouldn't happen
210
	 * @throws IOException
211
	 *             shouldn't happen
212
	 * @throws ParserConfigurationException
213
	 *             shouldn't happen
214
	 * @throws SAXException
215
	 *             shouldn't happen
216
	 * @throws XPathExpressionException
217
	 *             shouldn't happen
218
	 * @throws ValidationException
219
	 */
220
	@Test
221
	public void testRegisterProfile() throws ISRegistryException, ISStoreException, IOException, XPathExpressionException, SAXException,
222
			ParserConfigurationException, ValidationException {
223

    
224
		when(profileValidationStrategy.accept((OpaqueResource) anyObject(), eq(RegistrationPhase.Register))).thenReturn(true);
225
		final String newId = registry.registerProfile(profileSource);
226

    
227
		verify(storeService).insertXML(eq(resIdResolver.getFileName(newId)), eq(DB_DRIVER + resIdResolver.getCollectionName(newId)), anyString());
228

    
229
		final OpaqueResource resource = new StringOpaqueResource(profileSource);
230
		assertFalse("new identifier is generated", newId.equals(resource.getResourceId()));
231

    
232
	}
233

    
234
	/**
235
	 * test update profile.
236
	 * 
237
	 * @throws XPathExpressionException
238
	 *             should'nt happen
239
	 * @throws SAXException
240
	 *             should'nt happen
241
	 * @throws IOException
242
	 *             should'nt happen
243
	 * @throws ParserConfigurationException
244
	 *             should'nt happen
245
	 * @throws ISRegistryException
246
	 *             should'nt happen
247
	 * @throws ISStoreException
248
	 *             should'nt happen
249
	 */
250
	@Test
251
	public void testUpdateProfile() throws XPathExpressionException, SAXException, IOException, ParserConfigurationException, ISRegistryException,
252
			ISStoreException {
253
		final OpaqueResource resource = new StringOpaqueResource(profileSource);
254

    
255
		final String fileName = resIdResolver.getFileName(resource.getResourceId());
256
		final String fileColl = resIdResolver.getCollectionName(resource.getResourceId());
257
		when(storeService.updateXML(eq(fileName), eq(DB_DRIVER + fileColl), anyString())).thenReturn(true);
258

    
259
		when(storeService.getXML(fileName, DB_DRIVER + fileColl)).thenReturn(resource.asString());
260

    
261
		final boolean res = registry.updateProfile(resource.getResourceId(), resource.asString(), resource.getResourceType());
262
		assertTrue("success", res);
263
	}
264

    
265
	/**
266
	 * Check that the xml schema is correctly validated even on updateProfile.
267
	 * 
268
	 * @throws XPathExpressionException
269
	 *             cannot happen
270
	 * @throws SAXException
271
	 *             cannot happen
272
	 * @throws IOException
273
	 *             cannot happen
274
	 * @throws ParserConfigurationException
275
	 *             cannot happen
276
	 * @throws ISRegistryException
277
	 *             mock
278
	 * @throws ValidationException
279
	 *             mock
280
	 */
281
	@Test(expected = ISRegistryException.class)
282
	public void testUpdateProfileSchemaValidate() throws XPathExpressionException, SAXException, IOException, ParserConfigurationException,
283
			ISRegistryException, ValidationException {
284
		final OpaqueResource resource = new StringOpaqueResource(profileSource);
285

    
286
		//doThrow(new ValidationException("invalid xml")).when(validator).validate((OpaqueResource) anyObject());
287

    
288
		registry.updateProfile(resource.getResourceId(), "<invalid/>", resource.getResourceType());
289
	}
290

    
291
	/**
292
	 * test profile validation.
293
	 * 
294
	 * @throws XPathExpressionException
295
	 *             shouldn't happen
296
	 * @throws SAXException
297
	 *             shouldn't happen
298
	 * @throws IOException
299
	 *             shouldn't happen
300
	 * @throws ParserConfigurationException
301
	 *             shouldn't happen
302
	 * @throws ISRegistryException
303
	 *             shouldn't happen
304
	 * @throws ISStoreException
305
	 *             shouldn't happen
306
	 * @throws ISLookUpException
307
	 *             shouldn't happen
308
	 * @throws ValidationException
309
	 */
310
	@Test
311
	public void testValidateProfile() throws XPathExpressionException, SAXException, IOException, ParserConfigurationException, ISRegistryException,
312
			ISStoreException, ISLookUpException, ValidationException {
313
		final OpaqueResource resource = new StringOpaqueResource(profileSource);
314

    
315
		final String oldId = registry.insertProfileForValidation(resource.getResourceType(), resource.asString());
316
		resource.setResourceKind("PendingRepositoryResources"); // simulate the validation
317

    
318
		resource.setResourceId(oldId);
319
		when(lookUpService.getResourceProfile(oldId)).thenReturn(resource.asString());
320
		when(storeService.deleteXML(resIdResolver.getFileName(oldId), DB_DRIVER + resIdResolver.getCollectionName(oldId))).thenReturn(true);
321
		when(profileValidationStrategy.accept((OpaqueResource) anyObject(), eq(RegistrationPhase.Register))).thenReturn(true);
322

    
323
		final String newId = registry.validateProfile(oldId);
324

    
325
		verify(storeService).deleteXML(resIdResolver.getFileName(oldId), DB_DRIVER + resIdResolver.getCollectionName(oldId));
326
		verify(storeService).insertXML(eq(resIdResolver.getFileName(newId)), eq(DB_DRIVER + resIdResolver.getCollectionName(newId)), anyString());
327

    
328
		assertFalse("the id should be be unstable", resIdResolver.getFileName(oldId).equals(resIdResolver.getFileName(newId)));
329
	}
330

    
331
	/**
332
	 * test invalidate profile.
333
	 * 
334
	 * @throws XPathExpressionException
335
	 *             shouldn't happen
336
	 * @throws SAXException
337
	 *             shouldn't happen
338
	 * @throws IOException
339
	 *             shouldn't happen
340
	 * @throws ParserConfigurationException
341
	 *             shouldn't happen
342
	 * @throws ISRegistryException
343
	 *             shouldn't happen
344
	 * @throws ISStoreException
345
	 *             shouldn't happen
346
	 * @throws ISLookUpException
347
	 *             shouldn't happen
348
	 */
349
	@Test
350
	public void testInvalidateProfile() throws XPathExpressionException, SAXException, IOException, ParserConfigurationException, ISRegistryException,
351
			ISStoreException, ISLookUpException {
352

    
353
		final OpaqueResource resource = new StringOpaqueResource(profileSource);
354
		resource.setResourceKind("PendingResources");
355

    
356
		final String oldId = registry.registerProfile(resource.asString());
357

    
358
		resource.setResourceId(oldId);
359
		when(lookUpService.getResourceProfile(oldId)).thenReturn(resource.asString());
360
		when(storeService.deleteXML(resIdResolver.getFileName(oldId), DB_DRIVER + resIdResolver.getCollectionName(oldId))).thenReturn(true);
361

    
362
		final String newId = registry.invalidateProfile(oldId);
363

    
364
		verify(storeService).deleteXML(resIdResolver.getFileName(oldId), DB_DRIVER + resIdResolver.getCollectionName(oldId));
365
		verify(storeService).insertXML(eq(resIdResolver.getFileName(newId)), eq(DB_DRIVER + resIdResolver.getCollectionName(newId)), anyString());
366

    
367
		assertFalse("the id should be be unstable", resIdResolver.getFileName(oldId).equals(resIdResolver.getFileName(newId)));
368

    
369
	}
370

    
371
	/**
372
	 * test that the resource kind is correctly changed when inserting for validation.
373
	 * 
374
	 * @throws IOException
375
	 *             shouldn't happen
376
	 * @throws ISRegistryException
377
	 *             shouldn't happen
378
	 * @throws ISStoreException
379
	 *             shouldn't happen
380
	 * @throws XPathExpressionException
381
	 *             shouldn't happen
382
	 * @throws SAXException
383
	 *             shouldn't happen
384
	 * @throws ParserConfigurationException
385
	 *             shouldn't happen
386
	 */
387
	@Test
388
	public void testInsertProfileForValidation() throws IOException, ISRegistryException, ISStoreException, XPathExpressionException, SAXException,
389
			ParserConfigurationException {
390

    
391
		final OpaqueResource resource = new StringOpaqueResource(profileSource);
392
		final String newId = registry.insertProfileForValidation(resource.getResourceType(), resource.asString());
393

    
394
		verify(storeService).insertXML(eq(resIdResolver.getFileName(newId)), eq(DB_DRIVER + "PendingRepositoryResources/RepositoryServiceResourceType"),
395
				anyString());
396

    
397
		verify(storeService, never()).deleteXML(anyString(), anyString());
398

    
399
		assertEquals("check pending id", "PendingRepositoryResources/RepositoryServiceResourceType", resIdResolver.getCollectionName(newId));
400
	}
401

    
402
	/**
403
	 * The ISRegistry's insertProfileForValidation contract specifies that an exception should be raised if the argument doesn't match the
404
	 * type.
405
	 * 
406
	 * @throws IOException
407
	 *             shouldn't happen
408
	 * @throws ISRegistryException
409
	 *             expected
410
	 */
411
	@Test(expected = ISRegistryException.class)
412
	public void testInsertProfileForValidationInvalid() throws IOException, ISRegistryException {
413
		registry.insertProfileForValidation("WrongRepositoryServiceResourceType", profileSource);
414
	}
415

    
416
	/**
417
	 * test resource type addition.
418
	 * 
419
	 * @throws ISRegistryException
420
	 *             shouldn't happen
421
	 * @throws IOException
422
	 *             shouldn't happen
423
	 * @throws ISStoreException
424
	 *             cannot happen, mock
425
	 */
426
	@Test
427
	public void testAddResourceType() throws ISRegistryException, IOException, ISStoreException {
428
		final StringWriter resourceSchema = new StringWriter();
429

    
430
		IOUtils.copy(getClass().getResourceAsStream("RepositoryServiceResourceType.xsd"), resourceSchema);
431
		assertNotNull("check schema", resourceSchema.toString());
432

    
433
		registry.addResourceType(REPOSITORY_TYPE, resourceSchema.toString());
434

    
435
		verify(storeService).insertXML(REPOSITORY_TYPE, xqueryUtils.getRootCollection() + ResourceType.RESOURCE_TYPES, resourceSchema.toString());
436
	}
437

    
438
	/**
439
	 * test resource type deletion.
440
	 * 
441
	 * @throws ISRegistryException
442
	 *             shouldn't happen
443
	 * @throws ISStoreException
444
	 *             cannot happen, mock
445
	 */
446
	@Test
447
	public void testDeleteResourceType() throws ISRegistryException, ISStoreException {
448
		assertNotNull("dummy", registry);
449

    
450
		registry.deleteResourceType(REPOSITORY_TYPE, false);
451

    
452
		verify(storeService).deleteXML(REPOSITORY_TYPE, xqueryUtils.getRootCollection() + ResourceType.RESOURCE_TYPES);
453
	}
454

    
455
	/**
456
	 * register secure profile.
457
	 * 
458
	 * @throws ISRegistryException
459
	 *             shouldn't happen
460
	 * @throws ISLookUpException
461
	 *             mocked
462
	 * @throws IOException
463
	 *             shouldn't happen
464
	 * @throws ISStoreException
465
	 *             mock
466
	 * @throws ValidationException
467
	 */
468
	@Test
469
	public void testRegisterSecureProfile() throws ISRegistryException, ISLookUpException, IOException, ISStoreException, ValidationException { // NOPMD
470
		final String resourceProfId = "12-70b2a9e0-7d53-4ffb-be43-f3c571ae21a6_VXNlckRTUmVzb3VyY2VzL1VzZXJEU1Jlc291cmNlVHlwZQ==";
471
		final String secureProfId = "sec123_U2VjdXJpdHlQcm9maWxlRFNSZXNvdXJjZXMvU2VjdXJpdHlQcm9maWxlRFNSZXNvdXJjZVR5cGU=";
472

    
473
		final StringWriter profile = new StringWriter();
474
		IOUtils.copy(getClass().getResourceAsStream("userProfile.xml"), profile);
475
		when(lookUpService.getResourceProfile(resourceProfId)).thenReturn(profile.toString());
476
		when(profileValidationStrategy.accept((OpaqueResource) anyObject(), eq(RegistrationPhase.Register))).thenReturn(true);
477

    
478
		final StringWriter secure = new StringWriter();
479
		IOUtils.copy(getClass().getResourceAsStream("securityProfile.xml"), secure);
480
		when(lookUpService.getResourceProfile(secureProfId)).thenReturn(secure.toString());
481
		when(storeService.deleteXML(resIdResolver.getFileName(resourceProfId), DB_DRIVER + resIdResolver.getCollectionName(resourceProfId))).thenReturn(
482
				true);
483
		when(storeService.updateXML(eq("sec123"), eq(DB_DRIVER + "SecurityProfileDSResources/SecurityProfileDSResourceType"), anyString())).thenReturn(
484
				true);
485
		when(storeService.getXML("sec123", DB_DRIVER + "SecurityProfileDSResources/SecurityProfileDSResourceType")).thenReturn("<RESOURCE_PROFILE/>");
486

    
487
		registry.registerSecureProfile(resourceProfId, secureProfId);
488
	}
489
}
(3-3/6)