Skip to content

Commit

Permalink
Fix operation result in resolveReferenceIfExists
Browse files Browse the repository at this point in the history
When the reference didn't exist the method returned null (correctly)
but incorrectly marked result as FATAL_ERROR. This is now changed to
HANDLED_ERROR.

Resolves MID-6133.

(cherry picked from commit 05dc531)
  • Loading branch information
mederly committed Mar 27, 2020
1 parent cd582c0 commit 6c7f51b
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 7 deletions.
Expand Up @@ -131,6 +131,7 @@ public class MidpointFunctionsImpl implements MidpointFunctions {
@Autowired private SynchronizationExpressionsEvaluator correlationConfirmationEvaluator;
@Autowired private ArchetypeManager archetypeManager;
@Autowired private TriggerCreatorGlobalState triggerCreatorGlobalState;
@Autowired private SchemaHelper schemaHelper;

@Autowired
@Qualifier("cacheRepositoryService")
Expand Down Expand Up @@ -842,6 +843,12 @@ public <T extends ObjectType> T createEmptyObjectWithName(Class<T> type, PolyStr

@Override
public <T extends ObjectType> T resolveReference(ObjectReferenceType reference)
throws ObjectNotFoundException, SchemaException, CommunicationException, ConfigurationException,
SecurityViolationException, ExpressionEvaluationException {
return resolveReferenceInternal(reference, false);
}

private <T extends ObjectType> T resolveReferenceInternal(ObjectReferenceType reference, boolean allowNotFound)
throws ObjectNotFoundException, SchemaException,
CommunicationException, ConfigurationException,
SecurityViolationException, ExpressionEvaluationException {
Expand All @@ -854,19 +861,21 @@ public <T extends ObjectType> T resolveReference(ObjectReferenceType reference)
if (objectDefinition == null) {
throw new SchemaException("No definition for type " + type);
}
return modelService.getObject(
objectDefinition.getCompileTimeClass(), reference.getOid(),
SelectorOptions.createCollection(GetOperationOptions.createExecutionPhase()), getCurrentTask(), getCurrentResult())
.asObjectable();
Collection<SelectorOptions<GetOperationOptions>> options = schemaHelper.getOperationOptionsBuilder()
.executionPhase()
.allowNotFound(allowNotFound)
.build();
return modelService.getObject(objectDefinition.getCompileTimeClass(), reference.getOid(), options,
getCurrentTask(), getCurrentResult())
.asObjectable();
}

@Override
public <T extends ObjectType> T resolveReferenceIfExists(ObjectReferenceType reference)
throws SchemaException,
CommunicationException, ConfigurationException,
throws SchemaException, CommunicationException, ConfigurationException,
SecurityViolationException, ExpressionEvaluationException {
try {
return resolveReference(reference);
return resolveReferenceInternal(reference, true);
} catch (ObjectNotFoundException e) {
return null;
}
Expand Down
@@ -0,0 +1,87 @@
/*
* Copyright (c) 2010-2018 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.model.intest;

import com.evolveum.midpoint.model.impl.expr.ExpressionEnvironment;
import com.evolveum.midpoint.model.impl.expr.ModelExpressionThreadLocalHolder;
import com.evolveum.midpoint.schema.constants.ObjectTypes;
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.util.ObjectTypeUtil;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.util.exception.CommonException;
import com.evolveum.midpoint.util.exception.ObjectNotFoundException;
import com.evolveum.midpoint.xml.ns._public.common.common_3.ObjectReferenceType;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.annotation.DirtiesContext.ClassMode;
import org.springframework.test.context.ContextConfiguration;
import org.testng.annotations.Test;

/**
* Tests selected methods in MidpointFunctions.
* In the future we can test custom functions mechanism here.
*/
@SuppressWarnings({ "FieldCanBeLocal", "unused", "SameParameterValue" })
@ContextConfiguration(locations = { "classpath:ctx-model-intest-test-main.xml" })
@DirtiesContext(classMode = ClassMode.AFTER_CLASS)
public class TestFunctions extends AbstractInitializedModelIntegrationTest {

@FunctionalInterface
private interface CheckedRunnable {
void run() throws CommonException;
}

/**
* MID-6133
*/
@Test
public void test100ResolveReferenceIfExists() throws Exception {
// GIVEN
Task task = createTask("test100ResolveReferenceIfExists");
OperationResult result = task.getResult();

ObjectReferenceType broken = ObjectTypeUtil.createObjectRef("non-existing-oid", ObjectTypes.USER);

// WHEN
execute(task, result, () -> libraryMidpointFunctions.resolveReferenceIfExists(broken));

// THEN
assertSuccess(result);
}

/**
* MID-6133
*/
@Test
public void test110ResolveReference() throws Exception {
// GIVEN
Task task = createTask("test110ResolveReference");
OperationResult result = task.getResult();

ObjectReferenceType broken = ObjectTypeUtil.createObjectRef("non-existing-oid", ObjectTypes.USER);

// WHEN
try {
execute(task, result, () -> libraryMidpointFunctions.resolveReference(broken));
fail("unexpected success");
} catch (ObjectNotFoundException e) {
System.out.println("expected failure: " + e.getMessage());
}

// THEN
assertFailure(result);
}

private void execute(Task task, OperationResult result, CheckedRunnable runnable) throws CommonException {
ExpressionEnvironment<?,?,?> environment = new ExpressionEnvironment<>(task, result);
ModelExpressionThreadLocalHolder.pushExpressionEnvironment(environment);
try {
runnable.run();
} finally {
ModelExpressionThreadLocalHolder.popExpressionEnvironment();
}
}
}
1 change: 1 addition & 0 deletions model/model-intest/testng-integration.xml
Expand Up @@ -65,6 +65,7 @@
<class name="com.evolveum.midpoint.model.intest.misc.TestMisc"/>
<class name="com.evolveum.midpoint.model.intest.misc.TestUuidNonUniqueName"/>
<class name="com.evolveum.midpoint.model.intest.misc.TestMigration"/>
<class name="com.evolveum.midpoint.model.intest.TestFunctions"/>
</classes>
</test>
<test name="Synchronization" preserve-order="true" parallel="false" verbose="10">
Expand Down

0 comments on commit 6c7f51b

Please sign in to comment.