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;
25 import javax.ws.rs.core.Response;
26 import org.collectionspace.services.client.test.AbstractPoxServiceTestImpl;
27 import org.collectionspace.services.client.test.ServiceRequestType;
28 import org.collectionspace.services.dutyofcare.DutyofcaresCommon;
29 import org.collectionspace.services.jaxb.AbstractCommonList;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
32 import org.testng.Assert;
34 public class DutyofcareServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, DutyofcaresCommon> {
36 private final Logger logger = LoggerFactory.getLogger(DutyofcareServiceTest.class);
38 /** The service path component. */
39 final String SERVICE_NAME = "dutyofcares";
41 final String SERVICE_PATH_COMPONENT = "dutyofcares";
44 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
47 protected CollectionSpaceClient getClientInstance() throws Exception {
48 return new DutyofcareClient();
52 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
53 return new DutyofcareClient(clientPropertiesFilename);
57 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
60 protected AbstractCommonList getCommonList(Response response) {
61 return response.readEntity(AbstractCommonList.class);
64 // ---------------------------------------------------------------
65 // CRUD tests : CREATE tests
66 // ---------------------------------------------------------------
71 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
74 public void create(String testName) throws Exception {
75 // Perform setup, such as initializing the type of service request
76 // (e.g. CREATE, DELETE), its valid and expected status codes, and
77 // its associated HTTP method name (e.g. POST, DELETE).
80 // Submit the request to the service and store the response.
81 DutyofcareClient client = new DutyofcareClient();
82 String identifier = createIdentifier();
83 PoxPayloadOut multipart = createDutyofcareInstance(identifier);
85 Response res = client.create(multipart);
87 int statusCode = res.getStatus();
89 // Check the status code of the response: does it match
90 // the expected response(s)?
93 // Does it fall within the set of valid status codes?
94 // Does it exactly match the expected status code?
95 logger.debug(testName + ": status = " + statusCode);
97 testRequestType.isValidStatusCode(statusCode),
98 invalidStatusCodeMessage(testRequestType, statusCode));
99 Assert.assertEquals(statusCode, testExpectedStatusCode);
101 newID = extractId(res);
108 // Store the ID returned from the first resource created
109 // for additional tests below.
110 if (knownResourceId == null) {
111 knownResourceId = newID;
112 logger.debug(testName + ": knownResourceId=" + knownResourceId);
115 // Store the IDs from every resource created by tests,
116 // so they can be deleted after tests have been run.
117 allResourceIdsCreated.add(newID);
121 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
124 public void createList(String testName) throws Exception {
125 for (int i = 0; i < 3; i++) {
130 // ---------------------------------------------------------------
131 // CRUD tests : READ tests
132 // ---------------------------------------------------------------
137 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#read(java.lang.String)
140 public void read(String testName) throws Exception {
144 // Submit the request to the service and store the response.
145 DutyofcareClient client = new DutyofcareClient();
146 Response res = client.read(knownResourceId);
149 assertStatusCode(res, testName);
150 input = new PoxPayloadIn(res.readEntity(String.class));
157 // Get the common part of the response and verify that it is not null.
158 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
159 DutyofcaresCommon dutyofcareCommon = null;
160 if (payloadInputPart != null) {
161 dutyofcareCommon = (DutyofcaresCommon) payloadInputPart.getBody();
163 Assert.assertNotNull(dutyofcareCommon);
169 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
172 public void readNonExistent(String testName) throws Exception {
174 setupReadNonExistent();
176 // Submit the request to the service and store the response.
177 DutyofcareClient client = new DutyofcareClient();
178 Response res = client.read(NON_EXISTENT_ID);
180 int statusCode = res.getStatus();
182 // Check the status code of the response: does it match
183 // the expected response(s)?
184 logger.debug(testName + ": status = " + statusCode);
186 testRequestType.isValidStatusCode(statusCode),
187 invalidStatusCodeMessage(testRequestType, statusCode));
188 Assert.assertEquals(statusCode, testExpectedStatusCode);
196 // ---------------------------------------------------------------
197 // CRUD tests : READ_LIST tests
198 // ---------------------------------------------------------------
203 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readList(java.lang.String)
206 public void readList(String testName) throws Exception {
210 // Submit the request to the service and store the response.
211 AbstractCommonList list;
212 DutyofcareClient client = new DutyofcareClient();
213 Response res = client.readList();
214 assertStatusCode(res, testName);
216 int statusCode = res.getStatus();
218 // Check the status code of the response: does it match
219 // the expected response(s)?
220 logger.debug(testName + ": status = " + statusCode);
222 testRequestType.isValidStatusCode(statusCode),
223 invalidStatusCodeMessage(testRequestType, statusCode));
224 Assert.assertEquals(statusCode, testExpectedStatusCode);
226 list = res.readEntity(getCommonListType());
231 // Optionally output additional data about list members for debugging.
232 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
238 // ---------------------------------------------------------------
239 // CRUD tests : UPDATE tests
240 // ---------------------------------------------------------------
245 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
248 public void update(String testName) throws Exception {
252 // Retrieve the contents of a resource to update.
253 DutyofcareClient client = new DutyofcareClient();
254 Response res = client.read(knownResourceId);
257 assertStatusCode(res, testName);
258 input = new PoxPayloadIn(res.readEntity(String.class));
259 logger.debug("got object to update with ID: " + knownResourceId);
266 // Extract the common part from the response.
267 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
268 DutyofcaresCommon dutyofcareCommon = null;
269 if (payloadInputPart != null) {
270 dutyofcareCommon = (DutyofcaresCommon) payloadInputPart.getBody();
272 Assert.assertNotNull(dutyofcareCommon);
274 // Update the content of this resource.
275 dutyofcareCommon.setDutyOfCareNumber("updated-" + dutyofcareCommon.getDutyOfCareNumber());
277 logger.debug("to be updated object");
278 logger.debug(objectAsXmlString(dutyofcareCommon, DutyofcaresCommon.class));
282 // Submit the updated common part in an update request to the service
283 // and store the response.
284 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
285 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), dutyofcareCommon);
286 res = client.update(knownResourceId, output);
288 assertStatusCode(res, testName);
289 int statusCode = res.getStatus();
290 // Check the status code of the response: does it match the expected response(s)?
291 logger.debug(testName + ": status = " + statusCode);
293 testRequestType.isValidStatusCode(statusCode),
294 invalidStatusCodeMessage(testRequestType, statusCode));
295 Assert.assertEquals(statusCode, testExpectedStatusCode);
296 input = new PoxPayloadIn(res.readEntity(String.class));
303 // Extract the updated common part from the response.
304 payloadInputPart = input.getPart(client.getCommonPartName());
305 DutyofcaresCommon updatedDutyofcareCommon = null;
306 if (payloadInputPart != null) {
307 updatedDutyofcareCommon = (DutyofcaresCommon) payloadInputPart.getBody();
309 Assert.assertNotNull(updatedDutyofcareCommon);
311 // Check selected fields in the updated common part.
313 updatedDutyofcareCommon.getDutyOfCareNumber(),
314 dutyofcareCommon.getDutyOfCareNumber(),
315 "Data in updated object did not match submitted data.");
319 public void updateNonExistent(String testName) throws Exception {
321 setupUpdateNonExistent();
323 // Submit the request to the service and store the response.
324 // Note: The ID used in this 'create' call may be arbitrary.
325 // The only relevant ID may be the one used in update(), below.
326 DutyofcareClient client = new DutyofcareClient();
327 PoxPayloadOut multipart = createDutyofcareInstance(NON_EXISTENT_ID);
328 Response res = client.update(NON_EXISTENT_ID, multipart);
330 int statusCode = res.getStatus();
332 // Check the status code of the response: does it match
333 // the expected response(s)?
334 logger.debug(testName + ": status = " + statusCode);
336 testRequestType.isValidStatusCode(statusCode),
337 invalidStatusCodeMessage(testRequestType, statusCode));
338 Assert.assertEquals(statusCode, testExpectedStatusCode);
346 // ---------------------------------------------------------------
347 // CRUD tests : DELETE tests
348 // ---------------------------------------------------------------
353 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
356 public void delete(String testName) throws Exception {
360 // Submit the request to the service and store the response.
361 DutyofcareClient client = new DutyofcareClient();
362 Response res = client.delete(knownResourceId);
364 int statusCode = res.getStatus();
366 // Check the status code of the response: does it match
367 // the expected response(s)?
368 logger.debug(testName + ": status = " + statusCode);
370 testRequestType.isValidStatusCode(statusCode),
371 invalidStatusCodeMessage(testRequestType, statusCode));
372 Assert.assertEquals(statusCode, testExpectedStatusCode);
383 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
386 public void deleteNonExistent(String testName) throws Exception {
388 setupDeleteNonExistent();
390 // Submit the request to the service and store the response.
391 DutyofcareClient client = new DutyofcareClient();
392 Response res = client.delete(NON_EXISTENT_ID);
394 int statusCode = res.getStatus();
396 // Check the status code of the response: does it match
397 // the expected response(s)?
398 logger.debug(testName + ": status = " + statusCode);
400 testRequestType.isValidStatusCode(statusCode),
401 invalidStatusCodeMessage(testRequestType, statusCode));
402 Assert.assertEquals(statusCode, testExpectedStatusCode);
410 // ---------------------------------------------------------------
411 // Utility tests : tests of code used in tests above
412 // ---------------------------------------------------------------
415 * Tests the code for manually submitting data that is used by several
416 * of the methods above.
418 public void testSubmitRequest() {
420 // Expected status code: 200 OK
421 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
423 // Submit the request to the service and store the response.
424 String method = ServiceRequestType.READ.httpMethodName();
425 String url = getResourceURL(knownResourceId);
426 int statusCode = submitRequest(method, url);
428 // Check the status code of the response: does it match
429 // the expected response(s)?
430 logger.debug("testSubmitRequest: url=" + url + " status=" + statusCode);
431 Assert.assertEquals(statusCode, EXPECTED_STATUS);
434 // ---------------------------------------------------------------
435 // Utility methods used by tests above
436 // ---------------------------------------------------------------
439 public String getServiceName() {
444 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
447 public String getServicePathComponent() {
448 return SERVICE_PATH_COMPONENT;
452 protected PoxPayloadOut createInstance(String identifier) throws Exception {
453 return createDutyofcareInstance(identifier);
457 * Creates the dutyofcare instance.
459 * @param dutyofcareNumber the exhibition number
460 * @return the multipart output
463 private PoxPayloadOut createDutyofcareInstance(String dutyofcareNumber) throws Exception {
464 DutyofcaresCommon dutyofcareCommon = new DutyofcaresCommon();
465 dutyofcareCommon.setDutyOfCareNumber(dutyofcareNumber);
467 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
468 PayloadOutputPart commonPart = multipart.addPart(new DutyofcareClient().getCommonPartName(), dutyofcareCommon);
470 logger.debug("to be created, dutyofcare common");
471 logger.debug(objectAsXmlString(dutyofcareCommon, DutyofcaresCommon.class));
477 public void CRUDTests(String testName) {
478 // TODO Auto-generated method stub
483 protected PoxPayloadOut createInstance(String commonPartName, String identifier) throws Exception {
484 return createDutyofcareInstance(identifier);
488 protected DutyofcaresCommon updateInstance(DutyofcaresCommon commonPartObject) {
489 // TODO Auto-generated method stub
494 protected void compareUpdatedInstances(DutyofcaresCommon original, DutyofcaresCommon updated) {
495 // TODO Auto-generated method stub