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