ANCRegisterService { public ANCRegister getRegisterForANM(String anmIdentifier) { ArrayList<ANCRegisterEntry> ancRegisterEntries = new ArrayList<>(); List<Mother> mothers = allMothers.findAllOpenMothersForANM(anmIdentifier); Collection<String> ecIDs = selectDistinct(collect(mothers, on(Mother.class).ecCaseId())); List<String> ecIdsList = new ArrayList<>(); ecIdsList.addAll(ecIDs); List<EligibleCouple> ecs = allEligibleCouples.findAll(ecIdsList); for (Mother mother : mothers) { EligibleCouple ec = selectUnique(ecs, having(on(EligibleCouple.class).caseId(), equalTo(mother.ecCaseId()))); ANCRegisterEntry entry = new ANCRegisterEntry() .withANCNumber(mother.getDetail(ANC_NUMBER)) .withRegistrationDate(mother.getDetail(REGISTRATION_DATE)) .withECNumber(ec.ecNumber()) .withThayiCardNumber(mother.thayiCardNumber()) .withAadharCardNumber(ec.getDetail(AADHAR_NUMBER)) .withWifeName(ec.wifeName()) .withHusbandName(ec.husbandName()) .withAddress(ec.getDetail(HOUSEHOLD_ADDRESS)) .withWifeDOB(ec.wifeDOB()) .withPhoneNumber(ec.getDetail(PHONE_NUMBER)) .withWifeEducationLevel(ec.getDetail(WIFE_EDUCATIONAL_LEVEL)) .withHusbandEducationLevel(ec.getDetail(HUSBAND_EDUCATION_LEVEL)) .withCaste(ec.getDetail(CASTE)) .withReligion(ec.getDetail(RELIGION)) .withEconomicStatus(ec.getDetail(ECONOMIC_STATUS)) .withBPLCardNumber(ec.getDetail(BPL_CARD_NUMBER)) .withJSYBeneficiary(mother.getDetail(JSY_BENEFICIARY)) .withGravida(ec.getDetail(NUMBER_OF_PREGNANCIES)) .withParity(ec.getDetail(PARITY)) .withNumberOfLivingChildren(ec.getDetail(NUMBER_OF_LIVING_CHILDREN)) .withNumberOfStillBirths(ec.getDetail(NUMBER_OF_STILL_BIRTHS)) .withNumberOfAbortions(ec.getDetail(NUMBER_OF_ABORTIONS)) .withYoungestChildDOB(ec.getDetail(YOUNGEST_CHILD_DOB)) .withLMP(mother.lmp().toString()) .withEDD(mother.getDetail(EDD)) .withHeight(mother.getDetail(HEIGHT)) .withBloodGroup(mother.getDetail(BLOOD_GROUP)) .withIsHRP(mother.getDetail(IS_HIGH_RISK)) .withHBTests(mother.hbTests()) .withANCInvestigations(mother.ancInvestigations()) .withANCVisits(mother.ancVisits()) .withIFATablets(mother.ifaTablets()) .withTTDoses(mother.ttDoses()); ancRegisterEntries.add(entry); } return new ANCRegister(ancRegisterEntries); } @Autowired  ANCRegisterService(AllMothers allMothers,
                              AllEligibleCouples allEligibleCouples); ANCRegister getRegisterForANM(String anmIdentifier);  }
