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 private String knownResourceId = null;
54 // ---------------------------------------------------------------
55 // CRUD tests : CREATE tests
56 // ---------------------------------------------------------------
60 public void create() {
62 // Perform setup, such as initializing the type of service request
63 // (e.g. CREATE, DELETE), its valid and expected status codes, and
64 // its associated HTTP method name (e.g. POST, DELETE).
67 // Submit the request to the service and store the response.
68 String identifier = createIdentifier();
70 MultipartOutput multipart = createAcquisitionInstance(identifier);
71 ClientResponse<Response> res = client.create(multipart);
73 int statusCode = res.getStatus();
75 // Check the status code of the response: does it match
76 // the expected response(s)?
79 // Does it fall within the set of valid status codes?
80 // Does it exactly match the expected status code?
81 verbose("create: status = " + statusCode);
82 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
83 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
84 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
86 // Store the ID returned from this create operation for
87 // additional tests below.
88 knownResourceId = extractId(res);
89 verbose("create: knownResourceId=" + knownResourceId);
93 @Test(dependsOnMethods = {"create"})
94 public void createList() {
95 for(int i = 0; i < 3; i++){
101 // Placeholders until the three tests below can be uncommented.
102 // See Issue CSPACE-401.
103 public void createWithEmptyEntityBody() {
106 public void createWithMalformedXml() {
109 public void createWithWrongXmlSchema() {
114 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
115 public void createWithMalformedXml() {
118 setupCreateWithMalformedXml();
120 // Submit the request to the service and store the response.
121 String method = REQUEST_TYPE.httpMethodName();
122 String url = getServiceRootURL();
123 final String entity = MALFORMED_XML_DATA; // Constant from base class.
124 int statusCode = submitRequest(method, url, entity);
126 // Check the status code of the response: does it match
127 // the expected response(s)?
128 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
129 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
130 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
131 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
135 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
136 public void createWithWrongXmlSchema() {
139 setupCreateWithWrongXmlSchema();
141 // Submit the request to the service and store the response.
142 String method = REQUEST_TYPE.httpMethodName();
143 String url = getServiceRootURL();
144 final String entity = WRONG_XML_SCHEMA_DATA;
145 int statusCode = submitRequest(method, url, entity);
147 // Check the status code of the response: does it match
148 // the expected response(s)?
149 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
150 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
151 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
152 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
155 // ---------------------------------------------------------------
156 // CRUD tests : READ tests
157 // ---------------------------------------------------------------
160 @Test(dependsOnMethods = {"create"})
166 // Submit the request to the service and store the response.
167 ClientResponse<MultipartInput> res = client.read(knownResourceId);
168 int statusCode = res.getStatus();
170 // Check the status code of the response: does it match
171 // the expected response(s)?
172 verbose("read: status = " + statusCode);
173 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
174 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
175 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
176 //FIXME: remove the following try catch once Aron fixes signatures
178 MultipartInput input = (MultipartInput) res.getEntity();
179 AcquisitionsCommon acquistionObject = (AcquisitionsCommon) extractPart(input,
180 client.getCommonPartName(), AcquisitionsCommon.class);
181 Assert.assertNotNull(acquistionObject);
183 throw new RuntimeException(e);
189 @Test(dependsOnMethods = {"read"})
190 public void readNonExistent() {
193 setupReadNonExistent();
195 // Submit the request to the service and store the response.
196 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
197 int statusCode = res.getStatus();
199 // Check the status code of the response: does it match
200 // the expected response(s)?
201 verbose("readNonExistent: status = " + res.getStatus());
202 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
203 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
204 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
207 // ---------------------------------------------------------------
208 // CRUD tests : READ_LIST tests
209 // ---------------------------------------------------------------
212 @Test(dependsOnMethods = {"createList", "read"})
213 public void readList() {
218 // Submit the request to the service and store the response.
219 ClientResponse<AcquisitionsCommonList> res = client.readList();
220 AcquisitionsCommonList list = res.getEntity();
221 int statusCode = res.getStatus();
223 // Check the status code of the response: does it match
224 // the expected response(s)?
225 verbose("readList: status = " + res.getStatus());
226 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
227 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
228 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
230 // Optionally output additional data about list members for debugging.
231 boolean iterateThroughList = false;
232 if(iterateThroughList && logger.isDebugEnabled()){
233 List<AcquisitionsCommonList.AcquisitionListItem> items =
234 list.getAcquisitionListItem();
236 for(AcquisitionsCommonList.AcquisitionListItem item : items){
237 verbose("readList: list-item[" + i + "] csid=" +
239 verbose("readList: list-item[" + i + "] objectNumber=" +
240 item.getAccessionDate());
241 verbose("readList: list-item[" + i + "] URI=" +
251 // ---------------------------------------------------------------
252 // CRUD tests : UPDATE tests
253 // ---------------------------------------------------------------
256 @Test(dependsOnMethods = {"read"})
257 public void update() {
261 try{ //ideally, just remove try-catch and let the exception bubble up
262 // Retrieve an existing resource that we can update.
263 ClientResponse<MultipartInput> res =
264 client.read(knownResourceId);
265 verbose("update: read status = " + res.getStatus());
266 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
268 verbose("got object to update with ID: " + knownResourceId);
269 MultipartInput input = (MultipartInput) res.getEntity();
270 AcquisitionsCommon acquisition = (AcquisitionsCommon) extractPart(input,
271 client.getCommonPartName(), AcquisitionsCommon.class);
272 Assert.assertNotNull(acquisition);
274 // Update the content of this resource.
275 acquisition.setAccessionDate("updated-" + acquisition.getAccessionDate());
276 verbose("updated object", acquisition, AcquisitionsCommon.class);
277 // Submit the request to the service and store the response.
278 MultipartOutput output = new MultipartOutput();
279 OutputPart commonPart = output.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
280 commonPart.getHeaders().add("label", client.getCommonPartName());
282 res = client.update(knownResourceId, output);
283 int statusCode = res.getStatus();
284 // Check the status code of the response: does it match the expected response(s)?
285 verbose("update: status = " + res.getStatus());
286 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
287 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
288 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
291 input = (MultipartInput) res.getEntity();
292 AcquisitionsCommon updatedAcquisition =
293 (AcquisitionsCommon) extractPart(input,
294 client.getCommonPartName(), AcquisitionsCommon.class);
295 Assert.assertNotNull(updatedAcquisition);
297 Assert.assertEquals(updatedAcquisition.getAccessionDate(),
298 acquisition.getAccessionDate(),
299 "Data in updated object did not match submitted data.");
306 // Placeholders until the three tests below can be uncommented.
307 // See Issue CSPACE-401.
308 public void updateWithEmptyEntityBody() {
311 public void updateWithMalformedXml() {
314 public void updateWithWrongXmlSchema() {
319 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
320 public void updateWithEmptyEntityBody() {
323 setupUpdateWithEmptyEntityBody();
325 // Submit the request to the service and store the response.
326 String method = REQUEST_TYPE.httpMethodName();
327 String url = getResourceURL(knownResourceId);
328 String mediaType = MediaType.APPLICATION_XML;
329 final String entity = "";
330 int statusCode = submitRequest(method, url, mediaType, entity);
332 // Check the status code of the response: does it match
333 // the expected response(s)?
334 verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
335 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
336 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
337 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
341 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
342 public void createWithEmptyEntityBody() {
345 setupCreateWithEmptyEntityBody();
347 // Submit the request to the service and store the response.
348 String method = REQUEST_TYPE.httpMethodName();
349 String url = getServiceRootURL();
350 String mediaType = MediaType.APPLICATION_XML;
351 final String entity = "";
352 int statusCode = submitRequest(method, url, mediaType, entity);
354 // Check the status code of the response: does it match
355 // the expected response(s)?
356 verbose("createWithEmptyEntityBody url=" + url + " status=" + statusCode);
357 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
358 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
359 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
363 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
364 public void updateWithMalformedXml() {
367 setupUpdateWithMalformedXml();
369 // Submit the request to the service and store the response.
370 String method = REQUEST_TYPE.httpMethodName();
371 String url = getResourceURL(knownResourceId);
372 final String entity = MALFORMED_XML_DATA;
373 int statusCode = submitRequest(method, url, entity);
375 // Check the status code of the response: does it match
376 // the expected response(s)?
377 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
378 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
379 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
380 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
384 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
385 public void updateWithWrongXmlSchema() {
388 setupUpdateWithWrongXmlSchema();
390 // Submit the request to the service and store the response.
391 String method = REQUEST_TYPE.httpMethodName();
392 String url = getResourceURL(knownResourceId);
393 final String entity = WRONG_XML_SCHEMA_DATA;
394 int statusCode = submitRequest(method, url, entity);
396 // Check the status code of the response: does it match
397 // the expected response(s)?
398 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
399 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
400 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
401 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
405 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
406 public void updateNonExistent() {
409 setupUpdateNonExistent();
411 // Submit the request to the service and store the response.
412 // Note: The ID used in this 'create' call may be arbitrary.
413 // The only relevant ID may be the one used in update(), below.
414 MultipartOutput multipart = createAcquisitionInstance(NON_EXISTENT_ID);
415 ClientResponse<MultipartInput> res =
416 client.update(NON_EXISTENT_ID, multipart);
417 int statusCode = res.getStatus();
419 // Check the status code of the response: does it match
420 // the expected response(s)?
421 verbose("updateNonExistent: status = " + res.getStatus());
422 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
423 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
424 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
427 // ---------------------------------------------------------------
428 // CRUD tests : DELETE tests
429 // ---------------------------------------------------------------
432 @Test(dependsOnMethods = {"create", "read", "update"})
433 public void delete() {
438 // Submit the request to the service and store the response.
439 ClientResponse<Response> res = client.delete(knownResourceId);
440 int statusCode = res.getStatus();
442 // Check the status code of the response: does it match
443 // the expected response(s)?
444 verbose("delete: status = " + res.getStatus());
445 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
446 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
447 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
452 @Test(dependsOnMethods = {"delete"})
453 public void deleteNonExistent() {
456 setupDeleteNonExistent();
458 // Submit the request to the service and store the response.
459 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
460 int statusCode = res.getStatus();
462 // Check the status code of the response: does it match
463 // the expected response(s)?
464 verbose("deleteNonExistent: status = " + res.getStatus());
465 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
466 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
467 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
470 // ---------------------------------------------------------------
471 // Utility tests : tests of code used in tests above
472 // ---------------------------------------------------------------
474 * Tests the code for manually submitting data that is used by several
475 * of the methods above.
477 @Test(dependsOnMethods = {"create", "read"})
478 public void testSubmitRequest() {
480 // Expected status code: 200 OK
481 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
483 // Submit the request to the service and store the response.
484 String method = ServiceRequestType.READ.httpMethodName();
485 String url = getResourceURL(knownResourceId);
486 int statusCode = submitRequest(method, url);
488 // Check the status code of the response: does it match
489 // the expected response(s)?
490 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
491 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
495 // ---------------------------------------------------------------
496 // Utility methods used by tests above
497 // ---------------------------------------------------------------
499 public String getServicePathComponent() {
500 return client.getServicePathComponent();
504 private MultipartOutput createAcquisitionInstance(String identifier) {
505 AcquisitionsCommon acquisition = new AcquisitionsCommon();
506 acquisition.setAccessionDate("accessionDate-" + identifier);
507 MultipartOutput multipart = new MultipartOutput();
508 OutputPart commonPart = multipart.addPart(acquisition, MediaType.APPLICATION_XML_TYPE);
509 commonPart.getHeaders().add("label", client.getCommonPartName());
511 verbose("to be created, acquisition common ", acquisition, AcquisitionsCommon.class);