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 javax.ws.rs.core.Response;
26 import org.collectionspace.services.client.AbstractCommonListUtils;
27 import org.collectionspace.services.client.CollectionSpaceClient;
28 import org.collectionspace.services.client.NagpraClaimClient;
29 import org.collectionspace.services.client.PayloadInputPart;
30 import org.collectionspace.services.client.PayloadOutputPart;
31 import org.collectionspace.services.client.PoxPayloadIn;
32 import org.collectionspace.services.client.PoxPayloadOut;
33 import org.collectionspace.services.jaxb.AbstractCommonList;
34 import org.collectionspace.services.nagpraclaim.NagpraclaimsCommon;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.testng.Assert;
39 public class NagpraClaimServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, NagpraclaimsCommon> {
41 private final Logger logger = LoggerFactory.getLogger(NagpraClaimServiceTest.class);
43 /** The service path component. */
44 final String SERVICE_NAME = "nagpraclaims";
46 final String SERVICE_PATH_COMPONENT = "nagpraclaims";
49 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
52 protected CollectionSpaceClient getClientInstance() throws Exception {
53 return new NagpraClaimClient();
57 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
58 return new NagpraClaimClient(clientPropertiesFilename);
62 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
65 protected AbstractCommonList getCommonList(Response response) {
66 return response.readEntity(AbstractCommonList.class);
69 // ---------------------------------------------------------------
70 // CRUD tests : CREATE tests
71 // ---------------------------------------------------------------
76 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
79 public void create(String testName) throws Exception {
80 // Perform setup, such as initializing the type of service request
81 // (e.g. CREATE, DELETE), its valid and expected status codes, and
82 // its associated HTTP method name (e.g. POST, DELETE).
85 // Submit the request to the service and store the response.
86 NagpraClaimClient client = new NagpraClaimClient();
87 String identifier = createIdentifier();
88 PoxPayloadOut multipart = createNagpraClaimInstance(identifier);
90 Response res = client.create(multipart);
92 int statusCode = res.getStatus();
94 // Check the status code of the response: does it match
95 // the expected response(s)?
98 // Does it fall within the set of valid status codes?
99 // Does it exactly match the expected status code?
100 logger.debug(testName + ": status = " + statusCode);
102 testRequestType.isValidStatusCode(statusCode),
103 invalidStatusCodeMessage(testRequestType, statusCode));
104 Assert.assertEquals(statusCode, testExpectedStatusCode);
106 newID = extractId(res);
113 // Store the ID returned from the first resource created
114 // for additional tests below.
115 if (knownResourceId == null) {
116 knownResourceId = newID;
117 logger.debug(testName + ": knownResourceId=" + knownResourceId);
120 // Store the IDs from every resource created by tests,
121 // so they can be deleted after tests have been run.
122 allResourceIdsCreated.add(newID);
126 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
129 public void createList(String testName) throws Exception {
130 for (int i = 0; i < 3; i++) {
135 // ---------------------------------------------------------------
136 // CRUD tests : READ tests
137 // ---------------------------------------------------------------
142 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
145 public void read(String testName) throws Exception {
149 // Submit the request to the service and store the response.
150 NagpraClaimClient client = new NagpraClaimClient();
151 Response res = client.read(knownResourceId);
154 assertStatusCode(res, testName);
155 input = new PoxPayloadIn(res.readEntity(String.class));
162 // Get the common part of the response and verify that it is not null.
163 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
164 NagpraclaimsCommon common = null;
165 if (payloadInputPart != null) {
166 common = (NagpraclaimsCommon) payloadInputPart.getBody();
168 Assert.assertNotNull(common);
174 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
177 public void readNonExistent(String testName) throws Exception {
179 setupReadNonExistent();
181 // Submit the request to the service and store the response.
182 NagpraClaimClient client = new NagpraClaimClient();
183 Response res = client.read(NON_EXISTENT_ID);
185 int statusCode = res.getStatus();
187 // Check the status code of the response: does it match
188 // the expected response(s)?
189 logger.debug(testName + ": status = " + statusCode);
191 testRequestType.isValidStatusCode(statusCode),
192 invalidStatusCodeMessage(testRequestType, statusCode));
193 Assert.assertEquals(statusCode, testExpectedStatusCode);
201 // ---------------------------------------------------------------
202 // CRUD tests : READ_LIST tests
203 // ---------------------------------------------------------------
208 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
211 public void readList(String testName) throws Exception {
215 // Submit the request to the service and store the response.
216 AbstractCommonList list;
217 NagpraClaimClient client = new NagpraClaimClient();
218 Response res = client.readList();
219 assertStatusCode(res, testName);
221 int statusCode = res.getStatus();
223 // Check the status code of the response: does it match
224 // the expected response(s)?
225 logger.debug(testName + ": status = " + statusCode);
227 testRequestType.isValidStatusCode(statusCode),
228 invalidStatusCodeMessage(testRequestType, statusCode));
229 Assert.assertEquals(statusCode, testExpectedStatusCode);
231 list = res.readEntity(getCommonListType());
236 // Optionally output additional data about list members for debugging.
237 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
243 // ---------------------------------------------------------------
244 // CRUD tests : UPDATE tests
245 // ---------------------------------------------------------------
250 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
253 public void update(String testName) throws Exception {
257 // Retrieve the contents of a resource to update.
258 NagpraClaimClient client = new NagpraClaimClient();
259 Response res = client.read(knownResourceId);
262 assertStatusCode(res, testName);
263 input = new PoxPayloadIn(res.readEntity(String.class));
264 logger.debug("got object to update with ID: " + knownResourceId);
271 // Extract the common part from the response.
272 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
273 NagpraclaimsCommon common = null;
274 if (payloadInputPart != null) {
275 common = (NagpraclaimsCommon) payloadInputPart.getBody();
277 Assert.assertNotNull(common);
279 // Update the content of this resource.
280 common.setClaimNumber("updated-" + common.getClaimNumber());
282 logger.debug("to be updated object");
283 logger.debug(objectAsXmlString(common, NagpraclaimsCommon.class));
287 // Submit the updated common part in an update request to the service
288 // and store the response.
289 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
290 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), common);
291 res = client.update(knownResourceId, output);
293 assertStatusCode(res, testName);
294 int statusCode = res.getStatus();
295 // Check the status code of the response: does it match the expected response(s)?
296 logger.debug(testName + ": status = " + statusCode);
298 testRequestType.isValidStatusCode(statusCode),
299 invalidStatusCodeMessage(testRequestType, statusCode));
300 Assert.assertEquals(statusCode, testExpectedStatusCode);
301 input = new PoxPayloadIn(res.readEntity(String.class));
308 // Extract the updated common part from the response.
309 payloadInputPart = input.getPart(client.getCommonPartName());
310 NagpraclaimsCommon updatedNagpraClaimCommon = null;
311 if (payloadInputPart != null) {
312 updatedNagpraClaimCommon = (NagpraclaimsCommon) payloadInputPart.getBody();
314 Assert.assertNotNull(updatedNagpraClaimCommon);
316 // Check selected fields in the updated common part.
318 updatedNagpraClaimCommon.getClaimNumber(),
319 common.getClaimNumber(),
320 "Data in updated object did not match submitted data.");
324 public void updateNonExistent(String testName) throws Exception {
326 setupUpdateNonExistent();
328 // Submit the request to the service and store the response.
329 // Note: The ID used in this 'create' call may be arbitrary.
330 // The only relevant ID may be the one used in update(), below.
331 NagpraClaimClient client = new NagpraClaimClient();
332 PoxPayloadOut multipart = createNagpraClaimInstance(NON_EXISTENT_ID);
333 Response res = client.update(NON_EXISTENT_ID, multipart);
335 int statusCode = res.getStatus();
337 // Check the status code of the response: does it match
338 // the expected response(s)?
339 logger.debug(testName + ": status = " + statusCode);
341 testRequestType.isValidStatusCode(statusCode),
342 invalidStatusCodeMessage(testRequestType, statusCode));
343 Assert.assertEquals(statusCode, testExpectedStatusCode);
351 // ---------------------------------------------------------------
352 // CRUD tests : DELETE tests
353 // ---------------------------------------------------------------
358 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
361 public void delete(String testName) throws Exception {
365 // Submit the request to the service and store the response.
366 NagpraClaimClient client = new NagpraClaimClient();
367 Response res = client.delete(knownResourceId);
369 int statusCode = res.getStatus();
371 // Check the status code of the response: does it match
372 // the expected response(s)?
373 logger.debug(testName + ": status = " + statusCode);
375 testRequestType.isValidStatusCode(statusCode),
376 invalidStatusCodeMessage(testRequestType, statusCode));
377 Assert.assertEquals(statusCode, testExpectedStatusCode);
388 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
391 public void deleteNonExistent(String testName) throws Exception {
393 setupDeleteNonExistent();
395 // Submit the request to the service and store the response.
396 NagpraClaimClient client = new NagpraClaimClient();
397 Response res = client.delete(NON_EXISTENT_ID);
399 int statusCode = res.getStatus();
401 // Check the status code of the response: does it match
402 // the expected response(s)?
403 logger.debug(testName + ": status = " + statusCode);
405 testRequestType.isValidStatusCode(statusCode),
406 invalidStatusCodeMessage(testRequestType, statusCode));
407 Assert.assertEquals(statusCode, testExpectedStatusCode);
415 // ---------------------------------------------------------------
416 // Utility tests : tests of code used in tests above
417 // ---------------------------------------------------------------
420 * Tests the code for manually submitting data that is used by several
421 * of the methods above.
423 public void testSubmitRequest() {
425 // Expected status code: 200 OK
426 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
428 // Submit the request to the service and store the response.
429 String method = ServiceRequestType.READ.httpMethodName();
430 String url = getResourceURL(knownResourceId);
431 int statusCode = submitRequest(method, url);
433 // Check the status code of the response: does it match
434 // the expected response(s)?
435 logger.debug("testSubmitRequest: url=" + url + " status=" + statusCode);
436 Assert.assertEquals(statusCode, EXPECTED_STATUS);
439 // ---------------------------------------------------------------
440 // Utility methods used by tests above
441 // ---------------------------------------------------------------
444 public String getServiceName() {
449 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
452 public String getServicePathComponent() {
453 return SERVICE_PATH_COMPONENT;
457 protected PoxPayloadOut createInstance(String identifier) throws Exception {
458 return createNagpraClaimInstance(identifier);
462 * Creates the nagpraclaim instance.
464 * @param claimNumber the exhibition number
465 * @return the multipart output
468 private PoxPayloadOut createNagpraClaimInstance(String claimNumber) throws Exception {
469 NagpraclaimsCommon common = new NagpraclaimsCommon();
470 common.setClaimNumber(claimNumber);
472 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
473 PayloadOutputPart commonPart = multipart.addPart(new NagpraClaimClient().getCommonPartName(), common);
475 logger.debug("to be created, nagpraclaim common");
476 logger.debug(objectAsXmlString(common, NagpraclaimsCommon.class));
482 public void CRUDTests(String testName) {
483 // TODO Auto-generated method stub
488 protected PoxPayloadOut createInstance(String commonPartName, String identifier) throws Exception {
489 return createNagpraClaimInstance(identifier);
493 protected NagpraclaimsCommon updateInstance(NagpraclaimsCommon commonPartObject) {
494 // TODO Auto-generated method stub
499 protected void compareUpdatedInstances(NagpraclaimsCommon original, NagpraclaimsCommon updated) {
500 // TODO Auto-generated method stub