Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/edu/harvard/iq/dataverse/api/Metrics.java
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
41 changes: 40 additions & 1 deletion src/test/java/edu/harvard/iq/dataverse/api/MetricsIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
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;

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;

Expand Down Expand Up @@ -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);
}

}
10 changes: 10 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/UtilIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -4627,4 +4636,5 @@ public static Response updateDataverseInputLevelDisplayOnCreate(String dataverse
.contentType(ContentType.JSON)
.put("/api/dataverses/" + dataverseAlias + "/inputLevels");
}

}
Loading