]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d0cf49e0bfa196a66834d751b1d765a99d3c9d03
[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.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
30
31 import org.collectionspace.services.client.IntakeClient;
32 import org.collectionspace.services.client.test.ServiceRequestType;
33 import org.collectionspace.services.intake.Intake;
34 import org.collectionspace.services.intake.IntakeList;
35
36 import org.jboss.resteasy.client.ClientResponse;
37
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
40
41 /**
42  * IntakeServiceTest, carries out tests against a
43  * deployed and running Intake Service.
44  * 
45  * $LastChangedRevision$
46  * $LastChangedDate$
47  */
48 public class IntakeServiceTest extends AbstractServiceTest {
49
50     // Instance variables specific to this test.
51     private IntakeClient client = new IntakeClient();
52     final String SERVICE_PATH_COMPONENT = "intakes";
53     private String knownResourceId = null;
54
55
56     // ---------------------------------------------------------------
57     // CRUD tests : CREATE tests
58     // ---------------------------------------------------------------
59
60     // Success outcomes
61     
62     @Override
63     @Test
64     public void create() {
65
66         // Perform setup, such as initializing the type of service request
67         // (e.g. CREATE, DELETE), its valid and expected status codes, and
68         // its associated HTTP method name (e.g. POST, DELETE).
69         setupCreate();
70
71         // Submit the request to the service and store the response.
72         String identifier = createIdentifier();
73         Intake intake = createIntakeInstance(identifier);
74         ClientResponse<Response> res = client.create(intake);
75         int statusCode = res.getStatus();
76
77         // Check the status code of the response: does it match
78         // the expected response(s)?
79         //
80         // Specifically:
81         // Does it fall within the set of valid status codes?
82         // Does it exactly match the expected status code?
83         verbose("create: status = " + statusCode);
84         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
85             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
86         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
87
88         // Store the ID returned from this create operation for
89         // additional tests below.
90         knownResourceId = extractId(res);
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
103     // Placeholders until the two tests below can be uncommented.
104     // See Issue CSPACE-401.
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         String mediaType = MediaType.APPLICATION_XML;
120         final String entity = MALFORMED_XML_DATA; // Constant from base class.
121         int statusCode = submitRequest(method, url, mediaType, entity);
122         
123         // Check the status code of the response: does it match
124         // the expected response(s)?
125         verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
126         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
127             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
128         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
129     }
130
131     @Override
132     @Test(dependsOnMethods = {"create", "testSubmitRequest"})
133     public void createWithWrongXmlSchema() {
134     
135         // Perform setup.
136         setupCreateWithWrongXmlSchema();
137      
138         // Submit the request to the service and store the response.
139         String method = REQUEST_TYPE.httpMethodName();
140         String url = getServiceRootURL();
141         String mediaType = MediaType.APPLICATION_XML;
142         final String entity = WRONG_XML_SCHEMA_DATA;
143         int statusCode = submitRequest(method, url, mediaType, entity);
144         
145         // Check the status code of the response: does it match
146         // the expected response(s)?
147         verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
148         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
149             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
150         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
151     }
152 */
153
154     // ---------------------------------------------------------------
155     // CRUD tests : READ tests
156     // ---------------------------------------------------------------
157
158     // Success outcomes
159
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<Intake> 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     }
178
179     // Failure outcomes
180
181     @Override
182     @Test(dependsOnMethods = {"read"})
183     public void readNonExistent() {
184
185         // Perform setup.
186         setupReadNonExistent();
187         
188         // Submit the request to the service and store the response.
189         ClientResponse<Intake> res = client.read(NON_EXISTENT_ID);
190         int statusCode = res.getStatus();
191
192         // Check the status code of the response: does it match
193         // the expected response(s)?
194         verbose("readNonExistent: status = " + res.getStatus());
195         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
198     }
199
200
201     // ---------------------------------------------------------------
202     // CRUD tests : READ_LIST tests
203     // ---------------------------------------------------------------
204
205     // Success outcomes
206
207     @Override
208     @Test(dependsOnMethods = {"createList"})
209     public void readList() {
210     
211         // Perform setup.
212         setupReadList();
213
214         // Submit the request to the service and store the response.
215         ClientResponse<IntakeList> res = client.readList();
216         IntakeList list = res.getEntity();
217         int statusCode = res.getStatus();
218
219         // Check the status code of the response: does it match
220         // the expected response(s)?
221         verbose("readList: status = " + res.getStatus());
222         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
223             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
224         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
225
226         // Optionally output additional data about list members for debugging.
227         boolean iterateThroughList = false;
228         if (iterateThroughList && logger.isDebugEnabled()) {
229             List<IntakeList.IntakeListItem> items =
230                 list.getIntakeListItem();
231             int i = 0;
232             for(IntakeList.IntakeListItem item : items){
233                 verbose("readList: list-item[" + i + "] csid=" +
234                     item.getCsid());
235                 verbose("readList: list-item[" + i + "] objectNumber=" +
236                     item.getEntryNumber());
237                 verbose("readList: list-item[" + i + "] URI=" +
238                     item.getUri());
239                 i++;
240             }
241         }
242         
243     }
244
245     // Failure outcomes
246     
247     // None at present.
248
249
250     // ---------------------------------------------------------------
251     // CRUD tests : UPDATE tests
252     // ---------------------------------------------------------------
253
254     // Success outcomes
255
256     @Override
257     @Test(dependsOnMethods = {"create"})
258     public void update() {
259     
260         // Perform setup.
261         setupUpdate();
262
263         // Retrieve an existing resource that we can update.
264         ClientResponse<Intake> res = client.read(knownResourceId);
265         verbose("read: status = " + res.getStatus());
266         Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
267         Intake intake = res.getEntity();
268         verbose("Got object to update with ID: " + knownResourceId,
269                 intake, Intake.class);
270
271         // Update the content of this resource.
272         intake.setEntryNumber("updated-" + intake.getEntryNumber());
273         intake.setEntryDate("updated-" + intake.getEntryDate());
274     
275         // Submit the request to the service and store the response.
276         res = client.update(knownResourceId, intake);
277         int statusCode = res.getStatus();
278         Intake updatedObject = res.getEntity();
279
280         // Check the status code of the response: does it match
281         // the expected response(s)?
282         verbose("update: status = " + res.getStatus());
283         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
284             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
285         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
286         
287         // Check the contents of the response: does it match
288         // what was submitted?
289         verbose("update: ", updatedObject, Intake.class);
290         Assert.assertEquals(updatedObject.getEntryDate(), 
291             intake.getEntryDate(), 
292             "Data in updated object did not match submitted data.");
293     }
294
295     // Failure outcomes
296
297     // Placeholders until the two tests below can be uncommented.
298     // See Issue CSPACE-401.
299     public void updateWithMalformedXml() {}
300     public void updateWithWrongXmlSchema() {}
301
302 /*
303     @Override
304     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
305     public void updateWithMalformedXml() {
306
307         // Perform setup.
308         setupUpdateWithMalformedXml();
309
310         // Submit the request to the service and store the response.
311         String method = REQUEST_TYPE.httpMethodName();
312         String url = getResourceURL(knownResourceId);
313         String mediaType = MediaType.APPLICATION_XML;
314         final String entity = MALFORMED_XML_DATA;
315         int statusCode = submitRequest(method, url, mediaType, entity);
316         
317         // Check the status code of the response: does it match
318         // the expected response(s)?
319         verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
320         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
321             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
322         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
323     }
324
325     @Override
326     @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
327     public void updateWithWrongXmlSchema() {
328     
329         // Perform setup.
330         setupUpdateWithWrongXmlSchema();
331         
332         // Submit the request to the service and store the response.
333         String method = REQUEST_TYPE.httpMethodName();
334         String url = getResourceURL(knownResourceId);
335         String mediaType = MediaType.APPLICATION_XML;
336         final String entity = WRONG_XML_SCHEMA_DATA;
337         int statusCode = submitRequest(method, url, mediaType, entity);
338         
339         // Check the status code of the response: does it match
340         // the expected response(s)?
341         verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
342         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
343             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
344         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
345     }
346 */
347
348     @Override
349     @Test(dependsOnMethods = {"update", "testSubmitRequest"})
350     public void updateNonExistent() {
351
352         // Perform setup.
353         setupUpdateNonExistent();
354
355         // Submit the request to the service and store the response.
356         // Note: The ID used in this 'create' call may be arbitrary.
357         // The only relevant ID may be the one used in update(), below.
358         Intake intake = createIntakeInstance(NON_EXISTENT_ID);
359         ClientResponse<Intake> res =
360           client.update(NON_EXISTENT_ID, intake);
361         int statusCode = res.getStatus();
362
363         // Check the status code of the response: does it match
364         // the expected response(s)?
365         verbose("updateNonExistent: status = " + res.getStatus());
366         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
367             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
368         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
369     }
370
371     // ---------------------------------------------------------------
372     // CRUD tests : DELETE tests
373     // ---------------------------------------------------------------
374
375     // Success outcomes
376
377     @Override
378     @Test(dependsOnMethods = 
379         {"create", "read", "update"})
380     public void delete() {
381
382         // Perform setup.
383         setupDelete();
384
385         // Submit the request to the service and store the response.
386         ClientResponse<Response> res = client.delete(knownResourceId);
387         int statusCode = res.getStatus();
388
389         // Check the status code of the response: does it match
390         // the expected response(s)?
391         verbose("delete: status = " + res.getStatus());
392         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
393             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
394         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
395     }
396
397     // Failure outcomes
398
399     @Override
400     @Test(dependsOnMethods = {"delete"})
401     public void deleteNonExistent() {
402
403         // Perform setup.
404         setupDeleteNonExistent();
405
406         // Submit the request to the service and store the response.
407         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
408         int statusCode = res.getStatus();
409
410         // Check the status code of the response: does it match
411         // the expected response(s)?
412         verbose("deleteNonExistent: status = " + res.getStatus());
413         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
414             invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
415         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
416     }
417
418
419     // ---------------------------------------------------------------
420     // Utility tests : tests of code used in tests above
421     // ---------------------------------------------------------------
422
423     /**
424      * Tests the code for manually submitting data that is used by several
425      * of the methods above.
426      */
427     @Test(dependsOnMethods = {"create", "read"})
428     public void testSubmitRequest() {
429
430         // Expected status code: 200 OK
431         final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
432
433         // Submit the request to the service and store the response.
434         String method = ServiceRequestType.READ.httpMethodName();
435         String url = getResourceURL(knownResourceId);
436         int statusCode = submitRequest(method, url);
437         
438         // Check the status code of the response: does it match
439         // the expected response(s)?
440         verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
441         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
442
443     }           
444
445     // ---------------------------------------------------------------
446     // Utility methods used by tests above
447     // ---------------------------------------------------------------
448
449     @Override
450     public String getServicePathComponent() {
451         return SERVICE_PATH_COMPONENT;
452     }
453     
454     private Intake createIntakeInstance(String identifier) {
455         Intake intake =
456             createIntakeInstance(
457                 "entryNumber-" + identifier,
458                 "entryDate-" + identifier);
459         return intake;
460     }
461     
462     private Intake createIntakeInstance(String entryNumber, String entryDate) {
463         Intake intake = new Intake();
464         intake.setEntryNumber(entryNumber);
465         intake.setEntryDate(entryDate);
466         return intake;
467     }
468
469 }