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