com.imsweb.validation.entities.RuleTest Maven / Gradle / Ivy
/*
* Copyright (C) 2011 Information Management Services, Inc.
*/
package com.imsweb.validation.entities;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.StringReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import groovy.lang.Binding;
import groovy.lang.Script;
import com.imsweb.validation.TestingContextFunctions;
import com.imsweb.validation.ValidationEngine;
import com.imsweb.validation.ValidatorContextFunctions;
import com.imsweb.validation.ValidatorServices;
/**
* A single rule test (a Groovy script).
*
* Created on Aug 7, 2011 by depryf
*/
public class RuleTest {
/** Tested Rule ID */
protected String _testedRuleId;
/** Testing Script text */
protected String _scriptText;
/** Translated Script text (we dynamically add line number to the assertions) */
protected String _translatedScriptText;
/** Assertions (key is line number, value is true if it's an assertPass, false if it's an assertFail) */
protected Map _assertions;
/**
* Created on Aug 8, 2011 by depryf
* @param testedRuleId tested Rule ID
*/
public void setTestedRuleId(String testedRuleId) {
_testedRuleId = testedRuleId;
}
/** @param scriptText The sScriptText to set. */
public void setScriptText(String scriptText) throws IOException {
this._scriptText = scriptText;
this._translatedScriptText = scriptText;
this._assertions = new HashMap<>();
if (scriptText != null && !scriptText.isEmpty()) {
StringBuilder buf = new StringBuilder();
LineNumberReader reader = new LineNumberReader(new StringReader(scriptText));
String line = reader.readLine();
while (line != null) {
int count = StringUtils.countMatches(line, "Testing.assert");
if (count > 1)
throw new IOException("Line " + reader.getLineNumber() + " contains several assertions!");
if (count > 0) {
int lineNum = reader.getLineNumber(); // line num must be 1-base, but lineNumber is already 1-based...
if (line.contains("Testing.assertPass")) {
line = line.replace("Testing.assertPass(", "Testing.assertPass(" + lineNum + ", ");
this._assertions.put(lineNum, Boolean.TRUE);
}
else {
line = line.replace("Testing.assertFail(", "Testing.assertFail(" + lineNum + ", ");
this._assertions.put(lineNum, Boolean.FALSE);
}
}
buf.append(line).append("\n");
line = reader.readLine();
}
this._translatedScriptText = buf.toString();
}
}
/** @return Returns the testedRuleId. */
public String getTestedRuleId() {
return _testedRuleId;
}
/** @return Returns the originalScriptText. */
public String getScriptText() {
return _scriptText;
}
/** @return Returns the assertions. */
public Map getAssertions() {
return _assertions;
}
/**
* Executes the tests using the default testing context object (which supports the NAACCR line notation).
*
* Created on Aug 8, 2011 by depryf
* @return a map of Integer (line number) -> list of RuleTestResult, the list indexes determines the assertion indexes
* @throws Exception
*/
public Map> executeTest() throws Exception {
return executeTest(new TestingContextFunctions(this, null), null);
}
/**
* Executes the tests using the default testing context object (which supports the NAACCR line notation).
*
* Created on Aug 8, 2011 by depryf
* @param rule a Rule to use for the test (useful for testing modified rules or new ones)
* @return a map of Integer (line number) -> list of RuleTestResult, the list indexes determines the assertion indexes
* @throws Exception
*/
public Map> executeTest(Rule rule) throws Exception {
return executeTest(new TestingContextFunctions(this, rule), rule);
}
/**
* Executes the tests using the provided testing context object.
*
* Created on Aug 8, 2011 by depryf
* @param context a TestingContextFunctions, can be nulls
* @return a map of Integer (line number) -> list of RuleTestResult, the list indexes determines the assertion indexes
* @throws Exception
*/
public Map> executeTest(TestingContextFunctions context) throws Exception {
return executeTest(context, null);
}
/**
* Executes the tests using the provided testing context object.
*
* Created on Aug 8, 2011 by depryf
* @param context a TestingContextFunctions, can be nulls
* @param rule a Rule to use for the test (useful for testing modified rules or new ones)
* @return a map of Integer (line number) -> list of RuleTestResult, the list indexes determines the assertion indexes
* @throws Exception
*/
public Map> executeTest(TestingContextFunctions context, Rule rule) throws Exception {
// better safe than sorry
if (context == null)
context = new TestingContextFunctions(this, rule);
// create Groovy script and set its binding
Script script = ValidatorServices.getInstance().compileExpression(_translatedScriptText);
Binding binding = new Binding();
binding.setVariable(ValidationEngine.VALIDATOR_TESTING_FUNCTIONS_KEY, context);
binding.setVariable(ValidationEngine.VALIDATOR_FUNCTIONS_KEY, ValidatorContextFunctions.getInstance());
script.setBinding(binding);
// execute the script
script.run();
return context.getTestsResults();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy