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.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;
39 import org.jboss.resteasy.client.ClientResponse;
40 import org.testng.Assert;
41 //import org.testng.annotations.AfterClass;
42 import org.testng.annotations.Test;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
48 * FIXME: http://issues.collectionspace.org/browse/CSPACE-1685
49 * ReportServiceTest, carries out tests against a
50 * deployed and running Report Service.
52 * $LastChangedRevision: 2261 $
53 * $LastChangedDate: 2010-05-28 16:52:22 -0700 (Fri, 28 May 2010) $
55 public class ReportServiceTest extends AbstractServiceTestImpl {
58 private final String CLASS_NAME = ReportServiceTest.class.getName();
59 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
60 final String SERVICE_NAME = "reports";
61 final String SERVICE_PATH_COMPONENT = "reports";
62 // Instance variables specific to this test.
63 /** The known resource id. */
64 private String knownResourceId = null;
67 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
70 protected CollectionSpaceClient getClientInstance() {
71 return new ReportClient();
75 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
78 protected AbstractCommonList getAbstractCommonList(
79 ClientResponse<AbstractCommonList> response) {
80 return response.getEntity(ReportsCommonList.class);
84 // protected PoxPayloadOut createInstance(String identifier) {
85 // PoxPayloadOut multipart = createReportInstance(identifier);
89 // ---------------------------------------------------------------
90 // CRUD tests : CREATE tests
91 // ---------------------------------------------------------------
94 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
97 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
98 public void create(String testName) throws Exception {
100 if (logger.isDebugEnabled()) {
101 logger.debug(testBanner(testName, CLASS_NAME));
103 // Perform setup, such as initializing the type of service request
104 // (e.g. CREATE, DELETE), its valid and expected status codes, and
105 // its associated HTTP method name (e.g. POST, DELETE).
108 // Submit the request to the service and store the response.
109 ReportClient client = new ReportClient();
110 String identifier = createIdentifier();
111 PoxPayloadOut multipart = createReportInstance(identifier);
112 ClientResponse<Response> res = client.create(multipart);
114 // Check the status code of the response: does it match
115 // the expected response(s)?
118 // Does it fall within the set of valid status codes?
119 // Does it exactly match the expected status code?
120 int statusCode = res.getStatus();
121 if (logger.isDebugEnabled()) {
122 logger.debug(testName + ": status = " + statusCode);
124 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
125 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
126 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
128 // Store the ID returned from the first resource created
129 // for additional tests below.
130 if (knownResourceId == null) {
131 knownResourceId = extractId(res);
132 if (logger.isDebugEnabled()) {
133 logger.debug(testName + ": knownResourceId=" + knownResourceId);
137 // Store the IDs from every resource created by tests,
138 // so they can be deleted after tests have been run.
139 allResourceIdsCreated.add(extractId(res));
143 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
146 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
147 dependsOnMethods = {"create"})
148 public void createList(String testName) throws Exception {
149 for (int i = 0; i < 3; i++) {
155 // Placeholders until the three tests below can be uncommented.
156 // See Issue CSPACE-401.
158 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
161 public void createWithEmptyEntityBody(String testName) throws Exception {
162 //Should this really be empty?
166 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
169 public void createWithMalformedXml(String testName) throws Exception {
170 //Should this really be empty?
174 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
177 public void createWithWrongXmlSchema(String testName) throws Exception {
178 //Should this really be empty?
181 // ---------------------------------------------------------------
182 // CRUD tests : READ tests
183 // ---------------------------------------------------------------
186 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
189 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
190 dependsOnMethods = {"create"})
191 public void read(String testName) throws Exception {
193 if (logger.isDebugEnabled()) {
194 logger.debug(testBanner(testName, CLASS_NAME));
199 // Submit the request to the service and store the response.
200 ReportClient client = new ReportClient();
201 ClientResponse<String> res = client.read(knownResourceId);
203 // Check the status code of the response: does it match
204 // the expected response(s)?
205 int statusCode = res.getStatus();
206 if (logger.isDebugEnabled()) {
207 logger.debug(testName + ": status = " + statusCode);
209 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
210 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
211 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
213 // Get the common part of the response and verify that it is not null.
214 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
215 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
216 ReportsCommon reportCommon = null;
217 if (payloadInputPart != null) {
218 reportCommon = (ReportsCommon) payloadInputPart.getBody();
220 Assert.assertNotNull(reportCommon);
222 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
223 if (logger.isDebugEnabled()) {
224 logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
225 + "UTF-8 data received=" + reportCommon.getNotes());
227 Assert.assertEquals(reportCommon.getNotes(), getUTF8DataFragment(),
228 "UTF-8 data retrieved '" + reportCommon.getNotes()
229 + "' does not match expected data '" + getUTF8DataFragment());
234 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
237 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
238 dependsOnMethods = {"read"})
239 public void readNonExistent(String testName) throws Exception {
241 if (logger.isDebugEnabled()) {
242 logger.debug(testBanner(testName, CLASS_NAME));
245 setupReadNonExistent();
247 // Submit the request to the service and store the response.
248 ReportClient client = new ReportClient();
249 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
250 int statusCode = res.getStatus();
252 // Check the status code of the response: does it match
253 // the expected response(s)?
254 if (logger.isDebugEnabled()) {
255 logger.debug(testName + ": status = " + statusCode);
257 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
258 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
259 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
262 // ---------------------------------------------------------------
263 // CRUD tests : READ_LIST tests
264 // ---------------------------------------------------------------
267 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
270 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
271 dependsOnMethods = {"createList", "read"})
272 public void readList(String testName) throws Exception {
274 if (logger.isDebugEnabled()) {
275 logger.debug(testBanner(testName, CLASS_NAME));
280 // Submit the request to the service and store the response.
281 ReportClient client = new ReportClient();
282 ClientResponse<ReportsCommonList> res = client.readList();
283 ReportsCommonList list = res.getEntity();
284 int statusCode = res.getStatus();
286 // Check the status code of the response: does it match
287 // the expected response(s)?
288 if (logger.isDebugEnabled()) {
289 logger.debug(testName + ": status = " + statusCode);
291 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
292 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
293 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
295 // Optionally output additional data about list members for debugging.
296 boolean iterateThroughList = false;
297 if (iterateThroughList && logger.isDebugEnabled()) {
298 List<ReportsCommonList.ReportListItem> items =
299 list.getReportListItem();
301 for (ReportsCommonList.ReportListItem item : items) {
302 logger.debug(testName + ": list-item[" + i + "] csid="
304 logger.debug(testName + ": list-item[" + i + "] name="
306 logger.debug(testName + ": list-item[" + i + "] outputMIME="
307 + item.getOutputMIME());
308 logger.debug(testName + ": list-item[" + i + "] URI="
316 // ---------------------------------------------------------------
317 // CRUD tests : UPDATE tests
318 // ---------------------------------------------------------------
321 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
324 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
325 dependsOnMethods = {"read"})
326 public void update(String testName) throws Exception {
328 if (logger.isDebugEnabled()) {
329 logger.debug(testBanner(testName, CLASS_NAME));
334 // Retrieve the contents of a resource to update.
335 ReportClient client = new ReportClient();
336 ClientResponse<String> res = client.read(knownResourceId);
337 if (logger.isDebugEnabled()) {
338 logger.debug(testName + ": read status = " + res.getStatus());
340 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
341 if (logger.isDebugEnabled()) {
342 logger.debug("got object to update with ID: " + knownResourceId);
345 // Extract the common part from the response.
346 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
347 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
348 ReportsCommon reportCommon = null;
349 if (payloadInputPart != null) {
350 reportCommon = (ReportsCommon) payloadInputPart.getBody();
352 Assert.assertNotNull(reportCommon);
354 // Update its content.
355 reportCommon.setName("updated-" + reportCommon.getName());
356 reportCommon.setOutputMIME("updated-" + reportCommon.getOutputMIME());
357 if (logger.isDebugEnabled()) {
358 logger.debug("to be updated object");
359 logger.debug(objectAsXmlString(reportCommon, ReportsCommon.class));
361 reportCommon.setNotes("updated-" + reportCommon.getNotes());
363 // Submit the updated common part in an update request to the service
364 // and store the response.
365 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
366 PayloadOutputPart commonPart = output.addPart(reportCommon, MediaType.APPLICATION_XML_TYPE);
367 commonPart.setLabel(client.getCommonPartName());
368 res = client.update(knownResourceId, output);
370 // Check the status code of the response: does it match the expected response(s)?
371 int statusCode = res.getStatus();
372 if (logger.isDebugEnabled()) {
373 logger.debug(testName + ": status = " + statusCode);
375 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
376 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
377 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
379 // Extract the updated common part from the response.
380 input = new PoxPayloadIn(res.getEntity());
381 payloadInputPart = input.getPart(client.getCommonPartName());
382 ReportsCommon updatedReportCommon = null;
383 if (payloadInputPart != null) {
384 updatedReportCommon = (ReportsCommon) payloadInputPart.getBody();
386 Assert.assertNotNull(updatedReportCommon);
387 if (logger.isDebugEnabled()) {
388 logger.debug("updated object");
389 logger.debug(objectAsXmlString(updatedReportCommon, ReportsCommon.class));
392 // Check selected fields in the updated common part.
393 Assert.assertEquals(updatedReportCommon.getName(),
394 reportCommon.getName(),
395 "Data in updated object did not match submitted data.");
397 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
398 if (logger.isDebugEnabled()) {
399 logger.debug("UTF-8 data sent=" + reportCommon.getNotes() + "\n"
400 + "UTF-8 data received=" + updatedReportCommon.getNotes());
402 Assert.assertTrue(updatedReportCommon.getNotes().contains(getUTF8DataFragment()),
403 "UTF-8 data retrieved '" + updatedReportCommon.getNotes()
404 + "' does not contain expected data '" + getUTF8DataFragment());
405 Assert.assertEquals(updatedReportCommon.getNotes(),
406 reportCommon.getNotes(),
407 "Data in updated object did not match submitted data.");
411 // Placeholders until the three tests below can be uncommented.
412 // See Issue CSPACE-401.
414 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
417 public void updateWithEmptyEntityBody(String testName) throws Exception {
418 //Should this really be empty?
422 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
425 public void updateWithMalformedXml(String testName) throws Exception {
426 //Should this really be empty?
430 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
433 public void updateWithWrongXmlSchema(String testName) throws Exception {
434 //Should this really be empty?
438 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
441 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
442 dependsOnMethods = {"update", "testSubmitRequest"})
443 public void updateNonExistent(String testName) throws Exception {
445 if (logger.isDebugEnabled()) {
446 logger.debug(testBanner(testName, CLASS_NAME));
449 setupUpdateNonExistent();
451 // Submit the request to the service and store the response.
452 // Note: The ID used in this 'create' call may be arbitrary.
453 // The only relevant ID may be the one used in update(), below.
454 ReportClient client = new ReportClient();
455 PoxPayloadOut multipart = createReportInstance(NON_EXISTENT_ID);
456 ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
457 int statusCode = res.getStatus();
459 // Check the status code of the response: does it match
460 // the expected response(s)?
461 if (logger.isDebugEnabled()) {
462 logger.debug(testName + ": status = " + statusCode);
464 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
465 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
466 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
469 // ---------------------------------------------------------------
470 // CRUD tests : DELETE tests
471 // ---------------------------------------------------------------
474 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
477 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
478 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update", "readWorkflow"})
479 public void delete(String testName) throws Exception {
481 if (logger.isDebugEnabled()) {
482 logger.debug(testBanner(testName, CLASS_NAME));
487 // Submit the request to the service and store the response.
488 ReportClient client = new ReportClient();
489 ClientResponse<Response> res = client.delete(knownResourceId);
490 int statusCode = res.getStatus();
492 // Check the status code of the response: does it match
493 // the expected response(s)?
494 if (logger.isDebugEnabled()) {
495 logger.debug(testName + ": status = " + statusCode);
497 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
498 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
499 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
504 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
507 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
508 dependsOnMethods = {"delete"})
509 public void deleteNonExistent(String testName) throws Exception {
511 if (logger.isDebugEnabled()) {
512 logger.debug(testBanner(testName, CLASS_NAME));
515 setupDeleteNonExistent();
517 // Submit the request to the service and store the response.
518 ReportClient client = new ReportClient();
519 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
520 int statusCode = res.getStatus();
522 // Check the status code of the response: does it match
523 // the expected response(s)?
524 if (logger.isDebugEnabled()) {
525 logger.debug(testName + ": status = " + statusCode);
527 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
528 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
529 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
532 // ---------------------------------------------------------------
533 // Utility tests : tests of code used in tests above
534 // ---------------------------------------------------------------
536 * Tests the code for manually submitting data that is used by several
537 * of the methods above.
539 @Test(dependsOnMethods = {"create", "read"})
540 public void testSubmitRequest() {
542 // Expected status code: 200 OK
543 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
545 // Submit the request to the service and store the response.
546 String method = ServiceRequestType.READ.httpMethodName();
547 String url = getResourceURL(knownResourceId);
548 int statusCode = submitRequest(method, url);
550 // Check the status code of the response: does it match
551 // the expected response(s)?
552 if (logger.isDebugEnabled()) {
553 logger.debug("testSubmitRequest: url=" + url
554 + " status=" + statusCode);
556 Assert.assertEquals(statusCode, EXPECTED_STATUS);
560 // ---------------------------------------------------------------
561 // Utility methods used by tests above
562 // ---------------------------------------------------------------
564 protected String getServiceName() {
569 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
572 public String getServicePathComponent() {
573 return SERVICE_PATH_COMPONENT;
577 * Creates the report instance.
579 * @param identifier the identifier
580 * @return the multipart output
582 private PoxPayloadOut createReportInstance(String identifier) {
583 return createReportInstance(
584 "name-" + identifier,
585 "persAuthTermCountsTest.jasper",
590 * Creates the report instance.
592 * @param name the report name
593 * @param filename the relative path to the report
594 * @param outputMIME the MIME type we will return for this report
595 * @return the multipart output
597 private PoxPayloadOut createReportInstance(String name,
600 ReportsCommon reportCommon = new ReportsCommon();
601 reportCommon.setName(name);
602 reportCommon.setFilename(filename);
603 reportCommon.setOutputMIME(outputMIME);
604 reportCommon.setNotes(getUTF8DataFragment()); // For UTF-8 tests
606 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
607 PayloadOutputPart commonPart =
608 multipart.addPart(reportCommon, MediaType.APPLICATION_XML_TYPE);
609 commonPart.setLabel(new ReportClient().getCommonPartName());
611 if (logger.isDebugEnabled()) {
612 logger.debug("to be created, report common");
613 logger.debug(objectAsXmlString(reportCommon, ReportsCommon.class));
614 logger.debug(multipart.toXML());