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.apache.commons.httpclient.methods.GetMethod;
31 import org.apache.commons.httpclient.methods.PostMethod;
32 import org.apache.commons.httpclient.methods.PutMethod;
33 import org.apache.commons.httpclient.methods.StringRequestEntity;
35 import org.collectionspace.services.client.CollectionObjectClient;
36 import org.collectionspace.services.client.test.ServiceRequestType;
37 import org.collectionspace.services.collectionobject.CollectionObject;
38 import org.collectionspace.services.collectionobject.CollectionObjectList;
40 import org.jboss.resteasy.client.ClientResponse;
42 import org.testng.Assert;
43 import org.testng.annotations.Test;
46 * CollectionObjectServiceTest, carries out tests against a
47 * deployed and running CollectionObject Service.
49 * $LastChangedRevision$
52 public class CollectionObjectServiceTest extends AbstractServiceTest {
54 // Instance variables specific to this test.
55 private CollectionObjectClient client = new CollectionObjectClient();
56 final String SERVICE_PATH_COMPONENT = "collectionobjects";
57 private String knownObjectId = null;
59 // ---------------------------------------------------------------
60 // CRUD tests : CREATE tests
61 // ---------------------------------------------------------------
67 public void create() {
69 // Perform setup, such as initializing the type of service request
70 // and its valid and expected status codes.
73 // Submit the request to the service and store the response.
74 String identifier = createIdentifier();
75 CollectionObject collectionObject = createCollectionObject(identifier);
76 ClientResponse<Response> res = client.createCollectionObject(collectionObject);
77 int statusCode = res.getStatus();
79 // Check the status code of the response: does it match 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 for additional tests below.
89 knownObjectId = extractId(res);
93 @Test(dependsOnMethods = {"create"})
94 public void createMultiple() {
95 for(int i = 0; i < 3; i++){
103 @Test(dependsOnMethods = {"create"}, expectedExceptions = IllegalArgumentException.class)
104 public void createNull() {
105 ClientResponse<Response> res = client.createCollectionObject(null);
109 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
110 public void createWithMalformedXml() {
113 setupCreateWithMalformedXml();
115 // Submit the request to the service and store the response.
116 String url = getServiceRootURL();
117 PostMethod method = new PostMethod(url);
118 final String MALFORMED_XML_DATA =
119 "<malformed_xml>wrong schema contents</malformed_xml"; // Note: intentionally missing bracket.
120 StringRequestEntity entity = getXmlEntity(MALFORMED_XML_DATA);
121 int statusCode = submitRequest(method, entity);
123 // Check the status code of the response: does it match the expected response(s)?
124 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
125 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
126 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
127 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
131 @Test(dependsOnMethods = {"create", "testSubmitRequest"}) //, "createWithMalformedXml"})
132 public void createWithWrongXmlSchema() {
135 setupCreateWithWrongXmlSchema();
137 // Submit the request to the service and store the response.
138 String url = getServiceRootURL();
139 PostMethod method = new PostMethod(url);
140 final String WRONG_SCHEMA_DATA = "<wrong_schema>wrong schema contents</wrong_schema>";
141 StringRequestEntity entity = getXmlEntity(WRONG_SCHEMA_DATA);
142 int statusCode = submitRequest(method, entity);
144 // Check the status code of the response: does it match the expected response(s)?
145 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
146 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
147 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
148 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
151 // ---------------------------------------------------------------
152 // CRUD tests : READ tests
153 // ---------------------------------------------------------------
158 @Test(dependsOnMethods = {"create"})
164 // Submit the request to the service and store the response.
165 ClientResponse<CollectionObject> res =
166 client.getCollectionObject(knownObjectId);
167 int statusCode = res.getStatus();
169 // Check the status code of the response: does it match the expected response(s)?
170 verbose("read: status = " + statusCode);
171 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
172 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
173 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
177 @Test(dependsOnMethods = {"read"})
178 public void readNonExistent() {
181 setupReadNonExistent();
183 // Submit the request to the service and store the response.
184 ClientResponse<CollectionObject> res =
185 client.getCollectionObject(NON_EXISTENT_ID);
186 int statusCode = res.getStatus();
188 // Check the status code of the response: does it match the expected response(s)?
189 verbose("readNonExistent: status = " + res.getStatus());
190 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
191 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
192 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
196 // ---------------------------------------------------------------
197 // CRUD tests : READ (list, or multiple) tests
198 // ---------------------------------------------------------------
203 @Test(dependsOnMethods = {"createMultiple"})
204 public void readList() {
209 // Submit the request to the service and store the response.
210 ClientResponse<CollectionObjectList> res = client.getCollectionObjectList();
211 CollectionObjectList coList = res.getEntity();
212 int statusCode = res.getStatus();
214 // Check the status code of the response: does it match the expected response(s)?
215 verbose("readList: status = " + res.getStatus());
216 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
217 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
218 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
220 // Optionally output additional data about list members for debugging.
221 boolean iterateThroughList = false;
222 if (iterateThroughList && logger.isDebugEnabled()) {
223 List<CollectionObjectList.CollectionObjectListItem> coItemList =
224 coList.getCollectionObjectListItem();
226 for(CollectionObjectList.CollectionObjectListItem pli : coItemList){
227 verbose("readList: list-item[" + i + "] csid=" + pli.getCsid());
228 verbose("readList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber());
229 verbose("readList: list-item[" + i + "] URI=" + pli.getUri());
241 // ---------------------------------------------------------------
242 // CRUD tests : UPDATE tests
243 // ---------------------------------------------------------------
248 @Test(dependsOnMethods = {"create"})
249 public void update() {
254 // Retrieve an existing resource that we can update.
255 ClientResponse<CollectionObject> res =
256 client.getCollectionObject(knownObjectId);
257 verbose("read: status = " + res.getStatus());
258 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
259 CollectionObject collectionObject = res.getEntity();
260 verbose("Got object to update with ID: " + knownObjectId,
261 collectionObject, CollectionObject.class);
263 // Update the content of this resource.
264 //collectionObject.setCsid("updated-" + knownObjectId);
265 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
266 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
268 // Submit the request to the service and store the response.
269 res = client.updateCollectionObject(knownObjectId, collectionObject);
270 int statusCode = res.getStatus();
271 CollectionObject updatedCollectionObject = res.getEntity();
273 // Check the status code of the response: does it match the expected response(s)?
274 verbose("update: status = " + res.getStatus());
275 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
276 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
277 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
279 // Check the contents of the response: does it match what was submitted?
280 verbose("update: ", updatedCollectionObject, CollectionObject.class);
281 Assert.assertEquals(updatedCollectionObject.getObjectName(),
282 collectionObject.getObjectName(), "Data in updated object did not match submitted data.");
286 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
287 public void updateWithMalformedXml() {
290 setupUpdateWithMalformedXml();
292 // Submit the request to the service and store the response.
293 String url = getResourceURL(knownObjectId);
294 PutMethod method = new PutMethod(url);
295 final String MALFORMED_XML_DATA =
296 "<malformed_xml>wrong schema contents</malformed_xml"; // Note: intentionally missing bracket.
297 StringRequestEntity entity = getXmlEntity(MALFORMED_XML_DATA);
298 int statusCode = submitRequest(method, entity);
300 // Check the status code of the response: does it match the expected response(s)?
301 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
302 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
303 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
304 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
308 @Test(dependsOnMethods = {"create", "testSubmitRequest"}) // , "createWithMalformedXml"})
309 public void updateWithWrongXmlSchema() {
312 setupUpdateWithWrongXmlSchema();
314 // Submit the request to the service and store the response.
315 String url = getResourceURL(knownObjectId);
316 PutMethod method = new PutMethod(url);
317 final String WRONG_SCHEMA_DATA = "<wrong_schema>wrong schema contents</wrong_schema>";
318 StringRequestEntity entity = getXmlEntity(WRONG_SCHEMA_DATA);
319 int statusCode = submitRequest(method, entity);
321 // Check the status code of the response: does it match the expected response(s)?
322 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
323 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
324 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
325 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
329 @Test(dependsOnMethods = {"update"})
330 public void updateNonExistent() {
333 setupUpdateNonExistent();
335 // Submit the request to the service and store the response.
336 // Note: The ID used in this 'create' call may be arbitrary.
337 // The only relevant ID may be the one used in updateCollectionObject(), below.
338 CollectionObject collectionObject = createCollectionObject(NON_EXISTENT_ID);
339 ClientResponse<CollectionObject> res =
340 client.updateCollectionObject(NON_EXISTENT_ID, collectionObject);
341 int statusCode = res.getStatus();
343 // Check the status code of the response: does it match the expected response(s)?
344 verbose("updateNonExistent: status = " + res.getStatus());
345 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
346 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
347 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
351 // ---------------------------------------------------------------
352 // CRUD tests : DELETE tests
353 // ---------------------------------------------------------------
358 @Test(dependsOnMethods =
359 {"create", "read", "testSubmitRequest", "update"})
360 public void delete() {
365 // Submit the request to the service and store the response.
366 ClientResponse<Response> res = client.deleteCollectionObject(knownObjectId);
367 int statusCode = res.getStatus();
369 // Check the status code of the response: does it match the expected response(s)?
370 verbose("delete: status = " + res.getStatus());
371 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
372 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
373 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
379 @Test(dependsOnMethods = {"delete"})
380 public void deleteNonExistent() {
383 setupDeleteNonExistent();
385 // Submit the request to the service and store the response.
386 ClientResponse<Response> res =
387 client.deleteCollectionObject(NON_EXISTENT_ID);
388 int statusCode = res.getStatus();
390 // Check the status code of the response: does it match the expected response(s)?
391 verbose("deleteNonExistent: status = " + res.getStatus());
392 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
393 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
394 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
398 // ---------------------------------------------------------------
399 // Utility tests : tests of code used in tests above
400 // ---------------------------------------------------------------
403 * Tests the HttpClient-based code used to submit data, in various methods below.
405 @Test(dependsOnMethods = {"create", "read"})
406 public void testSubmitRequest() {
408 // Expected status code: 200 OK
409 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
411 // Submit the request to the service and store the response.
412 String url = getResourceURL(knownObjectId);
413 GetMethod method = new GetMethod(url);
414 int statusCode = submitRequest(method);
416 // Check the status code of the response: does it match the expected response(s)?
417 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
418 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
423 // ---------------------------------------------------------------
424 // Utility methods used by tests above
425 // ---------------------------------------------------------------
428 public String getServicePathComponent() {
429 // @TODO Determine if it is possible to obtain this value programmatically.
430 // We set this in an annotation in the CollectionObjectProxy interface, for instance.
431 // We also set service-specific constants in each service module.
432 return SERVICE_PATH_COMPONENT;
435 private CollectionObject createCollectionObject(String identifier) {
436 CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier,
437 "objectName-" + identifier);
438 return collectionObject;
441 private CollectionObject createCollectionObject(String objectNumber, String objectName) {
442 CollectionObject collectionObject = new CollectionObject();
443 collectionObject.setObjectNumber(objectNumber);
444 collectionObject.setObjectName(objectName);
445 return collectionObject;