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