]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
6db1f4eb5f9165e98515c7f4f133632c86e2451e
[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  
24  package org.collectionspace.services.client.test;
25
26 import java.util.List;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.Response.Status;
29
30 import org.collectionspace.services.client.AcquisitionClient;
31 import org.collectionspace.services.client.test.ServiceRequestType;
32 import org.collectionspace.services.acquisition.Acquisition;
33 import org.collectionspace.services.acquisition.AcquisitionList;
34
35 import org.jboss.resteasy.client.ClientResponse;
36
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
39
40 /**
41  * AcquisitionServiceTest, carries out tests against a
42  * deployed and running Acquisition Service.
43  * 
44  * $LastChangedRevision: 621 $
45  * $LastChangedDate: 2009-09-02 16:49:01 -0700 (Wed, 02 Sep 2009) $
46  */
47 public class AcquisitionServiceTest extends AbstractServiceTest {
48
49     // Instance variables specific to this test.
50     private AcquisitionClient client = new AcquisitionClient();
51     final String SERVICE_PATH_COMPONENT = "acquisitions";
52     private String knownResourceId = null;
53
54
55     // ---------------------------------------------------------------
56     // CRUD tests : CREATE tests
57     // ---------------------------------------------------------------
58
59     // Success outcomes
60     
61     @Override
62     @Test
63     public void create() {
64
65         // Perform setup, such as initializing the type of service request
66         // (e.g. CREATE, DELETE), its valid and expected status codes, and
67         // its associated HTTP method name (e.g. POST, DELETE).
68         setupCreate();
69
70         // Submit the request to the service and store the response.
71         String identifier = createIdentifier();
72         Acquisition acquisition = createAcquisitionInstance(identifier);
73         ClientResponse<Response> res = client.create(acquisition);
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     }
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     
102     // Placeholders until the three tests below can be uncommented.
103     // See Issue CSPACE-401.
104     public void createWithEmptyEntityBody() {}
105     public void createWithMalformedXml() {}
106     public void createWithWrongXmlSchema() {}
107
108 /*
109     @Override
110     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
111     public void createWithMalformedXml() {
112     
113         // Perform setup.
114         setupCreateWithMalformedXml();
115
116         // Submit the request to the service and store the response.
117         String method = REQUEST_TYPE.httpMethodName();
118         String url = getServiceRootURL();
119         final String entity = MALFORMED_XML_DATA; // Constant from base class.
120         int statusCode = submitRequest(method, url, entity);
121         
122         // Check the status code of the response: does it match
123         // the expected response(s)?
124         verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
125         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
126             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
127         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
128     }
129
130     @Override
131     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
132     public void createWithWrongXmlSchema() {
133     
134         // Perform setup.
135         setupCreateWithWrongXmlSchema();
136      
137         // Submit the request to the service and store the response.
138         String method = REQUEST_TYPE.httpMethodName();
139         String url = getServiceRootURL();
140         final String entity = WRONG_XML_SCHEMA_DATA;
141         int statusCode = submitRequest(method, url, entity);
142         
143         // Check the status code of the response: does it match
144         // the expected response(s)?
145         verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
146         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
147             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
148         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
149     }
150 */
151
152     // ---------------------------------------------------------------
153     // CRUD tests : READ tests
154     // ---------------------------------------------------------------
155
156     // Success outcomes
157
158     @Override
159     @Test(dependsOnMethods = {"create"})
160     public void read() {
161     
162         // Perform setup.
163         setupRead();
164
165         // Submit the request to the service and store the response.
166         ClientResponse<Acquisition> res = client.read(knownResourceId);
167         int statusCode = res.getStatus();
168             
169         // Check the status code of the response: does it match
170         // the expected response(s)?
171         verbose("read: status = " + statusCode);
172         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
173             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
174         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
175     }
176
177     // Failure outcomes
178
179     @Override
180     @Test(dependsOnMethods = {"read"})
181     public void readNonExistent() {
182
183         // Perform setup.
184         setupReadNonExistent();
185         
186         // Submit the request to the service and store the response.
187         ClientResponse<Acquisition> res = client.read(NON_EXISTENT_ID);
188         int statusCode = res.getStatus();
189
190         // Check the status code of the response: does it match
191         // the expected response(s)?
192         verbose("readNonExistent: status = " + res.getStatus());
193         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
194             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
195         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
196     }
197
198
199     // ---------------------------------------------------------------
200     // CRUD tests : READ_LIST tests
201     // ---------------------------------------------------------------
202
203     // Success outcomes
204
205     @Override
206     @Test(dependsOnMethods = {"createList"})
207     public void readList() {
208     
209         // Perform setup.
210         setupReadList();
211
212         // Submit the request to the service and store the response.
213         ClientResponse<AcquisitionList> res = client.readList();
214         AcquisitionList list = res.getEntity();
215         int statusCode = res.getStatus();
216
217         // Check the status code of the response: does it match
218         // the expected response(s)?
219         verbose("readList: status = " + res.getStatus());
220         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
221             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
222         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
223
224         // Optionally output additional data about list members for debugging.
225         boolean iterateThroughList = false;
226         if (iterateThroughList && logger.isDebugEnabled()) {
227             List<AcquisitionList.AcquisitionListItem> items =
228                 list.getAcquisitionListItem();
229             int i = 0;
230             for(AcquisitionList.AcquisitionListItem item : items){
231                 verbose("readList: list-item[" + i + "] csid=" +
232                     item.getCsid());
233                 verbose("readList: list-item[" + i + "] objectNumber=" +
234                     item.getAccessiondate());
235                 verbose("readList: list-item[" + i + "] URI=" +
236                     item.getUri());
237                 i++;
238             }
239         }
240         
241     }
242
243     // Failure outcomes
244     
245     // None at present.
246
247
248     // ---------------------------------------------------------------
249     // CRUD tests : UPDATE tests
250     // ---------------------------------------------------------------
251
252     // Success outcomes
253
254     @Override
255     @Test(dependsOnMethods = {"create"})
256     public void update() {
257     
258         // Perform setup.
259         setupUpdate();
260
261         // Retrieve an existing resource that we can update.
262         ClientResponse<Acquisition> res = client.read(knownResourceId);
263         verbose("read: status = " + res.getStatus());
264         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
265         Acquisition acquisition = res.getEntity();
266         verbose("Got object to update with ID: " + knownResourceId,
267                 acquisition, Acquisition.class);
268
269         // Update the content of this resource.
270         acquisition.setAccessiondate("updated-" + acquisition.getAccessiondate());
271 //        acquisition.setEntryDate("updated-" + acquisition.getEntryDate());
272     
273         // Submit the request to the service and store the response.
274         res = client.update(knownResourceId, acquisition);
275         int statusCode = res.getStatus();
276         Acquisition updatedObject = res.getEntity();
277
278         // Check the status code of the response: does it match
279         // the expected response(s)?
280         verbose("update: status = " + res.getStatus());
281         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
282             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
283         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
284         
285         // Check the contents of the response: does it match
286         // what was submitted?
287         verbose("update: ", updatedObject, Acquisition.class);
288         Assert.assertEquals(updatedObject.getAccessiondate(), 
289             acquisition.getAccessiondate(), 
290             "Data in updated object did not match submitted data.");
291     }
292
293     // Failure outcomes
294
295     // Placeholders until the three tests below can be uncommented.
296     // See Issue CSPACE-401.
297     public void updateWithEmptyEntityBody() {}
298     public void updateWithMalformedXml() {}
299     public void updateWithWrongXmlSchema() {}
300
301 /*
302     @Override
303     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
304     public void updateWithEmptyEntityBody() {
305     
306         // Perform setup.
307         setupUpdateWithEmptyEntityBody();
308
309         // Submit the request to the service and store the response.
310         String method = REQUEST_TYPE.httpMethodName();
311         String url = getResourceURL(knownResourceId);
312         String mediaType = MediaType.APPLICATION_XML;
313         final String entity = "";
314         int statusCode = submitRequest(method, url, mediaType, entity);
315         
316         // Check the status code of the response: does it match
317         // the expected response(s)?
318         verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
319         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
320             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
321         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
322     }
323
324     @Override
325     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
326     public void createWithEmptyEntityBody() {
327     
328         // Perform setup.
329         setupCreateWithEmptyEntityBody();
330
331         // Submit the request to the service and store the response.
332         String method = REQUEST_TYPE.httpMethodName();
333         String url = getServiceRootURL();
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("createWithEmptyEntityBody 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", "update", "testSubmitRequest"})
348     public void updateWithMalformedXml() {
349
350         // Perform setup.
351         setupUpdateWithMalformedXml();
352
353         // Submit the request to the service and store the response.
354         String method = REQUEST_TYPE.httpMethodName();
355         String url = getResourceURL(knownResourceId);
356         final String entity = MALFORMED_XML_DATA;
357         int statusCode = submitRequest(method, url, entity);
358         
359         // Check the status code of the response: does it match
360         // the expected response(s)?
361         verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
362         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
363             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
364         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
365     }
366
367     @Override
368     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
369     public void updateWithWrongXmlSchema() {
370     
371         // Perform setup.
372         setupUpdateWithWrongXmlSchema();
373         
374         // Submit the request to the service and store the response.
375         String method = REQUEST_TYPE.httpMethodName();
376         String url = getResourceURL(knownResourceId);
377         final String entity = WRONG_XML_SCHEMA_DATA;
378         int statusCode = submitRequest(method, url, entity);
379         
380         // Check the status code of the response: does it match
381         // the expected response(s)?
382         verbose("updateWithWrongSchema: 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
389     @Override
390     @Test(dependsOnMethods = {"update", "testSubmitRequest"})
391     public void updateNonExistent() {
392
393         // Perform setup.
394         setupUpdateNonExistent();
395
396         // Submit the request to the service and store the response.
397         // Note: The ID used in this 'create' call may be arbitrary.
398         // The only relevant ID may be the one used in update(), below.
399         Acquisition acquisition = createAcquisitionInstance(NON_EXISTENT_ID);
400         ClientResponse<Acquisition> res =
401           client.update(NON_EXISTENT_ID, acquisition);
402         int statusCode = res.getStatus();
403
404         // Check the status code of the response: does it match
405         // the expected response(s)?
406         verbose("updateNonExistent: status = " + res.getStatus());
407         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
408             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
409         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
410     }
411
412     // ---------------------------------------------------------------
413     // CRUD tests : DELETE tests
414     // ---------------------------------------------------------------
415
416     // Success outcomes
417
418     @Override
419     @Test(dependsOnMethods = 
420         {"create", "read", "update"})
421     public void delete() {
422
423         // Perform setup.
424         setupDelete();
425
426         // Submit the request to the service and store the response.
427         ClientResponse<Response> res = client.delete(knownResourceId);
428         int statusCode = res.getStatus();
429
430         // Check the status code of the response: does it match
431         // the expected response(s)?
432         verbose("delete: status = " + res.getStatus());
433         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
434             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
435         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
436     }
437
438     // Failure outcomes
439
440     @Override
441     @Test(dependsOnMethods = {"delete"})
442     public void deleteNonExistent() {
443
444         // Perform setup.
445         setupDeleteNonExistent();
446
447         // Submit the request to the service and store the response.
448         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
449         int statusCode = res.getStatus();
450
451         // Check the status code of the response: does it match
452         // the expected response(s)?
453         verbose("deleteNonExistent: 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
460     // ---------------------------------------------------------------
461     // Utility tests : tests of code used in tests above
462     // ---------------------------------------------------------------
463
464     /**
465      * Tests the code for manually submitting data that is used by several
466      * of the methods above.
467      */
468     @Test(dependsOnMethods = {"create", "read"})
469     public void testSubmitRequest() {
470
471         // Expected status code: 200 OK
472         final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
473
474         // Submit the request to the service and store the response.
475         String method = ServiceRequestType.READ.httpMethodName();
476         String url = getResourceURL(knownResourceId);
477         int statusCode = submitRequest(method, url);
478         
479         // Check the status code of the response: does it match
480         // the expected response(s)?
481         verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
482         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
483
484     }           
485
486     // ---------------------------------------------------------------
487     // Utility methods used by tests above
488     // ---------------------------------------------------------------
489
490     @Override
491     public String getServicePathComponent() {
492         return SERVICE_PATH_COMPONENT;
493     }
494     
495     private Acquisition createAcquisitionInstance(String identifier) {
496         Acquisition acquisition =
497             createAcquisitionInstance(
498                 "entryNumber-" + identifier,
499                 "entryDate-" + identifier);
500         return acquisition;
501     }
502     
503     private Acquisition createAcquisitionInstance(String entryNumber, String entryDate) {
504         Acquisition acquisition = new Acquisition();
505 //        acquisition.setEntryNumber(entryNumber);
506         acquisition.setAccessiondate(entryDate);
507         return acquisition;
508     }
509
510 }