ANMReportsRepository { @Transactional("anm_report") public void save(String anmIdentifier, String externalId, String indicator, String date, String quantity) { Probe probeForCache = monitor.start(REPORTING_ANM_REPORTS_CACHE_TIME); ANM anm = cachedANMs.fetch(new ANM(anmIdentifier)); Indicator fetchedIndicator = cachedIndicators.fetch(new Indicator(indicator)); monitor.end(probeForCache); int count = getCount(quantity); Probe probeForInsert = monitor.start(REPORTING_ANM_REPORTS_INSERT_TIME); for (int i = 0; i < count; i++) { try { anmReportDataRepository.save(anm, externalId, fetchedIndicator, LocalDate.parse(date).toDate()); } catch (Exception e) { cachedANMs.clear(anm); cachedIndicators.clear(fetchedIndicator); } } monitor.end(probeForInsert); } protected  ANMReportsRepository(); @Autowired  ANMReportsRepository(ANMCacheableRepository anmRepository,
                                @Qualifier("anmReportsIndicatorRepository") IndicatorCacheableRepository indicatorRepository,
                                AllANMReportDataRepository anmReportDataRepository,
                                AllAnnualTargetsRepository annualTargetsRepository, Monitor monitor, ReportMonth reportMonth); @Transactional("anm_report") void save(String anmIdentifier, String externalId, String indicator, String date, String quantity); @Transactional("anm_report") List<ANMIndicatorSummary> fetchANMSummary(String anmIdentifier); @Transactional("anm_report") List<ANMReport> fetchAllANMsReport(); @Transactional("anm_report") void update(ReportDataUpdateRequest request); @Transactional("anm_report") List getReportsFor(String anmId, String startDate, String endDate); @Transactional("anm_report") void delete(ReportDataDeleteRequest request);  }
@Test public void shouldSaveANMReportsAndUseCachedRepositories() throws Exception { String anmIdentifier = "ANM X"; String externalId = "EC CASE 1"; String indicator = "IUD"; Date date = parse("2012-04-04").toDate(); Indicator fetchedIndicator = new Indicator(2, indicator); ANM anm = new ANM(2, anmIdentifier); when(anmRepository.fetch(new ANM((anmIdentifier)))).thenReturn(anm); when(indicatorRepository.fetch(new Indicator(indicator))).thenReturn(fetchedIndicator); repository.save(anmIdentifier, externalId, indicator, "2012-04-04", null); repository.save(anmIdentifier, externalId, indicator, "2012-04-04", null); verifyCallsToReadOnlyCachedRepository(anmRepository, new ANM(anmIdentifier)); verifyCallsToReadOnlyCachedRepository(indicatorRepository, fetchedIndicator); verify(anmReportDataRepository, times(2)).save(anm, externalId, fetchedIndicator, date); }
@Test public void shouldSaveAsPerQuantityIfQuantityIsNotNull() throws Exception { String anmIdentifier = "ANM X"; String externalId = "EC CASE 1"; String indicator = "IUD"; Date date = parse("2012-04-04").toDate(); Indicator fetchedIndicator = new Indicator(2, indicator); ANM anm = new ANM(2, anmIdentifier); when(anmRepository.fetch(new ANM((anmIdentifier)))).thenReturn(anm); when(indicatorRepository.fetch(new Indicator(indicator))).thenReturn(fetchedIndicator); repository.save(anmIdentifier, externalId, indicator, "2012-04-04", "50"); verify(anmReportDataRepository, times(50)).save(anm, externalId, fetchedIndicator, date); }