1 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 ; 31 import java.util.Calendar ; 32 import java.util.Collections ; 33 import java.util.List ; 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 56 public abstract class AbstractTestResource<T extends Resource> extends AbstractTest { 57 58 61 @Before 62 @After 63 public void initialize() throws Exception { 64 deleteMultipleResources(getAllResources()); 65 } 66 67 70 @Test 71 public void attachAndDetachResourceFromPolicy() throws Exception { 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 91 @Test 92 public void createAndDeleteAccessRights() throws Exception { 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 108 @Test 109 public void assignAndUnassignAccessRights() throws Exception { 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 List <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 138 public abstract T getNormalResource() throws Exception ; 139 140 143 @Test 144 public void createNormalResource() throws Exception { 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 157 public abstract T getEmptyResource() throws Exception ; 158 159 162 @Test 163 public void createEmptyResource() throws Exception { 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 176 public abstract T getNullResource() throws Exception ; 177 178 181 @Test 182 public void createNullResource() throws Exception { 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 e) { 191 } 193 assertTrue("Should have none", getAllResources().isEmpty()); 194 } 195 196 199 @Test 200 public void createResourceWithSameName() throws Exception { 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 217 @Test 218 public void createTenResources() throws Exception { 219 T normalResource = getNormalResource(); 220 assertTrue("Should have none", getAllResources().isEmpty()); 221 List <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 230 @Test 231 public void updateUnknownResource() throws Exception { 232 T resource = getNormalResource(); 233 assertTrue("Should have none", getAllResources().isEmpty()); 234 T updatedResource = updateResource(resource); 235 assertNull(updatedResource); 236 } 237 238 241 @Test 242 public void updateResourceName() throws Exception { 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 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 260 @Test 261 public void updateResourceDescription() throws Exception { 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 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 279 @Test 280 public void deleteUnknownResource() throws Exception { 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 292 @Test 293 public void deleteResource() throws Exception { 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 e) { 302 } 304 } 305 306 309 @Test 310 public void deleteResourceTwice() throws Exception { 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 e) { 323 } 325 } 326 327 330 @Test 331 public void getUnknownResource() throws Exception { 332 T normalResource = getNormalResource(); 333 T foundResource = getResource(normalResource); 334 assertNull(foundResource); 335 } 336 337 340 @Test 341 public void getResource() throws Exception { 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 { 352 return createPolicy("Policy A", "Policy A description", Policy.TYPE_NORMAL, realm); 353 } 354 355 protected static Policy createPolicy(String name, String description, int type, Realm realm) throws Exception { 356 return getPolicyService().createPolicy(name, description, type, realm.getRealmID()); 357 } 358 359 protected AccessRights createAllAccessRights(String delegationClass) throws Exception { 360 List <AccessRight> accessRights = new ArrayList <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 delegationClass) throws Exception { 367 List <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 delegationClass) throws Exception { 372 List <AccessRight> accessRights = Collections.singletonList(new AccessRight(getResourceType(), PolicyConstants.PERM_EDIT_AND_ASSIGN)); 373 return createAccessRights(delegationClass, accessRights); 374 } 375 376 protected AccessRights createAssignAccessRights(String delegationClass) throws Exception { 377 List <AccessRight> accessRights = Collections.singletonList(new AccessRight(getResourceType(), PolicyConstants.PERM_ASSIGN)); 378 return createAccessRights(delegationClass, accessRights); 379 } 380 381 protected AccessRights createDeleteAccessRights(String delegationClass) throws Exception { 382 List <AccessRight> accessRights = Collections.singletonList(new AccessRight(getResourceType(), PolicyConstants.PERM_DELETE)); 383 return createAccessRights(delegationClass, accessRights); 384 } 385 386 private static AccessRights createAccessRights(String delegationClass, List <AccessRight> accessRights) throws Exception { 387 Calendar 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 397 public abstract T updateResource(T resource) throws Exception ; 398 399 404 public abstract T deleteResource(T resource) throws Exception ; 405 406 411 public abstract T getResource(T resource) throws Exception ; 412 413 418 public abstract T createResource(T resource) throws Exception ; 419 420 424 public final T createResource() throws Exception { 425 return createResource(getNormalResource()); 426 } 427 428 432 public abstract ResourceType getResourceType() throws Exception ; 433 434 438 public abstract List <T> getAllResources() throws Exception ; 439 440 441 protected List <T> createMultipleResources(T resource, int number) throws Exception { 442 List <T> resources = new ArrayList <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 <T> resources) throws Exception { 451 for (T resource : resources) { 452 deleteResource(resource); 453 } 454 } 455 456 459 public int getDefaultAccessRightCount() { 460 return 1; 461 } 462 } | Popular Tags |