-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix bug with going back in subflows (#196)
* Fix bug with going back in subflows Co-authored-by: Ana Medrano <amedrano@codeforamerica.org>
- Loading branch information
1 parent
2b7fb08
commit a3c11fd
Showing
9 changed files
with
255 additions
and
6 deletions.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
src/main/java/org/mdbenefits/app/submission/actions/ClearIncompleteIncomeIterations.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package org.mdbenefits.app.submission.actions; | ||
|
||
|
||
import formflow.library.config.submission.Action; | ||
import formflow.library.data.Submission; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Slf4j | ||
public class ClearIncompleteIncomeIterations implements Action { | ||
|
||
/** | ||
* This action will clear out the incomplete iterations from the income subflow. These can be created when the user goes back | ||
* to the beginning of the subflow before completing an iteration. | ||
* | ||
* @param submission submission object the action is associated with, not null | ||
* @param id the id of the current iteration being submitted when this action is run, not null | ||
*/ | ||
@Override | ||
public void run(Submission submission, String id) { | ||
ArrayList<HashMap<String, Object>> incomeSubflow = (ArrayList<HashMap<String, Object>>) submission.getInputData().get("income"); | ||
if (incomeSubflow != null) { | ||
List<HashMap<String, Object>> filteredIncomeSubflow = incomeSubflow.stream() | ||
.filter(iteration -> id.equals(iteration.get("uuid")) || | ||
Boolean.TRUE.equals(iteration.get(Submission.ITERATION_IS_COMPLETE_KEY))) | ||
.collect(Collectors.toList()); | ||
submission.getInputData().put("income", filteredIncomeSubflow); | ||
} | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...in/java/org/mdbenefits/app/submission/actions/ClearIncompleteIterationsFromHousehold.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.mdbenefits.app.submission.actions; | ||
|
||
import formflow.library.config.submission.Action; | ||
import formflow.library.data.Submission; | ||
import formflow.library.data.SubmissionRepository; | ||
import formflow.library.data.SubmissionRepositoryService; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.stream.Collectors; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Slf4j | ||
public class ClearIncompleteIterationsFromHousehold implements Action { | ||
|
||
SubmissionRepositoryService submissionRepositoryService; | ||
|
||
public ClearIncompleteIterationsFromHousehold(SubmissionRepositoryService submissionRepositoryService) { | ||
this.submissionRepositoryService = submissionRepositoryService; | ||
} | ||
|
||
/** | ||
* This action will clear out the incomplete iterations from the household subflow. These can be created when the user goes back | ||
* to the beginning of the subflow before completing an iteration. | ||
* | ||
* @param submission submission object the action is associated with, not null | ||
*/ | ||
@Override | ||
public void run(Submission submission) { | ||
ArrayList<HashMap<String, Object>> household = (ArrayList<HashMap<String, Object>>) submission.getInputData().get("household"); | ||
if (household != null) { | ||
List<HashMap<String, Object>> filteredHouseholdSubflow = household.stream() | ||
.filter(iteration -> Boolean.TRUE.equals(iteration.get(Submission.ITERATION_IS_COMPLETE_KEY))) | ||
.collect(Collectors.toList()); | ||
submission.getInputData().put("household", filteredHouseholdSubflow); | ||
submissionRepositoryService.save(submission); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/test/java/org/mdbenefits/app/submission/actions/ClearIncompleteIncomeIterationsTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package org.mdbenefits.app.submission.actions; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import formflow.library.data.Submission; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class ClearIncompleteIncomeIterationsTest { | ||
|
||
@Test | ||
void shouldClearIncompleteIterationsFromIncomeSubflow() { | ||
ClearIncompleteIncomeIterations clearIncompleteIncomeIterations = new ClearIncompleteIncomeIterations(); | ||
Submission submission = new Submission(); | ||
var income = new ArrayList<Map<String, Object>>(); | ||
var job1 = new HashMap<String, Object>(); | ||
job1.put(Submission.ITERATION_IS_COMPLETE_KEY, true); | ||
job1.put("uuid", "complete iteration"); | ||
job1.put("employerName", "ACME Inc"); | ||
job1.put("payPeriod", "It varies"); | ||
job1.put("payPeriodAmount", 400.0); | ||
var job2 = new HashMap<String, Object>(); | ||
job2.put(Submission.ITERATION_IS_COMPLETE_KEY, false); | ||
job2.put("uuid", "Current Iteration"); | ||
job2.put("employerName", "Monsters Inc"); | ||
job2.put("payPeriodAmount", 200.0); | ||
var job3 = new HashMap<String, Object>(); | ||
job3.put(Submission.ITERATION_IS_COMPLETE_KEY, false); | ||
job3.put("uuid", "Incomplete Iteration"); | ||
job3.put("employerName", "Disney Inc"); | ||
income.add(job1); | ||
income.add(job2); | ||
income.add(job3); | ||
HashMap<String, Object> inputData = new HashMap<>(); | ||
inputData.put("income", income); | ||
submission.setInputData(inputData); | ||
clearIncompleteIncomeIterations.run(submission, "Current Iteration"); | ||
ArrayList<HashMap<String, Object>> incomeSubflow = (ArrayList<HashMap<String, Object>>) submission.getInputData().get("income"); | ||
assertThat(incomeSubflow).size().isEqualTo(2); | ||
assertThat(incomeSubflow).doesNotContain(job3); | ||
assertThat(incomeSubflow).isEqualTo(List.of(job1, job2)); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
...ava/org/mdbenefits/app/submission/actions/ClearIncompleteIterationsFromHouseholdTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.mdbenefits.app.submission.actions; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.Mockito.when; | ||
|
||
import formflow.library.data.Submission; | ||
import formflow.library.data.SubmissionRepositoryService; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.UUID; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
@ActiveProfiles("test") | ||
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) | ||
class ClearIncompleteIterationsFromHouseholdTest { | ||
|
||
@Autowired | ||
SubmissionRepositoryService submissionRepositoryService; | ||
|
||
@Test | ||
void shouldClearIncompleteIterationsFromHouseholdSubflow() { | ||
ClearIncompleteIterationsFromHousehold clearIncompleteIterationsFromHousehold = new ClearIncompleteIterationsFromHousehold(submissionRepositoryService); | ||
Submission submission = new Submission().builder() | ||
.id(UUID.randomUUID()) | ||
.flow("flow") | ||
.build(); | ||
var household = new ArrayList<Map<String, Object>>(); | ||
var householdMember1 = new HashMap<String, Object>(); | ||
householdMember1.put(Submission.ITERATION_IS_COMPLETE_KEY, true); | ||
var householdMember2 = new HashMap<String, Object>(); | ||
householdMember2.put(Submission.ITERATION_IS_COMPLETE_KEY, true); | ||
var householdMember3 = new HashMap<String, Object>(); | ||
householdMember3.put(Submission.ITERATION_IS_COMPLETE_KEY, false); | ||
household.add(householdMember1); | ||
household.add(householdMember2); | ||
household.add(householdMember3); | ||
HashMap<String, Object> inputData = new HashMap<>(); | ||
inputData.put("household", household); | ||
submission.setInputData(inputData); | ||
clearIncompleteIterationsFromHousehold.run(submission); | ||
ArrayList<HashMap<String, Object>> incomeSubflow = (ArrayList<HashMap<String, Object>>) submission.getInputData().get("household"); | ||
assertThat(incomeSubflow).size().isEqualTo(2); | ||
assertThat(incomeSubflow).doesNotContain(householdMember3); | ||
assertThat(incomeSubflow).isEqualTo(List.of(householdMember1, householdMember2)); | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
src/test/java/org/mdbenefits/app/utils/IncomeCalculatorTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.mdbenefits.app.utils; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
import formflow.library.data.Submission; | ||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
import org.junit.jupiter.api.Test; | ||
|
||
class IncomeCalculatorTest { | ||
|
||
@Test | ||
void totalFutureEarnedIncomeShouldFilterIncompleteSubflowIterations() { | ||
// given | ||
var submission = new Submission(); | ||
var income = new ArrayList<Map<String, Object>>(); | ||
var job1 = new HashMap<String, Object>(); | ||
job1.put(Submission.ITERATION_IS_COMPLETE_KEY, true); | ||
job1.put("employerName", "ACME Inc"); | ||
job1.put("payPeriod", "It varies"); | ||
job1.put("payPeriodAmount", 400.0); | ||
var job2 = new HashMap<String, Object>(); | ||
job2.put(Submission.ITERATION_IS_COMPLETE_KEY, false); | ||
job2.put("employerName", "Monsters Inc"); | ||
job2.put("payPeriodAmount", 200.0); | ||
income.add(job1); | ||
income.add(job2); | ||
submission.setInputData(Map.of("income", income)); | ||
var calculator = new IncomeCalculator(submission); | ||
|
||
var total = calculator.totalFutureEarnedIncome(); | ||
|
||
// Amount will exclude amount from incomplete iteration | ||
assertEquals(400.0, total); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters