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.MediaType;
30 import javax.ws.rs.core.Response;
32 import org.collectionspace.services.acquisition.AcquisitionSourceList;
33 import org.collectionspace.services.acquisition.AcquisitionsCommon;
34 import org.collectionspace.services.acquisition.StructuredDateGroup;
35 import org.collectionspace.services.client.AbstractCommonListUtils;
36 import org.collectionspace.services.client.CollectionSpaceClient;
37 import org.collectionspace.services.client.PayloadOutputPart;
38 import org.collectionspace.services.client.PoxPayloadOut;
39 import org.collectionspace.services.client.ReportClient;
40 import org.collectionspace.services.common.invocable.InvocationContext;
41 import org.collectionspace.services.report.ReportsCommon;
42 import org.collectionspace.services.jaxb.AbstractCommonList;
43 import org.collectionspace.services.client.AcquisitionClient;
45 import org.jboss.resteasy.client.ClientResponse;
46 import org.testng.Assert;
47 //import org.testng.annotations.AfterClass;
48 import org.testng.annotations.Test;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
54 * FIXME: http://issues.collectionspace.org/browse/CSPACE-1685
55 * ReportServiceTest, carries out tests against a
56 * deployed and running Report Service.
58 * $LastChangedRevision: 2261 $
59 * $LastChangedDate: 2010-05-28 16:52:22 -0700 (Fri, 28 May 2010) $
61 public class ReportServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, ReportsCommon> {
64 private final String CLASS_NAME = ReportServiceTest.class.getName();
65 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
66 final String SERVICE_NAME = "reports";
67 final String SERVICE_PATH_COMPONENT = "reports";
68 // Instance variables specific to this test.
69 private String testDocType = "Acquisition";
71 private String createAquisitionResource() {
74 AcquisitionClient acquisitionClient = new AcquisitionClient();
75 AcquisitionsCommon acquisitionsCommon = new AcquisitionsCommon();
76 acquisitionsCommon.setAcquisitionReason("It was nice.");
77 acquisitionsCommon.setOriginalObjectPurchasePriceValue(new BigDecimal(500));
78 acquisitionsCommon.setAcquisitionReferenceNumber("2013.003.0004");
80 AcquisitionSourceList asl = new AcquisitionSourceList();
81 List<String> sourceList = asl.getAcquisitionSource();
82 sourceList.add("The Jim Henson Legacy");
83 acquisitionsCommon.setAcquisitionSources(asl);
85 StructuredDateGroup sdg = new StructuredDateGroup();
86 sdg.setDateDisplayDate("12/12/2012");
87 acquisitionsCommon.setAccessionDateGroup(sdg);
89 PoxPayloadOut poxPayloadOut = new PoxPayloadOut(AcquisitionClient.SERVICE_PAYLOAD_NAME);
90 PayloadOutputPart commonPart = poxPayloadOut.addPart(acquisitionClient.getCommonPartName(), acquisitionsCommon);
91 Response res = acquisitionClient.create(poxPayloadOut);
94 int statusCode = res.getStatus();
95 if (logger.isDebugEnabled()) {
96 logger.debug(this.getClass().getCanonicalName() + ": HTTP status = " + statusCode);
98 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
99 invalidStatusCodeMessage(testRequestType, statusCode));
100 Assert.assertEquals(statusCode, testExpectedStatusCode);
102 result = extractId(res);
103 // Store the IDs from every resource created by tests,
104 // so they can be deleted after tests have been run.
105 allResourceIdsCreated.add(result);
113 @Test(dataProvider = "testName", dependsOnMethods = {"create"})
114 public void publishReportInstance(String testName) throws Exception {
117 // Submit the request to the service and store the response.
118 ReportClient client = (ReportClient)this.getClientInstance();
119 String reportCsid = createResource(testName, this.getKnowResourceIdentifier());
120 String acquisitionCsid = createAquisitionResource();
123 // Hard coded for now, but need to create this test in the Integration test area where
124 // we'll create an Acquisition instance for this test
126 InvocationContext invocationContext = new InvocationContext();
127 invocationContext.setDocType("Acquisition");
128 invocationContext.setMode("single");
129 invocationContext.setSingleCSID(acquisitionCsid);
131 ClientResponse<Response> res = client.publishReport(reportCsid, invocationContext);
132 int statusCode = res.getStatus();
135 // Check the status code of the response: does it match
136 // the expected response(s)?
137 if (logger.isDebugEnabled()) {
138 logger.debug(testName + ": status = " + statusCode);
140 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
141 invalidStatusCodeMessage(testRequestType, statusCode));
142 Assert.assertEquals(statusCode, testExpectedStatusCode);
144 String publicItemCsid = extractId(res);
145 Assert.assertNotNull(publicItemCsid);
149 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
152 protected CollectionSpaceClient getClientInstance() {
153 return new ReportClient();
156 @Test(dataProvider = "testName",
157 dependsOnMethods = {"CRUDTests"})
158 public void readListFiltered(String testName) throws Exception {
162 // Submit the request to the service and store the response.
163 ReportClient client = new ReportClient();
164 ClientResponse<AbstractCommonList> res = client.readListFiltered(testDocType, "single");
165 AbstractCommonList list = null;
167 assertStatusCode(res, testName);
168 list = res.getEntity();
171 res.releaseConnection();
174 List<AbstractCommonList.ListItem> items = list.getListItem();
175 // We must find the basic one we created
176 boolean fFoundBaseItem = false;
177 for (AbstractCommonList.ListItem item : items) {
178 String itemCsid = AbstractCommonListUtils.ListItemGetCSID(item);
179 if (knownResourceId.equalsIgnoreCase(itemCsid)) {
180 fFoundBaseItem = true;
184 if(!fFoundBaseItem) {
185 Assert.fail("readListFiltered failed to return base item");
188 // Now filter for something else, and ensure it is NOT returned
189 res = client.readListFiltered("Intake", "single");
191 assertStatusCode(res, testName);
192 list = res.getEntity();
195 res.releaseConnection();
199 items = list.getListItem();
200 // We must NOT find the basic one we created
201 for (AbstractCommonList.ListItem item : items) {
202 Assert.assertNotSame(AbstractCommonListUtils.ListItemGetCSID(item), knownResourceId,
203 "readListFiltered(\"Intake\", \"single\") incorrectly returned base item");
206 // Now filter for something else, and ensure it is NOT returned
207 res = client.readListFiltered(testDocType, "group");
209 assertStatusCode(res, testName);
210 list = res.getEntity();
213 res.releaseConnection();
217 items = list.getListItem();
218 // We must NOT find the basic one we created
219 for (AbstractCommonList.ListItem item : items) {
220 Assert.assertNotSame(AbstractCommonListUtils.ListItemGetCSID(item), knownResourceId,
221 "readListFiltered(\""+testDocType+"\", \"group\") incorrectly returned base item");
226 * This method overrides the delete method in the base class which is marked with the TestNG @Test annotation.
227 * Since we don't want the actually delete test to happen until later in the dependency test chain, we're make this
228 * an empty method. Later in the test suite, the method localDelete() will get called and it will call super.delete()
231 public void delete(String testName) throws Exception {
233 // Do nothing for now. The test localDelete() will get called later in the dependency chain.
238 * This test will delete the known resource after the test readListFiltered() is run
240 @Test(dataProvider = "testName",
241 dependsOnMethods = {"readListFiltered"})
242 public void localDelete(String testName) throws Exception {
243 super.delete(testName);
246 // ---------------------------------------------------------------
247 // Utility methods used by tests above
248 // ---------------------------------------------------------------
250 protected String getServiceName() {
255 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
258 public String getServicePathComponent() {
259 return SERVICE_PATH_COMPONENT;
263 * Creates the report instance.
265 * @param identifier the identifier
266 * @return the multipart output
268 private PoxPayloadOut createReportInstance(String identifier) {
269 List<String> docTypes = new ArrayList<String>();
270 docTypes.add(testDocType);
271 return createReportInstance(
272 "Acquisition Summary " + identifier,
273 docTypes, true, false, false, true,
279 * Creates the report instance.
281 * @param name the report name
282 * @param filename the relative path to the report
283 * @param outputMIME the MIME type we will return for this report
284 * @return the multipart output
286 private PoxPayloadOut createReportInstance(String name,
287 List<String> forDocTypeList,
288 boolean supportsSingle, boolean supportsList,
289 boolean supportsGroup, boolean supportsNoContext,
292 ReportsCommon reportCommon = new ReportsCommon();
293 reportCommon.setName(name);
294 ReportsCommon.ForDocTypes forDocTypes = new ReportsCommon.ForDocTypes();
295 List<String> docTypeList = forDocTypes.getForDocType();
296 docTypeList.addAll(forDocTypeList);
297 reportCommon.setForDocTypes(forDocTypes);
298 reportCommon.setSupportsSingleDoc(supportsSingle);
299 reportCommon.setSupportsDocList(supportsList);
300 reportCommon.setSupportsGroup(supportsGroup);
301 reportCommon.setSupportsNoContext(supportsNoContext);
302 reportCommon.setFilename(filename);
303 reportCommon.setOutputMIME(outputMIME);
304 reportCommon.setNotes(getUTF8DataFragment()); // For UTF-8 tests
306 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
307 PayloadOutputPart commonPart =
308 multipart.addPart(new ReportClient().getCommonPartName(), reportCommon);
310 if (logger.isDebugEnabled()) {
311 logger.debug("to be created, report common");
312 logger.debug(objectAsXmlString(reportCommon, ReportsCommon.class));
313 logger.debug(multipart.toXML());
320 protected PoxPayloadOut createInstance(String commonPartName,
322 PoxPayloadOut result = createReportInstance(identifier);
327 protected ReportsCommon updateInstance(ReportsCommon reportsCommon) {
328 ReportsCommon result = new ReportsCommon();
330 result.setSupportsSingleDoc(true);
331 result.setName("updated-" + reportsCommon.getName());
332 result.setOutputMIME("updated-" + reportsCommon.getOutputMIME());
333 result.setNotes("updated-" + reportsCommon.getNotes());
339 protected void compareUpdatedInstances(ReportsCommon original,
340 ReportsCommon updated) throws Exception {
341 // Check selected fields in the updated common part.
342 Assert.assertEquals(updated.getName(),
344 "Name in updated object did not match submitted data.");
346 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
347 if (logger.isDebugEnabled()) {
348 logger.debug("UTF-8 data sent=" + original.getNotes() + "\n"
349 + "UTF-8 data received=" + updated.getNotes());
351 Assert.assertTrue(updated.getNotes().contains(getUTF8DataFragment()),
352 "UTF-8 data retrieved '" + updated.getNotes()
353 + "' does not contain expected data '" + getUTF8DataFragment());
354 Assert.assertEquals(updated.getNotes(),
356 "Notes in updated object did not match submitted data.");
359 protected void compareReadInstances(ReportsCommon original, ReportsCommon fromRead) throws Exception {
360 Assert.assertEquals(fromRead.getNotes(), getUTF8DataFragment(),
361 "UTF-8 data retrieved '" + fromRead.getNotes()
362 + "' does not match expected data '" + getUTF8DataFragment());
366 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
367 * refer to this method in their @Test annotation declarations.
370 @Test(dataProvider = "testName",
372 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
373 public void CRUDTests(String testName) {
374 // TODO Auto-generated method stub