]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
570aa6563ffdf73cb4c8436654f521eaf918ad2f
[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.HashMap;
26
27 import org.collectionspace.services.common.vocabulary.AuthorityItemJAXBSchema;
28 import org.collectionspace.services.client.AuthorityClient;
29 import org.collectionspace.services.client.CollectionSpaceClient;
30 import org.collectionspace.services.client.PayloadOutputPart;
31 import org.collectionspace.services.client.PoxPayloadIn;
32 import org.collectionspace.services.client.PoxPayloadOut;
33 import org.collectionspace.services.client.VocabularyClient;
34 import org.collectionspace.services.client.VocabularyClientUtils;
35 import org.collectionspace.services.vocabulary.VocabulariesCommon;
36 import org.collectionspace.services.vocabulary.VocabularyitemsCommon;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39 import org.testng.Assert;
40 import org.testng.annotations.Test;
41
42 import javax.ws.rs.core.Response;
43
44 /**
45  * VocabularyServiceTest, carries out tests against a
46  * deployed and running Vocabulary Service.
47  *
48  * $LastChangedRevision: 753 $
49  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
50  */
51 public class VocabularyServiceTest extends AbstractAuthorityServiceTest<VocabulariesCommon, VocabularyitemsCommon> {
52
53     private final String CLASS_NAME = VocabularyServiceTest.class.getName();
54     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
55     // Instance variables specific to this test.
56     final String SERVICE_PATH_COMPONENT = VocabularyClient.SERVICE_PATH_COMPONENT;//"vocabularies";
57     final String SERVICE_PAYLOAD_NAME = VocabularyClient.SERVICE_PAYLOAD_NAME;
58     final String SERVICE_ITEM_PAYLOAD_NAME = VocabularyClient.SERVICE_ITEM_PAYLOAD_NAME;
59
60
61     /* (non-Javadoc)
62      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
63      */
64     @Override
65     protected CollectionSpaceClient getClientInstance() {
66         return new VocabularyClient();
67     }
68
69         @Override
70         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
71         return new VocabularyClient(clientPropertiesFilename);
72         }
73     
74     @Override
75     protected String createItemInAuthority(AuthorityClient client, String authorityId) {
76         String result = null;
77         
78         HashMap<String, String> itemInfo = new HashMap<String, String>();
79         String shortId = createIdentifier();
80         itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
81         itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
82         result = VocabularyClientUtils.createItemInVocabulary(authorityId,
83                 null /*knownResourceRefName*/, itemInfo, (VocabularyClient) client);
84         allResourceItemIdsCreated.put(result, authorityId);
85         
86         return result;
87     }
88     
89     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
90                 dependsOnMethods = {"CRUDTests"})
91     public void createWithBadShortId(String testName) throws Exception {
92         testSetup(STATUS_BAD_REQUEST, ServiceRequestType.CREATE);
93
94         // Submit the request to the service and store the response.
95         VocabularyClient client = new VocabularyClient();
96         PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
97                 "Vocab with Bad Short Id", "Bad Short Id!", client.getCommonPartName());
98         Response res = client.create(multipart);
99         try {
100                 assertStatusCode(res, testName);
101         } finally {
102                 if (res != null) {
103                 res.close();
104             }
105         }
106     }
107     
108     @Override
109     public void createItemList(String testName) throws Exception {
110         knownAuthorityWithItems = createResource(testName, READITEMS_SHORT_IDENTIFIER);
111         for (int j = 0; j < nItemsToCreateInList; j++) {
112                 createItemInAuthority((AuthorityClient) getClientInstance(), knownAuthorityWithItems);
113         }
114     }    
115     
116     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
117                 dependsOnMethods = {"CRUDTests"})
118     public void createWithNonuniqueShortId(String testName) throws Exception {
119         testSetup(STATUS_CREATED, ServiceRequestType.CREATE);
120
121         // Create a new vocabulary
122         VocabularyClient client = new VocabularyClient();
123         PoxPayloadOut multipart = VocabularyClientUtils.createEnumerationInstance(
124                 "Vocab with non-unique Short Id", "nonunique", client.getCommonPartName());
125         Response res = client.create(multipart);
126         try {
127                 assertStatusCode(res, testName);
128                 String newId = extractId(res);
129                 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves
130         } finally {
131                 if (res != null) {
132                 res.close();
133             }
134         }
135         
136         //
137         // Now try to create a duplicate, we should fail because we're using a non-unique short id
138         // 
139         res = client.create(multipart);
140         try {
141                 Assert.assertTrue(res.getStatus() != STATUS_CREATED, "Expect create to fail because of non unique short identifier.");
142         } catch (AssertionError ae) {
143                 // We expected a failure, but we didn't get it.  Therefore, we need to cleanup
144                 // the vocabulary we just created.
145                 String newId = extractId(res);
146                 allResourceIdsCreated.add(newId); // save this so we can cleanup after ourselves.
147                 throw ae; // rethrow the exception
148         } finally {
149                 if (res != null) {
150                 res.close();
151             }
152         }
153     }
154     
155
156     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
157                 dependsOnMethods = {"authorityTests"})
158     public void createItemWithBadShortId(String testName) throws Exception {
159         setupCreateWithMalformedXml();
160
161         // Submit the request to the service and store the response.
162         VocabularyClient client = new VocabularyClient();
163         HashMap<String, String> itemInfo = new HashMap<String, String>();
164         itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "Bad Item Short Id!");
165         itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "Bad Item!");
166         PoxPayloadOut multipart =
167                 VocabularyClientUtils.createVocabularyItemInstance(null, //knownResourceRefName,
168                 itemInfo, client.getItemCommonPartName());
169         Response res = client.createItem(knownResourceId, multipart);
170         try {
171                 int statusCode = res.getStatus();
172
173             if (!testRequestType.isValidStatusCode(statusCode)) {
174                 throw new RuntimeException("Could not create Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
175                         + "\" in personAuthority: \"" + knownResourceId //knownResourceRefName
176                         + "\" " + invalidStatusCodeMessage(testRequestType, statusCode));
177             }
178             if (statusCode != testExpectedStatusCode) {
179                 throw new RuntimeException("Unexpected Status when creating Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
180                         + "\" in personAuthority: \"" + knownResourceId /*knownResourceRefName*/ + "\", Status:" + statusCode);
181             }
182        } finally {
183                 if (res != null) {
184                 res.close();
185             }
186         }
187     }
188     
189     // Failure outcomes
190     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
191                 dependsOnMethods = {"updateItem"})
192     public void verifyIllegalItemDisplayName(String testName) throws Exception {
193         // Perform setup for read.
194         setupRead();
195         
196         // Submit the request to the service and store the response.
197         VocabularyClient client = new VocabularyClient();
198         Response res = client.readItem(knownResourceId, knownItemResourceId);
199         VocabularyitemsCommon vitem = null;
200         try {
201                 assertStatusCode(res, testName);
202                 // Check whether Person has expected displayName.
203                 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
204                 vitem = (VocabularyitemsCommon) extractPart(input,
205                         client.getItemCommonPartName(), VocabularyitemsCommon.class);
206                 Assert.assertNotNull(vitem);
207         } finally {
208                 if (res != null) {
209                 res.close();
210             }
211         }
212         //
213         // Try to Update with null displayName
214         //
215         setupUpdateWithInvalidBody();
216         vitem.setDisplayName(null);
217         // Submit the updated resource to the service and store the response.
218         PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
219         PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), vitem);
220         res = client.updateItem(knownResourceId, knownItemResourceId, output);
221         try {
222                 assertStatusCode(res, testName);
223         } finally {
224                 if (res != null) {
225                 res.close();
226             }
227         }
228         //
229         // Now try to Update with 1-char displayName (too short)
230         //
231         setupUpdateWithInvalidBody();
232         vitem.setDisplayName("a");
233         // Submit the updated resource to the service and store the response.
234         output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
235         commonPart = output.addPart(client.getItemCommonPartName(), vitem);
236         res = client.updateItem(knownResourceId, knownItemResourceId, output);
237         try {
238                 assertStatusCode(res, testName);
239         } finally {
240                 if (res != null) {
241                 res.close();
242             }
243         }
244     }
245
246     @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
247     public void localDelete(String testName) throws Exception {
248         super.delete(testName);
249     }
250
251     @Override
252     public void delete(String testName) throws Exception {
253         //
254         // This overrides the base test.  We don't want to do anything at this point
255         // in the test suite.  See the localDelete() method for the actual "delete" test
256         //
257     }
258         
259     @Override
260     public void deleteItem(String testName) throws Exception {
261         //Do nothing.  We don't want to delete the known item until all the dependencies of the
262         // localDeleteItem() test have been fulfilled.
263     }    
264
265     @Test(dataProvider = "testName",
266                 dependsOnMethods = {"authorityTests", "readItemList", "testItemSubmitRequest",
267         "updateItem", "verifyIllegalItemDisplayName", "verifyIgnoredUpdateWithInAuthority"})
268     public void localDeleteItem(String testName) throws Exception {
269         super.deleteItem(testName);
270     }    
271     
272     /*
273      * For convenience and terseness, this test method is the base of the test execution dependency chain.  Other test methods may
274      * refer to this method in their @Test annotation declarations.
275      */
276     @Override
277     @Test(dataProvider = "testName",
278                 dependsOnMethods = {
279                         "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.baseAuthorityTests"})    
280         public void authorityTests(String testName) {
281                 // This method only exists as a dependency target for TestNG
282         }
283     
284     // ---------------------------------------------------------------
285     // Vocabulary test specific overrides
286     // ---------------------------------------------------------------
287     
288     @Override
289     public String getServicePathComponent() {
290         return SERVICE_PATH_COMPONENT;
291     }
292
293     @Override
294     protected String getServiceName() {
295         return VocabularyClient.SERVICE_NAME;
296     }
297
298         @Override
299         protected PoxPayloadOut createInstance(String commonPartName,
300                         String identifier) {
301         String displayName = "displayName-" + identifier;
302         PoxPayloadOut result = VocabularyClientUtils.createEnumerationInstance(
303                 displayName, identifier, commonPartName);
304                 return result;
305         }
306     
307     @Override
308     protected PoxPayloadOut createInstance(String identifier) {
309         VocabularyClient client = new VocabularyClient();
310         return createInstance(client.getCommonPartName(), identifier);
311     }    
312
313         @Override
314         protected VocabulariesCommon updateInstance(
315                         VocabulariesCommon vocabulariesCommon) {
316                 VocabulariesCommon result = new VocabulariesCommon();
317                 
318                 result.setDisplayName("updated-" + vocabulariesCommon.getDisplayName());
319                 result.setVocabType("updated-" + vocabulariesCommon.getVocabType());
320         
321         return result;
322         }
323
324         @Override
325         protected void compareUpdatedInstances(VocabulariesCommon original,
326                         VocabulariesCommon updated) throws Exception {
327         Assert.assertEquals(updated.getDisplayName(),
328                         original.getDisplayName(),
329                 "Display name in updated object did not match submitted data.");
330         Assert.assertEquals(updated.getVocabType(),
331                         original.getVocabType(),
332                 "Vocabulary tyype name in updated object did not match submitted data.");
333         }
334
335     //
336     // Vocabulary item specific overrides
337     //
338
339     @Override
340     protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) {
341         String headerLabel = new VocabularyClient().getItemCommonPartName();
342         HashMap<String, String> vocabItemInfo = new HashMap<String, String>();
343         String shortId = identifier;
344         vocabItemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
345         vocabItemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
346
347         return VocabularyClientUtils.createVocabularyItemInstance(identifier, vocabItemInfo, headerLabel);
348     }    
349         
350         @Override
351         protected VocabularyitemsCommon updateItemInstance(
352                         VocabularyitemsCommon authorityItem) {
353                 VocabularyitemsCommon result = new VocabularyitemsCommon();
354                 result.setDisplayName("updated-" + authorityItem.getDisplayName());
355                 return result;
356         }
357
358         @Override
359         protected void compareUpdatedItemInstances(VocabularyitemsCommon original,
360                         VocabularyitemsCommon updated) throws Exception {
361         Assert.assertEquals(updated.getDisplayName(),
362                         original.getDisplayName(),
363                 "Display name in updated VocabularyItem did not match submitted data.");
364         }
365
366         @Override
367         protected void verifyReadItemInstance(VocabularyitemsCommon item)
368                         throws Exception {
369                 // TODO Auto-generated method stub
370                 
371         }
372
373         @Override
374         protected PoxPayloadOut createNonExistenceItemInstance(
375                         String commonPartName, String identifier) {
376         HashMap<String, String> itemInfo = new HashMap<String, String>();
377         itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "nonex");
378         itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-nonex");
379         PoxPayloadOut result =
380                 VocabularyClientUtils.createVocabularyItemInstance(
381                 null, //VocabularyClientUtils.createVocabularyRefName(NON_EXISTENT_ID, null),
382                 itemInfo, commonPartName);
383                 return result;
384         }
385 }