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();
57 // Instance variables common to all entity service test classes.
58 private final String NON_EXISTENT_ID = createNonExistentIdentifier();
59 private String knownObjectId = null;
61 // ---------------------------------------------------------------
62 // CRUD tests : CREATE tests
63 // ---------------------------------------------------------------
69 public void create() {
71 // Perform setup, such as initializing the type of service request
72 // and its valid and expected status codes.
75 // Submit the request to the service and store the response.
76 String identifier = createIdentifier();
77 CollectionObject collectionObject = createCollectionObject(identifier);
78 ClientResponse<Response> res = client.createCollectionObject(collectionObject);
79 int statusCode = res.getStatus();
81 // Check the status code of the response: does it match the expected response(s)?
83 // Does it fall within the set of valid status codes?
84 // Does it exactly match the expected status code?
85 verbose("create: status = " + statusCode);
86 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
87 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
88 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
90 // Store the ID returned from this create operation for additional tests below.
91 knownObjectId = extractId(res);
95 @Test(dependsOnMethods = {"create"})
96 public void createMultiple() {
97 for(int i = 0; i < 3; i++){
105 @Test(dependsOnMethods = {"create"}, expectedExceptions = IllegalArgumentException.class)
106 public void createNull() {
107 ClientResponse<Response> res = client.createCollectionObject(null);
111 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
112 public void createWithMalformedXml() {
115 super.setupCreateWithMalformedXml();
117 // Submit the request to the service and store the response.
118 String url = getServiceRootURL();
119 PostMethod method = new PostMethod(url);
120 final String MALFORMED_XML_DATA =
121 "<malformed_xml>wrong schema contents</malformed_xml"; // Note: intentionally missing bracket.
122 StringRequestEntity entity = getXmlEntity(MALFORMED_XML_DATA);
123 int statusCode = submitRequest(method, entity);
125 // Check the status code of the response: does it match the expected response(s)?
126 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
127 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
128 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
129 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
133 @Test(dependsOnMethods = {"create", "testSubmitRequest"}) //, "createWithMalformedXml"})
134 public void createWithWrongXmlSchema() {
137 super.setupCreateWithWrongXmlSchema();
139 // Submit the request to the service and store the response.
140 String url = getServiceRootURL();
141 PostMethod method = new PostMethod(url);
142 final String WRONG_SCHEMA_DATA = "<wrong_schema>wrong schema contents</wrong_schema>";
143 StringRequestEntity entity = getXmlEntity(WRONG_SCHEMA_DATA);
144 int statusCode = submitRequest(method, entity);
146 // Check the status code of the response: does it match 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);
153 // ---------------------------------------------------------------
154 // CRUD tests : READ tests
155 // ---------------------------------------------------------------
160 @Test(dependsOnMethods = {"create"})
166 // Submit the request to the service and store the response.
167 ClientResponse<CollectionObject> res =
168 client.getCollectionObject(knownObjectId);
169 int statusCode = res.getStatus();
171 // Check the status code of the response: does it match 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);
179 @Test(dependsOnMethods = {"read"})
180 public void readNonExistent() {
183 super.setupReadNonExistent();
185 // Submit the request to the service and store the response.
186 ClientResponse<CollectionObject> res =
187 client.getCollectionObject(NON_EXISTENT_ID);
188 int statusCode = res.getStatus();
190 // Check the status code of the response: does it match the expected response(s)?
191 verbose("readNonExistent: status = " + res.getStatus());
192 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
193 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
194 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
198 // ---------------------------------------------------------------
199 // CRUD tests : READ (list, or multiple) tests
200 // ---------------------------------------------------------------
205 @Test(dependsOnMethods = {"createMultiple"})
206 public void readList() {
209 super.setupReadList();
211 // Submit the request to the service and store the response.
212 ClientResponse<CollectionObjectList> res = client.getCollectionObjectList();
213 CollectionObjectList coList = res.getEntity();
214 int statusCode = res.getStatus();
216 // Check the status code of the response: does it match the expected response(s)?
217 verbose("readList: status = " + res.getStatus());
218 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
219 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
220 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
222 // Optionally output additional data about list members for debugging.
223 boolean iterateThroughList = false;
224 if (iterateThroughList && logger.isDebugEnabled()) {
225 List<CollectionObjectList.CollectionObjectListItem> coItemList =
226 coList.getCollectionObjectListItem();
228 for(CollectionObjectList.CollectionObjectListItem pli : coItemList){
229 verbose("readList: list-item[" + i + "] csid=" + pli.getCsid());
230 verbose("readList: list-item[" + i + "] objectNumber=" + pli.getObjectNumber());
231 verbose("readList: list-item[" + i + "] URI=" + pli.getUri());
243 // ---------------------------------------------------------------
244 // CRUD tests : UPDATE tests
245 // ---------------------------------------------------------------
250 @Test(dependsOnMethods = {"create"})
251 public void update() {
256 // Retrieve an existing resource that we can update.
257 ClientResponse<CollectionObject> res =
258 client.getCollectionObject(knownObjectId);
259 verbose("read: status = " + res.getStatus());
260 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
261 CollectionObject collectionObject = res.getEntity();
262 verbose("Got object to update with ID: " + knownObjectId,
263 collectionObject, CollectionObject.class);
265 // Update the content of this resource.
266 //collectionObject.setCsid("updated-" + knownObjectId);
267 collectionObject.setObjectNumber("updated-" + collectionObject.getObjectNumber());
268 collectionObject.setObjectName("updated-" + collectionObject.getObjectName());
270 // Submit the request to the service and store the response.
271 res = client.updateCollectionObject(knownObjectId, collectionObject);
272 int statusCode = res.getStatus();
273 CollectionObject updatedCollectionObject = res.getEntity();
275 // Check the status code of the response: does it match the expected response(s)?
276 verbose("update: status = " + res.getStatus());
277 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
278 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
279 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
281 // Check the contents of the response: does it match what was submitted?
282 verbose("update: ", updatedCollectionObject, CollectionObject.class);
283 Assert.assertEquals(updatedCollectionObject.getObjectName(),
284 collectionObject.getObjectName(), "Data in updated object did not match submitted data.");
288 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
289 public void updateWithMalformedXml() {
292 super.setupUpdateWithMalformedXml();
294 // Submit the request to the service and store the response.
295 String url = getResourceURL(knownObjectId);
296 PutMethod method = new PutMethod(url);
297 final String MALFORMED_XML_DATA =
298 "<malformed_xml>wrong schema contents</malformed_xml"; // Note: intentionally missing bracket.
299 StringRequestEntity entity = getXmlEntity(MALFORMED_XML_DATA);
300 int statusCode = submitRequest(method, entity);
302 // Check the status code of the response: does it match the expected response(s)?
303 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
304 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
305 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
306 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
309 @Test(dependsOnMethods = {"create", "testSubmitRequest"}) // , "createWithMalformedXml"})
310 public void updateWithWrongXmlSchema() {
313 super.setupUpdateWithWrongXmlSchema();
315 // Submit the request to the service and store the response.
316 String url = getResourceURL(knownObjectId);
317 PutMethod method = new PutMethod(url);
318 final String WRONG_SCHEMA_DATA = "<wrong_schema>wrong schema contents</wrong_schema>";
319 StringRequestEntity entity = getXmlEntity(WRONG_SCHEMA_DATA);
320 int statusCode = submitRequest(method, entity);
322 // Check the status code of the response: does it match the expected response(s)?
323 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
324 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
325 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
326 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
329 @Test(dependsOnMethods = {"update"})
330 public void updateNonExistent() {
333 super.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 // ---------------------------------------------------------------
357 @Test(dependsOnMethods =
358 {"create", "read", "testSubmitRequest", "update"})
359 public void delete() {
364 // Submit the request to the service and store the response.
365 ClientResponse<Response> res = client.deleteCollectionObject(knownObjectId);
366 int statusCode = res.getStatus();
368 // Check the status code of the response: does it match the expected response(s)?
369 verbose("delete: status = " + res.getStatus());
370 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
371 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
372 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
377 @Test(dependsOnMethods = {"delete"})
378 public void deleteNonExistent() {
381 super.setupDeleteNonExistent();
383 // Submit the request to the service and store the response.
384 ClientResponse<Response> res =
385 client.deleteCollectionObject(NON_EXISTENT_ID);
386 int statusCode = res.getStatus();
388 // Check the status code of the response: does it match the expected response(s)?
389 verbose("deleteNonExistent: status = " + res.getStatus());
390 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
391 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
392 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
396 // ---------------------------------------------------------------
397 // Utility tests : tests of code used in tests above
398 // ---------------------------------------------------------------
401 * Tests the HttpClient-based code used to submit data, in various methods below.
403 @Test(dependsOnMethods = {"create", "read"})
404 public void testSubmitRequest() {
406 // Expected status code: 200 OK
407 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
409 // Submit the request to the service and store the response.
410 String url = getResourceURL(knownObjectId);
411 GetMethod method = new GetMethod(url);
412 int statusCode = submitRequest(method);
414 // Check the status code of the response: does it match the expected response(s)?
415 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
416 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
421 // ---------------------------------------------------------------
422 // Utility methods used by tests above
423 // ---------------------------------------------------------------
425 private CollectionObject createCollectionObject(String identifier) {
426 CollectionObject collectionObject = createCollectionObject("objectNumber-" + identifier,
427 "objectName-" + identifier);
428 return collectionObject;
431 private CollectionObject createCollectionObject(String objectNumber, String objectName) {
432 CollectionObject collectionObject = new CollectionObject();
433 collectionObject.setObjectNumber(objectNumber);
434 collectionObject.setObjectName(objectName);
435 return collectionObject;
439 public String getServicePathComponent() {
440 // @TODO Determine if it is possible to obtain this value programmatically.
441 // We set this in an annotation in the CollectionObjectProxy interface, for instance.
442 // We also set service-specific constants in each service module.
443 final String SERVICE_PATH_COMPONENT = "collectionobjects";
444 return SERVICE_PATH_COMPONENT;