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