Skip to content

Commit

Permalink
Full support for subtype (+tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Mar 14, 2018
1 parent e3c0895 commit 2834bc6
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 105 deletions.
Expand Up @@ -175,8 +175,11 @@ public static <O extends ObjectType> List<String> determineSubTypes(PrismObject<
if (object == null) {
return null;
}

// TODO: get subType (from ObjectType)

List<String> subtypes = object.asObjectable().getSubType();
if (!subtypes.isEmpty()) {
return subtypes;
}

if (object.canRepresent(UserType.class)) {
return (((UserType)object.asObjectable()).getEmployeeType());
Expand Down Expand Up @@ -205,34 +208,12 @@ public static <O extends ObjectType> boolean hasSubtype(PrismObject<O> object, S

public static <O extends ObjectType> void setSubtype(PrismObject<O> object, List<String> subtypes) {

// TODO: set subType (from ObjectType)

List<String> objSubtypes = null;
if (object.canRepresent(UserType.class)) {
objSubtypes = (((UserType)object.asObjectable()).getEmployeeType());
List<String> objSubtypes = object.asObjectable().getSubType();
if (!objSubtypes.isEmpty()) {
objSubtypes.clear();
}
if (object.canRepresent(OrgType.class)) {
objSubtypes = (((OrgType)object.asObjectable()).getOrgType());
if (subtypes != null) {
objSubtypes.addAll(subtypes);
}
if (object.canRepresent(RoleType.class)) {
if (subtypes == null || subtypes.isEmpty()) {
((RoleType)object.asObjectable()).setRoleType(null);
} else {
((RoleType)object.asObjectable()).setRoleType(subtypes.get(0));
}
return;
}
if (object.canRepresent(ServiceType.class)) {
objSubtypes = (((ServiceType)object.asObjectable()).getServiceType());
}
if (objSubtypes != null) {
if (!objSubtypes.isEmpty()) {
objSubtypes.clear();
}
if (subtypes != null) {
objSubtypes.addAll(subtypes);
}
}

}
}
Expand Up @@ -100,70 +100,6 @@ public static <O extends ObjectType> String getOperationUrlFromDelta(ObjectDelta
throw new IllegalArgumentException("Unknown delta type "+delta);
}

public static <O extends ObjectType> ObjectPolicyConfigurationType determineObjectPolicyConfiguration(PrismObject<O> object, SystemConfigurationType systemConfigurationType) throws ConfigurationException {
List<String> subTypes;
if (object.getOid() == null){
subTypes = new ArrayList<>();
} else {
subTypes = FocusTypeUtil.determineSubTypes(object);
}
return determineObjectPolicyConfiguration(object.getCompileTimeClass(), subTypes, systemConfigurationType);
}

public static <O extends ObjectType> ObjectPolicyConfigurationType determineObjectPolicyConfiguration(Class<O> objectClass, List<String> objectSubtypes, SystemConfigurationType systemConfigurationType) throws ConfigurationException {
ObjectPolicyConfigurationType applicablePolicyConfigurationType = null;
for (ObjectPolicyConfigurationType aPolicyConfigurationType: systemConfigurationType.getDefaultObjectPolicyConfiguration()) {
QName typeQName = aPolicyConfigurationType.getType();
if (typeQName == null) {
continue; // TODO implement correctly (using 'applicable policies' perhaps)
}
ObjectTypes objectType = ObjectTypes.getObjectTypeFromTypeQName(typeQName);
if (objectType == null) {
throw new ConfigurationException("Unknown type "+typeQName+" in default object policy definition in system configuration");
}
if (objectType.getClassDefinition() == objectClass) {
String aSubType = aPolicyConfigurationType.getSubtype();
if (aSubType == null) {
if (applicablePolicyConfigurationType == null) {
applicablePolicyConfigurationType = aPolicyConfigurationType;
}
} else if (objectSubtypes != null && objectSubtypes.contains(aSubType)) {
applicablePolicyConfigurationType = aPolicyConfigurationType;
}
}
}
if (applicablePolicyConfigurationType != null) {
return applicablePolicyConfigurationType;
}

// Deprecated
for (ObjectPolicyConfigurationType aPolicyConfigurationType: systemConfigurationType.getObjectTemplate()) {
QName typeQName = aPolicyConfigurationType.getType();
if (typeQName == null) {
continue;
}
ObjectTypes objectType = ObjectTypes.getObjectTypeFromTypeQName(typeQName);
if (objectType == null) {
throw new ConfigurationException("Unknown type "+typeQName+" in object template definition in system configuration");
}
if (objectType.getClassDefinition() == objectClass) {
return aPolicyConfigurationType;
}
}

// Deprecated method to specify user template. For compatibility only
if (objectClass == UserType.class) {
ObjectReferenceType templateRef = systemConfigurationType.getDefaultUserTemplateRef();
if (templateRef == null) {
return null;
}
ObjectPolicyConfigurationType policy = new ObjectPolicyConfigurationType();
policy.setObjectTemplateRef(templateRef.clone());
return policy;
}

return null;
}

// from the most to least appropriate
@NotNull
Expand Down
Expand Up @@ -392,7 +392,10 @@ private <F extends ObjectType> void loadFromSystemConfig(LensContext<F> context,
if (context.getFocusContext().getObjectPolicyConfigurationType() == null) {
ObjectPolicyConfigurationType policyConfigurationType =
ModelUtils.determineObjectPolicyConfiguration(object, systemConfigurationType);
LOGGER.trace("Selected policy configuration: {}", policyConfigurationType);
if (LOGGER.isTraceEnabled()) {
LOGGER.trace("Selected policy configuration from subtypes {}:\n{}",
FocusTypeUtil.determineSubTypes(object), policyConfigurationType==null?null:policyConfigurationType.asPrismContainerValue().debugDump(1));
}
context.getFocusContext().setObjectPolicyConfigurationType(policyConfigurationType);
}
}
Expand Down
Expand Up @@ -89,8 +89,8 @@ public class TestUserTemplate extends AbstractInitializedModelIntegrationTest {
private static final String ACCOUNT_STAN_USERNAME = "stan";
private static final String ACCOUNT_STAN_FULLNAME = "Stan the Salesman";

private static final String EMPLOYEE_TYPE_MAROONED = "marooned";
private static final String EMPLOYEE_TYPE_USELESS = "useless";
private static final String SUBTYPE_MAROONED = "marooned";
private static final String SUBTYPE_USELESS = "useless";

private static final int NUMBER_OF_IMPORTED_ROLES = 5;

Expand All @@ -110,8 +110,8 @@ public void initSystem(Task initTask, OperationResult initResult) throws Excepti
repoAddObjectFromFile(USER_TEMPLATE_MAROONED_FILE, initResult);
repoAddObjectFromFile(USER_TEMPLATE_USELESS_FILE, initResult);
setDefaultObjectTemplate(UserType.COMPLEX_TYPE, USER_TEMPLATE_COMPLEX_OID, initResult);
setDefaultObjectTemplate(UserType.COMPLEX_TYPE, EMPLOYEE_TYPE_MAROONED, USER_TEMPLATE_MAROONED_OID, initResult);
setDefaultObjectTemplate(UserType.COMPLEX_TYPE, EMPLOYEE_TYPE_USELESS, USER_TEMPLATE_USELESS_OID, initResult);
setDefaultObjectTemplate(UserType.COMPLEX_TYPE, SUBTYPE_MAROONED, USER_TEMPLATE_MAROONED_OID, initResult);
setDefaultObjectTemplate(UserType.COMPLEX_TYPE, SUBTYPE_USELESS, USER_TEMPLATE_USELESS_OID, initResult);
}

protected int getNumberOfRoles() {
Expand Down Expand Up @@ -141,8 +141,8 @@ public void test000Sanity() throws Exception {
assertNotNull("No object policy", defaultObjectPolicyConfiguration);
assertEquals("Wrong object policy size", 5, defaultObjectPolicyConfiguration.size()); // last two are conflict resolution rules
assertObjectTemplate(defaultObjectPolicyConfiguration, UserType.COMPLEX_TYPE, null, USER_TEMPLATE_COMPLEX_OID);
assertObjectTemplate(defaultObjectPolicyConfiguration, UserType.COMPLEX_TYPE, EMPLOYEE_TYPE_MAROONED, USER_TEMPLATE_MAROONED_OID);
assertObjectTemplate(defaultObjectPolicyConfiguration, UserType.COMPLEX_TYPE, EMPLOYEE_TYPE_USELESS, USER_TEMPLATE_USELESS_OID);
assertObjectTemplate(defaultObjectPolicyConfiguration, UserType.COMPLEX_TYPE, SUBTYPE_MAROONED, USER_TEMPLATE_MAROONED_OID);
assertObjectTemplate(defaultObjectPolicyConfiguration, UserType.COMPLEX_TYPE, SUBTYPE_USELESS, USER_TEMPLATE_USELESS_OID);

assertRoles(getNumberOfRoles());
}
Expand Down Expand Up @@ -1209,6 +1209,8 @@ public void test174ModifyUserGuybrushHonorificPrefixNone() throws Exception {

/**
* Setting employee type to marooned. This should cause switch to different user template.
* Almost same as test185ModifyUserGuybrushSubtypeMarooned.
* employeeType is deprecated, but it should still work for compatibility.
*/
@Test
public void test180ModifyUserGuybrushEmployeeTypeMarooned() throws Exception {
Expand All @@ -1221,11 +1223,12 @@ public void test180ModifyUserGuybrushEmployeeTypeMarooned() throws Exception {

PrismObject<UserType> userBefore = getUser(USER_GUYBRUSH_OID);
display("User before", userBefore);
assertEquals("Wrong costCenter", "G001", userBefore.asObjectable().getCostCenter());
assertAssignedNoRole(userBefore);

// WHEN
displayWhen(TEST_NAME);
modifyUserReplace(USER_GUYBRUSH_OID, UserType.F_EMPLOYEE_TYPE, task, result, EMPLOYEE_TYPE_MAROONED);
modifyUserReplace(USER_GUYBRUSH_OID, UserType.F_EMPLOYEE_TYPE, task, result, SUBTYPE_MAROONED);

// THEN
displayThen(TEST_NAME);
Expand All @@ -1239,9 +1242,12 @@ public void test180ModifyUserGuybrushEmployeeTypeMarooned() throws Exception {
assertAssignedNoRole(userAfter);
}

/**
* employeeType is deprecated, but it should still work for compatibility.
*/
@Test
public void test189ModifyUserGuybrushEmployeeTypeNone() throws Exception {
final String TEST_NAME = "test189ModifyUserGuybrushEmployeeTypeNone";
public void test184ModifyUserGuybrushEmployeeTypeNone() throws Exception {
final String TEST_NAME = "test184ModifyUserGuybrushEmployeeTypeNone";
displayTestTitle(TEST_NAME);

// GIVEN
Expand All @@ -1260,13 +1266,81 @@ public void test189ModifyUserGuybrushEmployeeTypeNone() throws Exception {
displayThen(TEST_NAME);
assertSuccess(result);

modifyUserReplace(USER_GUYBRUSH_OID, UserType.F_COST_CENTER, task, result, "S321");

PrismObject<UserType> userAfter = modelService.getObject(UserType.class, USER_GUYBRUSH_OID, null, task, result);
display("User after", userAfter);

assertEquals("Wrong costCenter", "S321", userAfter.asObjectable().getCostCenter());


assertAssignedNoRole(userAfter);
}

/**
* Setting subtype to marooned. This should cause switch to different user template.
*/
@Test
public void test185ModifyUserGuybrushSubtypeMarooned() throws Exception {
final String TEST_NAME = "test185ModifyUserGuybrushSubtypeMarooned";
displayTestTitle(TEST_NAME);

// GIVEN
Task task = createTask(TEST_NAME);
OperationResult result = task.getResult();

PrismObject<UserType> userBefore = getUser(USER_GUYBRUSH_OID);
display("User before", userBefore);
assertAssignedNoRole(userBefore);

// WHEN
displayWhen(TEST_NAME);
modifyUserReplace(USER_GUYBRUSH_OID, UserType.F_SUB_TYPE, task, result, SUBTYPE_MAROONED);

// THEN
displayThen(TEST_NAME);
assertSuccess(result);

PrismObject<UserType> userAfter = modelService.getObject(UserType.class, USER_GUYBRUSH_OID, null, task, result);
display("User after", userAfter);

assertEquals("Wrong costCenter", "NOCOST", userAfter.asObjectable().getCostCenter());

assertAssignedNoRole(userAfter);
}

@Test
public void test189ModifyUserGuybrushSubtypeNone() throws Exception {
final String TEST_NAME = "test189ModifyUserGuybrushSubtypeNone";
displayTestTitle(TEST_NAME);

// GIVEN
Task task = createTask(TEST_NAME);
OperationResult result = task.getResult();

PrismObject<UserType> userBefore = getUser(USER_GUYBRUSH_OID);
display("User before", userBefore);
assertAssignedNoRole(userBefore);

// WHEN
displayWhen(TEST_NAME);
modifyUserReplace(USER_GUYBRUSH_OID, UserType.F_SUB_TYPE, task, result);

// THEN
displayThen(TEST_NAME);
assertSuccess(result);

modifyUserReplace(USER_GUYBRUSH_OID, UserType.F_COST_CENTER, task, result, "S321");

PrismObject<UserType> userAfter = modelService.getObject(UserType.class, USER_GUYBRUSH_OID, null, task, result);
display("User after", userAfter);

assertEquals("Wrong costCenter", "S321", userAfter.asObjectable().getCostCenter());

assertAssignedNoRole(userAfter);
}



/**
* Assignment mapping with domain. Control: nothing should happen.
Expand Down Expand Up @@ -2705,7 +2779,7 @@ public void test960ReconcileUserJackWithTemplate() throws Exception {
}

/**
* Setting employee type to marooned. This should cause switch to different user template.
* Setting employee type to useless. This should cause switch to different user template.
*/
@Test
public void test970ModifyUserGuybrushEmployeeTypeUseless() throws Exception {
Expand All @@ -2722,7 +2796,7 @@ public void test970ModifyUserGuybrushEmployeeTypeUseless() throws Exception {

// WHEN
displayWhen(TEST_NAME);
modifyUserReplace(USER_GUYBRUSH_OID, UserType.F_EMPLOYEE_TYPE, task, result, EMPLOYEE_TYPE_USELESS);
modifyUserReplace(USER_GUYBRUSH_OID, UserType.F_EMPLOYEE_TYPE, task, result, SUBTYPE_USELESS);

// THEN
displayThen(TEST_NAME);
Expand Down

0 comments on commit 2834bc6

Please sign in to comment.