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);
83 // ---------------------------------------------------------------
84 // CRUD tests : CREATE tests
85 // ---------------------------------------------------------------
88 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
91 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
92 public void create(String testName) throws Exception {
94 if (logger.isDebugEnabled()) {
95 logger.debug(testBanner(testName, CLASS_NAME));
97 // Perform setup, such as initializing the type of service request
98 // (e.g. CREATE, DELETE), its valid and expected status codes, and
99 // its associated HTTP method name (e.g. POST, DELETE).
102 // Submit the request to the service and store the response.
103 ReportClient client = new ReportClient();
104 String identifier = createIdentifier();
105 PoxPayloadOut multipart = createReportInstance(identifier);
106 ClientResponse<Response> res = client.create(multipart);
108 // Check the status code of the response: does it match
109 // the expected response(s)?
112 // Does it fall within the set of valid status codes?
113 // Does it exactly match the expected status code?
114 int statusCode = res.getStatus();
115 if (logger.isDebugEnabled()) {
116 logger.debug(testName + ": status = " + statusCode);
118 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
119 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
120 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
122 // Store the ID returned from the first resource created
123 // for additional tests below.
124 if (knownResourceId == null) {
125 knownResourceId = extractId(res);
126 if (logger.isDebugEnabled()) {
127 logger.debug(testName + ": knownResourceId=" + knownResourceId);
131 // Store the IDs from every resource created by tests,
132 // so they can be deleted after tests have been run.
133 allResourceIdsCreated.add(extractId(res));
137 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
140 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
141 dependsOnMethods = {"create"})
142 public void createList(String testName) throws Exception {
143 for (int i = 0; i < 3; i++) {
149 // Placeholders until the three tests below can be uncommented.
150 // See Issue CSPACE-401.
152 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
155 public void createWithEmptyEntityBody(String testName) throws Exception {
156 //Should this really be empty?
160 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
163 public void createWithMalformedXml(String testName) throws Exception {
164 //Should this really be empty?
168 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
171 public void createWithWrongXmlSchema(String testName) throws Exception {
172 //Should this really be empty?
175 // ---------------------------------------------------------------
176 // CRUD tests : READ tests
177 // ---------------------------------------------------------------
180 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
183 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
184 dependsOnMethods = {"create"})
185 public void read(String testName) throws Exception {
187 if (logger.isDebugEnabled()) {
188 logger.debug(testBanner(testName, CLASS_NAME));
193 // Submit the request to the service and store the response.
194 ReportClient client = new ReportClient();
195 ClientResponse<String> res = client.read(knownResourceId);
197 // Check the status code of the response: does it match
198 // the expected response(s)?
199 int statusCode = res.getStatus();
200 if (logger.isDebugEnabled()) {
201 logger.debug(testName + ": status = " + statusCode);
203 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
204 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
205 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
207 // Get the common part of the response and verify that it is not null.
208 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
209 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
210 ReportsCommon reportCommon = null;
211 if (payloadInputPart != null) {
212 reportCommon = (ReportsCommon) payloadInputPart.getBody();
214 Assert.assertNotNull(reportCommon);
216 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
217 if (logger.isDebugEnabled()) {
218 logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
219 + "UTF-8 data received=" + reportCommon.getNotes());
221 Assert.assertEquals(reportCommon.getNotes(), getUTF8DataFragment(),
222 "UTF-8 data retrieved '" + reportCommon.getNotes()
223 + "' does not match expected data '" + getUTF8DataFragment());
228 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
231 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
232 dependsOnMethods = {"read"})
233 public void readNonExistent(String testName) throws Exception {
235 if (logger.isDebugEnabled()) {
236 logger.debug(testBanner(testName, CLASS_NAME));
239 setupReadNonExistent();
241 // Submit the request to the service and store the response.
242 ReportClient client = new ReportClient();
243 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
244 int statusCode = res.getStatus();
246 // Check the status code of the response: does it match
247 // the expected response(s)?
248 if (logger.isDebugEnabled()) {
249 logger.debug(testName + ": status = " + statusCode);
251 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
252 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
253 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
256 // ---------------------------------------------------------------
257 // CRUD tests : READ_LIST tests
258 // ---------------------------------------------------------------
261 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
264 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
265 dependsOnMethods = {"createList", "read"})
266 public void readList(String testName) throws Exception {
268 if (logger.isDebugEnabled()) {
269 logger.debug(testBanner(testName, CLASS_NAME));
274 // Submit the request to the service and store the response.
275 ReportClient client = new ReportClient();
276 ClientResponse<ReportsCommonList> res = client.readList();
277 ReportsCommonList list = res.getEntity();
278 int statusCode = res.getStatus();
280 // Check the status code of the response: does it match
281 // the expected response(s)?
282 if (logger.isDebugEnabled()) {
283 logger.debug(testName + ": status = " + statusCode);
285 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
286 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
287 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
289 // Optionally output additional data about list members for debugging.
290 boolean iterateThroughList = false;
291 if (iterateThroughList && logger.isDebugEnabled()) {
292 List<ReportsCommonList.ReportListItem> items =
293 list.getReportListItem();
295 for (ReportsCommonList.ReportListItem item : items) {
296 logger.debug(testName + ": list-item[" + i + "] csid="
298 logger.debug(testName + ": list-item[" + i + "] name="
300 logger.debug(testName + ": list-item[" + i + "] outputMIME="
301 + item.getOutputMIME());
302 logger.debug(testName + ": list-item[" + i + "] URI="
310 // ---------------------------------------------------------------
311 // CRUD tests : UPDATE tests
312 // ---------------------------------------------------------------
315 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
318 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
319 dependsOnMethods = {"read"})
320 public void update(String testName) throws Exception {
322 if (logger.isDebugEnabled()) {
323 logger.debug(testBanner(testName, CLASS_NAME));
328 // Retrieve the contents of a resource to update.
329 ReportClient client = new ReportClient();
330 ClientResponse<String> res = client.read(knownResourceId);
331 if (logger.isDebugEnabled()) {
332 logger.debug(testName + ": read status = " + res.getStatus());
334 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
335 if (logger.isDebugEnabled()) {
336 logger.debug("got object to update with ID: " + knownResourceId);
339 // Extract the common part from the response.
340 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
341 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
342 ReportsCommon reportCommon = null;
343 if (payloadInputPart != null) {
344 reportCommon = (ReportsCommon) payloadInputPart.getBody();
346 Assert.assertNotNull(reportCommon);
348 // Update its content.
349 reportCommon.setName("updated-" + reportCommon.getName());
350 reportCommon.setOutputMIME("updated-" + reportCommon.getOutputMIME());
351 if (logger.isDebugEnabled()) {
352 logger.debug("to be updated object");
353 logger.debug(objectAsXmlString(reportCommon, ReportsCommon.class));
355 reportCommon.setNotes("updated-" + reportCommon.getNotes());
357 // Submit the updated common part in an update request to the service
358 // and store the response.
359 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
360 PayloadOutputPart commonPart = output.addPart(reportCommon, MediaType.APPLICATION_XML_TYPE);
361 commonPart.setLabel(client.getCommonPartName());
362 res = client.update(knownResourceId, output);
364 // Check the status code of the response: does it match the expected response(s)?
365 int statusCode = res.getStatus();
366 if (logger.isDebugEnabled()) {
367 logger.debug(testName + ": status = " + statusCode);
369 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
370 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
371 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
373 // Extract the updated common part from the response.
374 input = new PoxPayloadIn(res.getEntity());
375 payloadInputPart = input.getPart(client.getCommonPartName());
376 ReportsCommon updatedReportCommon = null;
377 if (payloadInputPart != null) {
378 updatedReportCommon = (ReportsCommon) payloadInputPart.getBody();
380 Assert.assertNotNull(updatedReportCommon);
381 if (logger.isDebugEnabled()) {
382 logger.debug("updated object");
383 logger.debug(objectAsXmlString(updatedReportCommon, ReportsCommon.class));
386 // Check selected fields in the updated common part.
387 Assert.assertEquals(updatedReportCommon.getName(),
388 reportCommon.getName(),
389 "Data in updated object did not match submitted data.");
391 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
392 if (logger.isDebugEnabled()) {
393 logger.debug("UTF-8 data sent=" + reportCommon.getNotes() + "\n"
394 + "UTF-8 data received=" + updatedReportCommon.getNotes());
396 Assert.assertTrue(updatedReportCommon.getNotes().contains(getUTF8DataFragment()),
397 "UTF-8 data retrieved '" + updatedReportCommon.getNotes()
398 + "' does not contain expected data '" + getUTF8DataFragment());
399 Assert.assertEquals(updatedReportCommon.getNotes(),
400 reportCommon.getNotes(),
401 "Data in updated object did not match submitted data.");
405 // Placeholders until the three tests below can be uncommented.
406 // See Issue CSPACE-401.
408 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
411 public void updateWithEmptyEntityBody(String testName) throws Exception {
412 //Should this really be empty?
416 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
419 public void updateWithMalformedXml(String testName) throws Exception {
420 //Should this really be empty?
424 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
427 public void updateWithWrongXmlSchema(String testName) throws Exception {
428 //Should this really be empty?
432 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
435 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
436 dependsOnMethods = {"update", "testSubmitRequest"})
437 public void updateNonExistent(String testName) throws Exception {
439 if (logger.isDebugEnabled()) {
440 logger.debug(testBanner(testName, CLASS_NAME));
443 setupUpdateNonExistent();
445 // Submit the request to the service and store the response.
446 // Note: The ID used in this 'create' call may be arbitrary.
447 // The only relevant ID may be the one used in update(), below.
448 ReportClient client = new ReportClient();
449 PoxPayloadOut multipart = createReportInstance(NON_EXISTENT_ID);
450 ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
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);
463 // ---------------------------------------------------------------
464 // CRUD tests : DELETE tests
465 // ---------------------------------------------------------------
468 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
471 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
472 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
473 public void delete(String testName) throws Exception {
475 if (logger.isDebugEnabled()) {
476 logger.debug(testBanner(testName, CLASS_NAME));
481 // Submit the request to the service and store the response.
482 ReportClient client = new ReportClient();
483 ClientResponse<Response> res = client.delete(knownResourceId);
484 int statusCode = res.getStatus();
486 // Check the status code of the response: does it match
487 // the expected response(s)?
488 if (logger.isDebugEnabled()) {
489 logger.debug(testName + ": status = " + statusCode);
491 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
492 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
493 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
498 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
501 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
502 dependsOnMethods = {"delete"})
503 public void deleteNonExistent(String testName) throws Exception {
505 if (logger.isDebugEnabled()) {
506 logger.debug(testBanner(testName, CLASS_NAME));
509 setupDeleteNonExistent();
511 // Submit the request to the service and store the response.
512 ReportClient client = new ReportClient();
513 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
514 int statusCode = res.getStatus();
516 // Check the status code of the response: does it match
517 // the expected response(s)?
518 if (logger.isDebugEnabled()) {
519 logger.debug(testName + ": status = " + statusCode);
521 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
522 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
523 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
526 // ---------------------------------------------------------------
527 // Utility tests : tests of code used in tests above
528 // ---------------------------------------------------------------
530 * Tests the code for manually submitting data that is used by several
531 * of the methods above.
533 @Test(dependsOnMethods = {"create", "read"})
534 public void testSubmitRequest() {
536 // Expected status code: 200 OK
537 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
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);
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);
550 Assert.assertEquals(statusCode, EXPECTED_STATUS);
554 // ---------------------------------------------------------------
555 // Utility methods used by tests above
556 // ---------------------------------------------------------------
558 protected String getServiceName() {
563 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
566 public String getServicePathComponent() {
567 return SERVICE_PATH_COMPONENT;
571 * Creates the report instance.
573 * @param identifier the identifier
574 * @return the multipart output
576 private PoxPayloadOut createReportInstance(String identifier) {
577 return createReportInstance(
578 "name-" + identifier,
579 "persAuthTermCountsTest.jasper",
584 * Creates the report instance.
586 * @param name the report name
587 * @param filename the relative path to the report
588 * @param outputMIME the MIME type we will return for this report
589 * @return the multipart output
591 private PoxPayloadOut createReportInstance(String name,
594 ReportsCommon reportCommon = new ReportsCommon();
595 reportCommon.setName(name);
596 reportCommon.setFilename(filename);
597 reportCommon.setOutputMIME(outputMIME);
598 reportCommon.setNotes(getUTF8DataFragment()); // For UTF-8 tests
600 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
601 PayloadOutputPart commonPart =
602 multipart.addPart(reportCommon, MediaType.APPLICATION_XML_TYPE);
603 commonPart.setLabel(new ReportClient().getCommonPartName());
605 if (logger.isDebugEnabled()) {
606 logger.debug("to be created, report common");
607 logger.debug(objectAsXmlString(reportCommon, ReportsCommon.class));
608 logger.debug(multipart.toXML());