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 javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
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.jaxb.AbstractCommonList;
34 import org.collectionspace.services.objectexit.ObjectexitCommon;
36 import org.jboss.resteasy.client.ClientResponse;
38 import org.testng.Assert;
39 import org.testng.annotations.Test;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43 //import org.w3c.dom.Element;
44 //import org.w3c.dom.Node;
47 * ObjectExitServiceTest, carries out tests against a deployed and running ObjectExit Service. <p/>
48 * $LastChangedRevision: $
51 public class ObjectExitServiceTest extends AbstractServiceTestImpl {
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;
59 public String getServicePathComponent() {
60 return ObjectExitClient.SERVICE_PATH_COMPONENT;
64 protected String getServiceName() {
65 return ObjectExitClient.SERVICE_NAME;
69 protected CollectionSpaceClient getClientInstance() {
70 return new ObjectExitClient();
74 protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
75 return response.getEntity(AbstractCommonList.class);
79 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
80 public void create(String testName) throws Exception {
81 logger.debug(testBanner(testName, CLASS_NAME));
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);
91 allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run.
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++) {
104 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"})
105 public void read(String testName) throws Exception {
106 logger.debug(testBanner(testName, CLASS_NAME));
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);
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));
121 ObjectExitClient client = new ObjectExitClient();
122 ClientResponse<AbstractCommonList> res = client.readList();
123 String bar = "\r\n\r\n=================================\r\n\r\n";
124 System.out.println(bar+" res: "+res);
125 AbstractCommonList list = res.getEntity();
127 System.out.println(bar+" list: "+list);
128 assertStatusCode(res, testName);
130 // Optionally output additional data about list members for debugging.
131 boolean iterateThroughList = true;
132 if(iterateThroughList && logger.isDebugEnabled()){
133 ListItemsInAbstractCommonList(list, logger, testName);
139 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"read"})
140 public void update(String testName) throws Exception {
141 logger.debug(testBanner(testName, CLASS_NAME));
143 ObjectExitClient client = new ObjectExitClient();
144 ClientResponse<String> res = client.read(knownResourceId);
145 assertStatusCode(res, testName);
146 logger.debug("got object to update with ID: " + knownResourceId);
147 PoxPayloadIn input = new PoxPayloadIn(res.getEntity());
148 ObjectexitCommon objectexit = (ObjectexitCommon) extractPart(input, client.getCommonPartName(), ObjectexitCommon.class);
149 Assert.assertNotNull(objectexit);
151 objectexit.setExitNumber("updated-" + objectexit.getExitNumber());
152 logger.debug("Object to be updated:"+objectAsXmlString(objectexit, ObjectexitCommon.class));
153 PoxPayloadOut output = new PoxPayloadOut(ObjectExitClient.SERVICE_PAYLOAD_NAME);
154 PayloadOutputPart commonPart = output.addPart(objectexit, MediaType.APPLICATION_XML_TYPE);
155 commonPart.setLabel(client.getCommonPartName());
156 res = client.update(knownResourceId, output);
157 assertStatusCode(res, testName);
158 input = new PoxPayloadIn(res.getEntity());
159 ObjectexitCommon updatedObjectExit = (ObjectexitCommon) extractPart(input, client.getCommonPartName(), ObjectexitCommon.class);
160 Assert.assertNotNull(updatedObjectExit);
164 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"update", "testSubmitRequest"})
165 public void updateNonExistent(String testName) throws Exception {
166 logger.debug(testBanner(testName, CLASS_NAME));
167 setupUpdateNonExistent();
168 // Submit the request to the service and store the response.
169 // Note: The ID used in this 'create' call may be arbitrary.
170 // The only relevant ID may be the one used in update(), below.
171 ObjectExitClient client = new ObjectExitClient();
172 PoxPayloadOut multipart = createObjectExitInstance(NON_EXISTENT_ID);
173 ClientResponse<String> res = client.update(NON_EXISTENT_ID, multipart);
174 assertStatusCode(res, testName);
178 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create", "readList", "testSubmitRequest", "update"})
179 public void delete(String testName) throws Exception {
180 logger.debug(testBanner(testName, CLASS_NAME));
182 ObjectExitClient client = new ObjectExitClient();
183 ClientResponse<Response> res = client.delete(knownResourceId);
184 assertStatusCode(res, testName);
187 // ---------------------------------------------------------------
188 // Failure outcome tests : means we expect response to fail, but test to succeed
189 // ---------------------------------------------------------------
193 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"read"})
194 public void readNonExistent(String testName) throws Exception {
195 logger.debug(testBanner(testName, CLASS_NAME));
196 setupReadNonExistent();
197 ObjectExitClient client = new ObjectExitClient();
198 ClientResponse<String> res = client.read(NON_EXISTENT_ID);
199 assertStatusCode(res, testName);
204 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"delete"})
205 public void deleteNonExistent(String testName) throws Exception {
206 logger.debug(testBanner(testName, CLASS_NAME));
207 setupDeleteNonExistent();
208 ObjectExitClient client = new ObjectExitClient();
209 ClientResponse<Response> res = client.delete(NON_EXISTENT_ID);
210 assertStatusCode(res, testName);
214 // Placeholders until the tests below can be implemented. See Issue CSPACE-401.
217 public void createWithEmptyEntityBody(String testName) throws Exception {
221 public void createWithMalformedXml(String testName) throws Exception {
225 public void createWithWrongXmlSchema(String testName) throws Exception {
229 public void updateWithEmptyEntityBody(String testName) throws Exception {
233 public void updateWithMalformedXml(String testName) throws Exception {
237 public void updateWithWrongXmlSchema(String testName) throws Exception {
240 // ---------------------------------------------------------------
241 // Utility tests : tests of code used in tests above
242 // ---------------------------------------------------------------
244 @Test(dependsOnMethods = {"create", "read"})
245 public void testSubmitRequest() {
246 final int EXPECTED_STATUS = Response.Status.OK.getStatusCode(); // Expected status code: 200 OK
247 String method = ServiceRequestType.READ.httpMethodName();
248 String url = getResourceURL(knownResourceId);
249 int statusCode = submitRequest(method, url);
250 logger.debug("testSubmitRequest: url=" + url + " status=" + statusCode);
251 Assert.assertEquals(statusCode, EXPECTED_STATUS);
254 // ---------------------------------------------------------------
255 // Utility methods used by tests above
256 // ---------------------------------------------------------------
259 protected PoxPayloadOut createInstance(String identifier) {
260 ObjectExitClient client = new ObjectExitClient();
261 return createObjectExitInstance(identifier);
264 private PoxPayloadOut createObjectExitInstance(String exitNumber) {
265 String identifier = "objectexitNumber-" + exitNumber;
266 ObjectexitCommon objectexit = new ObjectexitCommon();
267 objectexit.setExitNumber(identifier);
268 objectexit.setDepositor("urn:cspace:org.collectionspace.demo:orgauthority:name(TestOrgAuth):organization:name(Northern Climes Museum)'Northern Climes Museum'");
269 PoxPayloadOut multipart = new PoxPayloadOut(ObjectExitClient.SERVICE_PAYLOAD_NAME);
270 PayloadOutputPart commonPart = multipart.addPart(objectexit, MediaType.APPLICATION_XML_TYPE);
271 commonPart.setLabel(new ObjectExitClient().getCommonPartName());
273 if (logger.isDebugEnabled()) {
274 logger.debug("to be created, objectexit common");
275 logger.debug(objectAsXmlString(objectexit, ObjectexitCommon.class));