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()
59 protected String getServicePathComponent() {
60 // no need to return anything but null since no auth resources are
66 * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
70 public void create() {
71 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
72 String identifier = this.createIdentifier();
73 MultipartOutput multipart = createCollectionObjectInstance(
74 collectionObjectClient.getCommonPartName(), identifier);
76 if (!collectionObjectClient.isServerSecure()) {
78 .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 verbose("create: status = " + res.getStatus());
96 Assert.assertEquals(res.getStatus(), Response.Status.CREATED
97 .getStatusCode(), "expected "
98 + Response.Status.CREATED.getStatusCode());
100 // Store the ID returned from this create operation for additional tests
102 knownResourceId = extractId(res);
106 * Creates the without user.
108 @Test(dependsOnMethods = { "create" })
109 public void createWithoutUser() {
110 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
111 String identifier = this.createIdentifier();
112 MultipartOutput multipart = createCollectionObjectInstance(
113 collectionObjectClient.getCommonPartName(), identifier);
114 if (!collectionObjectClient.isServerSecure()) {
116 .warn("set -Dcspace.server.secure=true to run security tests");
119 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
121 collectionObjectClient
122 .removeProperty(CollectionSpaceClient.USER_PROPERTY);
123 collectionObjectClient.setProperty(
124 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
126 collectionObjectClient.setupHttpClient();
127 collectionObjectClient.setProxy();
128 } catch (Exception e) {
129 logger.error("createWithoutUser: caught " + e.getMessage());
132 ClientResponse<Response> res = collectionObjectClient.create(multipart);
133 verbose("createWithoutUser: status = " + res.getStatus());
134 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED
135 .getStatusCode(), "expected "
136 + Response.Status.UNAUTHORIZED.getStatusCode());
140 * Creates the without password.
142 @Test(dependsOnMethods = { "createWithoutUser" })
143 public void createWithoutPassword() {
144 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
145 String identifier = this.createIdentifier();
146 MultipartOutput multipart = createCollectionObjectInstance(
147 collectionObjectClient.getCommonPartName(), identifier);
148 if (!collectionObjectClient.isServerSecure()) {
150 .warn("set -Dcspace.server.secure=true to run security tests");
153 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
155 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
157 collectionObjectClient
158 .removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
160 collectionObjectClient.setupHttpClient();
161 collectionObjectClient.setProxy();
162 } catch (Exception e) {
163 logger.error("createWithoutPassword: caught " + e.getMessage());
166 ClientResponse<Response> res = collectionObjectClient.create(multipart);
167 verbose("createWithoutPassword: status = " + res.getStatus());
168 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED
169 .getStatusCode(), "expected "
170 + Response.Status.UNAUTHORIZED.getStatusCode());
174 * Creates the with incorrect password.
176 @Test(dependsOnMethods = { "createWithoutPassword" })
177 public void createWithIncorrectPassword() {
178 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
179 String identifier = this.createIdentifier();
180 MultipartOutput multipart = createCollectionObjectInstance(
181 collectionObjectClient.getCommonPartName(), identifier);
182 if (!collectionObjectClient.isServerSecure()) {
184 .warn("set -Dcspace.server.secure=true to run security tests");
187 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
189 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
191 collectionObjectClient.setProperty(
192 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
194 collectionObjectClient.setupHttpClient();
195 collectionObjectClient.setProxy();
196 } catch (Exception e) {
197 logger.error("createWithIncorrectPassword: caught "
201 ClientResponse<Response> res = collectionObjectClient.create(multipart);
202 verbose("createWithIncorrectPassword: status = " + res.getStatus());
203 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED
204 .getStatusCode(), "expected "
205 + Response.Status.UNAUTHORIZED.getStatusCode());
209 * Creates the without user password.
211 @Test(dependsOnMethods = { "createWithoutPassword" })
212 public void createWithoutUserPassword() {
213 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
214 String identifier = this.createIdentifier();
215 MultipartOutput multipart = createCollectionObjectInstance(
216 collectionObjectClient.getCommonPartName(), identifier);
217 if (!collectionObjectClient.isServerSecure()) {
218 logger.warn("set -Dcspace.server.secure=true to run security tests");
221 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
223 collectionObjectClient
224 .removeProperty(CollectionSpaceClient.USER_PROPERTY);
225 collectionObjectClient
226 .removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
228 collectionObjectClient.setupHttpClient();
229 collectionObjectClient.setProxy();
230 } catch (Exception e) {
231 logger.error("createWithoutUserPassword: caught " + e.getMessage());
234 ClientResponse<Response> res = collectionObjectClient.create(multipart);
235 verbose("createWithoutUserPassword: status = " + res.getStatus());
236 Assert.assertEquals(res.getStatus(), Response.Status.FORBIDDEN
237 .getStatusCode(), "expected "
238 + Response.Status.FORBIDDEN.getStatusCode());
242 * Creates the with incorrect user password.
244 @Test(dependsOnMethods = { "createWithoutPassword" })
245 public void createWithIncorrectUserPassword() {
246 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
247 String identifier = this.createIdentifier();
248 MultipartOutput multipart = createCollectionObjectInstance(
249 collectionObjectClient.getCommonPartName(), identifier);
250 if (!collectionObjectClient.isServerSecure()) {
251 logger.warn("set -Dcspace.server.secure=true to run security tests");
254 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
256 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
258 collectionObjectClient.setProperty(
259 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
261 collectionObjectClient.setupHttpClient();
262 collectionObjectClient.setProxy();
263 } catch (Exception e) {
264 logger.error("createWithIncorrectUserPassword: caught "
268 ClientResponse<Response> res = collectionObjectClient.create(multipart);
269 verbose("createWithIncorrectUserPassword: status = " + res.getStatus());
270 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED
271 .getStatusCode(), "expected "
272 + Response.Status.UNAUTHORIZED.getStatusCode());
276 * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
279 @Test(dependsOnMethods = { "createWithIncorrectUserPassword" })
280 public void delete() {
281 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
282 collectionObjectClient = new CollectionObjectClient();
283 if (!collectionObjectClient.isServerSecure()) {
284 logger.warn("set -Dcspace.server.secure=true to run security tests");
287 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
289 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
291 collectionObjectClient.setProperty(
292 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
294 collectionObjectClient.setupHttpClient();
295 collectionObjectClient.setProxy();
296 } catch (Exception e) {
297 logger.error("deleteCollectionObject: caught " + e.getMessage());
300 verbose("Calling deleteCollectionObject:" + knownResourceId);
301 ClientResponse<Response> res = collectionObjectClient
302 .delete(knownResourceId);
303 verbose("deleteCollectionObject: status = " + res.getStatus());
304 Assert.assertEquals(res.getStatus(),
305 Response.Status.OK.getStatusCode(), "expected "
306 + Response.Status.OK.getStatusCode());
309 // ---------------------------------------------------------------
310 // Utility methods used by tests above
311 // ---------------------------------------------------------------
313 * Creates the collection object instance.
315 * @param commonPartName the common part name
316 * @param identifier the identifier
318 * @return the multipart output
320 private MultipartOutput createCollectionObjectInstance(
321 String commonPartName, String identifier) {
322 return createCollectionObjectInstance(commonPartName, "objectNumber-"
323 + identifier, "objectName-" + identifier);
327 * Creates the collection object instance.
329 * @param commonPartName the common part name
330 * @param objectNumber the object number
331 * @param objectName the object name
333 * @return the multipart output
335 private MultipartOutput createCollectionObjectInstance(
336 String commonPartName, String objectNumber, String objectName) {
337 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
339 collectionObject.setObjectNumber(objectNumber);
340 collectionObject.setObjectName(objectName);
341 MultipartOutput multipart = new MultipartOutput();
342 OutputPart commonPart = multipart.addPart(collectionObject,
343 MediaType.APPLICATION_XML_TYPE);
344 commonPart.getHeaders().add("label", commonPartName);
346 verbose("to be created, collectionobject common ", collectionObject,
347 CollectionobjectsCommon.class);
352 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
355 public void createList() {
359 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
362 public void createWithEmptyEntityBody() {
366 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
369 public void createWithMalformedXml() {
373 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
376 public void createWithWrongXmlSchema() {
380 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
387 * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
390 public void readNonExistent() {
394 * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
397 public void readList() {
401 * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
404 public void update() {
408 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
411 public void updateWithEmptyEntityBody() {
415 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
418 public void updateWithMalformedXml() {
422 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
425 public void updateWithWrongXmlSchema() {
429 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
432 public void updateNonExistent() {
436 * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
439 public void deleteNonExistent() {