KickJava   Java API By Example, From Geeks To Geeks.

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


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
24 import org.junit.BeforeClass;
25 import org.junit.Test;
26
27 import com.sslexplorer.core.UserDatabaseManager;
28 import com.sslexplorer.policyframework.Policy;
29 import com.sslexplorer.realms.Realm;
30 import com.sslexplorer.testcontainer.AbstractTest;
31
32 /**
33  */

34 public class PoliciesTest extends AbstractTest {
35     
36     
37     /**
38      * @throws Exception
39      */

40     @BeforeClass
41     public static void oneTimeSetUp() throws Exception JavaDoc {
42         setUp("");
43     }
44
45     /**
46      * @throws Exception
47      */

48     @Test
49     public void createPolicy() throws Exception JavaDoc {
50         Realm realm = getUserService().getRealm(1);
51         assertEquals("There should be only one policy", getPolicyService().getPolicies().size(), 1);
52         Policy policy = getPolicyService().createPolicy("Policy A", "Policy A description", Policy.TYPE_NORMAL, realm.getRealmID());
53         assertEquals("There should be only two policies", getPolicyService().getPolicies().size(), 2);
54         getPolicyService().deletePolicy(policy.getResourceId());
55         assertEquals("There should be only one policy", getPolicyService().getPolicies().size(), 1);
56     }
57
58     /**
59      * @throws Exception
60      */

61     @Test
62     public void updatePolicyName() throws Exception JavaDoc {
63         String JavaDoc newPolicyName = "NewName";
64         Realm realm = getUserService().getRealm(1);
65         Policy policy = createPolicy(realm);
66         policy.setResourceName(newPolicyName);
67         getPolicyService().updatePolicy(policy);
68         Policy updatedPolicy = getPolicyService().getPolicy(policy.getResourceId());
69         assertEquals("The new policy name should be " + newPolicyName, newPolicyName, updatedPolicy.getResourceName());
70         getPolicyService().deletePolicy(policy.getResourceId());
71     }
72
73     /**
74      * @throws Exception
75      */

76     @Test
77     public void updatePolicyDescription() throws Exception JavaDoc {
78         String JavaDoc newPolicyDescription = "NewDescription";
79         Realm realm = getUserService().getRealm(1);
80         Policy policy = createPolicy(realm);
81         policy.setResourceDescription(newPolicyDescription);
82         getPolicyService().updatePolicy(policy);
83         Policy updatedPolicy = getPolicyService().getPolicy(policy.getResourceId());
84         assertEquals("The new policy description should be " + newPolicyDescription, newPolicyDescription, updatedPolicy.getResourceDescription());
85         getPolicyService().deletePolicy(policy.getResourceId());
86     }
87
88     /**
89      * @throws Exception
90      */

91     @Test
92     public void checkPolicyRetrieval() throws Exception JavaDoc {
93         Realm realm = getUserService().getRealm(1);
94         Policy policy = createPolicy(realm);
95         Policy retrievedById = getPolicyService().getPolicy(policy.getResourceId());
96         Policy retrievedByName = getPolicyService().getPolicyByName(policy.getResourceName(), realm.getResourceId());
97         assertEquals("The policies should be the same.", retrievedById, retrievedByName);
98         getPolicyService().deletePolicy(policy.getResourceId());
99     }
100     
101     /**
102      * @return UserDatabaseManager
103      * @throws Exception
104      */

105     public static UserDatabaseManager getUserService() throws Exception JavaDoc {
106         return UserDatabaseManager.getInstance();
107     }
108     
109     private static Policy createPolicy(Realm realm) throws Exception JavaDoc {
110         return createPolicy("Policy A", "Policy A description", Policy.TYPE_NORMAL, realm);
111     }
112     
113     private static Policy createPolicy(String JavaDoc name, String JavaDoc description, int type, Realm realm) throws Exception JavaDoc {
114         return getPolicyService().createPolicy(name, description, type, realm.getRealmID());
115     }
116 }
Popular Tags