]> git.aero2k.de Git - tmp/jakarta-migration.git/blob
fdbef7311c2641d5d4b3869da8b3efc50725558c
[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             //create default role(s) for the tenant and assign permissions
52             authzGen.createDefaultPermissionsRoles();
53             String exportDir = getExportDir();
54             authzGen.exportPermissions(exportDir + PERMISSION_FILE);
55             authzGen.exportPermissionRoles(exportDir + PERMISSION_ROLE_FILE);
56             if (logger.isDebugEnabled()) {
57                 logger.debug("authroization generation completed ");
58             }
59             status = beginTransaction("seedData");
60             AuthorizationSeed authzSeed = new AuthorizationSeed();
61             String importDir = getImportDir();
62             authzSeed.seedPermissions(importDir + PERMISSION_FILE,
63                     importDir + PERMISSION_ROLE_FILE);
64             if (logger.isDebugEnabled()) {
65                 logger.debug("authroization seeding completed ");
66             }
67         } catch (Exception ex) {
68             if (status != null) {
69                 rollbackTransaction(status);
70             }
71             if (logger.isDebugEnabled()) {
72                 ex.printStackTrace();
73             }
74             throw new RuntimeException(ex);
75         } finally {
76             if (status != null) {
77                 commitTransaction(status);
78             }
79         }
80     }
81
82     private String getTenantBindingFile() {
83         String tenantBindingFile = System.getProperty("tenantbindings");
84         if (tenantBindingFile == null || tenantBindingFile.isEmpty()) {
85             throw new IllegalStateException("tenantbindings are required."
86                     + " System property tenantbindings is missing or empty");
87         }
88         return tenantBindingFile;
89     }
90
91     private String getImportDir() {
92         String importDir = System.getProperty("importdir");
93         if (importDir == null || importDir.isEmpty()) {
94             throw new IllegalStateException("importdir required."
95                     + " System property importdir is missing or empty");
96         }
97         return importDir + File.separator;
98     }
99
100     private String getExportDir() {
101         String exportDir = System.getProperty("exportdir");
102         if (exportDir == null || exportDir.isEmpty()) {
103             throw new IllegalStateException("exportdir required."
104                     + " System property exportdir is missing or empty");
105         }
106         return exportDir + File.separator;
107     }
108 }