KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > policies > PolicyPrincipalAssignmentTest


1 /*
2  * SSL-Explorer
3  *
4  * Copyright (C) 2003-2006 3SP LTD. All Rights Reserved
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public
16  * License along with this program; if not, write to the Free Software
17  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18  */

19             
20 package com.sslexplorer.policies;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertTrue;
25
26 import java.util.Collections JavaDoc;
27
28 import org.junit.BeforeClass;
29 import org.junit.Test;
30
31 import com.sslexplorer.core.UserDatabaseManager;
32 import com.sslexplorer.policyframework.Policy;
33 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
34 import com.sslexplorer.realms.Realm;
35 import com.sslexplorer.security.Role;
36 import com.sslexplorer.security.User;
37 import com.sslexplorer.testcontainer.AbstractTest;
38
39 /**
40  */

41 public class PolicyPrincipalAssignmentTest extends AbstractTest {
42
43     /**
44      * @throws Exception
45      */

46     @BeforeClass
47     public static void oneTimeSetUp() throws Exception JavaDoc {
48         setUp("");
49     }
50
51     /**
52      * @throws Exception
53      */

54     @Test
55     public void attachDetachPolicyToUser() throws Exception JavaDoc {
56         Realm realm = getUserService().getDefaultRealm();
57         Policy policy = createPolicy(realm);
58         int users = getUserService().getDefaultUserDatabase().listAllUsers("*.*").length;
59         User user = createAccount();
60         assertEquals(getUserService().getDefaultUserDatabase().listAllUsers("*.*").length, users + 1);
61         getPolicyService().grantPolicyToPrincipal(policy, user);
62         assertTrue("The policy should be granted", PolicyDatabaseFactory.getInstance().isPolicyGrantedToUser(policy, user));
63         getPolicyService().revokePolicyFromPrincipal(policy, user);
64         assertFalse("The policy should not be granted", PolicyDatabaseFactory.getInstance().isPolicyGrantedToUser(policy, user));
65         getUserService().getDefaultUserDatabase().deleteAccount(user);
66         assertEquals(getUserService().getDefaultUserDatabase().listAllUsers("*.*").length, users);
67         getPolicyService().deletePolicy(policy.getResourceId());
68     }
69
70     /**
71      * @throws Exception
72      */

73     @Test
74     public void attachDetachPolicyToRole() throws Exception JavaDoc {
75         Realm realm = getUserService().getDefaultRealm();
76         Policy policy = createPolicy(realm);
77         User user = createAccount();
78         Role role = createRole("Group1");
79         user = updateAccountRoles(user, Collections.singleton(role));
80         getPolicyService().grantPolicyToPrincipal(policy, role);
81         assertTrue("The policy should be granted", PolicyDatabaseFactory.getInstance().isPolicyGrantedToUser(policy, user));
82         getPolicyService().revokePolicyFromPrincipal(policy, role);
83         assertFalse("The policy should not be granted", PolicyDatabaseFactory.getInstance().isPolicyGrantedToUser(policy, user));
84         getUserService().getDefaultUserDatabase().deleteAccount(user);
85         getPolicyService().deletePolicy(policy.getResourceId());
86     }
87
88     /**
89      * @return UserDatabaseManager
90      * @throws Exception
91      */

92     public static UserDatabaseManager getUserService() throws Exception JavaDoc {
93         return UserDatabaseManager.getInstance();
94     }
95
96     private static Policy createPolicy(Realm realm) throws Exception JavaDoc {
97         return createPolicy("Policy A", "Policy A description", Policy.TYPE_NORMAL, realm);
98     }
99
100     private static Policy createPolicy(String JavaDoc name, String JavaDoc description, int type, Realm realm) throws Exception JavaDoc {
101         return getPolicyService().createPolicy(name, description, type, realm.getRealmID());
102     }
103 }
Popular Tags