Search in sources :

Example 1 with ValueWrapper

use of org.drools.scenariosimulation.backend.runner.model.ValueWrapper in project drools by kiegroup.

the class AbstractRunnerHelperTest method fillResult.

@Test
public void fillResult() {
    FactIdentifier factIdentifier = FactIdentifier.create("MyInstance", String.class.getCanonicalName());
    ExpressionIdentifier expressionIdentifier = ExpressionIdentifier.create("MyProperty", FactMappingType.GIVEN);
    FactMappingValue expectedResultSpy = spy(new FactMappingValue(factIdentifier, expressionIdentifier, VALUE));
    AtomicReference<ValueWrapper> resultWrapperAtomicReference = new AtomicReference<>();
    Supplier<ValueWrapper<?>> resultWrapperSupplier = resultWrapperAtomicReference::get;
    ExpressionEvaluator expressionEvaluator = new BaseExpressionEvaluator(AbstractRunnerHelper.class.getClassLoader());
    // Success
    resultWrapperAtomicReference.set(ValueWrapper.of(VALUE));
    assertTrue(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluator).getResult());
    verify(expectedResultSpy, times(1)).resetStatus();
    reset(expectedResultSpy);
    // Fail with expected value
    resultWrapperAtomicReference.set(ValueWrapper.errorWithValidValue(VALUE, "value1"));
    assertFalse(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluator).getResult());
    verify(expectedResultSpy, times(1)).setErrorValue(VALUE);
    reset(expectedResultSpy);
    // Fail with exception while reverting actual value
    resultWrapperAtomicReference.set(ValueWrapper.errorWithValidValue(VALUE, "value1"));
    ExpressionEvaluator expressionEvaluatorMock = mock(ExpressionEvaluator.class);
    when(expressionEvaluatorMock.fromObjectToExpression(any())).thenThrow(new IllegalArgumentException("Error"));
    assertFalse(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluatorMock).getResult());
    verify(expectedResultSpy, times(1)).setExceptionMessage("Error");
    reset(expectedResultSpy);
    // Fail in collection case
    List<String> pathToValue = Arrays.asList("field1", "fields2");
    resultWrapperAtomicReference.set(ValueWrapper.errorWithCollectionPathToValue(VALUE, pathToValue));
    assertFalse(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluator).getResult());
    verify(expectedResultSpy, times(1)).setCollectionPathToValue(pathToValue);
    verify(expectedResultSpy, times(1)).setErrorValue(VALUE);
    // Fail with exception
    resultWrapperAtomicReference.set(ValueWrapper.errorWithMessage("detailedError"));
    assertFalse(abstractRunnerHelper.fillResult(expectedResultSpy, resultWrapperSupplier, expressionEvaluator).getResult());
    verify(expectedResultSpy, times(1)).setExceptionMessage("detailedError");
}
Also used : ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) AtomicReference(java.util.concurrent.atomic.AtomicReference) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) BaseExpressionEvaluator(org.drools.scenariosimulation.backend.expression.BaseExpressionEvaluator) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) BaseExpressionEvaluator(org.drools.scenariosimulation.backend.expression.BaseExpressionEvaluator) Test(org.junit.Test)

Example 2 with ValueWrapper

use of org.drools.scenariosimulation.backend.runner.model.ValueWrapper in project drools by kiegroup.

the class AbstractRunnerHelperTest method getResultWrapper.

