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