Skip to content

Commit

Permalink
Fix pendingOperationsCount update (MID-4831)
Browse files Browse the repository at this point in the history
  • Loading branch information
mederly committed Aug 16, 2018
1 parent 90d1a9b commit 0fbaf28
Show file tree
Hide file tree
Showing 2 changed files with 113 additions and 13 deletions.
Expand Up @@ -18,6 +18,7 @@

import static com.evolveum.midpoint.prism.SerializationOptions.createSerializeForExport;
import static com.evolveum.midpoint.schema.GetOperationOptions.createRawCollection;
import static com.evolveum.midpoint.xml.ns._public.common.common_3.PendingOperationExecutionStatusType.COMPLETED;
import static org.testng.AssertJUnit.assertEquals;
import static org.testng.AssertJUnit.assertFalse;
import static org.testng.AssertJUnit.assertNotNull;
Expand All @@ -34,6 +35,7 @@
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;

import com.evolveum.midpoint.xml.ns._public.common.common_3.*;
import org.hibernate.Session;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.ContextConfiguration;
Expand Down Expand Up @@ -94,19 +96,6 @@
import com.evolveum.midpoint.util.logging.Trace;
import com.evolveum.midpoint.util.logging.TraceManager;
import com.evolveum.midpoint.xml.ns._public.common.api_types_3.ObjectModificationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.AssignmentType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.MetadataType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectCollectionType;
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.ResourceType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.RoleType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowAttributesType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ShadowType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SynchronizationSituationDescriptionType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.SynchronizationSituationType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.TaskType;
import com.evolveum.midpoint.xml.ns._public.common.common_3.UserType;
import com.evolveum.prism.xml.ns._public.query_3.SearchFilterType;
import com.evolveum.prism.xml.ns._public.types_3.PolyStringType;

Expand Down Expand Up @@ -1008,4 +997,84 @@ public void test210ModifyObjectCollection() throws Exception {
assertTrue("Filters differ", filterExpectedParsed.equals(filterFromRepoParsed, false));
}

/**
* Add shadow pendingOperations; MID-4831
*/
@Test
public void test250AddShadowPendingOperations() throws Exception {
final String TEST_NAME = "test250AddShadowPendingOperations";
TestUtil.displayTestTitle(TEST_NAME);

// GIVEN
OperationResult result = new OperationResult(TEST_NAME);

PrismObject<ShadowType> shadow = prismContext.createObjectable(ShadowType.class)
.name("shadow1")
.oid("000-aaa-bbb-ccc")
.asPrismObject();
repositoryService.addObject(shadow, null, result);

ObjectQuery query = QueryBuilder.queryFor(ShadowType.class, prismContext)
.item(ShadowType.F_NAME).eqPoly("shadow1")
.and().exists(ShadowType.F_PENDING_OPERATION)
.build();

List<PrismObject<ShadowType>> objectsBefore = repositoryService.searchObjects(ShadowType.class, query, null, result);
assertEquals("Wrong # of shadows found (before)", 0, objectsBefore.size());

// WHEN

List<ItemDelta<?, ?>> itemDeltas = DeltaBuilder.deltaFor(ShadowType.class, prismContext)
.item(ShadowType.F_PENDING_OPERATION).add(new PendingOperationType(prismContext).executionStatus(COMPLETED))
.asItemDeltas();
repositoryService.modifyObject(ShadowType.class, shadow.getOid(), itemDeltas, getModifyOptions(), result);

// THEN

List<PrismObject<ShadowType>> objectsAfter = repositoryService.searchObjects(ShadowType.class, query, null, result);
assertEquals("Wrong # of shadows found (after)", 1, objectsAfter.size());
display("object found (after)", objectsAfter.get(0));
}

