Skip to content

Commit

Permalink
Backport some testing methods from 4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Mar 18, 2020
1 parent 9672c52 commit 9e7b425
Show file tree
Hide file tree
Showing 6 changed files with 370 additions and 9 deletions.
Expand Up @@ -8,13 +8,12 @@
package com.evolveum.midpoint.schema.statistics;

import com.evolveum.midpoint.xml.ns._public.common.common_3.SynchronizationInformationType;

import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;

import javax.xml.namespace.QName;

/**
* @author Pavol Mederly
*/
public class SynchronizationInformation {

/*
Expand Down Expand Up @@ -75,7 +74,18 @@ public void setCountUnmatched(int countUnmatched) {
this.countUnmatched = countUnmatched;
}

};
public static Record createProtected() {
Record record = new Record();
record.setCountProtected(1);
return record;
}

public static Record createNotApplicable() {
Record record = new Record();
record.setCountNotApplicableForTask(1);
return record;
}
}

private final Record stateBefore = new Record();
private final Record stateAfter = new Record();
Expand Down Expand Up @@ -184,8 +194,32 @@ protected void addToState(Record state, Record increment) {
state.countUnmatched += increment.countUnmatched;
}

public void recordSynchronizationOperationStart(String objectName, String objectDisplayName, QName objectType, String objectOid) {
// noop
public static String format(SynchronizationInformationType source) {
StringBuilder sb = new StringBuilder();
appendHeader(sb);
SynchronizationInformationType i = source != null ? source : new SynchronizationInformationType();
append(sb, "Unmatched", i.getCountUnmatched(), i.getCountUnmatchedAfter());
append(sb, "Unlinked", i.getCountUnlinked(), i.getCountUnlinkedAfter());
append(sb, "Linked", i.getCountLinked(), i.getCountLinkedAfter());
append(sb, "Deleted", i.getCountDeleted(), i.getCountDeletedAfter());
append(sb, "Disputed", i.getCountDisputed(), i.getCountDisputedAfter());
append(sb, "Protected", i.getCountProtected(), i.getCountProtectedAfter());
append(sb, "No sync policy", i.getCountNoSynchronizationPolicy(), i.getCountNoSynchronizationPolicyAfter());
append(sb, "Sync disabled", i.getCountSynchronizationDisabled(), i.getCountSynchronizationDisabledAfter());
append(sb, "Not applicable", i.getCountNotApplicableForTask(), i.getCountNotApplicableForTaskAfter());
return sb.toString();
}

private static void appendHeader(StringBuilder sb) {
sb.append(String.format("%20s%10s%10s%10s\n", "Kind", "Before", "After", "Delta"));
sb.append(StringUtils.repeat('-', 60)).append("\n");
}

private static void append(StringBuilder sb, String label, int before, int after) {
sb.append(String.format("%20s%10d%10d%10d\n", label, before, after, after-before));
}

private static float div(long duration, int count) {
return count != 0 ? (float) duration / count : 0;
}
}
Expand Up @@ -5884,6 +5884,15 @@ protected UserAsserter<Void> assertUser(String oid, String message) throws Objec
PrismObject<UserType> user = getUser(oid);
return assertUser(user, message);
}

protected TaskAsserter<Void> assertTask(Task task, String message) {
return assertTask(task.getUpdatedTaskObject().asObjectable(), message);
}

protected TaskAsserter<Void> assertTask(TaskType task, String message) {
return TaskAsserter.forTask(task.asPrismObject(), message);
}

protected RepoOpAsserter createRepoOpAsserter(PerformanceInformation performanceInformation, String details) {
return new RepoOpAsserter(performanceInformation, details);
}
Expand Down
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.test.asserter;

import static org.testng.AssertJUnit.assertEquals;

import com.evolveum.midpoint.schema.statistics.IterativeTaskInformation;
import com.evolveum.midpoint.xml.ns._public.common.common_3.IterativeTaskInformationType;

import com.evolveum.midpoint.test.IntegrationTestTools;

/**
* Asserter that checks iterative task information.
*/
public class IterativeTaskInfoAsserter<RA> extends AbstractAsserter<RA> {

private final IterativeTaskInformationType information;

IterativeTaskInfoAsserter(IterativeTaskInformationType information, RA returnAsserter, String details) {
super(returnAsserter, details);
this.information = information;
}

public IterativeTaskInfoAsserter<RA> assertTotalCounts(int success, int failure) {
assertEquals("Wrong value of total success counter", success, information.getTotalSuccessCount());
assertEquals("Wrong value of total failure counter", failure, information.getTotalFailureCount());
return this;
}

@Override
protected String desc() {
return getDetails();
}

public IterativeTaskInfoAsserter display() {
IntegrationTestTools.display(desc(), IterativeTaskInformation.format(information));
return this;
}
}
@@ -0,0 +1,122 @@
/*
* Copyright (c) 2020 Evolveum and contributors
*
* This work is dual-licensed under the Apache License 2.0
* and European Union Public License. See LICENSE file for details.
*/

package com.evolveum.midpoint.test.asserter;

import static com.evolveum.midpoint.xml.ns._public.common.common_3.SynchronizationInformationType.*;

import static org.testng.AssertJUnit.assertEquals;

import com.evolveum.midpoint.prism.path.ItemName;
import com.evolveum.midpoint.repo.api.perf.PerformanceInformation;
import com.evolveum.midpoint.schema.statistics.RepositoryPerformanceInformationUtil;
import com.evolveum.midpoint.schema.statistics.SynchronizationInformation;
import com.evolveum.midpoint.test.IntegrationTestTools;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SynchronizationInformationType;

import org.apache.commons.lang3.StringUtils;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
* Asserter that checks task synchronization information.
*/
public class SynchronizationInfoAsserter<RA> extends AbstractAsserter<RA> {

private final SynchronizationInformationType information;

SynchronizationInfoAsserter(SynchronizationInformationType information, RA returnAsserter, String details) {
super(returnAsserter, details);
this.information = information;
}

public SynchronizationInfoAsserter<RA> assertProtected(int expectedBefore, int expectedAfter) {
return assertCounter(F_COUNT_PROTECTED, expectedBefore, expectedAfter);
}

public SynchronizationInfoAsserter<RA> assertUnmatched(int expectedBefore, int expectedAfter) {
return assertCounter(F_COUNT_UNMATCHED, expectedBefore, expectedAfter);
}

public SynchronizationInfoAsserter<RA> assertUnlinked(int expectedBefore, int expectedAfter) {
return assertCounter(F_COUNT_UNLINKED, expectedBefore, expectedAfter);
}

public SynchronizationInfoAsserter<RA> assertLinked(int expectedBefore, int expectedAfter) {
return assertCounter(F_COUNT_LINKED, expectedBefore, expectedAfter);
}

public SynchronizationInfoAsserter<RA> assertDeleted(int expectedBefore, int expectedAfter) {
return assertCounter(F_COUNT_DELETED, expectedBefore, expectedAfter);
}

@SuppressWarnings("WeakerAccess")
public SynchronizationInfoAsserter<RA> assertCounter(ItemName name, int expectedBefore, int expectedAfter) {
assertCounterBefore(name, expectedBefore);
assertCounterAfter(name, expectedAfter);
return this;
}

private void assertCounterBefore(ItemName name, int expected) {
assertEquals("Wrong # of 'before' value for " + name, expected, get(name, false));
}

private void assertCounterAfter(ItemName name, int expected) {
assertEquals("Wrong # of 'after' value for " + name, expected, get(name, true));
}

private int get(ItemName counterName, boolean after) {
String getterName = "get" + StringUtils.capitalize(counterName.getLocalPart()) + (after ? "After" : "");
try {
Method getter = information.getClass().getMethod(getterName);
return (Integer) getter.invoke(information);
} catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
throw new AssertionError("Couldn't find/invoke getter " + getterName + ": " + e.getMessage(), e);
}
}

public SynchronizationInfoAsserter<RA> assertTotal(int expectedBefore, int expectedAfter) {
assertEquals("Wrong sum of 'before' values", expectedBefore, getAllBefore());
assertEquals("Wrong sum of 'after' values", expectedAfter, getAllAfter());
return this;
}

private int getAllBefore() {
return information.getCountProtected()
+ information.getCountNoSynchronizationPolicy()
+ information.getCountSynchronizationDisabled()
+ information.getCountNotApplicableForTask()
+ information.getCountDeleted()
+ information.getCountDisputed()
+ information.getCountLinked()
+ information.getCountUnlinked()
+ information.getCountUnmatched();
}

private int getAllAfter() {
return information.getCountProtectedAfter()
+ information.getCountNoSynchronizationPolicyAfter()
+ information.getCountSynchronizationDisabledAfter()
+ information.getCountNotApplicableForTaskAfter()
+ information.getCountDeletedAfter()
+ information.getCountDisputedAfter()
+ information.getCountLinkedAfter()
+ information.getCountUnlinkedAfter()
+ information.getCountUnmatchedAfter();
}

@Override
protected String desc() {
return getDetails();
}

public SynchronizationInfoAsserter display() {
IntegrationTestTools.display(desc(), SynchronizationInformation.format(information));
return this;
}
}

0 comments on commit 9e7b425

Please sign in to comment.