KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sslexplorer > testcontainer > policyframework > AbstractTestResource


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.testcontainer.policyframework;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertFalse;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.assertNotSame;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 import java.util.ArrayList JavaDoc;
31 import java.util.Calendar JavaDoc;
32 import java.util.Collections JavaDoc;
33 import java.util.List JavaDoc;
34
35 import org.junit.After;
36 import org.junit.Before;
37 import org.junit.Test;
38
39 import com.sslexplorer.boot.PropertyList;
40 import com.sslexplorer.policyframework.AccessRight;
41 import com.sslexplorer.policyframework.AccessRights;
42 import com.sslexplorer.policyframework.DefaultAccessRights;
43 import com.sslexplorer.policyframework.Policy;
44 import com.sslexplorer.policyframework.PolicyConstants;
45 import com.sslexplorer.policyframework.PolicyDatabaseFactory;
46 import com.sslexplorer.policyframework.Resource;
47 import com.sslexplorer.policyframework.ResourceType;
48 import com.sslexplorer.realms.Realm;
49 import com.sslexplorer.security.AuthenticationScheme;
50 import com.sslexplorer.security.SystemDatabaseFactory;
51 import com.sslexplorer.testcontainer.AbstractTest;
52
53 /**
54  * @param <T>
55  */

56 public abstract class AbstractTestResource<T extends Resource> extends AbstractTest {
57
58     /**
59      * @throws Exception
60      */

61     @Before
62     @After
63     public void initialize() throws Exception JavaDoc {
64         deleteMultipleResources(getAllResources());
65     }
66     
67     /**
68      * @throws Exception
69      */

70     @Test
71     public void attachAndDetachResourceFromPolicy() throws Exception JavaDoc {
72         Realm realm = getDefaultRealm();
73         Policy policy = createPolicy(realm);
74         T resource = createResource();
75         PropertyList selectedPolicies = PropertyList.createFromArray(new int[] {policy.getResourceId()});
76         PolicyDatabaseFactory.getInstance().attachResourceToPolicyList(resource, selectedPolicies, getSessionInfo());
77         assertTrue("The resource should be attached to the policy", getPolicyService().isResourceAttachedToPolicy(resource, policy, realm));
78         assertTrue("The resource type must match the resource type of the type created.", getResourceType().equals(resource.getResourceType()));
79         
80         PolicyDatabaseFactory.getInstance().detachResourceFromPolicyList(resource, getSessionInfo());
81         assertFalse("The resource should not be attached to the policy", getPolicyService().isResourceAttachedToPolicy(resource, policy, realm));
82         T deletedResource = deleteResource(resource);
83         T checkWF = getResource(resource);
84         assertNotSame("The resource should have been deleted", deletedResource, checkWF);
85         getPolicyService().deletePolicy(policy.getResourceId());
86     }
87
88     /**
89      * @throws Exception
90      */

91     @Test
92     public void createAndDeleteAccessRights() throws Exception JavaDoc {
93         Realm realm = getDefaultRealm();
94         assertEquals("There should be one access rights.", getPolicyService().getAccessRights().size(), getDefaultAccessRightCount());
95         AccessRights accessRights = createAllAccessRights(getResourceType().getPermissionClass());
96         assertEquals("There should be two access rights.", getPolicyService().getAccessRights().size(), getDefaultAccessRightCount() + 1);
97         AccessRights accessRights2 = getPolicyService().getAccessRight(accessRights.getResourceId());
98         assertEquals("The acces rights should be the same", accessRights, accessRights2);
99         AccessRights accessRights3 = getPolicyService().getAccessRightsByName(accessRights.getResourceName(), realm.getRealmID());
100         assertEquals("The acces rights should be the same", accessRights, accessRights3);
101         getPolicyService().deleteAccessRights(accessRights.getResourceId());
102         assertEquals("There should be one access rights.", getPolicyService().getAccessRights().size(), getDefaultAccessRightCount());
103     }
104
105     /**
106      * @throws Exception
107      */

108     @Test
109     public void assignAndUnassignAccessRights() throws Exception JavaDoc {
110         Realm realm = getDefaultRealm();
111         Policy policy = createPolicy(realm);
112         AccessRights accessRights = createAllAccessRights(getResourceType().getPermissionClass());
113         
114         assertFalse("The resource should not be attached", getPolicyService().isResourceAttachedToPolicy(accessRights, policy, realm));
115         PropertyList selectedPolicies = PropertyList.createFromArray(new int[] {policy.getResourceId()});
116         PolicyDatabaseFactory.getInstance().attachResourceToPolicyList(accessRights, selectedPolicies, getSessionInfo());
117         assertTrue("The resource should be attached", getPolicyService().isResourceAttachedToPolicy(accessRights, policy, realm));
118         PolicyDatabaseFactory.getInstance().detachResourceFromPolicyList(accessRights, getSessionInfo());
119         assertFalse("The resource should not be attached", getPolicyService().isResourceAttachedToPolicy(accessRights, policy, realm));
120         
121         // other resources should still be attached.
122
List JavaDoc<AuthenticationScheme> authenticationSchemeSequences = SystemDatabaseFactory.getInstance().getAuthenticationSchemeSequences();
123         Policy everyone = getPolicyService().getPolicyByName("Everyone", realm.getResourceId());
124         for (AuthenticationScheme scheme : authenticationSchemeSequences) {
125             assertTrue("Auth scheme " + scheme.getResourceName() + " not assigned to everyone.", getPolicyService().isResourceAttachedToPolicy(scheme, everyone, realm));
126         }
127         AccessRights personal = getPolicyService().getAccessRightsByName("Global Permissions", realm.getRealmID());
128         assertTrue("Profile " + personal.getResourceName() + " not assigned to everyone.", getPolicyService().isResourceAttachedToPolicy(personal, everyone, realm));
129         
130         getPolicyService().deleteAccessRights(accessRights.getResourceId());
131         getPolicyService().deletePolicy(policy.getResourceId());
132     }
133     
134     /**
135      * @return T
136      * @throws Exception
137      */

138     public abstract T getNormalResource() throws Exception JavaDoc;
139     
140     /**
141      * @throws Exception
142      */

143     @Test
144     public void createNormalResource() throws Exception JavaDoc {
145         T resource = getNormalResource();
146         assertTrue("Should have none", getAllResources().isEmpty());
147         T createdResource = createResource(resource);
148         assertEquals("There should be only one", 1, getAllResources().size());
149         deleteResource(createdResource);
150         assertTrue("Should have none", getAllResources().isEmpty());
151     }
152
153     /**
154      * @return T
155      * @throws Exception
156      */

157     public abstract T getEmptyResource() throws Exception JavaDoc;
158     
159     /**
160      * @throws Exception
161      */

162     @Test
163     public void createEmptyResource() throws Exception JavaDoc {
164         T resource = getEmptyResource();
165         assertTrue("Should have none", getAllResources().isEmpty());
166         T createdResource = createResource(resource);
167         assertEquals("There should be only one", 1, getAllResources().size());
168         deleteResource(createdResource);
169         assertTrue("Should have none", getAllResources().isEmpty());
170     }
171
172     /**
173      * @return T
174      * @throws Exception
175      */

176     public abstract T getNullResource() throws Exception JavaDoc;
177     
178     /**
179      * @throws Exception
180      */

181     @Test
182     public void createNullResource() throws Exception JavaDoc {
183         T resource = getNullResource();
184         assertTrue("Should have none", getAllResources().isEmpty());
185         
186         try {
187             createResource(resource);
188             fail("This should have failed");
189         }
190         catch (Exception JavaDoc e) {
191             // ignore
192
}
193         assertTrue("Should have none", getAllResources().isEmpty());
194     }
195     
196     /**
197      * @throws Exception
198      */

199     @Test
200     public void createResourceWithSameName() throws Exception JavaDoc {
201         T resource = getNormalResource();
202         assertTrue("Should have none", getAllResources().isEmpty());
203         
204         T createdResourceOne = createResource(resource);
205         assertEquals("There should be only one", 1, getAllResources().size());
206         T createdResourceTwo = createResource(resource);
207         assertEquals("There should be two", 2, getAllResources().size());
208         
209         deleteResource(createdResourceOne);
210         deleteResource(createdResourceTwo);
211         assertTrue("Should have none", getAllResources().isEmpty());
212     }
213
214     /**
215      * @throws Exception
216      */

217     @Test
218     public void createTenResources() throws Exception JavaDoc {
219         T normalResource = getNormalResource();
220         assertTrue("Should have none", getAllResources().isEmpty());
221         List JavaDoc<T> resources = createMultipleResources(normalResource, 10);
222         assertEquals("There should be ten resources", 10, getAllResources().size());
223         deleteMultipleResources(resources);
224         assertTrue("Should have none", getAllResources().isEmpty());
225     }
226     
227     /**
228      * @throws Exception
229      */

230     @Test
231     public void updateUnknownResource() throws Exception JavaDoc {
232         T resource = getNormalResource();
233         assertTrue("Should have none", getAllResources().isEmpty());
234         T updatedResource = updateResource(resource);
235         assertNull(updatedResource);
236     }
237
238     /**
239      * @throws Exception
240      */

241     @Test
242     public void updateResourceName() throws Exception JavaDoc {
243         T normalResource = getNormalResource();
244         assertTrue("Should have none", getAllResources().isEmpty());
245         T createdResource = createResource(normalResource);
246         assertEquals("There should be only one", 1, getAllResources().size());
247         
248         final String JavaDoc newName = "thisIsMyNewName";
249         createdResource.setResourceName(newName);
250         T updatedResource = updateResource(createdResource);
251         assertEquals("Resource name should have been updated", newName, updatedResource.getResourceName());
252
253         deleteResource(createdResource);
254         assertTrue("Should have none", getAllResources().isEmpty());
255     }
256
257     /**
258      * @throws Exception
259      */

260     @Test
261     public void updateResourceDescription() throws Exception JavaDoc {
262         T normalResource = getNormalResource();
263         assertTrue("Should have none", getAllResources().isEmpty());
264         T createdResource = createResource(normalResource);
265         assertEquals("There should be only one", 1, getAllResources().size());
266         
267         final String JavaDoc newDescription = "thisIsMyNewDescription";
268         createdResource.setResourceDescription(newDescription);
269         T updatedResource = updateResource(createdResource);
270         assertEquals("Resource description should have been updated", newDescription, updatedResource.getResourceDescription());
271
272         deleteResource(createdResource);
273         assertTrue("Should have none", getAllResources().isEmpty());
274     }
275     
276     /**
277      * @throws Exception
278      */

279     @Test
280     public void deleteUnknownResource() throws Exception JavaDoc {
281         T resource = getNormalResource();
282         assertTrue("Should have none", getAllResources().isEmpty());
283         T createdResource = createResource(resource);
284         assertEquals("There should be only one", 1, getAllResources().size());
285         deleteResource(createdResource);
286         assertTrue("Should have none", getAllResources().isEmpty());
287     }
288     
289     /**
290      * @throws Exception
291      */

292     @Test
293     public void deleteResource() throws Exception JavaDoc {
294         T resource = getNormalResource();
295         assertTrue("Should have none", getAllResources().isEmpty());
296
297         try {
298             deleteResource(resource);
299             fail("This should have failed");
300         }
301         catch(Exception JavaDoc e) {
302             // ignore
303
}
304     }
305     
306     /**
307      * @throws Exception
308      */

309     @Test
310     public void deleteResourceTwice() throws Exception JavaDoc {
311         T resource = getNormalResource();
312         assertTrue("Should have none", getAllResources().isEmpty());
313         T createdResource = createResource(resource);
314         assertEquals("There should be only one", 1, getAllResources().size());
315         deleteResource(createdResource);
316         assertTrue("Should have none", getAllResources().isEmpty());
317         
318         try {
319             deleteResource(createdResource);
320             fail("This should have failed");
321         }
322         catch(Exception JavaDoc e) {
323             // ignore
324
}
325     }
326     
327     /**
328      * @throws Exception
329      */

330     @Test
331     public void getUnknownResource() throws Exception JavaDoc {
332         T normalResource = getNormalResource();
333         T foundResource = getResource(normalResource);
334         assertNull(foundResource);
335     }
336     
337     /**
338      * @throws Exception
339      */

340     @Test
341     public void getResource() throws Exception JavaDoc {
342         T normalResource = getNormalResource();
343         T createdResource = createResource(normalResource);
344         
345         T foundResource = getResource(createdResource);
346         assertNotNull(foundResource);
347         assertEquals("Found same Resource we created", createdResource, foundResource);
348         deleteResource(foundResource);
349     }
350     
351     protected static Policy createPolicy(Realm realm) throws Exception JavaDoc {
352         return createPolicy("Policy A", "Policy A description", Policy.TYPE_NORMAL, realm);
353     }
354     
355     protected static Policy createPolicy(String JavaDoc name, String JavaDoc description, int type, Realm realm) throws Exception JavaDoc {
356         return getPolicyService().createPolicy(name, description, type, realm.getRealmID());
357     }
358     
359     protected AccessRights createAllAccessRights(String JavaDoc delegationClass) throws Exception JavaDoc {
360         List JavaDoc<AccessRight> accessRights = new ArrayList JavaDoc<AccessRight>();
361         accessRights.add(new AccessRight(getResourceType(), PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN));
362         accessRights.add(new AccessRight(getResourceType(), PolicyConstants.PERM_DELETE));
363         return createAccessRights(delegationClass, accessRights);
364     }
365
366     protected AccessRights createCreateEditAssignAccessRights(String JavaDoc delegationClass) throws Exception JavaDoc {
367         List JavaDoc<AccessRight> accessRights = Collections.singletonList(new AccessRight(getResourceType(), PolicyConstants.PERM_CREATE_EDIT_AND_ASSIGN));
368         return createAccessRights(delegationClass, accessRights);
369     }
370     
371     protected AccessRights createEditAssignAccessRights(String JavaDoc delegationClass) throws Exception JavaDoc{
372         List JavaDoc<AccessRight> accessRights = Collections.singletonList(new AccessRight(getResourceType(), PolicyConstants.PERM_EDIT_AND_ASSIGN));
373         return createAccessRights(delegationClass, accessRights);
374     }
375     
376     protected AccessRights createAssignAccessRights(String JavaDoc delegationClass) throws Exception JavaDoc{
377         List JavaDoc<AccessRight> accessRights = Collections.singletonList(new AccessRight(getResourceType(), PolicyConstants.PERM_ASSIGN));
378         return createAccessRights(delegationClass, accessRights);
379     }
380     
381     protected AccessRights createDeleteAccessRights(String JavaDoc delegationClass) throws Exception JavaDoc{
382         List JavaDoc<AccessRight> accessRights = Collections.singletonList(new AccessRight(getResourceType(), PolicyConstants.PERM_DELETE));
383         return createAccessRights(delegationClass, accessRights);
384     }
385     
386     private static AccessRights createAccessRights(String JavaDoc delegationClass, List JavaDoc<AccessRight> accessRights) throws Exception JavaDoc {
387         Calendar JavaDoc calendar = Calendar.getInstance();
388         DefaultAccessRights defaultAccessRights = new DefaultAccessRights(getDefaultRealm().getRealmID(), 0, "Access1", "First set of access rights.", accessRights, delegationClass, calendar, calendar);
389         return getPolicyService().createAccessRights(defaultAccessRights);
390     }
391
392     /**
393      * @param resource
394      * @return Resource
395      * @throws Exception
396      */

397     public abstract T updateResource(T resource) throws Exception JavaDoc;
398     
399     /**
400      * @param resource
401      * @return Resource
402      * @throws Exception
403      */

404     public abstract T deleteResource(T resource) throws Exception JavaDoc;
405     
406     /**
407      * @param resource
408      * @return Resource
409      * @throws Exception
410      */

411     public abstract T getResource(T resource) throws Exception JavaDoc;
412     
413     /**
414      * @param resource
415      * @return Resource
416      * @throws Exception
417      */

418     public abstract T createResource(T resource) throws Exception JavaDoc;
419     
420     /**
421      * @return Resource
422      * @throws Exception
423      */

424     public final T createResource() throws Exception JavaDoc {
425         return createResource(getNormalResource());
426     }
427    
428     /**
429      * @return Resource
430      * @throws Exception
431      */

432     public abstract ResourceType getResourceType() throws Exception JavaDoc;
433     
434     /**
435      * @return List<Resource>
436      * @throws Exception
437      */

438     public abstract List JavaDoc<T> getAllResources() throws Exception JavaDoc;
439     
440     
441     protected List JavaDoc<T> createMultipleResources(T resource, int number) throws Exception JavaDoc {
442         List JavaDoc<T> resources = new ArrayList JavaDoc<T>(number);
443         for(int i = 0; i < number; i++) {
444             resource.setResourceName("Resource name " + i);
445             resources.add(createResource(resource));
446         }
447         return resources;
448     }
449     
450     protected void deleteMultipleResources(List JavaDoc<T> resources) throws Exception JavaDoc {
451         for (T resource : resources) {
452             deleteResource(resource);
453         }
454     }
455     
456     /**
457      * @return int
458      */

459     public int getDefaultAccessRightCount() {
460         return 1;
461     }
462 }
Popular Tags