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.AcquisitionClient;
31 import org.collectionspace.services.acquisition.AcquisitionsCommon;
32 import org.collectionspace.services.acquisition.AcquisitionsCommonList;
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;
42 * AcquisitionServiceTest, carries out tests against a
43 * deployed and running Acquisition Service.
45 * $LastChangedRevision: 621 $
46 * $LastChangedDate: 2009-09-02 16:49:01 -0700 (Wed, 02 Sep 2009) $
48 public class AcquisitionServiceTest extends AbstractServiceTest {
50 // Instance variables specific to this test.
51 private AcquisitionClient client = new AcquisitionClient();
52 final String SERVICE_PATH_COMPONENT = "acquisitions";
53 private String knownResourceId = null;
55 // ---------------------------------------------------------------
56 // CRUD tests : CREATE tests
57 // ---------------------------------------------------------------
61 public void create() {
63 // Perform setup, such as initializing the type of service request
64 // (e.g. CREATE, DELETE), its valid and expected status codes, and
65 // its associated HTTP method name (e.g. POST, DELETE).
68 // Submit the request to the service and store the response.
69 String identifier = createIdentifier();
71 MultipartOutput multipart = createAcquisitionInstance(identifier);
72 ClientResponse<Response> res = client.create(multipart);
74 int statusCode = res.getStatus();
76 // Check the status code of the response: does it match
77 // the expected response(s)?
80 // Does it fall within the set of valid status codes?
81 // Does it exactly match the expected status code?
82 verbose("create: status = " + statusCode);
83 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
84 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
85 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
87 // Store the ID returned from this create operation for
88 // additional tests below.
89 knownResourceId = extractId(res);
90 verbose("create: knownResourceId=" + knownResourceId);
94 @Test(dependsOnMethods = {"create"})
95 public void createList() {
96 for(int i = 0; i < 3; i++){
102 // Placeholders until the three tests below can be uncommented.
103 // See Issue CSPACE-401.
104 public void createWithEmptyEntityBody() {
107 public void createWithMalformedXml() {
110 public void createWithWrongXmlSchema() {
115 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
116 public void createWithMalformedXml() {
119 setupCreateWithMalformedXml();
121 // Submit the request to the service and store the response.
122 String method = REQUEST_TYPE.httpMethodName();
123 String url = getServiceRootURL();
124 final String entity = MALFORMED_XML_DATA; // Constant from base class.
125 int statusCode = submitRequest(method, url, entity);
127 // Check the status code of the response: does it match
128 // the expected response(s)?
129 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
130 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
131 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
132 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
136 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
137 public void createWithWrongXmlSchema() {
140 setupCreateWithWrongXmlSchema();
142 // Submit the request to the service and store the response.
143 String method = REQUEST_TYPE.httpMethodName();
144 String url = getServiceRootURL();
145 final String entity = WRONG_XML_SCHEMA_DATA;
146 int statusCode = submitRequest(method, url, entity);
148 // Check the status code of the response: does it match
149 // the expected response(s)?
150 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
151 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
152 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
153 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
156 // ---------------------------------------------------------------
157 // CRUD tests : READ tests
158 // ---------------------------------------------------------------
161 @Test(dependsOnMethods = {"create"})
167 // Submit the request to the service and store the response.
168 ClientResponse<MultipartInput> res = client.read(knownResourceId);
169 int statusCode = res.getStatus();
171 // Check the status code of the response: does it match
172 // the expected response(s)?
173 verbose("read: status = " + statusCode);
174 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
175 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
176 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
177 //FIXME: remove the following try catch once Aron fixes signatures
179 MultipartInput input = (MultipartInput) res.getEntity();
180 AcquisitionsCommon acquistionObject = (AcquisitionsCommon) extractPart(input,
181 getCommonPartName(), AcquisitionsCommon.class);
182 Assert.assertNotNull(acquistionObject);
184 throw new RuntimeException(e);
190 @Test(dependsOnMethods = {"read"})
191 public void readNonExistent() {
194 setupReadNonExistent();
196 // Submit the request to the service and store the response.
197 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
198 int statusCode = res.getStatus();
200 // Check the status code of the response: does it match
201 // the expected response(s)?
202 verbose("readNonExistent: status = " + res.getStatus());
203 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
204 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
205 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
208 // ---------------------------------------------------------------
209 // CRUD tests : READ_LIST tests
210 // ---------------------------------------------------------------
213 @Test(dependsOnMethods = {"createList", "read"})
214 public void readList() {
219 // Submit the request to the service and store the response.
220 ClientResponse<AcquisitionsCommonList> res = client.readList();
221 AcquisitionsCommonList list = res.getEntity();
222 int statusCode = res.getStatus();
224 // Check the status code of the response: does it match
225 // the expected response(s)?
226 verbose("readList: status = " + res.getStatus());
227 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
228 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
229 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
231 // Optionally output additional data about list members for debugging.
232 boolean iterateThroughList = false;
233 if(iterateThroughList && logger.isDebugEnabled()){
234 List<AcquisitionsCommonList.AcquisitionListItem> items =
235 list.getAcquisitionListItem();
237 for(AcquisitionsCommonList.AcquisitionListItem item : items){
238 verbose("readList: list-item[" + i + "] csid=" +
240 verbose("readList: list-item[" + i + "] objectNumber=" +
241 item.getAccessionDate());
242 verbose("readList: list-item[" + i + "] URI=" +
252 // ---------------------------------------------------------------
253 // CRUD tests : UPDATE tests
254 // ---------------------------------------------------------------
257 @Test(dependsOnMethods = {"read"})
258 public void update() {
262 try{ //ideally, just remove try-catch and let the exception bubble up
263 // Retrieve an existing resource that we can update.
264 ClientResponse<MultipartInput> res =
265 client.read(knownResourceId);
266 verbose("update: read status = " + res.getStatus());
267 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
269 verbose("got object to update with ID: " + knownResourceId);
270 MultipartInput input = (MultipartInput) res.getEntity();
271 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
272 getCommonPartName(), AcquisitionsCommon.class);
273 Assert.assertNotNull(acquisition);
275 // Update the content of this resource.
276 acquisition.setAccessionDate("updated-" + acquisition.getAccessionDate());
277 verbose("updated object", acquisition, AcquisitionsCommon.class);
278 // Submit the request to the service and store the response.
279 MultipartOutput output = new MultipartOutput();
280 OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
281 commonPart.getHeaders().add("label", getCommonPartName());
283 res = client.update(knownResourceId, output);
284 int statusCode = res.getStatus();
285 // Check the status code of the response: does it match the expected response(s)?
286 verbose("update: status = " + res.getStatus());
287 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
288 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
289 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
292 input = (MultipartInput) res.getEntity();
293 AcquisitionsCommon updatedAcquisition =
294 (AcquisitionsCommon) extractPart(input,
295 getCommonPartName(), AcquisitionsCommon.class);
296 Assert.assertNotNull(updatedAcquisition);
298 Assert.assertEquals(updatedAcquisition.getAccessionDate(),
299 acquisition.getAccessionDate(),
300 "Data in updated object did not match submitted data.");
307 // Placeholders until the three tests below can be uncommented.
308 // See Issue CSPACE-401.
309 public void updateWithEmptyEntityBody() {
312 public void updateWithMalformedXml() {
315 public void updateWithWrongXmlSchema() {
320 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
321 public void updateWithEmptyEntityBody() {
324 setupUpdateWithEmptyEntityBody();
326 // Submit the request to the service and store the response.
327 String method = REQUEST_TYPE.httpMethodName();
328 String url = getResourceURL(knownResourceId);
329 String mediaType = MediaType.APPLICATION_XML;
330 final String entity = "";
331 int statusCode = submitRequest(method, url, mediaType, entity);
333 // Check the status code of the response: does it match
334 // the expected response(s)?
335 verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
336 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
337 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
338 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
342 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
343 public void createWithEmptyEntityBody() {
346 setupCreateWithEmptyEntityBody();
348 // Submit the request to the service and store the response.
349 String method = REQUEST_TYPE.httpMethodName();
350 String url = getServiceRootURL();
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 verbose("createWithEmptyEntityBody url=" + url + " status=" + statusCode);
358 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
359 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
360 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
364 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
365 public void updateWithMalformedXml() {
368 setupUpdateWithMalformedXml();
370 // Submit the request to the service and store the response.
371 String method = REQUEST_TYPE.httpMethodName();
372 String url = getResourceURL(knownResourceId);
373 final String entity = MALFORMED_XML_DATA;
374 int statusCode = submitRequest(method, url, entity);
376 // Check the status code of the response: does it match
377 // the expected response(s)?
378 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
379 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
380 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
381 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
385 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
386 public void updateWithWrongXmlSchema() {
389 setupUpdateWithWrongXmlSchema();
391 // Submit the request to the service and store the response.
392 String method = REQUEST_TYPE.httpMethodName();
393 String url = getResourceURL(knownResourceId);
394 final String entity = WRONG_XML_SCHEMA_DATA;
395 int statusCode = submitRequest(method, url, entity);
397 // Check the status code of the response: does it match
398 // the expected response(s)?
399 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
400 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
401 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
402 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
406 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
407 public void updateNonExistent() {
410 setupUpdateNonExistent();
412 // Submit the request to the service and store the response.
413 // Note: The ID used in this 'create' call may be arbitrary.
414 // The only relevant ID may be the one used in update(), below.
415 MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
416 ClientResponse<MultipartInput> res =
417 client.update(NON_EXISTENT_ID, multipart);
418 int statusCode = res.getStatus();
420 // Check the status code of the response: does it match
421 // the expected response(s)?
422 verbose("updateNonExistent: status = " + res.getStatus());
423 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
424 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
425 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
428 // ---------------------------------------------------------------
429 // CRUD tests : DELETE tests
430 // ---------------------------------------------------------------
433 @Test(dependsOnMethods = {"create", "read", "update"})
434 public void delete() {
439 // Submit the request to the service and store the response.
440 ClientResponse<Response> res = client.delete(knownResourceId);
441 int statusCode = res.getStatus();
443 // Check the status code of the response: does it match
444 // the expected response(s)?
445 verbose("delete: status = " + res.getStatus());
446 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
447 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
448 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
453 @Test(dependsOnMethods = {"delete"})
454 public void deleteNonExistent() {
457 setupDeleteNonExistent();
459 // Submit the request to the service and store the response.
460 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
461 int statusCode = res.getStatus();
463 // Check the status code of the response: does it match
464 // the expected response(s)?
465 verbose("deleteNonExistent: status = " + res.getStatus());
466 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
467 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
468 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
471 // ---------------------------------------------------------------
472 // Utility tests : tests of code used in tests above
473 // ---------------------------------------------------------------
475 * Tests the code for manually submitting data that is used by several
476 * of the methods above.
478 @Test(dependsOnMethods = {"create", "read"})
479 public void testSubmitRequest() {
481 // Expected status code: 200 OK
482 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
484 // Submit the request to the service and store the response.
485 String method = ServiceRequestType.READ.httpMethodName();
486 String url = getResourceURL(knownResourceId);
487 int statusCode = submitRequest(method, url);
489 // Check the status code of the response: does it match
490 // the expected response(s)?
491 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
492 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
496 // ---------------------------------------------------------------
497 // Utility methods used by tests above
498 // ---------------------------------------------------------------
500 public String getServicePathComponent() {
501 return SERVICE_PATH_COMPONENT;
505 private MultipartOutput createAcquisitionInstance(String identifier) {
506 AcquisitionsCommon acquisition = new AcquisitionsCommon();
507 acquisition.setAccessionDate("accessionDate-" + identifier);
508 MultipartOutput multipart = new MultipartOutput();
509 OutputPart commonPart = multipart.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
510 commonPart.getHeaders().add("label", getCommonPartName());
512 verbose("to be created, acquisition common ", acquisition, AcquisitionsCommon.class);