]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
3582ae85404c78f34483b9e4f82e8b2cda2238de
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright © 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23 package org.collectionspace.services.client.test;
24
25 import java.util.List;
26
27 import javax.ws.rs.core.MediaType;
28 import javax.ws.rs.core.Response;
29
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;
34
35 import org.collectionspace.services.jaxb.AbstractCommonList;
36 import org.collectionspace.services.common.workflow.client.WorkflowClient;
37 import org.collectionspace.services.workflow.WorkflowsCommon;
38 import org.collectionspace.services.client.DimensionClient;
39 import org.collectionspace.services.dimension.DimensionsCommon;
40 import org.collectionspace.services.dimension.DimensionsCommonList;
41
42 import org.jboss.resteasy.client.ClientResponse;
43
44 import org.testng.Assert;
45 import org.testng.annotations.Test;
46
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
49
50 /**
51  * ObjectExitServiceTest, carries out tests against a deployed and running ObjectExit Service. <p/>
52  * $LastChangedRevision:  $
53  * $LastChangedDate:  $
54  */
55 public class WorkflowServiceTest extends AbstractServiceTestImpl {
56
57     private final String CLASS_NAME = WorkflowServiceTest.class.getName();
58     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
59     private String knownResourceId = null;
60
61     @Override
62         public String getServicePathComponent() {
63                 return WorkflowClient.SERVICE_PATH_COMPONENT;
64         }
65
66         @Override
67         protected String getServiceName() {
68                 return WorkflowClient.SERVICE_NAME;
69         }
70     
71     @Override
72     protected CollectionSpaceClient getClientInstance() {
73         return new WorkflowClient();
74     }
75
76     @Override
77     protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
78         return response.getEntity(AbstractCommonList.class);
79     }
80
81 //    @Override
82 //    @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
83 //    public void create(String testName) throws Exception {
84 //        logger.debug(testBanner(testName, CLASS_NAME));
85 //        setupCreate();
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);
93 //        }
94 //        allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run.
95 //    }
96     
97     
98     /*
99      * Create a Dimension instance to use as our test target.
100      */
101     public void createTestObject(String testName) throws Exception {
102         logger.debug(testBanner(testName, CLASS_NAME));
103         setupCreate();
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);
111         }
112         allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run.
113     }
114
115     @Override
116     public void createList(String testName) throws Exception {
117         //empty N/A
118     }
119
120     @Override
121     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"})
122     public void read(String testName) throws Exception {
123         logger.debug(testBanner(testName, CLASS_NAME));
124         setupRead();
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());
132         }
133         Assert.assertNotNull(workflowsCommon);
134     }
135
136 //    @Override
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));
140 //        setupReadList();
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();
148 //            int i = 0;
149 //            for(AbstractCommonList.ListItem item : items){
150 //                logger.debug(testName + ": list-item[" + i + "] " +
151 //                        item.toString());
152 //                i++;
153 //            }
154 //        }
155 //    }
156     
157     @Override
158     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"read"})
159     public void update(String testName) throws Exception {
160         logger.debug(testBanner(testName, CLASS_NAME));
161         setupUpdate();
162         updateLifeCycleState(testName, knownResourceId, WorkflowClient.WORKFLOWSTATE_APPROVED);
163     }    
164
165     private void updateLifeCycleState(String testName, String resourceId, String lifeCycleState) throws Exception {
166         //
167         // Read the existing object
168         //
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);
176         //
177         // Mark it for a soft delete.
178         //
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);
184         //
185         // Perform the update
186         //
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);
192         //
193         // Read the updated object and make sure it was updated correctly.
194         //
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);
202     }
203
204     @Override
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);
216     }
217
218     @Override
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));
222         setupDelete();
223         WorkflowClient client = new WorkflowClient();
224         ClientResponse<Response> res = client.delete(knownResourceId);
225         assertStatusCode(res, testName);
226     }
227
228     // ---------------------------------------------------------------
229     // Failure outcome tests : means we expect response to fail, but test to succeed
230     // ---------------------------------------------------------------
231
232     // Failure outcome
233     @Override
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);
241     }
242
243     // Failure outcome
244     @Override
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);
252     }
253
254     // Failure outcomes
255     // Placeholders until the tests below can be implemented. See Issue CSPACE-401.
256
257     @Override
258     public void createWithEmptyEntityBody(String testName) throws Exception {
259     }
260
261     @Override
262     public void createWithMalformedXml(String testName) throws Exception {
263     }
264
265     @Override
266     public void createWithWrongXmlSchema(String testName) throws Exception {
267     }
268
269     @Override
270     public void updateWithEmptyEntityBody(String testName) throws Exception {
271     }
272
273     @Override
274     public void updateWithMalformedXml(String testName) throws Exception {
275     }
276
277     @Override
278     public void updateWithWrongXmlSchema(String testName) throws Exception {
279     }
280
281     // ---------------------------------------------------------------
282     // Utility tests : tests of code used in tests above
283     // ---------------------------------------------------------------
284
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);
293     }
294
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();
302         
303         dimensionsCommon.setValue(value);
304         PoxPayloadOut multipart = new PoxPayloadOut(DimensionClient.SERVICE_PAYLOAD_NAME);
305         PayloadOutputPart commonPart = multipart.addPart(dimensionsCommonPartName, dimensionsCommon);
306
307         if (logger.isDebugEnabled()) {
308             logger.debug("To be created, Dimensions common: " + commonPart.asXML());
309             logger.debug(objectAsXmlString(dimensionsCommon, DimensionsCommon.class));
310         }
311
312         return multipart;
313     }
314
315         @Override
316     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
317         public void create(String testName) throws Exception {
318                 this.createTestObject(testName);
319         }
320
321         private void readList(String testName, int expectedSize, String queryParam) {
322         // Perform setup.
323         setupReadList();
324
325         // Submit the request to the service and store the response.
326         DimensionClient client = new DimensionClient();
327         ClientResponse<DimensionsCommonList> res = client.readList();
328         DimensionsCommonList list = res.getEntity();
329         int statusCode = res.getStatus();
330         //
331         // Check the status code of the response: does it match
332         // the expected response(s)?
333         //
334         if (logger.isDebugEnabled()) {
335             logger.debug(testName + ": status = " + statusCode);
336         }
337         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
338                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
339         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
340         //
341         // Now check that list size is correct
342         //
343         List<DimensionsCommonList.DimensionListItem> items =
344             list.getDimensionListItem();
345         Assert.assertEquals(items.size(), expectedSize);
346         }
347         
348         /*
349          * This test assumes that no objects exist yet.
350          * 
351          * http://localhost:8180/cspace-services/intakes?wf_deleted=false
352          */
353     @Override
354     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"update"})
355         public void readList(String testName) throws Exception {
356         //
357         // Create 3 new objects
358         //
359         final int OBJECTS_TOTAL = 3;
360         for (int i = 0; i < OBJECTS_TOTAL; i++) {
361                 this.createTestObject(testName);
362         }
363         //
364         // Mark one as soft deleted
365         //
366         int currentTotal = allResourceIdsCreated.size();
367         String csid = allResourceIdsCreated.get(currentTotal - 1); //get the last one added
368         this.setupUpdate();
369         this.updateLifeCycleState(testName, csid, WorkflowClient.WORKFLOWSTATE_DELETED);
370         //
371         // Read the list back
372         //
373         }
374         
375         @Override
376     public void readPaginatedList(String testName) throws Exception {
377                 //empty N/A
378         }
379         
380 }