Search in sources :

Example 1 with Cursor

use of org.opendatakit.briefcase.pull.aggregate.Cursor in project briefcase by opendatakit.

the class AggregateServerTest method knows_how_to_build_instance_id_batch_urls.

@Test
public void knows_how_to_build_instance_id_batch_urls() throws UnsupportedEncodingException {
    assertThat(server.getInstanceIdBatchRequest("some-form", 100, Cursor.empty(), true).getUrl().toString(), is("https://some.server.com/view/submissionList?formId=some-form&cursor=&numEntries=100&includeIncomplete=true"));
    Cursor cursor = Cursor.of(LocalDate.parse("2010-01-01"));
    assertThat(server.getInstanceIdBatchRequest("some-form", 100, cursor, false).getUrl().toString(), is("https://some.server.com/view/submissionList?formId=some-form&cursor=" + encode(cursor.getValue(), UTF_8.toString()) + "&numEntries=100&includeIncomplete=false"));
}
Also used : Cursor(org.opendatakit.briefcase.pull.aggregate.Cursor) Test(org.junit.Test)

Example 2 with Cursor

use of org.opendatakit.briefcase.pull.aggregate.Cursor in project briefcase by opendatakit.

the class FormMetadataQueriesTest method queries_the_last_cursor_of_a_form.

@Test
public void queries_the_last_cursor_of_a_form() {
    FormKey key = FormKey.of("Some form", "some-form");
    Path storageRoot = Paths.get("/some/path");
    Path formDir = storageRoot.resolve("forms/Some form");
    FormMetadata formMetadata = new FormMetadata(key, storageRoot, formDir, false, Cursor.empty(), Optional.empty(), Collections.emptySet());
    FormMetadataPort formMetadataPort = new InMemoryFormMetadataAdapter();
    formMetadataPort.persist(formMetadata);
    assertThat(formMetadataPort.query(lastCursorOf(key)), isPresentAndIs(Cursor.empty()));
    Cursor cursor = Cursor.from("some cursor");
    formMetadataPort.execute(FormMetadataCommands.updateAsPulled(key, cursor, storageRoot, formDir, new HashSet<>()));
    assertThat(formMetadataPort.query(lastCursorOf(key)), isPresentAndIs(cursor));
}
Also used : Path(java.nio.file.Path) Cursor(org.opendatakit.briefcase.pull.aggregate.Cursor) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 3 with Cursor

use of org.opendatakit.briefcase.pull.aggregate.Cursor in project briefcase by opendatakit.

the class FileSystemFormMetadataAdapterTest method persists_and_fetches_form_metadata.

@Test
public void persists_and_fetches_form_metadata() {
    // The filesystem-based adapter we're testing uses an in-memory cache of metadata
    // to avoid hitting the filesystem each time we need to fetch one.
    // In this tests, we persist and fetch a range of possible metadata objects with
    // all combinations of version and lastCursor, and we use different adapter instances
    // for persisting and fetching them to work around the in-memory cache.
    Arrays.<Pair<Supplier<Path>, Cursor>>asList(Pair.of(() -> installForm(storageRoot, "Form 1", "form-1", Optional.empty()), Cursor.empty()), Pair.of(() -> installForm(storageRoot, "Form 2", "form-2", Optional.of("20190701002")), Cursor.empty()), Pair.of(() -> installForm(storageRoot, "Form 3", "form-3", Optional.of("20190701003")), AggregateCursor.of(OffsetDateTime.now(), "uuid:" + UUID.randomUUID().toString())), Pair.of(() -> installForm(storageRoot, "Form 4", "form-4", Optional.of("20190701004")), OnaCursor.from("1234")), Pair.of(() -> installForm(storageRoot, "Form 5", "form-5", Optional.empty()), AggregateCursor.of(OffsetDateTime.now(), "uuid:" + UUID.randomUUID().toString())), Pair.of(() -> installForm(storageRoot, "Form 6", "form-6", Optional.empty()), OnaCursor.from("1234"))).forEach(pair -> {
        // Clean the storage root to isolate side-effects of each iteration
        deleteRecursive(storageRoot);
        createDirectories(storageRoot.resolve("forms"));
        // Get the scenario settings of this test
        Supplier<Path> formInstaller = pair.getLeft();
        Cursor cursor = pair.getRight();
        // Install the form in the storage root, and get a metadata object from it
        Path formFile = formInstaller.get();
        FormMetadata metadataToBePersisted = buildMetadataFrom(storageRoot, formFile, cursor);
        // Persist metadata into the filesystem
        FormMetadataPort persistPort = FileSystemFormMetadataAdapter.at(storageRoot);
        persistPort.persist(metadataToBePersisted);
        // Can't use the same adapter because it would return the object stored
        // in its in-memory cache, and we want to force the adapter to go search for
        // it in the filesystem.
        FormMetadataPort fetchPort = FileSystemFormMetadataAdapter.at(storageRoot);
        Optional<FormMetadata> fetchedMetadata = fetchPort.fetch(metadataToBePersisted.getKey());
        assertThat(fetchedMetadata, isPresentAnd(is(metadataToBePersisted)));
    });
}
Also used : Path(java.nio.file.Path) Supplier(java.util.function.Supplier) OnaCursor(org.opendatakit.briefcase.pull.aggregate.OnaCursor) AggregateCursor(org.opendatakit.briefcase.pull.aggregate.AggregateCursor) Cursor(org.opendatakit.briefcase.pull.aggregate.Cursor) Test(org.junit.Test)

