Skip to content

Commit 37c08e8

Browse files
committed
add tests for bad request errors
1 parent 0242899 commit 37c08e8

2 files changed

Lines changed: 50 additions & 1 deletion

File tree

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
@@ -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)