Removed DOCUMENT ME comments that were generated and applied source code
[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 package org.wamblee.security.authorization.hibernate;
17
18 import org.apache.log4j.Logger;
19
20 import org.hibernate.cfg.Configuration;
21
22 import org.hibernate.dialect.MySQL5Dialect;
23 import org.hibernate.dialect.MySQL5InnoDBDialect;
24
25 import org.hibernate.tool.hbm2ddl.SchemaExport;
26
27 import org.springframework.orm.hibernate3.HibernateTemplate;
28
29 import org.wamblee.general.BeanKernel;
30
31 import org.wamblee.security.authorization.AuthorizationService;
32 import org.wamblee.security.authorization.AuthorizationServiceTest;
33 import org.wamblee.security.authorization.TestUserAccessor;
34
35 import org.wamblee.system.adapters.ClassConfiguration;
36 import org.wamblee.system.adapters.ClassConfigurationTest;
37 import org.wamblee.system.adapters.DefaultContainer;
38 import org.wamblee.system.adapters.ObjectConfiguration;
39 import org.wamblee.system.components.DatabaseComponentFactory;
40 import org.wamblee.system.core.Scope;
41 import org.wamblee.system.spring.component.DatabaseTesterComponent;
42 import org.wamblee.system.spring.component.DatasourceComponent;
43
44 import org.wamblee.usermgt.UserAccessor;
45 import org.wamblee.usermgt.hibernate.AuthorizationComponent;
46 import org.wamblee.usermgt.hibernate.HibernateUserAdministrationTest;
47 import org.wamblee.usermgt.hibernate.UserAdministrationComponent;
48
49 import java.sql.SQLException;
50
51 /**
52  * Unit test for the persistent authorization service.
53  * 
54  * @author Erik Brakkee
55  */
56 public class PersistentAuthorizationServiceTest extends
57     AuthorizationServiceTest {
58     private static final Logger LOGGER = Logger
59         .getLogger(PersistentAuthorizationServiceTest.class);
60
61     private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
62
63     private static final String RULES_TABLE = "AUTHORIZATION_RULES";
64
65     private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
66
67     private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
68
69     private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
70
71     private static final String USERCOND_TABLE = "USER_CONDITIONS";
72
73     private DefaultContainer container;
74
75     private Scope scope;
76
77     private DatabaseTesterComponent databaseTester;
78
79     private UserAccessor userAccessor;
80
81     private HibernateTemplate hibernateTemplate;
82
83     private AuthorizationService authorizationService;
84
85     @Override
86     protected void setUp() throws Exception {
87         container = new DefaultContainer("top");
88         DatabaseComponentFactory.addDatabaseConfig(container);
89         container.addComponent(new DatasourceComponent("datasource"));
90
91         ClassConfiguration useraccessorConfig = new ClassConfiguration(
92             TestUserAccessor.class);
93         useraccessorConfig.getObjectConfig().getSetterConfig().initAllSetters();
94         container.addComponent("userAccessor", useraccessorConfig);
95         container
96             .addComponent(new AuthorizationComponent("authorization", true));
97
98         ClassConfiguration dbtesterConfig = new ClassConfiguration(
99             DatabaseTesterComponent.class);
100         dbtesterConfig.getObjectConfig().getSetterConfig().initAllSetters();
101         container.addComponent("databaseTester", dbtesterConfig);
102
103         ObjectConfiguration config = new ObjectConfiguration(
104             PersistentAuthorizationServiceTest.class);
105         config.getSetterConfig().clear().add("setUserAccessor").add(
106             "setDatabaseTester").add("setHibernateTemplate").add(
107             "setAuthorizationService");
108         container.addComponent("testcase", this, config);
109
110         scope = container.start();
111
112         databaseTester.cleanDatabase();
113
114         super.setUp();
115     }
116
117     public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
118         databaseTester = aDatabaseTester;
119     }
120
121     public void setUserAccessor(UserAccessor aUserAccessor) {
122         userAccessor = aUserAccessor;
123     }
124
125     public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
126         hibernateTemplate = aHibernateTemplate;
127     }
128
129     public void setAuthorizationService(
130         AuthorizationService aAuthorizationService) {
131         authorizationService = aAuthorizationService;
132     }
133
134     /*
135      * (non-Javadoc)
136      * 
137      * @see
138      * org.wamblee.security.authorization.AuthorizationServiceTest#createService
139      * ()
140      */
141     @Override
142     protected AuthorizationService createService() {
143         PersistentAuthorizationService service = new PersistentAuthorizationService(
144             "DEFAULT", hibernateTemplate, createUserAccessor(), 10000);
145
146         return service;
147     }
148
149     /*
150      * (non-Javadoc)
151      * 
152      * @see
153      * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
154      * (int)
155      */
156     @Override
157     protected void checkRuleCount(int aCount) {
158         try {
159             assertEquals(1, databaseTester.getTableSize(SERVICE_TABLE));
160             assertEquals(aCount, databaseTester.getTableSize(RULES_TABLE));
161             assertEquals(aCount, databaseTester
162                 .getTableSize(SERVICE_RULES_TABLE));
163             assertEquals(aCount, databaseTester.getTableSize(USERCOND_TABLE));
164             assertEquals(aCount, databaseTester.getTableSize(PATHCOND_TABLE));
165             assertEquals(aCount, databaseTester
166                 .getTableSize(OPERATIONCOND_TABLE));
167         } catch (SQLException e) {
168             throw new RuntimeException(e);
169         }
170     }
171
172     public void testSchemaExport() {
173         Configuration config = new Configuration();
174
175         for (String mappingFile : new AuthorizationMappingFiles()) {
176             config.addResource(mappingFile);
177         }
178
179         config.setProperty("hibernate.dialect", MySQL5InnoDBDialect.class
180             .getName());
181
182         SchemaExport exporter = new SchemaExport(config);
183         exporter.setOutputFile("target/mysql5.schema.sql");
184         exporter.create(true, false);
185     }
186
187     public void testPerformance() {
188         PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
189
190         int n = 1000;
191         long time = System.currentTimeMillis();
192
193         for (int i = 0; i < n; i++) {
194             testFirstRuleGrants();
195             resetTestRules();
196             testSecondRuleDenies();
197             resetTestRules();
198             testThirdRuleGrants();
199             resetTestRules();
200             testNoRulesSupportResource();
201         }
202
203         LOGGER.info("Executed " + (4 * n) + " authorization checks in " +
204             ((float) (System.currentTimeMillis() - time) / (float) 1000) +
205             " seconds.");
206     }
207 }