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()
69 @Test(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
71 public void create(String testName) {
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(dataProvider="testName", dataProviderClass=AbstractServiceTest.class,
293 dependsOnMethods = { "createWithIncorrectUserPassword" })
294 public void delete(String testName) {
295 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
296 collectionObjectClient = new CollectionObjectClient();
297 if (!collectionObjectClient.isServerSecure()) {
298 logger.warn("set -Dcspace.server.secure=true to run security tests");
301 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
303 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
305 collectionObjectClient.setProperty(
306 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
308 collectionObjectClient.setupHttpClient();
309 collectionObjectClient.setProxy();
310 } catch (Exception e) {
311 logger.error("deleteCollectionObject: caught " + e.getMessage());
314 if(logger.isDebugEnabled()){
315 logger.debug("Calling deleteCollectionObject:" + knownResourceId);
317 ClientResponse<Response> res = collectionObjectClient
318 .delete(knownResourceId);
319 if(logger.isDebugEnabled()){
320 logger.debug("deleteCollectionObject: status = " + res.getStatus());
322 Assert.assertEquals(res.getStatus(),
323 Response.Status.OK.getStatusCode(), "expected "
324 + Response.Status.OK.getStatusCode());
327 // ---------------------------------------------------------------
328 // Utility methods used by tests above
329 // ---------------------------------------------------------------
331 * Creates the collection object instance.
333 * @param commonPartName the common part name
334 * @param identifier the identifier
336 * @return the multipart output
338 private MultipartOutput createCollectionObjectInstance(
339 String commonPartName, String identifier) {
340 return createCollectionObjectInstance(commonPartName, "objectNumber-"
341 + identifier, "objectName-" + identifier);
345 * Creates the collection object instance.
347 * @param commonPartName the common part name
348 * @param objectNumber the object number
349 * @param objectName the object name
351 * @return the multipart output
353 private MultipartOutput createCollectionObjectInstance(
354 String commonPartName, String objectNumber, String objectName) {
355 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
357 collectionObject.setObjectNumber(objectNumber);
358 collectionObject.setObjectName(objectName);
359 MultipartOutput multipart = new MultipartOutput();
360 OutputPart commonPart = multipart.addPart(collectionObject,
361 MediaType.APPLICATION_XML_TYPE);
362 commonPart.getHeaders().add("label", commonPartName);
364 if(logger.isDebugEnabled()){
365 logger.debug("to be created, collectionobject common ",
366 collectionObject, CollectionobjectsCommon.class);
372 * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
375 public void createList(String testName) throws Exception {
379 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
382 public void createWithEmptyEntityBody(String testName) throws Exception {
386 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
389 public void createWithMalformedXml(String testName) throws Exception {
393 * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
396 public void createWithWrongXmlSchema(String testName) throws Exception {
400 * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
403 public void read(String testName) throws Exception {
407 * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
410 public void readNonExistent(String testName) throws Exception {
414 * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
417 public void readList(String testName) throws Exception {
421 * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
424 public void update(String testName) throws Exception {
428 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
431 public void updateWithEmptyEntityBody(String testName) throws Exception {
435 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
438 public void updateWithMalformedXml(String testName) throws Exception {
442 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
445 public void updateWithWrongXmlSchema(String testName) throws Exception {
449 * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
452 public void updateNonExistent(String testName) throws Exception {
456 * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
459 public void deleteNonExistent(String testName) throws Exception {