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