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.
23 package org.collectionspace.services.client.test;
25 import java.util.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
29 import org.collectionspace.services.client.VocabularyClient;
30 import org.collectionspace.services.vocabulary.VocabulariesCommon;
31 import org.collectionspace.services.vocabulary.VocabulariesCommonList;
33 import org.jboss.resteasy.client.ClientResponse;
35 import org.jboss.resteasy.plugins.providers.multipart.MultipartInput;
36 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
37 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
42 * VocabularyServiceTest, carries out tests against a
43 * deployed and running Vocabulary Service.
45 * $LastChangedRevision: 753 $
46 * $LastChangedDate: 2009-09-23 11:03:36 -0700 (Wed, 23 Sep 2009) $
48 public class VocabularyServiceTest extends AbstractServiceTest {
50 // Instance variables specific to this test.
51 private VocabularyClient client = new VocabularyClient();
52 final String SERVICE_PATH_COMPONENT = "vocabularies";
53 private String knownResourceId = null;
55 // ---------------------------------------------------------------
56 // CRUD tests : CREATE tests
57 // ---------------------------------------------------------------
61 public void create() {
63 // Perform setup, such as initializing the type of service request
64 // (e.g. CREATE, DELETE), its valid and expected status codes, and
65 // its associated HTTP method name (e.g. POST, DELETE).
68 // Submit the request to the service and store the response.
69 String identifier = createIdentifier();
71 MultipartOutput multipart = createVocabularyInstance(identifier);
72 ClientResponse<Response> res = client.create(multipart);
74 int statusCode = res.getStatus();
76 // Check the status code of the response: does it match
77 // the expected response(s)?
80 // Does it fall within the set of valid status codes?
81 // Does it exactly match the expected status code?
82 verbose("create: status = " + statusCode);
83 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
84 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
85 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
87 // Store the ID returned from this create operation
88 // for additional tests below.
89 knownResourceId = extractId(res);
90 verbose("create: knownResourceId=" + knownResourceId);
94 @Test(dependsOnMethods = {"create"})
95 public void createList() {
96 for(int i = 0; i < 3; i++){
102 // Placeholders until the three tests below can be uncommented.
103 // See Issue CSPACE-401.
104 public void createWithEmptyEntityBody() {
107 public void createWithMalformedXml() {
110 public void createWithWrongXmlSchema() {
115 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
116 public void createWithEmptyEntityBody() {
119 setupCreateWithEmptyEntityBody();
121 // Submit the request to the service and store the response.
122 String method = REQUEST_TYPE.httpMethodName();
123 String url = getServiceRootURL();
124 String mediaType = MediaType.APPLICATION_XML;
125 final String entity = "";
126 int statusCode = submitRequest(method, url, mediaType, entity);
128 // Check the status code of the response: does it match
129 // the expected response(s)?
130 verbose("createWithEmptyEntityBody url=" + url + " status=" + statusCode);
131 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
132 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
133 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
137 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
138 public void createWithMalformedXml() {
141 setupCreateWithMalformedXml();
143 // Submit the request to the service and store the response.
144 String method = REQUEST_TYPE.httpMethodName();
145 String url = getServiceRootURL();
146 String mediaType = MediaType.APPLICATION_XML;
147 final String entity = MALFORMED_XML_DATA; // Constant from base class.
148 int statusCode = submitRequest(method, url, mediaType, entity);
150 // Check the status code of the response: does it match
151 // the expected response(s)?
152 verbose("createWithMalformedXml url=" + url + " status=" + statusCode);
153 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
154 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
155 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
159 @Test(dependsOnMethods = {"create", "testSubmitRequest"})
160 public void createWithWrongXmlSchema() {
163 setupCreateWithWrongXmlSchema();
165 // Submit the request to the service and store the response.
166 String method = REQUEST_TYPE.httpMethodName();
167 String url = getServiceRootURL();
168 String mediaType = MediaType.APPLICATION_XML;
169 final String entity = WRONG_XML_SCHEMA_DATA;
170 int statusCode = submitRequest(method, url, mediaType, entity);
172 // Check the status code of the response: does it match
173 // the expected response(s)?
174 verbose("createWithWrongSchema url=" + url + " status=" + statusCode);
175 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
176 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
177 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
180 // ---------------------------------------------------------------
181 // CRUD tests : READ tests
182 // ---------------------------------------------------------------
185 @Test(dependsOnMethods = {"create"})
191 // Submit the request to the service and store the response.
192 ClientResponse<MultipartInput> res = client.read(knownResourceId);
193 int statusCode = res.getStatus();
195 // Check the status code of the response: does it match
196 // the expected response(s)?
197 verbose("read: status = " + statusCode);
198 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
199 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
200 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
201 //FIXME: remove the following try catch once Aron fixes signatures
203 MultipartInput input = (MultipartInput) res.getEntity();
204 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
205 client.getCommonPartName(), VocabulariesCommon.class);
206 Assert.assertNotNull(vocabulary);
208 throw new RuntimeException(e);
214 @Test(dependsOnMethods = {"read"})
215 public void readNonExistent() {
218 setupReadNonExistent();
220 // Submit the request to the service and store the response.
221 ClientResponse<MultipartInput> res = client.read(NON_EXISTENT_ID);
222 int statusCode = res.getStatus();
224 // Check the status code of the response: does it match
225 // the expected response(s)?
226 verbose("readNonExistent: status = " + res.getStatus());
227 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
228 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
229 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
232 // ---------------------------------------------------------------
233 // CRUD tests : READ_LIST tests
234 // ---------------------------------------------------------------
237 @Test(dependsOnMethods = {"read"})
238 public void readList() {
243 // Submit the request to the service and store the response.
244 ClientResponse<VocabulariesCommonList> res = client.readList();
245 VocabulariesCommonList list = res.getEntity();
246 int statusCode = res.getStatus();
248 // Check the status code of the response: does it match
249 // the expected response(s)?
250 verbose("readList: status = " + res.getStatus());
251 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
252 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
253 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
255 // Optionally output additional data about list members for debugging.
256 boolean iterateThroughList = false;
257 if(iterateThroughList && logger.isDebugEnabled()){
258 List<VocabulariesCommonList.VocabularyListItem> items =
259 list.getVocabularyListItem();
261 for(VocabulariesCommonList.VocabularyListItem item : items){
262 verbose("readList: list-item[" + i + "] csid=" +
264 verbose("readList: list-item[" + i + "] displayName=" +
265 item.getDisplayName());
266 verbose("readList: list-item[" + i + "] URI=" +
276 // ---------------------------------------------------------------
277 // CRUD tests : UPDATE tests
278 // ---------------------------------------------------------------
281 @Test(dependsOnMethods = {"read"})
282 public void update() {
287 try{ //ideally, just remove try-catch and let the exception bubble up
288 // Retrieve an existing resource that we can update.
289 ClientResponse<MultipartInput> res =
290 client.read(knownResourceId);
291 verbose("update: read status = " + res.getStatus());
292 Assert.assertEquals(res.getStatus(), EXPECTED_STATUS_CODE);
294 verbose("got object to update with ID: " + knownResourceId);
295 MultipartInput input = (MultipartInput) res.getEntity();
296 VocabulariesCommon vocabulary = (VocabulariesCommon) extractPart(input,
297 client.getCommonPartName(), VocabulariesCommon.class);
298 Assert.assertNotNull(vocabulary);
300 // Update the content of this resource.
301 vocabulary.setDisplayName("updated-" + vocabulary.getDisplayName());
302 vocabulary.setVocabType("updated-" + vocabulary.getVocabType());
303 verbose("to be updated object", vocabulary, VocabulariesCommon.class);
304 // Submit the request to the service and store the response.
305 MultipartOutput output = new MultipartOutput();
306 OutputPart commonPart = output.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
307 commonPart.getHeaders().add("label", client.getCommonPartName());
309 res = client.update(knownResourceId, output);
310 int statusCode = res.getStatus();
311 // Check the status code of the response: does it match the expected response(s)?
312 verbose("update: status = " + res.getStatus());
313 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
314 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
315 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
318 input = (MultipartInput) res.getEntity();
319 VocabulariesCommon updatedVocabulary =
320 (VocabulariesCommon) extractPart(input,
321 client.getCommonPartName(), VocabulariesCommon.class);
322 Assert.assertNotNull(updatedVocabulary);
324 Assert.assertEquals(updatedVocabulary.getDisplayName(),
325 vocabulary.getDisplayName(),
326 "Data in updated object did not match submitted data.");
333 // Placeholders until the three tests below can be uncommented.
334 // See Issue CSPACE-401.
335 public void updateWithEmptyEntityBody() {
338 public void updateWithMalformedXml() {
341 public void updateWithWrongXmlSchema() {
346 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
347 public void updateWithEmptyEntityBody() {
350 setupUpdateWithEmptyEntityBody();
352 // Submit the request to the service and store the response.
353 String method = REQUEST_TYPE.httpMethodName();
354 String url = getResourceURL(knownResourceId);
355 String mediaType = MediaType.APPLICATION_XML;
356 final String entity = "";
357 int statusCode = submitRequest(method, url, mediaType, entity);
359 // Check the status code of the response: does it match
360 // the expected response(s)?
361 verbose("updateWithEmptyEntityBody url=" + url + " status=" + statusCode);
362 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
363 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
364 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
368 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
369 public void updateWithMalformedXml() {
372 setupUpdateWithMalformedXml();
374 // Submit the request to the service and store the response.
375 String method = REQUEST_TYPE.httpMethodName();
376 String url = getResourceURL(knownResourceId);
377 String mediaType = MediaType.APPLICATION_XML;
378 final String entity = MALFORMED_XML_DATA;
379 int statusCode = submitRequest(method, url, mediaType, entity);
381 // Check the status code of the response: does it match
382 // the expected response(s)?
383 verbose("updateWithMalformedXml: url=" + url + " status=" + statusCode);
384 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
385 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
386 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
390 @Test(dependsOnMethods = {"create", "update", "testSubmitRequest"})
391 public void updateWithWrongXmlSchema() {
394 setupUpdateWithWrongXmlSchema();
396 // Submit the request to the service and store the response.
397 String method = REQUEST_TYPE.httpMethodName();
398 String url = getResourceURL(knownResourceId);
399 String mediaType = MediaType.APPLICATION_XML;
400 final String entity = WRONG_XML_SCHEMA_DATA;
401 int statusCode = submitRequest(method, url, mediaType, entity);
403 // Check the status code of the response: does it match
404 // the expected response(s)?
405 verbose("updateWithWrongSchema: url=" + url + " status=" + statusCode);
406 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
407 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
408 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
412 @Test(dependsOnMethods = {"update", "testSubmitRequest"})
413 public void updateNonExistent() {
416 setupUpdateNonExistent();
418 // Submit the request to the service and store the response.
419 // Note: The ID used in this 'create' call may be arbitrary.
420 // The only relevant ID may be the one used in update(), below.
422 // The only relevant ID may be the one used in update(), below.
423 MultipartOutput multipart = createVocabularyInstance(NON_EXISTENT_ID);
424 ClientResponse<MultipartInput> res =
425 client.update(NON_EXISTENT_ID, multipart);
426 int statusCode = res.getStatus();
428 // Check the status code of the response: does it match
429 // the expected response(s)?
430 verbose("updateNonExistent: status = " + res.getStatus());
431 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
432 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
433 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
436 // ---------------------------------------------------------------
437 // CRUD tests : DELETE tests
438 // ---------------------------------------------------------------
441 @Test(dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
442 public void delete() {
447 // Submit the request to the service and store the response.
448 ClientResponse<Response> res = client.delete(knownResourceId);
449 int statusCode = res.getStatus();
451 // Check the status code of the response: does it match
452 // the expected response(s)?
453 verbose("delete: status = " + res.getStatus());
454 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
455 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
456 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
461 @Test(dependsOnMethods = {"delete"})
462 public void deleteNonExistent() {
465 setupDeleteNonExistent();
467 // Submit the request to the service and store the response.
468 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
469 int statusCode = res.getStatus();
471 // Check the status code of the response: does it match
472 // the expected response(s)?
473 verbose("deleteNonExistent: status = " + res.getStatus());
474 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
475 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
476 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
479 // ---------------------------------------------------------------
480 // Utility tests : tests of code used in tests above
481 // ---------------------------------------------------------------
483 * Tests the code for manually submitting data that is used by several
484 * of the methods above.
486 @Test(dependsOnMethods = {"create", "read"})
487 public void testSubmitRequest() {
489 // Expected status code: 200 OK
490 final int EXPECTED_STATUS_CODE = Response.Status.OK.getStatusCode();
492 // Submit the request to the service and store the response.
493 String method = ServiceRequestType.READ.httpMethodName();
494 String url = getResourceURL(knownResourceId);
495 int statusCode = submitRequest(method, url);
497 // Check the status code of the response: does it match
498 // the expected response(s)?
499 verbose("testSubmitRequest: url=" + url + " status=" + statusCode);
500 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
504 // ---------------------------------------------------------------
505 // Utility methods used by tests above
506 // ---------------------------------------------------------------
508 public String getServicePathComponent() {
509 return SERVICE_PATH_COMPONENT;
512 private MultipartOutput createVocabularyInstance(String identifier) {
513 return createVocabularyInstance(
514 "displayName-" + identifier,
515 "vocabType-" + identifier);
518 private MultipartOutput createVocabularyInstance(String displayName, String vocabType) {
519 VocabulariesCommon vocabulary = new VocabulariesCommon();
520 vocabulary.setDisplayName(displayName);
521 vocabulary.setVocabType(vocabType);
522 MultipartOutput multipart = new MultipartOutput();
523 OutputPart commonPart = multipart.addPart(vocabulary, MediaType.APPLICATION_XML_TYPE);
524 commonPart.getHeaders().add("label", client.getCommonPartName());
526 verbose("to be created, vocabulary common ", vocabulary, VocabulariesCommon.class);