From 8c9ac3c68d4fbd5b4d9403c06d0c9c55f6a5d51d Mon Sep 17 00:00:00 2001 From: Michael Froh Date: Wed, 29 May 2024 17:57:25 +0000 Subject: [PATCH] Add basic type conversion for YAML tests Our yaml rest tests parse expected values based on basic yaml parsing rules, which don't draw a distinction between float and double values. If there's a mismatch between the types of the expected and actual values in an assertion, we should try parsing the actual value to the expected type. For now, I just added support for the 32- and 64-bit integer and floating point types. Signed-off-by: Michael Froh --- .../test/rest/yaml/section/Assertion.java | 41 ++++++++++++++++++- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/test/framework/src/main/java/org/opensearch/test/rest/yaml/section/Assertion.java b/test/framework/src/main/java/org/opensearch/test/rest/yaml/section/Assertion.java index b9cbaacdf8873..cacb8dd8b4ec5 100644 --- a/test/framework/src/main/java/org/opensearch/test/rest/yaml/section/Assertion.java +++ b/test/framework/src/main/java/org/opensearch/test/rest/yaml/section/Assertion.java @@ -37,6 +37,8 @@ import java.io.IOException; import java.util.Map; +import static org.junit.Assert.fail; + /** * Base class for executable sections that hold assertions */ @@ -73,10 +75,45 @@ protected final Object resolveExpectedValue(ClientYamlTestExecutionContext execu } protected final Object getActualValue(ClientYamlTestExecutionContext executionContext) throws IOException { + Object actualValue; if (executionContext.stash().containsStashedValue(field)) { - return executionContext.stash().getValue(field); + actualValue = executionContext.stash().getValue(field); + } else { + actualValue = executionContext.response(field); + } + // Expected/actual values have the same type. Can just return. + if (expectedValue.getClass().isAssignableFrom(actualValue.getClass())) { + return actualValue; + } + if (actualValue instanceof Number && expectedValue instanceof Number) { + if (expectedValue instanceof Float) { + return Float.parseFloat(actualValue.toString()); + } else if (expectedValue instanceof Double) { + return Double.parseDouble(actualValue.toString()); + } else if (expectedValue instanceof Integer) { + return Integer.parseInt(actualValue.toString()); + } else if (expectedValue instanceof Long) { + return Long.parseLong(actualValue.toString()); + } + } + // Force a class cast exception here, so developers can flesh out the above logic as needed. + try { + expectedValue.getClass().cast(actualValue); + } catch (ClassCastException e) { + fail( + "Type mismatch: Expected value (" + + expectedValue + + ") has type " + + expectedValue.getClass() + + ". " + + "Actual value (" + + actualValue + + ") has type " + + actualValue.getClass() + + "." + ); } - return executionContext.response(field); + return actualValue; } @Override