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