Skip to content

Commit

Permalink
CWDB-225 - Added unit test for queryParamAsDouble in Controllers clas…
Browse files Browse the repository at this point in the history
…s. Made descriptions of date/timezones consistent.
  • Loading branch information
rma-bryson committed Oct 17, 2024
1 parent 9017668 commit bad6ba2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
3 changes: 0 additions & 3 deletions cwms-data-api/src/main/java/cwms/cda/api/Controllers.java
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,6 @@ public final class Controllers {
public static final String ISSUE_DATE = "issue-date";
public static final String LOCATION_KIND_LIKE = "location-kind-like";
public static final String LOCATION_TYPE_LIKE = "location-type-like";

public static final String MIN_DATE = "min-date";
public static final String MAX_DATE = "max-date";
public static final String MIN_NUMBER = "min-number";
public static final String MAX_NUMBER = "max-number";
public static final String MIN_HEIGHT = "min-height";
Expand Down
44 changes: 32 additions & 12 deletions cwms-data-api/src/main/java/cwms/cda/api/MeasurementController.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,19 @@
import static com.codahale.metrics.MetricRegistry.name;
import com.codahale.metrics.Timer;
import static cwms.cda.api.Controllers.AGENCY;
import static cwms.cda.api.Controllers.BEGIN;
import static cwms.cda.api.Controllers.CREATE;
import static cwms.cda.api.Controllers.DATE_FORMAT;
import static cwms.cda.api.Controllers.DELETE;
import static cwms.cda.api.Controllers.EXAMPLE_DATE;
import static cwms.cda.api.Controllers.FAIL_IF_EXISTS;
import static cwms.cda.api.Controllers.GET_ALL;
import static cwms.cda.api.Controllers.GET_ONE;
import static cwms.cda.api.Controllers.ID_MASK;
import static cwms.cda.api.Controllers.LOCATION_ID;
import static cwms.cda.api.Controllers.MAX_DATE;
import static cwms.cda.api.Controllers.END;
import static cwms.cda.api.Controllers.MAX_FLOW;
import static cwms.cda.api.Controllers.MAX_HEIGHT;
import static cwms.cda.api.Controllers.MIN_DATE;
import static cwms.cda.api.Controllers.MIN_FLOW;
import static cwms.cda.api.Controllers.MIN_HEIGHT;
import static cwms.cda.api.Controllers.NOT_SUPPORTED_YET;
Expand Down Expand Up @@ -98,9 +100,18 @@ private Timer.Context markAndTime(String subject) {
@OpenApiParam(name = ID_MASK, description = "Location id mask for filtering measurements. Use null to retrieve measurements for all locations."),
@OpenApiParam(name = MIN_NUMBER, description = "Minimum measurement number-id for filtering measurements."),
@OpenApiParam(name = MAX_NUMBER, description = "Maximum measurement number-id for filtering measurements."),
@OpenApiParam(name = MIN_DATE, description = "Minimum date-time for filtering measurements in ISO-8601 format."),
@OpenApiParam(name = MAX_DATE, description = "Maximum date-time for filtering measurements in ISO-8601 format."),
@OpenApiParam(name = TIMEZONE, description = "Timezone for the date range."),
@OpenApiParam(name = BEGIN, description = "The start of the time "
+ "window to delete. The format for this field is ISO 8601 extended, with "
+ "optional offset and timezone, i.e., '" + DATE_FORMAT + "', e.g., '"
+ EXAMPLE_DATE + "'."),
@OpenApiParam(name = END, description = "The end of the time "
+ "window to delete.The format for this field is ISO 8601 extended, with "
+ "optional offset and timezone, i.e., '" + DATE_FORMAT + "', e.g., '"
+ EXAMPLE_DATE + "'."),
@OpenApiParam(name = TIMEZONE, description = "This field specifies a default timezone "
+ "to be used if the format of the " + BEGIN + "and " + END
+ " parameters do not include offset or time zone information. "
+ "Defaults to UTC."),
@OpenApiParam(name = MIN_HEIGHT, description = "Minimum height for filtering measurements."),
@OpenApiParam(name = MAX_HEIGHT, description = "Maximum height for filtering measurements."),
@OpenApiParam(name = MIN_FLOW, description = "Minimum flow for filtering measurements."),
Expand Down Expand Up @@ -128,8 +139,8 @@ public void getAll(@NotNull Context ctx) {
String officeId = ctx.queryParam(OFFICE_MASK);
String locationId = ctx.queryParam(ID_MASK);
String unitSystem = ctx.queryParamAsClass(UNIT_SYSTEM, String.class).getOrDefault(UnitSystem.EN.value());
Instant minDate = queryParamAsInstant(ctx, MIN_DATE);
Instant maxDate = queryParamAsInstant(ctx, MAX_DATE);
Instant minDate = queryParamAsInstant(ctx, BEGIN);
Instant maxDate = queryParamAsInstant(ctx, END);
String minNum = ctx.queryParam(MIN_NUMBER);
String maxNum = ctx.queryParam(MAX_NUMBER);
Number minHeight = queryParamAsDouble(ctx, MIN_HEIGHT);
Expand Down Expand Up @@ -217,9 +228,18 @@ public void update(@NotNull Context ctx, @NotNull String locationId) {
@OpenApiParam(name = OFFICE, required = true, description = "Specifies the office of the measurements to delete"),
@OpenApiParam(name = MIN_NUMBER, description = "Specifies the min number-id of the measurement to delete."),
@OpenApiParam(name = MAX_NUMBER, description = "Specifies the max number-id of the measurement to delete."),
@OpenApiParam(name = MIN_DATE, description = "Specifies the minimum date (in ISO-8601 format) of the measurement to delete."),
@OpenApiParam(name = MAX_DATE, description = "Specifies the maximum date (in ISO-8601 format) of the measurement to delete."),
@OpenApiParam(name = TIMEZONE, description = "Specifies the timezone of the date range.")
@OpenApiParam(name = BEGIN, description = "The start of the time "
+ "window to delete. The format for this field is ISO 8601 extended, with "
+ "optional offset and timezone, i.e., '" + DATE_FORMAT + "', e.g., '"
+ EXAMPLE_DATE + "'."),
@OpenApiParam(name = END, description = "The end of the time "
+ "window to delete.The format for this field is ISO 8601 extended, with "
+ "optional offset and timezone, i.e., '" + DATE_FORMAT + "', e.g., '"
+ EXAMPLE_DATE + "'."),
@OpenApiParam(name = TIMEZONE, description = "This field specifies a default timezone "
+ "to be used if the format of the " + BEGIN + "and " + END
+ " parameters do not include offset or time zone information. "
+ "Defaults to UTC."),
},
description = "Delete an existing measurement.",
method = HttpMethod.DELETE,
Expand All @@ -234,8 +254,8 @@ public void delete(@NotNull Context ctx, @NotNull String locationId) {
String officeId = requiredParam(ctx, OFFICE);
String minNum = ctx.queryParam(MIN_NUMBER);
String maxNum = ctx.queryParam(MAX_NUMBER);
Instant minDate = queryParamAsInstant(ctx, MIN_DATE);
Instant maxDate = queryParamAsInstant(ctx, MAX_DATE);
Instant minDate = queryParamAsInstant(ctx, BEGIN);
Instant maxDate = queryParamAsInstant(ctx, END);
try (Timer.Context ignored = markAndTime(DELETE)) {
DSLContext dsl = getDslContext(ctx);
MeasurementDao dao = new MeasurementDao(dsl);
Expand Down
21 changes: 21 additions & 0 deletions cwms-data-api/src/test/java/cwms/cda/api/ControllersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,4 +413,25 @@ void testRequiredParamAs() {
assertThrows(RequiredQueryParameterException.class,
() -> Controllers.requiredParamAs(ctx, Controllers.OFFICE, String.class));
}

@Test
void testQueryParamAsDouble() {
final HttpServletRequest request = mock(HttpServletRequest.class);
final HttpServletResponse response = mock(HttpServletResponse.class);
Map<String, String> urlParams = new LinkedHashMap<>();
urlParams.put("a_double", "1.0");
urlParams.put("an_int", "1");
String paramStr = ControllerTest.buildParamStr(urlParams);
when(request.getQueryString()).thenReturn(paramStr);
Context ctx = new Context(request, response, new LinkedHashMap<String, String>());

Double retVal = Controllers.queryParamAsDouble(ctx, "a_double");
assertEquals(1.0, retVal);

Double retVal2 = Controllers.queryParamAsDouble(ctx, "an_int");
assertEquals(1.0, retVal2);

Double retVal3 = Controllers.queryParamAsDouble(ctx, "null");
assertNull(retVal3);
}
}

0 comments on commit bad6ba2

Please sign in to comment.