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

Commit

Permalink
Add test for conditions under which submission.xml is downloaded
Browse files Browse the repository at this point in the history
  • Loading branch information
lognaturel committed Oct 28, 2020
1 parent 0e542e6 commit 2271261
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public Job<Void> pull(FormStatus form) {
.forEach(attachment ->
downloadSubmissionAttachment(form, instanceId, attachment, token, runnerStatus, tracker, currentSubmissionNumber, totalSubmissions, attachmentNumber.getAndIncrement(), totalAttachments)
);
if (!runnerStatus.isCancelled()) {
if (!runnerStatus.isCancelled() && !inDb) {
db.putRecordedInstanceDirectory(instanceId, form.getSubmissionDir(briefcaseDir, instanceId).toFile());
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
import static com.github.npathai.hamcrestopt.OptionalMatchers.isPresent;
import static java.util.stream.Collectors.joining;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.opendatakit.briefcase.matchers.PathMatchers.exists;
import static org.opendatakit.briefcase.pull.central.PullFromCentralTest.buildAttachments;
import static org.opendatakit.briefcase.pull.central.PullFromCentralTest.jsonOfAttachments;
import static org.opendatakit.briefcase.pull.central.PullFromCentralTest.jsonOfSubmissions;
Expand All @@ -35,13 +37,16 @@
import static org.opendatakit.briefcase.reused.job.JobsRunner.launchSync;
import static org.opendatakit.briefcase.reused.transfer.TransferTestHelpers.buildFormStatus;
import static org.opendatakit.briefcase.reused.transfer.TransferTestHelpers.buildMediaFileXml;
import static org.opendatakit.briefcase.reused.transfer.TransferTestHelpers.buildSubmissionXml;

import com.github.dreamhead.moco.HttpServer;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.OffsetDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -175,4 +180,82 @@ public void knows_how_to_pull_a_form() throws Exception {
// Assert that saves form metadata
assertThat(formMetadataPort.fetch(FormKey.from(form)), isPresent());
}

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

stubServerForSingleSubmission(instanceId, expectedSubmissionXml);

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

// Confirm the submission was downloaded
Path submissionFile = form.getSubmissionFile(briefcaseDir, instanceId);
running(server, () -> launchSync(pullOp.pull(form)));
assertThat(submissionFile, exists());

Files.delete(submissionFile);

// Confirm the submission was re-downloaded
running(server, () -> launchSync(pullOp.pull(form)));
assertThat(submissionFile, exists());
String actualSubmissionXml = new String(readAllBytes(submissionFile));
assertThat(actualSubmissionXml, is(expectedSubmissionXml));
}

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

stubServerForSingleSubmission(instanceId, expectedSubmissionXml);

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

// Confirm the submission was downloaded
Path submissionFile = form.getSubmissionFile(briefcaseDir, instanceId);
running(server, () -> launchSync(pullOp.pull(form)));
assertThat(submissionFile, exists());

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

// Confirm the submission was not re-downloaded
running(server, () -> launchSync(pullOp.pull(form)));
String actualSubmissionXml = new String(readAllBytes(submissionFile));
assertThat(actualSubmissionXml, is(expectedSubmissionXml));
}

public void stubServerForSingleSubmission(String instanceId, String expectedSubmissionXml) {
// Stub the token request
server
.request(by(uri("/v1/sessions")))
.response("{\n" +
" \"createdAt\": \"2018-04-18T03:04:51.695Z\",\n" +
" \"expiresAt\": \"2018-04-19T03:04:51.695Z\",\n" +
" \"token\": \"" + token + "\"\n" +
"}");

// Stub the form XML request
server
.request(by(uri("/v1/projects/1/forms/some-form.xml")))
.response(new String(readAllBytes(getPath("simple-form.xml"))));

// Stub the submissions request
List<String> expectedInstanceIds = Collections.singletonList(instanceId);
server
.request(by(uri("/v1/projects/1/forms/some-form/submissions")))
.response(jsonOfSubmissions(expectedInstanceIds));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,26 +126,6 @@ public void knows_how_to_download_a_form() throws IOException {
));
}

