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);
61 final String SERVICE_NAME = "reports";
62 final String SERVICE_PATH_COMPONENT = "reports";
64 // Instance variables specific to this test.
67 /** The known resource id. */
68 private String knownResourceId = null;
71 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
74 protected CollectionSpaceClient getClientInstance() {
75 return new ReportClient();
79 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
82 protected AbstractCommonList getAbstractCommonList(
83 ClientResponse<AbstractCommonList> response) {
84 return response.getEntity(ReportsCommonList.class);
87 // ---------------------------------------------------------------
88 // CRUD tests : CREATE tests
89 // ---------------------------------------------------------------
92 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
95 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
96 public void create(String testName) throws Exception {
98 if (logger.isDebugEnabled()) {
99 logger.debug(testBanner(testName, CLASS_NAME));
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).
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);
112 // Check the status code of the response: does it match
113 // the expected response(s)?
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);
122 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
123 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
124 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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);
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));
141 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
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++){
153 // Placeholders until the three tests below can be uncommented.
154 // See Issue CSPACE-401.
156 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
159 public void createWithEmptyEntityBody(String testName) throws Exception {
160 //Should this really be empty?
164 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
167 public void createWithMalformedXml(String testName) throws Exception {
168 //Should this really be empty?
172 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
175 public void createWithWrongXmlSchema(String testName) throws Exception {
176 //Should this really be empty?
179 // ---------------------------------------------------------------
180 // CRUD tests : READ tests
181 // ---------------------------------------------------------------
184 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
187 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
188 dependsOnMethods = {"create"})
189 public void read(String testName) throws Exception {
191 if (logger.isDebugEnabled()) {
192 logger.debug(testBanner(testName, CLASS_NAME));
197 // Submit the request to the service and store the response.
198 ReportClient client = new ReportClient();
199 ClientResponse<String> res = client.read(knownResourceId);
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);
207 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
208 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
209 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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();
218 Assert.assertNotNull(reportCommon);
223 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
226 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
227 dependsOnMethods = {"read"})
228 public void readNonExistent(String testName) throws Exception {
230 if (logger.isDebugEnabled()) {
231 logger.debug(testBanner(testName, CLASS_NAME));
234 setupReadNonExistent();
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();
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);
246 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
247 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
248 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
251 // ---------------------------------------------------------------
252 // CRUD tests : READ_LIST tests
253 // ---------------------------------------------------------------
256 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
259 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
260 dependsOnMethods = {"createList", "read"})
261 public void readList(String testName) throws Exception {
263 if (logger.isDebugEnabled()) {
264 logger.debug(testBanner(testName, CLASS_NAME));
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();
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);
280 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
281 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
282 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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();
290 for(ReportsCommonList.ReportListItem item : items){
291 logger.debug(testName + ": list-item[" + i + "] csid=" +
293 logger.debug(testName + ": list-item[" + i + "] name=" +
295 logger.debug(testName + ": list-item[" + i + "] outputMIME=" +
296 item.getOutputMIME());
297 logger.debug(testName + ": list-item[" + i + "] URI=" +
305 // ---------------------------------------------------------------
306 // CRUD tests : UPDATE tests
307 // ---------------------------------------------------------------
310 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
313 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
314 dependsOnMethods = {"read"})
315 public void update(String testName) throws Exception {
317 if (logger.isDebugEnabled()) {
318 logger.debug(testBanner(testName, CLASS_NAME));
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());
329 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
330 if(logger.isDebugEnabled()){
331 logger.debug("got object to update with ID: " + knownResourceId);
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();
341 Assert.assertNotNull(reportCommon);
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));
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);
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);
363 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
364 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
365 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
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();
374 Assert.assertNotNull(updatedReportCommon);
375 if(logger.isDebugEnabled()){
376 logger.debug("updated object");
377 logger.debug(objectAsXmlString(updatedReportCommon, ReportsCommon.class));
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.");
388 // Placeholders until the three tests below can be uncommented.
389 // See Issue CSPACE-401.
391 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
394 public void updateWithEmptyEntityBody(String testName) throws Exception {
395 //Should this really be empty?
399 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
402 public void updateWithMalformedXml(String testName) throws Exception {
403 //Should this really be empty?
407 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
410 public void updateWithWrongXmlSchema(String testName) throws Exception {
411 //Should this really be empty?
415 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
418 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
419 dependsOnMethods = {"update", "testSubmitRequest"})
420 public void updateNonExistent(String testName) throws Exception {
422 if (logger.isDebugEnabled()) {
423 logger.debug(testBanner(testName, CLASS_NAME));
426 setupUpdateNonExistent();
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();
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);
441 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
442 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
443 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
446 // ---------------------------------------------------------------
447 // CRUD tests : DELETE tests
448 // ---------------------------------------------------------------
451 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
454 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
455 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
456 public void delete(String testName) throws Exception {
458 if (logger.isDebugEnabled()) {
459 logger.debug(testBanner(testName, CLASS_NAME));
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();
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);
474 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
475 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
476 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
481 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
484 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
485 dependsOnMethods = {"delete"})
486 public void deleteNonExistent(String testName) throws Exception {
488 if (logger.isDebugEnabled()) {
489 logger.debug(testBanner(testName, CLASS_NAME));
492 setupDeleteNonExistent();
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();
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);
504 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
505 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
506 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
509 // ---------------------------------------------------------------
510 // Utility tests : tests of code used in tests above
511 // ---------------------------------------------------------------
514 * Tests the code for manually submitting data that is used by several
515 * of the methods above.
517 @Test(dependsOnMethods = {"create", "read"})
518 public void testSubmitRequest() {
520 // Expected status code: 200 OK
521 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
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);
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);
534 Assert.assertEquals(statusCode, EXPECTED_STATUS);
538 // ---------------------------------------------------------------
539 // Utility methods used by tests above
540 // ---------------------------------------------------------------
544 protected String getServiceName() {
549 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
552 public String getServicePathComponent() {
553 return SERVICE_PATH_COMPONENT;
557 * Creates the report instance.
559 * @param identifier the identifier
560 * @return the multipart output
562 private PoxPayloadOut createReportInstance(String identifier) {
563 return createReportInstance(
564 "name-" + identifier,
565 "persAuthTermCountsTest.jasper",
570 * Creates the report instance.
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
577 private PoxPayloadOut createReportInstance(String name,
580 ReportsCommon reportCommon = new ReportsCommon();
581 reportCommon.setName(name);
582 reportCommon.setFilename(filename);
583 reportCommon.setOutputMIME(outputMIME);
585 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
586 PayloadOutputPart commonPart =
587 multipart.addPart(reportCommon, MediaType.APPLICATION_XML_TYPE);
588 commonPart.setLabel(new ReportClient().getCommonPartName());
590 if(logger.isDebugEnabled()){
591 logger.debug("to be created, report common");
592 logger.debug(objectAsXmlString(reportCommon, ReportsCommon.class));
593 logger.debug(multipart.toXML());