@Test public void shouldGetANCRegisterForAGivenANM() throws Exception { Mother mother = new Mother("caseId", "ecCaseId", "thayi card number 1") .withLMP(LocalDate.parse("2013-12-30")) .withANCVisits(asList( create("ancVisitDate", "2013-01-01") .put("weight", "55") .put("bpSystolic", "120") .put("bpDiastolic", "80") .map())) .withIFATablets(asList( create("ifaTabletsDate", "2013-05-24") .put("numberOfIFATabletsGiven", "30") .map())) .withTTDoses(asList(create("ttDate", "2012-12-24") .put("ttDose", "tt1") .map())) .withHBTests(asList(create("hbTestDate", "2013-01-01") .put("hbLevel", "7.9") .map())) .withANCInvestigations(asList( create("testDate", "2012-09-01") .put("bileSalts", "present") .put("bilePigments", "present") .map(), create("testDate", "2013-01-01") .put("testResultsToEnter", "urine_sugar mp hiv") .put("testsResultPositive", "urine_sugar") .put("bileSalts", "absent") .put("bilePigments", "absent") .put("womanBloodGroup", "ab_positive") .put("rhIncompatibleCouple", "no") .map())) .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") .withDetails( create("aadharNumber", "aadhar card number 1") .put("householdAddress", "address1") .put("phoneNumber", "phone 1") .put("educationalLevel", "wife education level") .put("husbandEducationLevel", "husband education level") .put("caste", "sc") .put("religion", "hindu") .put("economicStatus", "bpl") .put("bplCardNumber", "bpl card number 1") .put("numberOfPregnancies", "1") .put("parity", "2") .put("numberOfLivingChildren", "3") .put("numberOfStillBirths", "4") .put("numberOfAbortions", "5") .put("youngestChildDOB", "2012-01-01") .put("womanDOB", "1989-01-01") .map()) .withCouple("name1", "name2"); when(allMothers.findAllOpenMothersForANM("anm1")).thenReturn(asList(mother)); when(allEligibleCouples.findAll(asList("ecCaseId"))).thenReturn(asList(eligibleCouple)); ANCRegister expectedRegister = new ANCRegister(asList(new ANCRegisterEntry() .withANCNumber("OA899") .withRegistrationDate("2014-01-01") .withECNumber("123") .withThayiCardNumber("thayi card number 1") .withAadharCardNumber("aadhar card number 1") .withWifeName("name1") .withHusbandName("name2") .withAddress("address1") .withWifeDOB("1989-01-01") .withPhoneNumber("phone 1") .withWifeEducationLevel("wife education level") .withHusbandEducationLevel("husband education level") .withCaste("sc") .withReligion("hindu") .withEconomicStatus("bpl") .withBPLCardNumber("bpl card number 1") .withJSYBeneficiary("yes") .withGravida("1") .withParity("2") .withNumberOfLivingChildren("3") .withNumberOfStillBirths("4") .withNumberOfAbortions("5") .withYoungestChildDOB("2012-01-01") .withLMP("2013-12-30") .withEDD("2014-09-01") .withHeight("150") .withBloodGroup("o -ve") .withIsHRP("yes") .withHBTests(asList( create("hbTestDate", "2013-01-01") .put("hbLevel", "7.9") .map())) .withANCVisits(asList( create("ancVisitDate", "2013-01-01") .put("weight", "55") .put("bpSystolic", "120") .put("bpDiastolic", "80") .map())) .withANCInvestigations(asList( create("testDate", "2012-09-01") .put("bileSalts", "present") .put("bilePigments", "present") .map(), create("testDate", "2013-01-01") .put("testResultsToEnter", "urine_sugar mp hiv") .put("testsResultPositive", "urine_sugar") .put("bileSalts", "absent") .put("bilePigments", "absent") .put("womanBloodGroup", "ab_positive") .put("rhIncompatibleCouple", "no") .map())) .withIFATablets(asList( create("ifaTabletsDate", "2013-05-24") .put("numberOfIFATabletsGiven", "30") .map())) .withTTDoses(asList(create("ttDate", "2012-12-24") .put("ttDose", "tt1") .map()))) ); ANCRegister register = registerService.getRegisterForANM("anm1"); assertEquals(expectedRegister, register); }
@Test public void shouldGetANCRegisterForANCOutOfAreaWithoutDOB() throws Exception { DateUtil.fakeIt(LocalDate.parse("2014-01-01")); 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") .withDetails( create("aadharNumber", "aadhar card number 1") .put("householdAddress", "address1") .put("phoneNumber", "phone 1") .put("educationalLevel", "wife education level") .put("husbandEducationLevel", "husband education level") .put("caste", "sc") .put("religion", "hindu") .put("economicStatus", "bpl") .put("bplCardNumber", "bpl card number 1") .put("numberOfPregnancies", "1") .put("parity", "2") .put("numberOfLivingChildren", "3") .put("numberOfStillBirths", "4") .put("numberOfAbortions", "5") .put("youngestChildDOB", "2012-01-01") .put("wifeAge", "26") .map()) .withCouple("name1", "name2"); when(allMothers.findAllOpenMothersForANM("anm1")).thenReturn(asList(mother)); when(allEligibleCouples.findAll(asList("ecCaseId"))).thenReturn(asList(eligibleCouple)); ANCRegister expectedRegister = new ANCRegister(asList(new ANCRegisterEntry() .withANCNumber("OA899") .withRegistrationDate("2014-01-01") .withECNumber("123") .withThayiCardNumber("thayi card number 1") .withAadharCardNumber("aadhar card number 1") .withWifeName("name1") .withHusbandName("name2") .withAddress("address1") .withWifeDOB("1988-01-01") .withPhoneNumber("phone 1") .withWifeEducationLevel("wife education level") .withHusbandEducationLevel("husband education level") .withCaste("sc") .withReligion("hindu") .withEconomicStatus("bpl") .withBPLCardNumber("bpl card number 1") .withJSYBeneficiary("yes") .withGravida("1") .withParity("2") .withNumberOfLivingChildren("3") .withNumberOfStillBirths("4") .withNumberOfAbortions("5") .withYoungestChildDOB("2012-01-01") .withLMP("2013-12-30") .withEDD("2014-09-01") .withHeight("150") .withBloodGroup("o -ve") .withIsHRP("yes"))); ANCRegister register = registerService.getRegisterForANM("anm1"); assertEquals(expectedRegister, register); }