ECRegisterService { public ECRegister getRegisterForANM(String anmIdentifier) { ArrayList<ECRegisterEntry> ecRegisterEntries = new ArrayList<>(); List<EligibleCouple> ecs = allEligibleCouples.allOpenECsForANM(anmIdentifier); for (EligibleCouple ec : ecs) { List<Mother> mothers = allMothers.findAllOpenANCByECCaseId(ec.caseId()); boolean isPregnant = mothers.size() > 0; ECRegisterEntry ecRegisterEntry = new ECRegisterEntry() .withECNumber(ec.ecNumber()) .withWifeName(ec.wifeName()) .withHusbandName(ec.husbandName()) .withRegistrationDate(ec.getDetail(REGISTRATION_DATE)) .withVillage(ec.village()) .withSubCenter(ec.subCenter()) .withPHC(ec.phc()) .withWifeAge(ec.getDetail(WIFE_AGE)) .withHusbandAge(ec.getDetail(HUSBAND_AGE)) .withHouseholdNumber(ec.getDetail(HOUSEHOLD_NUMBER)) .withHouseholdAddress(ec.getDetail(HOUSEHOLD_ADDRESS)) .withHeadOfHousehold(ec.getDetail(HEAD_OF_HOUSEHOLD)) .withReligion(ec.getDetail(RELIGION)) .withCaste(ec.getDetail(CASTE)) .withEconomicStatus(ec.getDetail(ECONOMIC_STATUS)) .withWifeEducationLevel(ec.getDetail(WIFE_EDUCATIONAL_LEVEL)) .withHusbandEducationLevel(ec.getDetail(HUSBAND_EDUCATION_LEVEL)) .withNumberOfLivingChildren(parseValidIntegersAndDefaultInvalidOnesToEmptyString(ec.getDetail(NUMBER_OF_LIVING_CHILDREN))) .withNumberOfStillBirths(parseValidIntegersAndDefaultInvalidOnesToEmptyString(ec.getDetail(NUMBER_OF_STILL_BIRTHS))) .withNumberOfAbortions(parseValidIntegersAndDefaultInvalidOnesToEmptyString(ec.getDetail(NUMBER_OF_ABORTIONS))) .withParity(parseValidIntegersAndDefaultInvalidOnesToEmptyString((ec.getDetail(PARITY)))) .withGravida(parseValidIntegersAndDefaultInvalidOnesToEmptyString((ec.getDetail(NUMBER_OF_PREGNANCIES)))) .withNumberOfLivingMaleChildren(parseValidIntegersAndDefaultInvalidOnesToEmptyString(ec.getDetail(NUMBER_OF_LIVING_MALE_CHILDREN))) .withNumberOfLivingFemaleChildren(parseValidIntegersAndDefaultInvalidOnesToEmptyString(ec.getDetail(NUMBER_OF_LIVING_FEMALE_CHILDREN))) .withYoungestChildAge(parseValidIntegersAndDefaultInvalidOnesToEmptyString(ec.getDetail(YOUNGEST_CHILD_AGE))) .withCurrentFPMethod(ec.getDetail(CURRENT_FP_METHOD_FIELD_NAME)) .withCurrentFPMethodStartDate(ec.getDetail(FP_METHOD_CHANGE_DATE_FIELD_NAME)) .withPregnancyStatus(isPregnant); ecRegisterEntries.add(ecRegisterEntry); } return new ECRegister(ecRegisterEntries); } @Autowired  ECRegisterService(AllMothers allMothers,
                             AllEligibleCouples allEligibleCouples); ECRegister getRegisterForANM(String anmIdentifier);  }
