KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejbca > core > ejb > authorization > TestAuthorizationSession


1 /*************************************************************************
2  * *
3  * EJBCA: The OpenSource Certificate Authority *
4  * *
5  * This software is free software; you can redistribute it and/or *
6  * modify it under the terms of the GNU Lesser General Public *
7  * License as published by the Free Software Foundation; either *
8  * version 2.1 of the License, or any later version. *
9  * *
10  * See terms of license at gnu.org. *
11  * *
12  *************************************************************************/

13
14 package org.ejbca.core.ejb.authorization;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.Iterator JavaDoc;
19
20 import javax.naming.Context JavaDoc;
21 import javax.naming.NamingException JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.apache.log4j.Logger;
26 import org.ejbca.core.model.authorization.AccessRule;
27 import org.ejbca.core.model.authorization.AdminGroup;
28 import org.ejbca.core.model.log.Admin;
29
30
31 /**
32  * Tests authentication session used by signer.
33  *
34  * @version $Id: TestAuthorizationSession.java,v 1.1.2.2 2007/05/09 08:11:03 anatom Exp $
35  */

36 public class TestAuthorizationSession extends TestCase {
37     private static Logger log = Logger.getLogger(TestAuthorizationSession.class);
38
39     private static Context JavaDoc ctx;
40     private static IAuthorizationSessionRemote authorizationsession;
41     private static int caid="CN=TEST Authorization,O=PrimeKey,C=SE".hashCode();
42     private static Admin admin = null;
43
44     /**
45      * Creates a new TestAuthenticationSession object.
46      *
47      * @param name name
48      */

49     public TestAuthorizationSession(String JavaDoc name) {
50         super(name);
51
52         try {
53             ctx = getInitialContext();
54             Object JavaDoc obj = ctx.lookup(IAuthorizationSessionHome.JNDI_NAME);
55             IAuthorizationSessionHome authorizationsessionhome = (IAuthorizationSessionHome) javax.rmi.PortableRemoteObject.narrow(obj, IAuthorizationSessionHome.class);
56             authorizationsession = authorizationsessionhome.create();
57             
58             admin = new Admin(Admin.TYPE_INTERNALUSER);
59         } catch (Exception JavaDoc e) {
60             e.printStackTrace();
61             assertTrue("Exception on setup", false);
62         }
63     }
64
65     protected void setUp() throws Exception JavaDoc {
66     }
67
68     protected void tearDown() throws Exception JavaDoc {
69     }
70
71     private Context JavaDoc getInitialContext() throws NamingException JavaDoc {
72         Context JavaDoc ctx = new javax.naming.InitialContext JavaDoc();
73         return ctx;
74     }
75
76
77     /**
78      * tests initialization of authorization bean
79      *
80      * @throws Exception error
81      */

82     public void test01Initialize() throws Exception JavaDoc {
83         log.debug(">test01Initialize()");
84         
85         // Initialize with a new CA
86
authorizationsession.initialize(admin, caid);
87
88         // Retrieve access rules and check that they were added
89
AdminGroup ag = authorizationsession.getAdminGroup(admin, LocalAuthorizationSessionBean.PUBLICWEBGROUPNAME, caid);
90         assertNotNull(ag);
91         Collection JavaDoc rules = ag.getAccessRules();
92         assertEquals(8, rules.size());
93
94         // Add some new strange access rules
95
ArrayList JavaDoc accessrules = new ArrayList JavaDoc();
96         accessrules.add(new AccessRule("/public_foo_user", AccessRule.RULE_ACCEPT, false));
97         accessrules.add(new AccessRule("/foo_functionality/basic_functions", AccessRule.RULE_ACCEPT, false));
98         accessrules.add(new AccessRule("/foo_functionality/view_certificate", AccessRule.RULE_ACCEPT, false));
99         authorizationsession.addAccessRules(admin, LocalAuthorizationSessionBean.PUBLICWEBGROUPNAME, caid, accessrules);
100         
101         // Retrieve the access rules and check that they were added
102
ag = authorizationsession.getAdminGroup(admin, LocalAuthorizationSessionBean.PUBLICWEBGROUPNAME, caid);
103         assertNotNull(ag);
104         rules = ag.getAccessRules();
105         assertEquals(11, rules.size()); // We have added three rules
106
Iterator JavaDoc iter = rules.iterator();
107         boolean found = false;
108         while (iter.hasNext()) {
109             AccessRule rule = (AccessRule)iter.next();
110             if (rule.getAccessRule().equals("/foo_functionality/view_certificate")) {
111                 found = true;
112             }
113         }
114         assertTrue(found);
115         
116         // Initialize the same CA again, this will remove old default Public Web rules and create new ones.
117
// This had some troubles with glassfish before, hence the creation of this test
118
authorizationsession.initialize(admin, caid);
119         // Retrieve access rules and check that we only have the default ones
120
ag = authorizationsession.getAdminGroup(admin, LocalAuthorizationSessionBean.PUBLICWEBGROUPNAME, caid);
121         assertNotNull(ag);
122         rules = ag.getAccessRules();
123         assertEquals(8, rules.size());
124         iter = rules.iterator();
125         found = false;
126         while (iter.hasNext()) {
127             AccessRule rule = (AccessRule)iter.next();
128             if (rule.getAccessRule().equals("/foo_functionality/view_certificate")) {
129                 found = true;
130             }
131         }
132         assertFalse(found);
133         
134         log.debug("<test01Initialize()");
135     }
136
137 }
138
Popular Tags