Skip to content

Commit 7321ec0

Browse files
author
Benedikt Kruse
committed
add PR IQSS#11358
1 parent c6b6dac commit 7321ec0

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

src/main/java/edu/harvard/iq/dataverse/api/Metrics.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,13 @@ public Response getMakeDataCountMetricTimeSeries(@Context Request req, @Context
662662
} catch (IllegalArgumentException ex) {
663663
return error(Response.Status.BAD_REQUEST, ex.getMessage());
664664
}
665+
if (country != null) {
666+
country = country.toLowerCase();
667+
668+
if (!MakeDataCountUtil.isValidCountryCode(country)) {
669+
return error(Response.Status.BAD_REQUEST, "Country must be one of the ISO 1366 Country Codes");
670+
}
671+
}
665672
String metricName = "MDC-" + metricType.toString() + ((country == null) ? "" : "-" + country);
666673

667674
JsonArray jsonArray = MetricsUtil.stringToJsonArray(metricsSvc.returnUnexpiredCacheAllTime(metricName, null, d));

src/test/java/edu/harvard/iq/dataverse/api/MetricsIT.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
import static jakarta.ws.rs.core.Response.Status.OK;
55
import static org.junit.jupiter.api.Assertions.assertEquals;
66

7+
import org.hamcrest.Matchers;
78
import org.junit.jupiter.api.AfterAll;
89
import org.junit.jupiter.api.BeforeAll;
910
import org.junit.jupiter.api.Test;
1011

1112
import edu.harvard.iq.dataverse.metrics.MetricsUtil;
1213
import edu.harvard.iq.dataverse.util.FileUtil;
1314
import io.restassured.RestAssured;
15+
import io.restassured.http.ContentType;
1416
import io.restassured.response.Response;
1517
import jakarta.ws.rs.core.MediaType;
1618

@@ -381,4 +383,41 @@ public void testGetDatasetsBySubjectToMonth() {
381383
response.then().assertThat()
382384
.statusCode(OK.getStatusCode());
383385
}
386+
387+
@Test
388+
public void testUnsupportedQueryParam() {
389+
Response response = UtilIT.makeDataCountMetricTimeSeries("viewCount", "unsupportedParam=value");
390+
391+
response.then().assertThat()
392+
.statusCode(BAD_REQUEST.getStatusCode())
393+
.body("message", Matchers.containsString("queryParameter unsupportedParam not supported for this endpoint"));
394+
}
395+
396+
@Test
397+
public void testInvalidMetric() {
398+
Response response = UtilIT.makeDataCountMetricTimeSeries("invalidMetric", null);
399+
400+
response.then().assertThat()
401+
.statusCode(BAD_REQUEST.getStatusCode())
402+
.body("message", Matchers.containsString("MetricType must be one of these values"));
403+
}
404+
405+
@Test
406+
public void testInvalidCountryCode() {
407+
Response response = UtilIT.makeDataCountMetricTimeSeries("viewCount", "country=INVALID");
408+
409+
response.then().assertThat()
410+
.statusCode(BAD_REQUEST.getStatusCode())
411+
.body("message", Matchers.containsString("Country must be one of the ISO 1366 Country Codes"));
412+
}
413+
414+
@Test
415+
public void testValidRequest() {
416+
Response response = UtilIT.makeDataCountMetricTimeSeries("viewCount", "country=us");
417+
418+
response.then().assertThat()
419+
.statusCode(OK.getStatusCode())
420+
.contentType(ContentType.JSON);
421+
}
422+
384423
}

src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2979,6 +2979,15 @@ static Response metricsDatasetsBySubjectToMonth(String month, String queryParams
29792979
return requestSpecification.get("/api/info/metrics/datasets/bySubject/toMonth/" + month + optionalQueryParams);
29802980
}
29812981

2982+
public static Response makeDataCountMetricTimeSeries(String metricType, String queryParams) {
2983+
String apiPath = "/api/v1/metrics/makeDataCount/" + metricType + "/monthly";
2984+
2985+
Response response = given()
2986+
.get(apiPath + (queryParams != null && !queryParams.isEmpty() ? "?" + queryParams : ""));
2987+
2988+
return response;
2989+
}
2990+
29822991
static Response clearMetricCache() {
29832992
RequestSpecification requestSpecification = given();
29842993
return requestSpecification.delete("/api/admin/clearMetricsCache");
@@ -4627,4 +4636,5 @@ public static Response updateDataverseInputLevelDisplayOnCreate(String dataverse
46274636
.contentType(ContentType.JSON)
46284637
.put("/api/dataverses/" + dataverseAlias + "/inputLevels");
46294638
}
4639+
46304640
}

0 commit comments

Comments
 (0)