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