Skip to content

Commit

Permalink
Fix clustering and detection task tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tchrapovic committed Aug 30, 2023
1 parent cfe382d commit 6571275
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@

import static com.evolveum.midpoint.util.ClassPathUtil.LOGGER;

import java.util.ArrayList;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Set;
import java.util.*;
import java.util.stream.Collectors;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
Expand Down Expand Up @@ -70,10 +67,15 @@ public static DetectionOption loadDetectionOption(@NotNull RoleAnalysisDetection
public static List<String> getRolesOidAssignment(AssignmentHolderType object) {
List<String> oidList;
List<AssignmentType> assignments = object.getAssignment();
oidList = assignments.stream().map(AssignmentType::getTargetRef).filter(
targetRef -> targetRef.getType().equals(RoleType.COMPLEX_TYPE))
.map(AbstractReferencable::getOid).sorted()

oidList = assignments.stream()
.map(AssignmentType::getTargetRef)
.filter(Objects::nonNull)
.filter(targetRef -> targetRef.getType().equals(RoleType.COMPLEX_TYPE))
.map(AbstractReferencable::getOid)
.sorted()
.collect(Collectors.toList());

return oidList;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ public void execute(@NotNull ModelService modelService, String sessionOid,
PrismObject<RoleAnalysisSessionType> prismSession = getSessionTypeObject(modelService, result, sessionOid, task);
if (prismSession != null) {

deleteRoleAnalysisSessionClusters(modelService, result, prismSession.getOid(), task);

RoleAnalysisProcessModeType processMode = prismSession.asObjectable().getProcessMode();
if (processMode.equals(RoleAnalysisProcessModeType.USER)) {
this.clusterable = new UserBasedClustering();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.task.api.TaskManager;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleAnalysisClusterType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleAnalysisProcessModeType;
Expand Down Expand Up @@ -85,14 +84,14 @@ public void executeDetectionProcess() {
List<DetectedPattern> detectedPatterns = executeDetection(miningRoleTypeChunks, miningUserTypeChunks,
processMode, detectionOption);

if (detectedPatterns != null) {
if (detectedPatterns != null && !detectedPatterns.isEmpty()) {
detectedPatterns = loadTopPatterns(detectedPatterns);
replaceRoleAnalysisClusterDetectionPattern(clusterOid, modelService,
result,
detectedPatterns, task
);
}

replaceRoleAnalysisClusterDetectionPattern(clusterOid, modelService,
result,
detectedPatterns, task
);
}

private List<DetectedPattern> executeDetection(List<MiningRoleTypeChunk> miningRoleTypeChunks,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.query.ObjectFilter;
import com.evolveum.midpoint.prism.query.ObjectQuery;
import com.evolveum.midpoint.schema.ResultHandler;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.*;
Expand Down Expand Up @@ -216,4 +217,122 @@ public static void replaceRoleAnalysisClusterDetectionPattern(String clusterOid,
}
return objectReferenceList;
}

public static void deleteRoleAnalysisSessionClusters(ModelService modelService, OperationResult result, String sessionOid,
Task task) {

ResultHandler<RoleAnalysisClusterType> resultHandler = (object, parentResult) -> {
try {
deleteSingleRoleAnalysisCluster(result, object.asObjectable(), modelService, task);
} catch (Exception e) {
throw new RuntimeException(e);
}
return true;
};

ObjectQuery query = PrismContext.get().queryFor(RoleAnalysisClusterType.class)
.item(RoleAnalysisClusterType.F_ROLE_ANALYSIS_SESSION_REF).ref(sessionOid)
.build();

try {
modelService.searchObjectsIterative(RoleAnalysisClusterType.class, query, resultHandler, null,
task, result);
} catch (Exception ex) {
LoggingUtils.logExceptionOnDebugLevel(LOGGER, "Couldn't deleteRoleAnalysisSessionClusters", ex);
}

}

public static void deleteSingleRoleAnalysisCluster(OperationResult result,
@NotNull RoleAnalysisClusterType cluster, @NotNull ModelService modelService, Task task) {

String clusterOid = cluster.getOid();
PrismObject<RoleAnalysisSessionType> sessionObject = getSessionTypeObject(modelService, result,
cluster.getRoleAnalysisSessionRef().getOid(), task);

if (sessionObject == null) {
return;
}

try {

ObjectDelta<RoleAnalysisClusterType> deleteDelta = PrismContext.get().deltaFactory().object()
.createDeleteDelta(RoleAnalysisClusterType.class, clusterOid);

modelService.executeChanges(singleton(deleteDelta), null, task, result);

} catch (SchemaException | ObjectAlreadyExistsException | ObjectNotFoundException | ExpressionEvaluationException |
CommunicationException | ConfigurationException | PolicyViolationException | SecurityViolationException e) {
LOGGER.error("Couldn't delete RoleAnalysisClusterType {}", clusterOid, e);
}

try {

ObjectDelta<RoleAnalysisSessionType> delta = PrismContext.get().deltaFor(RoleAnalysisSessionType.class)
.item(RoleAnalysisSessionType.F_METADATA, F_MODIFY_TIMESTAMP).replace(getCurrentXMLGregorianCalendar())
.asObjectDelta(sessionObject.getOid());

modelService.executeChanges(singleton(delta), null, task, result);

recomputeSessionStatic(result, sessionObject.getOid(), cluster, modelService, task);

} catch (SchemaException | ObjectAlreadyExistsException | ObjectNotFoundException | ExpressionEvaluationException |
CommunicationException | ConfigurationException | PolicyViolationException | SecurityViolationException e) {
LOGGER.error("Couldn't recompute RoleAnalysisSessionStatistic {}", sessionObject.getOid(), e);
}

}

public static void recomputeSessionStatic(OperationResult result, String sessionOid,
@NotNull RoleAnalysisClusterType roleAnalysisClusterType, @NotNull ModelService modelService, Task task) {
PrismObject<RoleAnalysisSessionType> sessionTypeObject = getSessionTypeObject(modelService, result,
sessionOid, task);

assert sessionTypeObject != null;
RoleAnalysisSessionType session = sessionTypeObject.asObjectable();

int deletedClusterMembersCount = roleAnalysisClusterType.getMember().size();
AnalysisClusterStatisticType clusterStatistics = roleAnalysisClusterType.getClusterStatistics();
RoleAnalysisSessionStatisticType sessionStatistic = session.getSessionStatistic();

if (sessionStatistic == null || clusterStatistics == null) {
LOGGER.error("Couldn't recompute RoleAnalysisSessionStatistic {}. "
+ "Statistic container is null:{},{}", sessionOid, sessionStatistic, clusterStatistics);
return;
}
Double membershipDensity = clusterStatistics.getMembershipDensity();

Integer processedObjectCount = sessionStatistic.getProcessedObjectCount();
Double meanDensity = sessionStatistic.getMeanDensity();
Integer clusterCount = sessionStatistic.getClusterCount();

int newClusterCount = clusterCount - 1;

RoleAnalysisSessionStatisticType recomputeSessionStatistic = new RoleAnalysisSessionStatisticType();

if (newClusterCount == 0) {
recomputeSessionStatistic.setMeanDensity(0.0);
recomputeSessionStatistic.setProcessedObjectCount(0);
} else {
double recomputeMeanDensity = ((meanDensity * clusterCount) - (membershipDensity)) / newClusterCount;
int recomputeProcessedObjectCount = processedObjectCount - deletedClusterMembersCount;
recomputeSessionStatistic.setMeanDensity(recomputeMeanDensity);
recomputeSessionStatistic.setProcessedObjectCount(recomputeProcessedObjectCount);
}
recomputeSessionStatistic.setClusterCount(newClusterCount);

try {

ObjectDelta<RoleAnalysisSessionType> delta = PrismContext.get().deltaFor(RoleAnalysisSessionType.class)
.item(RoleAnalysisSessionType.F_SESSION_STATISTIC).replace(recomputeSessionStatistic.asPrismContainerValue())
.asObjectDelta(sessionOid);

modelService.executeChanges(singleton(delta), null, task, result);

} catch (SchemaException | ObjectAlreadyExistsException | ObjectNotFoundException | ExpressionEvaluationException |
CommunicationException | ConfigurationException | PolicyViolationException | SecurityViolationException e) {
LOGGER.error("Couldn't recompute RoleAnalysisSessionStatistic {}", sessionOid, e);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,6 @@
import java.io.File;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.schema.util.task.ActivityBasedTaskInformation;
import com.evolveum.midpoint.test.TestTask;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.CommonException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;

import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
Expand All @@ -30,14 +24,10 @@
import com.evolveum.midpoint.schema.util.task.TaskInformation;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.test.TestObject;
import com.evolveum.midpoint.test.TestResource;
import com.evolveum.midpoint.test.TestTask;
import com.evolveum.midpoint.util.QNameUtil;
import com.evolveum.midpoint.util.exception.CommonException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import com.evolveum.midpoint.test.TestObject;

import javax.xml.namespace.QName;

/**
* Tests miscellaneous kinds of tasks that do not deserve their own test class.
Expand Down Expand Up @@ -73,9 +63,9 @@ public class TestMiscTasks extends AbstractInitializedModelIntegrationTest {
private static final TestTask TASK_ROLE_ANALYSIS_PATTERN_DETECTION_BASIC =
new TestTask(TEST_DIR, "task-role-analysis-pattern-detection-basic.xml", "a3429097-52c4-4474-80b7-fc1667584831");

private static final TestObject<RoleType> SESSION_ROLE_BASED = TestObject.file(
private static final TestObject<RoleAnalysisSessionType> SESSION_ROLE_BASED = TestObject.file(
TEST_DIR, "session-role-based.xml", "c0d74ad2-f92a-40f0-b661-3c0a6a5dc225");
private static final TestObject<RoleType> CLUSTER_ROLE_BASED = TestObject.file(
private static final TestObject<RoleAnalysisClusterType> CLUSTER_ROLE_BASED = TestObject.file(
TEST_DIR, "cluster-role-based.xml", "16a8b95a-8a37-4f91-b835-bb77670c2899");
private static final TestObject<RoleType> ROLE_BUSINESS_1 = TestObject.file(
TEST_DIR, "role-business-1.xml", "b48628a2-a032-47c2-947d-adc51940e920");
Expand All @@ -93,7 +83,11 @@ public void initSystem(Task initTask, OperationResult initResult) throws Excepti
super.initSystem(initTask, initResult);
initTestObjects(initTask, initResult,
ROLE_APPLICATION_1, ROLE_APPLICATION_2, ROLE_BUSINESS_1,
USER_1, USER_2, SESSION_ROLE_BASED, CLUSTER_ROLE_BASED);
USER_1, USER_2);

if(isNativeRepository()){
initTestObjects(initTask, initResult, SESSION_ROLE_BASED, CLUSTER_ROLE_BASED);
}
}

/**
Expand Down Expand Up @@ -636,9 +630,10 @@ private void assertBusinessRoleAndNoApplicationRoles(String oid) throws CommonEx
// @formatter:on
}

/** Currently does nothing, as the activity handler has no implementation. */
@Test
public void test400RoleAnalysisClusteringBasic() throws CommonException {
public void test400RoleAnalysisClusteringBasic() throws Exception {
skipIfNotNativeRepository();

Task task = getTestTask();
OperationResult result = task.getResult();

Expand All @@ -652,9 +647,10 @@ public void test400RoleAnalysisClusteringBasic() throws CommonException {
.assertProgress(1);
}

/** Currently does nothing, as the activity handler has no implementation. */
@Test
public void test410RoleAnalysisPatternDetectionBasic() throws CommonException {
skipIfNotNativeRepository();

Task task = getTestTask();
OperationResult result = task.getResult();

Expand Down

0 comments on commit 6571275

Please sign in to comment.