Skip to content

Commit

Permalink
Adding test for ssns
Browse files Browse the repository at this point in the history
  • Loading branch information
sree-cfa committed Dec 13, 2023
1 parent fbfb421 commit 593845a
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 63 deletions.
47 changes: 17 additions & 30 deletions src/main/java/org/ladocuploader/app/preparers/SsnPreparer.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
import org.ladocuploader.app.submission.StringEncryptor;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.ladocuploader.app.utils.SubmissionUtilities.getDecryptedSSNKeyName;

@Component
public class SsnPreparer implements SubmissionFieldPreparer {
private static final String ENCRYPTED_SSNS_INPUT_NAME = "encryptedSSNs";

private final StringEncryptor encryptor;

public SsnPreparer(StringEncryptor stringEncryptor) {
Expand All @@ -27,38 +29,23 @@ private StringEncryptor getEncryptor() {
@Override
public Map<String, SubmissionField> prepareSubmissionFields(Submission submission, PdfMap pdfMap) {
Map<String, SubmissionField> results = new HashMap<>();

// Digital Assister
List<String> householdSsnInputs = new ArrayList<>();
String encryptedSSN = (String) submission.getInputData().remove(ENCRYPTED_SSNS_INPUT_NAME);
String encryptedSSN = (String) submission.getInputData().remove("encryptedSSN");
if (encryptedSSN != null) {
String decryptedSSN = getEncryptor().decrypt(encryptedSSN);
householdSsnInputs.add(decryptedSSN);
String decryptedSSN = encryptor.decrypt(encryptedSSN);
results.put("applicantSsn", new SingleField("applicantSsn", decryptedSSN, null));
}

var householdMembers = (List) submission.getInputData().get("household");
if (householdMembers != null && !householdMembers.isEmpty()) {
if (householdSsnInputs.isEmpty()) {
// Make sure there's one for head of household
householdSsnInputs.add("");
List<Map<String, Object>> householdMembers = (List) submission.getInputData().get("household");
if(householdMembers != null) {
int i = 1;
for (Map<String, Object> hhmember : householdMembers) {
encryptedSSN = (String) hhmember.get(getDecryptedSSNKeyName((String) hhmember.get("uuid")));
String decryptedSSN = encryptor.decrypt(encryptedSSN);
results.put("ssns_" + i, new SingleField("ssns_" + i, decryptedSSN, null));
i++;
}
for (int i = 0; i < householdMembers.size(); i++) {
encryptedSSN = (String) ((Map<?, ?>) householdMembers.get(i)).remove(ENCRYPTED_SSNS_INPUT_NAME);
String decryptedSSN = getEncryptor().decrypt(encryptedSSN);
householdSsnInputs.add(decryptedSSN);
}
}

if (!householdSsnInputs.isEmpty()) {
results.put("applicantSsn", new SingleField("applicantSsn", householdSsnInputs.get(0), null));

for (int i = 1; i < householdSsnInputs.size(); i++) {
results.put("ssns_" + i, new SingleField("ssns_" + i, householdSsnInputs.get(i), null));
}

return results;
}

return Collections.emptyMap();
return results;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@

import formflow.library.config.submission.Action;
import formflow.library.data.Submission;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import org.ladocuploader.app.submission.StringEncryptor;
import org.springframework.stereotype.Component;
import static formflow.library.inputs.FieldNameMarkers.DYNAMIC_FIELD_MARKER;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import static org.ladocuploader.app.utils.SubmissionUtilities.ENCRYPTED_SSNS_INPUT_NAME;
import static org.ladocuploader.app.utils.SubmissionUtilities.getDecryptedSSNKeyName;

/**
*
*/
@Component
public class DecryptSSNBeforeDisplaying implements Action {
private static final String ENCRYPTED_SSNS_INPUT_NAME = "householdMemberEncryptedSSN";

private final StringEncryptor encryptor;

Expand All @@ -35,18 +34,14 @@ public void run(Submission submission) {
String decryptedSSN = getEncryptor().decrypt(encryptedSSN);
submission.getInputData().put("ssn", decryptedSSN);
}

ArrayList<LinkedHashMap> householdMembers = (ArrayList) submission.getInputData().get("household");

for (LinkedHashMap hhmember : householdMembers) {
String ssnKey = "householdMemberSsn" + DYNAMIC_FIELD_MARKER + hhmember.get("uuid");
String ssnKey = getDecryptedSSNKeyName((String) hhmember.get("uuid"));
encryptedSSN = (String) hhmember.remove(ENCRYPTED_SSNS_INPUT_NAME);
if(!encryptedSSN.isBlank()) {
String decryptedSSN = getEncryptor().decrypt(encryptedSSN);
submission.getInputData().put(ssnKey, decryptedSSN);
} else {
submission.getInputData().put(ssnKey, "");
}
String decryptedSSN = getEncryptor().decrypt(encryptedSSN);
submission.getInputData().put(ssnKey, decryptedSSN);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

import formflow.library.config.submission.Action;
import formflow.library.data.Submission;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import org.ladocuploader.app.submission.StringEncryptor;
import org.springframework.stereotype.Component;
import static formflow.library.inputs.FieldNameMarkers.DYNAMIC_FIELD_MARKER;

import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.LinkedHashMap;

import static org.ladocuploader.app.utils.SubmissionUtilities.ENCRYPTED_SSNS_INPUT_NAME;
import static org.ladocuploader.app.utils.SubmissionUtilities.getDecryptedSSNKeyName;

@Component
public class EncryptSSNBeforeSaving implements Action {
private static final String ENCRYPTED_SSNS_INPUT_NAME = "householdMemberEncryptedSSN";

private final StringEncryptor encryptor;

Expand All @@ -30,15 +29,12 @@ public void run(Submission submission) {
}

ArrayList<LinkedHashMap> householdMembers = (ArrayList) submission.getInputData().get("household");

for (LinkedHashMap hhmember : householdMembers) {
String ssnKey = "householdMemberSsn" + DYNAMIC_FIELD_MARKER + hhmember.get("uuid");
if (householdMembers != null) {
for (LinkedHashMap hhmember : householdMembers) {
String ssnKey = getDecryptedSSNKeyName((String) hhmember.get("uuid"));
var householdMemberSsn = (String) submission.getInputData().remove(ssnKey);
if(!householdMemberSsn.isBlank()){
String encryptedSSN = encryptor.encrypt(householdMemberSsn);
hhmember.put(ENCRYPTED_SSNS_INPUT_NAME,encryptedSSN);
} else {
hhmember.put(ENCRYPTED_SSNS_INPUT_NAME, "");
String encryptedSSN = encryptor.encrypt(householdMemberSsn);
hhmember.put(ENCRYPTED_SSNS_INPUT_NAME, encryptedSSN);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
import java.text.DecimalFormat;
import java.util.*;

import static formflow.library.inputs.FieldNameMarkers.DYNAMIC_FIELD_MARKER;

public class SubmissionUtilities {
public static final String ENCRYPTED_SSNS_INPUT_NAME = "householdMemberEncryptedSSN";

public static final Map<String, String> EDUCATION_MAP = new HashMap<>();
public static final Map<String, String> MARITAL_STATUS_MAP = new HashMap<>();
Expand Down Expand Up @@ -136,5 +139,7 @@ public static ArrayList<HashMap<String, Object>> getHouseholdIncomeReviewItems(S
return items;
}


public static String getDecryptedSSNKeyName(String uuid) {
return "householdMemberSsn%s%s".formatted(DYNAMIC_FIELD_MARKER, uuid);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
import java.util.List;
import java.util.Map;

import static org.ladocuploader.app.utils.SubmissionUtilities.getDecryptedSSNKeyName;

public class SubmissionTestBuilder {

private final Submission submission = new Submission();
Expand All @@ -28,7 +30,7 @@ public SubmissionTestBuilder withPersonalInfo(
submission.getInputData().put("sex", sex);
submission.getInputData().put("maritalStatus", maritalStatus);
submission.getInputData().put("highestEducation", education);
submission.getInputData().put("ssn", ssn);
submission.getInputData().put("encryptedSSN", ssn);
return this;
}

Expand All @@ -42,7 +44,8 @@ public SubmissionTestBuilder withHouseholdMember(
}

Map<String, Object> member = new HashMap<>();
member.put("uuid", String.format("%s-%s", firstName, lastName).toLowerCase());
String uuid = "%s-%s".formatted(firstName, lastName).toLowerCase();
member.put("uuid", uuid);
member.put("householdMemberFirstName", firstName);
member.put("householdMemberLastName", lastName);
member.put("householdMemberBirthDay", birthDay);
Expand All @@ -52,7 +55,7 @@ public SubmissionTestBuilder withHouseholdMember(
member.put("householdMemberSex", sex);
member.put("householdMemberMaritalStatus", maritalStatus);
member.put("householdMemberHighestEducation", education);
member.put("householdMemberSsn", ssn);
member.put(getDecryptedSSNKeyName(uuid), ssn);

household.add(member);
submission.getInputData().put("household", household);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,73 @@
package org.ladocuploader.app.preparers;

import formflow.library.data.Submission;
import formflow.library.pdf.SingleField;
import formflow.library.pdf.SubmissionField;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.ladocuploader.app.data.SubmissionTestBuilder;
import org.ladocuploader.app.submission.StringEncryptor;
import org.mockito.Mockito;

import java.util.Map;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;

class SsnPreparerTest {


private StringEncryptor encryptor;
private SsnPreparer preparer;

@BeforeEach
public void setUp() {
encryptor = Mockito.mock(StringEncryptor.class);
preparer = new SsnPreparer(encryptor);
}

@Test
public void testNoSSNs() {
Submission submission = new Submission();
Map<String, SubmissionField> result = preparer.prepareSubmissionFields(submission, null);

assertThat(result).isEmpty();
}

@Test
public void testApplicantSSN() {
Submission submission = new SubmissionTestBuilder()
.withPersonalInfo("","","","","", "","","","",
"somethingencrypted")
.build();
when(encryptor.decrypt(eq("somethingencrypted"))).thenReturn("111223333");
Map<String, SubmissionField> result = preparer.prepareSubmissionFields(submission, null);

assertThat(result.size()).isEqualTo(1);
assertThat(result.get("applicantSsn")).isEqualTo(new SingleField("applicantSsn", "111223333", null));
}

@Test
public void testHouseholdSSNs() {
Submission submission = new SubmissionTestBuilder()
.withPersonalInfo("applicant","test","","","", "","","","",
"")
.withHouseholdMember("member1", "test", "", "", "", "", "", "", "",
"somethingencrypted")
.withHouseholdMember("member2", "test", "", "", "", "", "", "", "",
"")
.withHouseholdMember("member3", "test", "", "", "", "", "", "", "",
"somethingelseencrypted")
.build();
when(encryptor.decrypt(eq("somethingencrypted"))).thenReturn("111223333");
when(encryptor.decrypt(eq("somethingelseencrypted"))).thenReturn("444556666");

Map<String, SubmissionField> result = preparer.prepareSubmissionFields(submission, null);

assertThat(result.size()).isEqualTo(4);
assertThat(result.get("applicantSsn")).isEqualTo(new SingleField("applicantSsn", null, null));
assertThat(result.get("ssns_1")).isEqualTo(new SingleField("ssns_1", "111223333", null));
assertThat(result.get("ssns_2")).isEqualTo(new SingleField("ssns_2", null, null));
assertThat(result.get("ssns_3")).isEqualTo(new SingleField("ssns_3", "444556666", null));
}
}

0 comments on commit 593845a

Please sign in to comment.