Search in sources :

Example 16 with Privilege

use of com.sun.identity.entitlement.Privilege in project OpenAM by OpenRock.

the class PolicyConditionUpgraderTest method isPolicyWithNotSubjectConditionUpgradable.

@Test(dataProvider = "isPolicyWithNotSubjectConditionUpgradableDataProvider")
public void isPolicyWithNotSubjectConditionUpgradable(Class<? extends EntitlementSubject> sub, boolean subInMap, boolean expectedResult) {
    //Given
    Privilege policy = mock(Privilege.class);
    NotSubject notSubject = mock(NotSubject.class);
    Set<EntitlementSubject> notSubjects = new HashSet<EntitlementSubject>();
    EntitlementSubject subject = mock(sub);
    notSubjects.add(subject);
    given(policy.getSubject()).willReturn(notSubject);
    given(notSubject.getESubjects()).willReturn(notSubjects);
    if (subject instanceof PolicySubject) {
        given(((PolicySubject) subject).getClassName()).willReturn("SUBJECT_CLASS_NAME");
    }
    given(conditionUpgradeMap.containsSubjectCondition("SUBJECT_CLASS_NAME")).willReturn(subInMap);
    //When
    boolean upgradable = conditionUpgrader.isPolicyUpgradable(policy);
    //Then
    assertThat(upgradable).isEqualTo(expectedResult);
}
Also used : EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) PolicySubject(com.sun.identity.entitlement.opensso.PolicySubject) Privilege(com.sun.identity.entitlement.Privilege) NotSubject(com.sun.identity.entitlement.NotSubject) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 17 with Privilege

use of com.sun.identity.entitlement.Privilege in project OpenAM by OpenRock.

the class PolicyConditionUpgraderTest method isPolicyWithOrSubjectConditionUpgradable.

@Test(dataProvider = "isPolicyWithOrSubjectConditionUpgradableDataProvider")
public void isPolicyWithOrSubjectConditionUpgradable(Class<? extends EntitlementSubject> sub1, boolean sub1InMap, Class<? extends EntitlementSubject> sub2, boolean sub2InMap, boolean expectedResult) {
    //Given
    Privilege policy = mock(Privilege.class);
    OrSubject orSubject = mock(OrSubject.class);
    Set<EntitlementSubject> orSubjects = new HashSet<EntitlementSubject>();
    EntitlementSubject subject1 = mock(sub1);
    EntitlementSubject subject2 = mock(sub2);
    orSubjects.add(subject1);
    orSubjects.add(subject2);
    given(policy.getSubject()).willReturn(orSubject);
    given(orSubject.getESubjects()).willReturn(orSubjects);
    if (subject1 instanceof PolicySubject) {
        given(((PolicySubject) subject1).getClassName()).willReturn("SUBJECT1_CLASS_NAME");
    }
    if (subject2 instanceof PolicySubject) {
        given(((PolicySubject) subject2).getClassName()).willReturn("SUBJECT2_CLASS_NAME");
    }
    given(conditionUpgradeMap.containsSubjectCondition("SUBJECT1_CLASS_NAME")).willReturn(sub1InMap);
    given(conditionUpgradeMap.containsSubjectCondition("SUBJECT2_CLASS_NAME")).willReturn(sub2InMap);
    //When
    boolean upgradable = conditionUpgrader.isPolicyUpgradable(policy);
    //Then
    assertThat(upgradable).isEqualTo(expectedResult);
}
Also used : EntitlementSubject(com.sun.identity.entitlement.EntitlementSubject) PolicySubject(com.sun.identity.entitlement.opensso.PolicySubject) Privilege(com.sun.identity.entitlement.Privilege) OrSubject(com.sun.identity.entitlement.OrSubject) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 18 with Privilege

use of com.sun.identity.entitlement.Privilege in project OpenAM by OpenRock.

the class PolicyConditionUpgraderTest method isPolicyWithOrEnvironmentConditionUpgradable.

