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