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