use of org.opendatakit.briefcase.reused.transfer.TransferTestHelpers in project briefcase by opendatakit.
the class PullFromAggregateIntegrationTest method knows_how_to_pull_a_form.
@Test
public void knows_how_to_pull_a_form() throws Exception {
// Create and stub a couple of attachments that we will reuse on the form and all submissions
List<AggregateAttachment> attachments = buildMediaFiles(BASE_URL.toString(), 2);
attachments.forEach(a -> server.request(by(uri(a.getDownloadUrl().toString().substring(BASE_URL.toString().length())))).response("some attachment contents"));
// Stub the form XML request
server.request(by(uri("/formXml"))).response(new String(readAllBytes(getPath("simple-form.xml"))));
// Stub the form's manifest XML request with a manifest that uses the attachments stubbed above
server.request(by(uri("/manifest"))).response(buildManifestXml(attachments));
// Stub the request for the submission id batches, for a total of 250 submissions
List<Pair<String, Cursor>> submissionPages = generatePages(250, 100);
server.request(by(uri("/view/submissionList"))).response(seq(submissionPages.get(0).getLeft(), submissionPages.get(1).getLeft(), submissionPages.get(2).getLeft(), submissionPages.get(3).getLeft()));
// Stub all the 250 submissions, each one with the couple of attachments stubbed above
String submissionTpl = new String(readAllBytes(getPath("submission-download-template.xml")));
AtomicInteger uidSeq = new AtomicInteger(1);
String submissionDate = OffsetDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
List<String> submissionXmls = IntStream.range(0, 250).mapToObj(i -> String.format(submissionTpl, "some sequential uid " + uidSeq.getAndIncrement(), submissionDate, submissionDate, "some text", attachments.stream().map(TransferTestHelpers::buildMediaFileXml).collect(joining("\n")))).collect(toList());
server.request(by(uri("/view/downloadSubmission"))).response(seq(submissionXmls.get(0), submissionXmls.subList(1, 250).toArray(new String[249])));
// Run the pull operation and just check that some key events are published
running(server, () -> launchSync(pullOp.pull(form, Optional.empty())));
// Assert that pulling the form works indirectly by going through the status changes of the form
assertThat(form.getStatusHistory(), containsString("Form downloaded"));
assertThat(form.getStatusHistory(), containsString("Start downloading form attachment 1 of 2"));
assertThat(form.getStatusHistory(), containsString("Start downloading form attachment 2 of 2"));
assertThat(form.getStatusHistory(), containsString("Form attachment 1 of 2 downloaded"));
assertThat(form.getStatusHistory(), containsString("Form attachment 2 of 2 downloaded"));
assertThat(form.getStatusHistory(), containsString("Start downloading submission 1 of 250"));
assertThat(form.getStatusHistory(), containsString("Submission 1 of 250 downloaded"));
assertThat(form.getStatusHistory(), containsString("Attachment 1 of 2 of submission 1 of 250 downloaded"));
assertThat(form.getStatusHistory(), containsString("Attachment 2 of 2 of submission 1 of 250 downloaded"));
assertThat(form.getStatusHistory(), containsString("Start downloading submission 250 of 250"));
assertThat(form.getStatusHistory(), containsString("Submission 250 of 250 downloaded"));
assertThat(form.getStatusHistory(), containsString("Attachment 1 of 2 of submission 250 of 250 downloaded"));
assertThat(form.getStatusHistory(), containsString("Attachment 2 of 2 of submission 250 of 250 downloaded"));
assertThat(form.getStatusHistory(), containsString("Success"));
// Assert that the last pull cursor gets saved
Cursor actualLastCursor = formMetadataPort.query(lastCursorOf(FormKey.from(form))).orElseThrow(RuntimeException::new);
assertThat(actualLastCursor, is(submissionPages.get(3).getRight()));
}
Aggregations