Skip to content
This repository has been archived by the owner on Apr 16, 2022. It is now read-only.

Commit

Permalink
Add tests for submission attachment download conditions
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel committed Oct 28, 2020
1 parent 2271261 commit 8a010b1
Showing 1 changed file with 81 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,40 @@ public void downloads_submission_xml_if_not_on_disk() throws Exception {
assertThat(actualSubmissionXml, is(expectedSubmissionXml));
}

@Test
public void downloads_submission_attachment_if_not_on_disk() throws Exception {
String instanceId = "some instance id";
String expectedSubmissionXml = buildSubmissionXml(instanceId);

stubServerForSingleSubmission(instanceId, expectedSubmissionXml);

// Stub a submission with one attachment
server
.request(by(uri("/v1/projects/1/forms/some-form/submissions/" + instanceId + ".xml")))
.response(expectedSubmissionXml);

List<CentralAttachment> attachments = buildAttachments(1);
server
.request(by(uri("/v1/projects/1/forms/some-form/submissions/" + instanceId + "/attachments")))
.response(jsonOfAttachments(attachments));
server
.request(by(uri("/v1/projects/1/forms/some-form/submissions/" + instanceId + "/attachments/" + attachments.get(0).getName())))
.response("some attachment content");

// Confirm the attachment was downloaded
Path attachmentFile = form.getSubmissionMediaFile(briefcaseDir, instanceId, attachments.get(0).getName());
running(server, () -> launchSync(pullOp.pull(form)));
assertThat(attachmentFile, exists());

Files.delete(attachmentFile);

// Confirm the attachment was re-downloaded
running(server, () -> launchSync(pullOp.pull(form)));
assertThat(attachmentFile, exists());
String actualSubmissionXml = new String(readAllBytes(attachmentFile));
assertThat(actualSubmissionXml, is("some attachment content"));
}

@Test
public void skips_submission_file_if_already_on_disk() throws Exception {
String instanceId = "some instance id";
Expand Down Expand Up @@ -237,6 +271,53 @@ public void skips_submission_file_if_already_on_disk() throws Exception {
assertThat(actualSubmissionXml, is(expectedSubmissionXml));
}

@Test
public void skips_submission_attachment_if_already_on_disk() throws Exception {
String instanceId = "some instance id";
String expectedSubmissionXml = buildSubmissionXml(instanceId);

stubServerForSingleSubmission(instanceId, expectedSubmissionXml);

// Stub a submission with one attachment
server
.request(by(uri("/v1/projects/1/forms/some-form/submissions/" + instanceId + ".xml")))
.response(expectedSubmissionXml);

List<CentralAttachment> attachments = buildAttachments(1);
server
.request(by(uri("/v1/projects/1/forms/some-form/submissions/" + instanceId + "/attachments")))
.response(jsonOfAttachments(attachments));
server
.request(by(uri("/v1/projects/1/forms/some-form/submissions/" + instanceId + "/attachments/" + attachments.get(0).getName())))
.response("some attachment content");

// Confirm the attachment was downloaded
Path attachmentFile = form.getSubmissionMediaFile(briefcaseDir, instanceId, attachments.get(0).getName());
running(server, () -> launchSync(pullOp.pull(form)));
assertThat(attachmentFile, exists());

// Stub a bad attachment to make sure it doesn't get downloaded
server = httpServer(serverPort);
stubServerForSingleSubmission(instanceId, expectedSubmissionXml);
// Stub a submission with one attachment
server
.request(by(uri("/v1/projects/1/forms/some-form/submissions/" + instanceId + ".xml")))
.response(expectedSubmissionXml);

attachments = buildAttachments(1);
server
.request(by(uri("/v1/projects/1/forms/some-form/submissions/" + instanceId + "/attachments")))
.response(jsonOfAttachments(attachments));
server
.request(by(uri("/v1/projects/1/forms/some-form/submissions/" + instanceId + "/attachments/" + attachments.get(0).getName())))
.response("some bad attachment content");

// Confirm the attachment was not re-downloaded
running(server, () -> launchSync(pullOp.pull(form)));
String actualSubmissionXml = new String(readAllBytes(attachmentFile));
assertThat(actualSubmissionXml, is("some attachment content"));
}

public void stubServerForSingleSubmission(String instanceId, String expectedSubmissionXml) {
// Stub the token request
server
Expand Down

0 comments on commit 8a010b1

Please sign in to comment.