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