]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
06e9be7c77bf74ab132193fe3d06beef26a42f2d
[tmp/jakarta-migration.git] /
1 /**
2  *  This document is a part of the source code and related artifacts
3  *  for CollectionSpace, an open source collections management system
4  *  for museums and related institutions:
5
6  *  http://www.collectionspace.org
7  *  http://wiki.collectionspace.org
8
9  *  Copyright 2009 University of California at Berkeley
10
11  *  Licensed under the Educational Community License (ECL), Version 2.0.
12  *  You may not use this file except in compliance with this License.
13
14  *  You may obtain a copy of the ECL 2.0 License at
15
16  *  https://source.collectionspace.org/collection-space/LICENSE.txt
17
18  *  Unless required by applicable law or agreed to in writing, software
19  *  distributed under the License is distributed on an "AS IS" BASIS,
20  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  *  See the License for the specific language governing permissions and
22  *  limitations under the License.
23  *//**
24  *  This document is a part of the source code and related artifacts
25  *  for CollectionSpace, an open source collections management system
26  *  for museums and related institutions:
27
28  *  http://www.collectionspace.org
29  *  http://wiki.collectionspace.org
30
31  *  Copyright 2009 University of California at Berkeley
32
33  *  Licensed under the Educational Community License (ECL), Version 2.0.
34  *  You may not use this file except in compliance with this License.
35
36  *  You may obtain a copy of the ECL 2.0 License at
37
38  *  https://source.collectionspace.org/collection-space/LICENSE.txt
39
40  *  Unless required by applicable law or agreed to in writing, software
41  *  distributed under the License is distributed on an "AS IS" BASIS,
42  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
43  *  See the License for the specific language governing permissions and
44  *  limitations under the License.
45  */
46 /*
47  * To change this template, choose Tools | Templates
48  * and open the template in the editor.
49  */
50 package org.collectionspace.services.authorization.test;
51
52 import java.io.File;
53 import java.io.InputStream;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
56 import java.lang.reflect.Method;
57 import java.util.ArrayList;
58 import java.util.HashSet;
59 import javax.xml.bind.JAXBContext;
60 import javax.xml.bind.Marshaller;
61 import javax.xml.bind.Unmarshaller;
62 import org.collectionspace.services.authorization.ActionType;
63 import org.collectionspace.services.authorization.AuthZ;
64 import org.collectionspace.services.authorization.PermissionConfig;
65 import org.collectionspace.services.authorization.EffectType;
66 import org.collectionspace.services.authorization.PermissionConfigList;
67 import org.springframework.context.support.ClassPathXmlApplicationContext;
68 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
69 import org.springframework.security.core.Authentication;
70 import org.springframework.security.core.GrantedAuthority;
71 import org.springframework.security.core.authority.GrantedAuthorityImpl;
72 import org.springframework.security.core.context.SecurityContextHolder;
73 import org.springframework.transaction.TransactionDefinition;
74 import org.springframework.transaction.TransactionStatus;
75 import org.springframework.transaction.support.DefaultTransactionDefinition;
76 import org.testng.annotations.BeforeClass;
77 import org.testng.annotations.DataProvider;
78 import org.testng.annotations.Test;
79
80 /**
81  *
82  * @author 
83  */
84 public class AuthorizationSeedTest {
85
86     final Logger logger = LoggerFactory.getLogger(AuthorizationSeedTest.class);
87
88     /**
89      * Returns the name of the currently running test.
90      *
91      * Note: although the return type is listed as Object[][],
92      * this method instead returns a String.
93      *
94      * @param   m  The currently running test method.
95      *
96      * @return  The name of the currently running test method.
97      */
98     @DataProvider(name = "testName")
99     public static Object[][] testName(Method m) {
100         return new Object[][]{
101                     new Object[]{m.getName()}
102                 };
103     }
104
105     @BeforeClass(alwaysRun = true)
106     public void seedData() {
107         ClassPathXmlApplicationContext appContext = new ClassPathXmlApplicationContext(
108                 new String[]{"applicationContext-authorization-test.xml"});
109         GrantedAuthority gauth = new GrantedAuthorityImpl("ROLE_ADMINISTRATOR");
110         HashSet<GrantedAuthority> gauths = new HashSet<GrantedAuthority>();
111         gauths.add(gauth);
112         Authentication authRequest = new UsernamePasswordAuthenticationToken("test", "test", gauths);
113
114         SecurityContextHolder.getContext().setAuthentication(authRequest);
115         AuthZ authZ = AuthZ.get();
116
117         org.springframework.jdbc.datasource.DataSourceTransactionManager txManager =
118                 (org.springframework.jdbc.datasource.DataSourceTransactionManager) appContext.getBean("transactionManager");
119         DefaultTransactionDefinition def = new DefaultTransactionDefinition();
120         // explicitly setting the transaction name is something that can only be done programmatically
121         def.setName("seedData");
122         def.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
123
124         TransactionStatus status = txManager.getTransaction(def);
125         try {
126             seedRoles();
127             seedPermissions();
128         } catch (Exception ex) {
129             txManager.rollback(status);
130             ex.printStackTrace();
131             throw new RuntimeException(ex);
132         }
133         txManager.commit(status);
134
135     }
136
137     public void seedRoles() throws Exception {
138     }
139
140     public void seedPermissions() throws Exception {
141
142         PermissionConfigList pcList =
143                 (PermissionConfigList) fromFile(PermissionConfigList.class,
144                 "./test-data/test-permissions.xml");
145         AuthZ authZ = AuthZ.get();
146         for (PermissionConfig pc : pcList.getPermission()) {
147             if(logger.isDebugEnabled()) {
148                 logger.debug("adding permission for res=" + pc.getResourceName());
149             }
150             authZ.addPermissions(pc);
151         }
152     }
153
154     private void genPermissions() {
155         PermissionConfigList pcList = new PermissionConfigList();
156         ArrayList<PermissionConfig> apcList = new ArrayList<PermissionConfig>();
157         pcList.setPermission(apcList);
158         PermissionConfig pc = new PermissionConfig();
159         pc.setResourceName("accounts");
160         pc.setEffect(EffectType.PERMIT);
161         ArrayList<String> roles = new ArrayList<String>();
162         roles.add("ROLE_USERS");
163         roles.add("ROLE_ADMINISTRATOR");
164         pc.setRole(roles);
165         ArrayList<ActionType> actions = new ArrayList<ActionType>();
166         actions.add(ActionType.CREATE);
167         actions.add(ActionType.READ);
168         actions.add(ActionType.UPDATE);
169         actions.add(ActionType.DELETE);
170         pc.setAction(actions);
171         apcList.add(pc);
172         toFile(pcList, PermissionConfigList.class, "./target/test-permissions.xml");
173
174     }
175
176     private void toFile(Object o, Class jaxbClass, String fileName) {
177         File f = new File(fileName);
178         try {
179             JAXBContext jc = JAXBContext.newInstance(jaxbClass);
180             Marshaller m = jc.createMarshaller();
181             m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
182                     Boolean.TRUE);
183             m.marshal(o, f);
184         } catch (Exception e) {
185             e.printStackTrace();
186         }
187     }
188
189     private Object fromFile(Class jaxbClass, String fileName) throws Exception {
190         ClassLoader tccl = Thread.currentThread().getContextClassLoader();
191         InputStream is = tccl.getResourceAsStream(fileName);
192         JAXBContext context = JAXBContext.newInstance(jaxbClass);
193         Unmarshaller unmarshaller = context.createUnmarshaller();
194         //note: setting schema to null will turn validator off
195         unmarshaller.setSchema(null);
196         return jaxbClass.cast(unmarshaller.unmarshal(is));
197     }
198
199     @Test(dataProvider = "testName", dataProviderClass = AuthorizationSeedTest.class)
200     public void test(String testName) {
201         if (logger.isDebugEnabled()) {
202             logger.debug(testName);
203         }
204     }
205 }