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