]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
d1e45006af4922b209a75caca925b2fc22ef8f36
[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 package org.collectionspace.services.authorization.importer;
25
26 import java.io.File;
27 import org.slf4j.Logger;
28 import org.slf4j.LoggerFactory;
29 import org.springframework.transaction.TransactionStatus;
30 import org.testng.annotations.BeforeClass;
31
32 /**
33  *
34  * @author 
35  */
36 public class AuthorizationSeedTest extends AbstractAuthorizationTestImpl {
37
38     final Logger logger = LoggerFactory.getLogger(AuthorizationSeedTest.class);
39     final static String PERMISSION_FILE = "import-permissions.xml";
40     final static String PERMISSION_ROLE_FILE = "import-permissions-roles.xml";
41
42     @BeforeClass(alwaysRun = true)
43     public void seedData() {
44         setup();
45         TransactionStatus status = null;
46         try {
47             AuthorizationGen authzGen = new AuthorizationGen();
48             String tenantBindingFile = getTenantBindingFile();
49             authzGen.initialize(tenantBindingFile);
50             authzGen.createDefaultServicePermissions();
51             authzGen.createDefaultPermissionsRoles("ROLE_ADMINISTRATOR");
52             String exportDir = getExportDir();
53             authzGen.exportPermissions(exportDir + PERMISSION_FILE);
54             authzGen.exportPermissionRoles(exportDir + PERMISSION_ROLE_FILE);
55             if (logger.isDebugEnabled()) {
56                 logger.debug("authroization generation completed ");
57             }
58             status = beginTransaction("seedData");
59             AuthorizationSeed authzSeed = new AuthorizationSeed();
60             String importDir = getImportDir();
61             authzSeed.seedPermissions(importDir + PERMISSION_FILE,
62                     importDir + PERMISSION_ROLE_FILE);
63             if (logger.isDebugEnabled()) {
64                 logger.debug("authroization seeding completed ");
65             }
66         } catch (Exception ex) {
67             if (status != null) {
68                 rollbackTransaction(status);
69             }
70             if (logger.isDebugEnabled()) {
71                 ex.printStackTrace();
72             }
73             throw new RuntimeException(ex);
74         } finally {
75             if (status != null) {
76                 commitTransaction(status);
77             }
78         }
79     }
80
81     private String getTenantBindingFile() {
82         String tenantBindingFile = System.getProperty("tenantbindings");
83         if (tenantBindingFile == null || tenantBindingFile.isEmpty()) {
84             throw new IllegalStateException("tenantbindings are required."
85                     + " System property tenantbindings is missing or empty");
86         }
87         return tenantBindingFile;
88     }
89
90     private String getImportDir() {
91         String importDir = System.getProperty("importdir");
92         if (importDir == null || importDir.isEmpty()) {
93             throw new IllegalStateException("importdir required."
94                     + " System property importdir is missing or empty");
95         }
96         return importDir + File.separator;
97     }
98
99     private String getExportDir() {
100         String exportDir = System.getProperty("exportdir");
101         if (exportDir == null || exportDir.isEmpty()) {
102             throw new IllegalStateException("exportdir required."
103                     + " System property exportdir is missing or empty");
104         }
105         return exportDir + File.separator;
106     }
107 }