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;
34 import org.jboss.resteasy.client.ClientResponse;
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;
42 import org.slf4j.Logger;
43 import org.slf4j.LoggerFactory;
46 * ObjectExitServiceTest, carries out tests against a deployed and running ObjectExit Service. <p/>
47 * $LastChangedRevision: $
50 public class ObjectExitServiceTest extends AbstractServiceTestImpl {
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;
58 protected CollectionSpaceClient getClientInstance() {
59 return new ObjectExitClient();
63 protected AbstractCommonList getAbstractCommonList(ClientResponse<AbstractCommonList> response) {
64 return response.getEntity(AbstractCommonList.class);
68 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
69 public void create(String testName) throws Exception {
70 logger.debug(testBanner(testName, CLASS_NAME));
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);
80 allResourceIdsCreated.add(extractId(res)); // Store the IDs from every resource created by tests so they can be deleted after tests have been run.
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++) {
93 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"create"})
94 public void read(String testName) throws Exception {
95 logger.debug(testBanner(testName, CLASS_NAME));
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);
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));
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 =
118 for(AbstractCommonList.ListItem item : items){
119 logger.debug(testName + ": list-item[" + i + "] " +
127 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class, dependsOnMethods = {"read"})
128 public void update(String testName) throws Exception {
129 logger.debug(testBanner(testName, CLASS_NAME));
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);
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);
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);
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));
170 ObjectExitClient client = new ObjectExitClient();
171 ClientResponse<Response> res = client.delete(knownResourceId);
172 assertStatusCode(res, testName);
175 // ---------------------------------------------------------------
176 // Failure outcome tests : means we expect response to fail, but test to succeed
177 // ---------------------------------------------------------------
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);
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);
202 // Placeholders until the tests below can be implemented. See Issue CSPACE-401.
205 public void createWithEmptyEntityBody(String testName) throws Exception {
209 public void createWithMalformedXml(String testName) throws Exception {
213 public void createWithWrongXmlSchema(String testName) throws Exception {
217 public void updateWithEmptyEntityBody(String testName) throws Exception {
221 public void updateWithMalformedXml(String testName) throws Exception {
225 public void updateWithWrongXmlSchema(String testName) throws Exception {
228 // ---------------------------------------------------------------
229 // Utility tests : tests of code used in tests above
230 // ---------------------------------------------------------------
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);
242 // ---------------------------------------------------------------
243 // Utility methods used by tests above
244 // ---------------------------------------------------------------
247 public String getServicePathComponent() {
248 return SERVICE_PATH_COMPONENT;
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());
260 if (logger.isDebugEnabled()) {
261 logger.debug("to be created, objectexit common");
262 logger.debug(objectAsXmlString(objectexit, ObjectexitCommon.class));