a6f0ae333d791a5f0a54641af5dc1626992fab0f
[utils] / security / jpatest / src / test / java / org / wamblee / security / authorization / jpa / JpaAuthorizationServiceTest.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.jpa;
17
18 import org.apache.log4j.Logger;
19 import org.wamblee.security.authentication.jpa.SecurityPersistenceUnit;
20 import org.wamblee.security.authorization.AuthorizationService;
21 import org.wamblee.security.authorization.AuthorizationServiceTest;
22 import org.wamblee.support.persistence.JpaTester;
23 import org.wamblee.support.persistence.TransactionProxyFactory;
24
25 /**
26  * Unit test for the persistent authorization service.
27  * 
28  * @author Erik Brakkee
29  */
30 public class JpaAuthorizationServiceTest extends AuthorizationServiceTest {
31     private static final Logger LOGGER = Logger
32         .getLogger(JpaAuthorizationServiceTest.class);
33
34     private static final String SERVICE_TABLE = "SEC_AUTH_SVC";
35
36     private static final String RULES_TABLE = "SEC_AUTH_RULE";
37
38     private static final String SERVICE_RULES_TABLE = "SEC_AUTH_SVC_RULE";
39
40     private static final String OPERATIONCOND_TABLE = "SEC_OPERATION_CONDITION";
41
42     private static final String PATHCOND_TABLE = "SEC_PATH_CONDITION";
43
44     private static final String USERCOND_TABLE = "SEC_USER_CONDITION";
45
46     private JpaTester jpaTester;
47
48     @Override
49     protected void setUp() throws Exception {
50         jpaTester = new JpaTester(new SecurityPersistenceUnit());
51         jpaTester.start();
52
53         super.setUp();
54     }
55
56     @Override
57     protected void tearDown() throws Exception {
58         jpaTester.stop();
59         super.tearDown();
60     }
61
62     /*
63      * (non-Javadoc)
64      * 
65      * @see
66      * org.wamblee.security.authorization.AuthorizationServiceTest#createService
67      * ()
68      */
69     @Override
70     protected AuthorizationService createService() {
71         TransactionProxyFactory<AuthorizationService> factory = new TransactionProxyFactory<AuthorizationService>(
72             jpaTester.getJpaBuilder(), AuthorizationService.class);
73         JpaAuthorizationService service = new JpaAuthorizationService(
74             "DEFAULT", factory.getTransactionScopedEntityManager(),
75             getUserAccessor(), getUserAdministration(), 10000);
76
77         return factory.getProxy(service);
78     }
79
80     /*
81      * (non-Javadoc)
82      * 
83      * @see
84      * org.wamblee.security.authorization.AuthorizationServiceTest#checkRuleCount
85      * (int)
86      */
87     @Override
88     protected void checkRuleCount(int aCount) {
89         try {
90             assertEquals(1, jpaTester.getDbUtils().getTableSize(SERVICE_TABLE));
91             assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
92                 RULES_TABLE));
93             assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
94                 SERVICE_RULES_TABLE));
95             assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
96                 USERCOND_TABLE));
97             assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
98                 PATHCOND_TABLE));
99             assertEquals(aCount, jpaTester.getDbUtils().getTableSize(
100                 OPERATIONCOND_TABLE));
101         } catch (Exception e) {
102             throw new RuntimeException(e);
103         }
104     }
105
106     public void testPerformance() {
107         int n = 1000;
108         long time = System.currentTimeMillis();
109
110         for (int i = 0; i < n; i++) {
111             testFirstRuleGrants();
112             resetTestRules();
113             testSecondRuleDenies();
114             resetTestRules();
115             testThirdRuleGrants();
116             resetTestRules();
117             testNoRulesSupportResource();
118         }
119
120         LOGGER.info("Executed " + (4 * n) + " authorization checks in " +
121             ((float) (System.currentTimeMillis() - time) / (float) 1000) +
122             " seconds.");
123     }
124 }