@Test
public void getResultWrapper() {
    ExpressionEvaluator expressionEvaluatorMock = mock(ExpressionEvaluator.class);
    Object resultRaw = "test";
    Object expectedResultRaw = "";
    String collectionWrongValue = "value";
    String collectionValuePath = "Item #: 1";
    String genericErrorMessage = "errorMessage";
    // case 1: succeed
    when(expressionEvaluatorMock.evaluateUnaryExpression(any(), any(), any(Class.class))).thenReturn(ExpressionEvaluatorResult.ofSuccessful());
    ValueWrapper valueWrapper = abstractRunnerHelper.getResultWrapper(String.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, String.class);
    assertTrue(valueWrapper.isValid());
    assertNull(valueWrapper.getCollectionPathToValue());
    // case 2: failed with actual value
    when(expressionEvaluatorMock.evaluateUnaryExpression(any(), any(), any(Class.class))).thenReturn(ExpressionEvaluatorResult.ofFailed());
    valueWrapper = abstractRunnerHelper.getResultWrapper(String.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, String.class);
    assertFalse(valueWrapper.isValid());
    assertEquals(resultRaw, valueWrapper.getValue());
    assertNull(valueWrapper.getCollectionPathToValue());
    // case 3: failed without actual value (list)
    valueWrapper = abstractRunnerHelper.getResultWrapper(List.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, List.class);
    assertFalse(valueWrapper.getErrorMessage().isPresent());
    assertTrue(valueWrapper.getCollectionPathToValue().isEmpty());
    assertNull(valueWrapper.getValue());
    // case 4: failed without actual value (map)
    valueWrapper = abstractRunnerHelper.getResultWrapper(Map.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, Map.class);
    assertFalse(valueWrapper.getErrorMessage().isPresent());
    assertTrue(valueWrapper.getCollectionPathToValue().isEmpty());
    assertNull(valueWrapper.getValue());
    // case 5: failed with wrong value (list)
    ExpressionEvaluatorResult result = ExpressionEvaluatorResult.ofFailed(collectionWrongValue, collectionValuePath);
    when(expressionEvaluatorMock.evaluateUnaryExpression(any(), any(), any(Class.class))).thenReturn(result);
    valueWrapper = abstractRunnerHelper.getResultWrapper(List.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, List.class);
    assertFalse(valueWrapper.getErrorMessage().isPresent());
    assertEquals(1, valueWrapper.getCollectionPathToValue().size());
    assertEquals(collectionWrongValue, valueWrapper.getValue());
    // case 6: failed without actual value (map)
    valueWrapper = abstractRunnerHelper.getResultWrapper(Map.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, Map.class);
    assertFalse(valueWrapper.getErrorMessage().isPresent());
    assertEquals(1, valueWrapper.getCollectionPathToValue().size());
    assertEquals(collectionWrongValue, valueWrapper.getValue());
    // case 7: failed without wrong value (list)
    result = ExpressionEvaluatorResult.ofFailed(null, collectionValuePath);
    when(expressionEvaluatorMock.evaluateUnaryExpression(any(), any(), any(Class.class))).thenReturn(result);
    valueWrapper = abstractRunnerHelper.getResultWrapper(List.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, List.class);
    assertFalse(valueWrapper.getErrorMessage().isPresent());
    assertEquals(1, valueWrapper.getCollectionPathToValue().size());
    assertNull(valueWrapper.getValue());
    // case 8: failed without actual value (map)
    valueWrapper = abstractRunnerHelper.getResultWrapper(Map.class.getCanonicalName(), new FactMappingValue(), expressionEvaluatorMock, expectedResultRaw, resultRaw, Map.class);
    assertFalse(valueWrapper.getErrorMessage().isPresent());
    assertEquals(1, valueWrapper.getCollectionPathToValue().size());
    assertNull(valueWrapper.getValue());
    // case 9: failed with generic exception
    when(expressionEvaluatorMock.evaluateUnaryExpression(any(), any(), any(Class.class))).thenThrow(new IllegalArgumentException(genericErrorMessage));
    FactMappingValue expectedResult5 = new FactMappingValue();
    valueWrapper = abstractRunnerHelper.getResultWrapper(Map.class.getCanonicalName(), expectedResult5, expressionEvaluatorMock, expectedResultRaw, resultRaw, Map.class);
    assertEquals(genericErrorMessage, valueWrapper.getErrorMessage().get());
    assertEquals(genericErrorMessage, expectedResult5.getExceptionMessage());
}
Also used : ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) ExpressionEvaluatorResult(org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorResult) List(java.util.List) BaseExpressionEvaluator(org.drools.scenariosimulation.backend.expression.BaseExpressionEvaluator) ExpressionEvaluator(org.drools.scenariosimulation.backend.expression.ExpressionEvaluator) Map(java.util.Map) Test(org.junit.Test)

Example 3 with ValueWrapper

use of org.drools.scenariosimulation.backend.runner.model.ValueWrapper in project drools by kiegroup.

