]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
c686465a16d37c8d986eda3fd08e56b34896e145
[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.IntakeClient;
31 import org.collectionspace.services.client.test.ServiceRequestType;
32 import org.collectionspace.services.intake.Intake;
33 import org.collectionspace.services.intake.IntakeList;
34
35 import org.jboss.resteasy.client.ClientResponse;
36
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
39
40 /**
41  * IntakeServiceTest, carries out tests against a
42  * deployed and running Intake Service.
43  * 
44  * $LastChangedRevision: 511 $
45  * $LastChangedDate: 2009-08-06 20:16:16 +0000 (Thu, 06 Aug 2009) $
46  */
47 public class IntakeServiceTest extends AbstractServiceTest {
48
49     // Instance variables specific to this test.
50     private IntakeClient client = new IntakeClient();
51     final String SERVICE_PATH_COMPONENT = "intakes";
52     private String knownObjectId = 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         Intake intake = createIntake(identifier);
73         ClientResponse<Response> res = client.create(intake);
74         int statusCode = res.getStatus();
75
76         // Check the status code of the response: does it match the expected response(s)?
77         //
78         // Does it fall within the set of valid status codes?
79         // Does it exactly match the expected status code?
80         verbose("create: status = " + statusCode);
81         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
82             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
83         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
84
85         // Store the ID returned from this create operation for additional tests below.
86         knownObjectId = extractId(res);
87     }
88
89     @Override
90     @Test(dependsOnMethods = {"create"})
91     public void createList() {
92         for(int i = 0; i < 3; i++){
93             create();
94         }
95     }
96
97     // Failure outcomes
98
99     @Override
100     @Test(dependsOnMethods = {"create"}, expectedExceptions = IllegalArgumentException.class)
101     public void createNull() {
102         ClientResponse<Response> res = client.create(null);
103     }
104     
105     // Placeholders until the two tests below can be uncommented.  See Issue CSPACE-401.
106     public void createWithMalformedXml() {}
107     public void createWithWrongXmlSchema() {}
108
109 /*
110     @Override
111     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
112     public void createWithMalformedXml() {
113     
114         // Perform setup.
115         setupCreateWithMalformedXml();
116
117         // Submit the request to the service and store the response.
118         String method = REQUEST_TYPE.httpMethodName();
119         String url = getServiceRootURL();
120         final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
121         int statusCode = submitRequest(method, url, entity);
122         
123         // Check the status code of the response: does it match 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 the expected response(s)?
144         verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
145         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
146             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
147         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
148     }
149 */
150
151     // ---------------------------------------------------------------
152     // CRUD tests : READ tests
153     // ---------------------------------------------------------------
154
155     // Success outcomes
156
157     @Override
158     @Test(dependsOnMethods = {"create"})
159     public void read() {
160     
161         // Perform setup.
162         setupRead();
163
164         // Submit the request to the service and store the response.
165         ClientResponse<Intake> res = client.read(knownObjectId);
166         int statusCode = res.getStatus();
167             
168         // Check the status code of the response: does it match the expected response(s)?
169         verbose("read: status = " + statusCode);
170         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
171             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
172         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
173     }
174
175     @Override
176     @Test(dependsOnMethods = {"read"})
177     public void readNonExistent() {
178
179         // Perform setup.
180         setupReadNonExistent();
181         
182         // Submit the request to the service and store the response.
183         ClientResponse<Intake> res = client.read(NON_EXISTENT_ID);
184         int statusCode = res.getStatus();
185
186         // Check the status code of the response: does it match the expected response(s)?
187         verbose("readNonExistent: status = " + res.getStatus());
188         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
189             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
190         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
191     }
192
193
194     // ---------------------------------------------------------------
195     // CRUD tests : READ_LIST tests
196     // ---------------------------------------------------------------
197
198     // Success outcomes
199
200     @Override
201     @Test(dependsOnMethods = {"createList"})
202     public void readList() {
203     
204         // Perform setup.
205         setupReadList();
206
207         // Submit the request to the service and store the response.
208         ClientResponse<IntakeList> res = client.readList();
209         IntakeList list = res.getEntity();
210         int statusCode = res.getStatus();
211
212         // Check the status code of the response: does it match the expected response(s)?
213         verbose("readList: status = " + res.getStatus());
214         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
215             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
216         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
217
218         // Optionally output additional data about list members for debugging.
219         boolean iterateThroughList = false;
220         if (iterateThroughList && logger.isDebugEnabled()) {
221             List<IntakeList.IntakeListItem> items =
222                 list.getIntakeListItem();
223             int i = 0;
224             for(IntakeList.IntakeListItem item : items){
225                 verbose("readList: list-item[" + i + "] csid=" + item.getCsid());
226                 verbose("readList: list-item[" + i + "] objectNumber=" + item.getEntryNumber());
227                 verbose("readList: list-item[" + i + "] URI=" + item.getUri());
228                 i++;
229             }
230         }
231         
232     }
233
234     // Failure outcomes
235     
236     // None at present.
237
238
239     // ---------------------------------------------------------------
240     // CRUD tests : UPDATE tests
241     // ---------------------------------------------------------------
242
243     // Success outcomes
244
245     @Override
246     @Test(dependsOnMethods = {"create"})
247     public void update() {
248     
249         // Perform setup.
250         setupUpdate();
251
252         // Retrieve an existing resource that we can update.
253         ClientResponse<Intake> res = client.read(knownObjectId);
254         verbose("read: status = " + res.getStatus());
255         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
256         Intake intake = res.getEntity();
257         verbose("Got object to update with ID: " + knownObjectId,
258                 intake, Intake.class);
259
260         // Update the content of this resource.
261         intake.setEntryNumber("updated-" + intake.getEntryNumber());
262         intake.setEntryDate("updated-" + intake.getEntryDate());
263     
264         // Submit the request to the service and store the response.
265         res = client.update(knownObjectId, intake);
266         int statusCode = res.getStatus();
267         Intake updatedObject = res.getEntity();
268
269         // Check the status code of the response: does it match the expected response(s)?
270         verbose("update: status = " + res.getStatus());
271         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
272             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
273         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
274         
275         // Check the contents of the response: does it match what was submitted?
276         verbose("update: ", updatedObject, Intake.class);
277         Assert.assertEquals(updatedObject.getEntryDate(), 
278             intake.getEntryDate(), 
279             "Data in updated object did not match submitted data.");
280     }
281
282     // Placeholders until the two tests below can be uncommented.  See Issue CSPACE-401.
283     public void updateWithMalformedXml() {}
284     public void updateWithWrongXmlSchema() {}
285
286 /*
287     @Override
288     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
289     public void updateWithMalformedXml() {
290
291         // Perform setup.
292         setupUpdateWithMalformedXml();
293
294         // Submit the request to the service and store the response.
295         String method = REQUEST_TYPE.httpMethodName();
296         String url = getResourceURL(knownObjectId);
297         final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
298         int statusCode = submitRequest(method, url, entity);
299         
300         // Check the status code of the response: does it match the expected response(s)?
301         verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
302         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
303             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
304         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
305     }
306
307     @Override
308     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
309     public void updateWithWrongXmlSchema() {
310     
311         // Perform setup.
312         setupUpdateWithWrongXmlSchema();
313         
314         // Submit the request to the service and store the response.
315         String method = REQUEST_TYPE.httpMethodName();
316         String url = getResourceURL(knownObjectId);
317         final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class.
318         int statusCode = submitRequest(method, url, entity);
319         
320         // Check the status code of the response: does it match the expected response(s)?
321         verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
322         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
323             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
324         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
325     }
326 */
327
328     @Override
329     @Test(dependsOnMethods = {"update", "testSubmitRequest"})
330     public void updateNonExistent() {
331
332         // Perform setup.
333         setupUpdateNonExistent();
334
335         // Submit the request to the service and store the response.
336         // Note: The ID used in this 'create' call may be arbitrary.
337         // The only relevant ID may be the one used in update(), below.
338         Intake intake = createIntake(NON_EXISTENT_ID);
339         ClientResponse<Intake> res =
340           client.update(NON_EXISTENT_ID, intake);
341         int statusCode = res.getStatus();
342
343         // Check the status code of the response: does it match the expected response(s)?
344         verbose("updateNonExistent: status = " + res.getStatus());
345         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
346             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
347         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
348     }
349
350     // ---------------------------------------------------------------
351     // CRUD tests : DELETE tests
352     // ---------------------------------------------------------------
353
354     // Success outcomes
355
356     @Override
357     @Test(dependsOnMethods = 
358         {"create", "read", "update"})
359     public void delete() {
360
361         // Perform setup.
362         setupDelete();
363
364         // Submit the request to the service and store the response.
365         ClientResponse<Response> res = client.delete(knownObjectId);
366         int statusCode = res.getStatus();
367
368         // Check the status code of the response: does it match the expected response(s)?
369         verbose("delete: status = " + res.getStatus());
370         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
371             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
372         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
373     }
374
375     // Failure outcomes
376
377     @Override
378     @Test(dependsOnMethods = {"delete"})
379     public void deleteNonExistent() {
380
381         // Perform setup.
382         setupDeleteNonExistent();
383
384         // Submit the request to the service and store the response.
385         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
386         int statusCode = res.getStatus();
387
388         // Check the status code of the response: does it match the expected response(s)?
389         verbose("deleteNonExistent: status = " + res.getStatus());
390         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
391             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
392         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
393     }
394
395
396     // ---------------------------------------------------------------
397     // Utility tests : tests of code used in tests above
398     // ---------------------------------------------------------------
399
400     /**
401      * Tests the code for manually submitting data that is used by several
402      * of the methods above.
403      */
404     @Test(dependsOnMethods = {"create", "read"})
405     public void testSubmitRequest() {
406
407         // Expected status code: 200 OK
408         final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
409
410         // Submit the request to the service and store the response.
411         String method = ServiceRequestType.READ.httpMethodName();
412         String url = getResourceURL(knownObjectId);
413         int statusCode = submitRequest(method, url);
414         
415         // Check the status code of the response: does it match the expected response(s)?
416         verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
417         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
418
419     }           
420
421     // ---------------------------------------------------------------
422     // Utility methods used by tests above
423     // ---------------------------------------------------------------
424
425     @Override
426     public String getServicePathComponent() {
427         // @TODO Determine if it is possible to obtain this
428         // value programmatically.
429         //
430         // We set this in an annotation in the CollectionObjectProxy
431         // interface, for instance.  We also set service-specific
432         // constants in each service module, which might also
433         // return this value.
434         return SERVICE_PATH_COMPONENT;
435     }
436     
437     private Intake createIntake(String identifier) {
438         Intake intake =
439             createIntake(
440                 "entryNumber-" + identifier,
441                 "entryDate-" + identifier);
442         return intake;
443     }
444     
445     private Intake createIntake(String entryNumber, String entryDate) {
446         Intake intake = new Intake();
447         intake.setEntryNumber(entryNumber);
448         intake.setEntryDate(entryDate);
449         return intake;
450     }
451
452 }