Skip to content

Commit

Permalink
Added new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
grzesiek2010 committed Oct 25, 2024
1 parent 57c2b95 commit d367482
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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")
)));

Expand Down Expand Up @@ -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")
)));

Expand Down Expand Up @@ -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")
)));

Expand All @@ -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")
)));

Expand Down Expand Up @@ -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")
)));

Expand All @@ -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")
)));

Expand Down Expand Up @@ -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<Form> 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<Form> 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<Form> 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<Form> allForms = formsRepository.getAll();
assertThat(allForms.size(), is(1));
Form form = allForms.get(0);
assertThat(form.usesEntities(), is(false));
}

private String getFormFilesPath() {
return formsDir.getAbsolutePath();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 """<?xml version="1.0"?>
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<h:html xmlns="http://www.w3.org/2002/xforms" xmlns:ev="http://www.w3.org/2001/xml-events" xmlns:h="http://www.w3.org/1999/xhtml" xmlns:jr="http://openrosa.org/javarosa" xmlns:orx="http://openrosa.org/xforms" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:entities="http://www.opendatakit.org/xforms/entities">
<h:head>
<h:title>$title</h:title>
<model>
<model $entitiesVersionAttribute>
<instance>
<data id="$formId" orx:version="$version">
<question/>
Expand Down

0 comments on commit d367482

Please sign in to comment.