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