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