@Test public void shouldGetECRegisterForAGivenANM() throws Exception { Mother mother = new Mother("caseId", "ecCaseId", "thayi card number 1") .withLMP(LocalDate.parse("2013-12-30")) .withDetails( create("ancNumber", "OA899") .put("registrationDate", "2014-01-01") .put("isJSYBeneficiary", "yes") .put("edd", "2014-09-01") .put("height", "150") .put("bloodGroup", "o -ve") .put("isHighRisk", "yes") .map() ); EligibleCouple eligibleCouple = new EligibleCouple("ecCaseId", "123") .withANMIdentifier("anm1") .withLocation("village", "sc", "phc") .withCouple("wife Name", "husband Name") .withDetails( create("registrationDate", "2013-01-01") .put("wifeName", "wife Name") .put("husbandName", "husband Name") .put("wifeAge", "23") .put("husbandAge", "32") .put("householdNumber", "22") .put("householdAddress", "HH Address") .put("headOfHousehold", "Head of HH") .put("religion", "religion") .put("caste", "caste") .put("economicStatus", "bpl") .put("educationalLevel", "wife education level") .put("husbandEducationLevel", "husband education level") .put("parity", "1") .put("numberOfLivingChildren", "1") .put("numberOfLivingMaleChildren", "1") .put("numberOfLivingFemaleChild", "0") .put("numberOfAbortions", "0") .put("numberOfStillBirths", "0") .put("numberOfPregnancies", "2") .put("youngestChildAge", "1") .put("currentMethod", "ocp") .put("familyPlanningMethodChangeDate", "2012-01-01") .map() ); when(allEligibleCouples.allOpenECsForANM("anm1")).thenReturn(asList(eligibleCouple)); when(allMothers.findAllOpenANCByECCaseId("ecCaseId")).thenReturn(asList(mother)); ECRegister expectedRegister = new ECRegister(asList(new ECRegisterEntry() .withECNumber("123") .withRegistrationDate("2013-01-01") .withWifeName("wife Name") .withHusbandName("husband Name") .withVillage("village") .withSubCenter("sc") .withPHC("phc") .withWifeAge("23") .withHusbandAge("32") .withHouseholdNumber("22") .withHouseholdAddress("HH Address") .withHeadOfHousehold("Head of HH") .withReligion("religion") .withCaste("caste") .withEconomicStatus("bpl") .withWifeEducationLevel("wife education level") .withHusbandEducationLevel("husband education level") .withGravida("2") .withParity("1") .withNumberOfLivingChildren("1") .withNumberOfLivingMaleChildren("1") .withNumberOfLivingFemaleChildren("0") .withNumberOfStillBirths("0") .withNumberOfAbortions("0") .withYoungestChildAge("1") .withCurrentFPMethod("ocp") .withCurrentFPMethodStartDate("2012-01-01") .withPregnancyStatus(true) )); ECRegister register = registerService.getRegisterForANM("anm1"); assertEquals(expectedRegister, register); }
@Test public void shouldDefaultIntegerFieldsToZeroWhenValueIsNotInteger() throws Exception { Mother mother = new Mother("caseId", "ecCaseId", "thayi card number 1") .withLMP(LocalDate.parse("2013-12-30")) .withDetails( create("ancNumber", "OA899") .put("registrationDate", "2014-01-01") .put("isJSYBeneficiary", "yes") .put("edd", "2014-09-01") .put("height", "150") .put("bloodGroup", "o -ve") .put("isHighRisk", "yes") .map() ); EligibleCouple eligibleCouple = new EligibleCouple("ecCaseId", "123") .withANMIdentifier("anm1") .withLocation("village", "sc", "phc") .withCouple("wife Name", "husband Name") .withDetails( create("registrationDate", "2013-01-01") .put("wifeName", "wife Name") .put("husbandName", "husband Name") .put("wifeAge", "23") .put("husbandAge", "32") .put("householdNumber", "22") .put("householdAddress", "HH Address") .put("headOfHousehold", "Head of HH") .put("religion", "religion") .put("caste", "caste") .put("economicStatus", "bpl") .put("educationalLevel", "wife education level") .put("husbandEducationLevel", "husband education level") .put("parity", "NaN") .put("numberOfLivingChildren", "str") .put("numberOfLivingMaleChildren", null) .put("numberOfLivingFemaleChild", "NaN") .put("numberOfAbortions", "inp") .put("numberOfStillBirths", null) .put("youngestChildAge", "") .put("currentMethod", "ocp") .put("familyPlanningMethodChangeDate", "2012-01-01") .map() ); when(allEligibleCouples.allOpenECsForANM("anm1")).thenReturn(asList(eligibleCouple)); when(allMothers.findAllOpenANCByECCaseId("ecCaseId")).thenReturn(asList(mother)); ECRegister expectedRegister = new ECRegister(asList(new ECRegisterEntry() .withECNumber("123") .withRegistrationDate("2013-01-01") .withWifeName("wife Name") .withHusbandName("husband Name") .withVillage("village") .withSubCenter("sc") .withPHC("phc") .withWifeAge("23") .withHusbandAge("32") .withHouseholdNumber("22") .withHouseholdAddress("HH Address") .withHeadOfHousehold("Head of HH") .withReligion("religion") .withCaste("caste") .withEconomicStatus("bpl") .withWifeEducationLevel("wife education level") .withHusbandEducationLevel("husband education level") .withGravida("") .withParity("") .withNumberOfLivingChildren("") .withNumberOfLivingMaleChildren("") .withNumberOfLivingFemaleChildren("") .withNumberOfStillBirths("") .withNumberOfAbortions("") .withYoungestChildAge("") .withCurrentFPMethod("ocp") .withCurrentFPMethodStartDate("2012-01-01") .withPregnancyStatus(true) )); ECRegister register = registerService.getRegisterForANM("anm1"); assertEquals(expectedRegister, register); }