Search in sources :

Example 6 with FormSource

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

the class ServerFormDownloaderTest method whenFormHasMediaFiles_downloadsAndSavesFormAndMediaFiles.

@Test
public void whenFormHasMediaFiles_downloadsAndSavesFormAndMediaFiles() 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("contents1".getBytes()));
    when(formSource.fetchMediaFile("http://file2")).thenReturn(new ByteArrayInputStream("contents2".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));
    File mediaFile1 = new File(form.getFormMediaPath() + "/file1");
    assertThat(mediaFile1.exists(), is(true));
    assertThat(new String(read(mediaFile1)), is("contents1"));
    File mediaFile2 = new File(form.getFormMediaPath() + "/file2");
    assertThat(mediaFile2.exists(), is(true));
    assertThat(new String(read(mediaFile2)), is("contents2"));
}
Also used : MediaFile(org.odk.collect.forms.MediaFile) 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) ManifestFile(org.odk.collect.forms.ManifestFile) Analytics(org.odk.collect.analytics.Analytics) Test(org.junit.Test)

Example 7 with FormSource

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

the class ServerFormDownloaderTest method whenMultipleFormsWithSameFormIdVersionDeleted_reDownloadUnDeletesFormWithSameHash.

@Test
public void whenMultipleFormsWithSameFormIdVersionDeleted_reDownloadUnDeletesFormWithSameHash() throws Exception {
    String xform = FormUtils.createXFormBody("deleted-form", "version", "A title");
    Form form = buildForm("deleted-form", "version", getFormFilesPath(), xform).deleted(true).build();
    formsRepository.save(form);
    String xform2 = FormUtils.createXFormBody("deleted-form", "version", "A different title");
    Form form2 = buildForm("deleted-form", "version", getFormFilesPath(), xform2).deleted(true).build();
    formsRepository.save(form2);
    ServerFormDetails serverFormDetails = new ServerFormDetails(form2.getDisplayName(), "http://downloadUrl", form2.getFormId(), form2.getVersion(), Md5.getMd5Hash(new ByteArrayInputStream(xform2.getBytes())), true, false, null);
    FormSource formSource = mock(FormSource.class);
    when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform2.getBytes()));
    ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
    downloader.downloadForm(serverFormDetails, null, null);
    assertThat(formsRepository.get(1L).isDeleted(), is(true));
    assertThat(formsRepository.get(2L).isDeleted(), is(false));
}
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 8 with FormSource

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

the class ServerFormDownloaderTest method whenDownloadingFormWithVersion_andId_butNotHashOnDevice_logsAnalytics.

// endregion
// region Form update analytics
@Test
public void whenDownloadingFormWithVersion_andId_butNotHashOnDevice_logsAnalytics() 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", form2.getFormId(), form2.getVersion(), Md5.getMd5Hash(new ByteArrayInputStream(xform2.getBytes())), true, false, null);
    FormSource formSource = mock(FormSource.class);
    when(formSource.fetchForm("http://downloadUrl")).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);
    String formIdentifier = form.getDisplayName() + " " + form.getFormId();
    String formIdHash = Md5.getMd5Hash(new ByteArrayInputStream(formIdentifier.getBytes()));
    verify(mockAnalytics).logEventWithParam(DOWNLOAD_SAME_FORMID_VERSION_DIFFERENT_HASH, "form", formIdHash);
}
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 9 with FormSource

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

the class ServerFormDownloaderTest method whenFormAlreadyDownloaded_andFormHasNewMediaFiles_andMediaFetchFails_throwsFetchError.

@Test
public void whenFormAlreadyDownloaded_andFormHasNewMediaFiles_andMediaFetchFails_throwsFetchError() 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", Md5.getMd5Hash(new ByteArrayInputStream("contents".getBytes())), "http://file1"))));
    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()));
    ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), new FormMetadataParser(), mock(Analytics.class));
    // Initial download
    downloader.downloadForm(serverFormDetails, null, null);
    try {
        ServerFormDetails serverFormDetailsUpdatedMediaFile = new ServerFormDetails("Form", "http://downloadUrl", "id", "version", Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), false, false, new ManifestFile("", asList(new MediaFile("file1", Md5.getMd5Hash(new ByteArrayInputStream("contents-updated".getBytes())), "http://file1"))));
        when(formSource.fetchForm("http://downloadUrl")).thenReturn(new ByteArrayInputStream(xform.getBytes()));
        when(formSource.fetchMediaFile("http://file1")).thenThrow(new FormSourceException.FetchError());
        downloader.downloadForm(serverFormDetailsUpdatedMediaFile, null, null);
        fail("Expected exception");
    } catch (FormDownloadException.FormSourceError e) {
        // Check form is still intact
        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 : MediaFile(org.odk.collect.forms.MediaFile) FormSourceException(org.odk.collect.forms.FormSourceException) Form(org.odk.collect.forms.Form) FormUtils.buildForm(org.odk.collect.formstest.FormUtils.buildForm) ManifestFile(org.odk.collect.forms.ManifestFile) Analytics(org.odk.collect.analytics.Analytics) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) 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 10 with FormSource

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

the class ServerFormDownloaderTest method whenFormHasMediaFiles_andFileExistsInMediaDirPath_throwsDiskExceptionAndDoesNotSaveAnything.

@Test
public void whenFormHasMediaFiles_andFileExistsInMediaDirPath_throwsDiskExceptionAndDoesNotSaveAnything() 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")).thenReturn(new ByteArrayInputStream("contents1".getBytes()));
    // Create file where media dir would go
    assertThat(new File(formsDir, "Form-media").createNewFile(), is(true));
    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.DiskError 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) 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) 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