]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
7b12a296322daeaa5acabff5abf376e6229a2c93
[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.test;
24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.ws.rs.core.MediaType;
29 import javax.ws.rs.core.Response;
30
31 //import org.apache.commons.codec.binary.Base64;
32 import org.jboss.resteasy.client.ClientResponse;
33 import org.jboss.resteasy.plugins.providers.multipart.MultipartOutput;
34 import org.jboss.resteasy.plugins.providers.multipart.OutputPart;
35
36 import org.testng.Assert;
37 import org.testng.annotations.Test;
38
39 import org.collectionspace.services.account.AccountTenant;
40 import org.collectionspace.services.client.AccountClient;
41 import org.collectionspace.services.account.AccountsCommon;
42 import org.collectionspace.services.collectionobject.CollectionobjectsCommon;
43 import org.collectionspace.services.client.CollectionObjectClient;
44 import org.collectionspace.services.client.CollectionSpaceClient;
45 import org.collectionspace.services.client.test.AbstractServiceTestImpl;
46 import org.collectionspace.services.client.test.BaseServiceTest;
47 import org.collectionspace.services.jaxb.AbstractCommonList;
48
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
51
52 /**
53  * AuthenticationServiceTest uses CollectionObject service to test
54  * authentication
55  * 
56  * $LastChangedRevision: 434 $ $LastChangedDate: 2009-07-28 14:34:15 -0700 (Tue,
57  * 28 Jul 2009) $
58  */
59 public class AuthenticationServiceTest extends AbstractServiceTestImpl {
60
61     /** The known resource id. */
62     private String knownResourceId = null;
63     private String barneyAccountId = null;
64
65     /** The logger. */
66     final Logger logger = LoggerFactory.getLogger(AuthenticationServiceTest.class);
67
68     /* (non-Javadoc)
69      * @see org.collectionspace.services.client.test.AbstractServiceTest#getServicePathComponent()
70      */
71     @Override
72     protected String getServicePathComponent() {
73         // no need to return anything but null since no auth resources are
74         // accessed
75         return null;
76     }
77
78     /* (non-Javadoc)
79      * @see org.collectionspace.services.client.test.BaseServiceTest#getClientInstance()
80      */
81     @Override
82     protected CollectionSpaceClient getClientInstance() {
83         return new AccountClient();
84     }
85     
86     /* (non-Javadoc)
87      * @see org.collectionspace.services.client.test.BaseServiceTest#getAbstractCommonList(org.jboss.resteasy.client.ClientResponse)
88      */
89     @Override
90         protected AbstractCommonList getAbstractCommonList(
91                         ClientResponse<AbstractCommonList> response) {
92         throw new UnsupportedOperationException(); //Since this test does not support lists, this method is not needed.
93     }
94     
95         @Test(dataProvider = "testName")
96         @Override
97     public void readPaginatedList(String testName) throws Exception {
98                 // Test not supported.
99         }    
100
101         @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class)
102     public void createAccounts(String testName) throws Exception {
103         // Perform setup, such as initializing the type of service request
104         // (e.g. CREATE, DELETE), its valid and expected status codes, and
105         // its associated HTTP method name (e.g. POST, DELETE).
106         setupCreate(testName);
107         AccountClient accountClient = new AccountClient();
108
109         accountClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
110                 "true");
111         accountClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
112                 "test");
113         accountClient.setProperty(
114                 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
115         // Submit the request to the service and store the response.
116         AccountsCommon account =
117                 createAccountInstance("barney", "barney08", "barney@dinoland.com", "1");
118         ClientResponse<Response> res = accountClient.create(account);
119         int statusCode = res.getStatus();
120
121         if (logger.isDebugEnabled()) {
122             logger.debug(testName + ": barney status = " + statusCode);
123         }
124         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
125                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
126         Assert.assertEquals(statusCode, EXPECTED_STATUS_CODE);
127
128         // Store the ID returned from this create operation
129         // for additional tests below.
130         barneyAccountId = extractId(res);
131         if (logger.isDebugEnabled()) {
132             logger.debug(testName + ": barneyAccountId=" + barneyAccountId);
133         }
134
135         account = createAccountInstance("babybop", "babybop09", "babybop@dinoland.com", "non-existent");
136         res = accountClient.create(account);
137         statusCode = res.getStatus();
138
139         if (logger.isDebugEnabled()) {
140             logger.debug(testName + ": babybop status = " + statusCode);
141         }
142         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
143                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
144         Assert.assertEquals(statusCode, Response.Status.BAD_REQUEST.getStatusCode());
145
146     }
147
148
149     /* (non-Javadoc)
150      * @see org.collectionspace.services.client.test.AbstractServiceTest#create()
151      */
152     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
153     dependsOnMethods = {"createAccounts"})
154     @Override
155     public void create(String testName) {
156         setupCreate(testName);
157         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
158         String identifier = BaseServiceTest.createIdentifier();
159         MultipartOutput multipart = createCollectionObjectInstance(
160                 collectionObjectClient.getCommonPartName(), identifier);
161
162         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
163                 "true");
164         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
165                 "barney");
166         collectionObjectClient.setProperty(
167                 CollectionSpaceClient.PASSWORD_PROPERTY, "barney08");
168         try {
169             collectionObjectClient.setupHttpClient();
170             collectionObjectClient.setProxy();
171         } catch (Exception e) {
172             logger.error("create: caught " + e.getMessage());
173             return;
174         }
175         ClientResponse<Response> res = collectionObjectClient.create(multipart);
176         if (logger.isDebugEnabled()) {
177             logger.debug("create: status = " + res.getStatus());
178         }
179         Assert.assertEquals(res.getStatus(), Response.Status.CREATED.getStatusCode(), "expected " + Response.Status.CREATED.getStatusCode());
180
181         // Store the ID returned from this create operation for additional tests
182         // below.
183         knownResourceId = extractId(res);
184     }
185
186     /**
187      * Creates the collection object instance without password.
188      */
189     @Test(dependsOnMethods = {"createAccounts"})
190     public void createWithoutPassword() {
191         banner("createWithoutPassword");
192         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
193         String identifier = BaseServiceTest.createIdentifier();
194         MultipartOutput multipart = createCollectionObjectInstance(
195                 collectionObjectClient.getCommonPartName(), identifier);
196
197         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
198                 "true");
199         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
200                 "test");
201         collectionObjectClient.removeProperty(CollectionSpaceClient.PASSWORD_PROPERTY);
202         try {
203             collectionObjectClient.setupHttpClient();
204             collectionObjectClient.setProxy();
205         } catch (Exception e) {
206             logger.error("createWithoutPassword: caught " + e.getMessage());
207             return;
208         }
209         ClientResponse<Response> res = collectionObjectClient.create(multipart);
210         if (logger.isDebugEnabled()) {
211             logger.debug("createWithoutPassword: status = " + res.getStatus());
212         }
213         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
214     }
215
216     /**
217      * Creates the collection object with unknown user
218      */
219     @Test(dependsOnMethods = {"createAccounts"})
220     public void createWithUnknownUser() {
221         banner("createWithUnknownUser");
222         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
223         String identifier = BaseServiceTest.createIdentifier();
224         MultipartOutput multipart = createCollectionObjectInstance(
225                 collectionObjectClient.getCommonPartName(), identifier);
226
227         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
228                 "true");
229         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
230                 "foo");
231         collectionObjectClient.setProperty(CollectionSpaceClient.PASSWORD_PROPERTY,
232                 "bar");
233         try {
234             collectionObjectClient.setupHttpClient();
235             collectionObjectClient.setProxy();
236         } catch (Exception e) {
237             logger.error("createWithUnknownUser: caught " + e.getMessage());
238             return;
239         }
240         ClientResponse<Response> res = collectionObjectClient.create(multipart);
241         if (logger.isDebugEnabled()) {
242             logger.debug("createWithUnknownUser: status = " + res.getStatus());
243         }
244         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
245     }
246
247     /**
248      * Creates the collection object instance with incorrect password.
249      */
250     @Test(dependsOnMethods = {"createAccounts"})
251     public void createWithIncorrectPassword() {
252         banner("createWithIncorrectPassword");
253         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
254         String identifier = BaseServiceTest.createIdentifier();
255         MultipartOutput multipart = createCollectionObjectInstance(
256                 collectionObjectClient.getCommonPartName(), identifier);
257
258         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
259                 "true");
260         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
261                 "test");
262         collectionObjectClient.setProperty(
263                 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
264         try {
265             collectionObjectClient.setupHttpClient();
266             collectionObjectClient.setProxy();
267         } catch (Exception e) {
268             logger.error("createWithIncorrectPassword: caught " + e.getMessage());
269             return;
270         }
271         ClientResponse<Response> res = collectionObjectClient.create(multipart);
272         if (logger.isDebugEnabled()) {
273             logger.debug("createWithIncorrectPassword: status = " + res.getStatus());
274         }
275         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
276     }
277
278     /**
279      * Creates the collection object instance with incorrect user password.
280      */
281     @Test(dependsOnMethods = {"createAccounts"})
282     public void createWithIncorrectUserPassword() {
283         banner("createWithIncorrectUserPassword");
284         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
285         String identifier = BaseServiceTest.createIdentifier();
286         MultipartOutput multipart = createCollectionObjectInstance(
287                 collectionObjectClient.getCommonPartName(), identifier);
288
289         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
290                 "true");
291         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
292                 "foo");
293         collectionObjectClient.setProperty(
294                 CollectionSpaceClient.PASSWORD_PROPERTY, "bar");
295         try {
296             collectionObjectClient.setupHttpClient();
297             collectionObjectClient.setProxy();
298         } catch (Exception e) {
299             logger.error("createWithIncorrectUserPassword: caught " + e.getMessage());
300             return;
301         }
302         ClientResponse<Response> res = collectionObjectClient.create(multipart);
303         if (logger.isDebugEnabled()) {
304             logger.debug("createWithIncorrectUserPassword: status = "
305                     + res.getStatus());
306         }
307         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
308     }
309
310     /**
311      * Creates the collection object instance with incorrect user password.
312      */
313     @Test(dependsOnMethods = {"createAccounts"})
314     public void createWithoutTenant() {
315         banner("createWithoutTenant");
316         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
317         String identifier = BaseServiceTest.createIdentifier();
318         MultipartOutput multipart = createCollectionObjectInstance(
319                 collectionObjectClient.getCommonPartName(), identifier);
320
321         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
322                 "true");
323         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
324                 "babybop");
325         collectionObjectClient.setProperty(
326                 CollectionSpaceClient.PASSWORD_PROPERTY, "babybop09");
327         try {
328             collectionObjectClient.setupHttpClient();
329             collectionObjectClient.setProxy();
330         } catch (Exception e) {
331             logger.error("createWithoutTenant: caught " + e.getMessage());
332             return;
333         }
334         ClientResponse<Response> res = collectionObjectClient.create(multipart);
335         if (logger.isDebugEnabled()) {
336             logger.debug("createWithoutTenant: status = "
337                     + res.getStatus());
338         }
339         Assert.assertEquals(res.getStatus(), Response.Status.UNAUTHORIZED.getStatusCode(), "expected " + Response.Status.UNAUTHORIZED.getStatusCode());
340     }
341
342     /* (non-Javadoc)
343      * @see org.collectionspace.services.client.test.AbstractServiceTest#delete()
344      */
345     @Override
346     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
347     dependsOnMethods = {"create"})
348     public void delete(String testName) {
349         setupDelete(testName);
350         CollectionObjectClient collectionObjectClient = new CollectionObjectClient();
351         collectionObjectClient = new CollectionObjectClient();
352
353         collectionObjectClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
354                 "true");
355         collectionObjectClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
356                 "test");
357         collectionObjectClient.setProperty(
358                 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
359         try {
360             collectionObjectClient.setupHttpClient();
361             collectionObjectClient.setProxy();
362         } catch (Exception e) {
363             logger.error("deleteCollectionObject: caught " + e.getMessage());
364             return;
365         }
366         if (logger.isDebugEnabled()) {
367             logger.debug("Calling deleteCollectionObject:" + knownResourceId);
368         }
369         ClientResponse<Response> res = collectionObjectClient.delete(knownResourceId);
370         if (logger.isDebugEnabled()) {
371             logger.debug("deleteCollectionObject: status = " + res.getStatus());
372         }
373         Assert.assertEquals(res.getStatus(),
374                 Response.Status.OK.getStatusCode(), "expected " + Response.Status.OK.getStatusCode());
375     }
376
377     @Test(dataProvider = "testName", dataProviderClass = AbstractServiceTestImpl.class,
378     dependsOnMethods = {"delete"})
379     public void deleteAccounts(String testName) throws Exception {
380
381         // Perform setup.
382         setupDelete(testName);
383         AccountClient accountClient = new AccountClient();
384
385         accountClient.setProperty(CollectionSpaceClient.AUTH_PROPERTY,
386                 "true");
387         accountClient.setProperty(CollectionSpaceClient.USER_PROPERTY,
388                 "test");
389         accountClient.setProperty(
390                 CollectionSpaceClient.PASSWORD_PROPERTY, "test");
391         // Submit the request to the service and store the response.
392         ClientResponse<Response> res = accountClient.delete(barneyAccountId);
393         int statusCode = res.getStatus();
394         if (logger.isDebugEnabled()) {
395             logger.debug(testName + ": barney status = " + statusCode);
396         }
397         Assert.assertTrue(REQUEST_TYPE.isValidStatusCode(statusCode),
398                 invalidStatusCodeMessage(REQUEST_TYPE, statusCode));
399     }
400
401     // ---------------------------------------------------------------
402     // Utility methods used by tests above
403     // ---------------------------------------------------------------
404     /**
405      * Creates the collection object instance.
406      *
407      * @param commonPartName the common part name
408      * @param identifier the identifier
409      *
410      * @return the multipart output
411      */
412     private MultipartOutput createCollectionObjectInstance(
413             String commonPartName, String identifier) {
414         return createCollectionObjectInstance(commonPartName, "objectNumber-" + identifier, "objectName-" + identifier);
415     }
416
417     /**
418      * Creates the collection object instance.
419      *
420      * @param commonPartName the common part name
421      * @param objectNumber the object number
422      * @param objectName the object name
423      *
424      * @return the multipart output
425      */
426     private MultipartOutput createCollectionObjectInstance(
427             String commonPartName, String objectNumber, String objectName) {
428         CollectionobjectsCommon collectionObject = new CollectionobjectsCommon();
429
430         collectionObject.setObjectNumber(objectNumber);
431         collectionObject.setObjectName(objectName);
432         MultipartOutput multipart = new MultipartOutput();
433         OutputPart commonPart = multipart.addPart(collectionObject,
434                 MediaType.APPLICATION_XML_TYPE);
435         commonPart.getHeaders().add("label", commonPartName);
436
437         if (logger.isDebugEnabled()) {
438             logger.debug("to be created, collectionobject common ",
439                     collectionObject, CollectionobjectsCommon.class);
440         }
441         return multipart;
442     }
443
444     private AccountsCommon createAccountInstance(String screenName,
445             String passwd, String email, String tenantId) {
446
447         AccountsCommon account = new AccountsCommon();
448         account.setScreenName(screenName);
449         account.setUserId(screenName);
450         //jaxb would encode b64
451         account.setPassword(passwd.getBytes());
452         account.setEmail(email);
453         account.setPhone("1234567890");
454         List<AccountTenant> atl = new ArrayList<AccountTenant>();
455
456         AccountTenant at = new AccountTenant();
457         at.setTenantId(tenantId);//for testing purposes
458         atl.add(at);
459         //disable 2nd tenant till tenant identification is in effect
460         //on the service side for 1-n user-tenants
461 //        AccountsCommon.Tenant at2 = new AccountsCommon.Tenant();
462 //        at2.setId(UUID.randomUUID().toString());
463 //        at2.setName("collectionspace.org");
464 //        atl.add(at2);
465         account.setTenants(atl);
466
467         if (logger.isDebugEnabled()) {
468             logger.debug("to be created, account common");
469             logger.debug(objectAsXmlString(account,
470                     AccountsCommon.class));
471         }
472         return account;
473
474     }
475
476     /* (non-Javadoc)
477      * @see org.collectionspace.services.client.test.AbstractServiceTest#createList()
478      */
479     @Override
480     public void createList(String testName) throws Exception {
481         //FIXME: Should this test really be empty?  If so, please comment accordingly.
482     }
483
484     /* (non-Javadoc)
485      * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithEmptyEntityBody()
486      */
487     @Override
488     public void createWithEmptyEntityBody(String testName) throws Exception {
489         //FIXME: Should this test really be empty?  If so, please comment accordingly.
490     }
491
492     /* (non-Javadoc)
493      * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithMalformedXml()
494      */
495     @Override
496     public void createWithMalformedXml(String testName) throws Exception {
497         //FIXME: Should this test really be empty?  If so, please comment accordingly.
498     }
499
500     /* (non-Javadoc)
501      * @see org.collectionspace.services.client.test.AbstractServiceTest#createWithWrongXmlSchema()
502      */
503     @Override
504     public void createWithWrongXmlSchema(String testName) throws Exception {
505         //FIXME: Should this test really be empty?  If so, please comment accordingly.
506     }
507
508     /* (non-Javadoc)
509      * @see org.collectionspace.services.client.test.AbstractServiceTest#read()
510      */
511     @Override
512     public void read(String testName) throws Exception {
513         //FIXME: Should this test really be empty?  If so, please comment accordingly.
514     }
515
516     /* (non-Javadoc)
517      * @see org.collectionspace.services.client.test.AbstractServiceTest#readNonExistent()
518      */
519     @Override
520     public void readNonExistent(String testName) throws Exception {
521         //FIXME: Should this test really be empty?  If so, please comment accordingly.
522     }
523
524     /* (non-Javadoc)
525      * @see org.collectionspace.services.client.test.AbstractServiceTest#readList()
526      */
527     @Override
528     public void readList(String testName) throws Exception {
529         //FIXME: Should this test really be empty?  If so, please comment accordingly.
530     }
531
532     /* (non-Javadoc)
533      * @see org.collectionspace.services.client.test.AbstractServiceTest#update()
534      */
535     @Override
536     public void update(String testName) throws Exception {
537         //FIXME: Should this test really be empty?  If so, please comment accordingly.
538     }
539
540     /* (non-Javadoc)
541      * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithEmptyEntityBody()
542      */
543     @Override
544     public void updateWithEmptyEntityBody(String testName) throws Exception {
545         //FIXME: Should this test really be empty?  If so, please comment accordingly.
546     }
547
548     /* (non-Javadoc)
549      * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithMalformedXml()
550      */
551     @Override
552     public void updateWithMalformedXml(String testName) throws Exception {
553         //FIXME: Should this test really be empty?  If so, please comment accordingly.
554     }
555
556     /* (non-Javadoc)
557      * @see org.collectionspace.services.client.test.AbstractServiceTest#updateWithWrongXmlSchema()
558      */
559     @Override
560     public void updateWithWrongXmlSchema(String testName) throws Exception {
561         //FIXME: Should this test really be empty?  If so, please comment accordingly.
562     }
563
564     /* (non-Javadoc)
565      * @see org.collectionspace.services.client.test.AbstractServiceTest#updateNonExistent()
566      */
567     @Override
568     public void updateNonExistent(String testName) throws Exception {
569         //FIXME: Should this test really be empty?  If so, please comment accordingly.
570     }
571
572     /* (non-Javadoc)
573      * @see org.collectionspace.services.client.test.AbstractServiceTest#deleteNonExistent()
574      */
575     @Override
576     public void deleteNonExistent(String testName) throws Exception {
577         //FIXME: Should this test really be empty?  If so, please comment accordingly.
578     }
579 }