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