Search in sources :

Example 1 with Result

use of org.omg.dmn.tck.runner.junit4.TestResult.Result in project tck by dmn-tck.

the class DroolsTCKTest method executeTest.

@Override
public TestResult executeTest(Description description, TestSuiteContext context, TestCases.TestCase testCase) {
    DroolsContext ctx = (DroolsContext) context;
    logger.info("Executing test '{} / {}'\n", description.getClassName(), description.getMethodName());
    DMNContext dmnctx = ctx.runtime.newContext();
    testCase.getInputNode().forEach(in -> {
        InputDataNode input = ctx.dmnmodel.getInputByName(in.getName());
        DecisionNode decision = ctx.dmnmodel.getDecisionByName(in.getName());
        if (input != null) {
            // normally data input from file, should be pointing at a InputData in the model
            dmnctx.set(in.getName(), parseValue(in, input));
        } else if (decision != null) {
            // the test case offers a pre-evaluated Decision
            dmnctx.set(in.getName(), parseValue(in, decision));
        } else {
            logger.warn("Override input name {} with value {}", in.getName(), in);
            dmnctx.set(in.getName(), parseType(in, REGISTRY.unknown()));
        }
    });
    DMNContext resultctx = dmnctx;
    List<String> failures = new ArrayList<>();
    for (TestCases.TestCase.ResultNode rn : testCase.getResultNode()) {
        try {
            String name = rn.getName();
            DMNResult dmnResult = null;
            if (testCase.getType() == TestCaseType.DECISION_SERVICE) {
                dmnResult = ctx.runtime.evaluateDecisionService(ctx.dmnmodel, resultctx, testCase.getInvocableName());
            } else {
                dmnResult = ctx.runtime.evaluateByName(ctx.dmnmodel, resultctx, name);
            }
            if (!dmnResult.getMessages().isEmpty()) {
                logger.info("Messages: \n-----\n{}\n-----\n", dmnResult.getMessages().stream().map(m -> m.toString()).collect(Collectors.joining("\n")));
            }
            resultctx = dmnResult.getContext();
            Object expected = parseValue(rn, ctx.dmnmodel.getDecisionByName(name));
            Object actual = resultctx.get(name);
            if (rn.isErrorResult()) {
                for (DMNMessage msg : dmnResult.getMessages(DMNMessage.Severity.ERROR)) {
                    logger.info("TEST CASE is error Result, message reported is to be expected: {}", msg);
                }
            } else {
                if (expected == null && actual == null) {
                    for (DMNMessage msg : dmnResult.getMessages(DMNMessage.Severity.ERROR)) {
                        logger.warn("TCK value comparison is correct, but TCK test case '{}' might be missing errorResult flag?", name);
                    }
                } else if (dmnResult.hasErrors()) {
                    for (DMNMessage msg : dmnResult.getMessages(DMNMessage.Severity.ERROR)) {
                        failures.add(msg.toString());
                    }
                }
            }
            if (!isEquals(expected, actual)) {
                failures.add("FAILURE: '" + name + "' expected='" + expected + "' but found='" + actual + "'");
            }
        } catch (Throwable t) {
            failures.add("FAILURE: unnexpected exception executing test case '" + description.getClassName() + " / " + description.getMethodName() + "': " + t.getClass().getName());
            logger.error("FAILURE: unnexpected exception executing test case '{} / {}'", description.getClassName(), description.getMethodName(), t);
        }
    }
    // DMNResult dmnResult = ctx.runtime.evaluateAll( ctx.dmnmodel, dmnctx );
    logger.info("Result context: {}\n", resultctx);
    // if( ! dmnResult.getMessages().isEmpty() ) {
    // logger.info( "Messages: \n-----\n{}-----\n", dmnResult.getMessages().stream().map( m -> m.toString() ).collect( Collectors.joining( "\n" ) ) );
    // }
    // 
    // List<String> failures = new ArrayList<>();
    // if( dmnResult.hasErrors() ) {
    // for( DMNMessage msg : dmnResult.getMessages( DMNMessage.Severity.ERROR ) ) {
    // failures.add( msg.toString() );
    // }
    // }
    // testCase.getResultNode().forEach( rn -> {
    // try {
    // String name = rn.getName();
    // Object expected = parseValue( rn, ctx.dmnmodel.getDecisionByName( name ) );
    // Object actual = resultctx.get( name );
    // if( ! isEquals( expected, actual ) ) {
    // failures.add( "FAILURE: '"+name+"' expected='"+expected+"' but found='"+actual+"'" );
    // }
    // } catch ( Throwable t ) {
    // failures.add( "FAILURE: unnexpected exception executing test case '"+description.getClassName()+" / " +description.getMethodName()+"': "+t.getClass().getName() );
    // logger.error( "FAILURE: unnexpected exception executing test case '{} / {}'", description.getClassName(), description.getMethodName(), t );
    // }
    // } );
    TestResult.Result r = !failures.isEmpty() ? TestResult.Result.ERROR : TestResult.Result.SUCCESS;
    return new TestResult(r, failures.stream().collect(Collectors.joining("\n")));
}
Also used : DMNResult(org.kie.dmn.api.core.DMNResult) DMNContext(org.kie.dmn.api.core.DMNContext) ArrayList(java.util.ArrayList) TestResult(org.omg.dmn.tck.runner.junit4.TestResult) DecisionNode(org.kie.dmn.api.core.ast.DecisionNode) DMNMessage(org.kie.dmn.api.core.DMNMessage) InputDataNode(org.kie.dmn.api.core.ast.InputDataNode)

