From 0242899e4fd6ba665078e77901cd0f82ac153ad2 Mon Sep 17 00:00:00 2001 From: Jim Myers Date: Wed, 19 Mar 2025 14:57:49 -0400 Subject: [PATCH 1/2] missing country code check --- src/main/java/edu/harvard/iq/dataverse/api/Metrics.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/edu/harvard/iq/dataverse/api/Metrics.java b/src/main/java/edu/harvard/iq/dataverse/api/Metrics.java index f36c514859e..74bc4184099 100644 --- a/src/main/java/edu/harvard/iq/dataverse/api/Metrics.java +++ b/src/main/java/edu/harvard/iq/dataverse/api/Metrics.java @@ -662,6 +662,13 @@ public Response getMakeDataCountMetricTimeSeries(@Context Request req, @Context } catch (IllegalArgumentException ex) { return error(Response.Status.BAD_REQUEST, ex.getMessage()); } + if (country != null) { + country = country.toLowerCase(); + + if (!MakeDataCountUtil.isValidCountryCode(country)) { + return error(Response.Status.BAD_REQUEST, "Country must be one of the ISO 1366 Country Codes"); + } + } String metricName = "MDC-" + metricType.toString() + ((country == null) ? "" : "-" + country); JsonArray jsonArray = MetricsUtil.stringToJsonArray(metricsSvc.returnUnexpiredCacheAllTime(metricName, null, d)); From 37c08e847ae00b8be8e2205f8ab81e325ab31534 Mon Sep 17 00:00:00 2001 From: Jim Myers Date: Wed, 19 Mar 2025 15:44:01 -0400 Subject: [PATCH 2/2] add tests for bad request errors --- .../harvard/iq/dataverse/api/MetricsIT.java | 41 ++++++++++++++++++- .../edu/harvard/iq/dataverse/api/UtilIT.java | 10 +++++ 2 files changed, 50 insertions(+), 1 deletion(-) diff --git a/src/test/java/edu/harvard/iq/dataverse/api/MetricsIT.java b/src/test/java/edu/harvard/iq/dataverse/api/MetricsIT.java index a8f7afc1cb0..9595b9589f7 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/MetricsIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/MetricsIT.java @@ -4,6 +4,7 @@ import static jakarta.ws.rs.core.Response.Status.OK; import static org.junit.jupiter.api.Assertions.assertEquals; +import org.hamcrest.Matchers; import org.junit.jupiter.api.AfterAll; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; @@ -11,6 +12,7 @@ import edu.harvard.iq.dataverse.metrics.MetricsUtil; import edu.harvard.iq.dataverse.util.FileUtil; import io.restassured.RestAssured; +import io.restassured.http.ContentType; import io.restassured.response.Response; import jakarta.ws.rs.core.MediaType; @@ -381,4 +383,41 @@ public void testGetDatasetsBySubjectToMonth() { response.then().assertThat() .statusCode(OK.getStatusCode()); } -} + + @Test + public void testUnsupportedQueryParam() { + Response response = UtilIT.makeDataCountMetricTimeSeries("viewCount", "unsupportedParam=value"); + + response.then().assertThat() + .statusCode(BAD_REQUEST.getStatusCode()) + .body("message", Matchers.containsString("queryParameter unsupportedParam not supported for this endpoint")); + } + + @Test + public void testInvalidMetric() { + Response response = UtilIT.makeDataCountMetricTimeSeries("invalidMetric", null); + + response.then().assertThat() + .statusCode(BAD_REQUEST.getStatusCode()) + .body("message", Matchers.containsString("MetricType must be one of these values")); + } + + @Test + public void testInvalidCountryCode() { + Response response = UtilIT.makeDataCountMetricTimeSeries("viewCount", "country=INVALID"); + + response.then().assertThat() + .statusCode(BAD_REQUEST.getStatusCode()) + .body("message", Matchers.containsString("Country must be one of the ISO 1366 Country Codes")); + } + + @Test + public void testValidRequest() { + Response response = UtilIT.makeDataCountMetricTimeSeries("viewCount", "country=us"); + + response.then().assertThat() + .statusCode(OK.getStatusCode()) + .contentType(ContentType.JSON); + } + + } diff --git a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java index 5ee78a29582..0c021442cda 100644 --- a/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java +++ b/src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java @@ -2979,6 +2979,15 @@ static Response metricsDatasetsBySubjectToMonth(String month, String queryParams return requestSpecification.get("/api/info/metrics/datasets/bySubject/toMonth/" + month + optionalQueryParams); } + public static Response makeDataCountMetricTimeSeries(String metricType, String queryParams) { + String apiPath = "/api/v1/metrics/makeDataCount/" + metricType + "/monthly"; + + Response response = given() + .get(apiPath + (queryParams != null && !queryParams.isEmpty() ? "?" + queryParams : "")); + + return response; + } + static Response clearMetricCache() { RequestSpecification requestSpecification = given(); return requestSpecification.delete("/api/admin/clearMetricsCache"); @@ -4627,4 +4636,5 @@ public static Response updateDataverseInputLevelDisplayOnCreate(String dataverse .contentType(ContentType.JSON) .put("/api/dataverses/" + dataverseAlias + "/inputLevels"); } + }