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 org.collectionspace.services.client.AbstractCommonListUtils;
26 import org.collectionspace.services.client.CollectionSpaceClient;
27 import org.collectionspace.services.client.ValuationcontrolClient;
28 import org.collectionspace.services.client.PayloadInputPart;
29 import org.collectionspace.services.client.PayloadOutputPart;
30 import org.collectionspace.services.client.PoxPayloadIn;
31 import org.collectionspace.services.client.PoxPayloadOut;
32 import org.collectionspace.services.jaxb.AbstractCommonList;
33 import org.collectionspace.services.valuationcontrol.ValuationcontrolsCommon;
35 import javax.ws.rs.core.Response;
36 import org.testng.Assert;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * ValuationcontrolServiceTest, carries out tests against a
42 * deployed and running Valuationcontrol Service.
44 public class ValuationcontrolServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, ValuationcontrolsCommon> {
47 private final String CLASS_NAME = ValuationcontrolServiceTest.class.getName();
48 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
49 // Instance variables specific to this test.
50 /** The service path component. */
51 final String SERVICE_NAME = "valuationcontrols";
52 final String SERVICE_PATH_COMPONENT = "valuationcontrols";
55 protected CollectionSpaceClient getClientInstance() {
56 return new ValuationcontrolClient();
60 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) {
61 return new ValuationcontrolClient(clientPropertiesFilename);
65 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
68 protected AbstractCommonList getCommonList(Response response) {
69 return response.readEntity(AbstractCommonList.class);
72 // ---------------------------------------------------------------
73 // CRUD tests : CREATE tests
74 // ---------------------------------------------------------------
79 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
82 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
83 public void create(String testName) throws Exception {
84 // Perform setup, such as initializing the type of service request
85 // (e.g. CREATE, DELETE), its valid and expected status codes, and
86 // its associated HTTP method name (e.g. POST, DELETE).
89 // Submit the request to the service and store the response.
90 ValuationcontrolClient client = new ValuationcontrolClient();
91 String identifier = createIdentifier();
92 PoxPayloadOut multipart = createValuationcontrolInstance(identifier);
94 Response res = client.create(multipart);
96 int statusCode = res.getStatus();
98 // Check the status code of the response: does it match
99 // the expected response(s)?
102 // Does it fall within the set of valid status codes?
103 // Does it exactly match the expected status code?
104 if (logger.isDebugEnabled()) {
105 logger.debug(testName + ": status = " + statusCode);
107 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
108 invalidStatusCodeMessage(testRequestType, statusCode));
109 Assert.assertEquals(statusCode, testExpectedStatusCode);
111 newID = extractId(res);
118 // Store the ID returned from the first resource created
119 // for additional tests below.
120 if (knownResourceId == null) {
121 knownResourceId = newID;
122 if (logger.isDebugEnabled()) {
123 logger.debug(testName + ": knownResourceId=" + knownResourceId);
127 // Store the IDs from every resource created by tests,
128 // so they can be deleted after tests have been run.
129 allResourceIdsCreated.add(newID);
133 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
136 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
137 // dependsOnMethods = {"create"})
138 public void createList(String testName) throws Exception {
139 for (int i = 0; i < 3; i++) {
147 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
148 dependsOnMethods = {"create", "testSubmitRequest"})
149 public void createWithEmptyEntityBody(String testName) throws Exception {
151 if (logger.isDebugEnabled()) {
152 logger.debug(testBanner(testName, CLASS_NAME));
155 setupCreateWithEmptyEntityBody();
157 // Submit the request to the service and store the response.
158 String method = REQUEST_TYPE.httpMethodName();
159 String url = getServiceRootURL();
160 String mediaType = MediaType.APPLICATION_XML;
161 final String entity = "";
162 int statusCode = submitRequest(method, url, mediaType, entity);
164 // Check the status code of the response: does it match
165 // the expected response(s)?
166 if(logger.isDebugEnabled()){
167 logger.debug("createWithEmptyEntityBody url=" + url +
168 " status=" + statusCode);
170 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
171 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
172 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
176 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
177 dependsOnMethods = {"create", "testSubmitRequest"})
178 public void createWithMalformedXml(String testName) throws Exception {
180 if (logger.isDebugEnabled()) {
181 logger.debug(testBanner(testName, CLASS_NAME));
184 setupCreateWithMalformedXml(testName, logger);
186 // Submit the request to the service and store the response.
187 String method = REQUEST_TYPE.httpMethodName();
188 String url = getServiceRootURL();
189 String mediaType = MediaType.APPLICATION_XML;
190 final String entity = MALFORMED_XML_DATA; // Constant from base class.
191 int statusCode = submitRequest(method, url, mediaType, entity);
193 // Check the status code of the response: does it match
194 // the expected response(s)?
195 if(logger.isDebugEnabled()){
196 logger.debug(testName + ": url=" + url +
197 " status=" + statusCode);
199 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
200 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
201 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
205 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
206 dependsOnMethods = {"create", "testSubmitRequest"})
207 public void createWithWrongXmlSchema(String testName) throws Exception {
209 if (logger.isDebugEnabled()) {
210 logger.debug(testBanner(testName, CLASS_NAME));
213 setupCreateWithWrongXmlSchema(testName, logger);
215 // Submit the request to the service and store the response.
216 String method = REQUEST_TYPE.httpMethodName();
217 String url = getServiceRootURL();
218 String mediaType = MediaType.APPLICATION_XML;
219 final String entity = WRONG_XML_SCHEMA_DATA;
220 int statusCode = submitRequest(method, url, mediaType, entity);
222 // Check the status code of the response: does it match
223 // the expected response(s)?
224 if(logger.isDebugEnabled()){
225 logger.debug(testName + ": url=" + url +
226 " status=" + statusCode);
228 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
229 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
230 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
234 // ---------------------------------------------------------------
235 // CRUD tests : READ tests
236 // ---------------------------------------------------------------
241 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
244 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
245 // dependsOnMethods = {"create"})
246 public void read(String testName) throws Exception {
250 // Submit the request to the service and store the response.
251 ValuationcontrolClient client = new ValuationcontrolClient();
252 Response res = client.read(knownResourceId);
253 PoxPayloadIn input = null;
255 assertStatusCode(res, testName);
256 input = new PoxPayloadIn(res.readEntity(String.class));
263 // Get the common part of the response and verify that it is not null.
264 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
265 ValuationcontrolsCommon valuationcontrolCommon = null;
266 if (payloadInputPart != null) {
267 valuationcontrolCommon = (ValuationcontrolsCommon) payloadInputPart.getBody();
269 Assert.assertNotNull(valuationcontrolCommon);
271 // Check selected fields.
273 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
274 String valueNote = valuationcontrolCommon.getValueNote();
276 if (logger.isDebugEnabled()) {
277 logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
278 + "UTF-8 data received=" + valueNote);
281 Assert.assertEquals(valueNote, getUTF8DataFragment(),
282 "UTF-8 data retrieved '" + valueNote
283 + "' does not match expected data '" + getUTF8DataFragment());
289 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
292 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
293 // dependsOnMethods = {"read"})
294 public void readNonExistent(String testName) throws Exception {
296 setupReadNonExistent();
298 // Submit the request to the service and store the response.
299 ValuationcontrolClient client = new ValuationcontrolClient();
300 Response res = client.read(NON_EXISTENT_ID);
302 int statusCode = res.getStatus();
304 // Check the status code of the response: does it match
305 // the expected response(s)?
306 if (logger.isDebugEnabled()) {
307 logger.debug(testName + ": status = " + statusCode);
309 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
310 invalidStatusCodeMessage(testRequestType, statusCode));
311 Assert.assertEquals(statusCode, testExpectedStatusCode);
319 // ---------------------------------------------------------------
320 // CRUD tests : READ_LIST tests
321 // ---------------------------------------------------------------
326 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
329 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
330 // dependsOnMethods = {"createList", "read"})
331 public void readList(String testName) throws Exception {
335 // Submit the request to the service and store the response.
336 AbstractCommonList list = null;
337 ValuationcontrolClient client = new ValuationcontrolClient();
338 Response res = client.readList();
339 assertStatusCode(res, testName);
341 int statusCode = res.getStatus();
343 // Check the status code of the response: does it match
344 // the expected response(s)?
345 if (logger.isDebugEnabled()) {
346 logger.debug(testName + ": status = " + statusCode);
348 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
349 invalidStatusCodeMessage(testRequestType, statusCode));
350 Assert.assertEquals(statusCode, testExpectedStatusCode);
352 list = res.readEntity(getCommonListType());
359 // Optionally output additional data about list members for debugging.
360 boolean iterateThroughList = true;
361 if(iterateThroughList && logger.isDebugEnabled()){
362 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
370 // ---------------------------------------------------------------
371 // CRUD tests : UPDATE tests
372 // ---------------------------------------------------------------
377 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
380 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
381 // dependsOnMethods = {"read"})
382 public void update(String testName) throws Exception {
386 // Retrieve the contents of a resource to update.
387 ValuationcontrolClient client = new ValuationcontrolClient();
388 Response res = client.read(knownResourceId);
389 PoxPayloadIn input = null;
391 assertStatusCode(res, testName);
392 input = new PoxPayloadIn(res.readEntity(String.class));
393 if (logger.isDebugEnabled()) {
394 logger.debug("got object to update with ID: " + knownResourceId);
402 // Extract the common part from the response.
403 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
404 ValuationcontrolsCommon valuationcontrolCommon = null;
405 if (payloadInputPart != null) {
406 valuationcontrolCommon = (ValuationcontrolsCommon) payloadInputPart.getBody();
408 Assert.assertNotNull(valuationcontrolCommon);
410 // Update the content of this resource.
411 valuationcontrolCommon.setValuationcontrolRefNumber("updated-" + valuationcontrolCommon.getValuationcontrolRefNumber());
413 String valueNote = valuationcontrolCommon.getValueNote();
414 valuationcontrolCommon.setValueNote("updated-valueNote-" + valueNote);
416 if (logger.isDebugEnabled()) {
417 logger.debug("to be updated object");
418 logger.debug(objectAsXmlString(valuationcontrolCommon, ValuationcontrolsCommon.class));
423 // Submit the updated common part in an update request to the service
424 // and store the response.
425 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
426 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), valuationcontrolCommon);
427 res = client.update(knownResourceId, output);
429 assertStatusCode(res, testName);
430 int statusCode = res.getStatus();
431 // Check the status code of the response: does it match the expected response(s)?
432 if (logger.isDebugEnabled()) {
433 logger.debug(testName + ": status = " + statusCode);
435 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
436 invalidStatusCodeMessage(testRequestType, statusCode));
437 Assert.assertEquals(statusCode, testExpectedStatusCode);
438 input = new PoxPayloadIn(res.readEntity(String.class));
445 // Extract the updated common part from the response.
446 payloadInputPart = input.getPart(client.getCommonPartName());
447 ValuationcontrolsCommon updatedValuationcontrolCommon = null;
448 if (payloadInputPart != null) {
449 updatedValuationcontrolCommon = (ValuationcontrolsCommon) payloadInputPart.getBody();
451 Assert.assertNotNull(updatedValuationcontrolCommon);
453 // Check selected fields in the updated common part.
454 Assert.assertEquals(updatedValuationcontrolCommon.getValuationcontrolRefNumber(),
455 valuationcontrolCommon.getValuationcontrolRefNumber(),
456 "Data in updated object did not match submitted data.");
458 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
459 String originalValueNote = valuationcontrolCommon.getValueNote();
460 String updatedValueNote = updatedValuationcontrolCommon.getValueNote();
462 Assert.assertEquals(updatedValueNote, originalValueNote,
463 "Data in updated object did not match submitted data.");
465 if(logger.isDebugEnabled()){
466 logger.debug("UTF-8 data sent=" + originalValueNote + "\n"
467 + "UTF-8 data received=" + updatedValueNote);
469 Assert.assertTrue(updatedValueNote.contains(getUTF8DataFragment()),
470 "UTF-8 data retrieved '" + updatedValueNote
471 + "' does not match expected data '" + getUTF8DataFragment());
475 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
476 // dependsOnMethods = {"update", "testSubmitRequest"})
477 public void updateNonExistent(String testName) throws Exception {
479 setupUpdateNonExistent();
481 // Submit the request to the service and store the response.
482 // Note: The ID used in this 'create' call may be arbitrary.
483 // The only relevant ID may be the one used in update(), below.
484 ValuationcontrolClient client = new ValuationcontrolClient();
485 PoxPayloadOut multipart = createValuationcontrolInstance(NON_EXISTENT_ID);
486 Response res = client.update(NON_EXISTENT_ID, multipart);
488 int statusCode = res.getStatus();
490 // Check the status code of the response: does it match
491 // the expected response(s)?
492 if (logger.isDebugEnabled()) {
493 logger.debug(testName + ": status = " + statusCode);
495 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
496 invalidStatusCodeMessage(testRequestType, statusCode));
497 Assert.assertEquals(statusCode, testExpectedStatusCode);
505 // ---------------------------------------------------------------
506 // CRUD tests : DELETE tests
507 // ---------------------------------------------------------------
512 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
515 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
516 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
517 public void delete(String testName) throws Exception {
521 // Submit the request to the service and store the response.
522 ValuationcontrolClient client = new ValuationcontrolClient();
523 Response res = client.delete(knownResourceId);
525 int statusCode = res.getStatus();
527 // Check the status code of the response: does it match
528 // the expected response(s)?
529 if (logger.isDebugEnabled()) {
530 logger.debug(testName + ": status = " + statusCode);
532 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
533 invalidStatusCodeMessage(testRequestType, statusCode));
534 Assert.assertEquals(statusCode, testExpectedStatusCode);
545 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
548 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
549 // dependsOnMethods = {"delete"})
550 public void deleteNonExistent(String testName) throws Exception {
552 setupDeleteNonExistent();
554 // Submit the request to the service and store the response.
555 ValuationcontrolClient client = new ValuationcontrolClient();
556 Response res = client.delete(NON_EXISTENT_ID);
558 int statusCode = res.getStatus();
560 // Check the status code of the response: does it match
561 // the expected response(s)?
562 if (logger.isDebugEnabled()) {
563 logger.debug(testName + ": status = " + statusCode);
565 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
566 invalidStatusCodeMessage(testRequestType, statusCode));
567 Assert.assertEquals(statusCode, testExpectedStatusCode);
575 // ---------------------------------------------------------------
576 // Utility tests : tests of code used in tests above
577 // ---------------------------------------------------------------
580 * Tests the code for manually submitting data that is used by several
581 * of the methods above.
583 // @Test(dependsOnMethods = {"create", "read"})
584 public void testSubmitRequest() {
586 // Expected status code: 200 OK
587 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
589 // Submit the request to the service and store the response.
590 String method = ServiceRequestType.READ.httpMethodName();
591 String url = getResourceURL(knownResourceId);
592 int statusCode = submitRequest(method, url);
594 // Check the status code of the response: does it match
595 // the expected response(s)?
596 if (logger.isDebugEnabled()) {
597 logger.debug("testSubmitRequest: url=" + url
598 + " status=" + statusCode);
600 Assert.assertEquals(statusCode, EXPECTED_STATUS);
604 // ---------------------------------------------------------------
605 // Utility methods used by tests above
606 // ---------------------------------------------------------------
609 public String getServiceName() {
614 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
617 public String getServicePathComponent() {
618 return SERVICE_PATH_COMPONENT;
622 protected PoxPayloadOut createInstance(String identifier) {
623 return createValuationcontrolInstance(identifier);
627 * Creates the valuationcontrol instance.
629 * @param identifier the identifier
630 * @return the multipart output
632 //private PoxPayloadOut createValuationcontrolInstance(String identifier) {
633 // return createValuationcontrolInstance("valuationcontrolRefNumber-" + identifier);
637 * Creates the valuationcontrol instance.
639 * @param valuationcontrolRefNumber the valuationcontrol number
640 * @return the multipart output
642 private PoxPayloadOut createValuationcontrolInstance(String valuationcontrolRefNumber) {
644 ValuationcontrolsCommon valuationcontrolCommon = new ValuationcontrolsCommon();
645 valuationcontrolCommon.setValuationcontrolRefNumber(valuationcontrolRefNumber);
646 valuationcontrolCommon.setValueNote(getUTF8DataFragment());
648 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
649 PayloadOutputPart commonPart = multipart.addPart(new ValuationcontrolClient().getCommonPartName(),
650 valuationcontrolCommon);
652 if (logger.isDebugEnabled()) {
653 logger.debug("to be created, valuationcontrol common");
654 logger.debug(objectAsXmlString(valuationcontrolCommon, ValuationcontrolsCommon.class));
661 public void CRUDTests(String testName) {
662 // TODO Auto-generated method stub
667 protected PoxPayloadOut createInstance(String commonPartName,
669 PoxPayloadOut result = createValuationcontrolInstance(identifier);
674 protected ValuationcontrolsCommon updateInstance(ValuationcontrolsCommon commonPartObject) {
675 // TODO Auto-generated method stub
680 protected void compareUpdatedInstances(ValuationcontrolsCommon original,
681 ValuationcontrolsCommon updated) throws Exception {
682 // TODO Auto-generated method stub