com.imsweb.validation.functions.TestingContextFunctions Maven / Gradle / Ivy
/*
* Copyright (C) 2011 Information Management Services, Inc.
*/
package com.imsweb.validation.functions;
import com.imsweb.validation.ValidationEngine;
import com.imsweb.validation.ValidationException;
import com.imsweb.validation.entities.Rule;
import com.imsweb.validation.entities.RuleFailure;
import com.imsweb.validation.entities.RuleTest;
import com.imsweb.validation.entities.RuleTestResult;
import com.imsweb.validation.entities.SimpleMapValidatable;
import com.imsweb.validation.entities.SimpleNaaccrLinesValidatable;
import com.imsweb.validation.entities.Validatable;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeMap;
/**
* Context available to the testing framework. The testing Groovy scripts can access the methdos of this context
* using the "Testing." prefix. The only methods that should be accessed are the assertPass() and assertFail()
* methods. The getTestsResults() method is used by the testing framework to gather the assertion results and
* provide them back to the application.
*
* This class supports the NAACCR line notation (line.primarySite), which is pretty standard. For applications
* requiring to support other data types, this class should be extended (in particular the createValidatable()
* method, and the extended version should be provided to the ruleTest.executeTest() method.
*
* Created on Aug 8, 2011 by depryf
* @author depryf
*/
public class TestingContextFunctions {
/**
* Test assertion types.
*
* Created on Oct 2, 2011 by Fabian
* @author Fabian
*/
public enum AssertionType {
/**
* Use this when expecting the edit to pass
*/
PASS,
/**
* Use this when expecting the edit to fail
*/
FAIL
}
// tested rule ID
protected String _ruleId;
// tested rule, if not provided, it will be fetched from the referenced engine using the tested rule ID
protected Rule _rule;
// map of tests, key is the line number in the script, value is a list of tests (list will have more then one element in repetitions like for loops)
protected Map> _tests;
// referenced engine, if not provided, the default instance will be used
protected ValidationEngine _engine;
/**
* Constructor.
*
* Created on Oct 2, 2011 by Fabian
*/
public TestingContextFunctions(RuleTest test) {
this(test, null);
}
/**
* Constructor.
*
* Created on Oct 2, 2011 by Fabian
* @param test RuleTest, cannot be null
* @param rule tested Rule, can be null
*/
public TestingContextFunctions(RuleTest test, Rule rule) {
this(test, rule, ValidationEngine.getInstance());
}
/**
* Constructor.
*
* Created on Oct 5, 2018 by depryf
* @param test RuleTest, cannot be null
* @param rule tested Rule, can be null
* @param engine referenced ValidationEngine
*/
public TestingContextFunctions(RuleTest test, Rule rule, ValidationEngine engine) {
_ruleId = test.getTestedRuleId();
_rule = rule;
_tests = new TreeMap<>();
_engine = engine;
}
/**
* Asserts that a given test passes.
*
* Created on Jun 6, 2011 by murphyr
* @param lineNumber test line number
* @param dataObj test inputs
*/
public void assertPass(int lineNumber, Object dataObj) {
assertPass(lineNumber, dataObj, null);
}
/**
* Asserts that a given test passes.
*
* Created on Jul 21, 2011 by murphyr
* @param lineNumber test line number
* @param dataObj test inputs
* @param context extra context for the test
*/
public void assertPass(int lineNumber, Object dataObj, Map context) {
// redirect the output
OutputStream output = new OutputStream() {
private StringBuilder _buf = new StringBuilder();
@Override
public void write(int b) {
_buf.append((char)b);
}
@Override
public String toString() {
return _buf.toString();
}
};
if (context == null)
context = new HashMap<>();
context.put("out", output);
try {
Collection results = runTest(dataObj, context);
// a test runs for one edit, so there should be 0 or 1 failure (anything else is ignored)
RuleFailure failure = results.isEmpty() ? null : results.iterator().next();
// it's a success if there was no failure
boolean success = failure == null;
// add testing result
insertTestingResult(lineNumber, AssertionType.PASS, success, failure, dataObj, context, null, null, output);
}
catch (ValidationException e) {
insertTestingResult(lineNumber, AssertionType.PASS, false, null, dataObj, context, e, null, output);
}
}
/**
* Asserts that a given test fails.
*
* Created on Jun 6, 2011 by murphyr
* @param lineNumber test line number
* @param dataObj test inputs
* @param failingProperties array of properties that needs to appear in the list of failing properties for this failure
*/
public void assertFail(int lineNumber, Object dataObj, String... failingProperties) {
assertFail(lineNumber, dataObj, null, failingProperties);
}
/**
* Asserts that a given test fails.
*
* Created on Jul 21, 2011 by murphyr
* @param lineNumber test line number
* @param dataObj test inputs
* @param failingProperties array of properties that needs to appear in the list of failing properties for this failure
* @param context extra context for the test
*/
@SuppressWarnings("ThrowableResultOfMethodCallIgnored")
public void assertFail(int lineNumber, Object dataObj, Map context, String... failingProperties) {
Set props = new HashSet<>();
if (failingProperties != null)
props.addAll(Arrays.asList(failingProperties));
// redirect the output
OutputStream output = new OutputStream() {
private StringBuilder _buf = new StringBuilder();
@Override
public void write(int b) {
_buf.append((char)b);
}
@Override
public String toString() {
return _buf.toString();
}
};
if (context == null)
context = new HashMap<>();
context.put("out", output);
try {
Collection results = runTest(dataObj, context);
// a test runs for one edit, so there should be 0 or 1 failure (anything else is ignored)
RuleFailure failure = results.isEmpty() ? null : results.iterator().next();
// it's a success if there was a failure, but not an exception
boolean success = failure != null && failure.getGroovyException() == null && failure.getProperties().containsAll(props);
// add testing result
insertTestingResult(lineNumber, AssertionType.FAIL, success, failure, dataObj, context, null, props, output);
}
catch (ValidationException e) {
insertTestingResult(lineNumber, AssertionType.FAIL, false, null, dataObj, context, e, props, output);
}
}
/**
* Creation method.
*
* Created on Nov 18, 2011 by depryf
* @return created entity
*/
public List © 2015 - 2025 Weber Informatics LLC | Privacy Policy