Skip to content

Commit

Permalink
Verify DSpace REST API calls in integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
markpatton committed Dec 18, 2024
1 parent 236f078 commit d0481d1
Showing 1 changed file with 36 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.ok;
import static com.github.tomakehurst.wiremock.client.WireMock.patch;
import static com.github.tomakehurst.wiremock.client.WireMock.patchRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.post;
import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
import static com.github.tomakehurst.wiremock.client.WireMock.put;
Expand Down Expand Up @@ -117,6 +118,7 @@ void testSubmissionProcessing_Full() throws Exception {
verify(invenioRdmTransport, times(1)).open(anyMap());
verify(devNullTransport, times(0)).open(anyMap());
verifyInvenioApiStubs(1);
verifyDSpaceApiStubs(1);
}

@Test
Expand Down Expand Up @@ -144,6 +146,8 @@ void testSubmissionProcessing_Full_InvenioExistingRecord() throws Exception {
verify(invenioRdmTransport, times(1)).open(anyMap());
verify(devNullTransport, times(0)).open(anyMap());
verifyInvenioApiStubs(1);
verifyDSpaceApiStubs(1);

WireMock.verify(1, deleteRequestedFor(
urlEqualTo("/api/records/existing-record-id/draft")));
}
Expand All @@ -161,6 +165,7 @@ void testSubmissionProcessing_SkipTestSubmission() throws Exception {
verify(filesystemTransport, times(0)).open(anyMap());
verify(invenioRdmTransport, times(0)).open(anyMap());
verifyInvenioApiStubs(0);
verifyDSpaceApiStubs(0);
}

@Test
Expand All @@ -179,6 +184,7 @@ void testSubmissionProcessing_DontSkipTestSubmission() throws Exception {
verify(filesystemTransport, times(3)).open(anyMap());
verify(invenioRdmTransport, times(1)).open(anyMap());
verifyInvenioApiStubs(1);
verifyDSpaceApiStubs(1);
}

private void testSubmissionProcessor(Submission submission, boolean usingDevNull) throws IOException {
Expand Down Expand Up @@ -374,6 +380,29 @@ private void initInvenioApiStubs() throws IOException {
.willReturn(ok(publishJsonResponse)));
}

private void verifyInvenioApiStubs(int expectedCount) throws IOException, URISyntaxException {
WireMock.verify(expectedCount, getRequestedFor(
urlEqualTo("/api/user/records?q=metadata.title:%22Specific%20protein%20supplementation%20using%20" +
"soya%2C%20casein%20or%20whey%20differentially%20affects%20regional%20gut%20growth%20and%20luminal%20" +
"growth%20factor%20bioactivity%20in%20rats%3B%20implications%20for%20the%20treatment%20of%20gut%20" +
"injury%20and%20stimulating%20repair%22")));
String recordPayload = Files.readString(
Paths.get(SubmissionProcessorIT.class.getResource("expectedInvenioRecordPayload.json").toURI()));
WireMock.verify(expectedCount, postRequestedFor(urlEqualTo("/api/records"))
.withRequestBody(equalTo(recordPayload)));
List<String> fileNames = getFileNames();
WireMock.verify(expectedCount * fileNames.size(), postRequestedFor(
urlEqualTo("/api/records/test-record-id/draft/files")));
fileNames.forEach(fileName -> {
WireMock.verify(expectedCount, putRequestedFor(
urlEqualTo("/api/records/test-record-id/draft/files/" + fileName + "/content")));
WireMock.verify(expectedCount, postRequestedFor(
urlEqualTo("/api/records/test-record-id/draft/files/" + fileName + "/commit")));
});
WireMock.verify(expectedCount, postRequestedFor(
urlEqualTo("/api/records/test-record-id/draft/actions/publish")));
}

private void initDSpaceApiStubs() throws IOException {
stubFor(get("/dspace/api/security/csrf").willReturn(WireMock.notFound().
withHeader("DSPACE-XSRF-TOKEN", "csrftoken")));
Expand Down Expand Up @@ -409,27 +438,12 @@ private void initDSpaceApiStubs() throws IOException {
stubFor(post("/dspace/api/workflow/workflowitems").willReturn(WireMock.ok()));
}

private void verifyInvenioApiStubs(int expectedCount) throws IOException, URISyntaxException {
WireMock.verify(expectedCount, getRequestedFor(
urlEqualTo("/api/user/records?q=metadata.title:%22Specific%20protein%20supplementation%20using%20" +
"soya%2C%20casein%20or%20whey%20differentially%20affects%20regional%20gut%20growth%20and%20luminal%20" +
"growth%20factor%20bioactivity%20in%20rats%3B%20implications%20for%20the%20treatment%20of%20gut%20" +
"injury%20and%20stimulating%20repair%22")));
String recordPayload = Files.readString(
Paths.get(SubmissionProcessorIT.class.getResource("expectedInvenioRecordPayload.json").toURI()));
WireMock.verify(expectedCount, postRequestedFor(urlEqualTo("/api/records"))
.withRequestBody(equalTo(recordPayload)));
List<String> fileNames = getFileNames();
WireMock.verify(expectedCount * fileNames.size(), postRequestedFor(
urlEqualTo("/api/records/test-record-id/draft/files")));
fileNames.forEach(fileName -> {
WireMock.verify(expectedCount, putRequestedFor(
urlEqualTo("/api/records/test-record-id/draft/files/" + fileName + "/content")));
WireMock.verify(expectedCount, postRequestedFor(
urlEqualTo("/api/records/test-record-id/draft/files/" + fileName + "/commit")));
});
WireMock.verify(expectedCount, postRequestedFor(
urlEqualTo("/api/records/test-record-id/draft/actions/publish")));
private void verifyDSpaceApiStubs(int expectedCount) throws IOException {
WireMock.verify(expectedCount, getRequestedFor(urlEqualTo("/dspace/api/security/csrf")));
WireMock.verify(expectedCount, postRequestedFor(urlEqualTo("/dspace/api/authn/login")));
WireMock.verify(expectedCount, getRequestedFor(urlEqualTo("/dspace/api/discover/search/objects?query=handle:collectionhandle")));
WireMock.verify(expectedCount, postRequestedFor(urlEqualTo("/dspace/api/submission/workspaceitems?owningCollection=collectionuuid")));
WireMock.verify(expectedCount, patchRequestedFor(urlEqualTo("/dspace/api/submission/workspaceitems/1")));
WireMock.verify(expectedCount, postRequestedFor(urlEqualTo("/dspace/api/workflow/workflowitems")));
}

}

0 comments on commit d0481d1

Please sign in to comment.