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;
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
30 import org.collectionspace.services.client.CollectionSpaceClient;
31 import org.collectionspace.services.client.PayloadOutputPart;
32 import org.collectionspace.services.client.PoxPayloadIn;
33 import org.collectionspace.services.client.PoxPayloadOut;
35 import org.collectionspace.services.jaxb.AbstractCommonList;
36 import org.collectionspace.services.workflow.WorkflowsCommon;
37 import org.collectionspace.services.client.DimensionClient;
38 import org.collectionspace.services.client.workflow.WorkflowClient;
39 import org.collectionspace.services.dimension.DimensionsCommon;
40 import org.collectionspace.services.dimension.DimensionsCommonList;
42 import org.jboss.resteasy.client.ClientResponse;
44 import org.testng.Assert;
45 import org.testng.annotations.Test;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
51 * ObjectExitServiceTest, carries out tests against a deployed and running ObjectExit Service. <p/>
52 * $LastChangedRevision: $
55 public class WorkflowServiceTest extends AbstractServiceTestImpl {
57 private final String CLASS_NAME = WorkflowServiceTest.class.getName();
58 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
59 private String knownResourceId = null;
62 public String getServicePathComponent() {
63 return WorkflowClient.SERVICE_PATH_COMPONENT;
67 protected String getServiceName() {
68 return WorkflowClient.SERVICE_NAME;
72 protected CollectionSpaceClient getClientInstance() {
73 return new DimensionClient();
77 protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
78 return response.getEntity(AbstractCommonList.class);
82 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
83 // public void create(String testName) throws Exception {
84 // logger.debug(testBanner(testName, CLASS_NAME));
86 // WorkflowClient client = new WorkflowClient();
87 // PoxPayloadOut multipart = createObjectExitInstance(createIdentifier());
88 // ClientResponse<Response> res = client.create(multipart);
89 // assertStatusCode(res, testName);
90 // if (knownResourceId == null) {
91 // knownResourceId = extractId(res); // Store the ID returned from the first resource created for additional tests below.
92 // logger.debug(testName + ": knownResourceId=" + knownResourceId);
94 // allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run.
99 * Create a Dimension instance to use as our test target.
101 public void createTestObject(String testName) throws Exception {
102 logger.debug(testBanner(testName, CLASS_NAME));
104 DimensionClient client = new DimensionClient();
105 PoxPayloadOut multipart = createDimensionInstance(createIdentifier());
106 ClientResponse<Response> res = client.create(multipart);
107 assertStatusCode(res, testName);
108 if (knownResourceId == null) {
109 knownResourceId = extractId(res); // Store the ID returned from the first resource created for additional tests below.
110 logger.debug(testName + ": knownResourceId=" + knownResourceId);
112 allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run.
116 public void createList(String testName) throws Exception {
121 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"})
122 public void read(String testName) throws Exception {
123 logger.debug(testBanner(testName, CLASS_NAME));
125 DimensionClient client = new DimensionClient();
126 ClientResponse<String> res = client.getWorkflow(knownResourceId);
127 assertStatusCode(res, testName);
128 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
129 WorkflowsCommon workflowsCommon = (WorkflowsCommon) extractPart(input, WorkflowClient.SERVICE_COMMONPART_NAME, WorkflowsCommon.class);
130 if (logger.isDebugEnabled() == true) {
131 logger.debug("Workflow payload is: " + input.getXmlPayload());
133 Assert.assertNotNull(workflowsCommon);
137 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"createList", "read"})
138 // public void readList(String testName) throws Exception {
139 // logger.debug(testBanner(testName, CLASS_NAME));
141 // WorkflowClient client = new WorkflowClient();
142 // ClientResponse<AbstractCommonList> res = client.readList();
143 // AbstractCommonList list = res.getEntity();
144 // assertStatusCode(res, testName);
145 // if (logger.isDebugEnabled()) {
146 // List<AbstractCommonList.ListItem> items =
147 // list.getListItem();
149 // for(AbstractCommonList.ListItem item : items){
150 // logger.debug(testName + ": list-item[" + i + "] " +
158 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"read"})
159 public void update(String testName) throws Exception {
160 logger.debug(testBanner(testName, CLASS_NAME));
162 updateLifeCycleState(testName, knownResourceId, WorkflowClient.WORKFLOWSTATE_APPROVED);
165 private void updateLifeCycleState(String testName, String resourceId, String lifeCycleState) throws Exception {
167 // Read the existing object
169 DimensionClient client = new DimensionClient();
170 ClientResponse<String> res = client.getWorkflow(resourceId);
171 assertStatusCode(res, testName);
172 logger.debug("Got object to update life cycle state with ID: " + resourceId);
173 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
174 WorkflowsCommon workflowCommons = (WorkflowsCommon) extractPart(input, WorkflowClient.SERVICE_COMMONPART_NAME, WorkflowsCommon.class);
175 Assert.assertNotNull(workflowCommons);
177 // Mark it for a soft delete.
179 logger.debug("Current workflow state:" + objectAsXmlString(workflowCommons, WorkflowsCommon.class));
180 workflowCommons.setCurrentLifeCycleState(lifeCycleState);
181 PoxPayloadOut output = new PoxPayloadOut(WorkflowClient.SERVICE_PAYLOAD_NAME);
182 PayloadOutputPart commonPart = output.addPart(workflowCommons, MediaType.APPLICATION_XML_TYPE);
183 commonPart.setLabel(WorkflowClient.SERVICE_COMMONPART_NAME);
185 // Perform the update
187 res = client.updateWorkflow(resourceId, output);
188 assertStatusCode(res, testName);
189 input = new PoxPayloadIn(res.getEntity());
190 WorkflowsCommon updatedWorkflowCommons = (WorkflowsCommon) extractPart(input, WorkflowClient.SERVICE_COMMONPART_NAME, WorkflowsCommon.class);
191 Assert.assertNotNull(updatedWorkflowCommons);
193 // Read the updated object and make sure it was updated correctly.
195 res = client.getWorkflow(resourceId);
196 assertStatusCode(res, testName);
197 logger.debug("Got workflow state of updated object with ID: " + resourceId);
198 input = new PoxPayloadIn(res.getEntity());
199 updatedWorkflowCommons = (WorkflowsCommon) extractPart(input, WorkflowClient.SERVICE_COMMONPART_NAME, WorkflowsCommon.class);
200 Assert.assertNotNull(workflowCommons);
201 Assert.assertEquals(updatedWorkflowCommons.getCurrentLifeCycleState(), lifeCycleState);
205 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"update", "testSubmitRequest"})
206 public void updateNonExistent(String testName) throws Exception {
207 logger.debug(testBanner(testName, CLASS_NAME));
208 setupUpdateNonExistent();
209 // Submit the request to the service and store the response.
210 // Note: The ID used in this 'create' call may be arbitrary.
211 // The only relevant ID may be the one used in update(), below.
212 WorkflowClient client = new WorkflowClient();
213 PoxPayloadOut multipart = createDimensionInstance(NON_EXISTENT_ID);
214 ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
215 assertStatusCode(res, testName);
219 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
220 public void delete(String testName) throws Exception {
221 logger.debug(testBanner(testName, CLASS_NAME));
223 WorkflowClient client = new WorkflowClient();
224 ClientResponse<Response> res = client.delete(knownResourceId);
225 assertStatusCode(res, testName);
228 // ---------------------------------------------------------------
229 // Failure outcome tests : means we expect response to fail, but test to succeed
230 // ---------------------------------------------------------------
234 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"read"})
235 public void readNonExistent(String testName) throws Exception {
236 logger.debug(testBanner(testName, CLASS_NAME));
237 setupReadNonExistent();
238 WorkflowClient client = new WorkflowClient();
239 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
240 assertStatusCode(res, testName);
245 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"delete"})
246 public void deleteNonExistent(String testName) throws Exception {
247 logger.debug(testBanner(testName, CLASS_NAME));
248 setupDeleteNonExistent();
249 WorkflowClient client = new WorkflowClient();
250 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
251 assertStatusCode(res, testName);
255 // Placeholders until the tests below can be implemented. See Issue CSPACE-401.
258 public void createWithEmptyEntityBody(String testName) throws Exception {
262 public void createWithMalformedXml(String testName) throws Exception {
266 public void createWithWrongXmlSchema(String testName) throws Exception {
270 public void updateWithEmptyEntityBody(String testName) throws Exception {
274 public void updateWithMalformedXml(String testName) throws Exception {
278 public void updateWithWrongXmlSchema(String testName) throws Exception {
281 // ---------------------------------------------------------------
282 // Utility tests : tests of code used in tests above
283 // ---------------------------------------------------------------
285 // @Test(dependsOnMethods = {"create", "read"})
286 public void testSubmitRequest() {
287 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode(); // Expected status code: 200 OK
288 String method = ServiceRequestType.READ.httpMethodName();
289 String url = getResourceURL(knownResourceId);
290 int statusCode = submitRequest(method, url);
291 logger.debug("testSubmitRequest: url=" + url + " status=" + statusCode);
292 Assert.assertEquals(statusCode, EXPECTED_STATUS);
295 // ---------------------------------------------------------------
296 // Utility methods used by tests above
297 // ---------------------------------------------------------------
298 private PoxPayloadOut createDimensionInstance(String dimensionValue) {
299 String value = "dimensionValue-" + dimensionValue;
300 String dimensionsCommonPartName = new DimensionClient().getCommonPartName();
301 DimensionsCommon dimensionsCommon = new DimensionsCommon();
303 dimensionsCommon.setValue(value);
304 PoxPayloadOut multipart = new PoxPayloadOut(DimensionClient.SERVICE_PAYLOAD_NAME);
305 PayloadOutputPart commonPart = multipart.addPart(dimensionsCommonPartName, dimensionsCommon);
307 if (logger.isDebugEnabled()) {
308 logger.debug("To be created, Dimensions common: " + commonPart.asXML());
309 logger.debug(objectAsXmlString(dimensionsCommon, DimensionsCommon.class));
316 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
317 public void create(String testName) throws Exception {
318 this.createTestObject(testName);
321 private int readIncludeDeleted(String testName, Boolean includeDeleted) {
326 // Submit the request to the service and store the response.
327 DimensionClient client = new DimensionClient();
328 ClientResponse<DimensionsCommonList> res = client.readIncludeDeleted(includeDeleted);
329 DimensionsCommonList list = res.getEntity();
330 int statusCode = res.getStatus();
332 // Check the status code of the response: does it match
333 // the expected response(s)?
335 if (logger.isDebugEnabled()) {
336 logger.debug(testName + ": status = " + statusCode);
338 Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
339 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
340 Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
342 // Now check that list size is correct
344 List<DimensionsCommonList.DimensionListItem> items =
345 list.getDimensionListItem();
346 result = items.size();
352 * This test assumes that no objects exist yet.
354 * http://localhost:8180/cspace-services/intakes?wf_deleted=false
357 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"update"})
358 public void readList(String testName) throws Exception {
360 // Create 3 new objects
362 final int OBJECTS_TOTAL = 3;
363 for (int i = 0; i < OBJECTS_TOTAL; i++) {
364 this.createTestObject(testName);
367 // Mark one as soft deleted
369 int currentTotal = allResourceIdsCreated.size();
370 String csid = allResourceIdsCreated.get(currentTotal - 1); //0-based index to get the last one added
372 this.updateLifeCycleState(testName, csid, WorkflowClient.WORKFLOWSTATE_DELETED);
374 // Read the list back. The deleted item should not be in the list
376 int updatedTotal = readIncludeDeleted(testName, Boolean.FALSE);
377 Assert.assertEquals(updatedTotal, currentTotal - 1, "Deleted items seem to be returned in list results.");
381 public void readPaginatedList(String testName) throws Exception {