1 13 14 package org.ejbca.core.ejb.authorization; 15 16 import java.util.ArrayList ; 17 import java.util.Collection ; 18 import java.util.Iterator ; 19 20 import javax.naming.Context ; 21 import javax.naming.NamingException ; 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 36 public class TestAuthorizationSession extends TestCase { 37 private static Logger log = Logger.getLogger(TestAuthorizationSession.class); 38 39 private static Context 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 49 public TestAuthorizationSession(String name) { 50 super(name); 51 52 try { 53 ctx = getInitialContext(); 54 Object 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 e) { 60 e.printStackTrace(); 61 assertTrue("Exception on setup", false); 62 } 63 } 64 65 protected void setUp() throws Exception { 66 } 67 68 protected void tearDown() throws Exception { 69 } 70 71 private Context getInitialContext() throws NamingException { 72 Context ctx = new javax.naming.InitialContext (); 73 return ctx; 74 } 75 76 77 82 public void test01Initialize() throws Exception { 83 log.debug(">test01Initialize()"); 84 85 authorizationsession.initialize(admin, caid); 87 88 AdminGroup ag = authorizationsession.getAdminGroup(admin, LocalAuthorizationSessionBean.PUBLICWEBGROUPNAME, caid); 90 assertNotNull(ag); 91 Collection rules = ag.getAccessRules(); 92 assertEquals(8, rules.size()); 93 94 ArrayList accessrules = new ArrayList (); 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 ag = authorizationsession.getAdminGroup(admin, LocalAuthorizationSessionBean.PUBLICWEBGROUPNAME, caid); 103 assertNotNull(ag); 104 rules = ag.getAccessRules(); 105 assertEquals(11, rules.size()); Iterator 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 authorizationsession.initialize(admin, caid); 119 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 |