Skip to content

Commit

Permalink
Fixing some tests after delta/OID changes
Browse files Browse the repository at this point in the history
  • Loading branch information
semancik committed Jan 17, 2019
1 parent cf8acdd commit 6af1a50
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 20 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2013 Evolveum
* Copyright (c) 2010-2019 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -26,6 +26,7 @@
*
*/
public class ShadowDiscriminatorObjectDeltaImpl<T extends Objectable> extends ObjectDeltaImpl<T> implements ShadowDiscriminatorObjectDelta<T> {
private static final long serialVersionUID = 1L;

private ResourceShadowDiscriminator discriminator;

Expand Down Expand Up @@ -59,5 +60,16 @@ protected String debugName() {
protected String debugIdentifiers() {
return discriminator == null ? "null" : discriminator.toString();
}


@Override
public ShadowDiscriminatorObjectDeltaImpl<T> clone() {
ShadowDiscriminatorObjectDeltaImpl<T> clone = new ShadowDiscriminatorObjectDeltaImpl<>(this.getObjectTypeClass(), this.getChangeType(), this.getPrismContext());
copyValues(clone);
return clone;
}

protected void copyValues(ShadowDiscriminatorObjectDeltaImpl<T> clone) {
super.copyValues(clone);
clone.discriminator = this.discriminator;
}
}
Expand Up @@ -534,6 +534,11 @@ public void applyDefinitionIfPresent(PrismObjectDefinition<O> definition, boolea
*/
public ObjectDeltaImpl<O> clone() {
ObjectDeltaImpl<O> clone = new ObjectDeltaImpl<>(this.objectTypeClass, this.changeType, this.prismContext);
copyValues(clone);
return clone;
}

protected void copyValues(ObjectDeltaImpl<O> clone) {
clone.oid = this.oid;
for (ItemDelta<?,?> thisModification: this.modifications) {
((Collection)clone.modifications).add(thisModification.clone());
Expand All @@ -542,8 +547,7 @@ public ObjectDeltaImpl<O> clone() {
clone.objectToAdd = null;
} else {
clone.objectToAdd = this.objectToAdd.clone();
}
return clone;
}
}

/**
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2010-2018 Evolveum
* Copyright (c) 2010-2019 Evolveum
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,7 +18,10 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collector;
import java.util.stream.Collectors;

import com.evolveum.midpoint.prism.PrismObject;
import com.evolveum.midpoint.prism.delta.ObjectDelta;
import com.evolveum.midpoint.prism.polystring.PolyString;
import com.evolveum.midpoint.schema.result.OperationResult;
Expand Down Expand Up @@ -176,6 +179,55 @@ public static String findFocusDeltaOidInCollection(Collection<ObjectDeltaOperati
return odo.getObjectDelta().getOid();
}

public static List<ObjectDeltaOperation<ShadowType>> findProjectionDeltasInCollection(Collection<ObjectDeltaOperation<? extends ObjectType>> odos) {
List<ObjectDeltaOperation<ShadowType>> projectionDeltas = new ArrayList<>();
for (ObjectDeltaOperation<? extends ObjectType> odo : odos) {
Class<? extends ObjectType> objectTypeClass = odo.getObjectDelta().getObjectTypeClass();
if (ShadowType.class.equals(objectTypeClass)) {
projectionDeltas.add((ObjectDeltaOperation<ShadowType>) odo);
}
}
return projectionDeltas;
}

public static List<String> findProjectionDeltaOidsInCollection(Collection<ObjectDeltaOperation<? extends ObjectType>> executeChanges) {
return findProjectionDeltasInCollection(executeChanges).stream()
.map(odo -> odo.getObjectDelta().getOid())
.distinct()
.collect(Collectors.toList());
}

public static <O extends ObjectType> ObjectDeltaOperation<O> findAddDelta(Collection<ObjectDeltaOperation<? extends ObjectType>> executedChanges, PrismObject<O> object) {
for (ObjectDeltaOperation<? extends ObjectType> odo : executedChanges) {
Class<? extends ObjectType> objectTypeClass = odo.getObjectDelta().getObjectTypeClass();
if (odo.getObjectDelta().isAdd() && object.getCompileTimeClass().equals(objectTypeClass)) {
return (ObjectDeltaOperation<O>) odo;
}
}
return null;
}

public static <O extends ObjectType> String findAddDeltaOid(Collection<ObjectDeltaOperation<? extends ObjectType>> executedChanges, PrismObject<O> object) {
ObjectDeltaOperation<O> odo = findAddDelta(executedChanges, object);
if (odo == null) {
return null;
}
return odo.getObjectDelta().getOid();
}


// Mostly for use in tests.
public static String findProjectionDeltaOidInCollection(Collection<ObjectDeltaOperation<? extends ObjectType>> executeChanges) {
List<String> oids = findProjectionDeltaOidsInCollection(executeChanges);
if (oids.isEmpty()) {
return null;
}
if (oids.size() > 1) {
throw new IllegalStateException("More than one projection oid");
}
return oids.get(0);
}

@Override
public int hashCode() {
final int prime = 31;
Expand Down Expand Up @@ -283,4 +335,5 @@ public static <O extends ObjectType> String shorterDebugDump(List<? extends Obje
sb.append("]");
return sb.toString();
}

}
Expand Up @@ -46,6 +46,7 @@
import com.evolveum.midpoint.repo.cache.RepositoryCache;
import com.evolveum.midpoint.schema.DeltaConvertor;
import com.evolveum.midpoint.schema.GetOperationOptions;
import com.evolveum.midpoint.schema.ObjectDeltaOperation;
import com.evolveum.midpoint.schema.SelectorOptions;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.MiscSchemaUtil;
Expand Down Expand Up @@ -279,10 +280,10 @@ public <T extends ObjectType> String addObject(PrismObject<T> object, ModelExecu

ObjectDelta<T> objectDelta = DeltaFactory.Object.createAddDelta(object);
Collection<ObjectDelta<? extends ObjectType>> deltas = MiscSchemaUtil.createCollection(objectDelta);
modelService.executeChanges(deltas, options, task, result);

oid = objectDelta.getOid();
Collection<ObjectDeltaOperation<? extends ObjectType>> executedChanges = modelService.executeChanges(deltas, options, task, result);

oid = ObjectDeltaOperation.findAddDeltaOid(executedChanges, object);
result.computeStatus();
result.cleanupResult();

Expand Down
Expand Up @@ -813,30 +813,31 @@ public void test119ModifyUserDeleteAccount() throws Exception {

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

// GIVEN
Task task = taskManager.createTaskInstance(TestModelServiceContract.class.getName() + ".test120AddAccount");
Task task = taskManager.createTaskInstance(TestModelServiceContract.class.getName() + "." + TEST_NAME);
OperationResult result = task.getResult();
preTestCleanup(AssignmentPolicyEnforcementType.POSITIVE);

PrismObject<ShadowType> account = PrismTestUtil.parseObject(ACCOUNT_JACK_DUMMY_FILE);
ObjectDelta<ShadowType> accountDelta = DeltaFactory.Object.createAddDelta(account);
Collection<ObjectDelta<? extends ObjectType>> deltas = MiscSchemaUtil.createCollection(accountDelta);

XMLGregorianCalendar startTime = clock.currentTimeXMLGregorianCalendar();

// WHEN
modelService.executeChanges(deltas, null, task, result);
displayWhen(TEST_NAME);
Collection<ObjectDeltaOperation<? extends ObjectType>> executeChanges = executeChanges(accountDelta, null, task, result);

// THEN
result.computeStatus();
TestUtil.assertSuccess("executeChanges result", result);
displayThen(TEST_NAME);
assertSuccess(result);
XMLGregorianCalendar endTime = clock.currentTimeXMLGregorianCalendar();
assertCounterIncrement(InternalCounters.SHADOW_FETCH_OPERATION_COUNT, 0);

accountJackOid = accountDelta.getOid();
assertNotNull("No account OID in resulting delta", accountJackOid);
accountJackOid = ObjectDeltaOperation.findProjectionDeltaOidInCollection(executeChanges);
assertNotNull("No account OID in executed deltas", accountJackOid);
// Check accountRef (should be none)
PrismObject<UserType> userJack = modelService.getObject(UserType.class, USER_JACK_OID, null, task, result);
assertUserJack(userJack);
Expand Down Expand Up @@ -2367,10 +2368,11 @@ public void test189ModifyUserJackUnassignAndDeleteAccount() throws Exception {
*/
@Test
public void test190ModifyUserJackAssignAccountAndModify() throws Exception {
TestUtil.displayTestTitle(this, "test190ModifyUserJackAssignAccountAndModify");
final String TEST_NAME = "test190ModifyUserJackAssignAccountAndModify";
displayTestTitle(TEST_NAME);

// GIVEN
Task task = taskManager.createTaskInstance(TestModelServiceContract.class.getName() + ".test190ModifyUserJackAssignAccountAndModify");
Task task = createTask(TEST_NAME);
OperationResult result = task.getResult();
preTestCleanup(AssignmentPolicyEnforcementType.FULL);

Expand All @@ -2387,11 +2389,12 @@ public void test190ModifyUserJackAssignAccountAndModify() throws Exception {
XMLGregorianCalendar startTime = clock.currentTimeXMLGregorianCalendar();

// WHEN
modelService.executeChanges(deltas, null, task, result);
displayWhen(TEST_NAME);
executeChanges(deltas, null, task, result);

// THEN
result.computeStatus();
TestUtil.assertSuccess("executeChanges result", result);
displayThen(TEST_NAME);
assertSuccess(result);
XMLGregorianCalendar endTime = clock.currentTimeXMLGregorianCalendar();
assertCounterIncrement(InternalCounters.SHADOW_FETCH_OPERATION_COUNT, 0);

Expand Down
Expand Up @@ -1583,6 +1583,11 @@ protected <O extends ObjectType> Collection<ObjectDeltaOperation<? extends Objec
Collection<ObjectDelta<? extends ObjectType>> deltas = MiscSchemaUtil.createCollection(objectDelta);
return modelService.executeChanges(deltas, options, task, result);
}

protected <O extends ObjectType> Collection<ObjectDeltaOperation<? extends ObjectType>> executeChanges(Collection<ObjectDelta<? extends ObjectType>> deltas, ModelExecuteOptions options, Task task, OperationResult result) throws ObjectAlreadyExistsException, ObjectNotFoundException, SchemaException, ExpressionEvaluationException, CommunicationException, ConfigurationException, PolicyViolationException, SecurityViolationException {
display("Executing deltas", deltas);
return modelService.executeChanges(deltas, options, task, result);
}

protected <O extends ObjectType> Collection<ObjectDeltaOperation<? extends ObjectType>> executeChangesAssertSuccess(ObjectDelta<O> objectDelta, ModelExecuteOptions options, Task task, OperationResult result) throws ObjectAlreadyExistsException, ObjectNotFoundException, SchemaException, ExpressionEvaluationException, CommunicationException, ConfigurationException, PolicyViolationException, SecurityViolationException {
Collection<ObjectDeltaOperation<? extends ObjectType>> rv = executeChanges(objectDelta, options, task, result);
Expand Down

0 comments on commit 6af1a50

Please sign in to comment.