Search in sources :

Example 1 with ExpressionEvaluatorResult

use of org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorResult 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)

Aggregations

List (java.util.List)1 Map (java.util.Map)1 FactMappingValue (org.drools.scenariosimulation.api.model.FactMappingValue)1 BaseExpressionEvaluator (org.drools.scenariosimulation.backend.expression.BaseExpressionEvaluator)1 ExpressionEvaluator (org.drools.scenariosimulation.backend.expression.ExpressionEvaluator)1 ExpressionEvaluatorResult (org.drools.scenariosimulation.backend.expression.ExpressionEvaluatorResult)1 ValueWrapper (org.drools.scenariosimulation.backend.runner.model.ValueWrapper)1 Test (org.junit.Test)1