]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
096fc7265c34b462694531f32ce5b93ae7875219
[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.WorkJAXBSchema;
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.WorkAuthorityClient;
40 import org.collectionspace.services.client.WorkAuthorityClientUtils;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
42 import org.collectionspace.services.work.WorkTermGroup;
43 import org.collectionspace.services.work.WorkTermGroupList;
44 import org.collectionspace.services.work.WorkauthoritiesCommon;
45 import org.collectionspace.services.work.WorksCommon;
46 import org.jboss.resteasy.client.ClientResponse;
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  * WorkAuthorityServiceTest, carries out tests against a
55  * deployed and running WorkAuthority Service.
56  *
57  */
58 public class WorkAuthorityServiceTest extends AbstractAuthorityServiceTest<WorkauthoritiesCommon, WorksCommon> {
59
60     /** The logger. */
61     private final String CLASS_NAME = WorkAuthorityServiceTest.class.getName();
62     private final Logger logger = LoggerFactory.getLogger(WorkAuthorityServiceTest.class);
63
64     @Override
65     public String getServicePathComponent() {
66         return WorkAuthorityClient.SERVICE_PATH_COMPONENT;
67     }
68
69     @Override
70     protected String getServiceName() {
71         return WorkAuthorityClient.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 = "workauthorities";
82 //    
83 //    /** The ITEM service path component. */
84 //    final String ITEM_SERVICE_PATH_COMPONENT = "items";
85 //    
86
87     final String TEST_WORK_TERM_DISPLAY_NAME = "Muppets Take Manhattan (1984)";
88     final String TEST_WORK_TERM_NAME = "Muppets Take Manhattan";
89     final String TEST_WORK_TERM_TYPE = "";
90     final String TEST_WORK_TERM_STATUS = "accepted";
91     final String TEST_WORK_TERM_QUALIFIER = "";
92     final String TEST_WORK_TERM_LANGUAGE = "english";
93     final Boolean TEST_WORK_TERM_PREFFORLANG = true;
94     final String TEST_WORK_TERM_SOURCE = "featurefilms";
95     final String TEST_WORK_TERM_SOURCE_DETAIL = "internal";
96     final String TEST_WORK_TERM_SOURCE_ID = "12345";    
97     final String TEST_WORK_TERM_SOURCE_NOTE = "source note goes here";
98     final String TEST_WORK_HISTORY_NOTE = "history note goes here";
99     final String TEST_WORK_TYPE = "work type goes here";
100     final String TEST_WORK_CREATOR_GROUP_CREATOR = "Frank Oz";
101     final String TEST_WORK_CREATOR_GROUP_CREATOR_TYPE = "director";
102     final String TEST_WORK_PUBLISHER_GROUP_PUBLISHER = "TriStar Pictures";
103     final String TEST_WORK_PUBLISHER_GROUP_PUBLISHER_TYPE = "Distributor";
104     final String TEST_WORK_SHORT_IDENTIFIER = "muppetstakemanhattan";
105     final String TEST_WORK_REFNAME = "refname";
106     
107     /** The known resource id. */
108     private String knownResourceShortIdentifer = null;
109     private String knownResourceRefName = null;
110     private String knownWorkTypeRefName = null;
111     
112     /* (non-Javadoc)
113      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
114      */
115     @Override
116     protected CollectionSpaceClient getClientInstance() {
117         return new WorkAuthorityClient();
118     }
119
120     /**
121      * Creates the item in authority.
122      *
123      * @param vcsid the vcsid
124      * @param authRefName the auth ref name
125      * @return the string
126      */
127     private String createItemInAuthority(String vcsid, String authRefName) {
128         final String testName = "createItemInAuthority("+vcsid+","+authRefName+")"; 
129     
130         // Submit the request to the service and store the response.
131         WorkAuthorityClient client = new WorkAuthorityClient();
132         Map<String, String> workMap = new HashMap<String,String>();
133         // TODO Make work type and status be controlled vocabs.
134         workMap.put(WorkJAXBSchema.SHORT_IDENTIFIER, TEST_WORK_SHORT_IDENTIFIER);
135         workMap.put(WorkJAXBSchema.WORK_TYPE, TEST_WORK_TYPE);
136         workMap.put(WorkJAXBSchema.WORK_HISTORY_NOTE, TEST_WORK_HISTORY_NOTE);
137         
138         List<WorkTermGroup> terms = new ArrayList<WorkTermGroup>();
139         WorkTermGroup term = new WorkTermGroup();
140         term.setTermDisplayName(TEST_WORK_TERM_DISPLAY_NAME);
141         term.setTermName(TEST_WORK_TERM_NAME);
142         term.setTermSource(TEST_WORK_TERM_SOURCE);
143         term.setTermSourceDetail(TEST_WORK_TERM_SOURCE_DETAIL);
144         term.setTermStatus(TEST_WORK_TERM_STATUS);
145         terms.add(term);
146         
147         String newID = WorkAuthorityClientUtils.createItemInAuthority(vcsid,
148                 authRefName, workMap, terms, client );    
149
150         // Store the ID returned from the first item resource created
151         // for additional tests below.
152         if (knownItemResourceId == null){
153             setKnownItemResource(newID, TEST_WORK_SHORT_IDENTIFIER);
154             if (logger.isDebugEnabled()) {
155                 logger.debug(testName + ": knownItemResourceId=" + newID);
156             }
157         }
158         
159         // Store the IDs from any item resources created
160         // by tests, along with the IDs of their parents, so these items
161         // can be deleted after all tests have been run.
162         allResourceItemIdsCreated.put(newID, vcsid);
163
164         return newID;
165     }
166
167     /**
168      * Verify illegal item display name.
169      *
170      * @param testName the test name
171      * @throws Exception the exception
172      */
173     @Test(dataProvider="testName")
174         public void verifyIllegalItemDisplayName(String testName) throws Exception {
175                 // Perform setup for read.
176                 setupRead();
177
178                 // Submit the request to the service and store the response.
179                 WorkAuthorityClient client = new WorkAuthorityClient();
180                 Response res = client.readItem(knownResourceId, knownItemResourceId);
181                 WorksCommon work = null;
182                 try {
183                         assertStatusCode(res, testName);
184                         PoxPayloadIn input = new PoxPayloadIn((String)res.getEntity());
185                         work = (WorksCommon) extractPart(input,
186                                         client.getItemCommonPartName(), WorksCommon.class);
187                         Assert.assertNotNull(work);
188                 } finally {
189                         if (res != null) {
190                                 res.close();
191                         }
192                 }
193
194                 //
195                 // Make an invalid UPDATE request, without a display name
196                 //
197                 WorkTermGroupList termList = work.getWorkTermGroupList();
198                 Assert.assertNotNull(termList);
199                 List<WorkTermGroup> terms = termList.getWorkTermGroup();
200                 Assert.assertNotNull(terms);
201                 Assert.assertTrue(terms.size() > 0);
202                 terms.get(0).setTermDisplayName(null);
203                 terms.get(0).setTermName(null);
204
205                 setupUpdateWithInvalidBody(); // we expect a failure
206
207                 // Submit the updated resource to the service and store the response.
208                 PoxPayloadOut output = new PoxPayloadOut(
209                                 WorkAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
210                 PayloadOutputPart commonPart = output.addPart(
211                                 client.getItemCommonPartName(), work);
212                 setupUpdateWithInvalidBody(); // we expected a failure here.
213                 res = client.updateItem(knownResourceId, knownItemResourceId, output);
214                 try {
215                         assertStatusCode(res, testName);
216                 } finally {
217                         if (res != null) {
218                                 res.close();
219                         }
220                 }
221         }
222
223     /**
224      * Read item list.
225      */
226     @Test(dataProvider = "testName", groups = {"readList"},
227                dependsOnMethods = {"readList"})
228     public void readItemList(String testName) {
229         readItemList(knownAuthorityWithItems, null);
230     }
231
232     /**
233      * Read item list by authority name.
234      */
235     @Test(dataProvider = "testName", groups = {"readList"},
236             dependsOnMethods = {"readItemList"})
237     public void readItemListByAuthorityName(String testName) {
238         readItemList(null, READITEMS_SHORT_IDENTIFIER);
239     }
240     
241     /**
242      * Read item list.
243      * 
244      * @param vcsid
245      *            the vcsid
246      * @param name
247      *            the name
248      */
249     private void readItemList(String vcsid, String shortId) {
250         String testName = "readItemList";
251
252         // Perform setup.
253         setupReadList();
254
255         // Submit the request to the service and store the response.
256         WorkAuthorityClient client = new WorkAuthorityClient();
257         ClientResponse<AbstractCommonList> res = null;
258         if (vcsid != null) {
259             res = client.readItemList(vcsid, null, null);
260         } else if (shortId != null) {
261             res = client.readItemListForNamedAuthority(shortId, null, null);
262         } else {
263             Assert.fail("readItemList passed null csid and name!");
264         }
265         
266         AbstractCommonList list = null;
267         try {
268             assertStatusCode(res, testName);
269             list = res.getEntity();
270         } finally {
271             if (res != null) {
272                 res.close();
273             }
274         }
275         
276         List<AbstractCommonList.ListItem> items = list.getListItem();
277         int nItemsReturned = items.size();
278         // There will be 'nItemsToCreateInList'
279         // items created by the createItemList test,
280         // all associated with the same parent resource.
281         int nExpectedItems = nItemsToCreateInList;
282         if (logger.isDebugEnabled()) {
283             logger.debug(testName + ": Expected " + nExpectedItems
284                     + " items; got: " + nItemsReturned);
285         }
286         Assert.assertEquals(nItemsReturned, nExpectedItems);
287
288         for (AbstractCommonList.ListItem item : items) {
289             String value = AbstractCommonListUtils.ListItemGetElementValue(
290                     item, WorkJAXBSchema.REF_NAME);
291             Assert.assertTrue((null != value), "Item refName is null!");
292             value = AbstractCommonListUtils.ListItemGetElementValue(item,
293                     WorkJAXBSchema.WORK_TERM_DISPLAY_NAME);
294             Assert.assertTrue((null != value), "Item termDisplayName is null!");
295         }
296         if (logger.isTraceEnabled()) {
297             AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger,
298                     testName);
299         }
300     }
301
302     @Override
303     public void delete(String testName) throws Exception {
304         // Do nothing.  See localDelete().  This ensure proper test order.
305     }
306     
307     @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})    
308     public void localDelete(String testName) throws Exception {
309         super.delete(testName);
310     }
311
312     @Override
313     public void deleteItem(String testName) throws Exception {
314         // Do nothing.  We need to wait until after the test "localDelete" gets run.  When it does,
315         // its dependencies will get run first and then we can call the base class' delete method.
316     }
317     
318     @Test(dataProvider = "testName", groups = {"delete"},
319         dependsOnMethods = {"verifyIllegalItemDisplayName"})
320     public void localDeleteItem(String testName) throws Exception {
321         super.deleteItem(testName);
322     }
323     
324     // ---------------------------------------------------------------
325     // Cleanup of resources created during testing
326     // ---------------------------------------------------------------
327     
328     /**
329      * Deletes all resources created by tests, after all tests have been run.
330      *
331      * This cleanup method will always be run, even if one or more tests fail.
332      * For this reason, it attempts to remove all resources created
333      * at any point during testing, even if some of those resources
334      * may be expected to be deleted by certain tests.
335      */
336
337     @AfterClass(alwaysRun=true)
338     public void cleanUp() {
339         String noTest = System.getProperty("noTestCleanup");
340         if(Boolean.TRUE.toString().equalsIgnoreCase(noTest)) {
341             if (logger.isDebugEnabled()) {
342                 logger.debug("Skipping Cleanup phase ...");
343             }
344             return;
345         }
346         if (logger.isDebugEnabled()) {
347             logger.debug("Cleaning up temporary resources created for testing ...");
348         }
349         String parentResourceId;
350         String itemResourceId;
351         // Clean up contact resources.
352         WorkAuthorityClient client = new WorkAuthorityClient();
353         parentResourceId = knownResourceId;
354         // Clean up item resources.
355         for (Map.Entry<String, String> entry : allResourceItemIdsCreated.entrySet()) {
356             itemResourceId = entry.getKey();
357             parentResourceId = entry.getValue();
358             // Note: Any non-success responses from the delete operation
359             // below are ignored and not reported.
360             client.deleteItem(parentResourceId, itemResourceId).close();
361         }
362         // Clean up parent resources.
363         for (String resourceId : allResourceIdsCreated) {
364             // Note: Any non-success responses from the delete operation
365             // below are ignored and not reported.
366         client.delete(resourceId).close();
367         }
368     }
369
370     // ---------------------------------------------------------------
371     // Utility methods used by tests above
372     // ---------------------------------------------------------------
373     /* (non-Javadoc)
374      * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
375      */
376
377     /**
378      * Returns the root URL for the item service.
379      *
380      * This URL consists of a base URL for all services, followed by
381      * a path component for the owning parent, followed by the
382      * path component for the items.
383      *
384      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
385      * parent authority resource of the relevant item resource.
386      *
387      * @return The root URL for the item service.
388      */
389     protected String getItemServiceRootURL(String parentResourceIdentifier) {
390         return getResourceURL(parentResourceIdentifier) + "/" + getItemServicePathComponent();
391     }
392
393     /**
394      * Returns the URL of a specific item resource managed by a service, and
395      * designated by an identifier (such as a universally unique ID, or UUID).
396      *
397      * @param  parentResourceIdentifier  An identifier (such as a UUID) for the
398      * parent authority resource of the relevant item resource.
399      *
400      * @param  itemResourceIdentifier  An identifier (such as a UUID) for an
401      * item resource.
402      *
403      * @return The URL of a specific item resource managed by a service.
404      */
405     protected String getItemResourceURL(String parentResourceIdentifier, String itemResourceIdentifier) {
406         return getItemServiceRootURL(parentResourceIdentifier) + "/" + itemResourceIdentifier;
407     }
408
409         @Override
410     public void authorityTests(String testName) {
411         // TODO Auto-generated method stub
412         
413     }
414
415     //
416     // Work specific overrides
417     //
418     
419     @Override
420     protected PoxPayloadOut createInstance(String commonPartName,
421             String identifier) {
422         // Submit the request to the service and store the response.
423         String shortId = identifier;
424         String displayName = "displayName-" + shortId;
425         // String baseRefName = WorkAuthorityClientUtils.createWorkAuthRefName(shortId, null);      
426         PoxPayloadOut result = 
427             WorkAuthorityClientUtils.createWorkAuthorityInstance(
428             displayName, shortId, commonPartName);
429         return result;
430     }
431     
432     @Override
433     protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
434         String displayName = "displayName-NON_EXISTENT_ID";
435         PoxPayloadOut result = WorkAuthorityClientUtils.createWorkAuthorityInstance(
436                     displayName, "nonEx", commonPartName);
437         return result;
438     }
439
440     @Override
441     protected WorkauthoritiesCommon updateInstance(WorkauthoritiesCommon workauthoritiesCommon) {
442         WorkauthoritiesCommon result = new WorkauthoritiesCommon();
443         
444         result.setDisplayName("updated-" + workauthoritiesCommon.getDisplayName());
445         result.setVocabType("updated-" + workauthoritiesCommon.getVocabType());
446         
447         return result;
448     }
449
450     @Override
451     protected void compareUpdatedInstances(WorkauthoritiesCommon original,
452             WorkauthoritiesCommon updated) throws Exception {
453         Assert.assertEquals(updated.getDisplayName(),
454                 original.getDisplayName(),
455                 "Display name in updated object did not match submitted data.");
456     }
457
458     protected void compareReadInstances(WorkauthoritiesCommon original,
459             WorkauthoritiesCommon fromRead) throws Exception {
460         Assert.assertNotNull(fromRead.getDisplayName());
461         Assert.assertNotNull(fromRead.getShortIdentifier());
462         Assert.assertNotNull(fromRead.getRefName());
463     }
464     
465     //
466     // Authority item specific overrides
467     //
468     
469     @Override
470     protected String createItemInAuthority(String authorityId) {
471         return createItemInAuthority(authorityId, null /*refname*/);
472     }
473
474     @Override
475     protected WorksCommon updateItemInstance(WorksCommon worksCommon) {
476                             
477             WorkTermGroupList termList = worksCommon.getWorkTermGroupList();
478             Assert.assertNotNull(termList);
479             List<WorkTermGroup> terms = termList.getWorkTermGroup();
480             Assert.assertNotNull(terms);
481             Assert.assertTrue(terms.size() > 0);
482             terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
483             terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
484             worksCommon.setWorkTermGroupList(termList);
485
486             return worksCommon;
487     }
488
489     @Override
490     protected void compareUpdatedItemInstances(WorksCommon original,
491             WorksCommon updated) throws Exception {
492             
493             WorkTermGroupList originalTermList = original.getWorkTermGroupList();
494             Assert.assertNotNull(originalTermList);
495             List<WorkTermGroup> originalTerms = originalTermList.getWorkTermGroup();
496             Assert.assertNotNull(originalTerms);
497             Assert.assertTrue(originalTerms.size() > 0);
498             
499             WorkTermGroupList updatedTermList = updated.getWorkTermGroupList();
500             Assert.assertNotNull(updatedTermList);
501             List<WorkTermGroup> updatedTerms = updatedTermList.getWorkTermGroup();
502             Assert.assertNotNull(updatedTerms);
503             Assert.assertTrue(updatedTerms.size() > 0);
504             
505             Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(),
506                 originalTerms.get(0).getTermDisplayName(),
507                 "Value in updated record did not match submitted data.");
508     }
509
510     @Override
511     protected void verifyReadItemInstance(WorksCommon item)
512             throws Exception {
513         // TODO Auto-generated method stub
514         
515     }
516
517     @Override
518     protected PoxPayloadOut createNonExistenceItemInstance(
519             String commonPartName, String identifier) {
520         Map<String, String> nonexMap = new HashMap<String,String>();
521         nonexMap.put(WorkJAXBSchema.WORK_TERM_DISPLAY_NAME, TEST_WORK_TERM_DISPLAY_NAME);
522         nonexMap.put(WorkJAXBSchema.SHORT_IDENTIFIER, "nonEx");
523         nonexMap.put(WorkJAXBSchema.WORK_TERM_STATUS, TEST_WORK_TERM_STATUS);
524         final String EMPTY_REFNAME = "";
525         PoxPayloadOut result = 
526                 WorkAuthorityClientUtils.createWorkInstance(EMPTY_REFNAME, nonexMap,
527                 WorkAuthorityClientUtils.getTermGroupInstance(TEST_WORK_TERM_DISPLAY_NAME), commonPartName);
528         return result;
529     }
530 }