use of org.odk.collect.forms.Form 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.Form 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));
}
use of org.odk.collect.forms.Form 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));
}
use of org.odk.collect.forms.Form in project collect by opendatakit.
the class FormsRepositoryTest method deleteAll_deletesAllForms.
@Test
public void deleteAll_deletesAllForms() {
FormsRepository formsRepository = buildSubject();
formsRepository.save(FormUtils.buildForm("id1", "version", getFormFilesPath()).build());
formsRepository.save(FormUtils.buildForm("id2", "version", getFormFilesPath()).build());
List<Form> forms = formsRepository.getAll();
formsRepository.deleteAll();
assertThat(formsRepository.getAll().size(), is(0));
for (Form form : forms) {
assertThat(new File(form.getFormFilePath()).exists(), is(false));
assertThat(new File(form.getFormMediaPath()).exists(), is(false));
}
}
use of org.odk.collect.forms.Form in project collect by opendatakit.
the class FormsRepositoryTest method getLatestByFormIdAndVersion_whenMultipleExist_returnsLatest.
@Test
public void getLatestByFormIdAndVersion_whenMultipleExist_returnsLatest() {
Supplier<Long> mockClock = mock(Supplier.class);
when(mockClock.get()).thenReturn(2L, 3L, 1L);
FormsRepository formsRepository = buildSubject(mockClock);
formsRepository.save(FormUtils.buildForm("1", "1", getFormFilesPath()).build());
formsRepository.save(FormUtils.buildForm("1", "1", getFormFilesPath()).build());
formsRepository.save(FormUtils.buildForm("1", "1", getFormFilesPath()).build());
Form form = formsRepository.getLatestByFormIdAndVersion("1", "1");
assertThat(form, notNullValue());
assertThat(form.getDbId(), is(2L));
}
Aggregations