]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
60b43b6f4ee86cd92f078da8e8ede23765e9410b
[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     //FIXME: Remove this method once ALL the services use "_common" instead of "-common"
57     public String getCommonPartName() {
58         return getServicePathComponent() + "_common";
59     }
60
61     // ---------------------------------------------------------------
62     // CRUD tests : CREATE tests
63     // ---------------------------------------------------------------
64     // Success outcomes
65     @Override
66     @Test
67     public void create() {
68
69         // Perform setup, such as initializing the type of service request
70         // (e.g. CREATE, DELETE), its valid and expected status codes, and
71         // its associated HTTP method name (e.g. POST, DELETE).
72         setupCreate();
73
74         // Submit the request to the service and store the response.
75         String identifier = createIdentifier();
76
77         MultipartOutput multipart = createCollectionObjectInstance(identifier);
78         ClientResponse<Response> res = client.create(multipart);
79
80         int statusCode = res.getStatus();
81
82         // Check the status code of the response: does it match
83         // the expected response(s)?
84         //
85         // Specifically:
86         // Does it fall within the set of valid status codes?
87         // Does it exactly match the expected status code?
88         verbose("create: status = " + statusCode);
89         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
90                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
91         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
92
93         // Store the ID returned from this create operation
94         // for additional tests below.
95         knownResourceId = extractId(res);
96         verbose("create: knownResourceId=" + knownResourceId);
97     }
98
99     @Override
100     @Test(dependsOnMethods = {"create"})
101     public void createList() {
102         for(int i = 0; i < 3; i++){
103             create();
104         }
105     }
106
107     // Failure outcomes
108     // Placeholders until the three tests below can be uncommented.
109     // See Issue CSPACE-401.
110     public void createWithEmptyEntityBody() {}
111     public void createWithMalformedXml() {}
112     public void createWithWrongXmlSchema() {}
113
114 /*
115     @Override
116     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
117     public void createWithEmptyEntityBody() {
118     
119         // Perform setup.
120         setupCreateWithEmptyEntityBody();
121
122         // Submit the request to the service and store the response.
123         String method = REQUEST_TYPE.httpMethodName();
124         String url = getServiceRootURL();
125         String mediaType = MediaType.APPLICATION_XML;
126         final String entity = "";
127         int statusCode = submitRequest(method, url, mediaType, entity);
128         
129         // Check the status code of the response: does it match
130         // the expected response(s)?
131         verbose("createWithEmptyEntityBody url=" + url + " status=" + statusCode);
132         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
133             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
134         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
135     }
136
137     @Override
138     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
139     public void createWithMalformedXml() {
140     
141         // Perform setup.
142         setupCreateWithMalformedXml();
143
144         // Submit the request to the service and store the response.
145         String method = REQUEST_TYPE.httpMethodName();
146         String url = getServiceRootURL();
147         String mediaType = MediaType.APPLICATION_XML;
148         final String entity = MALFORMED_XML_DATA; // Constant from base class.
149         int statusCode = submitRequest(method, url, mediaType, entity);
150         
151         // Check the status code of the response: does it match
152         // the expected response(s)?
153         verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
154         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
155             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
156         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
157     }
158
159     @Override
160     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
161     public void createWithWrongXmlSchema() {
162     
163         // Perform setup.
164         setupCreateWithWrongXmlSchema();
165      
166         // Submit the request to the service and store the response.
167         String method = REQUEST_TYPE.httpMethodName();
168         String url = getServiceRootURL();
169         String mediaType = MediaType.APPLICATION_XML;
170         final String entity = WRONG_XML_SCHEMA_DATA;
171         int statusCode = submitRequest(method, url, mediaType, entity);
172         
173         // Check the status code of the response: does it match
174         // the expected response(s)?
175         verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
176         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
177             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
178         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
179     }
180 */
181
182     // ---------------------------------------------------------------
183     // CRUD tests : READ tests
184     // ---------------------------------------------------------------
185     // Success outcomes
186     @Override
187     @Test(dependsOnMethods = {"create"})
188     public void read() {
189
190         // Perform setup.
191         setupRead();
192
193         // Submit the request to the service and store the response.
194         ClientResponse<MultipartInput> res = client.read(knownResourceId);
195         int statusCode = res.getStatus();
196
197         // Check the status code of the response: does it match
198         // the expected response(s)?
199         verbose("read: status = " + statusCode);
200         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
201                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
202         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
203         //FIXME: remove the following try catch once Aron fixes signatures
204         try{
205             MultipartInput input = (MultipartInput) res.getEntity();
206             CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
207                     getCommonPartName(), CollectionobjectsCommon.class);
208             Assert.assertNotNull(collectionObject);
209         }catch(Exception e){
210             throw new RuntimeException(e);
211         }
212     }
213     
214     // Failure outcomes
215
216     @Override
217     @Test(dependsOnMethods = {"read"})
218     public void readNonExistent() {
219
220         // Perform setup.
221         setupReadNonExistent();
222
223         // Submit the request to the service and store the response.
224         ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
225         int statusCode = res.getStatus();
226
227         // Check the status code of the response: does it match
228         // the expected response(s)?
229         verbose("readNonExistent: status = " + res.getStatus());
230         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
231                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
232         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
233     }
234
235     // ---------------------------------------------------------------
236     // CRUD tests : READ_LIST tests
237     // ---------------------------------------------------------------
238     // Success outcomes
239     @Override
240     @Test(dependsOnMethods = {"createList", "read"})
241     public void readList() {
242         // Perform setup.
243         setupReadList();
244
245         // Submit the request to the service and store the response.
246         ClientResponse<CollectionobjectsCommonList> res = client.readList();
247         CollectionobjectsCommonList list = res.getEntity();
248
249         int statusCode = res.getStatus();
250
251         // Check the status code of the response: does it match
252         // the expected response(s)?
253         verbose("readList: status = " + res.getStatus());
254         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
255                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
256         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
257
258         // Optionally output additional data about list members for debugging.
259         boolean iterateThroughList = false;
260         if (iterateThroughList && logger.isDebugEnabled()) {
261             List<CollectionobjectsCommonList.CollectionObjectListItem> items =
262                 list.getCollectionObjectListItem();
263             int i = 0;
264
265             for(CollectionobjectsCommonList.CollectionObjectListItem item : items){
266                 verbose("readList: list-item[" + i + "] csid=" +
267                     item.getCsid());
268                 verbose("readList: list-item[" + i + "] objectNumber=" +
269                     item.getObjectNumber());
270                 verbose("readList: list-item[" + i + "] URI=" +
271                     item.getUri());
272                 i++;
273             }
274         }
275     }
276
277     // Failure outcomes
278     // None at present.
279     // ---------------------------------------------------------------
280     // CRUD tests : UPDATE tests
281     // ---------------------------------------------------------------
282     // Success outcomes
283     @Override
284     @Test(dependsOnMethods = {"read"})
285     public void update() {
286
287         // Perform setup.
288         setupUpdate();
289         try{ //ideally, just remove try-catch and let the exception bubble up
290             // Retrieve an existing resource that we can update.
291             ClientResponse<MultipartInput> res =
292                     client.read(knownResourceId);
293             verbose("update: read status = " + res.getStatus());
294             Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
295
296             verbose("got object to update with ID: " + knownResourceId);
297             MultipartInput input = (MultipartInput) res.getEntity();
298             CollectionobjectsCommon collectionObject = (CollectionobjectsCommon) extractPart(input,
299                     getCommonPartName(), CollectionobjectsCommon.class);
300             Assert.assertNotNull(collectionObject);
301
302             // Update the content of this resource.
303             collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
304             collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
305             verbose("updated object", collectionObject, CollectionobjectsCommon.class);
306             // Submit the request to the service and store the response.
307             MultipartOutput output = new MultipartOutput();
308             OutputPart commonPart = output.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
309             commonPart.getHeaders().add("label", getCommonPartName());
310
311             res = client.update(knownResourceId, output);
312             int statusCode = res.getStatus();
313             // Check the status code of the response: does it match the expected response(s)?
314             verbose("update: status = " + res.getStatus());
315             Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
316                     invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
317             Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
318
319
320             input = (MultipartInput) res.getEntity();
321             CollectionobjectsCommon updatedCollectionObject =
322                     (CollectionobjectsCommon) extractPart(input,
323                     getCommonPartName(), CollectionobjectsCommon.class);
324             Assert.assertNotNull(updatedCollectionObject);
325
326             Assert.assertEquals(updatedCollectionObject.getObjectName(),
327                     collectionObject.getObjectName(),
328                     "Data in updated object did not match submitted data.");
329         }catch(Exception e){
330             e.printStackTrace();
331         }
332     }
333     
334     // Failure outcomes
335
336     // Placeholders until the three tests below can be uncommented.
337     // See Issue CSPACE-401.
338     public void updateWithEmptyEntityBody() {}
339     public void updateWithMalformedXml() {}
340     public void updateWithWrongXmlSchema() {}
341
342 /*
343     @Override
344
345     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
346     public void updateWithEmptyEntityBody() {
347     
348         // Perform setup.
349         setupUpdateWithEmptyEntityBody();
350
351         // Submit the request to the service and store the response.
352         String method = REQUEST_TYPE.httpMethodName();
353         String url = getResourceURL(knownResourceId);
354         String mediaType = MediaType.APPLICATION_XML;
355         final String entity = "";
356         int statusCode = submitRequest(method, url, mediaType, entity);
357         
358         // Check the status code of the response: does it match
359         // the expected response(s)?
360         verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
361         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
362             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
363         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
364     }
365
366     @Override
367     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
368     public void updateWithMalformedXml() {
369
370         // Perform setup.
371         setupUpdateWithMalformedXml();
372
373         // Submit the request to the service and store the response.
374         String method = REQUEST_TYPE.httpMethodName();
375         String url = getResourceURL(knownResourceId);
376         final String entity = MALFORMED_XML_DATA;
377         String mediaType = MediaType.APPLICATION_XML;
378         int statusCode = submitRequest(method, url, mediaType, entity);
379         
380         // Check the status code of the response: does it match
381         // the expected response(s)?
382         verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
383         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
384             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
385         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
386     }
387
388     @Override
389     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
390     public void updateWithWrongXmlSchema() {
391     
392         // Perform setup.
393         setupUpdateWithWrongXmlSchema();
394         
395         // Submit the request to the service and store the response.
396         String method = REQUEST_TYPE.httpMethodName();
397         String url = getResourceURL(knownResourceId);
398         String mediaType = MediaType.APPLICATION_XML;
399         final String entity = WRONG_XML_SCHEMA_DATA;
400         int statusCode = submitRequest(method, url, mediaType, entity);
401         
402         // Check the status code of the response: does it match
403         // the expected response(s)?
404         verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
405         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
406             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
407         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
408     }
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
421         // The only relevant ID may be the one used in updateCollectionObject(), below.
422         MultipartOutput multipart = createCollectionObjectInstance(NON_EXISTENT_ID);
423         ClientResponse<MultipartInput> res =
424                 client.update(NON_EXISTENT_ID, multipart);
425
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         // @TODO Determine if it is possible to obtain this
510         // value programmatically.
511         //
512         // We set this in an annotation in the CollectionObjectProxy
513         // interface, for instance.  We also set service-specific
514         // constants in each service module, which might also
515         // return this value.
516         return SERVICE_PATH_COMPONENT;
517     }
518
519     private MultipartOutput createCollectionObjectInstance(String identifier) {
520         return createCollectionObjectInstance("objectNumber-" + identifier,
521                 "objectName-" + identifier);
522     }
523
524     private MultipartOutput createCollectionObjectInstance(String objectNumber, String objectName) {
525         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
526
527         collectionObject.setObjectNumber(objectNumber);
528         collectionObject.setObjectName(objectName);
529         MultipartOutput multipart = new MultipartOutput();
530         OutputPart commonPart = multipart.addPart(collectionObject, MediaType.APPLICATION_XML_TYPE);
531         commonPart.getHeaders().add("label", getCommonPartName());
532
533         verbose("to be created, collectionobject common ", collectionObject, CollectionobjectsCommon.class);
534
535         CollectionObjectNaturalhistory conh = new CollectionObjectNaturalhistory();
536         conh.setNhString("test-string");
537         conh.setNhInt(999);
538         conh.setNhLong(9999);
539         OutputPart nhPart = multipart.addPart(conh, MediaType.APPLICATION_XML_TYPE);
540         nhPart.getHeaders().add("label", getNHPartName());
541
542         verbose("to be created, collectionobject nhistory", conh, CollectionObjectNaturalhistory.class);
543         return multipart;
544
545     }
546
547     private String getNHPartName() {
548         return "collectionobjects-naturalhistory";
549     }
550 }