From e6c6308c3e069fce55d7f324f0922c15fdd402e5 Mon Sep 17 00:00:00 2001 From: rma-rripken <89810919+rma-rripken@users.noreply.github.com> Date: Mon, 10 Jun 2024 10:25:29 -0700 Subject: [PATCH] Adding a float matcher --- .../cwms/cda/api/LevelsControllerTestIT.java | 7 +- .../src/test/java/helpers/FloatCloseTo.java | 69 +++++++++++++++++++ 2 files changed, 72 insertions(+), 4 deletions(-) create mode 100644 cwms-data-api/src/test/java/helpers/FloatCloseTo.java diff --git a/cwms-data-api/src/test/java/cwms/cda/api/LevelsControllerTestIT.java b/cwms-data-api/src/test/java/cwms/cda/api/LevelsControllerTestIT.java index 858ae7501..ab12ac668 100644 --- a/cwms-data-api/src/test/java/cwms/cda/api/LevelsControllerTestIT.java +++ b/cwms-data-api/src/test/java/cwms/cda/api/LevelsControllerTestIT.java @@ -47,6 +47,7 @@ import java.util.TreeMap; import static cwms.cda.api.Controllers.*; +import static helpers.FloatCloseTo.floatCloseTo; import static io.restassured.RestAssured.given; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.*; @@ -329,8 +330,7 @@ void test_get_all_location_level() throws Exception { assertThat(response.path("levels[0].level-units-id"),equalTo("ac-ft")); assertThat(response.path("levels[0].level-date"),equalTo("2023-06-01T07:00:00Z")); assertThat(response.path("levels[0].duration-id"),equalTo("1Day")); - actual0 = Float.valueOf((float) response.path("levels[0].constant-value")).doubleValue(); - assertThat(actual0, closeTo(1.0, 0.01)); + assertThat(response.path("levels[0].constant-value"), floatCloseTo(1.0, 0.01)); assertThat(response.path("levels[1].office-id"),equalTo(OFFICE)); assertThat(response.path("levels[1].location-level-id"),equalTo(levelId2)); @@ -340,8 +340,7 @@ void test_get_all_location_level() throws Exception { assertThat(response.path("levels[1].level-units-id"),equalTo("ac-ft")); assertThat(response.path("levels[1].level-date"),equalTo("2023-06-01T07:00:00Z")); assertThat(response.path("levels[1].duration-id"),equalTo("1Day")); - actual1 = Float.valueOf((float) response.path("levels[1].constant-value")).doubleValue(); - assertThat(actual1, closeTo(2.0, 0.01)); + assertThat(response.path("levels[1].constant-value"), floatCloseTo(2.0, 0.01)); } } diff --git a/cwms-data-api/src/test/java/helpers/FloatCloseTo.java b/cwms-data-api/src/test/java/helpers/FloatCloseTo.java new file mode 100644 index 000000000..28db04e47 --- /dev/null +++ b/cwms-data-api/src/test/java/helpers/FloatCloseTo.java @@ -0,0 +1,69 @@ +package helpers; + +import org.hamcrest.Description; +import org.hamcrest.Factory; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeMatcher; + +/** + * A matcher that tests that an examined float is a number equal to a value within some range of + * acceptable error. + * RestAssured returns Floats from JSON paths and the hamcrest closeTo matcher only works with + * doubles. This matcher works just like the Hamcrest matcher but operates on float inputs. + * A slightly different name (floatCloseTo) is used to avoid confusion with the Hamcrest closeTo. + *

+ * For example: + *

assertThat(1.03f, is(floatCloseTo(1.0, 0.03)))
+ */ +public class FloatCloseTo extends TypeSafeMatcher { + private final double delta; + private final double value; + + public FloatCloseTo(double value, double error) { + this.delta = error; + this.value = value; + } + + @Override + public boolean matchesSafely(Float item) { + return actualDelta(item) <= 0.0; + } + + @Override + public void describeMismatchSafely(Float item, Description mismatchDescription) { + mismatchDescription.appendValue(item) + .appendText(" differed by ") + .appendValue(actualDelta(item)); + } + + @Override + public void describeTo(Description description) { + description.appendText("a numeric value within ") + .appendValue(delta) + .appendText(" of ") + .appendValue(value); + } + + private double actualDelta(Float item) { + return (Math.abs((item - value)) - delta); + } + + /** + * Creates a matcher of {@link Float}s that matches when an examined float is equal + * to the specified operand, within a range of +/- error. + *

+ * For example: + *

assertThat(1.03f, is(floatCloseTo(1.0, 0.03)))
+ * + * @param operand + * the expected value of matching doubles + * @param error + * the delta (+/-) within which matches will be allowed + */ + @Factory + public static Matcher floatCloseTo(double operand, double error) { + return new FloatCloseTo(operand, error); + } + + +}