]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
91e5d2276bd09f02ca9801bec94d9eabb88ae1a1
[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         
52         /** The logger. */
53         final Logger logger = LoggerFactory
54                         .getLogger(AuthenticationServiceTest.class);
55
56         /* (non-Javadoc)
57          * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
58          */
59         protected String getServicePathComponent() {
60                 // no need to return anything but null since no auth resources are
61                 // accessed
62                 return null;
63         }
64
65         /* (non-Javadoc)
66          * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
67          */
68         @Test
69         @Override
70         public void create() {
71                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
72                 String identifier = this.createIdentifier();
73                 MultipartOutput multipart = createCollectionObjectInstance(
74                                 collectionObjectClient.getCommonPartName(), identifier);
75
76                 if (!collectionObjectClient.isServerSecure()) {
77                         logger
78                                         .warn("set -Dcspace.server.secure=true to run security tests");
79                         return;
80                 }
81                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
82                                 "true");
83                 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
84                                 "test");
85                 collectionObjectClient.setProperty(
86                                 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
87                 try {
88                         collectionObjectClient.setupHttpClient();
89                         collectionObjectClient.setProxy();
90                 } catch (Exception e) {
91                         logger.error("create: caught " + e.getMessage());
92                         return;
93                 }
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());
99
100                 // Store the ID returned from this create operation for additional tests
101                 // below.
102                 knownResourceId = extractId(res);
103         }
104
105         /**
106          * Creates the without user.
107          */
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()) {
115                         logger
116                                         .warn("set -Dcspace.server.secure=true to run security tests");
117                         return;
118                 }
119                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
120                                 "true");
121                 collectionObjectClient
122                                 .removeProperty(CollectionSpaceClient.USER_PROPERTY);
123                 collectionObjectClient.setProperty(
124                                 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
125                 try {
126                         collectionObjectClient.setupHttpClient();
127                         collectionObjectClient.setProxy();
128                 } catch (Exception e) {
129                         logger.error("createWithoutUser: caught " + e.getMessage());
130                         return;
131                 }
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());
137         }
138
139         /**
140          * Creates the without password.
141          */
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()) {
149                         logger
150                                         .warn("set -Dcspace.server.secure=true to run security tests");
151                         return;
152                 }
153                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
154                                 "true");
155                 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
156                                 "test");
157                 collectionObjectClient
158                                 .removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
159                 try {
160                         collectionObjectClient.setupHttpClient();
161                         collectionObjectClient.setProxy();
162                 } catch (Exception e) {
163                         logger.error("createWithoutPassword: caught " + e.getMessage());
164                         return;
165                 }
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());
171         }
172
173         /**
174          * Creates the with incorrect password.
175          */
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()) {
183                         logger
184                                         .warn("set -Dcspace.server.secure=true to run security tests");
185                         return;
186                 }
187                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
188                                 "true");
189                 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
190                                 "test");
191                 collectionObjectClient.setProperty(
192                                 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
193                 try {
194                         collectionObjectClient.setupHttpClient();
195                         collectionObjectClient.setProxy();
196                 } catch (Exception e) {
197                         logger.error("createWithIncorrectPassword: caught "
198                                         + e.getMessage());
199                         return;
200                 }
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());
206         }
207
208         /**
209          * Creates the without user password.
210          */
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");
219                         return;
220                 }
221                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
222                                 "true");
223                 collectionObjectClient
224                                 .removeProperty(CollectionSpaceClient.USER_PROPERTY);
225                 collectionObjectClient
226                                 .removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
227                 try {
228                         collectionObjectClient.setupHttpClient();
229                         collectionObjectClient.setProxy();
230                 } catch (Exception e) {
231                         logger.error("createWithoutUserPassword: caught " + e.getMessage());
232                         return;
233                 }
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());
239         }
240
241         /**
242          * Creates the with incorrect user password.
243          */
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");
252                         return;
253                 }
254                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
255                                 "true");
256                 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
257                                 "foo");
258                 collectionObjectClient.setProperty(
259                                 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
260                 try {
261                         collectionObjectClient.setupHttpClient();
262                         collectionObjectClient.setProxy();
263                 } catch (Exception e) {
264                         logger.error("createWithIncorrectUserPassword: caught "
265                                         + e.getMessage());
266                         return;
267                 }
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());
273         }
274
275         /* (non-Javadoc)
276          * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
277          */
278         @Override
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");
285                         return;
286                 }
287                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
288                                 "true");
289                 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
290                                 "test");
291                 collectionObjectClient.setProperty(
292                                 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
293                 try {
294                         collectionObjectClient.setupHttpClient();
295                         collectionObjectClient.setProxy();
296                 } catch (Exception e) {
297                         logger.error("deleteCollectionObject: caught " + e.getMessage());
298                         return;
299                 }
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());
307         }
308
309         // ---------------------------------------------------------------
310         // Utility methods used by tests above
311         // ---------------------------------------------------------------
312         /**
313          * Creates the collection object instance.
314          * 
315          * @param commonPartName the common part name
316          * @param identifier the identifier
317          * 
318          * @return the multipart output
319          */
320         private MultipartOutput createCollectionObjectInstance(
321                         String commonPartName, String identifier) {
322                 return createCollectionObjectInstance(commonPartName, "objectNumber-"
323                                 + identifier, "objectName-" + identifier);
324         }
325
326         /**
327          * Creates the collection object instance.
328          * 
329          * @param commonPartName the common part name
330          * @param objectNumber the object number
331          * @param objectName the object name
332          * 
333          * @return the multipart output
334          */
335         private MultipartOutput createCollectionObjectInstance(
336                         String commonPartName, String objectNumber, String objectName) {
337                 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
338
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);
345
346                 verbose("to be created, collectionobject common ", collectionObject,
347                                 CollectionobjectsCommon.class);
348                 return multipart;
349         }
350
351         /* (non-Javadoc)
352          * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
353          */
354         @Override
355         public void createList() {
356         }
357
358         /* (non-Javadoc)
359          * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
360          */
361         @Override
362         public void createWithEmptyEntityBody() {
363         }
364
365         /* (non-Javadoc)
366          * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
367          */
368         @Override
369         public void createWithMalformedXml() {
370         }
371
372         /* (non-Javadoc)
373          * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
374          */
375         @Override
376         public void createWithWrongXmlSchema() {
377         }
378
379         /* (non-Javadoc)
380          * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
381          */
382         @Override
383         public void read() {
384         }
385
386         /* (non-Javadoc)
387          * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
388          */
389         @Override
390         public void readNonExistent() {
391         }
392
393         /* (non-Javadoc)
394          * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
395          */
396         @Override
397         public void readList() {
398         }
399
400         /* (non-Javadoc)
401          * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
402          */
403         @Override
404         public void update() {
405         }
406
407         /* (non-Javadoc)
408          * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
409          */
410         @Override
411         public void updateWithEmptyEntityBody() {
412         }
413
414         /* (non-Javadoc)
415          * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
416          */
417         @Override
418         public void updateWithMalformedXml() {
419         }
420
421         /* (non-Javadoc)
422          * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
423          */
424         @Override
425         public void updateWithWrongXmlSchema() {
426         }
427
428         /* (non-Javadoc)
429          * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
430          */
431         @Override
432         public void updateNonExistent() {
433         }
434
435         /* (non-Javadoc)
436          * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
437          */
438         @Override
439         public void deleteNonExistent() {
440         }
441 }