]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
460b6af6204fec32fff3b159be9044159257627e
[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(dataProvider="testName", dataProviderClass=AbstractServiceTest.class)
70         @Override
71         public void create(String testName) {
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(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");
299                         return;
300                 }
301                 collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
302                                 "true");
303                 collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
304                                 "test");
305                 collectionObjectClient.setProperty(
306                                 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
307                 try {
308                         collectionObjectClient.setupHttpClient();
309                         collectionObjectClient.setProxy();
310                 } catch (Exception e) {
311                         logger.error("deleteCollectionObject: caught " + e.getMessage());
312                         return;
313                 }
314                 if(logger.isDebugEnabled()){
315             logger.debug("Calling deleteCollectionObject:" + knownResourceId);
316         }
317                 ClientResponse<Response> res = collectionObjectClient
318                                 .delete(knownResourceId);
319                 if(logger.isDebugEnabled()){
320             logger.debug("deleteCollectionObject: status = " + res.getStatus());
321         }
322                 Assert.assertEquals(res.getStatus(),
323                                 Response.Status.OK.getStatusCode(), "expected "
324                                                 + Response.Status.OK.getStatusCode());
325         }
326
327         // ---------------------------------------------------------------
328         // Utility methods used by tests above
329         // ---------------------------------------------------------------
330         /**
331          * Creates the collection object instance.
332          * 
333          * @param commonPartName the common part name
334          * @param identifier the identifier
335          * 
336          * @return the multipart output
337          */
338         private MultipartOutput createCollectionObjectInstance(
339                         String commonPartName, String identifier) {
340                 return createCollectionObjectInstance(commonPartName, "objectNumber-"
341                                 + identifier, "objectName-" + identifier);
342         }
343
344         /**
345          * Creates the collection object instance.
346          * 
347          * @param commonPartName the common part name
348          * @param objectNumber the object number
349          * @param objectName the object name
350          * 
351          * @return the multipart output
352          */
353         private MultipartOutput createCollectionObjectInstance(
354                         String commonPartName, String objectNumber, String objectName) {
355                 CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
356
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);
363
364                 if(logger.isDebugEnabled()){
365             logger.debug("to be created, collectionobject common ",
366                 collectionObject, CollectionobjectsCommon.class);
367         }
368                 return multipart;
369         }
370
371         /* (non-Javadoc)
372          * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
373          */
374         @Override
375         public void createList(String testName) throws Exception {
376         }
377
378         /* (non-Javadoc)
379          * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
380          */
381         @Override
382         public void createWithEmptyEntityBody(String testName) throws Exception {
383         }
384
385         /* (non-Javadoc)
386          * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
387          */
388         @Override
389         public void createWithMalformedXml(String testName) throws Exception {
390         }
391
392         /* (non-Javadoc)
393          * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
394          */
395         @Override
396         public void createWithWrongXmlSchema(String testName) throws Exception {
397         }
398
399         /* (non-Javadoc)
400          * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
401          */
402         @Override
403         public void read(String testName) throws Exception {
404         }
405
406         /* (non-Javadoc)
407          * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
408          */
409         @Override
410         public void readNonExistent(String testName) throws Exception {
411         }
412
413         /* (non-Javadoc)
414          * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
415          */
416         @Override
417         public void readList(String testName) throws Exception {
418         }
419
420         /* (non-Javadoc)
421          * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
422          */
423         @Override
424         public void update(String testName) throws Exception {
425         }
426
427         /* (non-Javadoc)
428          * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
429          */
430         @Override
431         public void updateWithEmptyEntityBody(String testName) throws Exception {
432         }
433
434         /* (non-Javadoc)
435          * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
436          */
437         @Override
438         public void updateWithMalformedXml(String testName) throws Exception {
439         }
440
441         /* (non-Javadoc)
442          * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
443          */
444         @Override
445         public void updateWithWrongXmlSchema(String testName) throws Exception {
446         }
447
448         /* (non-Javadoc)
449          * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
450          */
451         @Override
452         public void updateNonExistent(String testName) throws Exception {
453         }
454
455         /* (non-Javadoc)
456          * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
457          */
458         @Override
459         public void deleteNonExistent(String testName) throws Exception {
460         }
461 }