use of org.odk.collect.forms.ManifestFile 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));
}
}
use of org.odk.collect.forms.ManifestFile 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()));
}
}
use of org.odk.collect.forms.ManifestFile 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()));
}
}
use of org.odk.collect.forms.ManifestFile 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));
}
Aggregations