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:
6 * http://www.collectionspace.org
7 * http://wiki.collectionspace.org
9 * Copyright 2009 University of California at Berkeley
11 * Licensed under the Educational Community License (ECL), Version 2.0.
12 * You may not use this file except in compliance with this License.
14 * You may obtain a copy of the ECL 2.0 License at
16 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
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:
28 * http://www.collectionspace.org
29 * http://wiki.collectionspace.org
31 * Copyright 2009 University of California at Berkeley
33 * Licensed under the Educational Community License (ECL), Version 2.0.
34 * You may not use this file except in compliance with this License.
36 * You may obtain a copy of the ECL 2.0 License at
38 * https://source.collectionspace.org/collection-space/LICENSE.txt
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.
47 * To change this template, choose Tools | Templates
48 * and open the template in the editor.
50 package org.collectionspace.services.authorization.test;
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;
84 public class AuthorizationSeedTest {
86 final Logger logger = LoggerFactory.getLogger(AuthorizationSeedTest.class);
89 * Returns the name of the currently running test.
91 * Note: although the return type is listed as Object[][],
92 * this method instead returns a String.
94 * @param m The currently running test method.
96 * @return The name of the currently running test method.
98 @DataProvider(name = "testName")
99 public static Object[][] testName(Method m) {
100 return new Object[][]{
101 new Object[]{m.getName()}
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>();
112 Authentication authRequest = new UsernamePasswordAuthenticationToken("test", "test", gauths);
114 SecurityContextHolder.getContext().setAuthentication(authRequest);
115 AuthZ authZ = AuthZ.get();
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);
124 TransactionStatus status = txManager.getTransaction(def);
128 } catch (Exception ex) {
129 txManager.rollback(status);
130 ex.printStackTrace();
131 throw new RuntimeException(ex);
133 txManager.commit(status);
137 public void seedRoles() throws Exception {
140 public void seedPermissions() throws Exception {
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());
150 authZ.addPermissions(pc);
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");
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);
172 toFile(pcList, PermissionConfigList.class, "./target/test-permissions.xml");
176 private void toFile(Object o, Class jaxbClass, String fileName) {
177 File f = new File(fileName);
179 JAXBContext jc = JAXBContext.newInstance(jaxbClass);
180 Marshaller m = jc.createMarshaller();
181 m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
184 } catch (Exception e) {
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));
199 @Test(dataProvider = "testName", dataProviderClass = AuthorizationSeedTest.class)
200 public void test(String testName) {
201 if (logger.isDebugEnabled()) {
202 logger.debug(testName);