Skip to content

Commit 97d2cbf

Browse files
authored
Merge pull request #11358 from GlobalDataverseCommunityConsortium/MetricsFix
Metrics Fix
2 parents 302aef3 + 37c08e8 commit 97d2cbf

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

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: 40 additions & 1 deletion
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
}
384-
}
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+
423+
}

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

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

2989+
public static Response makeDataCountMetricTimeSeries(String metricType, String queryParams) {
2990+
String apiPath = "/api/v1/metrics/makeDataCount/" + metricType + "/monthly";
2991+
2992+
Response response = given()
2993+
.get(apiPath + (queryParams != null && !queryParams.isEmpty() ? "?" + queryParams : ""));
2994+
2995+
return response;
2996+
}
2997+
29892998
static Response clearMetricCache() {
29902999
RequestSpecification requestSpecification = given();
29913000
return requestSpecification.delete("/api/admin/clearMetricsCache");
@@ -4634,4 +4643,5 @@ public static Response updateDataverseInputLevelDisplayOnCreate(String dataverse
46344643
.contentType(ContentType.JSON)
46354644
.put("/api/dataverses/" + dataverseAlias + "/inputLevels");
46364645
}
4646+
46374647
}

0 commit comments

Comments
 (0)