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