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.math.BigDecimal;
26 import java.util.ArrayList;
27 import java.util.List;
29 import javax.ws.rs.core.Response;
31 import org.collectionspace.services.acquisition.AcquisitionSourceList;
32 import org.collectionspace.services.acquisition.AcquisitionsCommon;
33 import org.collectionspace.services.acquisition.StructuredDateGroup;
34 import org.collectionspace.services.client.AbstractCommonListUtils;
35 import org.collectionspace.services.client.CollectionSpaceClient;
36 import org.collectionspace.services.client.PayloadOutputPart;
37 import org.collectionspace.services.client.PoxPayloadOut;
38 import org.collectionspace.services.client.ReportClient;
39 import org.collectionspace.services.common.invocable.InvocationContext;
40 import org.collectionspace.services.report.ReportsCommon;
41 import org.collectionspace.services.jaxb.AbstractCommonList;
42 import org.collectionspace.services.client.AcquisitionClient;
44 import org.testng.Assert;
45 import org.testng.annotations.Test;
46 import org.slf4j.Logger;
47 import org.slf4j.LoggerFactory;
50 * FIXME: http://issues.collectionspace.org/browse/CSPACE-1685
51 * ReportServiceTest, carries out tests against a
52 * deployed and running Report Service.
54 * $LastChangedRevision: 2261 $
55 * $LastChangedDate: 2010-05-28 16:52:22 -0700 (Fri, 28 May 2010) $
57 public class ReportServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, ReportsCommon> {
60 private final String CLASS_NAME = ReportServiceTest.class.getName();
61 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
62 final String SERVICE_NAME = "reports";
63 final String SERVICE_PATH_COMPONENT = "reports";
64 // Instance variables specific to this test.
65 private String testDocType = "Acquisition";
67 private String createAquisitionResource() {
70 AcquisitionClient acquisitionClient = new AcquisitionClient();
71 AcquisitionsCommon acquisitionsCommon = new AcquisitionsCommon();
72 acquisitionsCommon.setAcquisitionReason("It was nice.");
73 acquisitionsCommon.setOriginalObjectPurchasePriceValue(new BigDecimal(500));
74 acquisitionsCommon.setAcquisitionReferenceNumber("2013.003.0004");
76 AcquisitionSourceList asl = new AcquisitionSourceList();
77 List<String> sourceList = asl.getAcquisitionSource();
78 sourceList.add("The Jim Henson Legacy");
79 acquisitionsCommon.setAcquisitionSources(asl);
81 StructuredDateGroup sdg = new StructuredDateGroup();
82 sdg.setDateDisplayDate("12/12/2012");
83 acquisitionsCommon.setAccessionDateGroup(sdg);
85 PoxPayloadOut poxPayloadOut = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
86 PayloadOutputPart commonPart = poxPayloadOut.addPart(acquisitionClient.getCommonPartName(), acquisitionsCommon);
87 Response res = acquisitionClient.create(poxPayloadOut);
90 int statusCode = res.getStatus();
91 if (logger.isDebugEnabled()) {
92 logger.debug(this.getClass().getCanonicalName() + ": HTTP status = " + statusCode);
94 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
95 invalidStatusCodeMessage(testRequestType, statusCode));
96 Assert.assertEquals(statusCode, testExpectedStatusCode);
98 result = extractId(res);
99 // Store the IDs from every resource created by tests,
100 // so they can be deleted after tests have been run.
101 allResourceIdsCreated.add(result);
109 @Test(dataProvider = "testName", dependsOnMethods = {"create"})
110 public void publishReportInstance(String testName) throws Exception {
113 // Submit the request to the service and store the response.
114 ReportClient client = (ReportClient)this.getClientInstance();
115 String reportCsid = createResource(testName, this.getKnowResourceIdentifier());
116 String acquisitionCsid = createAquisitionResource();
119 // Hard coded for now, but need to create this test in the Integration test area where
120 // we'll create an Acquisition instance for this test
122 InvocationContext invocationContext = new InvocationContext();
123 invocationContext.setDocType("Acquisition");
124 invocationContext.setMode("single");
125 invocationContext.setSingleCSID(acquisitionCsid);
127 Response res = client.publishReport(reportCsid, invocationContext);
129 int statusCode = res.getStatus();
132 // Check the status code of the response: does it match
133 // the expected response(s)?
134 if (logger.isDebugEnabled()) {
135 logger.debug(testName + ": status = " + statusCode);
137 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
138 invalidStatusCodeMessage(testRequestType, statusCode));
139 Assert.assertEquals(statusCode, testExpectedStatusCode);
141 String publicItemCsid = extractId(res);
142 Assert.assertNotNull(publicItemCsid);
149 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
152 protected CollectionSpaceClient getClientInstance() {
153 return new ReportClient();
157 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
158 return new ReportClient(clientPropertiesFilename);
161 @Test(dataProvider = "testName",
162 dependsOnMethods = {"CRUDTests"})
163 public void readListFiltered(String testName) throws Exception {
167 // Submit the request to the service and store the response.
168 ReportClient client = new ReportClient();
169 Response res = client.readListFiltered(testDocType, "single");
170 AbstractCommonList list = null;
172 assertStatusCode(res, testName);
173 list = res.readEntity(AbstractCommonList.class);
180 List<AbstractCommonList.ListItem> items = list.getListItem();
181 // We must find the basic one we created
182 boolean fFoundBaseItem = false;
183 for (AbstractCommonList.ListItem item : items) {
184 String itemCsid = AbstractCommonListUtils.ListItemGetCSID(item);
185 if (knownResourceId.equalsIgnoreCase(itemCsid)) {
186 fFoundBaseItem = true;
191 if (!fFoundBaseItem) {
192 Assert.fail("readListFiltered failed to return base item");
195 // Now filter for something else, and ensure it is NOT returned
196 res = client.readListFiltered("Intake", "single");
198 assertStatusCode(res, testName);
199 list = res.readEntity(AbstractCommonList.class);
206 items = list.getListItem();
207 // We must NOT find the basic one we created
208 for (AbstractCommonList.ListItem item : items) {
209 Assert.assertNotSame(AbstractCommonListUtils.ListItemGetCSID(item), knownResourceId,
210 "readListFiltered(\"Intake\", \"single\") incorrectly returned base item");
213 // Now filter for something else, and ensure it is NOT returned
214 res = client.readListFiltered(testDocType, "group");
216 assertStatusCode(res, testName);
217 list = res.readEntity(AbstractCommonList.class);
224 items = list.getListItem();
225 // We must NOT find the basic one we created
226 for (AbstractCommonList.ListItem item : items) {
227 Assert.assertNotSame(AbstractCommonListUtils.ListItemGetCSID(item), knownResourceId,
228 "readListFiltered(\""+testDocType+"\", \"group\") incorrectly returned base item");
233 * This method overrides the delete method in the base class which is marked with the TestNG @Test annotation.
234 * Since we don't want the actually delete test to happen until later in the dependency test chain, we're make this
235 * an empty method. Later in the test suite, the method localDelete() will get called and it will call super.delete()
238 public void delete(String testName) throws Exception {
240 // Do nothing for now. The test localDelete() will get called later in the dependency chain.
245 * This test will delete the known resource after the test readListFiltered() is run
247 @Test(dataProvider = "testName",
248 dependsOnMethods = {"readListFiltered"})
249 public void localDelete(String testName) throws Exception {
250 super.delete(testName);
253 // ---------------------------------------------------------------
254 // Utility methods used by tests above
255 // ---------------------------------------------------------------
257 protected String getServiceName() {
262 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
265 public String getServicePathComponent() {
266 return SERVICE_PATH_COMPONENT;
270 * Creates the report instance.
272 * @param identifier the identifier
273 * @return the multipart output
275 private PoxPayloadOut createReportInstance(String identifier) {
276 List<String> docTypes = new ArrayList<String>();
277 docTypes.add(testDocType);
278 return createReportInstance(
279 "Acquisition Summary " + identifier,
280 docTypes, true, false, false, true,
286 * Creates the report instance.
288 * @param name the report name
289 * @param filename the relative path to the report
290 * @param outputMIME the MIME type we will return for this report
291 * @return the multipart output
293 private PoxPayloadOut createReportInstance(String name,
294 List<String> forDocTypeList,
295 boolean supportsSingle, boolean supportsList,
296 boolean supportsGroup, boolean supportsNoContext,
299 ReportsCommon reportCommon = new ReportsCommon();
300 reportCommon.setName(name);
301 ReportsCommon.ForDocTypes forDocTypes = new ReportsCommon.ForDocTypes();
302 List<String> docTypeList = forDocTypes.getForDocType();
303 docTypeList.addAll(forDocTypeList);
304 reportCommon.setForDocTypes(forDocTypes);
305 reportCommon.setSupportsSingleDoc(supportsSingle);
306 reportCommon.setSupportsDocList(supportsList);
307 reportCommon.setSupportsGroup(supportsGroup);
308 reportCommon.setSupportsNoContext(supportsNoContext);
309 reportCommon.setFilename(filename);
310 reportCommon.setOutputMIME(outputMIME);
311 reportCommon.setNotes(getUTF8DataFragment()); // For UTF-8 tests
313 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
314 PayloadOutputPart commonPart =
315 multipart.addPart(new ReportClient().getCommonPartName(), reportCommon);
317 if (logger.isDebugEnabled()) {
318 logger.debug("to be created, report common");
319 logger.debug(objectAsXmlString(reportCommon, ReportsCommon.class));
320 logger.debug(multipart.toXML());
327 protected PoxPayloadOut createInstance(String commonPartName,
329 PoxPayloadOut result = createReportInstance(identifier);
334 protected ReportsCommon updateInstance(ReportsCommon reportsCommon) {
335 ReportsCommon result = new ReportsCommon();
337 result.setSupportsSingleDoc(true);
338 result.setName("updated-" + reportsCommon.getName());
339 result.setOutputMIME("updated-" + reportsCommon.getOutputMIME());
340 result.setNotes("updated-" + reportsCommon.getNotes());
346 protected void compareUpdatedInstances(ReportsCommon original,
347 ReportsCommon updated) throws Exception {
348 // Check selected fields in the updated common part.
349 Assert.assertEquals(updated.getName(),
351 "Name in updated object did not match submitted data.");
353 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
354 if (logger.isDebugEnabled()) {
355 logger.debug("UTF-8 data sent=" + original.getNotes() + "\n"
356 + "UTF-8 data received=" + updated.getNotes());
358 Assert.assertTrue(updated.getNotes().contains(getUTF8DataFragment()),
359 "UTF-8 data retrieved '" + updated.getNotes()
360 + "' does not contain expected data '" + getUTF8DataFragment());
361 Assert.assertEquals(updated.getNotes(),
363 "Notes in updated object did not match submitted data.");
366 protected void compareReadInstances(ReportsCommon original, ReportsCommon fromRead) throws Exception {
367 Assert.assertEquals(fromRead.getNotes(), getUTF8DataFragment(),
368 "UTF-8 data retrieved '" + fromRead.getNotes()
369 + "' does not match expected data '" + getUTF8DataFragment());
373 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
374 * refer to this method in their @Test annotation declarations.
377 @Test(dataProvider = "testName",
379 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
380 public void CRUDTests(String testName) {
381 // TODO Auto-generated method stub