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 (c)) 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.authentication.client;
25 import javax.ws.rs.core.MediaType;
26 import javax.ws.rs.core.Response;
27 import org.jboss.resteasy.client.ClientResponse;
28 import org.testng.Assert;
29 import org.testng.annotations.Test;
31 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
32 import org.collectionspace.services.client.CollectionObjectClient;
33 import org.collectionspace.services.client.CollectionSpaceClient;
34 import org.collectionspace.services.client.test.AbstractServiceTest;
35 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
36 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
41 * AuthenticationServiceTest uses CollectionObject service to test
44 * $LastChangedRevision: 434 $ $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue,
47 public class AuthenticationServiceTest extends AbstractServiceTest {
49 /** The known resource id. */
50 private String knownResourceId = null;
52 final Logger logger = LoggerFactory.getLogger(AuthenticationServiceTest.class);
55 * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
58 protected String getServicePathComponent() {
59 // no need to return anything but null since no auth resources are
65 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
67 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class)
69 public void create(String testName) {
70 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
71 String identifier = this.createIdentifier();
72 MultipartOutput multipart = createCollectionObjectInstance(
73 collectionObjectClient.getCommonPartName(), identifier);
75 if (!collectionObjectClient.isServerSecure()) {
76 logger.warn("set -Dcspace.server.secure=true to run security tests");
79 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
81 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
83 collectionObjectClient.setProperty(
84 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
86 collectionObjectClient.setupHttpClient();
87 collectionObjectClient.setProxy();
88 } catch (Exception e) {
89 logger.error("create: caught " + e.getMessage());
92 ClientResponse<Response> res = collectionObjectClient.create(multipart);
93 if (logger.isDebugEnabled()) {
94 logger.debug("create: status = " + res.getStatus());
96 Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode(), "expected " + Response.Status.CREATED.getStatusCode());
98 // Store the ID returned from this create operation for additional tests
100 knownResourceId = extractId(res);
104 * Creates the collection object instance without user.
106 @Test(dependsOnMethods = {"create"})
107 public void createWithoutUser() {
108 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
109 String identifier = this.createIdentifier();
110 MultipartOutput multipart = createCollectionObjectInstance(
111 collectionObjectClient.getCommonPartName(), identifier);
112 if (!collectionObjectClient.isServerSecure()) {
113 logger.warn("set -Dcspace.server.secure=true to run security tests");
116 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
118 collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY);
119 collectionObjectClient.setProperty(
120 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
122 collectionObjectClient.setupHttpClient();
123 collectionObjectClient.setProxy();
124 } catch (Exception e) {
125 logger.error("createWithoutUser: caught " + e.getMessage());
128 ClientResponse<Response> res = collectionObjectClient.create(multipart);
129 if (logger.isDebugEnabled()) {
130 logger.debug("createWithoutUser: status = " + res.getStatus());
132 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
136 * Creates the collection object instance without password.
138 @Test(dependsOnMethods = {"createWithoutUser"})
139 public void createWithoutPassword() {
140 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
141 String identifier = this.createIdentifier();
142 MultipartOutput multipart = createCollectionObjectInstance(
143 collectionObjectClient.getCommonPartName(), identifier);
144 if (!collectionObjectClient.isServerSecure()) {
145 logger.warn("set -Dcspace.server.secure=true to run security tests");
148 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
150 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
152 collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
154 collectionObjectClient.setupHttpClient();
155 collectionObjectClient.setProxy();
156 } catch (Exception e) {
157 logger.error("createWithoutPassword: caught " + e.getMessage());
160 ClientResponse<Response> res = collectionObjectClient.create(multipart);
161 if (logger.isDebugEnabled()) {
162 logger.debug("createWithoutPassword: status = " + res.getStatus());
164 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
168 * Creates the collection object instance with incorrect password.
170 @Test(dependsOnMethods = {"createWithoutPassword"})
171 public void createWithIncorrectPassword() {
172 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
173 String identifier = this.createIdentifier();
174 MultipartOutput multipart = createCollectionObjectInstance(
175 collectionObjectClient.getCommonPartName(), identifier);
176 if (!collectionObjectClient.isServerSecure()) {
177 logger.warn("set -Dcspace.server.secure=true to run security tests");
180 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
182 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
184 collectionObjectClient.setProperty(
185 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
187 collectionObjectClient.setupHttpClient();
188 collectionObjectClient.setProxy();
189 } catch (Exception e) {
190 logger.error("createWithIncorrectPassword: caught " + e.getMessage());
193 ClientResponse<Response> res = collectionObjectClient.create(multipart);
194 if (logger.isDebugEnabled()) {
195 logger.debug("createWithIncorrectPassword: status = " + res.getStatus());
197 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
201 * Creates the collection object instance without user password.
203 @Test(dependsOnMethods = {"createWithoutPassword"})
204 public void createWithoutUserPassword() {
205 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
206 String identifier = this.createIdentifier();
207 MultipartOutput multipart = createCollectionObjectInstance(
208 collectionObjectClient.getCommonPartName(), identifier);
209 if (!collectionObjectClient.isServerSecure()) {
210 logger.warn("set -Dcspace.server.secure=true to run security tests");
213 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
215 collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY);
216 collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
218 collectionObjectClient.setupHttpClient();
219 collectionObjectClient.setProxy();
220 } catch (Exception e) {
221 logger.error("createWithoutUserPassword: caught " + e.getMessage());
224 ClientResponse<Response> res = collectionObjectClient.create(multipart);
225 if (logger.isDebugEnabled()) {
226 logger.debug("createWithoutUserPassword: status = " + res.getStatus());
228 Assert.assertEquals(res.getStatus(), Response.Status.FORBIDDEN.getStatusCode(), "expected " + Response.Status.FORBIDDEN.getStatusCode());
232 * Creates the collection object instance with incorrect user password.
234 @Test(dependsOnMethods = {"createWithoutPassword"})
235 public void createWithIncorrectUserPassword() {
236 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
237 String identifier = this.createIdentifier();
238 MultipartOutput multipart = createCollectionObjectInstance(
239 collectionObjectClient.getCommonPartName(), identifier);
240 if (!collectionObjectClient.isServerSecure()) {
241 logger.warn("set -Dcspace.server.secure=true to run security tests");
244 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
246 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
248 collectionObjectClient.setProperty(
249 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
251 collectionObjectClient.setupHttpClient();
252 collectionObjectClient.setProxy();
253 } catch (Exception e) {
254 logger.error("createWithIncorrectUserPassword: caught " + e.getMessage());
257 ClientResponse<Response> res = collectionObjectClient.create(multipart);
258 if (logger.isDebugEnabled()) {
259 logger.debug("createWithIncorrectUserPassword: status = " +
262 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
266 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
269 @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class,
270 dependsOnMethods = {"createWithIncorrectUserPassword"})
271 public void delete(String testName) {
272 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
273 collectionObjectClient = new CollectionObjectClient();
274 if (!collectionObjectClient.isServerSecure()) {
275 logger.warn("set -Dcspace.server.secure=true to run security tests");
278 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
280 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
282 collectionObjectClient.setProperty(
283 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
285 collectionObjectClient.setupHttpClient();
286 collectionObjectClient.setProxy();
287 } catch (Exception e) {
288 logger.error("deleteCollectionObject: caught " + e.getMessage());
291 if (logger.isDebugEnabled()) {
292 logger.debug("Calling deleteCollectionObject:" + knownResourceId);
294 ClientResponse<Response> res = collectionObjectClient.delete(knownResourceId);
295 if (logger.isDebugEnabled()) {
296 logger.debug("deleteCollectionObject: status = " + res.getStatus());
298 Assert.assertEquals(res.getStatus(),
299 Response.Status.OK.getStatusCode(), "expected " + Response.Status.OK.getStatusCode());
302 // ---------------------------------------------------------------
303 // Utility methods used by tests above
304 // ---------------------------------------------------------------
306 * Creates the collection object instance.
308 * @param commonPartName the common part name
309 * @param identifier the identifier
311 * @return the multipart output
313 private MultipartOutput createCollectionObjectInstance(
314 String commonPartName, String identifier) {
315 return createCollectionObjectInstance(commonPartName, "objectNumber-" + identifier, "objectName-" + identifier);
319 * Creates the collection object instance.
321 * @param commonPartName the common part name
322 * @param objectNumber the object number
323 * @param objectName the object name
325 * @return the multipart output
327 private MultipartOutput createCollectionObjectInstance(
328 String commonPartName, String objectNumber, String objectName) {
329 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
331 collectionObject.setObjectNumber(objectNumber);
332 collectionObject.setObjectName(objectName);
333 MultipartOutput multipart = new MultipartOutput();
334 OutputPart commonPart = multipart.addPart(collectionObject,
335 MediaType.APPLICATION_XML_TYPE);
336 commonPart.getHeaders().add("label", commonPartName);
338 if (logger.isDebugEnabled()) {
339 logger.debug("to be created, collectionobject common ",
340 collectionObject, CollectionobjectsCommon.class);
346 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
349 public void createList(String testName) throws Exception {
353 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
356 public void createWithEmptyEntityBody(String testName) throws Exception {
360 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
363 public void createWithMalformedXml(String testName) throws Exception {
367 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
370 public void createWithWrongXmlSchema(String testName) throws Exception {
374 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
377 public void read(String testName) throws Exception {
381 * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
384 public void readNonExistent(String testName) throws Exception {
388 * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
391 public void readList(String testName) throws Exception {
395 * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
398 public void update(String testName) throws Exception {
402 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
405 public void updateWithEmptyEntityBody(String testName) throws Exception {
409 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
412 public void updateWithMalformedXml(String testName) throws Exception {
416 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
419 public void updateWithWrongXmlSchema(String testName) throws Exception {
423 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
426 public void updateNonExistent(String testName) throws Exception {
430 * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
433 public void deleteNonExistent(String testName) throws Exception {