Skip to content

Commit 0897f10

Browse files
author
Benedikt Kruse
committed
add PR IQSS#11358
1 parent 934027a commit 0897f10

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
@@ -661,6 +661,13 @@ public Response getMakeDataCountMetricTimeSeries(@Context Request req, @Context
661661
} catch (IllegalArgumentException ex) {
662662
return error(Response.Status.BAD_REQUEST, ex.getMessage());
663663
}
664+
if (country != null) {
665+
country = country.toLowerCase();
666+
667+
if (!MakeDataCountUtil.isValidCountryCode(country)) {
668+
return error(Response.Status.BAD_REQUEST, "Country must be one of the ISO 1366 Country Codes");
669+
}
670+
}
664671
String metricName = "MDC-" + metricType.toString() + ((country == null) ? "" : "-" + country);
665672

666673
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
@@ -2702,6 +2702,15 @@ static Response metricsDatasetsBySubjectToMonth(String month, String queryParams
27022702
RequestSpecification requestSpecification = given();
27032703
return requestSpecification.get("/api/info/metrics/datasets/bySubject/toMonth/" + month + optionalQueryParams);
27042704
}
2705+
2706+
public static Response makeDataCountMetricTimeSeries(String metricType, String queryParams) {
2707+
String apiPath = "/api/v1/metrics/makeDataCount/" + metricType + "/monthly";
2708+
2709+
Response response = given()
2710+
.get(apiPath + (queryParams != null && !queryParams.isEmpty() ? "?" + queryParams : ""));
2711+
2712+
return response;
2713+
}
27052714

27062715
static Response clearMetricCache() {
27072716
RequestSpecification requestSpecification = given();
@@ -3986,4 +3995,5 @@ public static Response getOpenAPI(String accept, String format) {
39863995
.get("/openapi");
39873996
return response;
39883997
}
3998+
39893999
}

0 commit comments

Comments
 (0)