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