Moved over some of the security stuff from Photos.
[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
27 /**
28  * Unit test for the persistent authorization service. 
29  */
30 public class PersistentAuthorizationServiceTest extends AuthorizationServiceTest {
31     
32     private static final Logger LOGGER = Logger.getLogger(PersistentAuthorizationServiceTest.class);
33    
34     private static final String SERVICE_TABLE = "AUTHORIZATION_SERVICE";
35     private static final String RULES_TABLE = "AUTHORIZATION_RULES"; 
36     private static final String SERVICE_RULES_TABLE = "AUTHORIZATION_SERVICE_RULES";
37     private static final String OPERATIONCOND_TABLE = "OPERATION_CONDITIONS";
38     private static final String PATHCOND_TABLE = "PATH_CONDITIONS";
39     private static final String USERCOND_TABLE = "USER_CONDITIONS";
40    
41
42     public PersistentAuthorizationServiceTest() { 
43         super(AuthorizationSpringConfigFiles.class, AuthorizationMappingFiles.class);
44     }
45     
46     /* (non-Javadoc)
47      * @see org.wamblee.security.authorization.AuthorizationServiceTest#createService()
48      */
49     @Override
50     protected AuthorizationService createService() {
51         PersistentAuthorizationService service = new PersistentAuthorizationService("DEFAULT", 
52                 BeanKernel.getBeanFactory().find(HibernateTemplate.class), createUserAccessor(), 10000);
53         return service; 
54     }
55     
56     /* (non-Javadoc)
57      * @see org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount(int)
58      */
59     @Override
60     protected void checkRuleCount(int aCount) {
61         try { 
62             assertEquals(1, getTableSize(SERVICE_TABLE)); 
63             assertEquals(aCount, getTableSize(RULES_TABLE));
64             assertEquals(aCount, getTableSize(SERVICE_RULES_TABLE));
65             assertEquals(aCount, getTableSize(USERCOND_TABLE));
66             assertEquals(aCount, getTableSize(PATHCOND_TABLE));
67             assertEquals(aCount, getTableSize(OPERATIONCOND_TABLE));
68         } catch (SQLException e) {  
69             throw new RuntimeException(e);
70         }
71        
72     }
73     
74     public void testPerformance() { 
75        
76         PersistentAuthorizationService service = (PersistentAuthorizationService)getService(); 
77         
78         int n = 1000; 
79         long time = System.currentTimeMillis();
80         for (int i = 0; i < n; i++) { 
81             testFirstRuleGrants(); 
82             resetTestRules();
83             testSecondRuleDenies();
84             resetTestRules();
85             testThirdRuleGrants();
86             resetTestRules(); 
87             testNoRulesSupportResource();
88         }
89         LOGGER.info("Executed " + 4*n + " authorization checks in " + (float)(System.currentTimeMillis()-time)/(float)1000 + " seconds.");
90     }
91 }