Example 4 with Cursor

use of org.opendatakit.briefcase.pull.aggregate.Cursor in project briefcase by opendatakit.

the class FormMetadataCommandsTest method updates_a_form_as_having_been_pulled_adding_the_last_cursor_used_to_pull_it.

@Test
public void updates_a_form_as_having_been_pulled_adding_the_last_cursor_used_to_pull_it() {
    Cursor cursor = Cursor.from("some cursor data");
    formMetadataPort.execute(updateAsPulled(key, cursor, storageRoot, formDir, Collections.emptySet()));
    FormMetadata formMetadata = formMetadataPort.fetch(key).get();
    assertThat(formMetadata.hasBeenPulled(), is(true));
    assertThat(formMetadata.getCursor(), is(cursor));
}
Also used : Cursor(org.opendatakit.briefcase.pull.aggregate.Cursor) Test(org.junit.Test)

Example 5 with Cursor

use of org.opendatakit.briefcase.pull.aggregate.Cursor in project briefcase by opendatakit.

the class TransferTestHelpers method generatePages.

public static List<Pair<String, Cursor>> generatePages(int totalIds, int idsPerPage) {
    AtomicInteger idSeq = new AtomicInteger(0);
    OffsetDateTime startingDateTime = OffsetDateTime.parse("2010-01-01T00:00:00.000Z");
    Cursor lastCursor = Cursor.empty();
    List<Pair<String, Cursor>> pages = new ArrayList<>();
    for (int page : IntStream.range(0, (totalIds / idsPerPage) + 1).boxed().collect(Collectors.toList())) {
        int from = page * idsPerPage;
        int to = min(totalIds, (page + 1) * idsPerPage);
        List<String> ids = IntStream.range(from, to).mapToObj(i -> buildSequentialUid(idSeq.getAndIncrement())).collect(Collectors.toList());
        Cursor cursor = Cursor.of(startingDateTime.plusDays(to - 1), buildSequentialUid(idSeq.get()));
        pages.add(Pair.of("" + "<idChunk xmlns=\"http://opendatakit.org/submissions\">" + "<idList>" + ids.stream().map(id -> "<id>" + id + "</id>").collect(joining("")) + "</idList>" + "<resumptionCursor>" + escape(cursor.getValue()) + "</resumptionCursor>" + "</idChunk>" + "", cursor));
        lastCursor = cursor;
    }
    pages.add(Pair.of("" + "<idChunk xmlns=\"http://opendatakit.org/submissions\">" + "<idList></idList>" + "<resumptionCursor>" + escape(lastCursor.getValue()) + "</resumptionCursor>" + "</idChunk>" + "", lastCursor));
    return pages;
}
Also used : IntStream(java.util.stream.IntStream) ZonedDateTime(java.time.ZonedDateTime) SubmissionMetaData(org.opendatakit.briefcase.export.SubmissionMetaData) ArrayList(java.util.ArrayList) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) UncheckedFiles.readAllBytes(org.opendatakit.briefcase.reused.UncheckedFiles.readAllBytes) Pair(org.opendatakit.briefcase.reused.Pair) ZoneOffset(java.time.ZoneOffset) Path(java.nio.file.Path) RemoteFormDefinition(org.opendatakit.briefcase.model.RemoteFormDefinition) FormStatus(org.opendatakit.briefcase.model.FormStatus) Cursor(org.opendatakit.briefcase.pull.aggregate.Cursor) IOException(java.io.IOException) Math.min(java.lang.Math.min) Files.createDirectories(java.nio.file.Files.createDirectories) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) UncheckedFiles.toURI(org.opendatakit.briefcase.reused.UncheckedFiles.toURI) Collectors.toList(java.util.stream.Collectors.toList) List(java.util.List) AggregateAttachment(org.opendatakit.briefcase.pull.aggregate.AggregateAttachment) OffsetDateTime(java.time.OffsetDateTime) Stream(java.util.stream.Stream) Paths(java.nio.file.Paths) Files.copy(java.nio.file.Files.copy) DateTimeFormatter(java.time.format.DateTimeFormatter) Optional(java.util.Optional) XmlElement(org.opendatakit.briefcase.export.XmlElement) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) OffsetDateTime(java.time.OffsetDateTime) ArrayList(java.util.ArrayList) Cursor(org.opendatakit.briefcase.pull.aggregate.Cursor) Pair(org.opendatakit.briefcase.reused.Pair)

Aggregations

Cursor (org.opendatakit.briefcase.pull.aggregate.Cursor)6 Path (java.nio.file.Path)4 Test (org.junit.Test)4 Optional (java.util.Optional)2 FormStatus (org.opendatakit.briefcase.model.FormStatus)2 IOException (java.io.IOException)1 Math.min (java.lang.Math.min)1 Files.copy (java.nio.file.Files.copy)1 Files.createDirectories (java.nio.file.Files.createDirectories)1 Paths (java.nio.file.Paths)1 LocalDate (java.time.LocalDate)1 LocalDateTime (java.time.LocalDateTime)1 OffsetDateTime (java.time.OffsetDateTime)1 ZoneOffset (java.time.ZoneOffset)1 ZonedDateTime (java.time.ZonedDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 ISO_DATE_TIME (java.time.format.DateTimeFormatter.ISO_DATE_TIME)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 HashSet (java.util.HashSet)1