Search in sources :

Example 31 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class ServerFormDownloaderTest method whenDownloadingCentralDraftWithVersion_andId_butNotHashOnDevice_doesNotLogAnalytics.

@Test
public void whenDownloadingCentralDraftWithVersion_andId_butNotHashOnDevice_doesNotLogAnalytics() throws Exception {
    String xform = FormUtils.createXFormBody("form", "version", "A title");
    Form form = buildForm("form", "version", getFormFilesPath(), xform).build();
    formsRepository.save(form);
    String xform2 = FormUtils.createXFormBody("form2", "version", "A different title");
    Form form2 = buildForm("form", "version", getFormFilesPath(), xform2).build();
    ServerFormDetails serverFormDetails = new ServerFormDetails(form2.getDisplayName(), "http://downloadUrl/draft.xml", form2.getFormId(), form2.getVersion(), Md5.getMd5Hash(new ByteArrayInputStream(xform2.getBytes())), true, false, null);
    FormSource formSource = mock(FormSource.class);
    when(formSource.fetchForm("http://downloadUrl/draft.xml")).thenReturn(new ByteArrayInputStream(xform2.getBytes()));
    Analytics mockAnalytics = mock(Analytics.class);
    ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mockAnalytics);
    downloader.downloadForm(serverFormDetails, null, null);
    verifyNoInteractions(mockAnalytics);
}
Also used : Form(org.odk.collect.forms.Form) FormUtils.buildForm(org.odk.collect.formstest.FormUtils.buildForm) ByteArrayInputStream(java.io.ByteArrayInputStream) FormSource(org.odk.collect.forms.FormSource) Analytics(org.odk.collect.analytics.Analytics) Test(org.junit.Test)

Example 32 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class ServerFormDownloaderTest method downloadsAndSavesForm.

@Test
public void downloadsAndSavesForm() throws Exception {
    String xform = createXFormBody("id", "version");
    ServerFormDetails serverFormDetails = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, null);
    FormSource formSource = mock(FormSource.class);
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
    ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
    downloader.downloadForm(serverFormDetails, null, null);
    List<Form> allForms = formsRepository.getAll();
    assertThat(allForms.size(), is(1));
    Form form = allForms.get(0);
    assertThat(form.getFormId(), is("id"));
    File formFile = new File(getAbsoluteFilePath(formsDir.getAbsolutePath(), form.getFormFilePath()));
    assertThat(formFile.exists(), is(true));
    assertThat(new String(read(formFile)), is(xform));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Form(org.odk.collect.forms.Form) FormUtils.buildForm(org.odk.collect.formstest.FormUtils.buildForm) FormSource(org.odk.collect.forms.FormSource) MediaFile(org.odk.collect.forms.MediaFile) File(java.io.File) ManifestFile(org.odk.collect.forms.ManifestFile) Analytics(org.odk.collect.analytics.Analytics) Test(org.junit.Test)

Example 33 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class ServerFormDownloaderTest method whenFormAlreadyDownloaded_formRemainsOnDevice.

// endregion
@Test
public void whenFormAlreadyDownloaded_formRemainsOnDevice() throws Exception {
    String xform = createXFormBody("id", "version");
    ServerFormDetails serverFormDetails = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, null);
    FormSource formSource = mock(FormSource.class);
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
    ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
    // Initial download
    downloader.downloadForm(serverFormDetails, null, null);
    ServerFormDetails serverFormDetailsAlreadyOnDevice = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), false, false, null);
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
    downloader.downloadForm(serverFormDetailsAlreadyOnDevice, null, null);
    List<Form> allForms = formsRepository.getAll();
    assertThat(allForms.size(), is(1));
    Form form = allForms.get(0);
    File formFile = new File(getAbsoluteFilePath(formsDir.getAbsolutePath(), form.getFormFilePath()));
    assertThat(new String(read(formFile)), is(xform));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) Form(org.odk.collect.forms.Form) FormUtils.buildForm(org.odk.collect.formstest.FormUtils.buildForm) FormSource(org.odk.collect.forms.FormSource) MediaFile(org.odk.collect.forms.MediaFile) File(java.io.File) ManifestFile(org.odk.collect.forms.ManifestFile) Analytics(org.odk.collect.analytics.Analytics) Test(org.junit.Test)

Example 34 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class FormsRepositoryTest method deleteAll_deletesAllForms.

@Test
public void deleteAll_deletesAllForms() {
    FormsRepository formsRepository = buildSubject();
    formsRepository.save(FormUtils.buildForm("id1", "version", getFormFilesPath()).build());
    formsRepository.save(FormUtils.buildForm("id2", "version", getFormFilesPath()).build());
    List<Form> forms = formsRepository.getAll();
    formsRepository.deleteAll();
    assertThat(formsRepository.getAll().size(), is(0));
    for (Form form : forms) {
        assertThat(new File(form.getFormFilePath()).exists(), is(false));
        assertThat(new File(form.getFormMediaPath()).exists(), is(false));
    }
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) File(java.io.File) Test(org.junit.Test)

Example 35 with Form

use of org.odk.collect.forms.Form in project collect by opendatakit.

the class FormsRepositoryTest method getLatestByFormIdAndVersion_whenMultipleExist_returnsLatest.

@Test
public void getLatestByFormIdAndVersion_whenMultipleExist_returnsLatest() {
    Supplier<Long> mockClock = mock(Supplier.class);
    when(mockClock.get()).thenReturn(2L, 3L, 1L);
    FormsRepository formsRepository = buildSubject(mockClock);
    formsRepository.save(FormUtils.buildForm("1", "1", getFormFilesPath()).build());
    formsRepository.save(FormUtils.buildForm("1", "1", getFormFilesPath()).build());
    formsRepository.save(FormUtils.buildForm("1", "1", getFormFilesPath()).build());
    Form form = formsRepository.getLatestByFormIdAndVersion("1", "1");
    assertThat(form, notNullValue());
    assertThat(form.getDbId(), is(2L));
}
Also used : FormsRepository(org.odk.collect.forms.FormsRepository) Form(org.odk.collect.forms.Form) Test(org.junit.Test)

Aggregations

Form (org.odk.collect.forms.Form)62 Test (org.junit.Test)35 File (java.io.File)22 FormsRepository (org.odk.collect.forms.FormsRepository)21 ByteArrayInputStream (java.io.ByteArrayInputStream)13 Analytics (org.odk.collect.analytics.Analytics)13 FormSource (org.odk.collect.forms.FormSource)12 FormUtils.buildForm (org.odk.collect.formstest.FormUtils.buildForm)12 MediaFile (org.odk.collect.forms.MediaFile)9 FormsRepositoryProvider (org.odk.collect.android.utilities.FormsRepositoryProvider)7 ManifestFile (org.odk.collect.forms.ManifestFile)7 Instance (org.odk.collect.forms.instances.Instance)7 LocalizedApplicationKt.getLocalizedString (org.odk.collect.strings.localization.LocalizedApplicationKt.getLocalizedString)7 ArrayList (java.util.ArrayList)5 FormController (org.odk.collect.android.javarosawrapper.FormController)4 ViewModelProvider (androidx.lifecycle.ViewModelProvider)3 DatabaseObjectMapper.getValuesFromForm (org.odk.collect.android.database.DatabaseObjectMapper.getValuesFromForm)3 Intent (android.content.Intent)2 View (android.view.View)2 TextView (android.widget.TextView)2