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