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.jaxb.AbstractCommonList;
32 import org.collectionspace.services.objectexit.ObjectexitCommon;
33 import org.collectionspace.services.objectexit.ObjectexitCommonList;
35 import org.jboss.resteasy.client.ClientResponse;
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;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
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 protected CollectionSpaceClient getClientInstance() {
60 return new ObjectExitClient();
64 protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
65 return response.getEntity(ObjectexitCommonList.class);
69 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
70 public void create(String testName) throws Exception {
71 logger.debug(testBanner(testName, CLASS_NAME));
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);
81 allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run.
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++) {
94 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"})
95 public void read(String testName) throws Exception {
96 logger.debug(testBanner(testName, CLASS_NAME));
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);
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));
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();
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());
128 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"read"})
129 public void update(String testName) throws Exception {
130 logger.debug(testBanner(testName, CLASS_NAME));
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);
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);
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);
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));
171 ObjectExitClient client = new ObjectExitClient();
172 ClientResponse<Response> res = client.delete(knownResourceId);
173 assertStatusCode(res, testName);
176 // ---------------------------------------------------------------
177 // Failure outcome tests : means we expect response to fail, but test to succeed
178 // ---------------------------------------------------------------
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);
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);
203 // Placeholders until the tests below can be implemented. See Issue CSPACE-401.
206 public void createWithEmptyEntityBody(String testName) throws Exception {
210 public void createWithMalformedXml(String testName) throws Exception {
214 public void createWithWrongXmlSchema(String testName) throws Exception {
218 public void updateWithEmptyEntityBody(String testName) throws Exception {
222 public void updateWithMalformedXml(String testName) throws Exception {
226 public void updateWithWrongXmlSchema(String testName) throws Exception {
229 // ---------------------------------------------------------------
230 // Utility tests : tests of code used in tests above
231 // ---------------------------------------------------------------
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);
243 // ---------------------------------------------------------------
244 // Utility methods used by tests above
245 // ---------------------------------------------------------------
248 public String getServicePathComponent() {
249 return SERVICE_PATH_COMPONENT;
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());
261 if (logger.isDebugEnabled()) {
262 logger.debug("to be created, objectexit common");
263 logger.debug(objectAsXmlString(objectexit, ObjectexitCommon.class));