use of org.batfish.client.Command.TestComparisonMode in project batfish by batfish.
the class Client method test.
private boolean test(List<String> options, List<String> parameters) throws IOException {
boolean failingTest = false;
boolean missingReferenceFile = false;
int testCommandIndex = 1;
if (!isValidArgument(options, parameters, 1, 2, Integer.MAX_VALUE, Command.TEST)) {
return false;
}
TestComparisonMode comparisonMode = TestComparisonMode.COMPAREANSWER;
if (!options.isEmpty()) {
comparisonMode = // remove '-'
TestComparisonMode.valueOf(options.get(0).substring(1).toUpperCase());
}
if (parameters.get(testCommandIndex).equals(FLAG_FAILING_TEST)) {
testCommandIndex++;
failingTest = true;
}
String referenceFileName = parameters.get(0);
String[] testCommand = parameters.subList(testCommandIndex, parameters.size()).toArray(new String[0]);
_logger.debugf("Ref file is %s. \n", referenceFileName, parameters.size());
_logger.debugf("Test command is %s\n", Arrays.toString(testCommand));
File referenceFile = new File(referenceFileName);
if (!referenceFile.exists()) {
_logger.errorf("Reference file does not exist: %s\n", referenceFileName);
missingReferenceFile = true;
}
// Delete any existing testout filename before running this test.
Path failedTestoutPath = Paths.get(referenceFile + ".testout");
CommonUtil.deleteIfExists(failedTestoutPath);
File testoutFile = Files.createTempFile("test", "out").toFile();
testoutFile.deleteOnExit();
FileWriter testoutWriter = new FileWriter(testoutFile);
boolean testCommandSucceeded = processCommand(testCommand, testoutWriter);
testoutWriter.close();
String testOutput = CommonUtil.readFile(Paths.get(testoutFile.getAbsolutePath()));
boolean testPassed = false;
if (failingTest) {
if (!testCommandSucceeded) {
// Command failed in the client.
testPassed = true;
} else {
try {
Answer testAnswer = BatfishObjectMapper.mapper().readValue(testOutput, Answer.class);
testPassed = (testAnswer.getStatus() == AnswerStatus.FAILURE);
} catch (JsonProcessingException e) {
// pass here and let the test fail.
}
}
} else if (testCommandSucceeded) {
try {
if (TestComparisonMode.RAW != comparisonMode) {
Answer testAnswer = BatfishObjectMapper.mapper().readValue(testOutput, Answer.class);
testOutput = getTestComparisonString(testAnswer, comparisonMode);
}
if (!missingReferenceFile) {
String referenceOutput = CommonUtil.readFile(Paths.get(referenceFileName));
if (referenceOutput.equals(testOutput)) {
testPassed = true;
}
}
} catch (JsonProcessingException e) {
_logger.errorf("Error deserializing answer %s: %s\n", testOutput, ExceptionUtils.getStackTrace(e));
} catch (Exception e) {
_logger.errorf("Exception in comparing test results: %s\n", ExceptionUtils.getStackTrace(e));
}
}
StringBuilder sb = new StringBuilder();
sb.append("'" + testCommand[0]);
for (int i = 1; i < testCommand.length; i++) {
sb.append(" " + testCommand[i]);
}
sb.append("'");
String testCommandText = sb.toString();
_logger.outputf("Test [%s]: %s %s: %s\n", comparisonMode, testCommandText, failingTest ? "results in error as expected" : "matches " + referenceFileName, testPassed ? "Pass" : "Fail");
if (!testPassed) {
CommonUtil.writeFile(failedTestoutPath, testOutput);
_logger.outputf("Copied output to %s\n", failedTestoutPath);
}
return true;
}
Aggregations