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.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.collectionspace.services.client.CollectionSpaceClient;
30 import org.collectionspace.services.client.IntakeClient;
31 import org.collectionspace.services.intake.IntakesCommon;
32 import org.collectionspace.services.intake.IntakesCommonList;
33 import org.collectionspace.services.jaxb.AbstractCommonList;
35 import org.jboss.resteasy.client.ClientResponse;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
37 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
38 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
39 import org.testng.Assert;
40 //import org.testng.annotations.AfterClass;
41 import org.testng.annotations.Test;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
47 * FIXME: http://issues.collectionspace.org/browse/CSPACE-1685
48 * IntakeServiceTest, carries out tests against a
49 * deployed and running Intake Service.
51 * $LastChangedRevision$
54 public class IntakeServiceTest extends AbstractServiceTestImpl {
57 private final Logger logger =
58 LoggerFactory.getLogger(IntakeServiceTest.class);
60 // Instance variables specific to this test.
61 /** The SERVIC e_ pat h_ component. */
62 final String SERVICE_PATH_COMPONENT = "intakes";
64 /** The known resource id. */
65 private String knownResourceId = null;
68 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
71 protected CollectionSpaceClient getClientInstance() {
72 return new IntakeClient();
76 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
79 protected AbstractCommonList getAbstractCommonList(
80 ClientResponse<AbstractCommonList> response) {
81 return response.getEntity(IntakesCommonList.class);
84 // ---------------------------------------------------------------
85 // CRUD tests : CREATE tests
86 // ---------------------------------------------------------------
89 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
92 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class)
93 public void create(String testName) throws Exception {
95 // Perform setup, such as initializing the type of service request
96 // (e.g. CREATE, DELETE), its valid and expected status codes, and
97 // its associated HTTP method name (e.g. POST, DELETE).
98 setupCreate(testName);
100 // Submit the request to the service and store the response.
101 IntakeClient client = new IntakeClient();
102 String identifier = createIdentifier();
103 MultipartOutput multipart = createIntakeInstance(identifier);
104 ClientResponse<Response> res = client.create(multipart);
106 int statusCode = res.getStatus();
108 // Check the status code of the response: does it match
109 // the expected response(s)?
112 // Does it fall within the set of valid status codes?
113 // Does it exactly match the expected status code?
114 if(logger.isDebugEnabled()){
115 logger.debug(testName + ": status = " + statusCode);
117 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
118 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
119 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
121 // Store the ID returned from the first resource created
122 // for additional tests below.
123 if (knownResourceId == null){
124 knownResourceId = extractId(res);
125 if (logger.isDebugEnabled()) {
126 logger.debug(testName + ": knownResourceId=" + knownResourceId);
130 // Store the IDs from every resource created by tests,
131 // so they can be deleted after tests have been run.
132 allResourceIdsCreated.add(extractId(res));
136 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
139 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
140 dependsOnMethods = {"create"})
141 public void createList(String testName) throws Exception {
142 for(int i = 0; i < 3; i++){
148 // Placeholders until the three tests below can be uncommented.
149 // See Issue CSPACE-401.
151 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithEmptyEntityBody(java.lang.String)
154 public void createWithEmptyEntityBody(String testName) throws Exception {
155 //Should this really be empty?
159 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithMalformedXml(java.lang.String)
162 public void createWithMalformedXml(String testName) throws Exception {
163 //Should this really be empty?
167 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createWithWrongXmlSchema(java.lang.String)
170 public void createWithWrongXmlSchema(String testName) throws Exception {
171 //Should this really be empty?
176 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
177 dependsOnMethods = {"create", "testSubmitRequest"})
178 public void createWithEmptyEntityBody(String testName) throws Exception {
181 setupCreateWithEmptyEntityBody(testName);
183 // Submit the request to the service and store the response.
184 String method = REQUEST_TYPE.httpMethodName();
185 String url = getServiceRootURL();
186 String mediaType = MediaType.APPLICATION_XML;
187 final String entity = "";
188 int statusCode = submitRequest(method, url, mediaType, entity);
190 // Check the status code of the response: does it match
191 // the expected response(s)?
192 if(logger.isDebugEnabled()){
193 logger.debug("createWithEmptyEntityBody url=" + url +
194 " status=" + statusCode);
196 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
197 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
198 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
202 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
203 dependsOnMethods = {"create", "testSubmitRequest"})
204 public void createWithMalformedXml(String testName) throws Exception {
207 setupCreateWithMalformedXml(testName);
209 // Submit the request to the service and store the response.
210 String method = REQUEST_TYPE.httpMethodName();
211 String url = getServiceRootURL();
212 String mediaType = MediaType.APPLICATION_XML;
213 final String entity = MALFORMED_XML_DATA; // Constant from base class.
214 int statusCode = submitRequest(method, url, mediaType, entity);
216 // Check the status code of the response: does it match
217 // the expected response(s)?
218 if(logger.isDebugEnabled()){
219 logger.debug(testName + ": url=" + url +
220 " status=" + statusCode);
222 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
223 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
224 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
228 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
229 dependsOnMethods = {"create", "testSubmitRequest"})
230 public void createWithWrongXmlSchema(String testName) throws Exception {
233 setupCreateWithWrongXmlSchema(testName);
235 // Submit the request to the service and store the response.
236 String method = REQUEST_TYPE.httpMethodName();
237 String url = getServiceRootURL();
238 String mediaType = MediaType.APPLICATION_XML;
239 final String entity = WRONG_XML_SCHEMA_DATA;
240 int statusCode = submitRequest(method, url, mediaType, entity);
242 // Check the status code of the response: does it match
243 // the expected response(s)?
244 if(logger.isDebugEnabled()){
245 logger.debug(testName + ": url=" + url +
246 " status=" + statusCode);
248 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
249 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
250 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
254 // ---------------------------------------------------------------
255 // CRUD tests : READ tests
256 // ---------------------------------------------------------------
259 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
262 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
263 dependsOnMethods = {"create"})
264 public void read(String testName) throws Exception {
269 // Submit the request to the service and store the response.
270 IntakeClient client = new IntakeClient();
271 ClientResponse<MultipartInput> res = client.read(knownResourceId);
272 int statusCode = res.getStatus();
274 // Check the status code of the response: does it match
275 // the expected response(s)?
276 if(logger.isDebugEnabled()){
277 logger.debug(testName + ": status = " + statusCode);
279 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
280 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
281 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
283 MultipartInput input = (MultipartInput) res.getEntity();
284 IntakesCommon intake = (IntakesCommon) extractPart(input,
285 client.getCommonPartName(), IntakesCommon.class);
286 Assert.assertNotNull(intake);
291 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
294 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
295 dependsOnMethods = {"read"})
296 public void readNonExistent(String testName) throws Exception {
299 setupReadNonExistent(testName);
301 // Submit the request to the service and store the response.
302 IntakeClient client = new IntakeClient();
303 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
304 int statusCode = res.getStatus();
306 // Check the status code of the response: does it match
307 // the expected response(s)?
308 if(logger.isDebugEnabled()){
309 logger.debug(testName + ": status = " + statusCode);
311 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
312 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
313 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
316 // ---------------------------------------------------------------
317 // CRUD tests : READ_LIST tests
318 // ---------------------------------------------------------------
321 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
324 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
325 dependsOnMethods = {"createList", "read"})
326 public void readList(String testName) throws Exception {
329 setupReadList(testName);
331 // Submit the request to the service and store the response.
332 IntakeClient client = new IntakeClient();
333 ClientResponse<IntakesCommonList> res = client.readList();
334 IntakesCommonList list = res.getEntity();
335 int statusCode = res.getStatus();
337 // Check the status code of the response: does it match
338 // the expected response(s)?
339 if(logger.isDebugEnabled()){
340 logger.debug(testName + ": status = " + statusCode);
342 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
343 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
344 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
346 // Optionally output additional data about list members for debugging.
347 boolean iterateThroughList = false;
348 if(iterateThroughList && logger.isDebugEnabled()){
349 List<IntakesCommonList.IntakeListItem> items =
350 list.getIntakeListItem();
352 for(IntakesCommonList.IntakeListItem item : items){
353 logger.debug(testName + ": list-item[" + i + "] csid=" +
355 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
356 item.getEntryNumber());
357 logger.debug(testName + ": list-item[" + i + "] URI=" +
367 // ---------------------------------------------------------------
368 // CRUD tests : UPDATE tests
369 // ---------------------------------------------------------------
372 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
375 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
376 dependsOnMethods = {"read"})
377 public void update(String testName) throws Exception {
380 setupUpdate(testName);
382 // Retrieve the contents of a resource to update.
383 IntakeClient client = new IntakeClient();
384 ClientResponse<MultipartInput> res =
385 client.read(knownResourceId);
386 if(logger.isDebugEnabled()){
387 logger.debug(testName + ": read status = " + res.getStatus());
389 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
391 if(logger.isDebugEnabled()){
392 logger.debug("got object to update with ID: " + knownResourceId);
394 MultipartInput input = (MultipartInput) res.getEntity();
395 IntakesCommon intake = (IntakesCommon) extractPart(input,
396 client.getCommonPartName(), IntakesCommon.class);
397 Assert.assertNotNull(intake);
399 // Update the content of this resource.
400 // Update the content of this resource.
401 intake.setEntryNumber("updated-" + intake.getEntryNumber());
402 intake.setEntryDate("updated-" + intake.getEntryDate());
403 if(logger.isDebugEnabled()){
404 logger.debug("to be updated object");
405 logger.debug(objectAsXmlString(intake, IntakesCommon.class));
407 // Submit the request to the service and store the response.
408 MultipartOutput output = new MultipartOutput();
409 OutputPart commonPart = output.addPart(intake, MediaType.APPLICATION_XML_TYPE);
410 commonPart.getHeaders().add("label", client.getCommonPartName());
412 res = client.update(knownResourceId, output);
413 int statusCode = res.getStatus();
414 // Check the status code of the response: does it match the expected response(s)?
415 if(logger.isDebugEnabled()){
416 logger.debug(testName + ": status = " + statusCode);
418 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
419 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
420 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
423 input = (MultipartInput) res.getEntity();
424 IntakesCommon updatedIntake =
425 (IntakesCommon) extractPart(input,
426 client.getCommonPartName(), IntakesCommon.class);
427 Assert.assertNotNull(updatedIntake);
429 Assert.assertEquals(updatedIntake.getEntryDate(),
430 intake.getEntryDate(),
431 "Data in updated object did not match submitted data.");
436 // Placeholders until the three tests below can be uncommented.
437 // See Issue CSPACE-401.
439 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithEmptyEntityBody(java.lang.String)
442 public void updateWithEmptyEntityBody(String testName) throws Exception {
443 //Should this really be empty?
447 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithMalformedXml(java.lang.String)
450 public void updateWithMalformedXml(String testName) throws Exception {
451 //Should this really be empty?
455 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateWithWrongXmlSchema(java.lang.String)
458 public void updateWithWrongXmlSchema(String testName) throws Exception {
459 //Should this really be empty?
464 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
465 dependsOnMethods = {"create", "update", "testSubmitRequest"})
466 public void updateWithEmptyEntityBody(String testName) throws Exception {
469 setupUpdateWithEmptyEntityBody(testName);
471 // Submit the request to the service and store the response.
472 String method = REQUEST_TYPE.httpMethodName();
473 String url = getResourceURL(knownResourceId);
474 String mediaType = MediaType.APPLICATION_XML;
475 final String entity = "";
476 int statusCode = submitRequest(method, url, mediaType, entity);
478 // Check the status code of the response: does it match
479 // the expected response(s)?
480 if(logger.isDebugEnabled()){
481 logger.debug(testName + ": url=" + url +
482 " status=" + statusCode);
484 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
485 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
486 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
490 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
491 dependsOnMethods = {"create", "update", "testSubmitRequest"})
492 public void updateWithMalformedXml(String testName) throws Exception {
495 setupUpdateWithMalformedXml(testName);
497 // Submit the request to the service and store the response.
498 String method = REQUEST_TYPE.httpMethodName();
499 String url = getResourceURL(knownResourceId);
500 String mediaType = MediaType.APPLICATION_XML;
501 final String entity = MALFORMED_XML_DATA;
502 int statusCode = submitRequest(method, url, mediaType, entity);
504 // Check the status code of the response: does it match
505 // the expected response(s)?
506 if(logger.isDebugEnabled()){
507 logger.debug(testName + ": url=" + url +
508 " status=" + statusCode);
510 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
511 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
512 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
516 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
517 dependsOnMethods = {"create", "update", "testSubmitRequest"})
518 public void updateWithWrongXmlSchema(String testName) throws Exception {
521 setupUpdateWithWrongXmlSchema(testName);
523 // Submit the request to the service and store the response.
524 String method = REQUEST_TYPE.httpMethodName();
525 String url = getResourceURL(knownResourceId);
526 String mediaType = MediaType.APPLICATION_XML;
527 final String entity = WRONG_XML_SCHEMA_DATA;
528 int statusCode = submitRequest(method, url, mediaType, entity);
530 // Check the status code of the response: does it match
531 // the expected response(s)?
532 if(logger.isDebugEnabled()){
533 logger.debug(testName + ": url=" + url +
534 " status=" + statusCode);
536 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
537 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
538 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
543 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#updateNonExistent(java.lang.String)
546 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
547 dependsOnMethods = {"update", "testSubmitRequest"})
548 public void updateNonExistent(String testName) throws Exception {
551 setupUpdateNonExistent(testName);
553 // Submit the request to the service and store the response.
554 // Note: The ID used in this 'create' call may be arbitrary.
555 // The only relevant ID may be the one used in update(), below.
556 IntakeClient client = new IntakeClient();
557 MultipartOutput multipart = createIntakeInstance(NON_EXISTENT_ID);
558 ClientResponse<MultipartInput> res =
559 client.update(NON_EXISTENT_ID, multipart);
560 int statusCode = res.getStatus();
562 // Check the status code of the response: does it match
563 // the expected response(s)?
564 if(logger.isDebugEnabled()){
565 logger.debug(testName + ": status = " + statusCode);
567 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
568 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
569 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
572 // ---------------------------------------------------------------
573 // CRUD tests : DELETE tests
574 // ---------------------------------------------------------------
577 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
580 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
581 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
582 public void delete(String testName) throws Exception {
585 setupDelete(testName);
587 // Submit the request to the service and store the response.
588 IntakeClient client = new IntakeClient();
589 ClientResponse<Response> res = client.delete(knownResourceId);
590 int statusCode = res.getStatus();
592 // Check the status code of the response: does it match
593 // the expected response(s)?
594 if(logger.isDebugEnabled()){
595 logger.debug(testName + ": status = " + statusCode);
597 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
598 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
599 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
604 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
607 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTestImpl.class,
608 dependsOnMethods = {"delete"})
609 public void deleteNonExistent(String testName) throws Exception {
612 setupDeleteNonExistent(testName);
614 // Submit the request to the service and store the response.
615 IntakeClient client = new IntakeClient();
616 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
617 int statusCode = res.getStatus();
619 // Check the status code of the response: does it match
620 // the expected response(s)?
621 if(logger.isDebugEnabled()){
622 logger.debug(testName + ": status = " + statusCode);
624 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
625 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
626 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
629 // ---------------------------------------------------------------
630 // Utility tests : tests of code used in tests above
631 // ---------------------------------------------------------------
633 * Tests the code for manually submitting data that is used by several
634 * of the methods above.
636 @Test(dependsOnMethods = {"create", "read"})
637 public void testSubmitRequest() {
639 // Expected status code: 200 OK
640 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
642 // Submit the request to the service and store the response.
643 String method = ServiceRequestType.READ.httpMethodName();
644 String url = getResourceURL(knownResourceId);
645 int statusCode = submitRequest(method, url);
647 // Check the status code of the response: does it match
648 // the expected response(s)?
649 if(logger.isDebugEnabled()){
650 logger.debug("testSubmitRequest: url=" + url +
651 " status=" + statusCode);
653 Assert.assertEquals(statusCode, EXPECTED_STATUS);
657 // ---------------------------------------------------------------
658 // Utility methods used by tests above
659 // ---------------------------------------------------------------
661 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
664 public String getServicePathComponent() {
665 return SERVICE_PATH_COMPONENT;
669 * Creates the intake instance.
671 * @param identifier the identifier
672 * @return the multipart output
674 private MultipartOutput createIntakeInstance(String identifier) {
675 return createIntakeInstance(
676 "entryNumber-" + identifier,
677 "entryDate-" + identifier,
678 "depositor-" + identifier);
682 * Creates the intake instance.
684 * @param entryNumber the entry number
685 * @param entryDate the entry date
686 * @param depositor the depositor
687 * @return the multipart output
689 private MultipartOutput createIntakeInstance(String entryNumber,
692 IntakesCommon intake = new IntakesCommon();
693 intake.setEntryNumber(entryNumber);
694 intake.setEntryDate(entryDate);
695 intake.setDepositor(depositor);
696 MultipartOutput multipart = new MultipartOutput();
697 OutputPart commonPart =
698 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
699 commonPart.getHeaders().add("label", new IntakeClient().getCommonPartName());
701 if(logger.isDebugEnabled()){
702 logger.debug("to be created, intake common");
703 logger.debug(objectAsXmlString(intake, IntakesCommon.class));