Search in sources :

Example 1 with TransferTestHelpers

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()));
}
Also used : UncheckedFiles.deleteRecursive(org.opendatakit.briefcase.reused.UncheckedFiles.deleteRecursive) RequestBuilder.url(org.opendatakit.briefcase.reused.http.RequestBuilder.url) URL(java.net.URL) JobsRunner.launchSync(org.opendatakit.briefcase.reused.job.JobsRunner.launchSync) Assert.assertThat(org.junit.Assert.assertThat) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UncheckedFiles.readAllBytes(org.opendatakit.briefcase.reused.UncheckedFiles.readAllBytes) After(org.junit.After) AggregateServer(org.opendatakit.briefcase.reused.transfer.AggregateServer) Pair(org.opendatakit.briefcase.reused.Pair) Path(java.nio.file.Path) FormKey(org.opendatakit.briefcase.model.form.FormKey) CommonsHttp(org.opendatakit.briefcase.reused.http.CommonsHttp) Moco.httpServer(com.github.dreamhead.moco.Moco.httpServer) TransferTestHelpers.buildMediaFiles(org.opendatakit.briefcase.reused.transfer.TransferTestHelpers.buildMediaFiles) FormStatus(org.opendatakit.briefcase.model.FormStatus) FormMetadataQueries.lastCursorOf(org.opendatakit.briefcase.model.form.FormMetadataQueries.lastCursorOf) Moco.method(com.github.dreamhead.moco.Moco.method) TransferTestHelpers.generatePages(org.opendatakit.briefcase.reused.transfer.TransferTestHelpers.generatePages) UncheckedFiles.createTempDirectory(org.opendatakit.briefcase.reused.UncheckedFiles.createTempDirectory) GET(com.github.dreamhead.moco.HttpMethod.GET) Collectors.joining(java.util.stream.Collectors.joining) TransferTestHelpers(org.opendatakit.briefcase.reused.transfer.TransferTestHelpers) UncheckedFiles.toURI(org.opendatakit.briefcase.reused.UncheckedFiles.toURI) List(java.util.List) Moco.seq(com.github.dreamhead.moco.Moco.seq) OffsetDateTime(java.time.OffsetDateTime) Runner.running(com.github.dreamhead.moco.Runner.running) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) Matchers.containsString(org.hamcrest.Matchers.containsString) HttpServer(com.github.dreamhead.moco.HttpServer) IntStream(java.util.stream.IntStream) InMemoryFormMetadataAdapter(org.opendatakit.briefcase.model.form.InMemoryFormMetadataAdapter) Moco.uri(com.github.dreamhead.moco.Moco.uri) BriefcasePreferences(org.opendatakit.briefcase.model.BriefcasePreferences) TransferTestHelpers.buildFormStatus(org.opendatakit.briefcase.reused.transfer.TransferTestHelpers.buildFormStatus) InMemoryPreferences(org.opendatakit.briefcase.model.InMemoryPreferences) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Before(org.junit.Before) TransferTestHelpers.buildManifestXml(org.opendatakit.briefcase.reused.transfer.TransferTestHelpers.buildManifestXml) Files(java.nio.file.Files) RunnerStatus(org.opendatakit.briefcase.reused.job.RunnerStatus) IOException(java.io.IOException) Test(org.junit.Test) Collectors.toList(java.util.stream.Collectors.toList) Paths(java.nio.file.Paths) DateTimeFormatter(java.time.format.DateTimeFormatter) TestRunnerStatus(org.opendatakit.briefcase.reused.job.TestRunnerStatus) Moco.by(com.github.dreamhead.moco.Moco.by) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransferTestHelpers(org.opendatakit.briefcase.reused.transfer.TransferTestHelpers) Matchers.containsString(org.hamcrest.Matchers.containsString) Pair(org.opendatakit.briefcase.reused.Pair) Test(org.junit.Test)

Aggregations

GET (com.github.dreamhead.moco.HttpMethod.GET)1 HttpServer (com.github.dreamhead.moco.HttpServer)1 Moco.by (com.github.dreamhead.moco.Moco.by)1 Moco.httpServer (com.github.dreamhead.moco.Moco.httpServer)1 Moco.method (com.github.dreamhead.moco.Moco.method)1 Moco.seq (com.github.dreamhead.moco.Moco.seq)1 Moco.uri (com.github.dreamhead.moco.Moco.uri)1 Runner.running (com.github.dreamhead.moco.Runner.running)1 IOException (java.io.IOException)1 URL (java.net.URL)1 Files (java.nio.file.Files)1 Path (java.nio.file.Path)1 Paths (java.nio.file.Paths)1 OffsetDateTime (java.time.OffsetDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 List (java.util.List)1 Optional (java.util.Optional)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 Collectors.joining (java.util.stream.Collectors.joining)1 Collectors.toList (java.util.stream.Collectors.toList)1