Skip to content

Commit

Permalink
Do not map incomplete iterations in subflows during PDF generation
Browse files Browse the repository at this point in the history
[#185984169]

Co-authored-by: Chibuisi Enyia <cenyia@codeforamerica.org>
  • Loading branch information
spokenbird and enyia21 committed Oct 11, 2023
1 parent b60a7cc commit 386fb21
Show file tree
Hide file tree
Showing 3 changed files with 101 additions and 22 deletions.
17 changes: 11 additions & 6 deletions src/main/java/formflow/library/pdf/SubflowFieldPreparer.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public Map<String, SubmissionField> prepareSubmissionFields(Submission submissio
Map<String, SubmissionField> preppedFields = new HashMap<>();
List<Map<String, Object>> subflowDataList = new ArrayList<>();
Map<String, PdfMapSubflow> subflowMap = pdfMap.getSubflowInfo();
final List<String> IGNORED_FIELDS = List.of("uuid", "iterationIsComplete");

if (subflowMap == null) {
return Collections.emptyMap();
Expand All @@ -92,7 +93,7 @@ public Map<String, SubmissionField> prepareSubmissionFields(Submission submissio
subflowDataList.addAll((List<Map<String, Object>>) submission.getInputData().get(pdfSubflowName));
}

if (subflowDataList.size() > 0) {
if (!subflowDataList.isEmpty()) {

AtomicInteger atomInteger = new AtomicInteger(1);
subflowDataList.forEach(iteration -> {
Expand All @@ -101,12 +102,16 @@ public Map<String, SubmissionField> prepareSubmissionFields(Submission submissio
if (atomInteger.get() > pdfSubflow.getTotalIterations()) {
return;
}

// remove unnecessary fields
iteration.remove("uuid");
iteration.remove("iterationIsComplete");

if (iteration.get("iterationIsComplete") != null && iteration.get("iterationIsComplete").equals(false)) {
return;
}

iteration.forEach((key, value) -> {

if (IGNORED_FIELDS.contains(key)) {
return;
}

String newKey = getNewKey(key, atomInteger.get());

if (!pdfMap.getAllFields().containsKey(newKey.replace("[]", ""))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ void shouldReturn404WhenFlowDoesNotExist() throws Exception {
}

@Test
public void getDownloadGeneratesAndReturnsFilledFlattenedPdf() throws Exception {
void getDownloadGeneratesAndReturnsFilledFlattenedPdf() throws Exception {
session.setAttribute("id", submission.getId());
MvcResult result = mockMvc.perform(get("/download/ubi/" + submission.getId()).session(session))
.andExpect(header().string(HttpHeaders.CONTENT_DISPOSITION,
Expand All @@ -77,7 +77,7 @@ public void getDownloadGeneratesAndReturnsFilledFlattenedPdf() throws Exception
}

@Test
public void shouldNotAllowDownloadingAPdfWithADifferentSubmissionIdThanTheActiveSession() throws Exception {
void shouldNotAllowDownloadingAPdfWithADifferentSubmissionIdThanTheActiveSession() throws Exception {
session.setAttribute("id", UUID.randomUUID());

mockMvc.perform(get("/download/ubi/" + submission.getId()).session(session))
Expand Down
102 changes: 88 additions & 14 deletions src/test/java/formflow/library/pdf/SubflowFieldPreparersTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,42 +50,42 @@ void shouldNotMapIterationsInASubflowGreaterThanMaxIterations() {
iteration1.put("foo", "the foo 1");
iteration1.put("bar", "the bar 1");
iteration1.put("uuid", "uuid1");
iteration1.put("iterationIsComplete", "true");
iteration1.put("iterationIsComplete", true);
Map<String, Object> iteration2 = new HashMap<>();
iteration2.put("foo", "the foo 2");
iteration2.put("bar", "the bar 2");
iteration2.put("uuid", "uuid2");
iteration2.put("iterationIsComplete", "true");
iteration2.put("iterationIsComplete", true);
Map<String, Object> iteration3 = new HashMap<>();
iteration3.put("foo", "the foo 3");
iteration3.put("bar", "the bar 3");
iteration3.put("uuid", "uuid3");
iteration3.put("iterationIsComplete", "true");
iteration3.put("iterationIsComplete", true);
Map<String, Object> iteration4 = new HashMap<>();
iteration4.put("foo", "the foo 4");
iteration4.put("bar", "the bar 4");
iteration4.put("uuid", "uuid4");
iteration4.put("iterationIsComplete", "true");
iteration4.put("iterationIsComplete", true);
Map<String, Object> iteration5 = new HashMap<>();
iteration5.put("foo", "the foo 5");
iteration5.put("bar", "the bar 5");
iteration5.put("uuid", "uuid5");
iteration5.put("iterationIsComplete", "true");
iteration5.put("iterationIsComplete", true);
Map<String, Object> iteration6 = new HashMap<>();
iteration6.put("foo", "the foo 6");
iteration6.put("bar", "the bar 6");
iteration6.put("uuid", "uuid6");
iteration6.put("iterationIsComplete", "true");
iteration6.put("iterationIsComplete", true);
Map<String, Object> iteration7 = new HashMap<>();
iteration7.put("foo", "the foo 7");
iteration7.put("bar", "the bar 7");
iteration7.put("uuid", "uuid7");
iteration7.put("iterationIsComplete", "true");
iteration7.put("iterationIsComplete", true);
Map<String, Object> iteration8 = new HashMap<>();
iteration8.put("foo", "the foo 8");
iteration8.put("bar", "the bar 8");
iteration8.put("uuid", "uuid8");
iteration8.put("iterationIsComplete", "true");
iteration8.put("iterationIsComplete", true);
submission = Submission.builder().flow("flow1")
.inputData(
Map.of(
Expand Down Expand Up @@ -130,17 +130,17 @@ void submissionWithLessIterationsThanMaxIterationsShouldOnlyMapExistingIteration
iteration1.put("foo", "the foo 1");
iteration1.put("bar", "the bar 1");
iteration1.put("uuid", "uuid1");
iteration1.put("iterationIsComplete", "true");
iteration1.put("iterationIsComplete", true);
Map<String, Object> iteration2 = new HashMap<>();
iteration2.put("foo", "the foo 2");
iteration2.put("bar", "the bar 2");
iteration2.put("uuid", "uuid2");
iteration2.put("iterationIsComplete", "true");
iteration2.put("iterationIsComplete", true);
Map<String, Object> iteration3 = new HashMap<>();
iteration3.put("foo", "the foo 3");
iteration3.put("bar", "the bar 3");
iteration3.put("uuid", "uuid3");
iteration3.put("iterationIsComplete", "true");
iteration3.put("iterationIsComplete", true);

submission = Submission.builder().flow("flow1")
.inputData(
Expand Down Expand Up @@ -177,13 +177,13 @@ void shouldAddCorrectSuffixForCheckboxFieldsInSubflows() {
iteration1.put("bar", "the bar 1");
iteration1.put("checkboxInput[]", List.of("item1", "item2", "item3"));
iteration1.put("uuid", "uuid1");
iteration1.put("iterationIsComplete", "true");
iteration1.put("iterationIsComplete", true);
Map<String, Object> iteration2 = new HashMap<>();
iteration2.put("foo", "the foo 2");
iteration2.put("bar", "the bar 2");
iteration2.put("checkboxInput[]", List.of("item1", "item2", "item3"));
iteration2.put("uuid", "uuid2");
iteration2.put("iterationIsComplete", "true");
iteration2.put("iterationIsComplete", true);

submission = Submission.builder().flow("flow1")
.inputData(
Expand Down Expand Up @@ -223,7 +223,6 @@ void shouldNotThrownWhenNoSubflow() {
)
);
pdfMapWithoutSubflow.setFlow("flow1");
PdfMapSubflow noSubflow = new PdfMapSubflow();
pdfMapConfiguration = new PdfMapConfiguration(List.of(pdfMapWithoutSubflow));

SubflowFieldPreparer noSubflowFieldPreparer = new SubflowFieldPreparer();
Expand All @@ -242,4 +241,79 @@ void shouldNotThrownWhenNoSubflow() {
pdfMapConfiguration.getPdfMap("flow1"))
).doesNotThrowAnyException();
}

@Test
void shouldNotCreateFieldsForIncompleteSubflowIterations() {
Map<String, Object> iteration1 = new HashMap<>();
iteration1.put("foo", "foo from first iteration will not be removed because iteration is complete");
iteration1.put("bar", "bar from first iteration will not be removed because iteration is complete");
iteration1.put("uuid", "uuid1");
iteration1.put("iterationIsComplete", true);
Map<String, Object> iteration2 = new HashMap<>();
iteration2.put("foo", "foo from second iteration will be removed because iteration is not complete");
iteration2.put("bar", "bar from second iteration will be removed because iteration is not complete");
iteration2.put("uuid", "uuid2");
iteration2.put("iterationIsComplete", false);
Map<String, Object> iteration3 = new HashMap<>();
iteration3.put("foo", "foo from third iteration will not be removed because iteration is complete");
iteration3.put("bar", "bar from third iteration will not be removed because iteration is complete");
iteration3.put("uuid", "uuid3");
iteration3.put("iterationIsComplete", true);

submission = Submission.builder().flow("flow1")
.inputData(
Map.of("testSubflow", List.of(
iteration1,
iteration2,
iteration3)
)).build();

Map<String, SubmissionField> resultMap = subflowFieldPreparer.prepareSubmissionFields(submission,
pdfMapConfiguration.getPdfMap("flow1"));

assertThat(resultMap.equals(
Map.of(
"foo_1", new SingleField("foo", "foo from first iteration will not be removed because iteration is complete", 1),
"bar_1", new SingleField("bar", "bar from first iteration will not be removed because iteration is complete", 1),
"foo_2", new SingleField("foo", "foo from third iteration will not be removed because iteration is complete", 2),
"bar_2", new SingleField("bar", "bar from third iteration will not be removed because iteration is complete", 2)
)
)).isTrue();
}

@Test
void shouldNotPrepareIgnoredSubflowFields() {
Map<String, Object> iteration1 = new HashMap<>();
iteration1.put("foo", "foo 1");
iteration1.put("bar", "bar 1");
iteration1.put("uuid", "uuid1");
iteration1.put("iterationIsComplete", true);
Map<String, Object> iteration2 = new HashMap<>();
iteration2.put("foo", "foo 2");
iteration2.put("bar", "bar 2");
iteration2.put("uuid", "uuid2");
iteration2.put("iterationIsComplete", true);


submission = Submission.builder().flow("flow1")
.inputData(
Map.of("testSubflow", List.of(
iteration1,
iteration2)
)).build();

Map<String, SubmissionField> resultMap = subflowFieldPreparer.prepareSubmissionFields(submission,
pdfMapConfiguration.getPdfMap("flow1"));


// Result does not include UUID or iterationIsComplete which should be ignored
assertThat(resultMap.equals(
Map.of(
"foo_1", new SingleField("foo", "foo 1", 1),
"bar_1", new SingleField("bar", "bar 1", 1),
"foo_2", new SingleField("foo", "foo 2", 2),
"bar_2", new SingleField("bar", "bar 2", 2)
)
)).isTrue();
}
}

0 comments on commit 386fb21

Please sign in to comment.