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.math.BigDecimal;
27 import java.util.List;
29 import org.collectionspace.services.client.CollectionSpaceClient;
30 import org.collectionspace.services.client.DimensionClient;
31 import org.collectionspace.services.client.DimensionFactory;
32 import org.collectionspace.services.client.PoxPayloadOut;
33 import org.collectionspace.services.dimension.DimensionsCommon;
34 import org.collectionspace.services.dimension.DimensionsCommonList;
36 import org.testng.Assert;
37 //import org.testng.annotations.AfterClass;
38 import org.testng.annotations.Test;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
44 * DimensionServiceTest, carries out tests against a
45 * deployed and running Dimension Service.
47 * $LastChangedRevision: 917 $
48 * $LastChangedDate: 2009-11-06 12:20:28 -0800 (Fri, 06 Nov 2009) $
50 public class DimensionServiceTest extends AbstractPoxServiceTestImpl<DimensionsCommonList, DimensionsCommon> {
53 private final String CLASS_NAME = DimensionServiceTest.class.getName();
54 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
56 // Instance variables specific to this test.
57 /** The SERVIC e_ pat h_ component. */
58 private final String DIMENSION_VALUE = "78.306";
61 protected Logger getLogger() {
66 protected String getServiceName() {
67 return DimensionClient.SERVICE_NAME;
71 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
74 protected CollectionSpaceClient getClientInstance() {
75 return new DimensionClient();
79 protected Class<DimensionsCommonList> getCommonListType() {
80 return DimensionsCommonList.class;
84 * This method gets called by the parent's method public void readList(String testName)
86 protected void printList(String testName, DimensionsCommonList list) {
87 // Optionally output additional data about list members for debugging.
88 boolean iterateThroughList = false;
89 if (iterateThroughList && logger.isDebugEnabled()) {
90 List<DimensionsCommonList.DimensionListItem> items =
91 list.getDimensionListItem();
93 for (DimensionsCommonList.DimensionListItem item : items) {
94 logger.debug(testName + ": list-item[" + i + "] csid="
96 logger.debug(testName + ": list-item[" + i + "] objectNumber="
97 + item.getDimension());
98 logger.debug(testName + ": list-item[" + i + "] URI="
105 protected void compareInstances(DimensionsCommon original, DimensionsCommon updated) throws Exception {
106 Assert.assertEquals(original.getValueDate(),
107 updated.getValueDate(),
108 "Data in updated object did not match submitted data.");
112 protected DimensionsCommon updateInstance(DimensionsCommon dimensionsCommon) {
113 DimensionsCommon result = new DimensionsCommon();
115 // Update the content of this resource.
116 result.setValue(dimensionsCommon.getValue().multiply(new BigDecimal("2.0")));
117 result.setValueDate("updated-" + dimensionsCommon.getValueDate());
122 // ---------------------------------------------------------------
123 // Utility methods used by tests above
124 // ---------------------------------------------------------------
126 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
129 public String getServicePathComponent() {
130 return DimensionClient.SERVICE_PATH_COMPONENT;
134 protected PoxPayloadOut createInstance(String identifier) {
135 DimensionClient client = new DimensionClient();
136 return createInstance(client.getCommonPartName(), identifier);
140 * Creates the dimension instance.
142 * @param identifier the identifier
143 * @return the multipart output
146 protected PoxPayloadOut createInstance(String commonPartName, String identifier) {
147 return createDimensionInstance(commonPartName,
148 "dimensionType-" + identifier,
150 "entryDate-" + identifier);
154 * Creates the dimension instance.
156 * @param dimensionType the dimension type
157 * @param entryNumber the entry number
158 * @param entryDate the entry date
159 * @return the multipart output
161 private PoxPayloadOut createDimensionInstance(String commonPartName, String dimensionType, String dimensionValue, String entryDate) {
162 DimensionsCommon dimensionsCommon = new DimensionsCommon();
163 dimensionsCommon.setDimension(dimensionType);
164 dimensionsCommon.setValue(new BigDecimal(dimensionValue));
165 dimensionsCommon.setValueDate(entryDate);
166 PoxPayloadOut multipart = DimensionFactory.createDimensionInstance(
167 commonPartName, dimensionsCommon);
169 if (logger.isDebugEnabled()) {
170 logger.debug("to be created, dimension common");
171 logger.debug(objectAsXmlString(dimensionsCommon,
172 DimensionsCommon.class));
178 // Placeholders until the three tests below can be uncommented.
179 // See Issue CSPACE-401.
182 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
185 public void createWithMalformedXml(String testName) throws Exception {
186 //Should this really be empty?
190 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
193 public void createWithWrongXmlSchema(String testName) throws Exception {
194 //Should this really be empty?
198 public void createWithEmptyEntityBody(String testName) throws Exception {
199 //FIXME: Should this test really be empty?
202 public void updateWithEmptyEntityBody(String testName) throws Exception {
203 //Should this really be empty?
207 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
210 public void updateWithMalformedXml(String testName) throws Exception {
211 //Should this really be empty?
215 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
218 public void updateWithWrongXmlSchema(String testName) throws Exception {
219 //Should this really be empty?
223 protected void compareUpdatedInstances(DimensionsCommon original,
224 DimensionsCommon updated) throws Exception {
225 //Check the dimension value to see if the update happened correctly
226 BigDecimal expectedValue = original.getValue();
227 BigDecimal actualValue = updated.getValue();
228 Assert.assertTrue(actualValue.compareTo(expectedValue) == 0);
230 //Next, check the date value to see if it was updated
231 String expectedDate = original.getValueDate();
232 String actualDate = updated.getValueDate();
233 Assert.assertEquals(actualDate, expectedDate);
237 * For convenience and terseness, this test method is the base of the test execution dependency chain. Other test methods may
238 * refer to this method in their @Test annotation declarations.
241 @Test(dataProvider = "testName",
243 "org.collectionspace.services.client.test.AbstractServiceTestImpl.baseCRUDTests"})
244 public void CRUDTests(String testName) {
245 // Do nothing. Simply here to for a TestNG execution order for our tests
250 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
251 dependsOnMethods = {"create", "testSubmitRequest"})
252 public void createWithEmptyEntityBody(String testName) throws Exception {
254 if (logger.isDebugEnabled()) {
255 logger.debug(testBanner(testName, CLASS_NAME));
258 setupCreateWithEmptyEntityBody(testName, logger);
260 // Submit the request to the service and store the response.
261 String method = REQUEST_TYPE.httpMethodName();
262 String url = getServiceRootURL();
263 String mediaType = MediaType.APPLICATION_XML;
264 final String entity = "";
265 int statusCode = submitRequest(method, url, mediaType, entity);
267 // Check the status code of the response: does it match
268 // the expected response(s)?
269 if(logger.isDebugEnabled()){
270 logger.debug("createWithEmptyEntityBody url=" + url +
271 " status=" + statusCode);
273 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
274 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
275 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
279 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
280 dependsOnMethods = {"create", "testSubmitRequest"})
281 public void createWithMalformedXml(String testName) throws Exception {
283 if (logger.isDebugEnabled()) {
284 logger.debug(testBanner(testName, CLASS_NAME));
287 setupCreateWithMalformedXml();
289 // Submit the request to the service and store the response.
290 String method = REQUEST_TYPE.httpMethodName();
291 String url = getServiceRootURL();
292 String mediaType = MediaType.APPLICATION_XML;
293 final String entity = MALFORMED_XML_DATA; // Constant from base class.
294 int statusCode = submitRequest(method, url, mediaType, entity);
296 // Check the status code of the response: does it match
297 // the expected response(s)?
298 if(logger.isDebugEnabled()){
299 logger.debug(testName + ": url=" + url +
300 " status=" + statusCode);
302 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
303 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
304 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
308 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
309 dependsOnMethods = {"create", "testSubmitRequest"})
310 public void createWithWrongXmlSchema(String testName) throws Exception {
312 if (logger.isDebugEnabled()) {
313 logger.debug(testBanner(testName, CLASS_NAME));
316 setupCreateWithWrongXmlSchema();
318 // Submit the request to the service and store the response.
319 String method = REQUEST_TYPE.httpMethodName();
320 String url = getServiceRootURL();
321 String mediaType = MediaType.APPLICATION_XML;
322 final String entity = WRONG_XML_SCHEMA_DATA;
323 int statusCode = submitRequest(method, url, mediaType, entity);
325 // Check the status code of the response: does it match
326 // the expected response(s)?
327 if(logger.isDebugEnabled()){
328 logger.debug(testName + ": url=" + url +
329 " status=" + statusCode);
331 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
332 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
333 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
338 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
339 dependsOnMethods = {"create", "update", "testSubmitRequest"})
340 public void updateWithEmptyEntityBody(String testName) throws Exception {
342 if (logger.isDebugEnabled()) {
343 logger.debug(testBanner(testName, CLASS_NAME));
346 setupUpdateWithEmptyEntityBody();
348 // Submit the request to the service and store the response.
349 String method = REQUEST_TYPE.httpMethodName();
350 String url = getResourceURL(knownResourceId);
351 String mediaType = MediaType.APPLICATION_XML;
352 final String entity = "";
353 int statusCode = submitRequest(method, url, mediaType, entity);
355 // Check the status code of the response: does it match
356 // the expected response(s)?
357 if(logger.isDebugEnabled()){
358 logger.debug(testName + ": url=" + url +
359 " status=" + statusCode);
361 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
362 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
363 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
367 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
368 dependsOnMethods = {"create", "update", "testSubmitRequest"})
369 public void updateWithMalformedXml(String testName) throws Exception {
371 if (logger.isDebugEnabled()) {
372 logger.debug(testBanner(testName, CLASS_NAME));
375 setupUpdateWithMalformedXml(testName, logger);
377 // Submit the request to the service and store the response.
378 String method = REQUEST_TYPE.httpMethodName();
379 String url = getResourceURL(knownResourceId);
380 String mediaType = MediaType.APPLICATION_XML;
381 final String entity = MALFORMED_XML_DATA;
382 int statusCode = submitRequest(method, url, mediaType, entity);
384 // Check the status code of the response: does it match
385 // the expected response(s)?
386 if(logger.isDebugEnabled()){
387 logger.debug(testName + ": url=" + url +
388 " status=" + statusCode);
390 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
391 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
392 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
396 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
397 dependsOnMethods = {"create", "update", "testSubmitRequest"})
398 public void updateWithWrongXmlSchema(String testName) throws Exception {
400 if (logger.isDebugEnabled()) {
401 logger.debug(testBanner(testName, CLASS_NAME));
404 setupUpdateWithWrongXmlSchema(testName, logger);
406 // Submit the request to the service and store the response.
407 String method = REQUEST_TYPE.httpMethodName();
408 String url = getResourceURL(knownResourceId);
409 String mediaType = MediaType.APPLICATION_XML;
410 final String entity = WRONG_XML_SCHEMA_DATA;
411 int statusCode = submitRequest(method, url, mediaType, entity);
413 // Check the status code of the response: does it match
414 // the expected response(s)?
415 if(logger.isDebugEnabled()){
416 logger.debug(testName + ": url=" + url +
417 " status=" + statusCode);
419 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
420 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
421 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);