Search in sources :

Example 11 with FormSource

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

the class ServerFormDownloaderTest method whenDownloadingFormWithVersion_andId_andHashOnDevice_doesNotLogAnalytics.

@Test
public void whenDownloadingFormWithVersion_andId_andHashOnDevice_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();
    formsRepository.save(form2);
    ServerFormDetails serverFormDetails = new ServerFormDetails(form.getDisplayName(), "http://downloadUrl", form.getFormId(), form.getVersion(), Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, null);
    FormSource formSource = mock(FormSource.class);
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.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 12 with FormSource

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

the class ServerFormDownloaderTest method whenFormListMissingHash_throwsError.

@Test
public void whenFormListMissingHash_throwsError() throws Exception {
    String xform = createXFormBody("id", "version");
    ServerFormDetails serverFormDetails = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", null, 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));
    try {
        downloader.downloadForm(serverFormDetails, null, null);
        fail("Expected exception because of missing form hash");
    } catch (FormDownloadException.FormWithNoHash e) {
    // pass
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) FormSource(org.odk.collect.forms.FormSource) Analytics(org.odk.collect.analytics.Analytics) Test(org.junit.Test)

Example 13 with FormSource

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

the class ServerFormDownloaderTest method whenFormHasMediaFiles_andFetchingMediaFileFails_throwsFetchErrorAndDoesNotSaveAnything.

@Test
public void whenFormHasMediaFiles_andFetchingMediaFileFails_throwsFetchErrorAndDoesNotSaveAnything() throws Exception {
    String xform = createXFormBody("id", "version");
    ServerFormDetails serverFormDetails = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, new ManifestFile("", asList(new MediaFile("file1", "hash-1", "http://file1"))));
    FormSource formSource = mock(FormSource.class);
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
    when(formSource.fetchMediaFile("http://file1")).thenThrow(new FormSourceException.FetchError());
    ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
    try {
        downloader.downloadForm(serverFormDetails, null, null);
        fail("Expected exception");
    } catch (FormDownloadException.FormSourceError e) {
        assertThat(formsRepository.getAll(), is(empty()));
        assertThat(asList(new File(getCacheFilesPath()).listFiles()), is(empty()));
        assertThat(asList(new File(getFormFilesPath()).listFiles()), is(empty()));
    }
}
Also used : MediaFile(org.odk.collect.forms.MediaFile) FormSourceException(org.odk.collect.forms.FormSourceException) ManifestFile(org.odk.collect.forms.ManifestFile) Analytics(org.odk.collect.analytics.Analytics) ByteArrayInputStream(java.io.ByteArrayInputStream) FormSource(org.odk.collect.forms.FormSource) MediaFile(org.odk.collect.forms.MediaFile) File(java.io.File) ManifestFile(org.odk.collect.forms.ManifestFile) Test(org.junit.Test)

Example 14 with FormSource

use of org.odk.collect.forms.FormSource 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 15 with FormSource

use of org.odk.collect.forms.FormSource 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)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)17 Test (org.junit.Test)17 Analytics (org.odk.collect.analytics.Analytics)17 FormSource (org.odk.collect.forms.FormSource)17 Form (org.odk.collect.forms.Form)12 FormUtils.buildForm (org.odk.collect.formstest.FormUtils.buildForm)12 ManifestFile (org.odk.collect.forms.ManifestFile)11 MediaFile (org.odk.collect.forms.MediaFile)11 File (java.io.File)10 FormSourceException (org.odk.collect.forms.FormSourceException)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 List (java.util.List)1 Collectors.toList (java.util.stream.Collectors.toList)1