Search in sources :

Example 1 with CentralAttachment

use of org.opendatakit.briefcase.reused.transfer.CentralAttachment in project briefcase by opendatakit.

the class PullFromCentralTest method knows_how_to_download_a_submission_attachment.

@Test
public void knows_how_to_download_a_submission_attachment() {
    String instanceId = "some instance id";
    List<CentralAttachment> expectedAttachments = buildAttachments(3);
    expectedAttachments.forEach(attachment -> http.stub(server.getDownloadSubmissionAttachmentRequest(form.getFormId(), instanceId, attachment, form.getSubmissionMediaFile(briefcaseDir, instanceId, attachment.getName()), token), ok("some body")));
    AtomicInteger seq = new AtomicInteger(1);
    expectedAttachments.forEach(attachment -> pullOp.downloadSubmissionAttachment(form, instanceId, attachment, token, runnerStatus, tracker, 1, 1, seq.getAndIncrement(), 3));
    expectedAttachments.forEach(attachment -> assertThat(form.getSubmissionMediaFile(briefcaseDir, instanceId, attachment.getName()), exists()));
    assertThat(events, contains("  Start downloading attachment 1 of 3 of submission 1 of 1", "  Attachment 1 of 3 of submission 1 of 1 downloaded", "  Start downloading attachment 2 of 3 of submission 1 of 1", "  Attachment 2 of 3 of submission 1 of 1 downloaded", "  Start downloading attachment 3 of 3 of submission 1 of 1", "  Attachment 3 of 3 of submission 1 of 1 downloaded"));
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CentralAttachment(org.opendatakit.briefcase.reused.transfer.CentralAttachment) Test(org.junit.Test)

Example 2 with CentralAttachment

use of org.opendatakit.briefcase.reused.transfer.CentralAttachment in project briefcase by opendatakit.

the class PullFromCentralTest method knows_how_to_get_submission_attachments.

@Test
public void knows_how_to_get_submission_attachments() {
    String instanceId = "uuid:515a13cf-d7a5-4606-a18f-84940b0944b2";
    List<CentralAttachment> expectedAttachments = buildAttachments(3);
    http.stub(server.getSubmissionAttachmentListRequest(form.getFormId(), instanceId, token), ok(jsonOfAttachments(expectedAttachments)));
    List<CentralAttachment> actualAttachments = pullOp.getSubmissionAttachmentList(form, instanceId, token, runnerStatus, tracker, 1, 1);
    assertThat(actualAttachments, hasSize(expectedAttachments.size()));
    for (CentralAttachment attachment : actualAttachments) assertThat(expectedAttachments, hasItem(attachment));
    assertThat(events, contains("  Start getting attachment list of submission 1 of 1", "  Got attachment list of submission 1 of 1"));
}
Also used : CentralAttachment(org.opendatakit.briefcase.reused.transfer.CentralAttachment) Test(org.junit.Test)

Example 3 with CentralAttachment

use of org.opendatakit.briefcase.reused.transfer.CentralAttachment in project briefcase by opendatakit.

the class PullFromCentral method pull.

/**
 * Pulls a form completely, writing the form file, form attachments,
 * submission files and their attachments to the local filesystem
 * under the Briefcase Storage directory.
 */
public Job<Void> pull(FormStatus form) {
    FormKey key = FormKey.from(form);
    PullFromCentralTracker tracker = new PullFromCentralTracker(form, onEventCallback);
    return run(rs -> tracker.trackStart()).thenRun(allOf(supply(runnerStatus -> getSubmissionIds(form, token, runnerStatus, tracker)), run(runnerStatus -> {
        downloadForm(form, token, runnerStatus, tracker);
        List<CentralAttachment> attachments = getFormAttachments(form, token, runnerStatus, tracker);
        int totalAttachments = attachments.size();
        AtomicInteger attachmentNumber = new AtomicInteger(1);
        attachments.parallelStream().forEach(attachment -> downloadFormAttachment(form, attachment, token, runnerStatus, tracker, attachmentNumber.getAndIncrement(), totalAttachments));
    }))).thenAccept((runnerStatus, pair) -> withDb(form.getFormDir(briefcaseDir), db -> {
        List<String> submissions = pair.getLeft();
        int totalSubmissions = submissions.size();
        AtomicInteger submissionNumber = new AtomicInteger(1);
        Set<String> submissionVersions = new HashSet<>();
        if (submissions.isEmpty())
            tracker.trackNoSubmissions();
        submissions.stream().map(instanceId -> Pair.of(submissionNumber.getAndIncrement(), instanceId)).forEach(submissionNumberId -> {
            int currentSubmissionNumber = submissionNumberId.getLeft();
            String instanceId = submissionNumberId.getRight();
            boolean inDb = db.hasRecordedInstance(instanceId) != null;
            Path downloadedSubmissionPath = form.getSubmissionFile(briefcaseDir, instanceId);
            if (!inDb || !downloadedSubmissionPath.toFile().exists()) {
                downloadSubmission(form, instanceId, token, runnerStatus, tracker, currentSubmissionNumber, totalSubmissions);
                if (downloadedSubmissionPath.toFile().exists()) {
                    XmlElement root = XmlElement.from(new String(readAllBytes(downloadedSubmissionPath)));
                    SubmissionMetaData metaData = new SubmissionMetaData(root);
                    metaData.getVersion().ifPresent(submissionVersions::add);
                }
            } else {
                tracker.trackSubmissionAlreadyDownloaded(currentSubmissionNumber, totalSubmissions);
            }
            List<CentralAttachment> attachments = getSubmissionAttachmentList(form, instanceId, token, runnerStatus, tracker, currentSubmissionNumber, totalSubmissions);
            int totalAttachments = attachments.size();
            AtomicInteger attachmentNumber = new AtomicInteger(1);
            attachments.stream().filter(attachment -> !inDb || !form.getSubmissionMediaFile(briefcaseDir, instanceId, attachment.getName()).toFile().exists()).forEach(attachment -> downloadSubmissionAttachment(form, instanceId, attachment, token, runnerStatus, tracker, currentSubmissionNumber, totalSubmissions, attachmentNumber.getAndIncrement(), totalAttachments));
            if (!runnerStatus.isCancelled() && !inDb) {
                db.putRecordedInstanceDirectory(instanceId, form.getSubmissionDir(briefcaseDir, instanceId).toFile());
            }
        });
        tracker.trackEnd();
        formMetadataPort.execute(updateAsPulled(key, briefcaseDir, form.getFormDir(briefcaseDir), submissionVersions));
        EventBus.publish(PullEvent.Success.of(form, server));
    }));
}
Also used : Job.run(org.opendatakit.briefcase.reused.job.Job.run) CentralAttachment(org.opendatakit.briefcase.reused.transfer.CentralAttachment) Response(org.opendatakit.briefcase.reused.http.response.Response) Job.supply(org.opendatakit.briefcase.reused.job.Job.supply) SubmissionMetaData(org.opendatakit.briefcase.export.SubmissionMetaData) HashSet(java.util.HashSet) FormMetadataPort(org.opendatakit.briefcase.model.form.FormMetadataPort) PullEvent(org.opendatakit.briefcase.pull.PullEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UncheckedFiles.readAllBytes(org.opendatakit.briefcase.reused.UncheckedFiles.readAllBytes) EventBus(org.bushe.swing.event.EventBus) Pair(org.opendatakit.briefcase.reused.Pair) DatabaseUtils.withDb(org.opendatakit.briefcase.util.DatabaseUtils.withDb) Path(java.nio.file.Path) FormKey(org.opendatakit.briefcase.model.form.FormKey) FormStatus(org.opendatakit.briefcase.model.FormStatus) FormStatusEvent(org.opendatakit.briefcase.model.FormStatusEvent) CentralServer(org.opendatakit.briefcase.reused.transfer.CentralServer) Job.allOf(org.opendatakit.briefcase.reused.job.Job.allOf) Collections.emptyList(java.util.Collections.emptyList) Set(java.util.Set) RunnerStatus(org.opendatakit.briefcase.reused.job.RunnerStatus) FormMetadataCommands.updateAsPulled(org.opendatakit.briefcase.model.form.FormMetadataCommands.updateAsPulled) Http(org.opendatakit.briefcase.reused.http.Http) UncheckedFiles.createDirectories(org.opendatakit.briefcase.reused.UncheckedFiles.createDirectories) Consumer(java.util.function.Consumer) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) Job(org.opendatakit.briefcase.reused.job.Job) XmlElement(org.opendatakit.briefcase.export.XmlElement) Path(java.nio.file.Path) SubmissionMetaData(org.opendatakit.briefcase.export.SubmissionMetaData) HashSet(java.util.HashSet) Set(java.util.Set) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) XmlElement(org.opendatakit.briefcase.export.XmlElement) Collections.emptyList(java.util.Collections.emptyList) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) FormKey(org.opendatakit.briefcase.model.form.FormKey)

