]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
f40443af18014cc2317d445487982b31c561ee6c
[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 © 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.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28
29 import org.collectionspace.services.client.VocabularyClient;
30 import org.collectionspace.services.vocabulary.VocabulariesCommon;
31 import org.collectionspace.services.vocabulary.VocabulariesCommonList;
32
33 import org.jboss.resteasy.client.ClientResponse;
34
35 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
37 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
40
41 /**
42  * VocabularyServiceTest, carries out tests against a
43  * deployed and running Vocabulary Service.
44  *
45  * $LastChangedRevision: 753 $
46  * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
47  */
48 public class VocabularyServiceTest extends AbstractServiceTest {
49
50     // Instance variables specific to this test.
51     private VocabularyClient client = new VocabularyClient();
52     final String SERVICE_PATH_COMPONENT = "vocabularies";
53     private String knownResourceId = null;
54
55     // ---------------------------------------------------------------
56     // CRUD tests : CREATE tests
57     // ---------------------------------------------------------------
58     // Success outcomes
59     @Override
60     @Test
61     public void create() {
62
63         // Perform setup, such as initializing the type of service request
64         // (e.g. CREATE, DELETE), its valid and expected status codes, and
65         // its associated HTTP method name (e.g. POST, DELETE).
66         setupCreate();
67
68         // Submit the request to the service and store the response.
69         String identifier = createIdentifier();
70
71         MultipartOutput multipart = createVocabularyInstance(identifier);
72         ClientResponse<Response> res = client.create(multipart);
73
74         int statusCode = res.getStatus();
75
76         // Check the status code of the response: does it match
77         // the expected response(s)?
78         //
79         // Specifically:
80         // Does it fall within the set of valid status codes?
81         // Does it exactly match the expected status code?
82         verbose("create: status = " + statusCode);
83         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
84                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
85         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
86
87         // Store the ID returned from this create operation
88         // for additional tests below.
89         knownResourceId = extractId(res);
90         verbose("create: knownResourceId=" + knownResourceId);
91     }
92
93     @Override
94     @Test(dependsOnMethods = {"create"})
95     public void createList() {
96         for(int i = 0; i < 3; i++){
97             create();
98         }
99     }
100
101     // Failure outcomes
102     // Placeholders until the three tests below can be uncommented.
103     // See Issue CSPACE-401.
104     public void createWithEmptyEntityBody() {
105     }
106
107     public void createWithMalformedXml() {
108     }
109
110     public void createWithWrongXmlSchema() {
111     }
112
113     /*
114     @Override
115     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
116     public void createWithEmptyEntityBody() {
117
118     // Perform setup.
119     setupCreateWithEmptyEntityBody();
120
121     // Submit the request to the service and store the response.
122     String method = REQUEST_TYPE.httpMethodName();
123     String url = getServiceRootURL();
124     String mediaType = MediaType.APPLICATION_XML;
125     final String entity = "";
126     int statusCode = submitRequest(method, url, mediaType, entity);
127
128     // Check the status code of the response: does it match
129     // the expected response(s)?
130     verbose("createWithEmptyEntityBody url=" + url + " status=" + statusCode);
131     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
132     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
133     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
134     }
135
136     @Override
137     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
138     public void createWithMalformedXml() {
139
140     // Perform setup.
141     setupCreateWithMalformedXml();
142
143     // Submit the request to the service and store the response.
144     String method = REQUEST_TYPE.httpMethodName();
145     String url = getServiceRootURL();
146     String mediaType = MediaType.APPLICATION_XML;
147     final String entity = MALFORMED_XML_DATA; // Constant from base class.
148     int statusCode = submitRequest(method, url, mediaType, entity);
149
150     // Check the status code of the response: does it match
151     // the expected response(s)?
152     verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
153     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
154     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
155     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
156     }
157
158     @Override
159     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
160     public void createWithWrongXmlSchema() {
161
162     // Perform setup.
163     setupCreateWithWrongXmlSchema();
164
165     // Submit the request to the service and store the response.
166     String method = REQUEST_TYPE.httpMethodName();
167     String url = getServiceRootURL();
168     String mediaType = MediaType.APPLICATION_XML;
169     final String entity = WRONG_XML_SCHEMA_DATA;
170     int statusCode = submitRequest(method, url, mediaType, entity);
171
172     // Check the status code of the response: does it match
173     // the expected response(s)?
174     verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
175     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
176     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
177     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
178     }
179      */
180     // ---------------------------------------------------------------
181     // CRUD tests : READ tests
182     // ---------------------------------------------------------------
183     // Success outcomes
184     @Override
185     @Test(dependsOnMethods = {"create"})
186     public void read() {
187
188         // Perform setup.
189         setupRead();
190
191         // Submit the request to the service and store the response.
192         ClientResponse<MultipartInput> res = client.read(knownResourceId);
193         int statusCode = res.getStatus();
194
195         // Check the status code of the response: does it match
196         // the expected response(s)?
197         verbose("read: status = " + statusCode);
198         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
199                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
200         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
201         //FIXME: remove the following try catch once Aron fixes signatures
202         try{
203             MultipartInput input = (MultipartInput) res.getEntity();
204             VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
205                         client.getCommonPartName(), VocabulariesCommon.class);
206             Assert.assertNotNull(vocabulary);
207         }catch(Exception e){
208             throw new RuntimeException(e);
209         }
210     }
211
212     // Failure outcomes
213     @Override
214     @Test(dependsOnMethods = {"read"})
215     public void readNonExistent() {
216
217         // Perform setup.
218         setupReadNonExistent();
219
220         // Submit the request to the service and store the response.
221         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
222         int statusCode = res.getStatus();
223
224         // Check the status code of the response: does it match
225         // the expected response(s)?
226         verbose("readNonExistent: status = " + res.getStatus());
227         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
228                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
229         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
230     }
231
232     // ---------------------------------------------------------------
233     // CRUD tests : READ_LIST tests
234     // ---------------------------------------------------------------
235     // Success outcomes
236     @Override
237     @Test(dependsOnMethods = {"read"})
238     public void readList() {
239
240         // Perform setup.
241         setupReadList();
242
243         // Submit the request to the service and store the response.
244         ClientResponse<VocabulariesCommonList> res = client.readList();
245         VocabulariesCommonList list = res.getEntity();
246         int statusCode = res.getStatus();
247
248         // Check the status code of the response: does it match
249         // the expected response(s)?
250         verbose("readList: status = " + res.getStatus());
251         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
252                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
253         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
254
255         // Optionally output additional data about list members for debugging.
256         boolean iterateThroughList = false;
257         if(iterateThroughList && logger.isDebugEnabled()){
258             List<VocabulariesCommonList.VocabularyListItem> items =
259                     list.getVocabularyListItem();
260             int i = 0;
261             for(VocabulariesCommonList.VocabularyListItem item : items){
262                 verbose("readList: list-item[" + i + "] csid=" +
263                         item.getCsid());
264                 verbose("readList: list-item[" + i + "] displayName=" +
265                         item.getDisplayName());
266                 verbose("readList: list-item[" + i + "] URI=" +
267                         item.getUri());
268                 i++;
269             }
270         }
271
272     }
273
274     // Failure outcomes
275     // None at present.
276     // ---------------------------------------------------------------
277     // CRUD tests : UPDATE tests
278     // ---------------------------------------------------------------
279     // Success outcomes
280     @Override
281     @Test(dependsOnMethods = {"read"})
282     public void update() {
283
284         // Perform setup.
285         setupUpdate();
286
287         try{ //ideally, just remove try-catch and let the exception bubble up
288             // Retrieve an existing resource that we can update.
289             ClientResponse<MultipartInput> res =
290                     client.read(knownResourceId);
291             verbose("update: read status = " + res.getStatus());
292             Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
293
294             verbose("got object to update with ID: " + knownResourceId);
295             MultipartInput input = (MultipartInput) res.getEntity();
296             VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
297                         client.getCommonPartName(), VocabulariesCommon.class);
298             Assert.assertNotNull(vocabulary);
299
300             // Update the content of this resource.
301             vocabulary.setDisplayName("updated-" + vocabulary.getDisplayName());
302              vocabulary.setVocabType("updated-" + vocabulary.getVocabType());
303            verbose("to be updated object", vocabulary, VocabulariesCommon.class);
304             // Submit the request to the service and store the response.
305             MultipartOutput output = new MultipartOutput();
306             OutputPart commonPart = output.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
307             commonPart.getHeaders().add("label", client.getCommonPartName());
308
309             res = client.update(knownResourceId, output);
310             int statusCode = res.getStatus();
311             // Check the status code of the response: does it match the expected response(s)?
312             verbose("update: status = " + res.getStatus());
313             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
314                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
315             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
316
317
318             input = (MultipartInput) res.getEntity();
319             VocabulariesCommon updatedVocabulary =
320                     (VocabulariesCommon) extractPart(input,
321                                 client.getCommonPartName(), VocabulariesCommon.class);
322             Assert.assertNotNull(updatedVocabulary);
323
324             Assert.assertEquals(updatedVocabulary.getDisplayName(),
325                     vocabulary.getDisplayName(),
326                     "Data in updated object did not match submitted data.");
327         }catch(Exception e){
328             e.printStackTrace();
329         }
330     }
331
332     // Failure outcomes
333     // Placeholders until the three tests below can be uncommented.
334     // See Issue CSPACE-401.
335     public void updateWithEmptyEntityBody() {
336     }
337
338     public void updateWithMalformedXml() {
339     }
340
341     public void updateWithWrongXmlSchema() {
342     }
343
344     /*
345     @Override
346     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
347     public void updateWithEmptyEntityBody() {
348
349     // Perform setup.
350     setupUpdateWithEmptyEntityBody();
351
352     // Submit the request to the service and store the response.
353     String method = REQUEST_TYPE.httpMethodName();
354     String url = getResourceURL(knownResourceId);
355     String mediaType = MediaType.APPLICATION_XML;
356     final String entity = "";
357     int statusCode = submitRequest(method, url, mediaType, entity);
358
359     // Check the status code of the response: does it match
360     // the expected response(s)?
361     verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
362     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
363     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
364     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
365     }
366
367     @Override
368     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
369     public void updateWithMalformedXml() {
370
371     // Perform setup.
372     setupUpdateWithMalformedXml();
373
374     // Submit the request to the service and store the response.
375     String method = REQUEST_TYPE.httpMethodName();
376     String url = getResourceURL(knownResourceId);
377     String mediaType = MediaType.APPLICATION_XML;
378     final String entity = MALFORMED_XML_DATA;
379     int statusCode = submitRequest(method, url, mediaType, entity);
380
381     // Check the status code of the response: does it match
382     // the expected response(s)?
383     verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
384     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
385     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
386     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
387     }
388
389     @Override
390     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
391     public void updateWithWrongXmlSchema() {
392
393     // Perform setup.
394     setupUpdateWithWrongXmlSchema();
395
396     // Submit the request to the service and store the response.
397     String method = REQUEST_TYPE.httpMethodName();
398     String url = getResourceURL(knownResourceId);
399     String mediaType = MediaType.APPLICATION_XML;
400     final String entity = WRONG_XML_SCHEMA_DATA;
401     int statusCode = submitRequest(method, url, mediaType, entity);
402
403     // Check the status code of the response: does it match
404     // the expected response(s)?
405     verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
406     Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
407     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
408     Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
409     }
410      */
411     @Override
412     @Test(dependsOnMethods = {"update", "testSubmitRequest"})
413     public void updateNonExistent() {
414
415         // Perform setup.
416         setupUpdateNonExistent();
417
418         // Submit the request to the service and store the response.
419         // Note: The ID used in this 'create' call may be arbitrary.
420         // The only relevant ID may be the one used in update(), below.
421
422         // The only relevant ID may be the one used in update(), below.
423         MultipartOutput multipart = createVocabularyInstance(NON_EXISTENT_ID);
424         ClientResponse<MultipartInput> res =
425                 client.update(NON_EXISTENT_ID, multipart);
426         int statusCode = res.getStatus();
427
428         // Check the status code of the response: does it match
429         // the expected response(s)?
430         verbose("updateNonExistent: status = " + res.getStatus());
431         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
432                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
433         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
434     }
435
436     // ---------------------------------------------------------------
437     // CRUD tests : DELETE tests
438     // ---------------------------------------------------------------
439     // Success outcomes
440     @Override
441     @Test(dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
442     public void delete() {
443
444         // Perform setup.
445         setupDelete();
446
447         // Submit the request to the service and store the response.
448         ClientResponse<Response> res = client.delete(knownResourceId);
449         int statusCode = res.getStatus();
450
451         // Check the status code of the response: does it match
452         // the expected response(s)?
453         verbose("delete: status = " + res.getStatus());
454         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
455                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
456         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
457     }
458
459     // Failure outcomes
460     @Override
461     @Test(dependsOnMethods = {"delete"})
462     public void deleteNonExistent() {
463
464         // Perform setup.
465         setupDeleteNonExistent();
466
467         // Submit the request to the service and store the response.
468         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
469         int statusCode = res.getStatus();
470
471         // Check the status code of the response: does it match
472         // the expected response(s)?
473         verbose("deleteNonExistent: status = " + res.getStatus());
474         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
475                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
476         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
477     }
478
479     // ---------------------------------------------------------------
480     // Utility tests : tests of code used in tests above
481     // ---------------------------------------------------------------
482     /**
483      * Tests the code for manually submitting data that is used by several
484      * of the methods above.
485      */
486     @Test(dependsOnMethods = {"create", "read"})
487     public void testSubmitRequest() {
488
489         // Expected status code: 200 OK
490         final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
491
492         // Submit the request to the service and store the response.
493         String method = ServiceRequestType.READ.httpMethodName();
494         String url = getResourceURL(knownResourceId);
495         int statusCode = submitRequest(method, url);
496
497         // Check the status code of the response: does it match
498         // the expected response(s)?
499         verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
500         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
501
502     }
503
504     // ---------------------------------------------------------------
505     // Utility methods used by tests above
506     // ---------------------------------------------------------------
507     @Override
508     public String getServicePathComponent() {
509         return SERVICE_PATH_COMPONENT;
510     }
511
512     private MultipartOutput createVocabularyInstance(String identifier) {
513         return createVocabularyInstance(
514                 "displayName-" + identifier,
515                 "vocabType-" + identifier);
516     }
517
518     private MultipartOutput createVocabularyInstance(String displayName, String vocabType) {
519         VocabulariesCommon vocabulary = new VocabulariesCommon();
520         vocabulary.setDisplayName(displayName);
521         vocabulary.setVocabType(vocabType);
522         MultipartOutput multipart = new MultipartOutput();
523         OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
524         commonPart.getHeaders().add("label", client.getCommonPartName());
525
526         verbose("to be created, vocabulary common ", vocabulary, VocabulariesCommon.class);
527
528         return multipart;
529     }
530 }