ANCDetailController { public String get() { Mother mother = allBeneficiaries.findMotherWithOpenStatus(caseId); EligibleCouple couple = allEligibleCouples.findByCaseID(mother.ecCaseId()); LocalDate lmp = LocalDate.parse(mother.referenceDate()); String edd = lmp.plusWeeks(DURATION_OF_PREGNANCY_IN_WEEKS).toString(); Months numberOfMonthsPregnant = Months.monthsBetween(lmp, DateUtil.today()); String photoPath = isBlank(couple.photoPath()) ? DEFAULT_WOMAN_IMAGE_PLACEHOLDER_PATH : couple.photoPath(); int months = numberOfMonthsPregnant.getMonths(); LocalDate eddDate = LocalDate.parse(edd); Days daysPastEdd = Days.daysBetween(eddDate, DateUtil.today()); ANCDetail detail = new ANCDetail(caseId, mother.thayiCardNumber(), new CoupleDetails(couple.wifeName(), couple.husbandName(), couple.ecNumber(), couple.isOutOfArea()) .withCaste(couple.details().get("caste")) .withEconomicStatus(couple.details().get("economicStatus")) .withPhotoPath(photoPath), new LocationDetails(couple.village(), couple.subCenter()), new PregnancyDetails(String.valueOf(months), edd, daysPastEdd.getDays())) .addTimelineEvents(getEvents()) .addExtraDetails(mother.details()); return new Gson().toJson(detail); } ANCDetailController(Context context, String caseId, AllEligibleCouples allEligibleCouples, AllBeneficiaries allBeneficiaries, AllTimelineEvents allTimelineEvents); String get(); void takePhoto(); static final int DURATION_OF_PREGNANCY_IN_WEEKS; }
@Test public void shouldGetANCDetailsAsJSON() { TimelineEvent pregnancyEvent = TimelineEvent.forStartOfPregnancy(caseId, "2011-10-21", "2011-10-21"); TimelineEvent ancEvent = TimelineEvent.forANCCareProvided(caseId, "2", "2011-12-22", new HashMap<String, String>()); TimelineEvent eventVeryCloseToCurrentDate = TimelineEvent.forANCCareProvided(caseId, "2", "2012-07-29", new HashMap<String, String>()); HashMap<String, String> details = new HashMap<String, String>(); details.put("ashaName", "Shiwani"); when(allBeneficiaries.findMotherWithOpenStatus(caseId)).thenReturn(new Mother(caseId, "EC CASE 1", "TC 1", "2011-10-22").withDetails(details)); Map<String, String> ecDetails = mapOf("caste", "st"); ecDetails.put("economicStatus", "bpl"); when(allEligibleCouples.findByCaseID("EC CASE 1")).thenReturn(new EligibleCouple("EC CASE 1", "Woman 1", "Husband 1", "EC Number 1", "Village 1", "Subcenter 1", ecDetails).withPhotoPath("photo path")); when(allTimelineEvents.forCase(caseId)).thenReturn(asList(pregnancyEvent, ancEvent, eventVeryCloseToCurrentDate)); ANCDetail expectedDetail = new ANCDetail(caseId, "TC 1", new CoupleDetails("Woman 1", "Husband 1", "EC Number 1", false) .withCaste("st") .withEconomicStatus("bpl") .withPhotoPath("photo path"), new LocationDetails("Village 1", "Subcenter 1"), new PregnancyDetails("9", "2012-07-28", 4)) .addTimelineEvents(asList(eventFor(eventVeryCloseToCurrentDate, "29-07-2012"), eventFor(ancEvent, "22-12-2011"), eventFor(pregnancyEvent, "21-10-2011"))) .addExtraDetails(details); String actualJson = controller.get(); ANCDetail actualDetail = new Gson().fromJson(actualJson, ANCDetail.class); assertEquals(expectedDetail, actualDetail); }