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