(no commit message)
[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.springframework.orm.hibernate3.HibernateTemplate;
23 import org.wamblee.general.BeanKernel;
24 import org.wamblee.security.authorization.AuthorizationService;
25 import org.wamblee.security.authorization.AuthorizationServiceTest;
26 import org.wamblee.security.authorization.TestUserAccessor;
27 import org.wamblee.system.adapters.DefaultContainer;
28 import org.wamblee.system.adapters.ObjectConfiguration;
29 import org.wamblee.system.components.DatabaseComponentFactory;
30 import org.wamblee.system.core.Scope;
31 import org.wamblee.system.spring.component.DatabaseTesterComponent;
32 import org.wamblee.system.spring.component.DatasourceComponent;
33 import org.wamblee.usermgt.UserAccessor;
34 import org.wamblee.usermgt.hibernate.AuthorizationComponent;
35 import org.wamblee.usermgt.hibernate.HibernateUserAdministrationTest;
36 import org.wamblee.usermgt.hibernate.UserAdministrationComponent;
37
38 /**
39  * Unit test for the persistent authorization service.
40  * 
41  * @author Erik Brakkee
42  */
43 public class PersistentAuthorizationServiceTest extends
44         AuthorizationServiceTest {
45
46     private static final Logger LOGGER = Logger
47             .getLogger(PersistentAuthorizationServiceTest.class);
48
49     private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
50     private static final String RULES_TABLE = "AUTHORIZATION_RULES";
51     private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
52     private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
53     private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
54     private static final String USERCOND_TABLE = "USER_CONDITIONS";
55
56     private DefaultContainer _container;
57     private Scope _scope;
58
59     private DatabaseTesterComponent _databaseTester;
60     private UserAccessor _userAccessor;
61     private HibernateTemplate _hibernateTemplate;
62     private AuthorizationService _authorizationService;
63
64     @Override
65     protected void setUp() throws Exception {
66
67         _container = new DefaultContainer("top");
68         DatabaseComponentFactory.addDatabaseConfig(_container);
69         _container.addComponent(new DatasourceComponent("datasource"));
70         _container.addComponent("userAccessor", TestUserAccessor.class);
71         _container.addComponent(new AuthorizationComponent("authorization",
72                 true));
73
74         _container
75                 .addComponent("databaseTester", DatabaseTesterComponent.class);
76
77         ObjectConfiguration config = new ObjectConfiguration(
78                 PersistentAuthorizationServiceTest.class);
79         config.getSetterConfig().clear().add("userAccessor").add(
80                 "databaseTester").add("hibernateTemplate").add(
81                 "authorizationService");
82         _container.addComponent("testcase", this, config);
83
84         _scope = _container.start();
85
86         _databaseTester.cleanDatabase();
87
88         super.setUp();
89     }
90
91     public void setDatabaseTester(DatabaseTesterComponent aDatabaseTester) {
92         _databaseTester = aDatabaseTester;
93     }
94
95     public void setUserAccessor(UserAccessor aUserAccessor) {
96         _userAccessor = aUserAccessor;
97     }
98
99     public void setHibernateTemplate(HibernateTemplate aHibernateTemplate) {
100         _hibernateTemplate = aHibernateTemplate;
101     }
102     
103     public void setAuthorizationService(
104             AuthorizationService aAuthorizationService) {
105         _authorizationService = aAuthorizationService;
106     }
107
108     /*
109      * (non-Javadoc)
110      * 
111      * @see org.wamblee.security.authorization.AuthorizationServiceTest#createService()
112      */
113     @Override
114     protected AuthorizationService createService() {
115         PersistentAuthorizationService service = new PersistentAuthorizationService(
116                 "DEFAULT", _hibernateTemplate, createUserAccessor(), 10000);
117         return service;
118     }
119
120     /*
121      * (non-Javadoc)
122      * 
123      * @see org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount(int)
124      */
125     @Override
126     protected void checkRuleCount(int aCount) {
127         try {
128             assertEquals(1, _databaseTester.getTableSize(SERVICE_TABLE));
129             assertEquals(aCount, _databaseTester.getTableSize(RULES_TABLE));
130             assertEquals(aCount, _databaseTester.getTableSize(SERVICE_RULES_TABLE));
131             assertEquals(aCount, _databaseTester.getTableSize(USERCOND_TABLE));
132             assertEquals(aCount, _databaseTester.getTableSize(PATHCOND_TABLE));
133             assertEquals(aCount, _databaseTester.getTableSize(OPERATIONCOND_TABLE));
134         } catch (SQLException e) {
135             throw new RuntimeException(e);
136         }
137
138     }
139
140     public void testPerformance() {
141
142         PersistentAuthorizationService service = (PersistentAuthorizationService) getService();
143
144         int n = 1000;
145         long time = System.currentTimeMillis();
146         for (int i = 0; i < n; i++) {
147             testFirstRuleGrants();
148             resetTestRules();
149             testSecondRuleDenies();
150             resetTestRules();
151             testThirdRuleGrants();
152             resetTestRules();
153             testNoRulesSupportResource();
154         }
155         LOGGER.info("Executed " + 4 * n + " authorization checks in "
156                 + (float) (System.currentTimeMillis() - time) / (float) 1000
157                 + " seconds.");
158     }
159 }