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