Search in sources :

Example 16 with FormSource

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

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

the class ServerFormDownloaderTest method beforeDownloadingEachMediaFile_reportsProgress.

@Test
public void beforeDownloadingEachMediaFile_reportsProgress() 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"), new MediaFile("file2", "hash-2", "http://file2"))));
    FormSource formSource = mock(FormSource.class);
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
    when(formSource.fetchMediaFile("http://file1")).thenReturn(new ByteArrayInputStream("contents".getBytes()));
    when(formSource.fetchMediaFile("http://file2")).thenReturn(new ByteArrayInputStream("contents".getBytes()));
    ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
    RecordingProgressReporter progressReporter = new RecordingProgressReporter();
    downloader.downloadForm(serverFormDetails, progressReporter, null);
    assertThat(progressReporter.reports, contains(1, 2));
}
Also used : MediaFile(org.odk.collect.forms.MediaFile) ByteArrayInputStream(java.io.ByteArrayInputStream) FormSource(org.odk.collect.forms.FormSource) 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