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.IntakeClient;
30 import org.collectionspace.services.intake.IntakesCommon;
31 import org.collectionspace.services.intake.IntakesCommonList;
33 import org.jboss.resteasy.client.ClientResponse;
35 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
37 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * IntakeServiceTest, carries out tests against a
46 * deployed and running Intake Service.
48 * $LastChangedRevision$
51 public class IntakeServiceTest extends AbstractServiceTest {
53 private final Logger logger =
54 LoggerFactory.getLogger(IntakeServiceTest.class);
56 // Instance variables specific to this test.
57 private IntakeClient client = new IntakeClient();
58 final String SERVICE_PATH_COMPONENT = "intakes";
59 private String knownResourceId = null;
61 // ---------------------------------------------------------------
62 // CRUD tests : CREATE tests
63 // ---------------------------------------------------------------
66 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
67 public void create(String testName) throws Exception {
69 // Perform setup, such as initializing the type of service request
70 // (e.g. CREATE, DELETE), its valid and expected status codes, and
71 // its associated HTTP method name (e.g. POST, DELETE).
72 setupCreate(testName);
74 // Submit the request to the service and store the response.
75 String identifier = createIdentifier();
77 MultipartOutput multipart = createIntakeInstance(identifier);
78 ClientResponse<Response> res = client.create(multipart);
80 int statusCode = res.getStatus();
82 // Check the status code of the response: does it match
83 // the expected response(s)?
86 // Does it fall within the set of valid status codes?
87 // Does it exactly match the expected status code?
88 if(logger.isDebugEnabled()){
89 logger.debug(testName + ": status = " + statusCode);
91 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
92 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
93 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
95 // Store the ID returned from this create operation
96 // for additional tests below.
97 knownResourceId = extractId(res);
98 if(logger.isDebugEnabled()){
99 logger.debug(testName + ": knownResourceId=" + knownResourceId);
104 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
105 dependsOnMethods = {"create"})
106 public void createList(String testName) throws Exception {
107 for(int i = 0; i < 3; i++){
113 // Placeholders until the three tests below can be uncommented.
114 // See Issue CSPACE-401.
116 public void createWithEmptyEntityBody(String testName) throws Exception {
120 public void createWithMalformedXml(String testName) throws Exception {
124 public void createWithWrongXmlSchema(String testName) throws Exception {
129 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
130 dependsOnMethods = {"create", "testSubmitRequest"})
131 public void createWithEmptyEntityBody(String testName) throws Exception {
134 setupCreateWithEmptyEntityBody(testName);
136 // Submit the request to the service and store the response.
137 String method = REQUEST_TYPE.httpMethodName();
138 String url = getServiceRootURL();
139 String mediaType = MediaType.APPLICATION_XML;
140 final String entity = "";
141 int statusCode = submitRequest(method, url, mediaType, entity);
143 // Check the status code of the response: does it match
144 // the expected response(s)?
145 if(logger.isDebugEnabled()){
146 logger.debug("createWithEmptyEntityBody url=" + url +
147 " status=" + statusCode);
149 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
150 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
151 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
155 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
156 dependsOnMethods = {"create", "testSubmitRequest"})
157 public void createWithMalformedXml(String testName) throws Exception {
160 setupCreateWithMalformedXml(testName);
162 // Submit the request to the service and store the response.
163 String method = REQUEST_TYPE.httpMethodName();
164 String url = getServiceRootURL();
165 String mediaType = MediaType.APPLICATION_XML;
166 final String entity = MALFORMED_XML_DATA; // Constant from base class.
167 int statusCode = submitRequest(method, url, mediaType, entity);
169 // Check the status code of the response: does it match
170 // the expected response(s)?
171 if(logger.isDebugEnabled()){
172 logger.debug(testName + ": url=" + url +
173 " status=" + statusCode);
175 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
176 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
177 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
181 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
182 dependsOnMethods = {"create", "testSubmitRequest"})
183 public void createWithWrongXmlSchema(String testName) throws Exception {
186 setupCreateWithWrongXmlSchema(testName);
188 // Submit the request to the service and store the response.
189 String method = REQUEST_TYPE.httpMethodName();
190 String url = getServiceRootURL();
191 String mediaType = MediaType.APPLICATION_XML;
192 final String entity = WRONG_XML_SCHEMA_DATA;
193 int statusCode = submitRequest(method, url, mediaType, entity);
195 // Check the status code of the response: does it match
196 // the expected response(s)?
197 if(logger.isDebugEnabled()){
198 logger.debug(testName + ": url=" + url +
199 " status=" + statusCode);
201 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
202 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
203 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
207 // ---------------------------------------------------------------
208 // CRUD tests : READ tests
209 // ---------------------------------------------------------------
212 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
213 dependsOnMethods = {"create"})
214 public void read(String testName) throws Exception {
219 // Submit the request to the service and store the response.
220 ClientResponse<MultipartInput> res = client.read(knownResourceId);
221 int statusCode = res.getStatus();
223 // Check the status code of the response: does it match
224 // the expected response(s)?
225 if(logger.isDebugEnabled()){
226 logger.debug(testName + ": status = " + statusCode);
228 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
229 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
230 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
232 MultipartInput input = (MultipartInput) res.getEntity();
233 IntakesCommon intake = (IntakesCommon) extractPart(input,
234 client.getCommonPartName(), IntakesCommon.class);
235 Assert.assertNotNull(intake);
240 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
241 dependsOnMethods = {"read"})
242 public void readNonExistent(String testName) throws Exception {
245 setupReadNonExistent(testName);
247 // Submit the request to the service and store the response.
248 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
249 int statusCode = res.getStatus();
251 // Check the status code of the response: does it match
252 // the expected response(s)?
253 if(logger.isDebugEnabled()){
254 logger.debug(testName + ": status = " + statusCode);
256 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
257 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
258 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
261 // ---------------------------------------------------------------
262 // CRUD tests : READ_LIST tests
263 // ---------------------------------------------------------------
266 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
267 dependsOnMethods = {"read"})
268 public void readList(String testName) throws Exception {
271 setupReadList(testName);
273 // Submit the request to the service and store the response.
274 ClientResponse<IntakesCommonList> res = client.readList();
275 IntakesCommonList list = res.getEntity();
276 int statusCode = res.getStatus();
278 // Check the status code of the response: does it match
279 // the expected response(s)?
280 if(logger.isDebugEnabled()){
281 logger.debug(testName + ": status = " + statusCode);
283 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
284 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
285 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
287 // Optionally output additional data about list members for debugging.
288 boolean iterateThroughList = false;
289 if(iterateThroughList && logger.isDebugEnabled()){
290 List<IntakesCommonList.IntakeListItem> items =
291 list.getIntakeListItem();
293 for(IntakesCommonList.IntakeListItem item : items){
294 logger.debug(testName + ": list-item[" + i + "] csid=" +
296 logger.debug(testName + ": list-item[" + i + "] objectNumber=" +
297 item.getEntryNumber());
298 logger.debug(testName + ": list-item[" + i + "] URI=" +
308 // ---------------------------------------------------------------
309 // CRUD tests : UPDATE tests
310 // ---------------------------------------------------------------
313 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
314 dependsOnMethods = {"read"})
315 public void update(String testName) throws Exception {
318 setupUpdate(testName);
320 ClientResponse<MultipartInput> res =
321 client.read(knownResourceId);
322 if(logger.isDebugEnabled()){
323 logger.debug(testName + ": read status = " + res.getStatus());
325 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
327 if(logger.isDebugEnabled()){
328 logger.debug("got object to update with ID: " + knownResourceId);
330 MultipartInput input = (MultipartInput) res.getEntity();
331 IntakesCommon intake = (IntakesCommon) extractPart(input,
332 client.getCommonPartName(), IntakesCommon.class);
333 Assert.assertNotNull(intake);
335 // Update the content of this resource.
336 // Update the content of this resource.
337 intake.setEntryNumber("updated-" + intake.getEntryNumber());
338 intake.setEntryDate("updated-" + intake.getEntryDate());
339 if(logger.isDebugEnabled()){
340 logger.debug("to be updated object");
341 logger.debug(objectAsXmlString(intake, IntakesCommon.class));
343 // Submit the request to the service and store the response.
344 MultipartOutput output = new MultipartOutput();
345 OutputPart commonPart = output.addPart(intake, MediaType.APPLICATION_XML_TYPE);
346 commonPart.getHeaders().add("label", client.getCommonPartName());
348 res = client.update(knownResourceId, output);
349 int statusCode = res.getStatus();
350 // Check the status code of the response: does it match the expected response(s)?
351 if(logger.isDebugEnabled()){
352 logger.debug(testName + ": status = " + statusCode);
354 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
355 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
356 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
359 input = (MultipartInput) res.getEntity();
360 IntakesCommon updatedIntake =
361 (IntakesCommon) extractPart(input,
362 client.getCommonPartName(), IntakesCommon.class);
363 Assert.assertNotNull(updatedIntake);
365 Assert.assertEquals(updatedIntake.getEntryDate(),
366 intake.getEntryDate(),
367 "Data in updated object did not match submitted data.");
372 // Placeholders until the three tests below can be uncommented.
373 // See Issue CSPACE-401.
375 public void updateWithEmptyEntityBody(String testName) throws Exception{
378 public void updateWithMalformedXml(String testName) throws Exception {
381 public void updateWithWrongXmlSchema(String testName) throws Exception {
386 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
387 dependsOnMethods = {"create", "update", "testSubmitRequest"})
388 public void updateWithEmptyEntityBody(String testName) throws Exception {
391 setupUpdateWithEmptyEntityBody(testName);
393 // Submit the request to the service and store the response.
394 String method = REQUEST_TYPE.httpMethodName();
395 String url = getResourceURL(knownResourceId);
396 String mediaType = MediaType.APPLICATION_XML;
397 final String entity = "";
398 int statusCode = submitRequest(method, url, mediaType, entity);
400 // Check the status code of the response: does it match
401 // the expected response(s)?
402 if(logger.isDebugEnabled()){
403 logger.debug(testName + ": url=" + url +
404 " status=" + statusCode);
406 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
407 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
408 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
412 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
413 dependsOnMethods = {"create", "update", "testSubmitRequest"})
414 public void updateWithMalformedXml(String testName) throws Exception {
417 setupUpdateWithMalformedXml(testName);
419 // Submit the request to the service and store the response.
420 String method = REQUEST_TYPE.httpMethodName();
421 String url = getResourceURL(knownResourceId);
422 String mediaType = MediaType.APPLICATION_XML;
423 final String entity = MALFORMED_XML_DATA;
424 int statusCode = submitRequest(method, url, mediaType, entity);
426 // Check the status code of the response: does it match
427 // the expected response(s)?
428 if(logger.isDebugEnabled()){
429 logger.debug(testName + ": url=" + url +
430 " status=" + statusCode);
432 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
433 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
434 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
438 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
439 dependsOnMethods = {"create", "update", "testSubmitRequest"})
440 public void updateWithWrongXmlSchema(String testName) throws Exception {
443 setupUpdateWithWrongXmlSchema(testName);
445 // Submit the request to the service and store the response.
446 String method = REQUEST_TYPE.httpMethodName();
447 String url = getResourceURL(knownResourceId);
448 String mediaType = MediaType.APPLICATION_XML;
449 final String entity = WRONG_XML_SCHEMA_DATA;
450 int statusCode = submitRequest(method, url, mediaType, entity);
452 // Check the status code of the response: does it match
453 // the expected response(s)?
454 if(logger.isDebugEnabled()){
455 logger.debug(testName + ": url=" + url +
456 " status=" + statusCode);
458 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
459 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
460 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
465 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
466 dependsOnMethods = {"update", "testSubmitRequest"})
467 public void updateNonExistent(String testName) throws Exception {
470 setupUpdateNonExistent(testName);
472 // Submit the request to the service and store the response.
473 // Note: The ID used in this 'create' call may be arbitrary.
474 // The only relevant ID may be the one used in update(), below.
476 // The only relevant ID may be the one used in update(), below.
477 MultipartOutput multipart = createIntakeInstance(NON_EXISTENT_ID);
478 ClientResponse<MultipartInput> res =
479 client.update(NON_EXISTENT_ID, multipart);
480 int statusCode = res.getStatus();
482 // Check the status code of the response: does it match
483 // the expected response(s)?
484 if(logger.isDebugEnabled()){
485 logger.debug(testName + ": status = " + statusCode);
487 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
488 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
489 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
492 // ---------------------------------------------------------------
493 // CRUD tests : DELETE tests
494 // ---------------------------------------------------------------
497 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
498 dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
499 public void delete(String testName) throws Exception {
502 setupDelete(testName);
504 // Submit the request to the service and store the response.
505 ClientResponse<Response> res = client.delete(knownResourceId);
506 int statusCode = res.getStatus();
508 // Check the status code of the response: does it match
509 // the expected response(s)?
510 if(logger.isDebugEnabled()){
511 logger.debug(testName + ": status = " + statusCode);
513 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
514 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
515 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
520 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
521 dependsOnMethods = {"delete"})
522 public void deleteNonExistent(String testName) throws Exception {
525 setupDeleteNonExistent(testName);
527 // Submit the request to the service and store the response.
528 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
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(REQUEST_TYPE.isValidStatusCode(statusCode),
537 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
538 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
541 // ---------------------------------------------------------------
542 // Utility tests : tests of code used in tests above
543 // ---------------------------------------------------------------
545 * Tests the code for manually submitting data that is used by several
546 * of the methods above.
548 @Test(dependsOnMethods = {"create", "read"})
549 public void testSubmitRequest() {
551 // Expected status code: 200 OK
552 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
554 // Submit the request to the service and store the response.
555 String method = ServiceRequestType.READ.httpMethodName();
556 String url = getResourceURL(knownResourceId);
557 int statusCode = submitRequest(method, url);
559 // Check the status code of the response: does it match
560 // the expected response(s)?
561 if(logger.isDebugEnabled()){
562 logger.debug("testSubmitRequest: url=" + url +
563 " status=" + statusCode);
565 Assert.assertEquals(statusCode, EXPECTED_STATUS);
569 // ---------------------------------------------------------------
570 // Utility methods used by tests above
571 // ---------------------------------------------------------------
573 public String getServicePathComponent() {
574 return SERVICE_PATH_COMPONENT;
577 private MultipartOutput createIntakeInstance(String identifier) {
578 return createIntakeInstance(
579 "entryNumber-" + identifier,
580 "entryDate-" + identifier);
583 private MultipartOutput createIntakeInstance(String entryNumber, String entryDate) {
584 IntakesCommon intake = new IntakesCommon();
585 intake.setEntryNumber(entryNumber);
586 intake.setEntryDate(entryDate);
587 MultipartOutput multipart = new MultipartOutput();
588 OutputPart commonPart =
589 multipart.addPart(intake, MediaType.APPLICATION_XML_TYPE);
590 commonPart.getHeaders().add("label", client.getCommonPartName());
592 if(logger.isDebugEnabled()){
593 logger.debug("to be created, intake common");
594 logger.debug(objectAsXmlString(intake, IntakesCommon.class));