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;
53 final Logger logger = LoggerFactory
54 .getLogger(AuthenticationServiceTest.class);
57 * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
60 protected String getServicePathComponent() {
61 // no need to return anything but null since no auth resources are
67 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
71 public void create() {
72 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
73 String identifier = this.createIdentifier();
74 MultipartOutput multipart = createCollectionObjectInstance(
75 collectionObjectClient.getCommonPartName(), identifier);
77 if (!collectionObjectClient.isServerSecure()) {
78 logger.warn("set -Dcspace.server.secure=true to run security tests");
81 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
83 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
85 collectionObjectClient.setProperty(
86 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
88 collectionObjectClient.setupHttpClient();
89 collectionObjectClient.setProxy();
90 } catch (Exception e) {
91 logger.error("create: caught " + e.getMessage());
94 ClientResponse<Response> res = collectionObjectClient.create(multipart);
95 if(logger.isDebugEnabled()){
96 logger.debug("create: status = " + res.getStatus());
98 Assert.assertEquals(res.getStatus(), Response.Status.CREATED
99 .getStatusCode(), "expected "
100 + Response.Status.CREATED.getStatusCode());
102 // Store the ID returned from this create operation for additional tests
104 knownResourceId = extractId(res);
108 * Creates the collection object instance without user.
110 @Test(dependsOnMethods = { "create" })
111 public void createWithoutUser() {
112 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
113 String identifier = this.createIdentifier();
114 MultipartOutput multipart = createCollectionObjectInstance(
115 collectionObjectClient.getCommonPartName(), identifier);
116 if (!collectionObjectClient.isServerSecure()) {
118 .warn("set -Dcspace.server.secure=true to run security tests");
121 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
123 collectionObjectClient
124 .removeProperty(CollectionSpaceClient.USER_PROPERTY);
125 collectionObjectClient.setProperty(
126 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
128 collectionObjectClient.setupHttpClient();
129 collectionObjectClient.setProxy();
130 } catch (Exception e) {
131 logger.error("createWithoutUser: caught " + e.getMessage());
134 ClientResponse<Response> res = collectionObjectClient.create(multipart);
135 if(logger.isDebugEnabled()){
136 logger.debug("createWithoutUser: status = " + res.getStatus());
138 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED
139 .getStatusCode(), "expected "
140 + Response.Status.UNAUTHORIZED.getStatusCode());
144 * Creates the collection object instance without password.
146 @Test(dependsOnMethods = { "createWithoutUser" })
147 public void createWithoutPassword() {
148 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
149 String identifier = this.createIdentifier();
150 MultipartOutput multipart = createCollectionObjectInstance(
151 collectionObjectClient.getCommonPartName(), identifier);
152 if (!collectionObjectClient.isServerSecure()) {
154 .warn("set -Dcspace.server.secure=true to run security tests");
157 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
159 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
161 collectionObjectClient
162 .removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
164 collectionObjectClient.setupHttpClient();
165 collectionObjectClient.setProxy();
166 } catch (Exception e) {
167 logger.error("createWithoutPassword: caught " + e.getMessage());
170 ClientResponse<Response> res = collectionObjectClient.create(multipart);
171 if(logger.isDebugEnabled()){
172 logger.debug("createWithoutPassword: status = " + res.getStatus());
174 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED
175 .getStatusCode(), "expected "
176 + Response.Status.UNAUTHORIZED.getStatusCode());
180 * Creates the collection object instance with incorrect password.
182 @Test(dependsOnMethods = { "createWithoutPassword" })
183 public void createWithIncorrectPassword() {
184 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
185 String identifier = this.createIdentifier();
186 MultipartOutput multipart = createCollectionObjectInstance(
187 collectionObjectClient.getCommonPartName(), identifier);
188 if (!collectionObjectClient.isServerSecure()) {
190 .warn("set -Dcspace.server.secure=true to run security tests");
193 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
195 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
197 collectionObjectClient.setProperty(
198 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
200 collectionObjectClient.setupHttpClient();
201 collectionObjectClient.setProxy();
202 } catch (Exception e) {
203 logger.error("createWithIncorrectPassword: caught "
207 ClientResponse<Response> res = collectionObjectClient.create(multipart);
208 if(logger.isDebugEnabled()){
209 logger.debug("createWithIncorrectPassword: status = " + res.getStatus());
211 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED
212 .getStatusCode(), "expected "
213 + Response.Status.UNAUTHORIZED.getStatusCode());
217 * Creates the collection object instance without user password.
219 @Test(dependsOnMethods = { "createWithoutPassword" })
220 public void createWithoutUserPassword() {
221 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
222 String identifier = this.createIdentifier();
223 MultipartOutput multipart = createCollectionObjectInstance(
224 collectionObjectClient.getCommonPartName(), identifier);
225 if (!collectionObjectClient.isServerSecure()) {
226 logger.warn("set -Dcspace.server.secure=true to run security tests");
229 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
231 collectionObjectClient
232 .removeProperty(CollectionSpaceClient.USER_PROPERTY);
233 collectionObjectClient
234 .removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
236 collectionObjectClient.setupHttpClient();
237 collectionObjectClient.setProxy();
238 } catch (Exception e) {
239 logger.error("createWithoutUserPassword: caught " + e.getMessage());
242 ClientResponse<Response> res = collectionObjectClient.create(multipart);
243 if(logger.isDebugEnabled()){
244 logger.debug("createWithoutUserPassword: status = " + res.getStatus());
246 Assert.assertEquals(res.getStatus(), Response.Status.FORBIDDEN
247 .getStatusCode(), "expected "
248 + Response.Status.FORBIDDEN.getStatusCode());
252 * Creates the collection object instance with incorrect user password.
254 @Test(dependsOnMethods = { "createWithoutPassword" })
255 public void createWithIncorrectUserPassword() {
256 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
257 String identifier = this.createIdentifier();
258 MultipartOutput multipart = createCollectionObjectInstance(
259 collectionObjectClient.getCommonPartName(), identifier);
260 if (!collectionObjectClient.isServerSecure()) {
261 logger.warn("set -Dcspace.server.secure=true to run security tests");
264 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
266 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
268 collectionObjectClient.setProperty(
269 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
271 collectionObjectClient.setupHttpClient();
272 collectionObjectClient.setProxy();
273 } catch (Exception e) {
274 logger.error("createWithIncorrectUserPassword: caught "
278 ClientResponse<Response> res = collectionObjectClient.create(multipart);
279 if(logger.isDebugEnabled()){
280 logger.debug("createWithIncorrectUserPassword: status = " +
283 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED
284 .getStatusCode(), "expected "
285 + Response.Status.UNAUTHORIZED.getStatusCode());
289 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
292 @Test(dependsOnMethods = { "createWithIncorrectUserPassword" })
293 public void delete() {
294 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
295 collectionObjectClient = new CollectionObjectClient();
296 if (!collectionObjectClient.isServerSecure()) {
297 logger.warn("set -Dcspace.server.secure=true to run security tests");
300 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
302 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
304 collectionObjectClient.setProperty(
305 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
307 collectionObjectClient.setupHttpClient();
308 collectionObjectClient.setProxy();
309 } catch (Exception e) {
310 logger.error("deleteCollectionObject: caught " + e.getMessage());
313 if(logger.isDebugEnabled()){
314 logger.debug("Calling deleteCollectionObject:" + knownResourceId);
316 ClientResponse<Response> res = collectionObjectClient
317 .delete(knownResourceId);
318 if(logger.isDebugEnabled()){
319 logger.debug("deleteCollectionObject: status = " + res.getStatus());
321 Assert.assertEquals(res.getStatus(),
322 Response.Status.OK.getStatusCode(), "expected "
323 + Response.Status.OK.getStatusCode());
326 // ---------------------------------------------------------------
327 // Utility methods used by tests above
328 // ---------------------------------------------------------------
330 * Creates the collection object instance.
332 * @param commonPartName the common part name
333 * @param identifier the identifier
335 * @return the multipart output
337 private MultipartOutput createCollectionObjectInstance(
338 String commonPartName, String identifier) {
339 return createCollectionObjectInstance(commonPartName, "objectNumber-"
340 + identifier, "objectName-" + identifier);
344 * Creates the collection object instance.
346 * @param commonPartName the common part name
347 * @param objectNumber the object number
348 * @param objectName the object name
350 * @return the multipart output
352 private MultipartOutput createCollectionObjectInstance(
353 String commonPartName, String objectNumber, String objectName) {
354 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
356 collectionObject.setObjectNumber(objectNumber);
357 collectionObject.setObjectName(objectName);
358 MultipartOutput multipart = new MultipartOutput();
359 OutputPart commonPart = multipart.addPart(collectionObject,
360 MediaType.APPLICATION_XML_TYPE);
361 commonPart.getHeaders().add("label", commonPartName);
363 if(logger.isDebugEnabled()){
364 logger.debug("to be created, collectionobject common ",
365 collectionObject, CollectionobjectsCommon.class);
371 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
374 public void createList() throws Exception {
378 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
381 public void createWithEmptyEntityBody() throws Exception {
385 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
388 public void createWithMalformedXml() throws Exception {
392 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
395 public void createWithWrongXmlSchema() throws Exception {
399 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
402 public void read() throws Exception {
406 * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
409 public void readNonExistent() throws Exception {
413 * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
416 public void readList() throws Exception {
420 * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
423 public void update() throws Exception {
427 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
430 public void updateWithEmptyEntityBody() throws Exception {
434 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
437 public void updateWithMalformedXml() throws Exception {
441 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
444 public void updateWithWrongXmlSchema() throws Exception {
448 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
451 public void updateNonExistent() throws Exception {
455 * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
458 public void deleteNonExistent() throws Exception {