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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.client.test;
25 import java.util.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.collectionspace.services.client.CollectionSpaceClient;
30 import org.collectionspace.services.client.ReportClient;
31 import org.collectionspace.services.report.ReportsCommon;
32 import org.collectionspace.services.report.ReportsCommonList;
33 import org.collectionspace.services.jaxb.AbstractCommonList;
35 import org.jboss.resteasy.client.ClientResponse;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
38 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
39 import org.testng.Assert;
40 //import org.testng.annotations.AfterClass;
41 import org.testng.annotations.Test;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * FIXME: http://issues.collectionspace.org/browse/CSPACE-1685
48 * ReportServiceTest, carries out tests against a
49 * deployed and running Report Service.
51 * $LastChangedRevision: 2261 $
52 * $LastChangedDate: 2010-05-28 16:52:22 -0700 (Fri, 28 May 2010) $
54 public class ReportServiceTest extends AbstractServiceTestImpl {
57 private final String CLASS_NAME = ReportServiceTest.class.getName();
58 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
60 // Instance variables specific to this test.
61 /** The service path component. */
62 final String SERVICE_PATH_COMPONENT = "reports";
64 /** The known resource id. */
65 private String knownResourceId = null;
68 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
71 protected CollectionSpaceClient getClientInstance() {
72 return new ReportClient();
76 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
79 protected AbstractCommonList getAbstractCommonList(
80 ClientResponse<AbstractCommonList> response) {
81 return response.getEntity(ReportsCommonList.class);
84 // ---------------------------------------------------------------
85 // CRUD tests : CREATE tests
86 // ---------------------------------------------------------------
89 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
92 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
93 public void create(String testName) throws Exception {
95 if (logger.isDebugEnabled()) {
96 logger.debug(testBanner(testName, CLASS_NAME));
98 // Perform setup, such as initializing the type of service request
99 // (e.g. CREATE, DELETE), its valid and expected status codes, and
100 // its associated HTTP method name (e.g. POST, DELETE).
103 // Submit the request to the service and store the response.
104 ReportClient client = new ReportClient();
105 String identifier = createIdentifier();
106 MultipartOutput multipart = createReportInstance(identifier);
107 ClientResponse<Response> res = client.create(multipart);
109 int statusCode = res.getStatus();
111 // Check the status code of the response: does it match
112 // the expected response(s)?
115 // Does it fall within the set of valid status codes?
116 // Does it exactly match the expected status code?
117 if(logger.isDebugEnabled()){
118 logger.debug(testName + ": status = " + statusCode);
120 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
121 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
122 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
124 // Store the ID returned from the first resource created
125 // for additional tests below.
126 if (knownResourceId == null){
127 knownResourceId = extractId(res);
128 if (logger.isDebugEnabled()) {
129 logger.debug(testName + ": knownResourceId=" + knownResourceId);
133 // Store the IDs from every resource created by tests,
134 // so they can be deleted after tests have been run.
135 allResourceIdsCreated.add(extractId(res));
139 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
142 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
143 dependsOnMethods = {"create"})
144 public void createList(String testName) throws Exception {
145 for(int i = 0; i < 3; i++){
151 // Placeholders until the three tests below can be uncommented.
152 // See Issue CSPACE-401.
154 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
157 public void createWithEmptyEntityBody(String testName) throws Exception {
158 //Should this really be empty?
162 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
165 public void createWithMalformedXml(String testName) throws Exception {
166 //Should this really be empty?
170 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
173 public void createWithWrongXmlSchema(String testName) throws Exception {
174 //Should this really be empty?
177 // ---------------------------------------------------------------
178 // CRUD tests : READ tests
179 // ---------------------------------------------------------------
182 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
185 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
186 dependsOnMethods = {"create"})
187 public void read(String testName) throws Exception {
189 if (logger.isDebugEnabled()) {
190 logger.debug(testBanner(testName, CLASS_NAME));
195 // Submit the request to the service and store the response.
196 ReportClient client = new ReportClient();
197 ClientResponse<MultipartInput> res = client.read(knownResourceId);
198 int statusCode = res.getStatus();
200 // Check the status code of the response: does it match
201 // the expected response(s)?
202 if(logger.isDebugEnabled()){
203 logger.debug(testName + ": status = " + statusCode);
205 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
206 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
207 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
209 MultipartInput input = (MultipartInput) res.getEntity();
210 ReportsCommon report = (ReportsCommon) extractPart(input,
211 client.getCommonPartName(), ReportsCommon.class);
212 Assert.assertNotNull(report);
217 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
220 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
221 dependsOnMethods = {"read"})
222 public void readNonExistent(String testName) throws Exception {
224 if (logger.isDebugEnabled()) {
225 logger.debug(testBanner(testName, CLASS_NAME));
228 setupReadNonExistent();
230 // Submit the request to the service and store the response.
231 ReportClient client = new ReportClient();
232 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
233 int statusCode = res.getStatus();
235 // Check the status code of the response: does it match
236 // the expected response(s)?
237 if(logger.isDebugEnabled()){
238 logger.debug(testName + ": status = " + statusCode);
240 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
241 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
242 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
245 // ---------------------------------------------------------------
246 // CRUD tests : READ_LIST tests
247 // ---------------------------------------------------------------
250 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
253 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
254 dependsOnMethods = {"createList", "read"})
255 public void readList(String testName) throws Exception {
257 if (logger.isDebugEnabled()) {
258 logger.debug(testBanner(testName, CLASS_NAME));
263 // Submit the request to the service and store the response.
264 ReportClient client = new ReportClient();
265 ClientResponse<ReportsCommonList> res = client.readList();
266 ReportsCommonList list = res.getEntity();
267 int statusCode = res.getStatus();
269 // Check the status code of the response: does it match
270 // the expected response(s)?
271 if(logger.isDebugEnabled()){
272 logger.debug(testName + ": status = " + statusCode);
274 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
275 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
276 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
278 // Optionally output additional data about list members for debugging.
279 boolean iterateThroughList = false;
280 if(iterateThroughList && logger.isDebugEnabled()){
281 List<ReportsCommonList.ReportListItem> items =
282 list.getReportListItem();
284 for(ReportsCommonList.ReportListItem item : items){
285 logger.debug(testName + ": list-item[" + i + "] csid=" +
287 logger.debug(testName + ": list-item[" + i + "] name=" +
289 logger.debug(testName + ": list-item[" + i + "] outputMIME=" +
290 item.getOutputMIME());
291 logger.debug(testName + ": list-item[" + i + "] URI=" +
299 // ---------------------------------------------------------------
300 // CRUD tests : UPDATE tests
301 // ---------------------------------------------------------------
304 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
307 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
308 dependsOnMethods = {"read"})
309 public void update(String testName) throws Exception {
311 if (logger.isDebugEnabled()) {
312 logger.debug(testBanner(testName, CLASS_NAME));
317 // Retrieve the contents of a resource to update.
318 ReportClient client = new ReportClient();
319 ClientResponse<MultipartInput> res =
320 client.read(knownResourceId);
321 if(logger.isDebugEnabled()){
322 logger.debug(testName + ": read status = " + res.getStatus());
324 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
326 if(logger.isDebugEnabled()){
327 logger.debug("got object to update with ID: " + knownResourceId);
329 MultipartInput input = (MultipartInput) res.getEntity();
330 ReportsCommon report = (ReportsCommon) extractPart(input,
331 client.getCommonPartName(), ReportsCommon.class);
332 Assert.assertNotNull(report);
334 // Update the content of this resource.
335 // Update the content of this resource.
336 report.setName("updated-" + report.getName());
337 report.setOutputMIME("updated-" + report.getOutputMIME());
338 if(logger.isDebugEnabled()){
339 logger.debug("to be updated object");
340 logger.debug(objectAsXmlString(report, ReportsCommon.class));
342 // Submit the request to the service and store the response.
343 MultipartOutput output = new MultipartOutput();
344 OutputPart commonPart = output.addPart(report, MediaType.APPLICATION_XML_TYPE);
345 commonPart.getHeaders().add("label", client.getCommonPartName());
347 res = client.update(knownResourceId, output);
348 int statusCode = res.getStatus();
349 // Check the status code of the response: does it match the expected response(s)?
350 if(logger.isDebugEnabled()){
351 logger.debug(testName + ": status = " + statusCode);
353 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
354 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
355 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
358 input = (MultipartInput) res.getEntity();
359 ReportsCommon updatedReport =
360 (ReportsCommon) extractPart(input,
361 client.getCommonPartName(), ReportsCommon.class);
362 Assert.assertNotNull(updatedReport);
364 Assert.assertEquals(updatedReport.getName(),
366 "Data in updated object did not match submitted data.");
371 // Placeholders until the three tests below can be uncommented.
372 // See Issue CSPACE-401.
374 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
377 public void updateWithEmptyEntityBody(String testName) throws Exception {
378 //Should this really be empty?
382 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
385 public void updateWithMalformedXml(String testName) throws Exception {
386 //Should this really be empty?
390 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
393 public void updateWithWrongXmlSchema(String testName) throws Exception {
394 //Should this really be empty?
398 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
401 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
402 dependsOnMethods = {"update", "testSubmitRequest"})
403 public void updateNonExistent(String testName) throws Exception {
405 if (logger.isDebugEnabled()) {
406 logger.debug(testBanner(testName, CLASS_NAME));
409 setupUpdateNonExistent();
411 // Submit the request to the service and store the response.
412 // Note: The ID used in this 'create' call may be arbitrary.
413 // The only relevant ID may be the one used in update(), below.
414 ReportClient client = new ReportClient();
415 MultipartOutput multipart = createReportInstance(NON_EXISTENT_ID);
416 ClientResponse<MultipartInput> res =
417 client.update(NON_EXISTENT_ID, multipart);
418 int statusCode = res.getStatus();
420 // Check the status code of the response: does it match
421 // the expected response(s)?
422 if(logger.isDebugEnabled()){
423 logger.debug(testName + ": status = " + statusCode);
425 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
426 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
427 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
430 // ---------------------------------------------------------------
431 // CRUD tests : DELETE tests
432 // ---------------------------------------------------------------
435 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
438 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
439 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
440 public void delete(String testName) throws Exception {
442 if (logger.isDebugEnabled()) {
443 logger.debug(testBanner(testName, CLASS_NAME));
448 // Submit the request to the service and store the response.
449 ReportClient client = new ReportClient();
450 ClientResponse<Response> res = client.delete(knownResourceId);
451 int statusCode = res.getStatus();
453 // Check the status code of the response: does it match
454 // the expected response(s)?
455 if(logger.isDebugEnabled()){
456 logger.debug(testName + ": status = " + statusCode);
458 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
459 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
460 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
465 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
468 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
469 dependsOnMethods = {"delete"})
470 public void deleteNonExistent(String testName) throws Exception {
472 if (logger.isDebugEnabled()) {
473 logger.debug(testBanner(testName, CLASS_NAME));
476 setupDeleteNonExistent();
478 // Submit the request to the service and store the response.
479 ReportClient client = new ReportClient();
480 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
481 int statusCode = res.getStatus();
483 // Check the status code of the response: does it match
484 // the expected response(s)?
485 if(logger.isDebugEnabled()){
486 logger.debug(testName + ": status = " + statusCode);
488 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
489 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
490 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
493 // ---------------------------------------------------------------
494 // Utility tests : tests of code used in tests above
495 // ---------------------------------------------------------------
497 * Tests the code for manually submitting data that is used by several
498 * of the methods above.
500 @Test(dependsOnMethods = {"create", "read"})
501 public void testSubmitRequest() {
503 // Expected status code: 200 OK
504 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
506 // Submit the request to the service and store the response.
507 String method = ServiceRequestType.READ.httpMethodName();
508 String url = getResourceURL(knownResourceId);
509 int statusCode = submitRequest(method, url);
511 // Check the status code of the response: does it match
512 // the expected response(s)?
513 if(logger.isDebugEnabled()){
514 logger.debug("testSubmitRequest: url=" + url +
515 " status=" + statusCode);
517 Assert.assertEquals(statusCode, EXPECTED_STATUS);
521 // ---------------------------------------------------------------
522 // Utility methods used by tests above
523 // ---------------------------------------------------------------
525 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
528 public String getServicePathComponent() {
529 return SERVICE_PATH_COMPONENT;
533 * Creates the report instance.
535 * @param identifier the identifier
536 * @return the multipart output
538 private MultipartOutput createReportInstance(String identifier) {
539 return createReportInstance(
540 "name-" + identifier,
541 "persAuthTermCountsTest.jasper",
546 * Creates the report instance.
548 * @param name the report name
549 * @param filename the relative path to the report
550 * @param outputMIME the MIME type we will return for this report
551 * @return the multipart output
553 private MultipartOutput createReportInstance(String name,
556 ReportsCommon report = new ReportsCommon();
557 report.setName(name);
558 report.setFilename(filename);
559 report.setOutputMIME(outputMIME);
560 MultipartOutput multipart = new MultipartOutput();
561 OutputPart commonPart =
562 multipart.addPart(report, MediaType.APPLICATION_XML_TYPE);
563 commonPart.getHeaders().add("label", new ReportClient().getCommonPartName());
565 if(logger.isDebugEnabled()){
566 logger.debug("to be created, report common");
567 logger.debug(objectAsXmlString(report, ReportsCommon.class));