From 8a010b1c03b6e9d4ad164f20a1309b4710c89c14 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?H=C3=A9l=C3=A8ne=20Martin?= Date: Wed, 28 Oct 2020 14:24:10 -0700 Subject: [PATCH] Add tests for submission attachment download conditions --- .../PullFromCentralIntegrationTest.java | 81 +++++++++++++++++++ 1 file changed, 81 insertions(+) diff --git a/test/java/org/opendatakit/briefcase/pull/central/PullFromCentralIntegrationTest.java b/test/java/org/opendatakit/briefcase/pull/central/PullFromCentralIntegrationTest.java index daec39a35..3ca84b644 100644 --- a/test/java/org/opendatakit/briefcase/pull/central/PullFromCentralIntegrationTest.java +++ b/test/java/org/opendatakit/briefcase/pull/central/PullFromCentralIntegrationTest.java @@ -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 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"; @@ -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 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