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 knownObjectId = 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 = createCollectionObject(identifier);
72 ClientResponse<Response> res = client.createCollectionObject(collectionObject);
73 int statusCode = res.getStatus();
75 // Check the status code of the response: does it match the expected response(s)?
77 // Does it fall within the set of valid status codes?
78 // Does it exactly match the expected status code?
79 verbose("create: status = " + statusCode);
80 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
81 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
82 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
84 // Store the ID returned from this create operation for additional tests below.
85 knownObjectId = extractId(res);
89 @Test(dependsOnMethods = {"create"})
90 public void createMultiple() {
91 for(int i = 0; i < 3; i++){
99 @Test(dependsOnMethods = {"create"}, expectedExceptions = IllegalArgumentException.class)
100 public void createNull() {
101 ClientResponse<Response> res = client.createCollectionObject(null);
104 // Placeholders until the two tests below can be uncommented. 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 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
120 int statusCode = submitRequest(method, url, entity);
122 // Check the status code of the response: does it match the expected response(s)?
123 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
124 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
125 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
126 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
130 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
131 public void createWithWrongXmlSchema() {
134 setupCreateWithWrongXmlSchema();
136 // Submit the request to the service and store the response.
137 String method = REQUEST_TYPE.httpMethodName();
138 String url = getServiceRootURL();
139 final String entity = WRONG_XML_SCHEMA_DATA;
140 int statusCode = submitRequest(method, url, entity);
142 // Check the status code of the response: does it match the expected response(s)?
143 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
144 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
145 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
146 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
150 // ---------------------------------------------------------------
151 // CRUD tests : READ tests
152 // ---------------------------------------------------------------
157 @Test(dependsOnMethods = {"create"})
163 // Submit the request to the service and store the response.
164 ClientResponse<CollectionObject> res =
165 client.getCollectionObject(knownObjectId);
166 int statusCode = res.getStatus();
168 // Check the status code of the response: does it match the expected response(s)?
169 verbose("read: status = " + statusCode);
170 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
171 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
172 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
176 @Test(dependsOnMethods = {"read"})
177 public void readNonExistent() {
180 setupReadNonExistent();
182 // Submit the request to the service and store the response.
183 ClientResponse<CollectionObject> res =
184 client.getCollectionObject(NON_EXISTENT_ID);
185 int statusCode = res.getStatus();
187 // Check the status code of the response: does it match the expected response(s)?
188 verbose("readNonExistent: status = " + res.getStatus());
189 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
190 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
191 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
195 // ---------------------------------------------------------------
196 // CRUD tests : READ (list, or multiple) tests
197 // ---------------------------------------------------------------
202 @Test(dependsOnMethods = {"createMultiple"})
203 public void readList() {
208 // Submit the request to the service and store the response.
209 ClientResponse<CollectionObjectList> res = client.getCollectionObjectList();
210 CollectionObjectList coList = res.getEntity();
211 int statusCode = res.getStatus();
213 // Check the status code of the response: does it match the expected response(s)?
214 verbose("readList: status = " + res.getStatus());
215 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
216 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
217 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
219 // Optionally output additional data about list members for debugging.
220 boolean iterateThroughList = false;
221 if (iterateThroughList && logger.isDebugEnabled()) {
222 List<CollectionObjectList.CollectionObjectListItem> coItemList =
223 coList.getCollectionObjectListItem();
225 for(CollectionObjectList.CollectionObjectListItem pli : coItemList){
226 verbose("readList: list-item[" + i + "] csid=" + pli.getCsid());
227 verbose("readList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber());
228 verbose("readList: list-item[" + i + "] URI=" + pli.getUri());
240 // ---------------------------------------------------------------
241 // CRUD tests : UPDATE tests
242 // ---------------------------------------------------------------
247 @Test(dependsOnMethods = {"create"})
248 public void update() {
253 // Retrieve an existing resource that we can update.
254 ClientResponse<CollectionObject> res =
255 client.getCollectionObject(knownObjectId);
256 verbose("read: status = " + res.getStatus());
257 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
258 CollectionObject collectionObject = res.getEntity();
259 verbose("Got object to update with ID: " + knownObjectId,
260 collectionObject, CollectionObject.class);
262 // Update the content of this resource.
263 //collectionObject.setCsid("updated-" + knownObjectId);
264 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
265 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
267 // Submit the request to the service and store the response.
268 res = client.updateCollectionObject(knownObjectId, collectionObject);
269 int statusCode = res.getStatus();
270 CollectionObject updatedCollectionObject = res.getEntity();
272 // Check the status code of the response: does it match the expected response(s)?
273 verbose("update: status = " + res.getStatus());
274 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
275 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
276 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
278 // Check the contents of the response: does it match what was submitted?
279 verbose("update: ", updatedCollectionObject, CollectionObject.class);
280 Assert.assertEquals(updatedCollectionObject.getObjectName(),
281 collectionObject.getObjectName(),
282 "Data in updated object did not match submitted data.");
285 // Placeholders until the two tests below can be uncommented. See Issue CSPACE-401.
286 public void updateWithMalformedXml() {}
287 public void updateWithWrongXmlSchema() {}
291 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
292 public void updateWithMalformedXml() {
295 setupUpdateWithMalformedXml();
297 // Submit the request to the service and store the response.
298 String method = REQUEST_TYPE.httpMethodName();
299 String url = getResourceURL(knownObjectId);
300 final String entity = MALFORMED_XML_DATA; // Constant from abstract base class.
301 int statusCode = submitRequest(method, url, entity);
303 // Check the status code of the response: does it match the expected response(s)?
304 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
305 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
306 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
307 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
311 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
312 public void updateWithWrongXmlSchema() {
315 setupUpdateWithWrongXmlSchema();
317 // Submit the request to the service and store the response.
318 String method = REQUEST_TYPE.httpMethodName();
319 String url = getResourceURL(knownObjectId);
320 final String entity = WRONG_XML_SCHEMA_DATA; // Constant from abstract base class.
321 int statusCode = submitRequest(method, url, entity);
323 // Check the status code of the response: does it match the expected response(s)?
324 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
325 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
326 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
327 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
333 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
334 public void updateNonExistent() {
337 setupUpdateNonExistent();
339 // Submit the request to the service and store the response.
340 // Note: The ID used in this 'create' call may be arbitrary.
341 // The only relevant ID may be the one used in updateCollectionObject(), below.
342 CollectionObject collectionObject = createCollectionObject(NON_EXISTENT_ID);
343 ClientResponse<CollectionObject> res =
344 client.updateCollectionObject(NON_EXISTENT_ID, collectionObject);
345 int statusCode = res.getStatus();
347 // Check the status code of the response: does it match the expected response(s)?
348 verbose("updateNonExistent: status = " + res.getStatus());
349 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
350 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
351 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
354 // ---------------------------------------------------------------
355 // CRUD tests : DELETE tests
356 // ---------------------------------------------------------------
361 @Test(dependsOnMethods =
362 {"create", "read", "update"})
363 public void delete() {
368 // Submit the request to the service and store the response.
369 ClientResponse<Response> res = client.deleteCollectionObject(knownObjectId);
370 int statusCode = res.getStatus();
372 // Check the status code of the response: does it match the expected response(s)?
373 verbose("delete: status = " + res.getStatus());
374 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
375 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
376 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
382 @Test(dependsOnMethods = {"delete"})
383 public void deleteNonExistent() {
386 setupDeleteNonExistent();
388 // Submit the request to the service and store the response.
389 ClientResponse<Response> res =
390 client.deleteCollectionObject(NON_EXISTENT_ID);
391 int statusCode = res.getStatus();
393 // Check the status code of the response: does it match the expected response(s)?
394 verbose("deleteNonExistent: status = " + res.getStatus());
395 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
396 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
397 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
401 // ---------------------------------------------------------------
402 // Utility tests : tests of code used in tests above
403 // ---------------------------------------------------------------
406 * Tests the code for manually submitting data that is used by several
407 * of the methods above.
409 @Test(dependsOnMethods = {"create", "read"})
410 public void testSubmitRequest() {
412 // Expected status code: 200 OK
413 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
415 // Submit the request to the service and store the response.
416 String method = ServiceRequestType.READ.httpMethodName();
417 String url = getResourceURL(knownObjectId);
418 int statusCode = submitRequest(method, url);
420 // Check the status code of the response: does it match the expected response(s)?
421 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
422 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
426 // ---------------------------------------------------------------
427 // Utility methods used by tests above
428 // ---------------------------------------------------------------
431 public String getServicePathComponent() {
432 // @TODO Determine if it is possible to obtain this value programmatically.
433 // We set this in an annotation in the CollectionObjectProxy interface, for instance.
434 // We also set service-specific constants in each service module.
435 return SERVICE_PATH_COMPONENT;
438 private CollectionObject createCollectionObject(String identifier) {
439 CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier,
440 "objectName-" + identifier);
441 return collectionObject;
444 private CollectionObject createCollectionObject(String objectNumber, String objectName) {
445 CollectionObject collectionObject = new CollectionObject();
446 collectionObject.setObjectNumber(objectNumber);
447 collectionObject.setObjectName(objectName);
448 return collectionObject;