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.
24 package org.collectionspace.services.client.test;
26 import java.util.List;
27 import javax.ws.rs.core.Response;
28 import javax.ws.rs.core.Response.Status;
30 import org.collectionspace.services.client.CollectionObjectClient;
31 import org.collectionspace.services.client.test.ServiceRequestType;
32 import org.collectionspace.services.collectionobject.CollectionObject;
33 import org.collectionspace.services.collectionobject.CollectionObjectList;
35 import org.jboss.resteasy.client.ClientResponse;
37 import org.testng.Assert;
38 import org.testng.annotations.Test;
41 * CollectionObjectServiceTest, carries out tests against a
42 * deployed and running CollectionObject Service.
44 * $LastChangedRevision$
47 public class CollectionObjectServiceTest extends AbstractServiceTest {
49 // Instance variables specific to this test.
50 private CollectionObjectClient client = new CollectionObjectClient();
51 final String SERVICE_PATH_COMPONENT = "collectionobjects";
52 private String knownResourceId = null;
54 // ---------------------------------------------------------------
55 // CRUD tests : CREATE tests
56 // ---------------------------------------------------------------
62 public void create() {
64 // Perform setup, such as initializing the type of service request
65 // (e.g. CREATE, DELETE), its valid and expected status codes, and
66 // its associated HTTP method name (e.g. POST, DELETE).
69 // Submit the request to the service and store the response.
70 String identifier = createIdentifier();
71 CollectionObject collectionObject =
72 createCollectionObjectInstance(identifier);
73 ClientResponse<Response> res = client.create(collectionObject);
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
88 // for additional tests below.
89 knownResourceId = extractId(res);
93 @Test(dependsOnMethods = {"create"})
94 public void createList() {
95 for(int i = 0; i < 3; i++){
103 @Test(dependsOnMethods = {"create"},
104 expectedExceptions = IllegalArgumentException.class)
105 public void createNull() {
106 ClientResponse<Response> res = client.create(null);
109 // Placeholders until the two tests below can be uncommented.
110 // See Issue CSPACE-401.
111 public void createWithMalformedXml() {}
112 public void createWithWrongXmlSchema() {}
116 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
117 public void createWithMalformedXml() {
120 setupCreateWithMalformedXml();
122 // Submit the request to the service and store the response.
123 String method = REQUEST_TYPE.httpMethodName();
124 String url = getServiceRootURL();
125 final String entity = MALFORMED_XML_DATA; // Constant from base class.
126 int statusCode = submitRequest(method, url, entity);
128 // Check the status code of the response: does it match
129 // the expected response(s)?
130 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
131 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
132 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
133 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
137 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
138 public void createWithWrongXmlSchema() {
141 setupCreateWithWrongXmlSchema();
143 // Submit the request to the service and store the response.
144 String method = REQUEST_TYPE.httpMethodName();
145 String url = getServiceRootURL();
146 final String entity = WRONG_XML_SCHEMA_DATA;
147 int statusCode = submitRequest(method, url, entity);
149 // Check the status code of the response: does it match
150 // the expected response(s)?
151 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
152 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
153 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
154 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
158 // ---------------------------------------------------------------
159 // CRUD tests : READ tests
160 // ---------------------------------------------------------------
165 @Test(dependsOnMethods = {"create"})
171 // Submit the request to the service and store the response.
172 ClientResponse<CollectionObject> res = client.read(knownResourceId);
173 int statusCode = res.getStatus();
175 // Check the status code of the response: does it match
176 // the expected response(s)?
177 verbose("read: status = " + statusCode);
178 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
179 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
180 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
186 @Test(dependsOnMethods = {"read"})
187 public void readNonExistent() {
190 setupReadNonExistent();
192 // Submit the request to the service and store the response.
193 ClientResponse<CollectionObject> res = client.read(NON_EXISTENT_ID);
194 int statusCode = res.getStatus();
196 // Check the status code of the response: does it match
197 // the expected response(s)?
198 verbose("readNonExistent: status = " + res.getStatus());
199 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
200 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
201 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
205 // ---------------------------------------------------------------
206 // CRUD tests : READ_LIST tests
207 // ---------------------------------------------------------------
212 @Test(dependsOnMethods = {"createList"})
213 public void readList() {
218 // Submit the request to the service and store the response.
219 ClientResponse<CollectionObjectList> res = client.readList();
220 CollectionObjectList 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<CollectionObjectList.CollectionObjectListItem> items =
234 list.getCollectionObjectListItem();
236 for(CollectionObjectList.CollectionObjectListItem item : items){
237 verbose("readList: list-item[" + i + "] csid=" +
239 verbose("readList: list-item[" + i + "] objectNumber=" +
240 item.getObjectNumber());
241 verbose("readList: list-item[" + i + "] URI=" +
254 // ---------------------------------------------------------------
255 // CRUD tests : UPDATE tests
256 // ---------------------------------------------------------------
261 @Test(dependsOnMethods = {"create"})
262 public void update() {
267 // Retrieve an existing resource that we can update.
268 ClientResponse<CollectionObject> res = client.read(knownResourceId);
269 verbose("read: status = " + res.getStatus());
270 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
271 CollectionObject collectionObject = res.getEntity();
272 verbose("Got object to update with ID: " + knownResourceId,
273 collectionObject, CollectionObject.class);
275 // Update the content of this resource.
276 collectionObject.setObjectNumber("updated-" +
277 collectionObject.getObjectNumber());
278 collectionObject.setObjectName("updated-" +
279 collectionObject.getObjectName());
281 // Submit the request to the service and store the response.
282 res = client.update(knownResourceId, collectionObject);
283 int statusCode = res.getStatus();
285 // Check the status code of the response: does it match
286 // the expected response(s)?
287 verbose("update: status = " + res.getStatus());
288 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
289 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
290 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
292 // Check the contents of the response: does it match
293 // what was submitted?
294 CollectionObject updatedObject = res.getEntity();
295 verbose("update: ", updatedObject, CollectionObject.class);
296 Assert.assertEquals(updatedObject.getObjectName(),
297 collectionObject.getObjectName(),
298 "Data in updated object did not match submitted data.");
303 // Placeholders until the two tests below can be uncommented.
304 // See Issue CSPACE-401.
305 public void updateWithMalformedXml() {}
306 public void updateWithWrongXmlSchema() {}
310 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
311 public void updateWithMalformedXml() {
314 setupUpdateWithMalformedXml();
316 // Submit the request to the service and store the response.
317 String method = REQUEST_TYPE.httpMethodName();
318 String url = getResourceURL(knownResourceId);
319 final String entity = MALFORMED_XML_DATA;
320 int statusCode = submitRequest(method, url, entity);
322 // Check the status code of the response: does it match
323 // the expected response(s)?
324 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
325 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
326 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
327 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
331 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
332 public void updateWithWrongXmlSchema() {
335 setupUpdateWithWrongXmlSchema();
337 // Submit the request to the service and store the response.
338 String method = REQUEST_TYPE.httpMethodName();
339 String url = getResourceURL(knownResourceId);
340 final String entity = WRONG_XML_SCHEMA_DATA;
341 int statusCode = submitRequest(method, url, entity);
343 // Check the status code of the response: does it match
344 // the expected response(s)?
345 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
346 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
347 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
348 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
353 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
354 public void updateNonExistent() {
357 setupUpdateNonExistent();
359 // Submit the request to the service and store the response.
360 // Note: The ID used in this 'create' call may be arbitrary.
361 // The only relevant ID may be the one used in update(), below.
362 CollectionObject collectionObject = createCollectionObjectInstance(NON_EXISTENT_ID);
363 ClientResponse<CollectionObject> res =
364 client.update(NON_EXISTENT_ID, collectionObject);
365 int statusCode = res.getStatus();
367 // Check the status code of the response: does it match
368 // the expected response(s)?
369 verbose("updateNonExistent: status = " + res.getStatus());
370 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
371 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
372 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
375 // ---------------------------------------------------------------
376 // CRUD tests : DELETE tests
377 // ---------------------------------------------------------------
382 @Test(dependsOnMethods =
383 {"create", "read", "update"})
384 public void delete() {
389 // Submit the request to the service and store the response.
390 ClientResponse<Response> res = client.delete(knownResourceId);
391 int statusCode = res.getStatus();
393 // Check the status code of the response: does it match
394 // the expected response(s)?
395 verbose("delete: status = " + res.getStatus());
396 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
397 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
398 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
404 @Test(dependsOnMethods = {"delete"})
405 public void deleteNonExistent() {
408 setupDeleteNonExistent();
410 // Submit the request to the service and store the response.
411 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
412 int statusCode = res.getStatus();
414 // Check the status code of the response: does it match
415 // the expected response(s)?
416 verbose("deleteNonExistent: status = " + res.getStatus());
417 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
418 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
419 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
423 // ---------------------------------------------------------------
424 // Utility tests : tests of code used in tests above
425 // ---------------------------------------------------------------
428 * Tests the code for manually submitting data that is used by several
429 * of the methods above.
431 @Test(dependsOnMethods = {"create", "read"})
432 public void testSubmitRequest() {
434 // Expected status code: 200 OK
435 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
437 // Submit the request to the service and store the response.
438 String method = ServiceRequestType.READ.httpMethodName();
439 String url = getResourceURL(knownResourceId);
440 int statusCode = submitRequest(method, url);
442 // Check the status code of the response: does it match
443 // the expected response(s)?
444 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
445 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
449 // ---------------------------------------------------------------
450 // Utility methods used by tests above
451 // ---------------------------------------------------------------
454 public String getServicePathComponent() {
455 // @TODO Determine if it is possible to obtain this
456 // value programmatically.
458 // We set this in an annotation in the CollectionObjectProxy
459 // interface, for instance. We also set service-specific
460 // constants in each service module, which might also
461 // return this value.
462 return SERVICE_PATH_COMPONENT;
465 private CollectionObject createCollectionObjectInstance(String identifier) {
466 CollectionObject collectionObject =
467 createCollectionObjectInstance(
468 "objectNumber-" + identifier,
469 "objectName-" + identifier);
470 return collectionObject;
473 private CollectionObject createCollectionObjectInstance(
474 String objectNumber, String objectName) {
475 CollectionObject collectionObject = new CollectionObject();
476 collectionObject.setObjectNumber(objectNumber);
477 collectionObject.setObjectName(objectName);
478 return collectionObject;