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