KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > profiles > ProfilesTests


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.profiles;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertTrue;
24
25 import java.util.Calendar JavaDoc;
26 import java.util.List JavaDoc;
27
28 import org.junit.AfterClass;
29 import org.junit.BeforeClass;
30 import org.junit.Test;
31
32 import com.sslexplorer.boot.PropertyList;
33 import com.sslexplorer.core.CoreServlet;
34 import com.sslexplorer.core.UserDatabaseManager;
35 import com.sslexplorer.jdbc.JDBCPropertyDatabase;
36 import com.sslexplorer.policyframework.AccessRights;
37 import com.sslexplorer.policyframework.Policy;
38 import com.sslexplorer.policyframework.PolicyConstants;
39 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
40 import com.sslexplorer.policyframework.ResourceType;
41 import com.sslexplorer.properties.DefaultPropertyProfile;
42 import com.sslexplorer.properties.PropertyProfile;
43 import com.sslexplorer.realms.Realm;
44 import com.sslexplorer.security.User;
45 import com.sslexplorer.testcontainer.policyframework.AbstractTestPolicyEnabledResource;
46
47 /**
48  */

49 public class ProfilesTests extends AbstractTestPolicyEnabledResource<PropertyProfile> {
50
51     private static JDBCPropertyDatabase propertyDatabase;
52     
53     /**
54      * @throws Exception
55      */

56     @BeforeClass
57     public static void oneTimeSetUp() throws Exception JavaDoc {
58         setUp("");
59         propertyDatabase = new JDBCPropertyDatabase();
60         propertyDatabase.open(CoreServlet.getServlet());
61     }
62
63     /**
64      * @throws Exception
65      */

66     @AfterClass
67     public static void after() throws Exception JavaDoc {
68         propertyDatabase.close();
69     }
70     
71     @Override JavaDoc
72     public ResourceType getResourceType() throws Exception JavaDoc {
73       return PolicyConstants.PROFILE_RESOURCE_TYPE;
74     }
75
76     
77     @Override JavaDoc
78     public PropertyProfile getEmptyResource() throws Exception JavaDoc {
79         Calendar JavaDoc calendar = Calendar.getInstance();
80         return new DefaultPropertyProfile(-1, -1, "", "", "", calendar, calendar);
81     }
82
83     @Override JavaDoc
84     public PropertyProfile getNormalResource() throws Exception JavaDoc {
85         Calendar JavaDoc calendar = Calendar.getInstance();
86         return new DefaultPropertyProfile(getDefaultRealm().getRealmID(), -1, null, "resourceName", "resourceDescription", calendar, calendar);
87     }
88
89     @Override JavaDoc
90     public PropertyProfile getNullResource() throws Exception JavaDoc {
91         Calendar JavaDoc calendar = Calendar.getInstance();
92         return new DefaultPropertyProfile(-1, -1, null, null, null, calendar, calendar);
93     }
94
95     @SuppressWarnings JavaDoc("unchecked")
96     @Override JavaDoc
97     public List JavaDoc<PropertyProfile> getAllResources() throws Exception JavaDoc {
98         return propertyDatabase.getPropertyProfiles(null, true, UserDatabaseManager.getInstance().getDefaultRealmID());
99     }
100
101     @Override JavaDoc
102     public PropertyProfile createResource(PropertyProfile resource) throws Exception JavaDoc {
103         return propertyDatabase.createPropertyProfile(resource.getOwnerUsername(), resource.getResourceName(), resource.getResourceDescription(), 0, UserDatabaseManager.getInstance().getDefaultRealmID());
104     }
105
106     @Override JavaDoc
107     public PropertyProfile deleteResource(PropertyProfile resource) throws Exception JavaDoc {
108         return propertyDatabase.deletePropertyProfile(resource.getResourceId());
109     }
110
111     @Override JavaDoc
112     public PropertyProfile getResource(PropertyProfile resource) throws Exception JavaDoc {
113         return propertyDatabase.getPropertyProfile(resource.getResourceId());
114     }
115
116     @Override JavaDoc
117     public PropertyProfile updateResource(PropertyProfile resource) throws Exception JavaDoc {
118         propertyDatabase.updatePropertyProfile(resource.getResourceId(), resource.getResourceName(), resource.getResourceDescription());
119         return getResource(resource);
120     }
121     
122     @Test
123     public void checkResourceAccessRights() throws Exception JavaDoc {
124         Realm realm = getDefaultRealm();
125         Policy policy = createPolicy(realm);
126         User user = createAccount();
127         getPolicyService().grantPolicyToPrincipal(policy, user);
128         
129         AccessRights accessRights = createAssignAccessRights(getResourceType().getPermissionClass());
130         PropertyList selectedPolicies = PropertyList.createFromArray(new int[] {policy.getResourceId()});
131         PolicyDatabaseFactory.getInstance().attachResourceToPolicyList(accessRights, selectedPolicies, getSessionInfo());
132         PropertyProfile resource = createResource();
133         assertTrue("Should be, as one already exists.", getPolicyService().isPrincipalGrantedResourcesOfType(user, resource.getResourceType(), null));
134         assertEquals("Should be only one.", getPolicyService().getGrantedResourcesOfType(user, getResourceType()).size(), 1);
135         
136         PolicyDatabaseFactory.getInstance().attachResourceToPolicyList(resource, selectedPolicies, getSessionInfo());
137         assertEquals("Should be two.", getPolicyService().getGrantedResourcesOfType(user, getResourceType()).size(), 2);
138         
139         PolicyDatabaseFactory.getInstance().detachResourceFromPolicyList(resource, getSessionInfo());
140         assertEquals("Should be only one.", getPolicyService().getGrantedResourcesOfType(user, getResourceType()).size(), 1);
141         getPolicyService().revokePolicyFromPrincipal(policy, user);
142         deleteAccount(user);
143         
144         PolicyDatabaseFactory.getInstance().detachResourceFromPolicyList(accessRights, getSessionInfo());
145         deleteResource(resource);
146         getPolicyService().deleteAccessRights(accessRights.getResourceId());
147         getPolicyService().deletePolicy(policy.getResourceId());
148     }
149     
150     /**
151      * Test the creation of PropertyProfile with a no existing user, the PropertyProfile created is a general profile.
152      * @throws Exception
153      */

154     @Test
155     public void createProfileWithoutUser() throws Exception JavaDoc {
156         DefaultPropertyProfile newProfile = new DefaultPropertyProfile(getDefaultRealm().getRealmID(), -1, "test3", "profile", "A profile", Calendar.getInstance(), Calendar.getInstance());
157         assertEquals("There should not be any PropertyProfile", 0, propertyDatabase.getPropertyProfiles("test1", true, 1).size());
158         PropertyProfile createdProfile = createResource(newProfile);
159         assertEquals("There should be two PropertyProfile", 1, propertyDatabase.getPropertyProfiles("test3", true, 1).size());
160         deleteResource(createdProfile);
161         assertEquals("There should not be any PropertyProfile", 0, propertyDatabase.getPropertyProfiles("test1", true, 1).size());
162     }
163 }
Popular Tags