Example 2 with Result

use of org.omg.dmn.tck.runner.junit4.TestResult.Result in project tck by dmn-tck.

the class JDMNTckTest method executeTest.

@Override
public TestResult executeTest(Description description, TestSuiteContext context, TestCase testCase) {
    List<String> failures = new ArrayList<>();
    List<String> exceptions = new ArrayList<>();
    try {
        JDMNTestContext gsContext = (JDMNTestContext) context;
        BasicDMNToJavaTransformer basicTransformer = gsContext.getBasicToJavaTransformer();
        StandardFEELLib lib = gsContext.getLib();
        DMNInterpreter interpreter = gsContext.getInterpreter();
        TCKUtil tckUtil = new TCKUtil(basicTransformer, lib);
        TestCases testCases = gsContext.getTestCases();
        for (ResultNode res : testCase.getResultNode()) {
            String testLocation = String.format("Unexpected result in test for model '%s', TestCase.id='%s', ResultNode.name='%s'.", testCases.getModelName(), testCase.getId(), res.getName());
            Object expectedValue = null;
            Result actualResult = null;
            Object actualValue = null;
            try {
                expectedValue = tckUtil.expectedValue(testCases, testCase, res);
                actualResult = tckUtil.evaluate(interpreter, testCases, testCase, res);
                actualValue = Result.value(actualResult);
                if (!isEquals(expectedValue, actualValue)) {
                    String errorMessage = String.format("%s ResultNode '%s' output mismatch, expected '%s' actual '%s'", testLocation, res.getName(), expectedValue, actualValue);
                    failures.add(errorMessage);
                }
                if (!IGNORE_ERROR_FLAG) {
                    String errorFlagMessage = String.format("%s ResultNode '%s' error flag mismatch", testLocation, res.getName());
                    if (!isEquals(res.isErrorResult(), actualResult.hasErrors())) {
                        failures.add(errorFlagMessage);
                    }
                }
            } catch (Throwable e) {
                String stackTrace = ExceptionUtils.getStackTrace(e);
                LOGGER.error(stackTrace);
                String errorMessage = String.format("%s ResultNode '%s' output mismatch, expected '%s' actual '%s'", testLocation, res.getName(), expectedValue, actualValue);
                exceptions.add(errorMessage + ". Exception thrown while testing");
            }
        }
    } catch (Throwable e) {
        exceptions.add(e.getMessage());
    }
    TestResult.Result r = TestResult.Result.SUCCESS;
    String message = "";
    if (!failures.isEmpty()) {
        r = TestResult.Result.ERROR;
        message = String.join("\n", failures);
    }
    if (!exceptions.isEmpty()) {
        r = TestResult.Result.ERROR;
        message = String.join("\n", exceptions);
    }
    return new TestResult(r, message);
}
Also used : BasicDMNToJavaTransformer(com.gs.dmn.transformation.basic.BasicDMNToJavaTransformer) ResultNode(org.omg.dmn.tck.marshaller._20160719.TestCases.TestCase.ResultNode) ArrayList(java.util.ArrayList) TestResult(org.omg.dmn.tck.runner.junit4.TestResult) Result(com.gs.dmn.runtime.interpreter.Result) TestResult(org.omg.dmn.tck.runner.junit4.TestResult) TestCases(org.omg.dmn.tck.marshaller._20160719.TestCases) TCKUtil(com.gs.dmn.tck.TCKUtil) DMNInterpreter(com.gs.dmn.runtime.interpreter.DMNInterpreter) StandardFEELLib(com.gs.dmn.feel.lib.StandardFEELLib)

Aggregations

ArrayList (java.util.ArrayList)2 TestResult (org.omg.dmn.tck.runner.junit4.TestResult)2 StandardFEELLib (com.gs.dmn.feel.lib.StandardFEELLib)1 DMNInterpreter (com.gs.dmn.runtime.interpreter.DMNInterpreter)1 Result (com.gs.dmn.runtime.interpreter.Result)1 TCKUtil (com.gs.dmn.tck.TCKUtil)1 BasicDMNToJavaTransformer (com.gs.dmn.transformation.basic.BasicDMNToJavaTransformer)1 DMNContext (org.kie.dmn.api.core.DMNContext)1 DMNMessage (org.kie.dmn.api.core.DMNMessage)1 DMNResult (org.kie.dmn.api.core.DMNResult)1 DecisionNode (org.kie.dmn.api.core.ast.DecisionNode)1 InputDataNode (org.kie.dmn.api.core.ast.InputDataNode)1 TestCases (org.omg.dmn.tck.marshaller._20160719.TestCases)1 ResultNode (org.omg.dmn.tck.marshaller._20160719.TestCases.TestCase.ResultNode)1