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.ArrayList;
26 import java.util.List;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
30 import org.collectionspace.services.client.CollectionSpaceClient;
31 import org.collectionspace.services.client.PayloadInputPart;
32 import org.collectionspace.services.client.PayloadOutputPart;
33 import org.collectionspace.services.client.PoxPayloadIn;
34 import org.collectionspace.services.client.PoxPayloadOut;
35 import org.collectionspace.services.client.ReportClient;
36 import org.collectionspace.services.common.AbstractCommonListUtils;
37 import org.collectionspace.services.report.ReportsCommon;
38 import org.collectionspace.services.jaxb.AbstractCommonList;
40 import org.jboss.resteasy.client.ClientResponse;
41 import org.testng.Assert;
42 //import org.testng.annotations.AfterClass;
43 import org.testng.annotations.Test;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * FIXME: http://issues.collectionspace.org/browse/CSPACE-1685
50 * ReportServiceTest, carries out tests against a
51 * deployed and running Report Service.
53 * $LastChangedRevision: 2261 $
54 * $LastChangedDate: 2010-05-28 16:52:22 -0700 (Fri, 28 May 2010) $
56 public class ReportServiceTest extends AbstractServiceTestImpl {
59 private final String CLASS_NAME = ReportServiceTest.class.getName();
60 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
61 final String SERVICE_NAME = "reports";
62 final String SERVICE_PATH_COMPONENT = "reports";
63 // Instance variables specific to this test.
64 /** The known resource id. */
65 private String knownResourceId = null;
67 private String testDocType = "Acquisition";
70 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
73 protected CollectionSpaceClient getClientInstance() {
74 return new ReportClient();
78 // protected PoxPayloadOut createInstance(String identifier) {
79 // PoxPayloadOut multipart = createReportInstance(identifier);
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);
196 assertStatusCode(res, testName);
198 // Get the common part of the response and verify that it is not null.
199 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
200 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
201 ReportsCommon reportCommon = null;
202 if (payloadInputPart != null) {
203 reportCommon = (ReportsCommon) payloadInputPart.getBody();
205 Assert.assertNotNull(reportCommon);
207 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
208 if (logger.isDebugEnabled()) {
209 logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
210 + "UTF-8 data received=" + reportCommon.getNotes());
212 Assert.assertEquals(reportCommon.getNotes(), getUTF8DataFragment(),
213 "UTF-8 data retrieved '" + reportCommon.getNotes()
214 + "' does not match expected data '" + getUTF8DataFragment());
219 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
222 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
223 dependsOnMethods = {"read"})
224 public void readNonExistent(String testName) throws Exception {
226 if (logger.isDebugEnabled()) {
227 logger.debug(testBanner(testName, CLASS_NAME));
230 setupReadNonExistent();
232 // Submit the request to the service and store the response.
233 ReportClient client = new ReportClient();
234 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
235 int statusCode = res.getStatus();
237 // Check the status code of the response: does it match
238 // the expected response(s)?
239 if (logger.isDebugEnabled()) {
240 logger.debug(testName + ": status = " + statusCode);
242 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
243 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
244 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
247 // ---------------------------------------------------------------
248 // CRUD tests : READ_LIST tests
249 // ---------------------------------------------------------------
252 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
255 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
256 dependsOnMethods = {"createList", "read"})
257 public void readList(String testName) throws Exception {
259 if (logger.isDebugEnabled()) {
260 logger.debug(testBanner(testName, CLASS_NAME));
265 // Submit the request to the service and store the response.
266 ReportClient client = new ReportClient();
267 ClientResponse<AbstractCommonList> res = client.readList();
268 assertStatusCode(res, testName);
269 AbstractCommonList list = res.getEntity();
271 // Optionally output additional data about list members for debugging.
272 if(logger.isTraceEnabled()){
273 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
277 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
278 dependsOnMethods = {"readList"})
279 public void readListFiltered(String testName) throws Exception {
280 if (logger.isDebugEnabled()) {
281 logger.debug(testBanner(testName, CLASS_NAME));
286 // Submit the request to the service and store the response.
287 ReportClient client = new ReportClient();
288 ClientResponse<AbstractCommonList> res = client.readListFiltered(
289 testDocType, "single");
290 assertStatusCode(res, testName);
291 AbstractCommonList list = res.getEntity();
293 List<AbstractCommonList.ListItem> items =
295 // We must find the basic one we created
296 boolean fFoundBaseItem = false;
297 for (AbstractCommonList.ListItem item : items) {
298 if(knownResourceId.equalsIgnoreCase(AbstractCommonListUtils.ListItemGetCSID(item))) {
299 fFoundBaseItem = true;
304 Assert.fail("readListFiltered failed to return base item");
306 // Now filter for something else, and ensure it is NOT returned
307 res = client.readListFiltered("Intake", "single");
308 assertStatusCode(res, testName);
309 list = res.getEntity();
311 items = list.getListItem();
312 // We must NOT find the basic one we created
313 for (AbstractCommonList.ListItem item : items) {
314 Assert.assertNotSame(AbstractCommonListUtils.ListItemGetCSID(item), knownResourceId,
315 "readListFiltered(\"Intake\", \"single\") incorrectly returned base item");
318 // Now filter for something else, and ensure it is NOT returned
319 res = client.readListFiltered(testDocType, "group");
320 assertStatusCode(res, testName);
321 list = res.getEntity();
323 items = list.getListItem();
324 // We must NOT find the basic one we created
325 for (AbstractCommonList.ListItem item : items) {
326 Assert.assertNotSame(AbstractCommonListUtils.ListItemGetCSID(item), knownResourceId,
327 "readListFiltered(\""+testDocType+"\", \"group\") incorrectly returned base item");
331 // ---------------------------------------------------------------
332 // CRUD tests : UPDATE tests
333 // ---------------------------------------------------------------
336 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
339 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
340 dependsOnMethods = {"read", "readListFiltered"})
341 public void update(String testName) throws Exception {
343 if (logger.isDebugEnabled()) {
344 logger.debug(testBanner(testName, CLASS_NAME));
349 // Retrieve the contents of a resource to update.
350 ReportClient client = new ReportClient();
351 ClientResponse<String> res = client.read(knownResourceId);
352 assertStatusCode(res, testName);
353 if (logger.isDebugEnabled()) {
354 logger.debug("got object to update with ID: " + knownResourceId);
357 // Extract the common part from the response.
358 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
359 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
360 ReportsCommon reportCommon = null;
361 if (payloadInputPart != null) {
362 reportCommon = (ReportsCommon) payloadInputPart.getBody();
364 Assert.assertNotNull(reportCommon);
366 // Update its content.
367 reportCommon.setName("updated-" + reportCommon.getName());
368 reportCommon.setOutputMIME("updated-" + reportCommon.getOutputMIME());
369 if (logger.isDebugEnabled()) {
370 logger.debug("to be updated object");
371 logger.debug(objectAsXmlString(reportCommon, ReportsCommon.class));
373 reportCommon.setNotes("updated-" + reportCommon.getNotes());
375 // Submit the updated common part in an update request to the service
376 // and store the response.
377 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
378 PayloadOutputPart commonPart = output.addPart(reportCommon, MediaType.APPLICATION_XML_TYPE);
379 commonPart.setLabel(client.getCommonPartName());
380 res = client.update(knownResourceId, output);
381 assertStatusCode(res, testName);
383 // Extract the updated common part from the response.
384 input = new PoxPayloadIn(res.getEntity());
385 payloadInputPart = input.getPart(client.getCommonPartName());
386 ReportsCommon updatedReportCommon = null;
387 if (payloadInputPart != null) {
388 updatedReportCommon = (ReportsCommon) payloadInputPart.getBody();
390 Assert.assertNotNull(updatedReportCommon);
391 if (logger.isDebugEnabled()) {
392 logger.debug("updated object");
393 logger.debug(objectAsXmlString(updatedReportCommon, ReportsCommon.class));
396 // Check selected fields in the updated common part.
397 Assert.assertEquals(updatedReportCommon.getName(),
398 reportCommon.getName(),
399 "Data in updated object did not match submitted data.");
401 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
402 if (logger.isDebugEnabled()) {
403 logger.debug("UTF-8 data sent=" + reportCommon.getNotes() + "\n"
404 + "UTF-8 data received=" + updatedReportCommon.getNotes());
406 Assert.assertTrue(updatedReportCommon.getNotes().contains(getUTF8DataFragment()),
407 "UTF-8 data retrieved '" + updatedReportCommon.getNotes()
408 + "' does not contain expected data '" + getUTF8DataFragment());
409 Assert.assertEquals(updatedReportCommon.getNotes(),
410 reportCommon.getNotes(),
411 "Data in updated object did not match submitted data.");
415 // Placeholders until the three tests below can be uncommented.
416 // See Issue CSPACE-401.
418 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
421 public void updateWithEmptyEntityBody(String testName) throws Exception {
422 //Should this really be empty?
426 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
429 public void updateWithMalformedXml(String testName) throws Exception {
430 //Should this really be empty?
434 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
437 public void updateWithWrongXmlSchema(String testName) throws Exception {
438 //Should this really be empty?
442 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
445 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
446 dependsOnMethods = {"update", "testSubmitRequest"})
447 public void updateNonExistent(String testName) throws Exception {
449 if (logger.isDebugEnabled()) {
450 logger.debug(testBanner(testName, CLASS_NAME));
453 setupUpdateNonExistent();
455 // Submit the request to the service and store the response.
456 // Note: The ID used in this 'create' call may be arbitrary.
457 // The only relevant ID may be the one used in update(), below.
458 ReportClient client = new ReportClient();
459 PoxPayloadOut multipart = createReportInstance(NON_EXISTENT_ID);
460 ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
461 int statusCode = res.getStatus();
463 // Check the status code of the response: does it match
464 // the expected response(s)?
465 if (logger.isDebugEnabled()) {
466 logger.debug(testName + ": status = " + statusCode);
468 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
469 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
470 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
473 // ---------------------------------------------------------------
474 // CRUD tests : DELETE tests
475 // ---------------------------------------------------------------
478 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
481 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
482 dependsOnMethods = {"create", "readListFiltered", "testSubmitRequest", "update", "readWorkflow"})
483 public void delete(String testName) throws Exception {
485 if (logger.isDebugEnabled()) {
486 logger.debug(testBanner(testName, CLASS_NAME));
491 // Submit the request to the service and store the response.
492 ReportClient client = new ReportClient();
493 ClientResponse<Response> res = client.delete(knownResourceId);
494 int statusCode = res.getStatus();
496 // Check the status code of the response: does it match
497 // the expected response(s)?
498 if (logger.isDebugEnabled()) {
499 logger.debug(testName + ": status = " + statusCode);
501 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
502 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
503 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
508 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
511 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
512 dependsOnMethods = {"delete"})
513 public void deleteNonExistent(String testName) throws Exception {
515 if (logger.isDebugEnabled()) {
516 logger.debug(testBanner(testName, CLASS_NAME));
519 setupDeleteNonExistent();
521 // Submit the request to the service and store the response.
522 ReportClient client = new ReportClient();
523 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
524 int statusCode = res.getStatus();
526 // Check the status code of the response: does it match
527 // the expected response(s)?
528 if (logger.isDebugEnabled()) {
529 logger.debug(testName + ": status = " + statusCode);
531 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
532 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
533 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
536 // ---------------------------------------------------------------
537 // Utility tests : tests of code used in tests above
538 // ---------------------------------------------------------------
540 * Tests the code for manually submitting data that is used by several
541 * of the methods above.
543 @Test(dependsOnMethods = {"create", "read"})
544 public void testSubmitRequest() {
546 // Expected status code: 200 OK
547 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
549 // Submit the request to the service and store the response.
550 String method = ServiceRequestType.READ.httpMethodName();
551 String url = getResourceURL(knownResourceId);
552 int statusCode = submitRequest(method, url);
554 // Check the status code of the response: does it match
555 // the expected response(s)?
556 if (logger.isDebugEnabled()) {
557 logger.debug("testSubmitRequest: url=" + url
558 + " status=" + statusCode);
560 Assert.assertEquals(statusCode, EXPECTED_STATUS);
564 // ---------------------------------------------------------------
565 // Utility methods used by tests above
566 // ---------------------------------------------------------------
568 protected String getServiceName() {
573 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
576 public String getServicePathComponent() {
577 return SERVICE_PATH_COMPONENT;
581 * Creates the report instance.
583 * @param identifier the identifier
584 * @return the multipart output
586 private PoxPayloadOut createReportInstance(String identifier) {
587 List<String> docTypes = new ArrayList<String>();
588 docTypes.add(testDocType);
589 return createReportInstance(
590 "Acquisition Summary",
591 docTypes, true, false, false, true,
597 * Creates the report instance.
599 * @param name the report name
600 * @param filename the relative path to the report
601 * @param outputMIME the MIME type we will return for this report
602 * @return the multipart output
604 private PoxPayloadOut createReportInstance(String name,
605 List<String> forDocTypeList,
606 boolean supportsSingle, boolean supportsList,
607 boolean supportsGroup, boolean supportsNoContext,
610 ReportsCommon reportCommon = new ReportsCommon();
611 reportCommon.setName(name);
612 ReportsCommon.ForDocTypes forDocTypes = new ReportsCommon.ForDocTypes();
613 List<String> docTypeList = forDocTypes.getForDocType();
614 docTypeList.addAll(forDocTypeList);
615 reportCommon.setForDocTypes(forDocTypes);
616 reportCommon.setSupportsSingleDoc(supportsSingle);
617 reportCommon.setSupportsDocList(supportsList);
618 reportCommon.setSupportsGroup(supportsGroup);
619 reportCommon.setSupportsNoContext(supportsNoContext);
620 reportCommon.setFilename(filename);
621 reportCommon.setOutputMIME(outputMIME);
622 reportCommon.setNotes(getUTF8DataFragment()); // For UTF-8 tests
624 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
625 PayloadOutputPart commonPart =
626 multipart.addPart(reportCommon, MediaType.APPLICATION_XML_TYPE);
627 commonPart.setLabel(new ReportClient().getCommonPartName());
629 if (logger.isDebugEnabled()) {
630 logger.debug("to be created, report common");
631 logger.debug(objectAsXmlString(reportCommon, ReportsCommon.class));
632 logger.debug(multipart.toXML());