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.CollectionSpaceClient;
30 import org.collectionspace.services.client.PayloadOutputPart;
31 import org.collectionspace.services.client.PoxPayloadIn;
32 import org.collectionspace.services.client.PoxPayloadOut;
34 import org.collectionspace.services.jaxb.AbstractCommonList;
35 import org.collectionspace.services.common.workflow.client.WorkflowClient;
36 import org.collectionspace.services.workflow.WorkflowsCommon;
37 import org.collectionspace.services.client.DimensionClient;
38 import org.collectionspace.services.dimension.DimensionsCommon;
40 import org.jboss.resteasy.client.ClientResponse;
42 import org.testng.Assert;
43 import org.testng.annotations.Test;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * ObjectExitServiceTest, carries out tests against a deployed and running ObjectExit Service. <p/>
50 * $LastChangedRevision: $
53 public class WorkflowServiceTest extends AbstractServiceTestImpl {
55 private final String CLASS_NAME = WorkflowServiceTest.class.getName();
56 private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
57 private String knownResourceId = null;
60 public String getServicePathComponent() {
61 return WorkflowClient.SERVICE_PATH_COMPONENT;
65 protected String getServiceName() {
66 return WorkflowClient.SERVICE_NAME;
70 protected CollectionSpaceClient getClientInstance() {
71 return new WorkflowClient();
75 protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
76 return response.getEntity(AbstractCommonList.class);
80 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
81 // public void create(String testName) throws Exception {
82 // logger.debug(testBanner(testName, CLASS_NAME));
84 // WorkflowClient client = new WorkflowClient();
85 // PoxPayloadOut multipart = createObjectExitInstance(createIdentifier());
86 // ClientResponse<Response> res = client.create(multipart);
87 // assertStatusCode(res, testName);
88 // if (knownResourceId == null) {
89 // knownResourceId = extractId(res); // Store the ID returned from the first resource created for additional tests below.
90 // logger.debug(testName + ": knownResourceId=" + knownResourceId);
92 // allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run.
96 * Create a Dimension instance to use as our test target.
98 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
99 public void createTestObject(String testName) throws Exception {
100 logger.debug(testBanner(testName, CLASS_NAME));
102 DimensionClient client = new DimensionClient();
103 PoxPayloadOut multipart = createDimensionInstance(createIdentifier());
104 ClientResponse<Response> res = client.create(multipart);
105 assertStatusCode(res, testName);
106 if (knownResourceId == null) {
107 knownResourceId = extractId(res); // Store the ID returned from the first resource created for additional tests below.
108 logger.debug(testName + ": knownResourceId=" + knownResourceId);
110 allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run.
114 public void createList(String testName) throws Exception {
119 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"createTestObject"})
120 public void read(String testName) throws Exception {
121 logger.debug(testBanner(testName, CLASS_NAME));
123 DimensionClient client = new DimensionClient();
124 ClientResponse<String> res = client.getWorkflow(knownResourceId);
125 assertStatusCode(res, testName);
126 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
127 WorkflowsCommon workflowsCommon = (WorkflowsCommon) extractPart(input, WorkflowClient.SERVICE_COMMONPART_NAME, WorkflowsCommon.class);
128 if (logger.isDebugEnabled() == true) {
129 logger.debug("Workflow payload is: " + input.getXmlPayload());
131 Assert.assertNotNull(workflowsCommon);
135 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"createList", "read"})
136 // public void readList(String testName) throws Exception {
137 // logger.debug(testBanner(testName, CLASS_NAME));
139 // WorkflowClient client = new WorkflowClient();
140 // ClientResponse<AbstractCommonList> res = client.readList();
141 // AbstractCommonList list = res.getEntity();
142 // assertStatusCode(res, testName);
143 // if (logger.isDebugEnabled()) {
144 // List<AbstractCommonList.ListItem> items =
145 // list.getListItem();
147 // for(AbstractCommonList.ListItem item : items){
148 // logger.debug(testName + ": list-item[" + i + "] " +
156 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"read"})
157 public void update(String testName) throws Exception {
158 logger.debug(testBanner(testName, CLASS_NAME));
160 WorkflowClient client = new WorkflowClient();
161 ClientResponse<String> res = client.read(knownResourceId);
162 assertStatusCode(res, testName);
163 logger.debug("got object to update with ID: " + knownResourceId);
164 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
165 WorkflowsCommon objectexit = (WorkflowsCommon) extractPart(input, client.getCommonPartName(), WorkflowsCommon.class);
166 Assert.assertNotNull(objectexit);
168 // objectexit.setExitNumber("updated-" + objectexit.getExitNumber());
169 logger.debug("Object to be updated:"+objectAsXmlString(objectexit, WorkflowsCommon.class));
170 PoxPayloadOut output = new PoxPayloadOut(WorkflowClient.SERVICE_PAYLOAD_NAME);
171 PayloadOutputPart commonPart = output.addPart(objectexit, MediaType.APPLICATION_XML_TYPE);
172 commonPart.setLabel(client.getCommonPartName());
173 res = client.update(knownResourceId, output);
174 assertStatusCode(res, testName);
175 input = new PoxPayloadIn(res.getEntity());
176 WorkflowsCommon updatedObjectExit = (WorkflowsCommon) extractPart(input, client.getCommonPartName(), WorkflowsCommon.class);
177 Assert.assertNotNull(updatedObjectExit);
181 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"update", "testSubmitRequest"})
182 public void updateNonExistent(String testName) throws Exception {
183 logger.debug(testBanner(testName, CLASS_NAME));
184 setupUpdateNonExistent();
185 // Submit the request to the service and store the response.
186 // Note: The ID used in this 'create' call may be arbitrary.
187 // The only relevant ID may be the one used in update(), below.
188 WorkflowClient client = new WorkflowClient();
189 PoxPayloadOut multipart = createDimensionInstance(NON_EXISTENT_ID);
190 ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
191 assertStatusCode(res, testName);
195 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
196 public void delete(String testName) throws Exception {
197 logger.debug(testBanner(testName, CLASS_NAME));
199 WorkflowClient client = new WorkflowClient();
200 ClientResponse<Response> res = client.delete(knownResourceId);
201 assertStatusCode(res, testName);
204 // ---------------------------------------------------------------
205 // Failure outcome tests : means we expect response to fail, but test to succeed
206 // ---------------------------------------------------------------
210 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"read"})
211 public void readNonExistent(String testName) throws Exception {
212 logger.debug(testBanner(testName, CLASS_NAME));
213 setupReadNonExistent();
214 WorkflowClient client = new WorkflowClient();
215 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
216 assertStatusCode(res, testName);
221 // @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"delete"})
222 public void deleteNonExistent(String testName) throws Exception {
223 logger.debug(testBanner(testName, CLASS_NAME));
224 setupDeleteNonExistent();
225 WorkflowClient client = new WorkflowClient();
226 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
227 assertStatusCode(res, testName);
231 // Placeholders until the tests below can be implemented. See Issue CSPACE-401.
234 public void createWithEmptyEntityBody(String testName) throws Exception {
238 public void createWithMalformedXml(String testName) throws Exception {
242 public void createWithWrongXmlSchema(String testName) throws Exception {
246 public void updateWithEmptyEntityBody(String testName) throws Exception {
250 public void updateWithMalformedXml(String testName) throws Exception {
254 public void updateWithWrongXmlSchema(String testName) throws Exception {
257 // ---------------------------------------------------------------
258 // Utility tests : tests of code used in tests above
259 // ---------------------------------------------------------------
261 // @Test(dependsOnMethods = {"create", "read"})
262 public void testSubmitRequest() {
263 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode(); // Expected status code: 200 OK
264 String method = ServiceRequestType.READ.httpMethodName();
265 String url = getResourceURL(knownResourceId);
266 int statusCode = submitRequest(method, url);
267 logger.debug("testSubmitRequest: url=" + url + " status=" + statusCode);
268 Assert.assertEquals(statusCode, EXPECTED_STATUS);
271 // ---------------------------------------------------------------
272 // Utility methods used by tests above
273 // ---------------------------------------------------------------
274 private PoxPayloadOut createDimensionInstance(String dimensionValue) {
275 String value = "dimensionValue-" + dimensionValue;
276 String dimensionsCommonPartName = new DimensionClient().getCommonPartName();
277 DimensionsCommon dimensionsCommon = new DimensionsCommon();
279 dimensionsCommon.setValue(value);
280 PoxPayloadOut multipart = new PoxPayloadOut(DimensionClient.SERVICE_PAYLOAD_NAME);
281 PayloadOutputPart commonPart = multipart.addPart(dimensionsCommonPartName, dimensionsCommon);
283 if (logger.isDebugEnabled()) {
284 logger.debug("To be created, Dimensions common: " + commonPart.asXML());
285 logger.debug(objectAsXmlString(dimensionsCommon, DimensionsCommon.class));
292 public void create(String testName) throws Exception {
297 public void readList(String testName) throws Exception {
302 public void readPaginatedList(String testName) throws Exception {