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);
}
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
}
}
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()));
}
}
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);
}
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));
}
Aggregations