]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
53afd1721535e72b9c64a10d229e7c78be963017
[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.LocationJAXBSchema;
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.LocationAuthorityClient;
40 import org.collectionspace.services.client.LocationAuthorityClientUtils;
41 import org.collectionspace.services.common.api.GregorianCalendarDateTimeUtils;
42 import org.collectionspace.services.jaxb.AbstractCommonList;
43 import org.collectionspace.services.location.LocTermGroup;
44 import org.collectionspace.services.location.LocTermGroupList;
45 import org.collectionspace.services.location.LocationauthoritiesCommon;
46 import org.collectionspace.services.location.LocationsCommon;
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  * LocationAuthorityServiceTest, carries out tests against a
55  * deployed and running LocationAuthority Service.
56  *
57  * $LastChangedRevision: 753 $
58  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
59  */
60 public class LocationAuthorityServiceTest extends AbstractAuthorityServiceTest<LocationauthoritiesCommon, LocationsCommon> {
61
62     /** The logger. */
63     private final Logger logger = LoggerFactory.getLogger(LocationAuthorityServiceTest.class);
64     private final static String CURRENT_DATE_UTC = GregorianCalendarDateTimeUtils.currentDateUTC();
65
66     /**
67      * Default constructor.  Used to set the short ID for all tests authority items
68      */
69     public LocationAuthorityServiceTest() {
70         super();
71         TEST_SHORTID = "shelf1";
72     }
73     
74         @Override
75         public String getServicePathComponent() {
76                 return LocationAuthorityClient.SERVICE_PATH_COMPONENT;
77         }
78
79         @Override
80         protected String getServiceName() {
81                 return LocationAuthorityClient.SERVICE_NAME;
82         }
83     
84     public String getItemServicePathComponent() {
85         return AuthorityClient.ITEMS;
86     }   
87     
88     // Instance variables specific to this test.
89     
90     final String TEST_NAME = "Shelf 1";
91     final String TEST_CONDITION_NOTE = "Basically clean";
92     final String TEST_CONDITION_NOTE_DATE = CURRENT_DATE_UTC;
93     final String TEST_SECURITY_NOTE = "Kind of safe";
94     final String TEST_ACCESS_NOTE = "Only right-thinkers may see";
95     final String TEST_ADDRESS = "123 Main Street, Anytown USA";
96     // TODO Make loc type be a controlled vocab term.
97     final String TEST_LOCATION_TYPE = "Shelf";
98     // TODO Make status type be a controlled vocab term.
99     final String TEST_STATUS = "Approved";
100     
101     /** The known resource id. */
102     private String knownLocationTypeRefName = null;
103
104     /* (non-Javadoc)
105      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
106      */
107     @Override
108     protected CollectionSpaceClient getClientInstance() {
109         return new LocationAuthorityClient();
110     }
111     
112     @Override
113     protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
114         return new LocationAuthorityClient(clientPropertiesFilename);
115     }
116     
117         @Override
118         protected String createItemInAuthority(AuthorityClient client, String vcsid, String shortId) {
119                 return createItemInAuthority(client, vcsid, shortId, null/*refname*/);
120         }       
121     
122     /**
123      * Creates the item in authority.
124      *
125      * @param vcsid the vcsid
126      * @param authRefName the auth ref name
127      * @return the string
128      */
129     private String createItemInAuthority(AuthorityClient client, String vcsid, String shortId, String authRefName) {
130         final String testName = "createItemInAuthority("+vcsid+","+authRefName+")";
131
132         // Submit the request to the service and store the response.
133         Map<String, String> shelf1Map = new HashMap<String,String>();
134         // TODO Make loc type and status be controlled vocabs.
135         shelf1Map.put(LocationJAXBSchema.SHORT_IDENTIFIER, shortId);
136         shelf1Map.put(LocationJAXBSchema.CONDITION_NOTE, TEST_CONDITION_NOTE);
137         shelf1Map.put(LocationJAXBSchema.CONDITION_NOTE_DATE, TEST_CONDITION_NOTE_DATE);
138         shelf1Map.put(LocationJAXBSchema.SECURITY_NOTE, TEST_SECURITY_NOTE);
139         shelf1Map.put(LocationJAXBSchema.ACCESS_NOTE, TEST_ACCESS_NOTE);
140         shelf1Map.put(LocationJAXBSchema.ADDRESS, TEST_ADDRESS);
141         shelf1Map.put(LocationJAXBSchema.LOCATION_TYPE, TEST_LOCATION_TYPE);
142         
143         List<LocTermGroup> shelf1Terms = new ArrayList<LocTermGroup>();
144         LocTermGroup term = new LocTermGroup();
145         term.setTermDisplayName(TEST_NAME);
146         term.setTermName(TEST_NAME);
147         term.setTermStatus(TEST_STATUS);
148         shelf1Terms.add(term);
149                 shelf1Map.put(LocationJAXBSchema.TERM_STATUS, TEST_STATUS);
150
151         String newID = LocationAuthorityClientUtils.createItemInAuthority(vcsid,
152                         authRefName, shelf1Map, shelf1Terms, (LocationAuthorityClient) 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, shortId);
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         LocationAuthorityClient client = new LocationAuthorityClient();
184         Response res = client.readItem(knownResourceId, knownItemResourceId);
185         LocationsCommon location = null;
186         try {
187             assertStatusCode(res, testName);        
188                 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
189                 location = (LocationsCommon) extractPart(input,
190                         client.getItemCommonPartName(), LocationsCommon.class);
191                 Assert.assertNotNull(location);
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         LocTermGroupList termList = location.getLocTermGroupList();
202         Assert.assertNotNull(termList);
203         List<LocTermGroup> terms = termList.getLocTermGroup();
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(LocationAuthorityClient.SERVICE_ITEM_PAYLOAD_NAME);
213         PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), location);
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         LocationAuthorityClient client = new LocationAuthorityClient();
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         //
339         // Location specific overrides
340         //
341         
342         @Override
343         protected PoxPayloadOut createInstance(String commonPartName,
344                         String identifier) {
345         // Submit the request to the service and store the response.
346         String shortId = identifier;
347         String displayName = "displayName-" + shortId;
348         // String baseRefName = LocationAuthorityClientUtils.createLocationAuthRefName(shortId, null);          
349         PoxPayloadOut result = 
350             LocationAuthorityClientUtils.createLocationAuthorityInstance(
351             displayName, shortId, commonPartName);
352                 return result;
353         }
354         
355         @Override
356     protected PoxPayloadOut createNonExistenceInstance(String commonPartName, String identifier) {
357         String displayName = "displayName-NON_EXISTENT_ID";
358         PoxPayloadOut result = LocationAuthorityClientUtils.createLocationAuthorityInstance(
359                                 displayName, "nonEx", commonPartName);
360         return result;
361     }
362
363         @Override
364         protected LocationauthoritiesCommon updateInstance(LocationauthoritiesCommon locationauthoritiesCommon) {
365                 LocationauthoritiesCommon result = new LocationauthoritiesCommon();
366                 
367                 result.setDisplayName("updated-" + locationauthoritiesCommon.getDisplayName());
368                 result.setVocabType("updated-" + locationauthoritiesCommon.getVocabType());
369                 
370                 return result;
371         }
372
373         @Override
374         protected void compareUpdatedInstances(LocationauthoritiesCommon original,
375                         LocationauthoritiesCommon updated) throws Exception {
376         Assert.assertEquals(updated.getDisplayName(),
377                         original.getDisplayName(),
378                 "Display name in updated object did not match submitted data.");
379         }
380
381         protected void compareReadInstances(LocationauthoritiesCommon original,
382                         LocationauthoritiesCommon fromRead) throws Exception {
383         Assert.assertNotNull(fromRead.getDisplayName());
384         Assert.assertNotNull(fromRead.getShortIdentifier());
385         Assert.assertNotNull(fromRead.getRefName());
386         }
387         
388         //
389         // Authority item specific overrides
390         //
391
392         @Override
393         protected LocationsCommon updateItemInstance(LocationsCommon locationsCommon) {
394                 
395             LocTermGroupList termList = locationsCommon.getLocTermGroupList();
396             Assert.assertNotNull(termList);
397             List<LocTermGroup> terms = termList.getLocTermGroup();
398             Assert.assertNotNull(terms);
399             Assert.assertTrue(terms.size() > 0);
400             terms.get(0).setTermDisplayName("updated-" + terms.get(0).getTermDisplayName());
401             terms.get(0).setTermName("updated-" + terms.get(0).getTermName());
402             locationsCommon.setLocTermGroupList(termList);
403
404             return locationsCommon;
405
406         }
407
408         @Override
409         protected void compareUpdatedItemInstances(LocationsCommon original,
410                         LocationsCommon updated,
411                         boolean compareRevNumbers) throws Exception {
412                 LocTermGroupList originalTermList = original.getLocTermGroupList();
413                 Assert.assertNotNull(originalTermList);
414                 List<LocTermGroup> originalTerms = originalTermList.getLocTermGroup();
415                 Assert.assertNotNull(originalTerms);
416                 Assert.assertTrue(originalTerms.size() > 0);
417
418                 LocTermGroupList updatedTermList = updated.getLocTermGroupList();
419                 Assert.assertNotNull(updatedTermList);
420                 List<LocTermGroup> updatedTerms = updatedTermList.getLocTermGroup();
421                 Assert.assertNotNull(updatedTerms);
422                 Assert.assertTrue(updatedTerms.size() > 0);
423
424                 Assert.assertEquals(updatedTerms.get(0).getTermDisplayName(), originalTerms.get(0).getTermDisplayName(), "Value in updated record did not match submitted data.");
425                 
426                 if (compareRevNumbers == true) {
427                 Assert.assertEquals(original.getRev(), updated.getRev(), "Revision numbers should match.");
428         }
429         }
430
431         @Override
432         protected void verifyReadItemInstance(LocationsCommon item)
433                         throws Exception {
434                 // TODO Auto-generated method stub
435                 
436         }
437
438         @Override
439         protected PoxPayloadOut createNonExistenceItemInstance(
440                         String commonPartName, String identifier) {
441             Map<String, String> nonexMap = new HashMap<String, String>();
442             nonexMap.put(LocationJAXBSchema.SHORT_IDENTIFIER, "nonExistent");
443             final String EMPTY_REFNAME = "";
444             PoxPayloadOut result = 
445                 LocationAuthorityClientUtils.createLocationInstance(EMPTY_REFNAME, nonexMap,
446                 LocationAuthorityClientUtils.getTermGroupInstance(""), commonPartName);
447             return result;
448
449         }
450 }