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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright © 2009 Regents of the University of California
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
15 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
23 package org.collectionspace.services.client.test;
25 import java.util.List;
26 import javax.ws.rs.core.MediaType;
27 import javax.ws.rs.core.Response;
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;
39 import org.jboss.resteasy.client.ClientResponse;
41 import org.testng.Assert;
42 import org.testng.annotations.Test;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
46 //import org.w3c.dom.Element;
47 //import org.w3c.dom.Node;
50 * ObjectExitServiceTest, carries out tests against a deployed and running ObjectExit Service. <p/>
51 * $LastChangedRevision: $
54 public class ObjectExitServiceTest extends AbstractServiceTestImpl {
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;
62 public String getServicePathComponent() {
63 return ObjectExitClient.SERVICE_PATH_COMPONENT;
67 protected String getServiceName() {
68 return ObjectExitClient.SERVICE_NAME;
72 protected CollectionSpaceClient getClientInstance() {
73 return new ObjectExitClient();
77 protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
78 return response.getEntity(ObjectexitCommonList.class);
82 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
83 public void create(String testName) throws Exception {
84 logger.debug(testBanner(testName, CLASS_NAME));
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);
94 allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run.
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++) {
107 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"})
108 public void read(String testName) throws Exception {
109 logger.debug(testBanner(testName, CLASS_NAME));
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);
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));
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();
130 System.out.println(bar+" list: "+list);
131 assertStatusCode(res, testName);
133 if (logger.isDebugEnabled()) {
134 List<AbstractCommonList.ListItem> items =
137 for(AbstractCommonList.ListItem item : items){
138 logger.debug(testName + ": list-item[" + i + "] " +
145 List<AbstractCommonList.ListItem> items = list.getListItem();
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();
153 elementStrings.append("["+el.getNodeName()+":"+textEl.getNodeValue()+"] ");
156 System.out.println("\r\n\r\n\r\n~~~~~~~~~~~~~~~~~~~~~~~~~~~"+testName + ": list-item[" + i + "]: "+elementStrings.toString());
163 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"read"})
164 public void update(String testName) throws Exception {
165 logger.debug(testBanner(testName, CLASS_NAME));
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);
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);
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);
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));
206 ObjectExitClient client = new ObjectExitClient();
207 ClientResponse<Response> res = client.delete(knownResourceId);
208 assertStatusCode(res, testName);
211 // ---------------------------------------------------------------
212 // Failure outcome tests : means we expect response to fail, but test to succeed
213 // ---------------------------------------------------------------
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);
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);
238 // Placeholders until the tests below can be implemented. See Issue CSPACE-401.
241 public void createWithEmptyEntityBody(String testName) throws Exception {
245 public void createWithMalformedXml(String testName) throws Exception {
249 public void createWithWrongXmlSchema(String testName) throws Exception {
253 public void updateWithEmptyEntityBody(String testName) throws Exception {
257 public void updateWithMalformedXml(String testName) throws Exception {
261 public void updateWithWrongXmlSchema(String testName) throws Exception {
264 // ---------------------------------------------------------------
265 // Utility tests : tests of code used in tests above
266 // ---------------------------------------------------------------
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);
278 // ---------------------------------------------------------------
279 // Utility methods used by tests above
280 // ---------------------------------------------------------------
283 protected PoxPayloadOut createInstance(String identifier) {
284 ObjectExitClient client = new ObjectExitClient();
285 return createObjectExitInstance(identifier);
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());
297 if (logger.isDebugEnabled()) {
298 logger.debug("to be created, objectexit common");
299 logger.debug(objectAsXmlString(objectexit, ObjectexitCommon.class));