-
Notifications
You must be signed in to change notification settings - Fork 500
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
stub out GitHub import prototype #5209
- Loading branch information
Showing
5 changed files
with
206 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package edu.harvard.iq.dataverse.api; | ||
|
||
import edu.harvard.iq.dataverse.Dataset; | ||
import edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder; | ||
import static edu.harvard.iq.dataverse.util.json.NullSafeJsonBuilder.jsonObjectBuilder; | ||
import java.io.StringReader; | ||
import javax.json.Json; | ||
import javax.json.JsonReader; | ||
import javax.ws.rs.GET; | ||
import javax.ws.rs.POST; | ||
import javax.ws.rs.Path; | ||
import javax.ws.rs.PathParam; | ||
import javax.ws.rs.core.Response; | ||
|
||
@Path("datasets") | ||
public class GithubApi extends AbstractApiBean { | ||
|
||
@GET | ||
@Path("{id}/github") | ||
public Response getGithubUrl(@PathParam("id") String idSupplied) { | ||
try { | ||
Dataset dataset = findDatasetOrDie(idSupplied); | ||
final NullSafeJsonBuilder jsonObject = jsonObjectBuilder() | ||
.add("datasetId", dataset.getId()) | ||
.add("githubUrl", dataset.getGithubUrl()); | ||
return ok(jsonObject); | ||
} catch (WrappedResponse wr) { | ||
return wr.getResponse(); | ||
} | ||
} | ||
|
||
@POST | ||
@Path("{id}/github/setUrl") | ||
public Response setGithubUrl(String body, @PathParam("id") String idSupplied) { | ||
try { | ||
Dataset datasetBeforeSave = findDatasetOrDie(idSupplied); | ||
JsonReader jsonReader = Json.createReader(new StringReader(body)); | ||
String githubUrl = jsonReader.readObject().getString("githubUrl"); | ||
datasetBeforeSave.setGithubUrl(githubUrl); | ||
Dataset savedDataset = datasetSvc.merge(datasetBeforeSave); | ||
final NullSafeJsonBuilder jsonObject = jsonObjectBuilder() | ||
.add("datasetId", savedDataset.getId()) | ||
.add("githubUrl", savedDataset.getGithubUrl()); | ||
return ok(jsonObject); | ||
} catch (WrappedResponse wr) { | ||
return wr.getResponse(); | ||
} | ||
} | ||
|
||
@POST | ||
@Path("{id}/github/import") | ||
public Response importGithubRepo(@PathParam("id") String idSupplied) { | ||
return ok("FIXME: download GitHub repo as zip and create a file in dataverse, putting metadata about the repo into the file description."); | ||
} | ||
|
||
} |
70 changes: 70 additions & 0 deletions
70
src/test/java/edu/harvard/iq/dataverse/api/GithubApiIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package edu.harvard.iq.dataverse.api; | ||
|
||
import com.jayway.restassured.RestAssured; | ||
import com.jayway.restassured.path.json.JsonPath; | ||
import com.jayway.restassured.response.Response; | ||
import java.util.logging.Logger; | ||
import static javax.ws.rs.core.Response.Status.CREATED; | ||
import static javax.ws.rs.core.Response.Status.OK; | ||
import static org.hamcrest.CoreMatchers.equalTo; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
public class GithubApiIT { | ||
|
||
private static final Logger logger = Logger.getLogger(GithubApiIT.class.getCanonicalName()); | ||
|
||
@BeforeClass | ||
public static void setUpClass() { | ||
RestAssured.baseURI = UtilIT.getRestAssuredBaseUri(); | ||
} | ||
|
||
@Test | ||
public void testGithubDeposit() { | ||
|
||
Response createUser = UtilIT.createRandomUser(); | ||
createUser.prettyPrint(); | ||
createUser.then().assertThat() | ||
.statusCode(OK.getStatusCode()); | ||
String username = UtilIT.getUsernameFromResponse(createUser); | ||
String apitoken = UtilIT.getApiTokenFromResponse(createUser); | ||
|
||
Response createDataverseResponse = UtilIT.createRandomDataverse(apitoken); | ||
createDataverseResponse.prettyPrint(); | ||
createDataverseResponse.then().assertThat() | ||
.statusCode(CREATED.getStatusCode()); | ||
|
||
String dataverseAlias = UtilIT.getAliasFromResponse(createDataverseResponse); | ||
|
||
Response createDataset = UtilIT.createRandomDatasetViaNativeApi(dataverseAlias, apitoken); | ||
createDataset.prettyPrint(); | ||
createDataset.then().assertThat() | ||
.statusCode(CREATED.getStatusCode()); | ||
|
||
Integer datasetId = UtilIT.getDatasetIdFromResponse(createDataset); | ||
String datasetPersistentId = JsonPath.from(createDataset.getBody().asString()).getString("data.persistentId"); | ||
|
||
Response githubUrlShouldBeAbsent = UtilIT.getGithubUrl(datasetPersistentId, apitoken); | ||
githubUrlShouldBeAbsent.prettyPrint(); | ||
githubUrlShouldBeAbsent.then().assertThat() | ||
.statusCode(OK.getStatusCode()) | ||
.body("data.githubUrl", equalTo(null)); | ||
|
||
String githubUrl = "https://github.com/IQSS/metrics.dataverse.org"; | ||
Response setGithubUrl = UtilIT.setGithubUrl(datasetPersistentId, githubUrl, apitoken); | ||
setGithubUrl.then().assertThat() | ||
.statusCode(OK.getStatusCode()); | ||
setGithubUrl.prettyPrint(); | ||
|
||
Response getGithubUrl = UtilIT.getGithubUrl(datasetPersistentId, apitoken); | ||
getGithubUrl.prettyPrint(); | ||
getGithubUrl.then().assertThat() | ||
.statusCode(OK.getStatusCode()) | ||
.body("data.githubUrl", equalTo(githubUrl)); | ||
|
||
Response importGithubRepo = UtilIT.importGithubRepo(datasetPersistentId, apitoken); | ||
importGithubRepo.prettyPrint(); | ||
|
||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters