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
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