Skip to content

Commit

Permalink
Fixing getManagers lib function (+tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Feb 12, 2015
1 parent 38aed86 commit 57b127f
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 15 deletions.
Expand Up @@ -864,6 +864,8 @@ <T extends ObjectType> int countObjects(Class<T> type, ObjectQuery query)
Collection<UserType> getManagers(UserType user) throws SchemaException, ObjectNotFoundException;

Collection<UserType> getManagersByOrgType(UserType user, String orgType) throws SchemaException, ObjectNotFoundException;

Collection<UserType> getManagers(UserType user, String orgType, boolean allowSelf) throws SchemaException, ObjectNotFoundException;

UserType getUserByOid(String oid) throws ObjectNotFoundException, SchemaException;

Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2013 Evolveum
* Copyright (c) 2010-2015 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -158,26 +158,57 @@ public Collection<String> getManagersOidsExceptUser(UserType user) throws Schema

@Override
public Collection<UserType> getManagers(UserType user) throws SchemaException, ObjectNotFoundException {
Set<UserType> retval = new HashSet<UserType>();
Collection<String> orgOids = getOrgUnits(user);
for (String orgOid : orgOids) {
retval.addAll(getManagersOfOrg(orgOid));
}
return retval;
return getManagers(user, null, false);
}

@Override
public Collection<UserType> getManagersByOrgType(UserType user, String orgType) throws SchemaException, ObjectNotFoundException {
return getManagers(user, orgType, false);
}

@Override
public Collection<UserType> getManagers(UserType user, String orgType, boolean allowSelf) throws SchemaException, ObjectNotFoundException {
Set<UserType> retval = new HashSet<UserType>();
Collection<String> orgOids = getOrgUnits(user);
for (String orgOid : orgOids) {
if (orgType != null) {
OrgType org = getOrgByOid(orgOid);
if (!org.getOrgType().contains(orgType)) {
continue;
while (!orgOids.isEmpty()) {
LOGGER.trace("orgOids: {}",orgOids);
Collection<OrgType> thisLevelOrgs = new ArrayList<OrgType>();
for (String orgOid : orgOids) {
if (orgType != null) {
OrgType org = getOrgByOid(orgOid);
if (!org.getOrgType().contains(orgType)) {
continue;
} else {
thisLevelOrgs.add(org);
}
}
Collection<UserType> managersOfOrg = getManagersOfOrg(orgOid);
for (UserType managerOfOrg: managersOfOrg) {
if (allowSelf || !managerOfOrg.getOid().equals(user.getOid())) {
retval.add(managerOfOrg);
}
}
}
LOGGER.trace("retval: {}",retval);
if (!retval.isEmpty()) {
return retval;
}
Collection<String> nextLevelOids = new ArrayList<String>();
if (orgType == null) {
for (String orgOid : orgOids) {
OrgType org = getOrgByOid(orgOid);
thisLevelOrgs.add(org);
}
}
for (OrgType org: thisLevelOrgs) {
for (ObjectReferenceType parentOrgRef: org.getParentOrgRef()) {
if (!nextLevelOids.contains(parentOrgRef.getOid())) {
nextLevelOids.add(parentOrgRef.getOid());
}
}
}
retval.addAll(getManagersOfOrg(orgOid));
}
LOGGER.trace("nextLevelOids: {}",nextLevelOids);
orgOids = nextLevelOids;
}
return retval;
}
Expand Down
Expand Up @@ -302,6 +302,9 @@ public class AbstractConfiguredModelIntegrationTest extends AbstractModelIntegra
protected static final String ORG_PROJECT_ROOT_OID = "00000000-8888-6666-0000-200000000000";
protected static final String ORG_SAVE_ELAINE_OID = "00000000-8888-6666-0000-200000000001";

protected static final String ORG_TYPE_FUNCTIONAL = "functional";
protected static final String ORG_TYPE_PROJECT = "project";

protected static final String TASK_RECONCILE_DUMMY_FILENAME = COMMON_DIR + "/task-reconcile-dummy.xml";
protected static final String TASK_RECONCILE_DUMMY_OID = "10000000-0000-0000-5656-565600000004";

Expand Down
Expand Up @@ -32,6 +32,7 @@
import com.evolveum.midpoint.util.DebugUtil;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;

import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -48,7 +49,12 @@
import com.evolveum.midpoint.schema.util.ObjectQueryUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.test.util.TestUtil;
import com.evolveum.midpoint.util.exception.CommunicationException;
import com.evolveum.midpoint.util.exception.ConfigurationException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.util.exception.SchemaException;
import com.evolveum.midpoint.util.exception.SecurityViolationException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.OrgType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
Expand Down Expand Up @@ -675,6 +681,96 @@ public void test349DeleteJack() throws Exception {
}
}

/**
* Add new user Jack with an assignments as an manager and also a member of ministry of offense.
*/
@Test
public void test400AddJackAsMinistryOfOffenseManager() throws Exception {
final String TEST_NAME = "test400AddJackAsMinistryOfOffenseManager";
TestUtil.displayTestTile(this, TEST_NAME);

Task task = taskManager.createTaskInstance(TestOrgStruct.class.getName() + "." + TEST_NAME);
OperationResult result = task.getResult();

PrismObject<UserType> userJack = prismContext.parseObject(USER_JACK_FILE);

AssignmentType assignmentType = new AssignmentType();
ObjectReferenceType targetRef = new ObjectReferenceType();
targetRef.setOid(ORG_MINISTRY_OF_OFFENSE_OID);
targetRef.setType(OrgType.COMPLEX_TYPE);
assignmentType.setTargetRef(targetRef);
userJack.asObjectable().getAssignment().add(assignmentType);

assignmentType = new AssignmentType();
targetRef = new ObjectReferenceType();
targetRef.setOid(ORG_MINISTRY_OF_OFFENSE_OID);
targetRef.setType(OrgType.COMPLEX_TYPE);
targetRef.setRelation(SchemaConstants.ORG_MANAGER);
assignmentType.setTargetRef(targetRef);
userJack.asObjectable().getAssignment().add(assignmentType);

Collection<ObjectDelta<? extends ObjectType>> deltas = MiscSchemaUtil.createCollection(userJack.createAddDelta());
// WHEN
modelService.executeChanges(deltas, null, task, result);

// THEN
userJack = getUser(USER_JACK_OID);
display("User jack after", userJack);
assertUserAssignedOrgs(userJack, ORG_MINISTRY_OF_OFFENSE_OID);
assertUserHasOrgs(userJack, ORG_MINISTRY_OF_OFFENSE_OID, ORG_MINISTRY_OF_OFFENSE_OID);
assertAssignedOrg(userJack, ORG_MINISTRY_OF_OFFENSE_OID, SchemaConstants.ORG_MANAGER);
assertHasOrg(userJack, ORG_MINISTRY_OF_OFFENSE_OID, SchemaConstants.ORG_MANAGER);
assertAssignedOrg(userJack, ORG_MINISTRY_OF_OFFENSE_OID, null);
assertHasOrg(userJack, ORG_MINISTRY_OF_OFFENSE_OID, null);

assertManager(USER_JACK_OID, null, null, false);
assertManager(USER_JACK_OID, null, ORG_TYPE_FUNCTIONAL, false);
assertManager(USER_JACK_OID, null, ORG_TYPE_PROJECT, false);
assertManager(USER_JACK_OID, USER_JACK_OID, null, true);
assertManager(USER_JACK_OID, USER_JACK_OID, ORG_TYPE_FUNCTIONAL, true);
assertManager(USER_JACK_OID, null, ORG_TYPE_PROJECT, true);

// Postcondition
assertMonkeyIslandOrgSanity();
}

@Test
public void test410ElaineAssignGovernor() throws Exception {
final String TEST_NAME = "test410ElaineAssignGovernor";
TestUtil.displayTestTile(this, TEST_NAME);

Task task = taskManager.createTaskInstance(TestOrgStruct.class.getName() + "." + TEST_NAME);
OperationResult result = task.getResult();

// WHEN
assignOrg(USER_ELAINE_OID, ORG_GOVERNOR_OFFICE_OID, SchemaConstants.ORG_MANAGER, task, result);

// THEN
PrismObject<UserType> userElaine = getUser(USER_ELAINE_OID);
display("User jack after", userElaine);
assertUserAssignedOrgs(userElaine, ORG_GOVERNOR_OFFICE_OID);
assertUserHasOrgs(userElaine, ORG_GOVERNOR_OFFICE_OID);
assertAssignedOrg(userElaine, ORG_GOVERNOR_OFFICE_OID, SchemaConstants.ORG_MANAGER);
assertHasOrg(userElaine, ORG_GOVERNOR_OFFICE_OID, SchemaConstants.ORG_MANAGER);

assertManager(USER_JACK_OID, USER_ELAINE_OID, null, false);
assertManager(USER_JACK_OID, USER_ELAINE_OID, ORG_TYPE_FUNCTIONAL, false);
assertManager(USER_JACK_OID, null, ORG_TYPE_PROJECT, false);
assertManager(USER_JACK_OID, USER_JACK_OID, null, true);
assertManager(USER_JACK_OID, USER_JACK_OID, ORG_TYPE_FUNCTIONAL, true);
assertManager(USER_JACK_OID, null, ORG_TYPE_PROJECT, true);

assertManager(USER_ELAINE_OID, null, null, false);
assertManager(USER_ELAINE_OID, null, ORG_TYPE_FUNCTIONAL, false);
assertManager(USER_ELAINE_OID, null, ORG_TYPE_PROJECT, false);
assertManager(USER_ELAINE_OID, USER_ELAINE_OID, null, true);
assertManager(USER_ELAINE_OID, USER_ELAINE_OID, ORG_TYPE_FUNCTIONAL, true);
assertManager(USER_ELAINE_OID, null, ORG_TYPE_PROJECT, true);

// Postcondition
assertMonkeyIslandOrgSanity();
}

protected void assertUserOrg(PrismObject<UserType> user, String... orgOids) throws Exception {
for (String orgOid: orgOids) {
assertAssignedOrg(user, orgOid);
Expand Down Expand Up @@ -704,4 +800,29 @@ protected void assertUserNoOrg(PrismObject<UserType> user) throws Exception {

}

private void assertManager(String userOid, String managerOid, String orgType, boolean allowSelf) throws ObjectNotFoundException, SchemaException, SecurityViolationException, CommunicationException, ConfigurationException {
PrismObject<UserType> user = getUser(userOid);
Collection<UserType> managers = libraryMidpointFunctions.getManagers(user.asObjectable(), orgType, allowSelf);
if (managerOid == null) {
if (managers == null || managers.isEmpty()) {
return;
} else {
AssertJUnit.fail("Expected no manager for "+user+", but got "+managers);
}
} else {
if (managers == null) {
AssertJUnit.fail("Expected manager for "+user+", but got no manager");
} if (managers.size() != 1) {
AssertJUnit.fail("Expected one manager for "+user+", but got: "+managers);
} else {
UserType manager = managers.iterator().next();
if (manager.getOid().equals(managerOid)) {
return;
} else {
AssertJUnit.fail("Expected manager with OID "+managerOid+" for "+user+", but got "+manager);
}
}
}
}

}
3 changes: 2 additions & 1 deletion model/model-intest/src/test/resources/logback-test.xml
Expand Up @@ -70,8 +70,9 @@
<logger name="com.evolveum.midpoint.model.impl.sync.FocusValidityScannerTaskHandler" level="DEBUG" />
<logger name="com.evolveum.midpoint.model.impl.util.AbstractSearchIterativeTaskHandler" level="DEBUG" />
<logger name="com.evolveum.midpoint.model.impl.sync.SynchronizationService" level="DEBUG" />
<logger name="com.evolveum.midpoint.model.impl.controller.ModelController" level="TRACE" />
<logger name="com.evolveum.midpoint.model.impl.controller.ModelController" level="DEBUG" />
<logger name="com.evolveum.icf.dummy" level="DEBUG" />
<logger name="com.evolveum.midpoint.model.impl.expr" level="DEBUG" />

<logger name="com.evolveum.midpoint.repo" level="INFO" />

Expand Down
Expand Up @@ -29,6 +29,7 @@
import com.evolveum.midpoint.model.api.PolicyViolationException;
import com.evolveum.midpoint.model.api.context.ModelContext;
import com.evolveum.midpoint.model.api.context.ModelProjectionContext;
import com.evolveum.midpoint.model.api.expr.MidpointFunctions;
import com.evolveum.midpoint.model.api.hooks.HookRegistry;
import com.evolveum.midpoint.notifications.api.NotificationManager;
import com.evolveum.midpoint.notifications.api.transports.Message;
Expand Down Expand Up @@ -231,6 +232,9 @@ public abstract class AbstractModelIntegrationTest extends AbstractIntegrationTe
@Autowired(required=true)
private SecurityEnforcer securityEnforcer;

@Autowired(required=true)
protected MidpointFunctions libraryMidpointFunctions;

protected DummyAuditService dummyAuditService;

protected boolean verbose = false;
Expand Down

0 comments on commit 57b127f

Please sign in to comment.