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