Skip to content

Commit

Permalink
Demonstrate custom progress reporting
Browse files Browse the repository at this point in the history
Related to MID-8056.
  • Loading branch information
mederly committed Jan 9, 2023
1 parent ca318be commit 072542f
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
import com.evolveum.midpoint.schema.result.OperationResult;
import com.evolveum.midpoint.schema.result.OperationResultStatus;
import com.evolveum.midpoint.task.api.Task;
import com.evolveum.midpoint.test.TestTask;
import com.evolveum.midpoint.test.util.LogfileTestTailer;
import com.evolveum.midpoint.test.util.TestUtil;
import com.evolveum.midpoint.util.DebugUtil;
Expand Down Expand Up @@ -142,6 +143,9 @@ public abstract class AbstractBasicScriptingTest extends AbstractInitializedMode
private static final String PASSWORD_PLAINTEXT_2 = "pass1234wor2";
private static final String PASSWORD_PLAINTEXT_3 = "pass1234wor3";

private static final TestTask TASK_CUSTOM_SCRIPTING =
new TestTask(TEST_DIR, "task-custom-scripting.xml", "f3b3f5e1-b3fb-42d5-9676-fa8ab9e5b58c");

@Autowired ScriptingExpressionEvaluator evaluator;

@Override
Expand Down Expand Up @@ -1426,6 +1430,33 @@ public void test620ModifyJackPasswordViaExecuteChangesAsynchronously() throws Ex
displayDumpable("Audit", dummyAuditService);
}

/**
* Tests (experimental, unofficial) custom reporting.
*
* MID-8056
*/
@Test
public void test620CustomScripting() throws CommonException, IOException {
Task task = getTestTask();
OperationResult result = task.getResult();

TASK_CUSTOM_SCRIPTING.init(this, task, result);
TASK_CUSTOM_SCRIPTING.rerunErrorsOk(result);

// @formatter:off
assertTask(TASK_CUSTOM_SCRIPTING.oid, "after")
.display()
.assertPartialError()
.assertResultMessageContains("custom-error")
.rootActivityState()
.progress()
.assertCommitted(3, 1, 1)
.assertExpectedTotal(5)
.end()
.end();
// @formatter:on
}

private void assertNoOutputData(ExecutionContext output) {
assertTrue("Script returned unexpected data", output.getFinalOutput() == null || output.getFinalOutput().getData().isEmpty());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
<!--
~ Copyright (c) 2010-2019 Evolveum and contributors
~
~ This work is dual-licensed under the Apache License 2.0
~ and European Union Public License. See LICENSE file for details.
-->

<task xmlns="http://midpoint.evolveum.com/xml/ns/public/common/common-3"
xmlns:s="http://midpoint.evolveum.com/xml/ns/public/model/scripting-3"
oid="f3b3f5e1-b3fb-42d5-9676-fa8ab9e5b58c">
<name>task-custom-scripting</name>
<ownerRef oid="00000000-0000-0000-0000-000000000002" type="UserType">
<!-- administrator -->
</ownerRef>
<executionState>closed</executionState>
<activity>
<work>
<nonIterativeScripting>
<scriptExecutionRequest>
<s:execute>
<s:script>
<code>
import com.evolveum.midpoint.repo.common.activity.run.AbstractActivityRun
import com.evolveum.midpoint.schema.result.OperationResultStatus
import com.evolveum.midpoint.schema.statistics.IterationItemInformation
import com.evolveum.midpoint.schema.statistics.IterativeOperationStartInfo
import com.evolveum.midpoint.schema.statistics.Operation
import com.evolveum.midpoint.xml.ns._public.common.common_3.ItemProcessingOutcomeType
import com.evolveum.midpoint.xml.ns._public.common.common_3.QualifiedItemProcessingOutcomeType

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

// In this scenario, we'll simulate processing of 5 items, with the following outcomes:
def allOutcomes = [SUCCESS, SUCCESS, SKIP, FAILURE, SUCCESS]

// Internal object that we'll (mis)use to report the progress. Unofficial, unsupported.
def activityState = midpoint.currentTask.executionSupport.activityState

activityState.liveProgress.expectedTotal = 5

// Now let us simulate processing of individual items
for (int i in 0..4) {

// Here we record the start of the processing. It is a hack: normally we would call
// midpoint.currentTask.recordIterativeOperationStart(..) but the current implementation
// (that checks whether detailed statistics are enabled) precludes such use here.
// So we have to go through liveStatistics.liveItemProcessing etc.
IterativeOperationStartInfo startInfo =
new IterativeOperationStartInfo(
new IterationItemInformation())
startInfo.simpleCaller = true
Operation op = activityState.liveStatistics.liveItemProcessing.recordOperationStart(startInfo)

// (real processing would be here here)

// Now let's report the (fictitious) outcome. Note that the call below reports only the
// progress, not item processing and other detailed statistics, as the latter is currently
// disabled for non-iterative scripting tasks.
ItemProcessingOutcomeType currentOutcome = allOutcomes[i]
op.done(currentOutcome, null)
}

// We can also set the status like this
midpoint.currentResult.recordStatus(OperationResultStatus.PARTIAL_ERROR, "custom-error")
</code>
</s:script>
<s:forWholeInput>true</s:forWholeInput>
</s:execute>
</scriptExecutionRequest>
</nonIterativeScripting>
</work>
</activity>
</task>
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

import static org.testng.AssertJUnit.assertEquals;

import static org.assertj.core.api.Assertions.assertThat;

import com.evolveum.midpoint.schema.util.task.ActivityProgressUtil;
import com.evolveum.midpoint.test.IntegrationTestTools;
import com.evolveum.midpoint.util.DebugUtil;
Expand Down Expand Up @@ -109,4 +111,9 @@ private int getSkipCount(boolean open) {
private int getNonFailureCount(boolean open) {
return getSuccessCount(open) + getSkipCount(open);
}

public ActivityProgressAsserter<RA> assertExpectedTotal(int expected) {
assertThat(information.getExpectedTotal()).as("expected total").isEqualTo(expected);
return this;
}
}

0 comments on commit 072542f

Please sign in to comment.