@Test(dataProvider = "isPolicyWithOrEnvironmentConditionUpgradableDataProvider")
public void isPolicyWithOrEnvironmentConditionUpgradable(Class<? extends EntitlementCondition> con1, boolean con1InMap, Class<? extends EntitlementCondition> con2, boolean con2InMap, boolean expectedResult) {
    //Given
    Privilege policy = mock(Privilege.class);
    OrCondition orCondition = mock(OrCondition.class);
    Set<EntitlementCondition> orConditions = new HashSet<EntitlementCondition>();
    EntitlementCondition condition1 = mock(con1);
    EntitlementCondition condition2 = mock(con2);
    orConditions.add(condition1);
    orConditions.add(condition2);
    given(policy.getCondition()).willReturn(orCondition);
    given(orCondition.getEConditions()).willReturn(orConditions);
    if (condition1 instanceof PolicyCondition) {
        given(((PolicyCondition) condition1).getClassName()).willReturn("CONDITION1_CLASS_NAME");
    }
    if (condition2 instanceof PolicyCondition) {
        given(((PolicyCondition) condition2).getClassName()).willReturn("CONDITION2_CLASS_NAME");
    }
    given(conditionUpgradeMap.containsEnvironmentCondition("CONDITION1_CLASS_NAME")).willReturn(con1InMap);
    given(conditionUpgradeMap.containsEnvironmentCondition("CONDITION2_CLASS_NAME")).willReturn(con2InMap);
    //When
    boolean upgradable = conditionUpgrader.isPolicyUpgradable(policy);
    //Then
    assertThat(upgradable).isEqualTo(expectedResult);
}
Also used : EntitlementCondition(com.sun.identity.entitlement.EntitlementCondition) OrCondition(com.sun.identity.entitlement.OrCondition) PolicyCondition(com.sun.identity.entitlement.opensso.PolicyCondition) Privilege(com.sun.identity.entitlement.Privilege) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 19 with Privilege

use of com.sun.identity.entitlement.Privilege in project OpenAM by OpenRock.

the class PolicyConditionUpgraderTest method shouldMigratePolicyWithOrEnvironmentCondition.

@SuppressWarnings("unchecked")
@Test
public void shouldMigratePolicyWithOrEnvironmentCondition() throws EntitlementException, UpgradeException {
    //Given
    Privilege policy = mock(Privilege.class);
    OrCondition orCondition = mock(OrCondition.class);
    Set<EntitlementCondition> orConditions = new HashSet<EntitlementCondition>();
    PolicyCondition condition1 = mock(PolicyCondition.class);
    PolicyCondition condition2 = mock(PolicyCondition.class);
    orConditions.add(condition1);
    orConditions.add(condition2);
    EntitlementCondition migratedCondition1 = mock(EntitlementCondition.class);
    EntitlementCondition migratedCondition2 = mock(EntitlementCondition.class);
    given(policy.getCondition()).willReturn(orCondition);
    given(orCondition.getEConditions()).willReturn(orConditions);
    given(condition1.getClassName()).willReturn("CONDITION1_CLASS_NAME");
    given(condition2.getClassName()).willReturn("CONDITION2_CLASS_NAME");
    given(conditionUpgradeMap.migrateEnvironmentCondition(eq("CONDITION1_CLASS_NAME"), eq(condition1), Matchers.<MigrationReport>anyObject())).willReturn(migratedCondition1);
    given(conditionUpgradeMap.migrateEnvironmentCondition(eq("CONDITION2_CLASS_NAME"), eq(condition2), Matchers.<MigrationReport>anyObject())).willReturn(migratedCondition2);
    //When
    conditionUpgrader.dryRunPolicyUpgrade(policy);
    //Then
    ArgumentCaptor<Set> conditionsCaptor = ArgumentCaptor.forClass(Set.class);
    verify(orCondition).setEConditions(conditionsCaptor.capture());
    assertThat(conditionsCaptor.getValue()).hasSize(2).contains(migratedCondition1, migratedCondition2);
    verify(policy, never()).setSubject(Matchers.<EntitlementSubject>anyObject());
    verify(policy, never()).setCondition(Matchers.<EntitlementCondition>anyObject());
}
Also used : EntitlementCondition(com.sun.identity.entitlement.EntitlementCondition) HashSet(java.util.HashSet) Set(java.util.Set) OrCondition(com.sun.identity.entitlement.OrCondition) PolicyCondition(com.sun.identity.entitlement.opensso.PolicyCondition) Privilege(com.sun.identity.entitlement.Privilege) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 20 with Privilege

