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 * Licensed under the Educational Community License (ECL), Version 2.0.
10 * You may not use this file except in compliance with this License.
12 * You may obtain a copy of the ECL 2.0 License at
14 * https://source.collectionspace.org/collection-space/LICENSE.txt
16 * Unless required by applicable law or agreed to in writing, software
17 * distributed under the License is distributed on an "AS IS" BASIS,
18 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19 * See the License for the specific language governing permissions and
20 * limitations under the License.
22 package org.collectionspace.services.client.test;
24 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.IterationreportClient;
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.iterationreport.IterationreportsCommon;
34 import org.collectionspace.services.jaxb.AbstractCommonList;
35 import org.slf4j.Logger;
36 import org.slf4j.LoggerFactory;
37 import org.testng.Assert;
39 public class IterationreportServiceTest extends AbstractPoxServiceTestImpl<AbstractCommonList, IterationreportsCommon> {
42 private final String CLASS_NAME = IterationreportServiceTest.class.getName();
43 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
44 // Instance variables specific to this test.
45 /** The service path component. */
46 final String SERVICE_NAME = "transports";
47 final String SERVICE_PATH_COMPONENT = "transports";
51 * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
54 protected AbstractCommonList getCommonList(Response response) {
55 return response.readEntity(AbstractCommonList.class);
58 // ---------------------------------------------------------------
59 // CRUD tests : CREATE tests
60 // ---------------------------------------------------------------
65 * @see org.collectionspace.services.client.test.ServiceTest#create(java.lang.String)
68 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
69 public void create(String testName) throws Exception {
70 // Perform setup, such as initializing the type of service request
71 // (e.g. CREATE, DELETE), its valid and expected status codes, and
72 // its associated HTTP method name (e.g. POST, DELETE).
75 // Submit the request to the service and store the response.
76 IterationreportClient client = new IterationreportClient();
77 String identifier = createIdentifier();
78 PoxPayloadOut multipart = createIterationreportInstance(identifier);
80 Response res = client.create(multipart);
82 int statusCode = res.getStatus();
84 // Check the status code of the response: does it match
85 // the expected response(s)?
88 // Does it fall within the set of valid status codes?
89 // Does it exactly match the expected status code?
90 if (logger.isDebugEnabled()) {
91 logger.debug(testName + ": status = " + statusCode);
93 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
94 invalidStatusCodeMessage(testRequestType, statusCode));
95 Assert.assertEquals(statusCode, testExpectedStatusCode);
97 newID = extractId(res);
104 // Store the ID returned from the first resource created
105 // for additional tests below.
106 if (knownResourceId == null) {
107 knownResourceId = newID;
108 if (logger.isDebugEnabled()) {
109 logger.debug(testName + ": knownResourceId=" + knownResourceId);
113 // Store the IDs from every resource created by tests,
114 // so they can be deleted after tests have been run.
115 allResourceIdsCreated.add(newID);
119 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#createList(java.lang.String)
122 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
123 // dependsOnMethods = {"create"})
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 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
141 // dependsOnMethods = {"create"})
142 public void read(String testName) throws Exception {
146 // Submit the request to the service and store the response.
147 IterationreportClient client = new IterationreportClient();
148 Response res = client.read(knownResourceId);
149 PoxPayloadIn input = null;
151 assertStatusCode(res, testName);
152 input = new PoxPayloadIn(res.readEntity(String.class));
159 // Get the common part of the response and verify that it is not null.
160 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
161 IterationreportsCommon transportCommon = null;
162 if (payloadInputPart != null) {
163 transportCommon = (IterationreportsCommon) payloadInputPart.getBody();
165 Assert.assertNotNull(transportCommon);
171 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#readNonExistent(java.lang.String)
174 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
175 // dependsOnMethods = {"read"})
176 public void readNonExistent(String testName) throws Exception {
178 setupReadNonExistent();
180 // Submit the request to the service and store the response.
181 IterationreportClient client = new IterationreportClient();
182 Response res = client.read(NON_EXISTENT_ID);
184 int statusCode = res.getStatus();
186 // Check the status code of the response: does it match
187 // the expected response(s)?
188 if (logger.isDebugEnabled()) {
189 logger.debug(testName + ": status = " + statusCode);
191 Assert.assertTrue(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 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
212 // dependsOnMethods = {"createList", "read"})
213 public void readList(String testName) throws Exception {
217 // Submit the request to the service and store the response.
218 AbstractCommonList list = null;
219 IterationreportClient client = new IterationreportClient();
220 Response res = client.readList();
221 assertStatusCode(res, testName);
223 int statusCode = res.getStatus();
225 // Check the status code of the response: does it match
226 // the expected response(s)?
227 if (logger.isDebugEnabled()) {
228 logger.debug(testName + ": status = " + statusCode);
230 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
231 invalidStatusCodeMessage(testRequestType, statusCode));
232 Assert.assertEquals(statusCode, testExpectedStatusCode);
234 list = res.readEntity(getCommonListType());
241 // Optionally output additional data about list members for debugging.
242 boolean iterateThroughList = true;
243 if(iterateThroughList && logger.isDebugEnabled()){
244 AbstractCommonListUtils.ListItemsInAbstractCommonList(list, logger, testName);
252 // ---------------------------------------------------------------
253 // CRUD tests : UPDATE tests
254 // ---------------------------------------------------------------
259 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#update(java.lang.String)
262 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
263 // dependsOnMethods = {"read"})
264 public void update(String testName) throws Exception {
268 // Retrieve the contents of a resource to update.
269 IterationreportClient client = new IterationreportClient();
270 Response res = client.read(knownResourceId);
271 PoxPayloadIn input = null;
273 assertStatusCode(res, testName);
274 input = new PoxPayloadIn(res.readEntity(String.class));
275 if (logger.isDebugEnabled()) {
276 logger.debug("got object to update with ID: " + knownResourceId);
284 // Extract the common part from the response.
285 PayloadInputPart payloadInputPart = input.getPart(client.getCommonPartName());
286 IterationreportsCommon transportCommon = null;
287 if (payloadInputPart != null) {
288 transportCommon = (IterationreportsCommon) payloadInputPart.getBody();
290 Assert.assertNotNull(transportCommon);
292 // Update the content of this resource.
293 transportCommon.setIterationreportReferenceNumber("updated-" + transportCommon.getIterationreportReferenceNumber());
295 if (logger.isDebugEnabled()) {
296 logger.debug("to be updated object");
297 logger.debug(objectAsXmlString(transportCommon, IterationreportsCommon.class));
302 // Submit the updated common part in an update request to the service
303 // and store the response.
304 PoxPayloadOut output = new PoxPayloadOut(this.getServicePathComponent());
305 PayloadOutputPart commonPart = output.addPart(client.getCommonPartName(), transportCommon);
306 res = client.update(knownResourceId, output);
308 assertStatusCode(res, testName);
309 int statusCode = res.getStatus();
310 // Check the status code of the response: does it match the expected response(s)?
311 if (logger.isDebugEnabled()) {
312 logger.debug(testName + ": status = " + statusCode);
314 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
315 invalidStatusCodeMessage(testRequestType, statusCode));
316 Assert.assertEquals(statusCode, testExpectedStatusCode);
317 input = new PoxPayloadIn(res.readEntity(String.class));
324 // Extract the updated common part from the response.
325 payloadInputPart = input.getPart(client.getCommonPartName());
326 IterationreportsCommon updatedIterationreportCommon = null;
327 if (payloadInputPart != null) {
328 updatedIterationreportCommon = (IterationreportsCommon) payloadInputPart.getBody();
330 Assert.assertNotNull(updatedIterationreportCommon);
332 // Check selected fields in the updated common part.
333 Assert.assertEquals(updatedIterationreportCommon.getIterationreportReferenceNumber(),
334 transportCommon.getIterationreportReferenceNumber(),
335 "Data in updated object did not match submitted data.");
339 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
340 // dependsOnMethods = {"update", "testSubmitRequest"})
341 public void updateNonExistent(String testName) throws Exception {
343 setupUpdateNonExistent();
345 // Submit the request to the service and store the response.
346 // Note: The ID used in this 'create' call may be arbitrary.
347 // The only relevant ID may be the one used in update(), below.
348 IterationreportClient client = new IterationreportClient();
349 PoxPayloadOut multipart = createIterationreportInstance(NON_EXISTENT_ID);
350 Response res = client.update(NON_EXISTENT_ID, multipart);
352 int statusCode = res.getStatus();
354 // Check the status code of the response: does it match
355 // the expected response(s)?
356 if (logger.isDebugEnabled()) {
357 logger.debug(testName + ": status = " + statusCode);
359 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
360 invalidStatusCodeMessage(testRequestType, statusCode));
361 Assert.assertEquals(statusCode, testExpectedStatusCode);
369 // ---------------------------------------------------------------
370 // CRUD tests : DELETE tests
371 // ---------------------------------------------------------------
376 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#delete(java.lang.String)
379 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
380 // dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
381 public void delete(String testName) throws Exception {
385 // Submit the request to the service and store the response.
386 IterationreportClient client = new IterationreportClient();
387 Response res = client.delete(knownResourceId);
389 int statusCode = res.getStatus();
391 // Check the status code of the response: does it match
392 // the expected response(s)?
393 if (logger.isDebugEnabled()) {
394 logger.debug(testName + ": status = " + statusCode);
396 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
397 invalidStatusCodeMessage(testRequestType, statusCode));
398 Assert.assertEquals(statusCode, testExpectedStatusCode);
409 * @see org.collectionspace.services.client.test.AbstractServiceTestImpl#deleteNonExistent(java.lang.String)
412 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
413 // dependsOnMethods = {"delete"})
414 public void deleteNonExistent(String testName) throws Exception {
416 setupDeleteNonExistent();
418 // Submit the request to the service and store the response.
419 IterationreportClient client = new IterationreportClient();
420 Response res = client.delete(NON_EXISTENT_ID);
422 int statusCode = res.getStatus();
424 // Check the status code of the response: does it match
425 // the expected response(s)?
426 if (logger.isDebugEnabled()) {
427 logger.debug(testName + ": status = " + statusCode);
429 Assert.assertTrue(testRequestType.isValidStatusCode(statusCode),
430 invalidStatusCodeMessage(testRequestType, statusCode));
431 Assert.assertEquals(statusCode, testExpectedStatusCode);
439 // ---------------------------------------------------------------
440 // Utility tests : tests of code used in tests above
441 // ---------------------------------------------------------------
444 * Tests the code for manually submitting data that is used by several
445 * of the methods above.
447 // @Test(dependsOnMethods = {"create", "read"})
448 public void testSubmitRequest() {
450 // Expected status code: 200 OK
451 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode();
453 // Submit the request to the service and store the response.
454 String method = ServiceRequestType.READ.httpMethodName();
455 String url = getResourceURL(knownResourceId);
456 int statusCode = submitRequest(method, url);
458 // Check the status code of the response: does it match
459 // the expected response(s)?
460 if (logger.isDebugEnabled()) {
461 logger.debug("testSubmitRequest: url=" + url
462 + " status=" + statusCode);
464 Assert.assertEquals(statusCode, EXPECTED_STATUS);
468 // ---------------------------------------------------------------
469 // Utility methods used by tests above
470 // ---------------------------------------------------------------
473 protected PoxPayloadOut createInstance(String identifier) throws Exception {
474 return createIterationreportInstance(identifier);
478 * Creates the transport instance.
480 * @param transportNumber the transport number
481 * @return the multipart output
484 private PoxPayloadOut createIterationreportInstance(String transportNumber) throws Exception {
486 IterationreportsCommon transportCommon = new IterationreportsCommon();
487 transportCommon.setIterationreportReferenceNumber(transportNumber);
488 // transportCommon.setDestination(getUTF8DataFragment());
490 PoxPayloadOut multipart = new PoxPayloadOut(this.getServicePathComponent());
491 PayloadOutputPart commonPart =
492 multipart.addPart(new IterationreportClient().getCommonPartName(), transportCommon);
494 if (logger.isDebugEnabled()) {
495 logger.debug("to be created, transport common");
496 logger.debug(objectAsXmlString(transportCommon, IterationreportsCommon.class));
503 public void CRUDTests(String testName) {
504 // TODO Auto-generated method stub
509 protected PoxPayloadOut createInstance(String commonPartName,
510 String identifier) throws Exception {
511 PoxPayloadOut result = createIterationreportInstance(identifier);
516 protected IterationreportsCommon updateInstance(IterationreportsCommon commonPartObject) {
517 // TODO Auto-generated method stub
522 protected void compareUpdatedInstances(IterationreportsCommon original,
523 IterationreportsCommon updated) throws Exception {
524 // TODO Auto-generated method stub
529 * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
532 protected CollectionSpaceClient getClientInstance() throws Exception {
533 return new IterationreportClient();
537 protected CollectionSpaceClient getClientInstance(String clientPropertiesFilename) throws Exception {
538 return new IterationreportClient(clientPropertiesFilename);
542 * @see org.collectionspace.services.client.test.BaseServiceTest#getServicePathComponent()
545 protected String getServicePathComponent() throws Exception {
546 return SERVICE_PATH_COMPONENT;
550 protected String getServiceName() {