]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
b30fe5f1ec787432fc83287f8e41532e6aab86cf
[tmp/jakarta-migration.git] /
1 /**
2  * This document is a part of the source code and related artifacts
3  * for CollectionSpace, an open source collections management system
4  * for museums and related institutions:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c)) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  */
23 package org.collectionspace.services.client.test;
24
25 import java.util.ArrayList;
26 import java.util.HashMap;
27 import java.util.List;
28 import java.util.Map;
29
30 import javax.ws.rs.core.Response;
31
32 import org.collectionspace.services.MaterialJAXBSchema;
33 import org.collectionspace.services.client.AbstractCommonListUtils;
34 import org.collectionspace.services.client.AuthorityClient;
35 import org.collectionspace.services.client.CollectionSpaceClient;
36 import org.collectionspace.services.client.PayloadOutputPart;
37 import org.collectionspace.services.client.PoxPayloadIn;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.client.MaterialAuthorityClient;
40 import org.collectionspace.services.client.MaterialAuthorityClientUtils;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
42 import org.collectionspace.services.material.MaterialTermGroup;
43 import org.collectionspace.services.material.MaterialTermGroupList;
44 import org.collectionspace.services.material.MaterialauthoritiesCommon;
45 import org.collectionspace.services.material.MaterialsCommon;
46
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49 import org.testng.Assert;
50 import org.testng.annotations.AfterClass;
51 import org.testng.annotations.Test;
52
53 /**
54  * MaterialAuthorityServiceTest, carries out tests against a
55  * deployed and running MaterialAuthority Service.
56  *
57  */
58 public class MaterialAuthorityServiceTest extends AbstractAuthorityServiceTest<MaterialauthoritiesCommon, MaterialsCommon> {
59
60     /** The logger. */
61     private final String CLASS_NAME = MaterialAuthorityServiceTest.class.getName();
62     private final Logger logger = LoggerFactory.getLogger(MaterialAuthorityServiceTest.class);
63
64     @Override
65     public String getServicePathComponent() {
66         return MaterialAuthorityClient.SERVICE_PATH_COMPONENT;
67     }
68
69     @Override
70     protected String getServiceName() {
71         return MaterialAuthorityClient.SERVICE_NAME;
72     }
73     
74     public String getItemServicePathComponent() {
75         return AuthorityClient.ITEMS;
76     }   
77     
78     // Instance variables specific to this test.
79     
80 //    /** The SERVICE path component. */
81 //    final String SERVICE_PATH_COMPONENT = "materialauthorities";
82 //    
83 //    /** The ITEM service path component. */
84 //    final String ITEM_SERVICE_PATH_COMPONENT = "items";
85 //    
86
87     final String TEST_MATERIAL_TERM_DISPLAY_NAME = "SuperGlass 2";
88     final String TEST_MATERIAL_TERM_NAME = "SuperGlass";
89     final String TEST_MATERIAL_TERM_STATUS = "accepted";
90     final String TEST_MATERIAL_TERM_SOURCE = "source";
91     final String TEST_MATERIAL_TERM_SOURCE_DETAIL = "internal";
92     final String TEST_MATERIAL_DESCRIPTION = "Really strong glass";
93     final String TEST_MATERIAL_SHORT_IDENTIFIER = "superglass";
94     
95     /* (non-Javadoc)
96      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
97      */
98     @Override
99     protected CollectionSpaceClient getClientInstance() {
100         return new MaterialAuthorityClient();
101     }
102     
103     @Override
104     protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
105         return new MaterialAuthorityClient(clientPropertiesFilename);
106     }
107
108     /**
109      * Creates the item in authority.
110      *
111      * @param vcsid the vcsid
112      * @param authRefName the auth ref name
113      * @return the string
114      */
115     private String createItemInAuthority(AuthorityClient client, String vcsid, String authRefName) {
116         final String testName = "createItemInAuthority("+vcsid+","+authRefName+")"; 
117     
118         // Submit the request to the service and store the response.
119         Map<String, String> materialMap = new HashMap<String,String>();
120         // TODO Make material type and status be controlled vocabs.
121         materialMap.put(MaterialJAXBSchema.SHORT_IDENTIFIER, TEST_MATERIAL_SHORT_IDENTIFIER);
122         materialMap.put(MaterialJAXBSchema.MATERIAL_DESCRIPTION, TEST_MATERIAL_DESCRIPTION);
123         
124         List<MaterialTermGroup> terms = new ArrayList<MaterialTermGroup>();
125         MaterialTermGroup term = new MaterialTermGroup();
126         term.setTermDisplayName(TEST_MATERIAL_TERM_DISPLAY_NAME);
127         term.setTermName(TEST_MATERIAL_TERM_NAME);
128         term.setTermSource(TEST_MATERIAL_TERM_SOURCE);
129         term.setTermSourceDetail(TEST_MATERIAL_TERM_SOURCE_DETAIL);
130         term.setTermStatus(TEST_MATERIAL_TERM_STATUS);
131         terms.add(term);
132         
133         String newID = MaterialAuthorityClientUtils.createItemInAuthority(vcsid,
134                 authRefName, materialMap, terms, (MaterialAuthorityClient) client);    
135
136         // Store the ID returned from the first item resource created
137         // for additional tests below.
138         if (knownItemResourceId == null){
139             setKnownItemResource(newID, TEST_MATERIAL_SHORT_IDENTIFIER);
140             if (logger.isDebugEnabled()) {
141                 logger.debug(testName + ": knownItemResourceId=" + newID);
142             }
143         }
144         
145         // Store the IDs from any item resources created
146         // by tests, along with the IDs of their parents, so these items
147         // can be deleted after all tests have been run.
148         allResourceItemIdsCreated.put(newID, vcsid);
149
150         return newID;
151     }
152
153     /**
154      * Verify illegal item display name.
155      *
156      * @param testName the test name
157      * @throws Exception the exception
158      */
159     @Test(dataProvider="testName")
160         public void verifyIllegalItemDisplayName(String testName) throws Exception {
161                 // Perform setup for read.
162                 setupRead();
163
164                 // Submit the request to the service and store the response.
165                 MaterialAuthorityClient client = new MaterialAuthorityClient();
166                 Response res = client.readItem(knownResourceId, knownItemResourceId);
167                 MaterialsCommon material = null;
168                 try {
169                         assertStatusCode(res, testName);
170                         PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
171                         material = (MaterialsCommon) extractPart(input,
172                                         client.getItemCommonPartName(), MaterialsCommon.class);
173                         Assert.assertNotNull(material);
174                 } finally {
175                         if (res != null) {
176                                 res.close();
177                         }
178                 }
179
180                 //
181                 // Make an invalid UPDATE request, without a display name
182                 //
183                 MaterialTermGroupList termList = material.getMaterialTermGroupList();
184                 Assert.assertNotNull(termList);
185                 List<MaterialTermGroup> terms = termList.getMaterialTermGroup();
186                 Assert.assertNotNull(terms);
187                 Assert.assertTrue(terms.size() > 0);
188                 terms.get(0).setTermDisplayName(null);
189                 terms.get(0).setTermName(null);
190
191                 setupUpdateWithInvalidBody(); // we expect a failure
192
193                 // Submit the updated resource to the service and store the response.
194                 PoxPayloadOut output = new PoxPayloadOut(
195                                 MaterialAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
196                 PayloadOutputPart commonPart = output.addPart(
197                                 client.getItemCommonPartName(), material);
198                 setupUpdateWithInvalidBody(); // we expected a failure here.
199                 res = client.updateItem(knownResourceId, knownItemResourceId, output);
200                 try {
201                         assertStatusCode(res, testName);
202                 } finally {
203                         if (res != null) {
204                                 res.close();
205                         }
206                 }
207         }
208
209     /**
210      * Read item list.
211      */
212     @Test(dataProvider = "testName", groups = {"readList"},
213                dependsOnMethods = {"readList"})
214     public void readItemList(String testName) {
215         readItemList(knownAuthorityWithItems, null);
216     }
217
218     /**
219      * Read item list by authority name.
220      */
221     @Test(dataProvider = "testName", groups = {"readList"},
222             dependsOnMethods = {"readItemList"})
223     public void readItemListByAuthorityName(String testName) {
224         readItemList(null, READITEMS_SHORT_IDENTIFIER);
225     }
226     
227     /**
228      * Read item list.
229      * 
230      * @param vcsid
231      *            the vcsid
232      * @param name
233      *            the name
234      */
235     private void readItemList(String vcsid, String shortId) {
236         String testName = "readItemList";
237
238         // Perform setup.
239         setupReadList();
240
241         // Submit the request to the service and store the response.
242         MaterialAuthorityClient client = new MaterialAuthorityClient();
243         Response res = null;
244         if (vcsid != null) {
245             res = client.readItemList(vcsid, null, null);
246         } else if (shortId != null) {
247             res = client.readItemListForNamedAuthority(shortId, null, null);
248         } else {
249             Assert.fail("readItemList passed null csid and name!");
250         }
251         
252         AbstractCommonList list = null;
253         try {
254             assertStatusCode(res, testName);
255             list = res.readEntity(AbstractCommonList.class);
256         } finally {
257             if (res != null) {
258                 res.close();
259             }
260         }
261         
262         List<AbstractCommonList.ListItem> items = list.getListItem();
263         int nItemsReturned = items.size();
264         // There will be 'nItemsToCreateInList'
265         // items created by the createItemList test,
266         // all associated with the same parent resource.
267         int nExpectedItems = nItemsToCreateInList;
268         if (logger.isDebugEnabled()) {
269             logger.debug(testName + ": Expected " + nExpectedItems
270                     + " items; got: " + nItemsReturned);
271         }
272         Assert.assertEquals(nItemsReturned, nExpectedItems);
273
274         for (AbstractCommonList.ListItem item : items) {
275             String value = AbstractCommonListUtils.ListItemGetElementValue(
276                     item, MaterialJAXBSchema.REF_NAME);
277             Assert.assertTrue((null != value), "Item refName is null!");
278             value = AbstractCommonListUtils.ListItemGetElementValue(item,
279                     MaterialJAXBSchema.MATERIAL_TERM_DISPLAY_NAME);
280             Assert.assertTrue((null != value), "Item termDisplayName is null!");
281         }
282         if (logger.isTraceEnabled()) {
283             AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
284                     testName);
285         }
286     }
287
288     @Override
289     public void delete(String testName) throws Exception {
290         // Do nothing.  See localDelete().  This ensure proper test order.
291     }
292     
293     @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})    
294     public void localDelete(String testName) throws Exception {
295         super.delete(testName);
296     }
297
298     @Override
299     public void deleteItem(String testName) throws Exception {
300         // Do nothing.  We need to wait until after the test "localDelete" gets run.  When it does,
301         // its dependencies will get run first and then we can call the base class' delete method.
302     }
303     
304     @Test(dataProvider = "testName", groups = {"delete"},
305         dependsOnMethods = {"verifyIllegalItemDisplayName"})
306     public void localDeleteItem(String testName) throws Exception {
307         super.deleteItem(testName);
308     }
309     
310     // ---------------------------------------------------------------
311     // Cleanup of resources created during testing
312     // ---------------------------------------------------------------
313     
314     /**
315      * Deletes all resources created by tests, after all tests have been run.
316      *
317      * This cleanup method will always be run, even if one or more tests fail.
318      * For this reason, it attempts to remove all resources created
319      * at any point during testing, even if some of those resources
320      * may be expected to be deleted by certain tests.
321      */
322
323     @AfterClass(alwaysRun=true)
324     public void cleanUp() {
325         String noTest = System.getProperty("noTestCleanup");
326         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
327             if (logger.isDebugEnabled()) {
328                 logger.debug("Skipping Cleanup phase ...");
329             }
330             return;
331         }
332         if (logger.isDebugEnabled()) {
333             logger.debug("Cleaning up temporary resources created for testing ...");
334         }
335         String parentResourceId;
336         String itemResourceId;
337         // Clean up contact resources.
338         MaterialAuthorityClient client = new MaterialAuthorityClient();
339         parentResourceId = knownResourceId;
340         // Clean up item resources.
341         for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
342             itemResourceId = entry.getKey();
343             parentResourceId = entry.getValue();
344             // Note: Any non-success responses from the delete operation
345             // below are ignored and not reported.
346             client.deleteItem(parentResourceId, itemResourceId).close();
347         }
348         // Clean up parent resources.
349         for (String resourceId : allResourceIdsCreated) {
350             // Note: Any non-success responses from the delete operation
351             // below are ignored and not reported.
352         client.delete(resourceId).close();
353         }
354     }
355
356     // ---------------------------------------------------------------
357     // Utility methods used by tests above
358     // ---------------------------------------------------------------
359     /* (non-Javadoc)
360      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
361      */
362
363     /**
364      * Returns the root URL for the item service.
365      *
366      * This URL consists of a base URL for all services, followed by
367      * a path component for the owning parent, followed by the
368      * path component for the items.
369      *
370      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
371      * parent authority resource of the relevant item resource.
372      *
373      * @return The root URL for the item service.
374      */
375     protected String getItemServiceRootURL(String parentResourceIdentifier) {
376         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
377     }
378
379     /**
380      * Returns the URL of a specific item resource managed by a service, and
381      * designated by an identifier (such as a universally unique ID, or UUID).
382      *
383      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
384      * parent authority resource of the relevant item resource.
385      *
386      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
387      * item resource.
388      *
389      * @return The URL of a specific item resource managed by a service.
390      */
391     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
392         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
393     }
394
395         @Override
396     public void authorityTests(String testName) {
397         // TODO Auto-generated method stub
398         
399     }
400
401     //
402     // Material specific overrides
403     //
404     
405     @Override
406     protected PoxPayloadOut createInstance(String commonPartName,
407             String identifier) {
408         // Submit the request to the service and store the response.
409         String shortId = identifier;
410         String displayName = "displayName-" + shortId;
411         // String baseRefName = MaterialAuthorityClientUtils.createMaterialAuthRefName(shortId, null);      
412         PoxPayloadOut result = 
413             MaterialAuthorityClientUtils.createMaterialAuthorityInstance(
414             displayName, shortId, commonPartName);
415         return result;
416     }
417     
418     @Override
419     protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
420         String displayName = "displayName-NON_EXISTENT_ID";
421         PoxPayloadOut result = MaterialAuthorityClientUtils.createMaterialAuthorityInstance(
422                     displayName, "nonEx", commonPartName);
423         return result;
424     }
425
426     @Override
427     protected MaterialauthoritiesCommon updateInstance(MaterialauthoritiesCommon materialauthoritiesCommon) {
428         MaterialauthoritiesCommon result = new MaterialauthoritiesCommon();
429         
430         result.setDisplayName("updated-" + materialauthoritiesCommon.getDisplayName());
431         result.setVocabType("updated-" + materialauthoritiesCommon.getVocabType());
432         
433         return result;
434     }
435
436     @Override
437     protected void compareUpdatedInstances(MaterialauthoritiesCommon original,
438             MaterialauthoritiesCommon updated) throws Exception {
439         Assert.assertEquals(updated.getDisplayName(),
440                 original.getDisplayName(),
441                 "Display name in updated object did not match submitted data.");
442     }
443
444     protected void compareReadInstances(MaterialauthoritiesCommon original,
445             MaterialauthoritiesCommon fromRead) throws Exception {
446         Assert.assertNotNull(fromRead.getDisplayName());
447         Assert.assertNotNull(fromRead.getShortIdentifier());
448         Assert.assertNotNull(fromRead.getRefName());
449     }
450     
451     //
452     // Authority item specific overrides
453     //
454     
455     @Override
456     protected String createItemInAuthority(AuthorityClient client, String authorityId) {
457         return createItemInAuthority(client, authorityId, null /*refname*/);
458     }
459
460     @Override
461     protected MaterialsCommon updateItemInstance(MaterialsCommon materialsCommon) {
462                             
463             MaterialTermGroupList termList = materialsCommon.getMaterialTermGroupList();
464             Assert.assertNotNull(termList);
465             List<MaterialTermGroup> terms = termList.getMaterialTermGroup();
466             Assert.assertNotNull(terms);
467             Assert.assertTrue(terms.size() > 0);
468             terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
469             terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
470             materialsCommon.setMaterialTermGroupList(termList);
471
472             return materialsCommon;
473     }
474
475     @Override
476     protected void compareUpdatedItemInstances(MaterialsCommon original,
477             MaterialsCommon updated) throws Exception {
478             
479             MaterialTermGroupList originalTermList = original.getMaterialTermGroupList();
480             Assert.assertNotNull(originalTermList);
481             List<MaterialTermGroup> originalTerms = originalTermList.getMaterialTermGroup();
482             Assert.assertNotNull(originalTerms);
483             Assert.assertTrue(originalTerms.size() > 0);
484             
485             MaterialTermGroupList updatedTermList = updated.getMaterialTermGroupList();
486             Assert.assertNotNull(updatedTermList);
487             List<MaterialTermGroup> updatedTerms = updatedTermList.getMaterialTermGroup();
488             Assert.assertNotNull(updatedTerms);
489             Assert.assertTrue(updatedTerms.size() > 0);
490             
491             Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
492                 originalTerms.get(0).getTermDisplayName(),
493                 "Value in updated record did not match submitted data.");
494     }
495
496     @Override
497     protected void verifyReadItemInstance(MaterialsCommon item)
498             throws Exception {
499         // TODO Auto-generated method stub
500         
501     }
502
503     @Override
504     protected PoxPayloadOut createNonExistenceItemInstance(
505             String commonPartName, String identifier) {
506         Map<String, String> nonexMap = new HashMap<String,String>();
507         nonexMap.put(MaterialJAXBSchema.MATERIAL_TERM_DISPLAY_NAME, TEST_MATERIAL_TERM_DISPLAY_NAME);
508         nonexMap.put(MaterialJAXBSchema.SHORT_IDENTIFIER, "nonEx");
509         nonexMap.put(MaterialJAXBSchema.MATERIAL_TERM_STATUS, TEST_MATERIAL_TERM_STATUS);
510         final String EMPTY_REFNAME = "";
511         PoxPayloadOut result = 
512                 MaterialAuthorityClientUtils.createMaterialInstance(EMPTY_REFNAME, nonexMap,
513                 MaterialAuthorityClientUtils.getTermGroupInstance(TEST_MATERIAL_TERM_DISPLAY_NAME), commonPartName);
514         return result;
515     }
516 }