diff --git a/collect_app/src/test/java/org/odk/collect/android/formmanagement/download/ServerFormDownloaderTest.java b/collect_app/src/test/java/org/odk/collect/android/formmanagement/download/ServerFormDownloaderTest.java index 1c078e80181..e2276a6e619 100644 --- a/collect_app/src/test/java/org/odk/collect/android/formmanagement/download/ServerFormDownloaderTest.java +++ b/collect_app/src/test/java/org/odk/collect/android/formmanagement/download/ServerFormDownloaderTest.java @@ -349,7 +349,7 @@ public void whenFormHasMediaFiles_andFetchingMediaFileFails_throwsFetchErrorAndD Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, - new ManifestFile("", asList( + new ManifestFile("", List.of( new MediaFile("file1", "hash-1", "http://file1") ))); @@ -380,7 +380,7 @@ public void whenFormHasMediaFiles_andFileExistsInMediaDirPath_throwsDiskExceptio Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, - new ManifestFile("", asList( + new ManifestFile("", List.of( new MediaFile("file1", "hash-1", "http://file1") ))); @@ -545,7 +545,7 @@ public void whenFormAlreadyDownloaded_andFormHasNewMediaFiles_updatesMediaFilesA Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, - new ManifestFile("", asList( + new ManifestFile("", List.of( new MediaFile("file1", Md5.getMd5Hash(new ByteArrayInputStream("contents".getBytes())), "http://file1") ))); @@ -571,7 +571,7 @@ public void whenFormAlreadyDownloaded_andFormHasNewMediaFiles_updatesMediaFilesA Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), false, false, - new ManifestFile("", asList( + new ManifestFile("", List.of( new MediaFile("file1", Md5.getMd5Hash(new ByteArrayInputStream("contents-updated".getBytes())), "http://file1") ))); @@ -608,7 +608,7 @@ public void whenFormAlreadyDownloaded_andFormHasNewMediaFiles_andMediaFetchFails Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), true, false, - new ManifestFile("", asList( + new ManifestFile("", List.of( new MediaFile("file1", Md5.getMd5Hash(new ByteArrayInputStream("contents".getBytes())), "http://file1") ))); @@ -630,7 +630,7 @@ public void whenFormAlreadyDownloaded_andFormHasNewMediaFiles_andMediaFetchFails Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), false, false, - new ManifestFile("", asList( + new ManifestFile("", List.of( new MediaFile("file1", Md5.getMd5Hash(new ByteArrayInputStream("contents-updated".getBytes())), "http://file1") ))); @@ -706,6 +706,118 @@ public void afterDownloadingMediaFile_cancelling_throwsDownloadingInterruptedExc } } + @Test + public void whenFormHasEntityAttachmentsButDoesNotContainAnEntityBlock_downloadsAndSavesFormAsAFormThatUsesEntities() 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("", List.of( + new MediaFile("file1", "hash-1", "http://file1", true) + ))); + + 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())); + + ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), FormMetadataParser.INSTANCE, clock, entitiesRepository); + downloader.downloadForm(serverFormDetails, null, null); + + List
allForms = formsRepository.getAll(); + assertThat(allForms.size(), is(1)); + Form form = allForms.get(0); + assertThat(form.usesEntities(), is(true)); + } + + @Test + public void whenFormHasEntityAttachmentsAndContainsAnEntityBlock_downloadsAndSavesFormAsAFormThatUsesEntities() throws Exception { + String xform = createXFormBody("id", "version", "Test Form", "2024.1.0"); + ServerFormDetails serverFormDetails = new ServerFormDetails( + "Form", + "http://downloadUrl", + "id", + "version", + Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), + true, + false, + new ManifestFile("", List.of( + new MediaFile("file1", "hash-1", "http://file1", true) + ))); + + 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())); + + ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), FormMetadataParser.INSTANCE, clock, entitiesRepository); + downloader.downloadForm(serverFormDetails, null, null); + + List allForms = formsRepository.getAll(); + assertThat(allForms.size(), is(1)); + Form form = allForms.get(0); + assertThat(form.usesEntities(), is(true)); + } + + @Test + public void whenFormDoesNotHaveEntityAttachmentsButContainsAnEntityBlock_downloadsAndSavesFormAsAFormThatUsesEntities() throws Exception { + String xform = createXFormBody("id", "version", "Test Form", "2024.1.0"); + ServerFormDetails serverFormDetails = new ServerFormDetails( + "Form", + "http://downloadUrl", + "id", + "version", + Md5.getMd5Hash(new ByteArrayInputStream(xform.getBytes())), + true, + false, + new ManifestFile("", List.of( + 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())); + + ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), FormMetadataParser.INSTANCE, clock, entitiesRepository); + downloader.downloadForm(serverFormDetails, null, null); + + List allForms = formsRepository.getAll(); + assertThat(allForms.size(), is(1)); + Form form = allForms.get(0); + assertThat(form.usesEntities(), is(true)); + } + + @Test + public void whenFormDoesNotHaveEntityAttachmentsAndDoesNotContainAnEntityBlock_downloadsAndSavesFormAsAFormThatDoesNotUseEntities() 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("", List.of( + 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())); + + ServerFormDownloader downloader = new ServerFormDownloader(formSource, formsRepository, cacheDir, formsDir.getAbsolutePath(), FormMetadataParser.INSTANCE, clock, entitiesRepository); + downloader.downloadForm(serverFormDetails, null, null); + + List allForms = formsRepository.getAll(); + assertThat(allForms.size(), is(1)); + Form form = allForms.get(0); + assertThat(form.usesEntities(), is(false)); + } + private String getFormFilesPath() { return formsDir.getAbsolutePath(); } diff --git a/forms-test/src/main/java/org/odk/collect/formstest/FormUtils.kt b/forms-test/src/main/java/org/odk/collect/formstest/FormUtils.kt index bcd425a6ff4..761df2665b7 100644 --- a/forms-test/src/main/java/org/odk/collect/formstest/FormUtils.kt +++ b/forms-test/src/main/java/org/odk/collect/formstest/FormUtils.kt @@ -9,12 +9,14 @@ import java.nio.charset.Charset object FormUtils { @JvmStatic @JvmOverloads - fun createXFormBody(formId: String, version: String?, title: String = "Test Form"): String { + fun createXFormBody(formId: String, version: String?, title: String = "Test Form", entitiesVersion: String? = null): String { + val entitiesVersionAttribute = entitiesVersion?.let { """entities:entities-version="$it"""" } ?: "" + return """ - + $title - +