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"));
}
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"));
}
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));
}));
}
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"));
}
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());
}
Aggregations