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