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