]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2d7883a16f7a2b249de13dcd04e79d4dfe2ccc77
[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() throws Exception {
80         return new VocabularyClient();
81     }
82
83         @Override
84         protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
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      * Sets up create tests with malformed xml.
163      */
164         protected void setupcreateItemWithBadShortId() {
165         testExpectedStatusCode = STATUS_BAD_REQUEST;  // Malformed payload never gets past RESTEasy filter
166         testRequestType = ServiceRequestType.CREATE;
167         testSetup(testExpectedStatusCode, testRequestType);
168     }    
169
170     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
171                 dependsOnMethods = {"authorityTests"})
172     public void createItemWithBadShortId(String testName) throws Exception {
173         setupcreateItemWithBadShortId();
174
175         // Submit the request to the service and store the response.
176         VocabularyClient client = new VocabularyClient();
177         HashMap<String, String> itemInfo = new HashMap<String, String>();
178         itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "Bad Item Short Id!");
179         itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "Bad Item!");
180         PoxPayloadOut multipart =
181                 VocabularyClientUtils.createVocabularyItemInstance(null, //knownResourceRefName,
182                 itemInfo, client.getItemCommonPartName());
183         Response res = client.createItem(knownResourceId, multipart);
184         try {
185                 int statusCode = res.getStatus();
186
187             if (!testRequestType.isValidStatusCode(statusCode)) {
188                 throw new RuntimeException("Could not create Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
189                         + "\" in personAuthority: \"" + knownResourceId //knownResourceRefName
190                         + "\" " + invalidStatusCodeMessage(testRequestType, statusCode));
191             }
192             if (statusCode != testExpectedStatusCode) {
193                 throw new RuntimeException("Unexpected Status when creating Item: \"" + itemInfo.get(AuthorityItemJAXBSchema.DISPLAY_NAME)
194                         + "\" in personAuthority: \"" + knownResourceId /*knownResourceRefName*/ + "\", Status:" + statusCode);
195             }
196        } finally {
197                 if (res != null) {
198                 res.close();
199             }
200         }
201     }
202     
203     // Failure outcomes
204     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
205                 dependsOnMethods = {"updateItem"})
206     public void verifyIllegalItemDisplayName(String testName) throws Exception {
207         // Perform setup for read.
208         setupRead();
209         
210         // Submit the request to the service and store the response.
211         VocabularyClient client = new VocabularyClient();
212         Response res = client.readItem(knownResourceId, knownItemResourceId);
213         VocabularyitemsCommon vitem = null;
214         try {
215                 assertStatusCode(res, testName);
216                 // Check whether Person has expected displayName.
217                 PoxPayloadIn input = new PoxPayloadIn(res.readEntity(String.class));
218                 vitem = (VocabularyitemsCommon) extractPart(input,
219                         client.getItemCommonPartName(), VocabularyitemsCommon.class);
220                 Assert.assertNotNull(vitem);
221         } finally {
222                 if (res != null) {
223                 res.close();
224             }
225         }
226         //
227         // Try to Update with null displayName
228         //
229         setupUpdateWithInvalidBody();
230         vitem.setDisplayName(null);
231         // Submit the updated resource to the service and store the response.
232         PoxPayloadOut output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
233         PayloadOutputPart commonPart = output.addPart(client.getItemCommonPartName(), vitem);
234         res = client.updateItem(knownResourceId, knownItemResourceId, output);
235         try {
236                 assertStatusCode(res, testName);
237         } finally {
238                 if (res != null) {
239                 res.close();
240             }
241         }
242         //
243         // Now try to Update with 1-char displayName (too short)
244         //
245         setupUpdateWithInvalidBody();
246         vitem.setDisplayName("a");
247         // Submit the updated resource to the service and store the response.
248         output = new PoxPayloadOut(SERVICE_ITEM_PAYLOAD_NAME);
249         commonPart = output.addPart(client.getItemCommonPartName(), vitem);
250         res = client.updateItem(knownResourceId, knownItemResourceId, output);
251         try {
252                 assertStatusCode(res, testName);
253         } finally {
254                 if (res != null) {
255                 res.close();
256             }
257         }
258     }
259
260     @Test(dataProvider = "testName", dependsOnMethods = {"localDeleteItem"})
261     public void localDelete(String testName) throws Exception {
262         super.delete(testName);
263     }
264
265     @Override
266     public void delete(String testName) throws Exception {
267         //
268         // This overrides the base test.  We don't want to do anything at this point
269         // in the test suite.  See the localDelete() method for the actual "delete" test
270         //
271     }
272         
273     @Override
274     public void deleteItem(String testName) throws Exception {
275         //Do nothing.  We don't want to delete the known item until all the dependencies of the
276         // localDeleteItem() test have been fulfilled.
277     }    
278
279     @Test(dataProvider = "testName",
280                 dependsOnMethods = {"authorityTests", "readItemList", "testItemSubmitRequest",
281         "updateItem", "verifyIllegalItemDisplayName", "verifyIgnoredUpdateWithInAuthority"})
282     public void localDeleteItem(String testName) throws Exception {
283         super.deleteItem(testName);
284     }    
285     
286     /*
287      * For convenience and terseness, this test method is the base of the test execution dependency chain.  Other test methods may
288      * refer to this method in their @Test annotation declarations.
289      */
290     @Override
291     @Test(dataProvider = "testName",
292                 dependsOnMethods = {
293                         "org.collectionspace.services.client.test.AbstractAuthorityServiceTest.baseAuthorityTests"})    
294         public void authorityTests(String testName) {
295                 // This method only exists as a dependency target for TestNG
296         }
297     
298     // ---------------------------------------------------------------
299     // Vocabulary test specific overrides
300     // ---------------------------------------------------------------
301     
302     @Override
303     public String getServicePathComponent() {
304         return SERVICE_PATH_COMPONENT;
305     }
306
307     @Override
308     protected String getServiceName() {
309         return VocabularyClient.SERVICE_NAME;
310     }
311
312         @Override
313         protected PoxPayloadOut createInstance(String commonPartName,
314                         String identifier) {
315         String displayName = "displayName-" + identifier;
316         PoxPayloadOut result = VocabularyClientUtils.createEnumerationInstance(
317                 displayName, identifier, commonPartName);
318                 return result;
319         }
320     
321     @Override
322     protected PoxPayloadOut createInstance(String identifier) throws Exception {
323         VocabularyClient client = new VocabularyClient();
324         return createInstance(client.getCommonPartName(), identifier);
325     }    
326
327         @Override
328         protected VocabulariesCommon updateInstance(
329                         VocabulariesCommon vocabulariesCommon) {
330                 VocabulariesCommon result = new VocabulariesCommon();
331                 
332                 result.setDisplayName("updated-" + vocabulariesCommon.getDisplayName());
333                 result.setVocabType("updated-" + vocabulariesCommon.getVocabType());
334         
335         return result;
336         }
337
338         @Override
339         protected void compareUpdatedInstances(VocabulariesCommon original,
340                         VocabulariesCommon updated) throws Exception {
341         Assert.assertEquals(updated.getDisplayName(),
342                         original.getDisplayName(),
343                 "Display name in updated object did not match submitted data.");
344         Assert.assertEquals(updated.getVocabType(),
345                         original.getVocabType(),
346                 "Vocabulary tyype name in updated object did not match submitted data.");
347         }
348
349     //
350     // Vocabulary item specific overrides
351     //
352
353     @Override
354     protected PoxPayloadOut createItemInstance(String parentCsid, String identifier) throws Exception {
355         String headerLabel = new VocabularyClient().getItemCommonPartName();
356         HashMap<String, String> vocabItemInfo = new HashMap<String, String>();
357         String shortId = identifier;
358         vocabItemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, shortId);
359         vocabItemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-" + shortId);
360
361         return VocabularyClientUtils.createVocabularyItemInstance(identifier, vocabItemInfo, headerLabel);
362     }    
363         
364         @Override
365         protected VocabularyitemsCommon updateItemInstance(
366                         VocabularyitemsCommon authorityItem) {
367                 VocabularyitemsCommon result = new VocabularyitemsCommon();
368                 result.setDisplayName("updated-" + authorityItem.getDisplayName());
369                 return result;
370         }
371
372         @Override
373         protected void compareUpdatedItemInstances(VocabularyitemsCommon original,
374                         VocabularyitemsCommon updated,
375                         boolean compareRevNumbers) throws Exception {
376         Assert.assertEquals(updated.getDisplayName(),
377                         original.getDisplayName(),
378                 "Display name in updated VocabularyItem did not match submitted data.");
379         
380         if (compareRevNumbers == true) {
381                 Assert.assertEquals(original.getRev(), updated.getRev(), "Revision numbers should match.");
382         }
383         }
384
385         @Override
386         protected void verifyReadItemInstance(VocabularyitemsCommon item)
387                         throws Exception {
388                 // TODO Auto-generated method stub
389                 
390         }
391
392         @Override
393         protected PoxPayloadOut createNonExistenceItemInstance(
394                         String commonPartName, String identifier) {
395         HashMap<String, String> itemInfo = new HashMap<String, String>();
396         itemInfo.put(AuthorityItemJAXBSchema.SHORT_IDENTIFIER, "nonex");
397         itemInfo.put(AuthorityItemJAXBSchema.DISPLAY_NAME, "display-nonex");
398         PoxPayloadOut result =
399                 VocabularyClientUtils.createVocabularyItemInstance(
400                 null, //VocabularyClientUtils.createVocabularyRefName(NON_EXISTENT_ID, null),
401                 itemInfo, commonPartName);
402                 return result;
403         }
404         
405     @AfterClass(alwaysRun = true)
406     @Override
407     public void cleanUp() throws Exception {
408         super.cleanUp();
409     }   
410 }