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.MediaType;
28 import javax.ws.rs.core.Response;
29 import javax.ws.rs.core.Response.Status;
31 import org.collectionspace.services.client.CollectionObjectClient;
32 import org.collectionspace.services.client.test.ServiceRequestType;
33 import org.collectionspace.services.collectionobject.CollectionObject;
34 import org.collectionspace.services.collectionobject.CollectionObjectList;
36 import org.jboss.resteasy.client.ClientResponse;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
42 * CollectionObjectServiceTest, carries out tests against a
43 * deployed and running CollectionObject Service.
45 * $LastChangedRevision$
48 public class CollectionObjectServiceTest extends AbstractServiceTest {
50 // Instance variables specific to this test.
51 private CollectionObjectClient client = new CollectionObjectClient();
52 final String SERVICE_PATH_COMPONENT = "collectionobjects";
53 private String knownResourceId = null;
55 // ---------------------------------------------------------------
56 // CRUD tests : CREATE tests
57 // ---------------------------------------------------------------
63 public void create() {
65 // Perform setup, such as initializing the type of service request
66 // (e.g. CREATE, DELETE), its valid and expected status codes, and
67 // its associated HTTP method name (e.g. POST, DELETE).
70 // Submit the request to the service and store the response.
71 String identifier = createIdentifier();
72 CollectionObject collectionObject =
73 createCollectionObjectInstance(identifier);
74 ClientResponse<Response> res = client.create(collectionObject);
75 int statusCode = res.getStatus();
77 // Check the status code of the response: does it match
78 // the expected response(s)?
81 // Does it fall within the set of valid status codes?
82 // Does it exactly match the expected status code?
83 verbose("create: status = " + statusCode);
84 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
85 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
86 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
88 // Store the ID returned from this create operation
89 // for additional tests below.
90 knownResourceId = extractId(res);
94 @Test(dependsOnMethods = {"create"})
95 public void createList() {
96 for(int i = 0; i < 3; i++){
103 // Placeholders until the two tests below can be uncommented.
104 // See Issue CSPACE-401.
105 public void createWithMalformedXml() {}
106 public void createWithWrongXmlSchema() {}
110 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
111 public void createWithMalformedXml() {
114 setupCreateWithMalformedXml();
116 // Submit the request to the service and store the response.
117 String method = REQUEST_TYPE.httpMethodName();
118 String url = getServiceRootURL();
119 String mediaType = MediaType.APPLICATION_XML;
120 final String entity = MALFORMED_XML_DATA; // Constant from base class.
121 int statusCode = submitRequest(method, url, mediaType, entity);
123 // Check the status code of the response: does it match
124 // the expected response(s)?
125 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
126 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
127 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
128 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
132 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
133 public void createWithWrongXmlSchema() {
136 setupCreateWithWrongXmlSchema();
138 // Submit the request to the service and store the response.
139 String method = REQUEST_TYPE.httpMethodName();
140 String url = getServiceRootURL();
141 String mediaType = MediaType.APPLICATION_XML;
142 final String entity = WRONG_XML_SCHEMA_DATA;
143 int statusCode = submitRequest(method, url, mediaType, entity);
145 // Check the status code of the response: does it match
146 // the expected response(s)?
147 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
148 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
149 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
150 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
154 // ---------------------------------------------------------------
155 // CRUD tests : READ tests
156 // ---------------------------------------------------------------
161 @Test(dependsOnMethods = {"create"})
167 // Submit the request to the service and store the response.
168 ClientResponse<CollectionObject> 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);
182 @Test(dependsOnMethods = {"read"})
183 public void readNonExistent() {
186 setupReadNonExistent();
188 // Submit the request to the service and store the response.
189 ClientResponse<CollectionObject> res = client.read(NON_EXISTENT_ID);
190 int statusCode = res.getStatus();
192 // Check the status code of the response: does it match
193 // the expected response(s)?
194 verbose("readNonExistent: status = " + res.getStatus());
195 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
196 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
197 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
201 // ---------------------------------------------------------------
202 // CRUD tests : READ_LIST tests
203 // ---------------------------------------------------------------
208 @Test(dependsOnMethods = {"createList"})
209 public void readList() {
214 // Submit the request to the service and store the response.
215 ClientResponse<CollectionObjectList> res = client.readList();
216 CollectionObjectList list = res.getEntity();
217 int statusCode = res.getStatus();
219 // Check the status code of the response: does it match
220 // the expected response(s)?
221 verbose("readList: status = " + res.getStatus());
222 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
223 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
224 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
226 // Optionally output additional data about list members for debugging.
227 boolean iterateThroughList = false;
228 if (iterateThroughList && logger.isDebugEnabled()) {
229 List<CollectionObjectList.CollectionObjectListItem> items =
230 list.getCollectionObjectListItem();
232 for(CollectionObjectList.CollectionObjectListItem item : items){
233 verbose("readList: list-item[" + i + "] csid=" +
235 verbose("readList: list-item[" + i + "] objectNumber=" +
236 item.getObjectNumber());
237 verbose("readList: list-item[" + i + "] URI=" +
250 // ---------------------------------------------------------------
251 // CRUD tests : UPDATE tests
252 // ---------------------------------------------------------------
257 @Test(dependsOnMethods = {"create"})
258 public void update() {
263 // Retrieve an existing resource that we can update.
264 ClientResponse<CollectionObject> res = client.read(knownResourceId);
265 verbose("read: status = " + res.getStatus());
266 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
267 CollectionObject collectionObject = res.getEntity();
268 verbose("Got object to update with ID: " + knownResourceId,
269 collectionObject, CollectionObject.class);
271 // Update the content of this resource.
272 collectionObject.setObjectNumber("updated-" +
273 collectionObject.getObjectNumber());
274 collectionObject.setObjectName("updated-" +
275 collectionObject.getObjectName());
277 // Submit the request to the service and store the response.
278 res = client.update(knownResourceId, collectionObject);
279 int statusCode = res.getStatus();
281 // Check the status code of the response: does it match
282 // the expected response(s)?
283 verbose("update: status = " + res.getStatus());
284 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
285 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
286 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
288 // Check the contents of the response: does it match
289 // what was submitted?
290 CollectionObject updatedObject = res.getEntity();
291 verbose("update: ", updatedObject, CollectionObject.class);
292 Assert.assertEquals(updatedObject.getObjectName(),
293 collectionObject.getObjectName(),
294 "Data in updated object did not match submitted data.");
299 // Placeholders until the two tests below can be uncommented.
300 // See Issue CSPACE-401.
301 public void updateWithMalformedXml() {}
302 public void updateWithWrongXmlSchema() {}
306 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
307 public void updateWithMalformedXml() {
310 setupUpdateWithMalformedXml();
312 // Submit the request to the service and store the response.
313 String method = REQUEST_TYPE.httpMethodName();
314 String url = getResourceURL(knownResourceId);
315 final String entity = MALFORMED_XML_DATA;
316 String mediaType = MediaType.APPLICATION_XML;
317 int statusCode = submitRequest(method, url, mediaType, entity);
319 // Check the status code of the response: does it match
320 // the expected response(s)?
321 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
322 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
323 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
324 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
328 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
329 public void updateWithWrongXmlSchema() {
332 setupUpdateWithWrongXmlSchema();
334 // Submit the request to the service and store the response.
335 String method = REQUEST_TYPE.httpMethodName();
336 String url = getResourceURL(knownResourceId);
337 String mediaType = MediaType.APPLICATION_XML;
338 final String entity = WRONG_XML_SCHEMA_DATA;
339 int statusCode = submitRequest(method, url, mediaType, entity);
341 // Check the status code of the response: does it match
342 // the expected response(s)?
343 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
344 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
345 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
346 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
351 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
352 public void updateNonExistent() {
355 setupUpdateNonExistent();
357 // Submit the request to the service and store the response.
358 // Note: The ID used in this 'create' call may be arbitrary.
359 // The only relevant ID may be the one used in update(), below.
360 CollectionObject collectionObject = createCollectionObjectInstance(NON_EXISTENT_ID);
361 ClientResponse<CollectionObject> res =
362 client.update(NON_EXISTENT_ID, collectionObject);
363 int statusCode = res.getStatus();
365 // Check the status code of the response: does it match
366 // the expected response(s)?
367 verbose("updateNonExistent: status = " + res.getStatus());
368 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
369 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
370 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
373 // ---------------------------------------------------------------
374 // CRUD tests : DELETE tests
375 // ---------------------------------------------------------------
380 @Test(dependsOnMethods =
381 {"create", "read", "update"})
382 public void delete() {
387 // Submit the request to the service and store the response.
388 ClientResponse<Response> res = client.delete(knownResourceId);
389 int statusCode = res.getStatus();
391 // Check the status code of the response: does it match
392 // the expected response(s)?
393 verbose("delete: status = " + res.getStatus());
394 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
395 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
396 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
402 @Test(dependsOnMethods = {"delete"})
403 public void deleteNonExistent() {
406 setupDeleteNonExistent();
408 // Submit the request to the service and store the response.
409 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
410 int statusCode = res.getStatus();
412 // Check the status code of the response: does it match
413 // the expected response(s)?
414 verbose("deleteNonExistent: status = " + res.getStatus());
415 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
416 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
417 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
421 // ---------------------------------------------------------------
422 // Utility tests : tests of code used in tests above
423 // ---------------------------------------------------------------
426 * Tests the code for manually submitting data that is used by several
427 * of the methods above.
429 @Test(dependsOnMethods = {"create", "read"})
430 public void testSubmitRequest() {
432 // Expected status code: 200 OK
433 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
435 // Submit the request to the service and store the response.
436 String method = ServiceRequestType.READ.httpMethodName();
437 String url = getResourceURL(knownResourceId);
438 int statusCode = submitRequest(method, url);
440 // Check the status code of the response: does it match
441 // the expected response(s)?
442 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
443 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
447 // ---------------------------------------------------------------
448 // Utility methods used by tests above
449 // ---------------------------------------------------------------
452 public String getServicePathComponent() {
453 // @TODO Determine if it is possible to obtain this
454 // value programmatically.
456 // We set this in an annotation in the CollectionObjectProxy
457 // interface, for instance. We also set service-specific
458 // constants in each service module, which might also
459 // return this value.
460 return SERVICE_PATH_COMPONENT;
463 private CollectionObject createCollectionObjectInstance(String identifier) {
464 CollectionObject collectionObject =
465 createCollectionObjectInstance(
466 "objectNumber-" + identifier,
467 "objectName-" + identifier);
468 return collectionObject;
471 private CollectionObject createCollectionObjectInstance(
472 String objectNumber, String objectName) {
473 CollectionObject collectionObject = new CollectionObject();
474 collectionObject.setObjectNumber(objectNumber);
475 collectionObject.setObjectName(objectName);
476 return collectionObject;