Skip to content

Commit

Permalink
Add Attribute correlation phase to TestFirstSteps
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Dec 16, 2022
1 parent 9467555 commit 5712158
Show file tree
Hide file tree
Showing 7 changed files with 787 additions and 66 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public class MidPointTestConstants {
public static final ItemPath PATH_ENTRY_UUID = ItemPath.create(ShadowType.F_ATTRIBUTES, QNAME_ENTRY_UUID);
public static final ItemName QNAME_CAR_LICENSE = new ItemName(MidPointConstants.NS_RI, "carLicense");
public static final ItemName QNAME_EMPLOYEE_TYPE = new ItemName(MidPointConstants.NS_RI, "employeeType");
public static final ItemName QNAME_MAIL = new ItemName(NS_RI, "mail");
public static final ItemPath PATH_MAIL = ItemPath.create(ShadowType.F_ATTRIBUTES, QNAME_MAIL);

public static final String TEST_POLICY_SITUATION_LEGACY = qNameToUri(new QName(NS_MIDPOINT_TEST_PREFIX, "legacy"));
public static final String TEST_POLICY_SITUATION_ILLEGAL = qNameToUri(new QName(NS_MIDPOINT_TEST_PREFIX, "illegal"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;

import com.google.common.collect.Sets;
import org.jetbrains.annotations.NotNull;

import java.util.*;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -108,6 +109,7 @@ public ObjectDeltaAsserter<O,RA> assertModifiedExclusive(ItemPath... expectedPat
/** Asserts that (something) in every specified path is modified. Supports prefixes. */
@SuppressWarnings({ "UnusedReturnValue", "WeakerAccess" })
public ObjectDeltaAsserter<O,RA> assertModified(ItemPath... pathsThatMustBeModified) {
assertModify();
PathSet modifiedPaths = delta.getModifications().stream()
.map(ItemDelta::getPath)
.collect(Collectors.toCollection(() -> new PathSet()));
Expand All @@ -123,6 +125,7 @@ public ObjectDeltaAsserter<O,RA> assertModified(ItemPath... pathsThatMustBeModif
/** Asserts that _nothing_ in any specified path is modified. Supports prefixes. */
@SuppressWarnings("WeakerAccess")
public ObjectDeltaAsserter<O,RA> assertNotModified(ItemPath... pathsThatMustNotBeModified) {
assertModify();
for (ItemDelta<?, ?> modification : delta.getModifications()) {
ItemPath modifiedPath = modification.getPath();
if (ItemPathCollectionsUtil.containsSubpathOrEquivalent(List.of(pathsThatMustNotBeModified), modifiedPath)) {
Expand All @@ -140,6 +143,7 @@ public ObjectDeltaAsserter<O,RA> assertNoRealResourceObjectModifications() {

/** Asserts that nothing except for specified paths is modified. Supports prefixes. */
public ObjectDeltaAsserter<O,RA> assertNotModifiedExcept(ItemPath... pathsThatCanBeModified) {
assertModify();
for (ItemDelta<?, ?> modification : delta.getModifications()) {
ItemPath modifiedPath = modification.getPath();
if (!ItemPathCollectionsUtil.containsSubpathOrEquivalent(List.of(pathsThatCanBeModified), modifiedPath)) {
Expand All @@ -152,8 +156,8 @@ public ObjectDeltaAsserter<O,RA> assertNotModifiedExcept(ItemPath... pathsThatCa

public ObjectDeltaAsserter<O,RA> assertPolyStringModification(
ItemPath path, String expectedOldOrig, String expectedAddOrReplaceOrig) {
PropertyDelta<PolyString> propertyDelta = delta.findPropertyDelta(path);
assertThat(propertyDelta).as("delta for '" + path + "'").isNotNull();
assertModify();
PropertyDelta<PolyString> propertyDelta = getPropertyDeltaRequired(path);
Set<String> realAddOrReplaceOrig =
Sets.union(
getOrig(propertyDelta.getRealValuesToAdd()),
Expand All @@ -164,12 +168,48 @@ public ObjectDeltaAsserter<O,RA> assertPolyStringModification(
Set<String> realOldOrig = getOrig(
PrismValueCollectionsUtil.getRealValuesOfCollection(
propertyDelta.getEstimatedOldValues()));
assertThat(realOldOrig)
.as("old values")
.containsExactlyInAnyOrder(expectedOldOrig);
assertExpectedOldValues(expectedOldOrig, realOldOrig);
return this;
}

public <T> ObjectDeltaAsserter<O,RA> assertModification(
ItemPath path, T expectedOldOrig, T expectedAddOrReplaceOrig) {
assertModify();
PropertyDelta<T> propertyDelta = getPropertyDeltaRequired(path);
//noinspection unchecked
Collection<T> realAddOrReplaceOrig =
propertyDelta.isReplace() ?
(Collection<T>) propertyDelta.getRealValuesToReplace() :
(Collection<T>) propertyDelta.getRealValuesToAdd();
assertThat(realAddOrReplaceOrig)
.as("values added or replaced")
.containsExactlyInAnyOrder(expectedAddOrReplaceOrig);
Collection<T> realOldOrig =
PrismValueCollectionsUtil.getRealValuesOfCollection(
propertyDelta.getEstimatedOldValues());
assertExpectedOldValues(expectedOldOrig, realOldOrig);
return this;
}

@NotNull
private <T> PropertyDelta<T> getPropertyDeltaRequired(ItemPath path) {
PropertyDelta<T> propertyDelta = delta.findPropertyDelta(path);
assertThat(propertyDelta).as("delta for '" + path + "'").isNotNull();
return propertyDelta;
}

private static <T> void assertExpectedOldValues(T expectedOldOrig, Collection<T> realOldOrig) {
if (expectedOldOrig != null) {
assertThat(realOldOrig)
.as("old values")
.containsExactlyInAnyOrder(expectedOldOrig);
} else {
assertThat(realOldOrig)
.as("old values")
.isEmpty();
}
}

private Set<String> getOrig(Collection<?> polyStrings) {
return emptyIfNull(polyStrings).stream()
.filter(val -> val instanceof PolyString)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ public class ObjectDeltaFinder<RA> {

private final DeltaCollectionAsserter<RA> deltaCollectionAsserter;
private Class<? extends ObjectType> objectType;
private String objectOid;
private ChangeType changeType;
private Integer index;

Expand All @@ -34,6 +35,11 @@ public ObjectDeltaFinder<RA> objectType(Class<? extends ObjectType> objectType)
return this;
}

public ObjectDeltaFinder<RA> objectOid(String objectOid) {
this.objectOid = objectOid;
return this;
}

public ObjectDeltaFinder<RA> changeType(ChangeType changeType) {
this.changeType = changeType;
return this;
Expand All @@ -44,9 +50,10 @@ public ObjectDeltaFinder<RA> index(Integer index) {
return this;
}

public ObjectDeltaFinder<RA> assertCount(int expected) {
/** Returns to the parent asserter. */
public DeltaCollectionAsserter<RA> assertCount(int expected) {
assertThat(select()).as("matching deltas").hasSize(expected);
return this;
return deltaCollectionAsserter;
}

private List<ObjectDelta<?>> select() {
Expand All @@ -56,6 +63,9 @@ private List<ObjectDelta<?>> select() {
if (objectType != null && !Objects.equals(delta.getObjectTypeClass(), objectType)) {
continue;
}
if (objectOid != null && !Objects.equals(delta.getOid(), objectOid)) {
continue;
}
if (changeType != null && delta.getChangeType() != changeType) {
continue;
}
Expand Down Expand Up @@ -91,6 +101,7 @@ protected void fail(String message) {
public String toString() {
return "ObjectDeltaFinder{" +
"objectType=" + objectType +
", objectOid=" + objectOid +
", changeType=" + changeType +
", index=" + index +
'}';
Expand Down

0 comments on commit 5712158

Please sign in to comment.