PNCRegisterService { public PNCRegister getRegisterForANM(String anmIdentifier) { ArrayList<PNCRegisterEntry> pncRegisterEntries = new ArrayList<>(); List<Mother> mothers = allMothers.findAllOpenPNCsForANM(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()))); PNCRegisterEntry pncRegisterEntry = new PNCRegisterEntry() .withRegistrationDate(mother.getDetail(REGISTRATION_DATE)) .withThayiCardNumber(mother.thayiCardNumber()) .withWifeName(ec.wifeName()) .withHusbandName(ec.husbandName()) .withWifeDOB(ec.wifeDOB()) .withAddress(ec.getDetail(HOUSEHOLD_ADDRESS)) .withDateOfDelivery(mother.dateOfDelivery().toString()) .withPlaceOfDelivery(mother.getDetail(DELIVERY_PLACE)) .withTypeOfDelivery(mother.getDetail(DELIVERY_TYPE)) .withDischargeDate(mother.getDetail(DISCHARGE_DATE)) .withFPMethodName(ec.getDetail(CURRENT_FP_METHOD_FIELD_NAME)) .withFPMethodDate(ec.getDetail(FP_METHOD_CHANGE_DATE_FIELD_NAME)) .withChildrenDetails(mother.childrenDetails()) .withPNCVisits(mother.pncVisits()) .withDeliveryComplications(mother.getDetail(DELIVERY_COMPLICATIONS)); pncRegisterEntries.add(pncRegisterEntry); } return new PNCRegister(pncRegisterEntries); } @Autowired  PNCRegisterService(AllMothers allMothers,
                              AllEligibleCouples allEligibleCouples); PNCRegister getRegisterForANM(String anmIdentifier);  }
@Test public void shouldGetPNCRegisterForAGivenANM() throws Exception { Mother mother = new Mother("caseId", "ecCaseId", "thayi card number 1") .withDetails( create("deliveryPlace", "phc") .put("registrationDate", "2014-01-01") .put("dischargeDate", "2014-01-01") .put("deliveryType", "normal") .put("deliveryComplications", "difficulty breathing") .map() ) .withChildrenDetails(asList(create("id", "123") .put("sex", "male") .put("birthWeight", "2") .put("breastFeedingInOneHour", "yes") .map(), create("id", "234") .put("sex", "male") .put("birthWeight", "2") .put("breastFeedingInOneHour", "yes") .map() )) .withPNCVisits(asList(new PNCVisit() .withDate("2014-01-01") .withPerson("ASHA") .withPlace("phc") .withDifficulties("difficulty 1") .withAbdominalProblems("abdominal problem") .withVaginalProblems("vaginal problem") .withUrinalProblems("urinal problem") .withBreastProblems("breast problem") .withChildrenDetails(asList(create("id", "123") .put("complications 1", "complication 1 value") .put("complications 2", "complications 2 value") .map(), create("id", "234") .put("complications 1", "complication 1 value") .put("complications 2", "complications 2 value") .map() ) ), new PNCVisit() .withDate("2014-01-02") .withPerson("ASHA") .withPlace("phc") .withDifficulties("difficulty 1") .withAbdominalProblems("abdominal problem") .withVaginalProblems("vaginal problem") .withUrinalProblems("urinal problem") .withBreastProblems("breast problem") .withChildrenDetails(asList(create("id", "123") .put("complications 1", "complication 1 value") .put("complications 2", "complications 2 value") .map(), create("id", "234") .put("complications 1", "complication 1 value") .put("complications 2", "complications 2 value") .map() ) ) )) .withDeliveryOutCome("2014-01-01"); 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") .put("currentMethod", "condom") .put("familyPlanningMethodChangeDate", "2014-01-02") .map() ) .withCouple("name1", "name2"); when(allMothers.findAllOpenPNCsForANM("anm1")).thenReturn(asList(mother)); when(allEligibleCouples.findAll(asList("ecCaseId"))).thenReturn(asList(eligibleCouple)); PNCRegister expectedRegister = new PNCRegister(asList(new PNCRegisterEntry() .withRegistrationDate("2014-01-01") .withThayiCardNumber("thayi card number 1") .withWifeName("name1") .withHusbandName("name2") .withWifeDOB("1989-01-01") .withAddress("address1") .withDateOfDelivery("2014-01-01") .withPlaceOfDelivery("phc") .withTypeOfDelivery("normal") .withDischargeDate("2014-01-01") .withFPMethodName("condom") .withFPMethodDate("2014-01-02") .withDeliveryComplications("difficulty breathing") .withChildrenDetails(asList(create("id", "123") .put("sex", "male") .put("birthWeight", "2") .put("breastFeedingInOneHour", "yes") .map(), create("id", "234") .put("sex", "male") .put("birthWeight", "2") .put("breastFeedingInOneHour", "yes") .map() ) ).withPNCVisits(asList(new PNCVisit() .withDate("2014-01-01") .withPerson("ASHA") .withPlace("phc") .withDifficulties("difficulty 1") .withAbdominalProblems("abdominal problem") .withVaginalProblems("vaginal problem") .withUrinalProblems("urinal problem") .withBreastProblems("breast problem") .withChildrenDetails(asList(create("id", "123") .put("complications 1", "complication 1 value") .put("complications 2", "complications 2 value") .map(), create("id", "234") .put("complications 1", "complication 1 value") .put("complications 2", "complications 2 value") .map() ) ), new PNCVisit() .withDate("2014-01-02") .withPerson("ASHA") .withPlace("phc") .withDifficulties("difficulty 1") .withAbdominalProblems("abdominal problem") .withVaginalProblems("vaginal problem") .withUrinalProblems("urinal problem") .withBreastProblems("breast problem") .withChildrenDetails(asList(create("id", "123") .put("complications 1", "complication 1 value") .put("complications 2", "complications 2 value") .map(), create("id", "234") .put("complications 1", "complication 1 value") .put("complications 2", "complications 2 value") .map() ) ) )))); PNCRegister register = registerService.getRegisterForANM("anm1"); assertEquals(expectedRegister, register); }