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.IntakeClient;
32 import org.collectionspace.services.client.test.ServiceRequestType;
33 import org.collectionspace.services.intake.Intake;
34 import org.collectionspace.services.intake.IntakeList;
36 import org.jboss.resteasy.client.ClientResponse;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
42 * IntakeServiceTest, carries out tests against a
43 * deployed and running Intake Service.
45 * $LastChangedRevision$
48 public class IntakeServiceTest extends AbstractServiceTest {
50 // Instance variables specific to this test.
51 private IntakeClient client = new IntakeClient();
52 final String SERVICE_PATH_COMPONENT = "intakes";
53 private String knownResourceId = null;
56 // ---------------------------------------------------------------
57 // CRUD tests : CREATE tests
58 // ---------------------------------------------------------------
64 public void create() {
66 // Perform setup, such as initializing the type of service request
67 // (e.g. CREATE, DELETE), its valid and expected status codes, and
68 // its associated HTTP method name (e.g. POST, DELETE).
71 // Submit the request to the service and store the response.
72 String identifier = createIdentifier();
73 Intake intake = createIntakeInstance(identifier);
74 ClientResponse<Response> res = client.create(intake);
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 for
89 // 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<Intake> 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<Intake> 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<IntakeList> res = client.readList();
216 IntakeList 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<IntakeList.IntakeListItem> items =
230 list.getIntakeListItem();
232 for(IntakeList.IntakeListItem item : items){
233 verbose("readList: list-item[" + i + "] csid=" +
235 verbose("readList: list-item[" + i + "] objectNumber=" +
236 item.getEntryNumber());
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<Intake> res = client.read(knownResourceId);
265 verbose("read: status = " + res.getStatus());
266 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
267 Intake intake = res.getEntity();
268 verbose("Got object to update with ID: " + knownResourceId,
269 intake, Intake.class);
271 // Update the content of this resource.
272 intake.setEntryNumber("updated-" + intake.getEntryNumber());
273 intake.setEntryDate("updated-" + intake.getEntryDate());
275 // Submit the request to the service and store the response.
276 res = client.update(knownResourceId, intake);
277 int statusCode = res.getStatus();
278 Intake updatedObject = res.getEntity();
280 // Check the status code of the response: does it match
281 // the expected response(s)?
282 verbose("update: status = " + res.getStatus());
283 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
284 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
285 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
287 // Check the contents of the response: does it match
288 // what was submitted?
289 verbose("update: ", updatedObject, Intake.class);
290 Assert.assertEquals(updatedObject.getEntryDate(),
291 intake.getEntryDate(),
292 "Data in updated object did not match submitted data.");
297 // Placeholders until the two tests below can be uncommented.
298 // See Issue CSPACE-401.
299 public void updateWithMalformedXml() {}
300 public void updateWithWrongXmlSchema() {}
304 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
305 public void updateWithMalformedXml() {
308 setupUpdateWithMalformedXml();
310 // Submit the request to the service and store the response.
311 String method = REQUEST_TYPE.httpMethodName();
312 String url = getResourceURL(knownResourceId);
313 String mediaType = MediaType.APPLICATION_XML;
314 final String entity = MALFORMED_XML_DATA;
315 int statusCode = submitRequest(method, url, mediaType, entity);
317 // Check the status code of the response: does it match
318 // the expected response(s)?
319 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
320 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
321 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
322 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
326 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
327 public void updateWithWrongXmlSchema() {
330 setupUpdateWithWrongXmlSchema();
332 // Submit the request to the service and store the response.
333 String method = REQUEST_TYPE.httpMethodName();
334 String url = getResourceURL(knownResourceId);
335 String mediaType = MediaType.APPLICATION_XML;
336 final String entity = WRONG_XML_SCHEMA_DATA;
337 int statusCode = submitRequest(method, url, mediaType, entity);
339 // Check the status code of the response: does it match
340 // the expected response(s)?
341 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
342 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
343 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
344 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
349 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
350 public void updateNonExistent() {
353 setupUpdateNonExistent();
355 // Submit the request to the service and store the response.
356 // Note: The ID used in this 'create' call may be arbitrary.
357 // The only relevant ID may be the one used in update(), below.
358 Intake intake = createIntakeInstance(NON_EXISTENT_ID);
359 ClientResponse<Intake> res =
360 client.update(NON_EXISTENT_ID, intake);
361 int statusCode = res.getStatus();
363 // Check the status code of the response: does it match
364 // the expected response(s)?
365 verbose("updateNonExistent: status = " + res.getStatus());
366 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
367 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
368 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
371 // ---------------------------------------------------------------
372 // CRUD tests : DELETE tests
373 // ---------------------------------------------------------------
378 @Test(dependsOnMethods =
379 {"create", "read", "update"})
380 public void delete() {
385 // Submit the request to the service and store the response.
386 ClientResponse<Response> res = client.delete(knownResourceId);
387 int statusCode = res.getStatus();
389 // Check the status code of the response: does it match
390 // the expected response(s)?
391 verbose("delete: status = " + res.getStatus());
392 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
393 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
394 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
400 @Test(dependsOnMethods = {"delete"})
401 public void deleteNonExistent() {
404 setupDeleteNonExistent();
406 // Submit the request to the service and store the response.
407 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
408 int statusCode = res.getStatus();
410 // Check the status code of the response: does it match
411 // the expected response(s)?
412 verbose("deleteNonExistent: status = " + res.getStatus());
413 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
414 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
415 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
419 // ---------------------------------------------------------------
420 // Utility tests : tests of code used in tests above
421 // ---------------------------------------------------------------
424 * Tests the code for manually submitting data that is used by several
425 * of the methods above.
427 @Test(dependsOnMethods = {"create", "read"})
428 public void testSubmitRequest() {
430 // Expected status code: 200 OK
431 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
433 // Submit the request to the service and store the response.
434 String method = ServiceRequestType.READ.httpMethodName();
435 String url = getResourceURL(knownResourceId);
436 int statusCode = submitRequest(method, url);
438 // Check the status code of the response: does it match
439 // the expected response(s)?
440 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
441 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
445 // ---------------------------------------------------------------
446 // Utility methods used by tests above
447 // ---------------------------------------------------------------
450 public String getServicePathComponent() {
451 return SERVICE_PATH_COMPONENT;
454 private Intake createIntakeInstance(String identifier) {
456 createIntakeInstance(
457 "entryNumber-" + identifier,
458 "entryDate-" + identifier);
462 private Intake createIntakeInstance(String entryNumber, String entryDate) {
463 Intake intake = new Intake();
464 intake.setEntryNumber(entryNumber);
465 intake.setEntryDate(entryDate);