/**
* Delete shadow pendingOperations; MID-4831
*/
@Test
public void test260DeleteShadowPendingOperations() throws Exception {
final String TEST_NAME = "test260DeleteShadowPendingOperations";
TestUtil.displayTestTitle(TEST_NAME);

// GIVEN
OperationResult result = new OperationResult(TEST_NAME);

PrismObject<ShadowType> shadow = prismContext.createObjectable(ShadowType.class)
.name("shadow2")
.oid("000-aaa-bbb-ddd")
.beginPendingOperation()
.executionStatus(COMPLETED)
.<ShadowType>end()
.asPrismObject();
repositoryService.addObject(shadow, null, result);

ObjectQuery query = QueryBuilder.queryFor(ShadowType.class, prismContext)
.item(ShadowType.F_NAME).eqPoly("shadow2")
.and().exists(ShadowType.F_PENDING_OPERATION)
.build();
List<PrismObject<ShadowType>> objectsBefore = repositoryService.searchObjects(ShadowType.class, query, null, result);
assertEquals("Wrong # of shadows found (before)", 1, objectsBefore.size());
display("object found (before)", objectsBefore.get(0));

// WHEN

List<ItemDelta<?, ?>> itemDeltas = DeltaBuilder.deltaFor(ShadowType.class, prismContext)
.item(ShadowType.F_PENDING_OPERATION).replace()
.asItemDeltas();
repositoryService.modifyObject(ShadowType.class, shadow.getOid(), itemDeltas, getModifyOptions(), result);

// THEN

List<PrismObject<ShadowType>> objectsAfter = repositoryService.searchObjects(ShadowType.class, query, null, result);
assertEquals("Wrong # of shadows found (after)", 0, objectsAfter.size());
}

}
Expand Up @@ -121,6 +121,8 @@ public <T extends ObjectType> RObject<T> modifyObject(Class<T> type, String oid,

ManagedType mainEntityType = entityRegistry.getJaxbMapping(type);

boolean modifiesShadowPendingOperation = false;

for (ItemDelta delta : processedModifications) {
ItemPath path = delta.getPath();

Expand Down Expand Up @@ -176,6 +178,10 @@ public <T extends ObjectType> RObject<T> modifyObject(Class<T> type, String oid,
continue;
}

if (isShadowPendingOperation(object, delta)) {
modifiesShadowPendingOperation = true;
}

Attribute attribute = findAttribute(attributeStep, nameLocalPart, path, segments, nameSegment);
if (attribute == null) {
// there's no table/column that needs update
Expand All @@ -198,8 +204,13 @@ public <T extends ObjectType> RObject<T> modifyObject(Class<T> type, String oid,
}
}

// the following will apply deltas to prismObject
handleObjectCommonAttributes(type, processedModifications, prismObject, object, idGenerator);

if (modifiesShadowPendingOperation) {
handleShadowPendingOperation(object, prismObject);
}

LOGGER.trace("Entity changes applied");

return object;
Expand Down Expand Up @@ -313,6 +324,26 @@ private void handleOperationResult(Object bean, ItemDelta delta) {
}
}


private boolean isShadowPendingOperation(RObject<?> object, ItemDelta delta) {
return object instanceof RShadow && new ItemPath(ShadowType.F_PENDING_OPERATION).equals(delta.getPath());
}

private <T extends ObjectType> void handleShadowPendingOperation(RObject<T> bean, PrismObject<T> prismObject) throws SchemaException {
if (!(bean instanceof RShadow)) {
throw new SystemException("Bean is not instance of " + RShadow.class + ", shouldn't happen");
}
RShadow shadow = (RShadow) bean;

T objectable = prismObject.asObjectable();
if (!(objectable instanceof ShadowType)) {
throw new SystemException("PrismObject is not instance of " + ShadowType.class + ", shouldn't happen");
}
ShadowType shadowType = (ShadowType) objectable;

shadow.setPendingOperationCount(shadowType.getPendingOperation().size());
}

private <T extends ObjectType> void handleObjectCommonAttributes(Class<T> type, Collection<? extends ItemDelta> modifications,
PrismObject<T> prismObject, RObject object, PrismIdentifierGenerator<T> idGenerator) throws SchemaException {

Expand Down

0 comments on commit 0fbaf28

Please sign in to comment.