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