the class ConditionFilterTest method acceptTest.

@Test
public void acceptTest() {
    Function<Object, ValueWrapper> alwaysMatchFunction = ValueWrapper::of;
    FactMappingValue factMappingValue = new FactMappingValue(FactIdentifier.DESCRIPTION, ExpressionIdentifier.DESCRIPTION, "Test");
    ScenarioResult scenarioResult = new ScenarioResult(factMappingValue);
    ConditionFilter conditionFilter = new ConditionFilter(singletonList(new FactCheckerHandle(String.class, alwaysMatchFunction, scenarioResult)));
    Assert.assertFalse(conditionFilter.accept(1));
    Assert.assertTrue(conditionFilter.accept("String"));
    Function<Object, ValueWrapper> alwaysNotMatchFunction = object -> ValueWrapper.errorWithValidValue(null, null);
    ConditionFilter conditionFilterFail = new ConditionFilter(singletonList(new FactCheckerHandle(String.class, alwaysNotMatchFunction, scenarioResult)));
    Assert.assertFalse(conditionFilterFail.accept("String"));
}
Also used : ExpressionIdentifier(org.drools.scenariosimulation.api.model.ExpressionIdentifier) ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) FactIdentifier(org.drools.scenariosimulation.api.model.FactIdentifier) Test(org.junit.Test) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) Assert(org.junit.Assert) Function(java.util.function.Function) Collections.singletonList(java.util.Collections.singletonList) ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) ScenarioResult(org.drools.scenariosimulation.backend.runner.model.ScenarioResult) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) Test(org.junit.Test)

Example 4 with ValueWrapper

use of org.drools.scenariosimulation.backend.runner.model.ValueWrapper in project drools by kiegroup.

the class RuleScenarioRunnerHelperTest method createExtractorFunctionTest.

@Test
public void createExtractorFunctionTest() {
    String personName = "Test";
    FactMappingValue factMappingValue = new FactMappingValue(personFactIdentifier, firstNameGivenExpressionIdentifier, personName);
    Function<Object, ValueWrapper> extractorFunction = runnerHelper.createExtractorFunction(expressionEvaluator, factMappingValue, simulation.getScesimModelDescriptor());
    Person person = new Person();
    person.setFirstName(personName);
    assertTrue(extractorFunction.apply(person).isValid());
    person.setFirstName("OtherString");
    assertFalse(extractorFunction.apply(person).isValid());
    Function<Object, ValueWrapper> extractorFunction1 = runnerHelper.createExtractorFunction(expressionEvaluator, new FactMappingValue(personFactIdentifier, firstNameGivenExpressionIdentifier, null), simulation.getScesimModelDescriptor());
    ValueWrapper nullValue = extractorFunction1.apply(new Person());
    assertTrue(nullValue.isValid());
    assertNull(nullValue.getValue());
}
Also used : ValueWrapper(org.drools.scenariosimulation.backend.runner.model.ValueWrapper) FactMappingValue(org.drools.scenariosimulation.api.model.FactMappingValue) Person(org.drools.scenariosimulation.backend.model.Person) AbstractRuleCoverageTest(org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest) Test(org.junit.Test)

Aggregations

FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)4 ValueWrapper (org.drools.scenariosimulation.backend.runner.model.ValueWrapper)4 Test (org.junit.Test)4 ExpressionIdentifier (org.drools.scenariosimulation.api.model.ExpressionIdentifier)2 FactIdentifier (org.drools.scenariosimulation.api.model.FactIdentifier)2 BaseExpressionEvaluator (org.drools.scenariosimulation.backend.expression.BaseExpressionEvaluator)2 ExpressionEvaluator (org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)2 Collections.singletonList (java.util.Collections.singletonList)1 List (java.util.List)1 Map (java.util.Map)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Function (java.util.function.Function)1 ExpressionEvaluatorResult (org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorResult)1 AbstractRuleCoverageTest (org.drools.scenariosimulation.backend.fluent.AbstractRuleCoverageTest)1 Person (org.drools.scenariosimulation.backend.model.Person)1 ScenarioResult (org.drools.scenariosimulation.backend.runner.model.ScenarioResult)1 Assert (org.junit.Assert)1