Skip to content

Commit

Permalink
[Drools-3838] Improve error reporting in scenario result (#2335)
Browse files Browse the repository at this point in the history
* [DROOLS-3838] BackEnd part import

* [DROOLS-3838] Cleanup

* Unify assertion framework (#1)
  • Loading branch information
yesamer authored and manstis committed May 13, 2019
1 parent eebde29 commit 8792ae7
Show file tree
Hide file tree
Showing 16 changed files with 140 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ public class FactMappingValue {
private ExpressionIdentifier expressionIdentifier;
private Object rawValue;
@XStreamOmitField
private boolean error = false;
private FactMappingValueStatus status = FactMappingValueStatus.SUCCESS;
@XStreamOmitField
private Object errorValue;
@XStreamOmitField
private String exceptionMessage;

public FactMappingValue() {
}
Expand Down Expand Up @@ -63,11 +67,31 @@ FactMappingValue cloneFactMappingValue() {
return cloned;
}

public boolean isError() {
return error;
public FactMappingValueStatus getStatus() {
return status;
}

public Object getErrorValue() {
return errorValue;
}

public void setErrorValue(Object errorValue) {
this.errorValue = errorValue;
this.status = FactMappingValueStatus.FAILED_WITH_ERROR;
}

public String getExceptionMessage() {
return exceptionMessage;
}

public void setExceptionMessage(String exceptionMessage) {
this.exceptionMessage = exceptionMessage;
this.status = FactMappingValueStatus.FAILED_WITH_EXCEPTION;
}

public void setError(boolean error) {
this.error = error;
public void resetStatus() {
this.status = FactMappingValueStatus.SUCCESS;
this.exceptionMessage = null;
this.errorValue = null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@

/*
* Copyright 2019 Red Hat, Inc. and/or its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.drools.scenariosimulation.api.model;

public enum FactMappingValueStatus {

SUCCESS,
FAILED_WITH_ERROR,
FAILED_WITH_EXCEPTION
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class Scenario {
*/
private final List<FactMappingValue> factMappingValues = new ArrayList<>();

/**
* Not used, to be removed.
*/
@Deprecated
private SimulationDescriptor simulationDescriptor = new SimulationDescriptor();

public Scenario() {
Expand All @@ -46,11 +50,7 @@ public Scenario(SimulationDescriptor simulationDescriptor) {
/**
* Returns an <b>unmodifiable</b> list wrapping the backed one
* <p>
* NOTE: list order could not be aligned to factMapping order. Use {@link Scenario#sort()} before call this method
* to ensure the order.
* Best way to have ordered factMappingValues is to iterate over {@link SimulationDescriptor#factMappings} and use
* {@link #getFactMappingValue(FactIdentifier, ExpressionIdentifier)}
* @return not modifiable list of FactMappingValues
* NOTE: list order could not be aligned to factMapping order.
*/
public List<FactMappingValue> getUnmodifiableFactMappingValues() {
return Collections.unmodifiableList(factMappingValues);
Expand Down Expand Up @@ -88,15 +88,8 @@ public Optional<FactMappingValue> getFactMappingValue(FactIdentifier factIdentif
e.getExpressionIdentifier().equals(expressionIdentifier)).findFirst();
}

public Optional<FactMappingValue> getFactMappingValueByIndex(int index) {
FactMapping factMappingByIndex;
try {
factMappingByIndex = simulationDescriptor.getFactMappingByIndex(index);
} catch (IndexOutOfBoundsException e) {
throw new IllegalArgumentException(
new StringBuilder().append("Impossible to retrieve FactMapping at index ").append(index).toString(), e);
}
return getFactMappingValue(factMappingByIndex.getFactIdentifier(), factMappingByIndex.getExpressionIdentifier());
public Optional<FactMappingValue> getFactMappingValue(FactMapping factMapping) {
return getFactMappingValue(factMapping.getFactIdentifier(), factMapping.getExpressionIdentifier());
}

public List<FactMappingValue> getFactMappingValuesByFactIdentifier(FactIdentifier factIdentifier) {
Expand All @@ -120,16 +113,8 @@ public Collection<String> getFactNames() {
return factMappingValues.stream().map(e -> e.getFactIdentifier().getName()).collect(toSet());
}

public void sort() {
factMappingValues.sort((a, b) -> {
Integer aIndex = simulationDescriptor.getIndexByIdentifier(a.getFactIdentifier(), a.getExpressionIdentifier());
Integer bIndex = simulationDescriptor.getIndexByIdentifier(b.getFactIdentifier(), b.getExpressionIdentifier());
return aIndex.compareTo(bIndex);
});
}

public void resetErrors() {
factMappingValues.forEach(elem -> elem.setError(false));
factMappingValues.forEach(elem -> elem.resetStatus());
}

Scenario cloneScenario() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,6 @@ public void clearScenarios() {
scenarios.clear();
}

public void sort() {
scenarios.forEach(Scenario::sort);
}

public void resetErrors() {
scenarios.forEach(Scenario::resetErrors);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import org.junit.Test;

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

public class FactMappingValueTest {
Expand All @@ -32,4 +33,33 @@ public void emptyFactMappingValue() {
.isInstanceOf(NullPointerException.class)
.hasMessage("ExpressionIdentifier has to be not null");
}

@Test
public void resetStatus() {
FactMappingValue value = new FactMappingValue();
value.resetStatus();
assertThat(value.getStatus()).isEqualTo(FactMappingValueStatus.SUCCESS);
assertThat(value.getExceptionMessage()).isNull();
assertThat(value.getErrorValue()).isNull();
}

@Test
public void setErrorValue() {
String errorValue = "value";
FactMappingValue value = new FactMappingValue();
value.setErrorValue(errorValue);
assertThat(value.getStatus()).isEqualTo(FactMappingValueStatus.FAILED_WITH_ERROR);
assertThat(value.getExceptionMessage()).isNull();
assertThat(value.getErrorValue()).isEqualTo(errorValue);
}

@Test
public void setExceptionMessage() {
String exceptionValue = "Exception";
FactMappingValue value = new FactMappingValue();
value.setExceptionMessage(exceptionValue);
assertThat(value.getStatus()).isEqualTo(FactMappingValueStatus.FAILED_WITH_EXCEPTION);
assertThat(value.getExceptionMessage()).isEqualTo(exceptionValue);
assertThat(value.getErrorValue()).isNull();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

public class ScenarioTest {
Expand Down Expand Up @@ -92,19 +91,4 @@ public void addOrUpdateMappingValue() {
assertEquals(factMappingValue, factMappingValue1);
assertEquals(factMappingValue1.getRawValue(), value2);
}

@Test
public void sortTest() {
ExpressionIdentifier expressionIdentifier2 = ExpressionIdentifier.create("Test expression 2", FactMappingType.GIVEN);
simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier);
simulationDescriptor.addFactMapping(factIdentifier, expressionIdentifier2);
scenario.addMappingValue(factIdentifier, expressionIdentifier2, "Test 2");
FactMappingValue factMappingValue1 = scenario.addMappingValue(factIdentifier, this.expressionIdentifier, "Test");

assertEquals(scenario.getUnmodifiableFactMappingValues().get(1), factMappingValue1);

scenario.sort();
assertNotEquals(scenario.getUnmodifiableFactMappingValues().get(1), factMappingValue1);
assertEquals(scenario.getUnmodifiableFactMappingValues().get(0), factMappingValue1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public Void execute(Context context) {
if (objects.size() > 0) {
factToCheck.forEach(fact -> fact.getScenarioResult().setResult(true));
} else {
factToCheck.forEach(fact -> fact.getScenarioResult().getFactMappingValue().setError(true));
factToCheck.forEach(fact -> fact.getScenarioResult().getFactMappingValue().setExceptionMessage("There is no instance which satisfies the expected conditions"));
}
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public ResultWrapper<Object> getDirectMapping(Map<List<String>, Object> params)
return ResultWrapper.createResult(entry.getValue());
}
}
return ResultWrapper.createErrorResult();
return ResultWrapper.createErrorResult(null, null);
}

public List<ScenarioExpect> extractExpectedValues(List<FactMappingValue> factMappingValues) {
Expand Down Expand Up @@ -179,7 +179,7 @@ public Map<List<String>, Object> getParamsForBean(SimulationDescriptor simulatio
factMappingValue.getRawValue());
paramsForBean.put(pathToField, value);
} catch (IllegalArgumentException e) {
factMappingValue.setError(true);
factMappingValue.setExceptionMessage(e.getMessage());
throw new ScenarioException(e.getMessage(), e);
}
}
Expand All @@ -203,7 +203,12 @@ public void validateAssertion(List<ScenarioResult> scenarioResults, Scenario sce
protected ScenarioResult fillResult(FactMappingValue expectedResult, FactIdentifier factIdentifier, Supplier<ResultWrapper<?>> resultSupplier) {
ResultWrapper<?> resultValue = resultSupplier.get();

expectedResult.setError(!resultValue.isSatisfied());
if (!resultValue.isSatisfied()) {
expectedResult.setErrorValue(resultValue.getResult());

} else {
expectedResult.resetStatus();
}

return new ScenarioResult(factIdentifier, expectedResult, resultValue.getResult()).setResult(resultValue.isSatisfied());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ protected ResultWrapper getSingleFactValueResult(FactMapping factMapping,
DMNDecisionResult decisionResult,
ExpressionEvaluator expressionEvaluator) {
Object resultRaw = decisionResult.getResult();

if (!SUCCEEDED.equals(decisionResult.getEvaluationStatus())) {
return createErrorResult();
final DMNDecisionResult.DecisionEvaluationStatus evaluationStatus = decisionResult.getEvaluationStatus();
if (!SUCCEEDED.equals(evaluationStatus)) {
return createErrorResult(SUCCEEDED, evaluationStatus);
}

for (ExpressionElement expressionElement : factMapping.getExpressionElementsWithoutClass()) {
Expand All @@ -135,12 +135,13 @@ protected ResultWrapper getSingleFactValueResult(FactMapping factMapping,

Class<?> resultClass = resultRaw != null ? resultRaw.getClass() : null;

Object expectedResultRaw = expectedResult.getRawValue();
try {
return expressionEvaluator.evaluateUnaryExpression(expectedResult.getRawValue(), resultRaw, resultClass) ?
return expressionEvaluator.evaluateUnaryExpression(expectedResultRaw, resultRaw, resultClass) ?
createResult(resultRaw) :
createErrorResult();
createErrorResult(resultRaw, expectedResultRaw);
} catch (Exception e) {
expectedResult.setError(true);
expectedResult.setExceptionMessage(e.getMessage());
throw new ScenarioException(e.getMessage(), e);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,13 @@ protected Function<Object, ResultWrapper> createExtractorFunction(ExpressionEval
List<String> pathToValue = factMapping.getExpressionElementsWithoutClass().stream().map(ExpressionElement::getStep).collect(toList());
ScenarioBeanWrapper<?> scenarioBeanWrapper = ScenarioBeanUtil.navigateToObject(objectToCheck, pathToValue, false);
Object resultValue = scenarioBeanWrapper.getBean();

Object expectedResultValue = expectedResult.getRawValue();
try {
return expressionEvaluator.evaluateUnaryExpression(expectedResult.getRawValue(), resultValue, scenarioBeanWrapper.getBeanClass()) ?
return expressionEvaluator.evaluateUnaryExpression(expectedResultValue, resultValue, scenarioBeanWrapper.getBeanClass()) ?
createResult(resultValue) :
createErrorResult();
createErrorResult(resultValue, expectedResultValue);
} catch (Exception e) {
expectedResult.setError(true);
expectedResult.setExceptionMessage(e.getMessage());
throw new ScenarioException(e.getMessage(), e);
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,26 @@ public class ResultWrapper<T> {
private final boolean satisfied;

private final T result;
private final T expected;

private ResultWrapper(T result, boolean satisfied) {
this.satisfied = satisfied;
this.result = result;
this.expected = null;
}

private ResultWrapper(T result, T expected) {
this.satisfied = false;
this.result = result;
this.expected = expected;
}

public static <T> ResultWrapper<T> createResult(T result) {
return new ResultWrapper<>(result, true);
}

public static <T> ResultWrapper<T> createErrorResult() {
return new ResultWrapper<>(null, false);
public static <T> ResultWrapper<T> createErrorResult(T result, T expected) {
return new ResultWrapper<>(result, expected);
}

public boolean isSatisfied() {
Expand All @@ -49,6 +57,10 @@ public T getResult() {
return result;
}

public T getExpected() {
return expected;
}

public T orElse(T defaultValue) {
return satisfied ? result : defaultValue;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void acceptTest() {
Assert.assertFalse(conditionFilter.accept(1));
Assert.assertTrue(conditionFilter.accept("String"));

Function<Object, ResultWrapper> alwaysNotMatchFunction = object -> ResultWrapper.createErrorResult();
Function<Object, ResultWrapper> alwaysNotMatchFunction = object -> ResultWrapper.createErrorResult(null, null);
ConditionFilter conditionFilterFail = new ConditionFilter(asList(new FactCheckerHandle(String.class, alwaysNotMatchFunction, scenarioResult)));
Assert.assertFalse(conditionFilterFail.accept("String"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.function.Function;

import org.drools.scenariosimulation.api.model.FactMappingValue;
import org.drools.scenariosimulation.api.model.FactMappingValueStatus;
import org.drools.scenariosimulation.backend.runner.model.ResultWrapper;
import org.drools.scenariosimulation.backend.runner.model.ScenarioResult;
import org.junit.Test;
Expand Down Expand Up @@ -66,11 +67,10 @@ public void executeTest() {

reset(scenarioResult);

boolean expectedResult = true;
when(kieSession.getObjects(any(ObjectFilter.class))).thenReturn(Collections.emptyList());
when(scenarioResult.getFactMappingValue()).thenReturn(factMappingValue);
when(factMappingValue.isError()).thenReturn(expectedResult);
when(factMappingValue.getStatus()).thenReturn(FactMappingValueStatus.FAILED_WITH_EXCEPTION);
validateFactCommand.execute(registryContext);
verify(scenarioResult, times(0)).setResult(expectedResult);
verify(scenarioResult, times(0)).setResult(anyBoolean());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.drools.scenariosimulation.api.model.FactMapping;
import org.drools.scenariosimulation.api.model.FactMappingType;
import org.drools.scenariosimulation.api.model.FactMappingValue;
import org.drools.scenariosimulation.api.model.FactMappingValueStatus;
import org.drools.scenariosimulation.api.model.Scenario;
import org.drools.scenariosimulation.api.model.ScenarioWithIndex;
import org.drools.scenariosimulation.api.model.Simulation;
Expand Down Expand Up @@ -184,7 +185,7 @@ public void verifyConditions() {
newScenarioRunnerData,
mock(ExpressionEvaluator.class),
requestContextMock);
assertTrue(newScenarioRunnerData.getResults().get(0).getFactMappingValue().isError());
assertEquals(newScenarioRunnerData.getResults().get(0).getFactMappingValue().getStatus(), FactMappingValueStatus.FAILED_WITH_ERROR);
}

@SuppressWarnings("unchecked")
Expand Down
Loading

0 comments on commit 8792ae7

Please sign in to comment.