Example 4 with CentralAttachment

use of org.opendatakit.briefcase.reused.transfer.CentralAttachment in project briefcase by opendatakit.

the class PullFromCentralIntegrationTest method downloads_submission_attachment_if_not_on_disk.

@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"));
}
Also used : Path(java.nio.file.Path) Matchers.containsString(org.hamcrest.Matchers.containsString) CentralAttachment(org.opendatakit.briefcase.reused.transfer.CentralAttachment) Test(org.junit.Test)

Example 5 with CentralAttachment

use of org.opendatakit.briefcase.reused.transfer.CentralAttachment in project briefcase by opendatakit.

the class PullFromCentralIntegrationTest method knows_how_to_pull_a_form.

@Test
public void knows_how_to_pull_a_form() throws Exception {
    // 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 form attachments request
    List<CentralAttachment> formAttachments = buildAttachments(2);
    server.request(by(uri("/v1/projects/1/forms/some-form/attachments"))).response(jsonOfAttachments(formAttachments));
    // Stub the form attachments request
    formAttachments.forEach(attachment -> server.request(by(uri("/v1/projects/1/forms/some-form/attachments/" + attachment.getName()))).response("some attachment content"));
    // Stub the submissions request
    List<String> instanceIds = IntStream.range(0, 250).mapToObj(i -> "some_sequential_instance_id_" + (i + 1)).collect(Collectors.toList());
    server.request(by(uri("/v1/projects/1/forms/some-form/submissions"))).response(jsonOfSubmissions(instanceIds));
    // Stub all the 250 submissions, each one with a couple of attachments
    String submissionTpl = new String(readAllBytes(getPath("submission-download-template.xml")));
    String submissionDate = OffsetDateTime.now().format(DateTimeFormatter.ISO_OFFSET_DATE_TIME);
    instanceIds.forEach(instanceId -> {
        List<CentralAttachment> attachments = buildAttachments(2);
        String submissionXml = String.format(submissionTpl, instanceId, submissionDate, submissionDate, "some text", attachments.stream().map(a -> buildMediaFileXml(a.getName())).collect(joining("\n")));
        server.request(by(uri("/v1/projects/1/forms/some-form/submissions/" + instanceId + ".xml"))).response(submissionXml);
        server.request(by(uri("/v1/projects/1/forms/some-form/submissions/" + instanceId + "/attachments"))).response(jsonOfAttachments(attachments));
        attachments.forEach(attachment -> server.request(by(uri("/v1/projects/1/forms/some-form/submissions/" + instanceId + "/attachments/" + attachment.getName()))).response("some attachment content"));
    });
    // Run the pull operation and just check that some key events are published
    running(server, () -> launchSync(pullOp.pull(form)));
    assertThat(form.getStatusHistory(), containsString("Start pulling form and submissions"));
    assertThat(form.getStatusHistory(), containsString("Start getting submission IDs"));
    assertThat(form.getStatusHistory(), containsString("Got all the submission IDs"));
    assertThat(form.getStatusHistory(), containsString("Start downloading form"));
    assertThat(form.getStatusHistory(), containsString("Form downloaded"));
    assertThat(form.getStatusHistory(), containsString("Start getting form attachments"));
    assertThat(form.getStatusHistory(), containsString("Start downloading form attachment 2 of 2"));
    assertThat(form.getStatusHistory(), containsString("Start downloading form attachment 1 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("  Start getting attachment list of submission 1 of 250"));
    assertThat(form.getStatusHistory(), containsString("  Got attachment list of submission 1 of 250"));
    assertThat(form.getStatusHistory(), containsString("Start downloading submission 250 of 250"));
    assertThat(form.getStatusHistory(), containsString("Submission 250 of 250 downloaded"));
    assertThat(form.getStatusHistory(), containsString("  Start getting attachment list of submission 250 of 250"));
    assertThat(form.getStatusHistory(), containsString("  Got attachment list of submission 250 of 250"));
    assertThat(form.getStatusHistory(), containsString("Start downloading attachment 1 of 2 of submission 250 of 250"));
    assertThat(form.getStatusHistory(), containsString("Attachment 1 of 2 of submission 250 of 250 downloaded"));
    assertThat(form.getStatusHistory(), containsString("Start downloading attachment 2 of 2 of submission 250 of 250"));
    assertThat(form.getStatusHistory(), containsString("Attachment 2 of 2 of submission 250 of 250 downloaded"));
    // Assert that saves form metadata
    assertThat(formMetadataPort.fetch(FormKey.from(form)), isPresent());
}
Also used : IntStream(java.util.stream.IntStream) InMemoryFormMetadataAdapter(org.opendatakit.briefcase.model.form.InMemoryFormMetadataAdapter) UncheckedFiles.deleteRecursive(org.opendatakit.briefcase.reused.UncheckedFiles.deleteRecursive) CentralAttachment(org.opendatakit.briefcase.reused.transfer.CentralAttachment) Credentials(org.opendatakit.briefcase.reused.http.Credentials) Moco.uri(com.github.dreamhead.moco.Moco.uri) RequestBuilder.url(org.opendatakit.briefcase.reused.http.RequestBuilder.url) URL(java.net.URL) OptionalMatchers.isPresent(com.github.npathai.hamcrestopt.OptionalMatchers.isPresent) TransferTestHelpers.buildFormStatus(org.opendatakit.briefcase.reused.transfer.TransferTestHelpers.buildFormStatus) JobsRunner.launchSync(org.opendatakit.briefcase.reused.job.JobsRunner.launchSync) Assert.assertThat(org.junit.Assert.assertThat) PullFromCentralTest.jsonOfAttachments(org.opendatakit.briefcase.pull.central.PullFromCentralTest.jsonOfAttachments) UncheckedFiles.readAllBytes(org.opendatakit.briefcase.reused.UncheckedFiles.readAllBytes) After(org.junit.After) 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) Before(org.junit.Before) FormStatus(org.opendatakit.briefcase.model.FormStatus) CentralServer(org.opendatakit.briefcase.reused.transfer.CentralServer) Files(java.nio.file.Files) TransferTestHelpers.buildMediaFileXml(org.opendatakit.briefcase.reused.transfer.TransferTestHelpers.buildMediaFileXml) Test(org.junit.Test) UncheckedFiles.createTempDirectory(org.opendatakit.briefcase.reused.UncheckedFiles.createTempDirectory) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) PullFromCentralTest.jsonOfSubmissions(org.opendatakit.briefcase.pull.central.PullFromCentralTest.jsonOfSubmissions) UncheckedFiles.toURI(org.opendatakit.briefcase.reused.UncheckedFiles.toURI) TransferTestHelpers.buildSubmissionXml(org.opendatakit.briefcase.reused.transfer.TransferTestHelpers.buildSubmissionXml) List(java.util.List) PullFromCentralTest.buildAttachments(org.opendatakit.briefcase.pull.central.PullFromCentralTest.buildAttachments) OffsetDateTime(java.time.OffsetDateTime) Paths(java.nio.file.Paths) Runner.running(com.github.dreamhead.moco.Runner.running) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) Matchers.is(org.hamcrest.Matchers.is) PathMatchers.exists(org.opendatakit.briefcase.matchers.PathMatchers.exists) Matchers.containsString(org.hamcrest.Matchers.containsString) Collections(java.util.Collections) Moco.by(com.github.dreamhead.moco.Moco.by) HttpServer(com.github.dreamhead.moco.HttpServer) Matchers.containsString(org.hamcrest.Matchers.containsString) CentralAttachment(org.opendatakit.briefcase.reused.transfer.CentralAttachment) Test(org.junit.Test)

Aggregations

CentralAttachment (org.opendatakit.briefcase.reused.transfer.CentralAttachment)8 Test (org.junit.Test)7 Path (java.nio.file.Path)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Matchers.containsString (org.hamcrest.Matchers.containsString)3 List (java.util.List)2 FormStatus (org.opendatakit.briefcase.model.FormStatus)2 FormKey (org.opendatakit.briefcase.model.form.FormKey)2 UncheckedFiles.readAllBytes (org.opendatakit.briefcase.reused.UncheckedFiles.readAllBytes)2 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.uri (com.github.dreamhead.moco.Moco.uri)1 Runner.running (com.github.dreamhead.moco.Runner.running)1 OptionalMatchers.isPresent (com.github.npathai.hamcrestopt.OptionalMatchers.isPresent)1 URL (java.net.URL)1 Files (java.nio.file.Files)1 Paths (java.nio.file.Paths)1 OffsetDateTime (java.time.OffsetDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1