ChildSmartRegisterController { public String get() { return cache.get(CHILD_CLIENTS_LIST_CACHE_ENTRY_NAME, new CacheableData<String>() { @Override public String fetch() { List<Child> children = allBeneficiaries.allChildrenWithMotherAndEC(); List<ChildClient> childrenClient = new ArrayList<ChildClient>(); for (Child child : children) { String photoPath = isBlank(child.photoPath()) ? (AllConstants.FEMALE_GENDER.equalsIgnoreCase(child.gender()) ? AllConstants.DEFAULT_GIRL_INFANT_IMAGE_PLACEHOLDER_PATH : AllConstants.DEFAULT_BOY_INFANT_IMAGE_PLACEHOLDER_PATH) : child.photoPath(); List<AlertDTO> alerts = getAlerts(child.caseId()); List<ServiceProvidedDTO> servicesProvided = getServicesProvided(child.caseId()); ChildClient childClient = new ChildClient( child.caseId(), child.gender(), child.getDetail(AllConstants.ChildRegistrationFields.WEIGHT), child.mother().thayiCardNumber()) .withName(child.getDetail(AllConstants.ChildRegistrationFields.NAME)) .withEntityIdToSavePhoto(child.caseId()) .withMotherName(child.ec().wifeName()) .withDOB(child.dateOfBirth()) .withMotherAge(child.ec().age()) .withFatherName(child.ec().husbandName()) .withVillage(child.ec().village()) .withOutOfArea(child.ec().isOutOfArea()) .withEconomicStatus(child.ec().getDetail(AllConstants.ECRegistrationFields.ECONOMIC_STATUS)) .withCaste(child.ec().getDetail(AllConstants.ECRegistrationFields.CASTE)) .withIsHighRisk(child.isHighRisk()) .withPhotoPath(photoPath) .withECNumber(child.ec().ecNumber()) .withAlerts(alerts) .withServicesProvided(servicesProvided); childrenClient.add(childClient); } sortByMotherName(childrenClient); return new Gson().toJson(childrenClient); } }); } ChildSmartRegisterController(ServiceProvidedService serviceProvidedService, AlertService alertService,
                                        AllBeneficiaries allBeneficiaries, Cache<String> cache, Cache<SmartRegisterClients> smartRegisterCache); String get(); SmartRegisterClients getClients();  }
