Skip to content

Commit

Permalink
remove hard-coded "/" and use GlobalID object instead, add tests #10542
Browse files Browse the repository at this point in the history
  • Loading branch information
pdurbin committed Nov 25, 2024
1 parent 5a24a4c commit ca93d60
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,7 @@ public String getLinks() {
Exporter exporter;
try {
exporter = ExportService.getInstance().getExporter(formatName);
describedby += ",<" + systemConfig.getDataverseSiteUrl() + "/api/datasets/export?exporter=" + formatName + "&persistentId="
+ ds.getProtocol() + ":" + ds.getAuthority() + "/" + ds.getIdentifier() + ">;rel=\"describedby\"" + ";type=\"" + exporter.getMediaType() + "\"";
describedby += ",<" + getExporterUrl(formatName, ds) + ">;rel=\"describedby\"" + ";type=\"" + exporter.getMediaType() + "\"";
} catch (ExportException ex) {
logger.warning("Could not look up exporter based on " + formatName + ". Exception: " + ex);
}
Expand All @@ -98,7 +97,7 @@ public String getLinks() {

String linkset = "<" + systemConfig.getDataverseSiteUrl() + "/api/datasets/:persistentId/versions/"
+ workingDatasetVersion.getVersionNumber() + "." + workingDatasetVersion.getMinorVersionNumber()
+ "/linkset?persistentId=" + ds.getProtocol() + ":" + ds.getAuthority() + "/" + ds.getIdentifier() + "> ; rel=\"linkset\";type=\"application/linkset+json\"";
+ "/linkset?persistentId=" + ds.getGlobalId().asString() + "> ; rel=\"linkset\";type=\"application/linkset+json\"";
valueList.add(linkset);
logger.fine(String.format("valueList is: %s", valueList));

Expand All @@ -108,7 +107,7 @@ public String getLinks() {
public JsonArrayBuilder getJsonLinkset() {
Dataset ds = workingDatasetVersion.getDataset();
GlobalId gid = ds.getGlobalId();
String landingPage = systemConfig.getDataverseSiteUrl() + "/dataset.xhtml?persistentId=" + ds.getProtocol() + ":" + ds.getAuthority() + "/" + ds.getIdentifier();
String landingPage = systemConfig.getDataverseSiteUrl() + "/dataset.xhtml?persistentId=" + ds.getGlobalId().asString();
JsonArrayBuilder authors = getJsonAuthors(getAuthorURLs(false));
JsonArrayBuilder items = getJsonItems();

Expand All @@ -133,8 +132,7 @@ public JsonArrayBuilder getJsonLinkset() {
exporter = ExportService.getInstance().getExporter(formatName);
mediaTypes.add(
jsonObjectBuilder().add(
"href",
systemConfig.getDataverseSiteUrl() + "/api/datasets/export?exporter=" + formatName + "&persistentId=" + ds.getProtocol() + ":" + ds.getAuthority() + "/" + ds.getIdentifier()
"href", getExporterUrl(formatName, ds)
).add(
"type",
exporter.getMediaType()
Expand Down Expand Up @@ -297,4 +295,9 @@ private String getPublicDownloadUrl(DataFile dataFile) {
return FileUtil.getPublicDownloadUrl(systemConfig.getDataverseSiteUrl(),
((gid != null) ? gid.asString() : null), dataFile.getId());
}

private String getExporterUrl(String formatName, Dataset ds) {
return systemConfig.getDataverseSiteUrl()
+ "/api/datasets/export?exporter=" + formatName + "&persistentId=" + ds.getGlobalId().asString();
}
}
10 changes: 10 additions & 0 deletions src/test/java/edu/harvard/iq/dataverse/api/SignpostingIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import java.util.regex.Pattern;

import jakarta.json.JsonObject;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.is;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -56,6 +58,9 @@ public void testSignposting() {
String datasetLandingPage = RestAssured.baseURI + "/dataset.xhtml?persistentId=" + datasetPid;
System.out.println("Checking dataset landing page for Signposting: " + datasetLandingPage);
Response getHtml = given().get(datasetLandingPage);
getHtml.then().assertThat()
.statusCode(OK.getStatusCode())
.header("Link", endsWith("linkset?persistentId=" + datasetPid + "> ; rel=\"linkset\";type=\"application/linkset+json\""));

System.out.println("Link header: " + getHtml.getHeader("Link"));
if (false) {
Expand Down Expand Up @@ -104,6 +109,11 @@ public void testSignposting() {

Response linksetResponse = given().accept(ContentType.JSON).get(linksetUrl);
linksetResponse.prettyPrint();
linksetResponse.then().assertThat()
.statusCode(OK.getStatusCode())
.body("linkset[0].anchor", is(RestAssured.baseURI + "/dataset.xhtml?persistentId=" + datasetPid))
.body("linkset[0].license.href", is("http://creativecommons.org/publicdomain/zero/1.0"))
.body("linkset[0].describedby[1].href", endsWith("persistentId=" + datasetPid));

String responseString = linksetResponse.getBody().asString();
System.out.println("response string: " + responseString);
Expand Down

0 comments on commit ca93d60

Please sign in to comment.