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);
}
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);
}
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);
}
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());
}
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);
}
}
Aggregations