@Test public void shouldSortChildrenByMotherName() throws Exception { EligibleCouple ec1 = new EligibleCouple("ec id 1", "amma", "appa", "ec no 1", "chikkamagalur", null, emptyMap).asOutOfArea(); Mother mother1 = new Mother("mother id 1", "ec id 1", "thayi no 1", "2013-01-01").withDetails(emptyMap); Child child1 = new Child("child id 1", "mother id 1", "male", emptyMap).withDateOfBirth("2013-01-01").withMother(mother1).withEC(ec1); EligibleCouple ec2 = new EligibleCouple("ec id 2", "thayi", "appa", "ec no 2", "chikkamagalur", null, emptyMap).asOutOfArea(); Mother mother2 = new Mother("mother id 2", "ec id 2", "thayi no 2", "2013-01-01").withDetails(emptyMap); Child child2 = new Child("child id 2", "mother id 2", "male", emptyMap).withDateOfBirth("2013-01-01").withMother(mother2).withEC(ec2); when(allBeneficiaries.allChildrenWithMotherAndEC()).thenReturn(asList(child2, child1)); ChildClient expectedClient1 = createChildClient("child id 1", "thayi no 1", "amma", "ec no 1"); ChildClient expectedClient2 = createChildClient("child id 2", "thayi no 2", "thayi", "ec no 2"); String clients = controller.get(); List<ChildClient> actualClients = new Gson().fromJson(clients, new TypeToken<List<ChildClient>>() { }.getType()); assertEquals(asList(expectedClient1, expectedClient2), actualClients); }
@Test public void shouldMapPNCToPNCClient() throws Exception { Map<String, String> ecDetails = create("wifeAge", "26") .put("caste", "others") .put("economicStatus", "apl") .map(); Map<String, String> childDetails = create("weight", "3") .put("name", "chinnu") .put("isChildHighRisk", "yes") .map(); EligibleCouple eligibleCouple = new EligibleCouple("ec id 1", "amma", "appa", "ec no 1", "chikkamagalur", null, ecDetails).asOutOfArea(); Mother mother = new Mother("mother id 1", "ec id 1", "thayi no 1", "2013-01-01").withDetails(emptyMap); Child child = new Child("child id 1", "mother id 1", "female", childDetails).withDateOfBirth("2013-01-01").withMother(mother).withEC(eligibleCouple); when(allBeneficiaries.allChildrenWithMotherAndEC()).thenReturn(asList(child)); ChildClient expectedPNCClient = new ChildClient("child id 1", "female", "3", "thayi no 1") .withEntityIdToSavePhoto("child id 1") .withName("chinnu") .withMotherName("amma") .withDOB("2013-01-01") .withMotherAge("26") .withFatherName("appa") .withVillage("chikkamagalur") .withOutOfArea(true) .withEconomicStatus("apl") .withCaste("others") .withIsHighRisk(true) .withPhotoPath("../../img/icons/child-girlinfant@3x.png") .withEntityIdToSavePhoto("child id 1") .withECNumber("ec no 1") .withAlerts(Collections.<AlertDTO>emptyList()) .withServicesProvided(Collections.<ServiceProvidedDTO>emptyList()); String clients = controller.get(); List<ChildClient> actualClients = new Gson().fromJson(clients, new TypeToken<List<ChildClient>>() { }.getType()); assertEquals(asList(expectedPNCClient), actualClients); }
@Test public void shouldCreateChildClientsWithAlerts() throws Exception { EligibleCouple eligibleCouple = new EligibleCouple("ec id 1", "amma", "appa", "ec no 1", "chikkamagalur", null, emptyMap).asOutOfArea(); Mother mother = new Mother("mother id 1", "ec id 1", "thayi no 1", "2013-01-01").withDetails(emptyMap); Child child = new Child("child id 1", "mother id 1", "male", emptyMap).withDateOfBirth("2013-01-01").withMother(mother).withEC(eligibleCouple); Alert bcgAlert = new Alert("child id 1", "BCG", "bcg", normal, "2013-01-01", "2013-02-01"); when(allBeneficiaries.allChildrenWithMotherAndEC()).thenReturn(asList(child)); when(alertService.findByEntityIdAndAlertNames("child id 1", CHILD_ALERTS)).thenReturn(asList(bcgAlert)); String clients = controller.get(); List<ChildClient> actualClients = new Gson().fromJson(clients, new TypeToken<List<ChildClient>>() { }.getType()); verify(alertService).findByEntityIdAndAlertNames("child id 1", CHILD_ALERTS); AlertDTO expectedAlertDto = new AlertDTO("bcg", "normal", "2013-01-01"); ChildClient expectedPNCClient = new ChildClient("child id 1", "male", null, "thayi no 1") .withEntityIdToSavePhoto("child id 1") .withMotherName("amma") .withDOB("2013-01-01") .withFatherName("appa") .withVillage("chikkamagalur") .withOutOfArea(true) .withPhotoPath("../../img/icons/child-infant@3x.png") .withEntityIdToSavePhoto("child id 1") .withECNumber("ec no 1") .withAlerts(asList(expectedAlertDto)) .withServicesProvided(Collections.<ServiceProvidedDTO>emptyList()); assertEquals(asList(expectedPNCClient), actualClients); }
@Test public void shouldCreateChildClientsWithServicesProvided() throws Exception { EligibleCouple eligibleCouple = new EligibleCouple("ec id 1", "amma", "appa", "ec no 1", "chikkamagalur", null, emptyMap).asOutOfArea(); Mother mother = new Mother("mother id 1", "ec id 1", "thayi no 1", "2013-01-01").withDetails(emptyMap); Child child = new Child("child id 1", "mother id 1", "male", emptyMap).withDateOfBirth("2013-01-01").withMother(mother).withEC(eligibleCouple); when(allBeneficiaries.allChildrenWithMotherAndEC()).thenReturn(asList(child)); when(alertService.findByEntityIdAndAlertNames("child id 1", CHILD_ALERTS)).thenReturn(Collections.<Alert>emptyList()); when(serviceProvidedService.findByEntityIdAndServiceNames("child id 1", CHILD_SERVICES)) .thenReturn(asList(new ServiceProvided("entity id 1", "bcg", "2013-01-01", null))); String clients = controller.get(); List<ChildClient> actualClients = new Gson().fromJson(clients, new TypeToken<List<ChildClient>>() { }.getType()); verify(serviceProvidedService).findByEntityIdAndServiceNames("child id 1", CHILD_SERVICES); List<ServiceProvidedDTO> expectedServicesProvided = asList(new ServiceProvidedDTO("bcg", "2013-01-01", null)); ChildClient expectedPNCClient = createChildClient("child id 1", "thayi no 1", "amma", "ec no 1").withServicesProvided(expectedServicesProvided); assertEquals(asList(expectedPNCClient), actualClients); }