ChildRegisterService { public ChildRegister getRegisterForANM(String anmIdentifier) { ArrayList<ChildRegisterEntry> childRegisterEntries = new ArrayList<>(); List<Child> children = allChildren.findAllOpenChildrenForANM(anmIdentifier); Collection<String> motherIds = selectDistinct(collect(children, on(Child.class).motherCaseId())); List<String> motherIdsList = new ArrayList<>(); motherIdsList.addAll(motherIds); List<Mother> mothers = allMothers.findAll(motherIdsList); 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 (Child child : children) { Mother mother = selectUnique(mothers, having(on(Mother.class).caseId(), equalTo(child.motherCaseId()))); EligibleCouple ec = selectUnique(ecs, having(on(EligibleCouple.class).caseId(), equalTo(mother.ecCaseId()))); ChildRegisterEntry entry = new ChildRegisterEntry() .withThayiCardNumber(mother.thayiCardNumber()) .withWifeName(ec.wifeName()) .withHusbandName(ec.husbandName()) .withWifeDOB(ec.wifeDOB().toString()) .withVillage(ec.location().village()) .withSubCenter(ec.location().subCenter()) .withDOB(child.dateOfBirth()) .withImmunizations(child.immunizations()) .withVitaminADoses(child.vitaminADoses()); childRegisterEntries.add(entry); } return new ChildRegister(childRegisterEntries); } @Autowired  ChildRegisterService(AllChildren allChildren,
                                AllMothers allMothers, AllEligibleCouples allEligibleCouples); ChildRegister getRegisterForANM(String anmIdentifier);  }
@Test public void shouldGetChildRegisterForAGivenANM() throws Exception { Map<String, String> immunizations = EasyMap.create("bcg", "2013-01-01") .put("opv_0", "2013-01-01") .put("hepb_0", "2013-01-01") .put("opv_1", "2013-01-01") .put("pentavalent_1", "2013-01-01") .put("opv_2", "2013-01-01") .put("pentavalent_2", "2013-01-01") .put("opv_3", "2013-01-01") .put("pentavalent_3", "2013-01-01") .put("measles", "2013-01-01") .put("je", "2013-01-01") .put("mmr", "2013-01-01") .put("dptbooster_1", "2013-01-01") .put("dptbooster_2", "2013-01-01") .put("opvbooster", "2013-01-01") .put("measlesbooster", "2013-01-01") .put("je_2", "2013-01-01") .map(); Map<String, String> vitaminADoses = EasyMap.create("1", "2013-01-01") .put("2", "2013-01-01") .map(); Child child = new Child("child id", "mother id", "bcg opv_0 hepb_0 opv_1 pentavalent_1 opv_2 pentavalent_2", "3.0", "male") .withImmunizations(immunizations) .withVitaminADoses(vitaminADoses) .withDateOfBirth("2013-01-01"); Mother mother = new Mother("mother id", "ec id", "thayi card number 1"); EligibleCouple eligibleCouple = new EligibleCouple("ec id", "123") .withCouple("name1", "name2") .withLocation("boregowdanakoppalu", "hosa_agrahara", "phc") .withDetails( mapOf("womanDOB", "1989-01-01")); ChildRegister expectedRegister = new ChildRegister(asList(new ChildRegisterEntry() .withThayiCardNumber("thayi card number 1") .withWifeName("name1") .withHusbandName("name2") .withWifeDOB("1989-01-01") .withVillage("boregowdanakoppalu") .withSubCenter("hosa_agrahara") .withDOB("2013-01-01") .withImmunizations(immunizations) .withVitaminADoses(vitaminADoses))); when(allChildren.findAllOpenChildrenForANM("anm1")).thenReturn(asList(child)); when(allMothers.findAll(asList("mother id"))).thenReturn(asList(mother)); when(allEligibleCouples.findAll(asList("ec id"))).thenReturn(asList(eligibleCouple)); ChildRegister register = registerService.getRegisterForANM("anm1"); assertEquals(expectedRegister, register); }