b1a369347032d6122a01145543659a5afb5ab6ab
[utils] / security / src / test / java / org / wamblee / security / authorization / hibernate / PersistentAuthorizationServiceTest.java
1 /*
2  * Copyright 2005 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.wamblee.security.authorization.hibernate;
18
19 import java.sql.SQLException;
20
21 import org.apache.log4j.Logger;
22 import org.hibernate.cfg.Configuration;
23 import org.hibernate.dialect.MySQL5Dialect;
24 import org.hibernate.dialect.MySQL5InnoDBDialect;
25 import org.hibernate.tool.hbm2ddl.SchemaExport;
26 import org.springframework.orm.hibernate3.HibernateTemplate;
27 import org.wamblee.general.BeanKernel;
28 import org.wamblee.security.authorization.AuthorizationService;
29 import org.wamblee.security.authorization.AuthorizationServiceTest;
30 import org.wamblee.security.authorization.TestUserAccessor;
31 import org.wamblee.system.adapters.ClassConfiguration;
32 import org.wamblee.system.adapters.ClassConfigurationTest;
33 import org.wamblee.system.adapters.DefaultContainer;
34 import org.wamblee.system.adapters.ObjectConfiguration;
35 import org.wamblee.system.components.DatabaseComponentFactory;
36 import org.wamblee.system.core.Scope;
37 import org.wamblee.system.spring.component.DatabaseTesterComponent;
38 import org.wamblee.system.spring.component.DatasourceComponent;
39 import org.wamblee.usermgt.UserAccessor;
40 import org.wamblee.usermgt.hibernate.AuthorizationComponent;
41 import org.wamblee.usermgt.hibernate.HibernateUserAdministrationTest;
42 import org.wamblee.usermgt.hibernate.UserAdministrationComponent;
43
44 /**
45  * Unit test for the persistent authorization service.
46  * 
47  * @author Erik Brakkee
48  */
49 public class PersistentAuthorizationServiceTest extends
50         AuthorizationServiceTest {
51
52     private static final Logger LOGGER = Logger
53             .getLogger(PersistentAuthorizationServiceTest.class);
54
55     private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
56     private static final String RULES_TABLE = "AUTHORIZATION_RULES";
57     private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
58     private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
59     private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
60     private static final String USERCOND_TABLE = "USER_CONDITIONS";
61
62     private DefaultContainer container;
63     private Scope scope;
64
65     private DatabaseTesterComponent databaseTester;
66     private UserAccessor userAccessor;
67     private HibernateTemplate hibernateTemplate;
68     private AuthorizationService authorizationService;
69
70     @Override
71     protected void setUp() throws Exception {
72
73         container = new DefaultContainer("top");
74         DatabaseComponentFactory.addDatabaseConfig(container);
75         container.addComponent(new DatasourceComponent("datasource"));
76         ClassConfiguration useraccessorConfig = new ClassConfiguration(
77                 TestUserAccessor.class);
78         useraccessorConfig.getObjectConfig().getSetterConfig().initAllSetters();
79         container.addComponent("userAccessor", useraccessorConfig);
80         container.addComponent(new AuthorizationComponent("authorization",
81                 true));
82
83         ClassConfiguration dbtesterConfig = new ClassConfiguration(
84                 DatabaseTesterComponent.class);
85         dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
86         container.addComponent("databaseTester", dbtesterConfig);
87
88         ObjectConfiguration config = new ObjectConfiguration(
89                 PersistentAuthorizationServiceTest.class);
90         config.getSetterConfig().clear().add("setUserAccessor").add(
91                 "setDatabaseTester").add("setHibernateTemplate").add(
92                 "setAuthorizationService");
93         container.addComponent("testcase", this, config);
94
95         scope = container.start();
96
97         databaseTester.cleanDatabase();
98
99         super.setUp();
100     }
101
102     public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
103         databaseTester = aDatabaseTester;
104     }
105
106     public void setUserAccessor(UserAccessor aUserAccessor) {
107         userAccessor = aUserAccessor;
108     }
109
110     public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
111         hibernateTemplate = aHibernateTemplate;
112     }
113
114     public void setAuthorizationService(
115             AuthorizationService aAuthorizationService) {
116         authorizationService = aAuthorizationService;
117     }
118
119     /*
120      * (non-Javadoc)
121      * 
122      * @see
123      * org.wamblee.security.authorization.AuthorizationServiceTest#createService
124      * ()
125      */
126     @Override
127     protected AuthorizationService createService() {
128         PersistentAuthorizationService service = new PersistentAuthorizationService(
129                 "DEFAULT", hibernateTemplate, createUserAccessor(), 10000);
130         return service;
131     }
132
133     /*
134      * (non-Javadoc)
135      * 
136      * @see
137      * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
138      * (int)
139      */
140     @Override
141     protected void checkRuleCount(int aCount) {
142         try {
143             assertEquals(1, databaseTester.getTableSize(SERVICE_TABLE));
144             assertEquals(aCount, databaseTester.getTableSize(RULES_TABLE));
145             assertEquals(aCount, databaseTester
146                     .getTableSize(SERVICE_RULES_TABLE));
147             assertEquals(aCount, databaseTester.getTableSize(USERCOND_TABLE));
148             assertEquals(aCount, databaseTester.getTableSize(PATHCOND_TABLE));
149             assertEquals(aCount, databaseTester
150                     .getTableSize(OPERATIONCOND_TABLE));
151         } catch (SQLException e) {
152             throw new RuntimeException(e);
153         }
154
155     }
156
157     public void testSchemaExport() { 
158         Configuration config = new Configuration();
159         for (String mappingFile: new AuthorizationMappingFiles()) { 
160            config.addResource(mappingFile);
161         }
162         config.setProperty("hibernate.dialect", MySQL5InnoDBDialect.class.getName());
163         SchemaExport exporter = new SchemaExport(config);
164         exporter.setOutputFile("target/mysql5.schema.sql");
165         exporter.create(true,false);
166     }
167
168     public void testPerformance() {
169
170         PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
171
172         int n = 1000;
173         long time = System.currentTimeMillis();
174         for (int i = 0; i < n; i++) {
175             testFirstRuleGrants();
176             resetTestRules();
177             testSecondRuleDenies();
178             resetTestRules();
179             testThirdRuleGrants();
180             resetTestRules();
181             testNoRulesSupportResource();
182         }
183         LOGGER.info("Executed " + 4 * n + " authorization checks in "
184                 + (float) (System.currentTimeMillis() - time) / (float) 1000
185                 + " seconds.");
186     }
187 }