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