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