Skip to content

Commit

Permalink
Remove old test and changes response to JSON
Browse files Browse the repository at this point in the history
  • Loading branch information
jp-tosca committed May 29, 2024
1 parent b99b194 commit f265744
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
20 changes: 18 additions & 2 deletions src/main/java/edu/harvard/iq/dataverse/openapi/OpenAPI.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
import java.nio.charset.StandardCharsets;
import java.util.*;
import java.util.logging.*;

import jakarta.json.Json;
import jakarta.json.JsonObject;
import jakarta.json.JsonObjectBuilder;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.*;
Expand Down Expand Up @@ -59,10 +63,22 @@ protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws Se
} else if (YAML_FORMAT.equals(format)){
resp.setContentType(MediaType.TEXT_PLAIN_TYPE.toString());
} else {

List<String> args = Arrays.asList(format);
String bundleResponse = BundleUtil.getStringFromBundle("openapi.exception.invalid.format", args);
resp.sendError(Response.Status.UNSUPPORTED_MEDIA_TYPE.getStatusCode(),
bundleResponse);

JsonObject errorResponse = Json.createObjectBuilder()
.add("status", "ERROR")
.add("code", HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE)
.add("message", bundleResponse)
.build();

resp.setContentType(MediaType.APPLICATION_JSON_TYPE.toString());
resp.setStatus(HttpServletResponse.SC_UNSUPPORTED_MEDIA_TYPE);

PrintWriter responseWriter = resp.getWriter();
responseWriter.println(errorResponse.toString());
responseWriter.flush();
return;
}

Expand Down
18 changes: 0 additions & 18 deletions src/test/java/edu/harvard/iq/dataverse/api/InfoIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,24 +81,6 @@ public void testGetZipDownloadLimit() {
.body("data", notNullValue());
}

@Test
public void testOpenApiDefinition(){
Response response = given()
.get("/api/info/openapi/jasonInvalid");
response.prettyPrint();
response.then().assertThat().statusCode(BAD_REQUEST.getStatusCode());

String jsonDefinitionFileContent = UtilIT.getDatasetJson("src/main/resources/edu/harvard/iq/dataverse/openapi/dataverse_openapi.json");
response = given().get("/api/info/openapi/json");
response.then().assertThat().statusCode(OK.getStatusCode())
.body(equalTo(jsonDefinitionFileContent));

String yamlFileContent = UtilIT.getDatasetJson("src/main/resources/edu/harvard/iq/dataverse/openapi/dataverse_openapi.yaml");
response = given().get("/api/info/openapi/yaml");
response.then().assertThat().statusCode(OK.getStatusCode())
.body(equalTo(yamlFileContent));

}

private void testSettingEndpoint(SettingsServiceBean.Key settingKey, String testSettingValue) {
String endpoint = "/api/info/settings/" + settingKey;
Expand Down

0 comments on commit f265744

Please sign in to comment.