]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
72ebe285d0b2d51d688ed218455b55b75a4958d1
[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     @Override
60         protected String getServicePathComponent() {
61                 // no need to return anything but null since no auth resources are
62                 // accessed
63                 return null;
64         }
65
66         /* (non-Javadoc)
67          * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
68          */
69         @Test
70         @Override
71         public void create() {
72                 CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
73                 String identifier = this.createIdentifier();
74                 MultipartOutput multipart = createCollectionObjectInstance(
75                                 collectionObjectClient.getCommonPartName(), identifier);
76
77                 if (!collectionObjectClient.isServerSecure()) {
78                         logger.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                 if(logger.isDebugEnabled()){
96             logger.debug("create: status = " + res.getStatus());
97         }
98                 Assert.assertEquals(res.getStatus(), Response.Status.CREATED
99                                 .getStatusCode(), "expected "
100                                 + Response.Status.CREATED.getStatusCode());
101
102                 // Store the ID returned from this create operation for additional tests
103                 // below.
104                 knownResourceId = extractId(res);
105         }
106
107         /**
108          * Creates the collection object instance without user.
109          */
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()) {
117                         logger
118                                         .warn("set -Dcspace.server.secure=true to run security tests");
119                         return;
120                 }
121                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
122                                 "true");
123                 collectionObjectClient
124                                 .removeProperty(CollectionSpaceClient.USER_PROPERTY);
125                 collectionObjectClient.setProperty(
126                                 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
127                 try {
128                         collectionObjectClient.setupHttpClient();
129                         collectionObjectClient.setProxy();
130                 } catch (Exception e) {
131                         logger.error("createWithoutUser: caught " + e.getMessage());
132                         return;
133                 }
134                 ClientResponse<Response> res = collectionObjectClient.create(multipart);
135                 if(logger.isDebugEnabled()){
136             logger.debug("createWithoutUser: status = " + res.getStatus());
137         }
138                 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED
139                                 .getStatusCode(), "expected "
140                                 + Response.Status.UNAUTHORIZED.getStatusCode());
141         }
142
143         /**
144          * Creates the collection object instance without password.
145          */
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()) {
153                         logger
154                                         .warn("set -Dcspace.server.secure=true to run security tests");
155                         return;
156                 }
157                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
158                                 "true");
159                 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
160                                 "test");
161                 collectionObjectClient
162                                 .removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
163                 try {
164                         collectionObjectClient.setupHttpClient();
165                         collectionObjectClient.setProxy();
166                 } catch (Exception e) {
167                         logger.error("createWithoutPassword: caught " + e.getMessage());
168                         return;
169                 }
170                 ClientResponse<Response> res = collectionObjectClient.create(multipart);
171                 if(logger.isDebugEnabled()){
172             logger.debug("createWithoutPassword: status = " + res.getStatus());
173         }
174                 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED
175                                 .getStatusCode(), "expected "
176                                 + Response.Status.UNAUTHORIZED.getStatusCode());
177         }
178
179         /**
180          * Creates the collection object instance with incorrect password.
181          */
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()) {
189                         logger
190                                         .warn("set -Dcspace.server.secure=true to run security tests");
191                         return;
192                 }
193                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
194                                 "true");
195                 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
196                                 "test");
197                 collectionObjectClient.setProperty(
198                                 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
199                 try {
200                         collectionObjectClient.setupHttpClient();
201                         collectionObjectClient.setProxy();
202                 } catch (Exception e) {
203                         logger.error("createWithIncorrectPassword: caught "
204                                         + e.getMessage());
205                         return;
206                 }
207                 ClientResponse<Response> res = collectionObjectClient.create(multipart);
208                 if(logger.isDebugEnabled()){
209             logger.debug("createWithIncorrectPassword: status = " + res.getStatus());
210         }
211                 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED
212                                 .getStatusCode(), "expected "
213                                 + Response.Status.UNAUTHORIZED.getStatusCode());
214         }
215
216         /**
217          * Creates the collection object instance without user password.
218          */
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");
227                         return;
228                 }
229                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
230                                 "true");
231                 collectionObjectClient
232                                 .removeProperty(CollectionSpaceClient.USER_PROPERTY);
233                 collectionObjectClient
234                                 .removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
235                 try {
236                         collectionObjectClient.setupHttpClient();
237                         collectionObjectClient.setProxy();
238                 } catch (Exception e) {
239                         logger.error("createWithoutUserPassword: caught " + e.getMessage());
240                         return;
241                 }
242                 ClientResponse<Response> res = collectionObjectClient.create(multipart);
243                 if(logger.isDebugEnabled()){
244             logger.debug("createWithoutUserPassword: status = " + res.getStatus());
245         }
246                 Assert.assertEquals(res.getStatus(), Response.Status.FORBIDDEN
247                                 .getStatusCode(), "expected "
248                                 + Response.Status.FORBIDDEN.getStatusCode());
249         }
250
251         /**
252          * Creates the collection object instance with incorrect user password.
253          */
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");
262                         return;
263                 }
264                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
265                                 "true");
266                 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
267                                 "foo");
268                 collectionObjectClient.setProperty(
269                                 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
270                 try {
271                         collectionObjectClient.setupHttpClient();
272                         collectionObjectClient.setProxy();
273                 } catch (Exception e) {
274                         logger.error("createWithIncorrectUserPassword: caught "
275                                         + e.getMessage());
276                         return;
277                 }
278                 ClientResponse<Response> res = collectionObjectClient.create(multipart);
279                 if(logger.isDebugEnabled()){
280             logger.debug("createWithIncorrectUserPassword: status = " +
281                 res.getStatus());
282         }
283                 Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED
284                                 .getStatusCode(), "expected "
285                                 + Response.Status.UNAUTHORIZED.getStatusCode());
286         }
287
288         /* (non-Javadoc)
289          * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
290          */
291         @Override
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");
298                         return;
299                 }
300                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
301                                 "true");
302                 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
303                                 "test");
304                 collectionObjectClient.setProperty(
305                                 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
306                 try {
307                         collectionObjectClient.setupHttpClient();
308                         collectionObjectClient.setProxy();
309                 } catch (Exception e) {
310                         logger.error("deleteCollectionObject: caught " + e.getMessage());
311                         return;
312                 }
313                 if(logger.isDebugEnabled()){
314             logger.debug("Calling deleteCollectionObject:" + knownResourceId);
315         }
316                 ClientResponse<Response> res = collectionObjectClient
317                                 .delete(knownResourceId);
318                 if(logger.isDebugEnabled()){
319             logger.debug("deleteCollectionObject: status = " + res.getStatus());
320         }
321                 Assert.assertEquals(res.getStatus(),
322                                 Response.Status.OK.getStatusCode(), "expected "
323                                                 + Response.Status.OK.getStatusCode());
324         }
325
326         // ---------------------------------------------------------------
327         // Utility methods used by tests above
328         // ---------------------------------------------------------------
329         /**
330          * Creates the collection object instance.
331          * 
332          * @param commonPartName the common part name
333          * @param identifier the identifier
334          * 
335          * @return the multipart output
336          */
337         private MultipartOutput createCollectionObjectInstance(
338                         String commonPartName, String identifier) {
339                 return createCollectionObjectInstance(commonPartName, "objectNumber-"
340                                 + identifier, "objectName-" + identifier);
341         }
342
343         /**
344          * Creates the collection object instance.
345          * 
346          * @param commonPartName the common part name
347          * @param objectNumber the object number
348          * @param objectName the object name
349          * 
350          * @return the multipart output
351          */
352         private MultipartOutput createCollectionObjectInstance(
353                         String commonPartName, String objectNumber, String objectName) {
354                 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
355
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);
362
363                 if(logger.isDebugEnabled()){
364             logger.debug("to be created, collectionobject common ",
365                 collectionObject, CollectionobjectsCommon.class);
366         }
367                 return multipart;
368         }
369
370         /* (non-Javadoc)
371          * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
372          */
373         @Override
374         public void createList() throws Exception {
375         }
376
377         /* (non-Javadoc)
378          * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
379          */
380         @Override
381         public void createWithEmptyEntityBody() throws Exception {
382         }
383
384         /* (non-Javadoc)
385          * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
386          */
387         @Override
388         public void createWithMalformedXml() throws Exception {
389         }
390
391         /* (non-Javadoc)
392          * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
393          */
394         @Override
395         public void createWithWrongXmlSchema() throws Exception {
396         }
397
398         /* (non-Javadoc)
399          * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
400          */
401         @Override
402         public void read() throws Exception {
403         }
404
405         /* (non-Javadoc)
406          * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
407          */
408         @Override
409         public void readNonExistent() throws Exception {
410         }
411
412         /* (non-Javadoc)
413          * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
414          */
415         @Override
416         public void readList() throws Exception {
417         }
418
419         /* (non-Javadoc)
420          * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
421          */
422         @Override
423         public void update() throws Exception {
424         }
425
426         /* (non-Javadoc)
427          * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
428          */
429         @Override
430         public void updateWithEmptyEntityBody() throws Exception {
431         }
432
433         /* (non-Javadoc)
434          * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
435          */
436         @Override
437         public void updateWithMalformedXml() throws Exception {
438         }
439
440         /* (non-Javadoc)
441          * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
442          */
443         @Override
444         public void updateWithWrongXmlSchema() throws Exception {
445         }
446
447         /* (non-Javadoc)
448          * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
449          */
450         @Override
451         public void updateNonExistent() throws Exception {
452         }
453
454         /* (non-Javadoc)
455          * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
456          */
457         @Override
458         public void deleteNonExistent() throws Exception {
459         }
460 }