use of com.sun.identity.entitlement.Privilege in project OpenAM by OpenRock.

the class JsonPolicyParser method parsePrivilege.

private Privilege parsePrivilege(String providedName, JsonValue jsonValue) throws EntitlementException {
    try {
        // Note: this is a bit ugly as we re-serialise the JsonValue back into a JSON String to then parse it
        // again using Jackson. Unfortunately, that appears to be the easiest way as JsonValue does not support
        // data binding.
        JsonPolicy policy = MAPPER.readValue(jsonValue.toString(), JsonPolicy.class);
        Privilege privilege = policy.asPrivilege();
        if (isBlank(privilege.getName())) {
            privilege.setName(providedName);
        }
        if (isBlank(privilege.getName())) {
            throw new EntitlementException(EntitlementException.MISSING_PRIVILEGE_NAME);
        }
        // Validate the condition if present
        if (privilege.getCondition() != null) {
            privilege.getCondition().validate();
        }
        return privilege;
    } catch (UnrecognizedPropertyException ex) {
        throw new EntitlementException(EntitlementException.INVALID_VALUE, new Object[] { ex.getUnrecognizedPropertyName() });
    } catch (JsonMappingException ex) {
        throw new EntitlementException(EntitlementException.INVALID_JSON, ex, ex.getMessage());
    } catch (IOException e) {
        throw new EntitlementException(EntitlementException.UNABLE_TO_CREATE_POLICY, e);
    }
}
Also used : JsonPolicy(org.forgerock.openam.entitlement.rest.model.json.JsonPolicy) EntitlementException(com.sun.identity.entitlement.EntitlementException) JsonMappingException(com.fasterxml.jackson.databind.JsonMappingException) UnrecognizedPropertyException(com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException) IOException(java.io.IOException) Privilege(com.sun.identity.entitlement.Privilege)

Aggregations

Privilege (com.sun.identity.entitlement.Privilege)122 Test (org.testng.annotations.Test)76 JsonValue (org.forgerock.json.JsonValue)46 OpenSSOPrivilege (com.sun.identity.entitlement.opensso.OpenSSOPrivilege)39 HashSet (java.util.HashSet)30 ReferralPrivilege (com.sun.identity.entitlement.ReferralPrivilege)26 EntitlementException (com.sun.identity.entitlement.EntitlementException)23 IPrivilege (com.sun.identity.entitlement.IPrivilege)23 Entitlement (com.sun.identity.entitlement.Entitlement)19 EntitlementSubject (com.sun.identity.entitlement.EntitlementSubject)18 PrivilegeManager (com.sun.identity.entitlement.PrivilegeManager)17 HashMap (java.util.HashMap)15 EntitlementCondition (com.sun.identity.entitlement.EntitlementCondition)13 ApplicationPrivilege (com.sun.identity.entitlement.ApplicationPrivilege)11 PolicyCondition (com.sun.identity.entitlement.opensso.PolicyCondition)10 Set (java.util.Set)9 SSOToken (com.iplanet.sso.SSOToken)8 PolicySubject (com.sun.identity.entitlement.opensso.PolicySubject)8 Subject (javax.security.auth.Subject)8 ResourceResponse (org.forgerock.json.resource.ResourceResponse)8