@Test
public void knows_how_to_download_a_submission() {
String instanceId = "uuid:515a13cf-d7a5-4606-a18f-84940b0944b2";
String expectedSubmissionXml = buildSubmissionXml(instanceId);
Path submissionFile = form.getSubmissionFile(briefcaseDir, instanceId);
http.stub(server.getDownloadSubmissionRequest(form.getFormId(), instanceId, submissionFile, token), ok(expectedSubmissionXml));

pullOp.downloadSubmission(form, instanceId, token, runnerStatus, tracker, 1, 1);

String actualSubmissionXml = new String(readAllBytes(submissionFile));
assertThat(submissionFile, exists());
assertThat(actualSubmissionXml, is(expectedSubmissionXml));
// There's actually another event from the call to tracker.trackTotalSubmissions(1) in this test.
// Since we don't really care about it, we use Matchers.hasItem() instead of Matchers.contains()
assertThat(events, contains(
"Start downloading submission 1 of 1",
"Submission 1 of 1 downloaded"
));
}

@Test
public void knows_how_to_get_form_attachments() {
List<CentralAttachment> expectedAttachments = buildAttachments(3);
Expand Down Expand Up @@ -190,7 +170,7 @@ public void knows_how_to_download_a_form_attachment() {
}

@Test
public void knows_how_to_get_submission() {
public void knows_how_to_get_submission_list() {
List<String> expectedInstanceIds = IntStream.range(0, 250)
.mapToObj(i -> "submission instanceID " + i)
.collect(Collectors.toList());
Expand All @@ -209,6 +189,26 @@ public void knows_how_to_get_submission() {
));
}

@Test
public void knows_how_to_download_a_submission() {
String instanceId = "uuid:515a13cf-d7a5-4606-a18f-84940b0944b2";
String expectedSubmissionXml = buildSubmissionXml(instanceId);
Path submissionFile = form.getSubmissionFile(briefcaseDir, instanceId);
http.stub(server.getDownloadSubmissionRequest(form.getFormId(), instanceId, submissionFile, token), ok(expectedSubmissionXml));

pullOp.downloadSubmission(form, instanceId, token, runnerStatus, tracker, 1, 1);

String actualSubmissionXml = new String(readAllBytes(submissionFile));
assertThat(submissionFile, exists());
assertThat(actualSubmissionXml, is(expectedSubmissionXml));
// There's actually another event from the call to tracker.trackTotalSubmissions(1) in this test.
// Since we don't really care about it, we use Matchers.hasItem() instead of Matchers.contains()
assertThat(events, contains(
"Start downloading submission 1 of 1",
"Submission 1 of 1 downloaded"
));
}

@Test
public void knows_how_to_get_submission_attachments() {
String instanceId = "uuid:515a13cf-d7a5-4606-a18f-84940b0944b2";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
public class TransferTestHelpers {
public static String buildSubmissionXml(String instanceId) {
return "" +
"<some-form id=\"some-form\" instanceID=\"" + instanceId + "\" submissionDate=\"2018-07-19T10:36:50.779Z\" isComplete=\"true\" markedAsCompleteDate=\"2018-07-19T10:36:50.779Z\">\n" +
"<some-form xmlns:orx=\"http://openrosa.org/xforms\" id=\"some-form\" instanceID=\"" + instanceId + "\" submissionDate=\"2018-07-19T10:36:50.779Z\" isComplete=\"true\" markedAsCompleteDate=\"2018-07-19T10:36:50.779Z\">\n" +
" <orx:meta>\n" +
" <orx:instanceID>" + instanceId + "</orx:instanceID>\n" +
" </orx:meta>\n" +
Expand Down

0 comments on commit 2271261

Please sign in to comment.