]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
0e4dba46274fa5d9a391da5474a52e89219e101a
[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 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
28
29 import org.collectionspace.services.client.CollectionSpaceClient;
30 import org.collectionspace.services.client.ObjectExitClient;
31 import org.collectionspace.services.client.ObjectExitProxy;
32 import org.collectionspace.services.client.PayloadOutputPart;
33 import org.collectionspace.services.client.PoxPayloadIn;
34 import org.collectionspace.services.client.PoxPayloadOut;
35 import org.collectionspace.services.jaxb.AbstractCommonList;
36 import org.collectionspace.services.objectexit.ObjectexitCommonList;
37 import org.collectionspace.services.objectexit.ObjectexitCommon;
38
39 import org.jboss.resteasy.client.ClientResponse;
40
41 import org.testng.Assert;
42 import org.testng.annotations.Test;
43
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 //import org.w3c.dom.Element;
47 //import org.w3c.dom.Node;
48
49 /**
50  * ObjectExitServiceTest, carries out tests against a deployed and running ObjectExit Service. <p/>
51  * $LastChangedRevision:  $
52  * $LastChangedDate:  $
53  */
54 public class ObjectExitServiceTest extends AbstractServiceTestImpl {
55
56     private final String CLASS_NAME = ObjectExitServiceTest.class.getName();
57     private final Logger logger = LoggerFactory.getLogger(CLASS_NAME);
58     final String SERVICE_PATH_COMPONENT = "objectexit";
59     private String knownResourceId = null;
60
61     @Override
62         public String getServicePathComponent() {
63                 return ObjectExitClient.SERVICE_PATH_COMPONENT;
64         }
65
66         @Override
67         protected String getServiceName() {
68                 return ObjectExitClient.SERVICE_NAME;
69         }
70     
71     @Override
72     protected CollectionSpaceClient getClientInstance() {
73         return new ObjectExitClient();
74     }
75
76     @Override
77     protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
78         return response.getEntity(ObjectexitCommonList.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         ObjectExitClient client = new ObjectExitClient();
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     @Override
98     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"})
99     public void createList(String testName) throws Exception {
100         logger.debug(testBanner(testName, CLASS_NAME));
101         for (int i = 0; i < 3; i++) {
102             create(testName);
103         }
104     }
105
106     @Override
107     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"})
108     public void read(String testName) throws Exception {
109         logger.debug(testBanner(testName, CLASS_NAME));
110         setupRead();
111         ObjectExitClient client = new ObjectExitClient();
112         ClientResponse<String> res = client.read(knownResourceId);
113         assertStatusCode(res, testName);
114         PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
115         ObjectexitCommon objectexit = (ObjectexitCommon) extractPart(input, client.getCommonPartName(), ObjectexitCommon.class);
116         Assert.assertNotNull(objectexit);
117     }
118
119     @Override
120     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"createList", "read"})
121     public void readList(String testName) throws Exception {
122         logger.debug(testBanner(testName, CLASS_NAME));
123         setupReadList();
124         ObjectExitClient client = new ObjectExitClient();
125         ClientResponse<ObjectexitCommonList> res = client.readList();
126         String bar = "\r\n\r\n=================================\r\n\r\n";
127         System.out.println(bar+" res: "+res);
128         ObjectexitCommonList  list = res.getEntity();
129
130         System.out.println(bar+" list: "+list);
131         assertStatusCode(res, testName);
132
133         if (logger.isDebugEnabled()) {
134             List<AbstractCommonList.ListItem> items =
135                 list.getListItem();
136             int i = 0;
137             for(AbstractCommonList.ListItem item : items){
138                 logger.debug(testName + ": list-item[" + i + "] " +
139                         item.toString());
140                 i++;
141             }
142         }
143
144         /*
145         List<AbstractCommonList.ListItem> items = list.getListItem();
146         int i = 0;
147         for(AbstractCommonList.ListItem item : items){
148             List<Element> elList = item.getAny();
149             StringBuilder elementStrings = new StringBuilder();
150             for(Element el : elList) {
151                 Node textEl = el.getFirstChild();
152                 if (textEl != null){
153                     elementStrings.append("["+el.getNodeName()+":"+textEl.getNodeValue()+"] ");
154                 }
155             }
156             System.out.println("\r\n\r\n\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~"+testName + ": list-item[" + i + "]: "+elementStrings.toString());
157             i++;
158         }
159         */
160     }
161
162     @Override
163     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"read"})
164     public void update(String testName) throws Exception {
165         logger.debug(testBanner(testName, CLASS_NAME));
166         setupUpdate();
167         ObjectExitClient client = new ObjectExitClient();
168         ClientResponse<String> res = client.read(knownResourceId);
169         assertStatusCode(res, testName);
170         logger.debug("got object to update with ID: " + knownResourceId);
171         PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
172         ObjectexitCommon objectexit = (ObjectexitCommon) extractPart(input, client.getCommonPartName(), ObjectexitCommon.class);
173         Assert.assertNotNull(objectexit);
174
175         objectexit.setExitNumber("updated-" + objectexit.getExitNumber());
176         logger.debug("Object to be updated:"+objectAsXmlString(objectexit, ObjectexitCommon.class));
177         PoxPayloadOut output = new PoxPayloadOut(ObjectExitClient.SERVICE_PAYLOAD_NAME);
178         PayloadOutputPart commonPart = output.addPart(objectexit, MediaType.APPLICATION_XML_TYPE);
179         commonPart.setLabel(client.getCommonPartName());
180         res = client.update(knownResourceId, output);
181         assertStatusCode(res, testName);
182         input = new PoxPayloadIn(res.getEntity());
183         ObjectexitCommon updatedObjectExit = (ObjectexitCommon) extractPart(input, client.getCommonPartName(), ObjectexitCommon.class);
184         Assert.assertNotNull(updatedObjectExit);
185     }
186
187     @Override
188     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"update", "testSubmitRequest"})
189     public void updateNonExistent(String testName) throws Exception {
190         logger.debug(testBanner(testName, CLASS_NAME));
191         setupUpdateNonExistent();
192         // Submit the request to the service and store the response.
193         // Note: The ID used in this 'create' call may be arbitrary.
194         // The only relevant ID may be the one used in update(), below.
195         ObjectExitClient client = new ObjectExitClient();
196         PoxPayloadOut multipart = createObjectExitInstance(NON_EXISTENT_ID);
197         ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
198         assertStatusCode(res, testName);
199     }
200
201     @Override
202     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
203     public void delete(String testName) throws Exception {
204         logger.debug(testBanner(testName, CLASS_NAME));
205         setupDelete();
206         ObjectExitClient client = new ObjectExitClient();
207         ClientResponse<Response> res = client.delete(knownResourceId);
208         assertStatusCode(res, testName);
209     }
210
211     // ---------------------------------------------------------------
212     // Failure outcome tests : means we expect response to fail, but test to succeed
213     // ---------------------------------------------------------------
214
215     // Failure outcome
216     @Override
217     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"read"})
218     public void readNonExistent(String testName) throws Exception {
219         logger.debug(testBanner(testName, CLASS_NAME));
220         setupReadNonExistent();
221         ObjectExitClient client = new ObjectExitClient();
222         ClientResponse<String> res = client.read(NON_EXISTENT_ID);
223         assertStatusCode(res, testName);
224     }
225
226     // Failure outcome
227     @Override
228     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"delete"})
229     public void deleteNonExistent(String testName) throws Exception {
230         logger.debug(testBanner(testName, CLASS_NAME));
231         setupDeleteNonExistent();
232         ObjectExitClient client = new ObjectExitClient();
233         ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
234         assertStatusCode(res, testName);
235     }
236
237     // Failure outcomes
238     // Placeholders until the tests below can be implemented. See Issue CSPACE-401.
239
240     @Override
241     public void createWithEmptyEntityBody(String testName) throws Exception {
242     }
243
244     @Override
245     public void createWithMalformedXml(String testName) throws Exception {
246     }
247
248     @Override
249     public void createWithWrongXmlSchema(String testName) throws Exception {
250     }
251
252     @Override
253     public void updateWithEmptyEntityBody(String testName) throws Exception {
254     }
255
256     @Override
257     public void updateWithMalformedXml(String testName) throws Exception {
258     }
259
260     @Override
261     public void updateWithWrongXmlSchema(String testName) throws Exception {
262     }
263
264     // ---------------------------------------------------------------
265     // Utility tests : tests of code used in tests above
266     // ---------------------------------------------------------------
267
268     @Test(dependsOnMethods = {"create", "read"})
269     public void testSubmitRequest() {
270         final int EXPECTED_STATUS = Response.Status.OK.getStatusCode(); // Expected status code: 200 OK
271         String method = ServiceRequestType.READ.httpMethodName();
272         String url = getResourceURL(knownResourceId);
273         int statusCode = submitRequest(method, url);
274         logger.debug("testSubmitRequest: url=" + url + " status=" + statusCode);
275         Assert.assertEquals(statusCode, EXPECTED_STATUS);
276     }
277
278     // ---------------------------------------------------------------
279     // Utility methods used by tests above
280     // ---------------------------------------------------------------
281     
282     @Override
283     protected PoxPayloadOut createInstance(String identifier) {
284         ObjectExitClient client = new ObjectExitClient();
285         return createObjectExitInstance(identifier);
286     }
287     
288     private PoxPayloadOut createObjectExitInstance(String exitNumber) {
289         String identifier = "objectexitNumber-" + exitNumber;
290         ObjectexitCommon objectexit = new ObjectexitCommon();
291         objectexit.setExitNumber(identifier);
292         objectexit.setDepositor("urn:cspace:org.collectionspace.demo:orgauthority:name(TestOrgAuth):organization:name(Northern Climes Museum)'Northern Climes Museum'");
293         PoxPayloadOut multipart = new PoxPayloadOut(ObjectExitClient.SERVICE_PAYLOAD_NAME);
294         PayloadOutputPart commonPart = multipart.addPart(objectexit, MediaType.APPLICATION_XML_TYPE);
295         commonPart.setLabel(new ObjectExitClient().getCommonPartName());
296
297         if (logger.isDebugEnabled()) {
298             logger.debug("to be created, objectexit common");
299             logger.debug(objectAsXmlString(objectexit, ObjectexitCommon.class));
300         }
301
302         return multipart;
303     }
304 }