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.Response;
29 import org.collectionspace.services.client.AbstractCommonListUtils;
30 import org.collectionspace.services.client.CollectionSpaceClient;
31 import org.collectionspace.services.client.ValuationcontrolClient;
32 import org.collectionspace.services.client.PayloadInputPart;
33 import org.collectionspace.services.client.PayloadOutputPart;
34 import org.collectionspace.services.client.PoxPayloadIn;
35 import org.collectionspace.services.client.PoxPayloadOut;
36 import org.collectionspace.services.jaxb.AbstractCommonList;
37 import org.collectionspace.services.valuationcontrol.ValuationcontrolsCommon;
39 import org.jboss.resteasy.client.ClientResponse;
40 import org.testng.Assert;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * ValuationcontrolServiceTest, carries out tests against a
47 * deployed and running Valuationcontrol Service.
49 public class ValuationcontrolServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, ValuationcontrolsCommon> {
52 private final String CLASS_NAME = ValuationcontrolServiceTest.class.getName();
53 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
54 // Instance variables specific to this test.
55 /** The service path component. */
56 final String SERVICE_NAME = "valuationcontrols";
57 final String SERVICE_PATH_COMPONENT = "valuationcontrols";
60 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
63 protected CollectionSpaceClient getClientInstance() {
64 return new ValuationcontrolClient();
68 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
71 protected AbstractCommonList getCommonList(
72 ClientResponse<AbstractCommonList> response) {
73 return response.getEntity(AbstractCommonList.class);
76 // ---------------------------------------------------------------
77 // CRUD tests : CREATE tests
78 // ---------------------------------------------------------------
83 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
86 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
87 public void create(String testName) throws Exception {
88 // Perform setup, such as initializing the type of service request
89 // (e.g. CREATE, DELETE), its valid and expected status codes, and
90 // its associated HTTP method name (e.g. POST, DELETE).
93 // Submit the request to the service and store the response.
94 ValuationcontrolClient client = new ValuationcontrolClient();
95 String identifier = createIdentifier();
96 PoxPayloadOut multipart = createValuationcontrolInstance(identifier);
98 Response res = client.create(multipart);
100 int statusCode = res.getStatus();
102 // Check the status code of the response: does it match
103 // the expected response(s)?
106 // Does it fall within the set of valid status codes?
107 // Does it exactly match the expected status code?
108 if (logger.isDebugEnabled()) {
109 logger.debug(testName + ": status = " + statusCode);
111 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
112 invalidStatusCodeMessage(testRequestType, statusCode));
113 Assert.assertEquals(statusCode, testExpectedStatusCode);
115 newID = extractId(res);
122 // Store the ID returned from the first resource created
123 // for additional tests below.
124 if (knownResourceId == null) {
125 knownResourceId = newID;
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(newID);
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++) {
151 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
152 dependsOnMethods = {"create", "testSubmitRequest"})
153 public void createWithEmptyEntityBody(String testName) throws Exception {
155 if (logger.isDebugEnabled()) {
156 logger.debug(testBanner(testName, CLASS_NAME));
159 setupCreateWithEmptyEntityBody();
161 // Submit the request to the service and store the response.
162 String method = REQUEST_TYPE.httpMethodName();
163 String url = getServiceRootURL();
164 String mediaType = MediaType.APPLICATION_XML;
165 final String entity = "";
166 int statusCode = submitRequest(method, url, mediaType, entity);
168 // Check the status code of the response: does it match
169 // the expected response(s)?
170 if(logger.isDebugEnabled()){
171 logger.debug("createWithEmptyEntityBody url=" + url +
172 " status=" + statusCode);
174 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
175 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
176 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
180 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
181 dependsOnMethods = {"create", "testSubmitRequest"})
182 public void createWithMalformedXml(String testName) throws Exception {
184 if (logger.isDebugEnabled()) {
185 logger.debug(testBanner(testName, CLASS_NAME));
188 setupCreateWithMalformedXml(testName, logger);
190 // Submit the request to the service and store the response.
191 String method = REQUEST_TYPE.httpMethodName();
192 String url = getServiceRootURL();
193 String mediaType = MediaType.APPLICATION_XML;
194 final String entity = MALFORMED_XML_DATA; // Constant from base class.
195 int statusCode = submitRequest(method, url, mediaType, entity);
197 // Check the status code of the response: does it match
198 // the expected response(s)?
199 if(logger.isDebugEnabled()){
200 logger.debug(testName + ": url=" + url +
201 " status=" + statusCode);
203 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
204 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
205 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
209 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
210 dependsOnMethods = {"create", "testSubmitRequest"})
211 public void createWithWrongXmlSchema(String testName) throws Exception {
213 if (logger.isDebugEnabled()) {
214 logger.debug(testBanner(testName, CLASS_NAME));
217 setupCreateWithWrongXmlSchema(testName, logger);
219 // Submit the request to the service and store the response.
220 String method = REQUEST_TYPE.httpMethodName();
221 String url = getServiceRootURL();
222 String mediaType = MediaType.APPLICATION_XML;
223 final String entity = WRONG_XML_SCHEMA_DATA;
224 int statusCode = submitRequest(method, url, mediaType, entity);
226 // Check the status code of the response: does it match
227 // the expected response(s)?
228 if(logger.isDebugEnabled()){
229 logger.debug(testName + ": url=" + url +
230 " status=" + statusCode);
232 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
233 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
234 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
238 // ---------------------------------------------------------------
239 // CRUD tests : READ tests
240 // ---------------------------------------------------------------
245 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
248 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
249 // dependsOnMethods = {"create"})
250 public void read(String testName) throws Exception {
254 // Submit the request to the service and store the response.
255 ValuationcontrolClient client = new ValuationcontrolClient();
256 ClientResponse<String> res = client.read(knownResourceId);
257 PoxPayloadIn input = null;
259 assertStatusCode(res, testName);
260 input = new PoxPayloadIn(res.getEntity());
263 res.releaseConnection();
267 // Get the common part of the response and verify that it is not null.
268 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
269 ValuationcontrolsCommon valuationcontrolCommon = null;
270 if (payloadInputPart != null) {
271 valuationcontrolCommon = (ValuationcontrolsCommon) payloadInputPart.getBody();
273 Assert.assertNotNull(valuationcontrolCommon);
275 // Check selected fields.
277 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
278 String valueNote = valuationcontrolCommon.getValueNote();
280 if (logger.isDebugEnabled()) {
281 logger.debug("UTF-8 data sent=" + getUTF8DataFragment() + "\n"
282 + "UTF-8 data received=" + valueNote);
285 Assert.assertEquals(valueNote, getUTF8DataFragment(),
286 "UTF-8 data retrieved '" + valueNote
287 + "' does not match expected data '" + getUTF8DataFragment());
293 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
296 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
297 // dependsOnMethods = {"read"})
298 public void readNonExistent(String testName) throws Exception {
300 setupReadNonExistent();
302 // Submit the request to the service and store the response.
303 ValuationcontrolClient client = new ValuationcontrolClient();
304 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
306 int statusCode = res.getStatus();
308 // Check the status code of the response: does it match
309 // the expected response(s)?
310 if (logger.isDebugEnabled()) {
311 logger.debug(testName + ": status = " + statusCode);
313 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
314 invalidStatusCodeMessage(testRequestType, statusCode));
315 Assert.assertEquals(statusCode, testExpectedStatusCode);
318 res.releaseConnection();
323 // ---------------------------------------------------------------
324 // CRUD tests : READ_LIST tests
325 // ---------------------------------------------------------------
330 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
333 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
334 // dependsOnMethods = {"createList", "read"})
335 public void readList(String testName) throws Exception {
339 // Submit the request to the service and store the response.
340 AbstractCommonList list = null;
341 ValuationcontrolClient client = new ValuationcontrolClient();
342 ClientResponse<AbstractCommonList> res = client.readList();
343 assertStatusCode(res, testName);
345 int statusCode = res.getStatus();
347 // Check the status code of the response: does it match
348 // the expected response(s)?
349 if (logger.isDebugEnabled()) {
350 logger.debug(testName + ": status = " + statusCode);
352 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
353 invalidStatusCodeMessage(testRequestType, statusCode));
354 Assert.assertEquals(statusCode, testExpectedStatusCode);
356 list = res.getEntity();
359 res.releaseConnection();
363 // Optionally output additional data about list members for debugging.
364 boolean iterateThroughList = true;
365 if(iterateThroughList && logger.isDebugEnabled()){
366 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
374 // ---------------------------------------------------------------
375 // CRUD tests : UPDATE tests
376 // ---------------------------------------------------------------
381 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
384 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
385 // dependsOnMethods = {"read"})
386 public void update(String testName) throws Exception {
390 // Retrieve the contents of a resource to update.
391 ValuationcontrolClient client = new ValuationcontrolClient();
392 ClientResponse<String> res = client.read(knownResourceId);
393 PoxPayloadIn input = null;
395 assertStatusCode(res, testName);
396 input = new PoxPayloadIn(res.getEntity());
397 if (logger.isDebugEnabled()) {
398 logger.debug("got object to update with ID: " + knownResourceId);
402 res.releaseConnection();
406 // Extract the common part from the response.
407 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
408 ValuationcontrolsCommon valuationcontrolCommon = null;
409 if (payloadInputPart != null) {
410 valuationcontrolCommon = (ValuationcontrolsCommon) payloadInputPart.getBody();
412 Assert.assertNotNull(valuationcontrolCommon);
414 // Update the content of this resource.
415 valuationcontrolCommon.setValuationcontrolRefNumber("updated-" + valuationcontrolCommon.getValuationcontrolRefNumber());
417 String valueNote = valuationcontrolCommon.getValueNote();
418 valuationcontrolCommon.setValueNote("updated-valueNote-" + valueNote);
420 if (logger.isDebugEnabled()) {
421 logger.debug("to be updated object");
422 logger.debug(objectAsXmlString(valuationcontrolCommon, ValuationcontrolsCommon.class));
427 // Submit the updated common part in an update request to the service
428 // and store the response.
429 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
430 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), valuationcontrolCommon);
431 res = client.update(knownResourceId, output);
433 assertStatusCode(res, testName);
434 int statusCode = res.getStatus();
435 // Check the status code of the response: does it match the expected response(s)?
436 if (logger.isDebugEnabled()) {
437 logger.debug(testName + ": status = " + statusCode);
439 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
440 invalidStatusCodeMessage(testRequestType, statusCode));
441 Assert.assertEquals(statusCode, testExpectedStatusCode);
442 input = new PoxPayloadIn(res.getEntity());
445 res.releaseConnection();
449 // Extract the updated common part from the response.
450 payloadInputPart = input.getPart(client.getCommonPartName());
451 ValuationcontrolsCommon updatedValuationcontrolCommon = null;
452 if (payloadInputPart != null) {
453 updatedValuationcontrolCommon = (ValuationcontrolsCommon) payloadInputPart.getBody();
455 Assert.assertNotNull(updatedValuationcontrolCommon);
457 // Check selected fields in the updated common part.
458 Assert.assertEquals(updatedValuationcontrolCommon.getValuationcontrolRefNumber(),
459 valuationcontrolCommon.getValuationcontrolRefNumber(),
460 "Data in updated object did not match submitted data.");
462 // Check the values of fields containing Unicode UTF-8 (non-Latin-1) characters.
463 String originalValueNote = valuationcontrolCommon.getValueNote();
464 String updatedValueNote = updatedValuationcontrolCommon.getValueNote();
466 Assert.assertEquals(updatedValueNote, originalValueNote,
467 "Data in updated object did not match submitted data.");
469 if(logger.isDebugEnabled()){
470 logger.debug("UTF-8 data sent=" + originalValueNote + "\n"
471 + "UTF-8 data received=" + updatedValueNote);
473 Assert.assertTrue(updatedValueNote.contains(getUTF8DataFragment()),
474 "UTF-8 data retrieved '" + updatedValueNote
475 + "' does not match expected data '" + getUTF8DataFragment());
479 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
480 // dependsOnMethods = {"update", "testSubmitRequest"})
481 public void updateNonExistent(String testName) throws Exception {
483 setupUpdateNonExistent();
485 // Submit the request to the service and store the response.
486 // Note: The ID used in this 'create' call may be arbitrary.
487 // The only relevant ID may be the one used in update(), below.
488 ValuationcontrolClient client = new ValuationcontrolClient();
489 PoxPayloadOut multipart = createValuationcontrolInstance(NON_EXISTENT_ID);
490 ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
492 int statusCode = res.getStatus();
494 // Check the status code of the response: does it match
495 // the expected response(s)?
496 if (logger.isDebugEnabled()) {
497 logger.debug(testName + ": status = " + statusCode);
499 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
500 invalidStatusCodeMessage(testRequestType, statusCode));
501 Assert.assertEquals(statusCode, testExpectedStatusCode);
504 res.releaseConnection();
509 // ---------------------------------------------------------------
510 // CRUD tests : DELETE tests
511 // ---------------------------------------------------------------
516 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
519 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
520 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
521 public void delete(String testName) throws Exception {
525 // Submit the request to the service and store the response.
526 ValuationcontrolClient client = new ValuationcontrolClient();
527 Response res = client.delete(knownResourceId);
529 int statusCode = res.getStatus();
531 // Check the status code of the response: does it match
532 // the expected response(s)?
533 if (logger.isDebugEnabled()) {
534 logger.debug(testName + ": status = " + statusCode);
536 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
537 invalidStatusCodeMessage(testRequestType, statusCode));
538 Assert.assertEquals(statusCode, testExpectedStatusCode);
549 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
552 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
553 // dependsOnMethods = {"delete"})
554 public void deleteNonExistent(String testName) throws Exception {
556 setupDeleteNonExistent();
558 // Submit the request to the service and store the response.
559 ValuationcontrolClient client = new ValuationcontrolClient();
560 Response res = client.delete(NON_EXISTENT_ID);
562 int statusCode = res.getStatus();
564 // Check the status code of the response: does it match
565 // the expected response(s)?
566 if (logger.isDebugEnabled()) {
567 logger.debug(testName + ": status = " + statusCode);
569 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
570 invalidStatusCodeMessage(testRequestType, statusCode));
571 Assert.assertEquals(statusCode, testExpectedStatusCode);
579 // ---------------------------------------------------------------
580 // Utility tests : tests of code used in tests above
581 // ---------------------------------------------------------------
584 * Tests the code for manually submitting data that is used by several
585 * of the methods above.
587 // @Test(dependsOnMethods = {"create", "read"})
588 public void testSubmitRequest() {
590 // Expected status code: 200 OK
591 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
593 // Submit the request to the service and store the response.
594 String method = ServiceRequestType.READ.httpMethodName();
595 String url = getResourceURL(knownResourceId);
596 int statusCode = submitRequest(method, url);
598 // Check the status code of the response: does it match
599 // the expected response(s)?
600 if (logger.isDebugEnabled()) {
601 logger.debug("testSubmitRequest: url=" + url
602 + " status=" + statusCode);
604 Assert.assertEquals(statusCode, EXPECTED_STATUS);
608 // ---------------------------------------------------------------
609 // Utility methods used by tests above
610 // ---------------------------------------------------------------
613 public String getServiceName() {
618 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
621 public String getServicePathComponent() {
622 return SERVICE_PATH_COMPONENT;
626 protected PoxPayloadOut createInstance(String identifier) {
627 return createValuationcontrolInstance(identifier);
631 * Creates the valuationcontrol instance.
633 * @param identifier the identifier
634 * @return the multipart output
636 //private PoxPayloadOut createValuationcontrolInstance(String identifier) {
637 // return createValuationcontrolInstance("valuationcontrolRefNumber-" + identifier);
641 * Creates the valuationcontrol instance.
643 * @param valuationcontrolRefNumber the valuationcontrol number
644 * @return the multipart output
646 private PoxPayloadOut createValuationcontrolInstance(String valuationcontrolRefNumber) {
648 ValuationcontrolsCommon valuationcontrolCommon = new ValuationcontrolsCommon();
649 valuationcontrolCommon.setValuationcontrolRefNumber(valuationcontrolRefNumber);
650 valuationcontrolCommon.setValueNote(getUTF8DataFragment());
652 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
653 PayloadOutputPart commonPart =
654 multipart.addPart(new ValuationcontrolClient().getCommonPartName(), valuationcontrolCommon);
656 if (logger.isDebugEnabled()) {
657 logger.debug("to be created, valuationcontrol common");
658 logger.debug(objectAsXmlString(valuationcontrolCommon, ValuationcontrolsCommon.class));
665 public void CRUDTests(String testName) {
666 // TODO Auto-generated method stub
671 protected PoxPayloadOut createInstance(String commonPartName,
673 PoxPayloadOut result = createValuationcontrolInstance(identifier);
678 protected ValuationcontrolsCommon updateInstance(ValuationcontrolsCommon commonPartObject) {
679 // TODO Auto-generated method stub
684 protected void compareUpdatedInstances(ValuationcontrolsCommon original,
685 ValuationcontrolsCommon updated) throws Exception {
686 // TODO Auto-generated method stub