]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
2ca1edb11be47ba5eb96bba32b848495c9492092
[tmp/jakarta-migration.git] /
1 /**
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:
5  *
6  * http://www.collectionspace.org
7  * http://wiki.collectionspace.org
8  *
9  * Copyright (c)) 2009 Regents of the University of California
10  *
11  * Licensed under the Educational Community License (ECL), Version 2.0.
12  * You may not use this file except in compliance with this License.
13  *
14  * You may obtain a copy of the ECL 2.0 License at
15  * https://source.collectionspace.org/collection-space/LICENSE.txt
16  *
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.
22  */
23 package org.collectionspace.services.authentication.client;
24
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;
30
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;
39
40 /**
41  * AuthenticationServiceTest uses CollectionObject service to test
42  * authentication
43  * 
44  * $LastChangedRevision: 434 $ $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue,
45  * 28 Jul 2009) $
46  */
47 public class AuthenticationServiceTest extends AbstractServiceTest {
48
49     /** The known resource id. */
50     private String knownResourceId = null;
51     /** The logger. */
52     final Logger logger = LoggerFactory.getLogger(AuthenticationServiceTest.class);
53
54     /* (non-Javadoc)
55      * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
56      */
57     @Override
58     protected String getServicePathComponent() {
59         // no need to return anything but null since no auth resources are
60         // accessed
61         return null;
62     }
63
64     /* (non-Javadoc)
65      * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
66      */
67     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTest.class)
68     @Override
69     public void create(String testName) {
70         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
71         String identifier = this.createIdentifier();
72         MultipartOutput multipart = createCollectionObjectInstance(
73                 collectionObjectClient.getCommonPartName(), identifier);
74
75         if (!collectionObjectClient.isServerSecure()) {
76             logger.warn("set -Dcspace.server.secure=true to run security tests");
77             return;
78         }
79         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
80                 "true");
81         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
82                 "test");
83         collectionObjectClient.setProperty(
84                 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
85         try {
86             collectionObjectClient.setupHttpClient();
87             collectionObjectClient.setProxy();
88         } catch (Exception e) {
89             logger.error("create: caught " + e.getMessage());
90             return;
91         }
92         ClientResponse<Response> res = collectionObjectClient.create(multipart);
93         if (logger.isDebugEnabled()) {
94             logger.debug("create: status = " + res.getStatus());
95         }
96         Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode(), "expected " + Response.Status.CREATED.getStatusCode());
97
98         // Store the ID returned from this create operation for additional tests
99         // below.
100         knownResourceId = extractId(res);
101     }
102
103     /**
104      * Creates the collection object instance without user.
105      */
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");
114             return;
115         }
116         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
117                 "true");
118         collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY);
119         collectionObjectClient.setProperty(
120                 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
121         try {
122             collectionObjectClient.setupHttpClient();
123             collectionObjectClient.setProxy();
124         } catch (Exception e) {
125             logger.error("createWithoutUser: caught " + e.getMessage());
126             return;
127         }
128         ClientResponse<Response> res = collectionObjectClient.create(multipart);
129         if (logger.isDebugEnabled()) {
130             logger.debug("createWithoutUser: status = " + res.getStatus());
131         }
132         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
133     }
134
135     /**
136      * Creates the collection object instance without password.
137      */
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");
146             return;
147         }
148         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
149                 "true");
150         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
151                 "test");
152         collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
153         try {
154             collectionObjectClient.setupHttpClient();
155             collectionObjectClient.setProxy();
156         } catch (Exception e) {
157             logger.error("createWithoutPassword: caught " + e.getMessage());
158             return;
159         }
160         ClientResponse<Response> res = collectionObjectClient.create(multipart);
161         if (logger.isDebugEnabled()) {
162             logger.debug("createWithoutPassword: status = " + res.getStatus());
163         }
164         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
165     }
166
167     /**
168      * Creates the collection object instance with incorrect password.
169      */
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");
178             return;
179         }
180         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
181                 "true");
182         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
183                 "test");
184         collectionObjectClient.setProperty(
185                 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
186         try {
187             collectionObjectClient.setupHttpClient();
188             collectionObjectClient.setProxy();
189         } catch (Exception e) {
190             logger.error("createWithIncorrectPassword: caught " + e.getMessage());
191             return;
192         }
193         ClientResponse<Response> res = collectionObjectClient.create(multipart);
194         if (logger.isDebugEnabled()) {
195             logger.debug("createWithIncorrectPassword: status = " + res.getStatus());
196         }
197         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
198     }
199
200     /**
201      * Creates the collection object instance without user password.
202      */
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");
211             return;
212         }
213         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
214                 "true");
215         collectionObjectClient.removeProperty(CollectionSpaceClient.USER_PROPERTY);
216         collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
217         try {
218             collectionObjectClient.setupHttpClient();
219             collectionObjectClient.setProxy();
220         } catch (Exception e) {
221             logger.error("createWithoutUserPassword: caught " + e.getMessage());
222             return;
223         }
224         ClientResponse<Response> res = collectionObjectClient.create(multipart);
225         if (logger.isDebugEnabled()) {
226             logger.debug("createWithoutUserPassword: status = " + res.getStatus());
227         }
228         Assert.assertEquals(res.getStatus(), Response.Status.FORBIDDEN.getStatusCode(), "expected " + Response.Status.FORBIDDEN.getStatusCode());
229     }
230
231     /**
232      * Creates the collection object instance with incorrect user password.
233      */
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");
242             return;
243         }
244         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
245                 "true");
246         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
247                 "foo");
248         collectionObjectClient.setProperty(
249                 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
250         try {
251             collectionObjectClient.setupHttpClient();
252             collectionObjectClient.setProxy();
253         } catch (Exception e) {
254             logger.error("createWithIncorrectUserPassword: caught " + e.getMessage());
255             return;
256         }
257         ClientResponse<Response> res = collectionObjectClient.create(multipart);
258         if (logger.isDebugEnabled()) {
259             logger.debug("createWithIncorrectUserPassword: status = " +
260                     res.getStatus());
261         }
262         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
263     }
264
265     /* (non-Javadoc)
266      * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
267      */
268     @Override
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");
276             return;
277         }
278         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
279                 "true");
280         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
281                 "test");
282         collectionObjectClient.setProperty(
283                 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
284         try {
285             collectionObjectClient.setupHttpClient();
286             collectionObjectClient.setProxy();
287         } catch (Exception e) {
288             logger.error("deleteCollectionObject: caught " + e.getMessage());
289             return;
290         }
291         if (logger.isDebugEnabled()) {
292             logger.debug("Calling deleteCollectionObject:" + knownResourceId);
293         }
294         ClientResponse<Response> res = collectionObjectClient.delete(knownResourceId);
295         if (logger.isDebugEnabled()) {
296             logger.debug("deleteCollectionObject: status = " + res.getStatus());
297         }
298         Assert.assertEquals(res.getStatus(),
299                 Response.Status.OK.getStatusCode(), "expected " + Response.Status.OK.getStatusCode());
300     }
301
302     // ---------------------------------------------------------------
303     // Utility methods used by tests above
304     // ---------------------------------------------------------------
305     /**
306      * Creates the collection object instance.
307      *
308      * @param commonPartName the common part name
309      * @param identifier the identifier
310      *
311      * @return the multipart output
312      */
313     private MultipartOutput createCollectionObjectInstance(
314             String commonPartName, String identifier) {
315         return createCollectionObjectInstance(commonPartName, "objectNumber-" + identifier, "objectName-" + identifier);
316     }
317
318     /**
319      * Creates the collection object instance.
320      *
321      * @param commonPartName the common part name
322      * @param objectNumber the object number
323      * @param objectName the object name
324      *
325      * @return the multipart output
326      */
327     private MultipartOutput createCollectionObjectInstance(
328             String commonPartName, String objectNumber, String objectName) {
329         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
330
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);
337
338         if (logger.isDebugEnabled()) {
339             logger.debug("to be created, collectionobject common ",
340                     collectionObject, CollectionobjectsCommon.class);
341         }
342         return multipart;
343     }
344
345     /* (non-Javadoc)
346      * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
347      */
348     @Override
349     public void createList(String testName) throws Exception {
350     }
351
352     /* (non-Javadoc)
353      * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
354      */
355     @Override
356     public void createWithEmptyEntityBody(String testName) throws Exception {
357     }
358
359     /* (non-Javadoc)
360      * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
361      */
362     @Override
363     public void createWithMalformedXml(String testName) throws Exception {
364     }
365
366     /* (non-Javadoc)
367      * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
368      */
369     @Override
370     public void createWithWrongXmlSchema(String testName) throws Exception {
371     }
372
373     /* (non-Javadoc)
374      * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
375      */
376     @Override
377     public void read(String testName) throws Exception {
378     }
379
380     /* (non-Javadoc)
381      * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
382      */
383     @Override
384     public void readNonExistent(String testName) throws Exception {
385     }
386
387     /* (non-Javadoc)
388      * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
389      */
390     @Override
391     public void readList(String testName) throws Exception {
392     }
393
394     /* (non-Javadoc)
395      * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
396      */
397     @Override
398     public void update(String testName) throws Exception {
399     }
400
401     /* (non-Javadoc)
402      * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
403      */
404     @Override
405     public void updateWithEmptyEntityBody(String testName) throws Exception {
406     }
407
408     /* (non-Javadoc)
409      * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
410      */
411     @Override
412     public void updateWithMalformedXml(String testName) throws Exception {
413     }
414
415     /* (non-Javadoc)
416      * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
417      */
418     @Override
419     public void updateWithWrongXmlSchema(String testName) throws Exception {
420     }
421
422     /* (non-Javadoc)
423      * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
424      */
425     @Override
426     public void updateNonExistent(String testName) throws Exception {
427     }
428
429     /* (non-Javadoc)
430      * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
431      */
432     @Override
433     public void deleteNonExistent(String testName) throws Exception {
434     }
435 }