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