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