ServicesProvidedRepository { @Transactional("service_provided") public void save(String serviceProviderIdentifier, String serviceProviderType, String externalId, String indicator, String date, String village, String subCenter, String phcIdentifier, String quantity, String dristhiEntityId) { Probe probeForCache = monitor.start(REPORTING_SERVICE_PROVIDED_CACHE_TIME); Indicator fetchedIndicator = cachedIndicators.fetch(new Indicator(indicator)); Date dates = LocalDate.parse(date).toDate(); Location location = locationRepository.fetchBy(village, subCenter, phcIdentifier); ServiceProvider serviceProvider = serviceProvidersRepository.fetchBy(serviceProviderIdentifier, parse(serviceProviderType)); monitor.end(probeForCache); int count = getCount(quantity); Probe probeForInsert = monitor.start(REPORTING_SERVICE_PROVIDED_INSERT_TIME); for (int i = 0; i < count; i++) { try { servicesProvidedRepository.save(serviceProvider, externalId, fetchedIndicator, dates, location, dristhiEntityId); } catch (Exception e) { cachedIndicators.clear(fetchedIndicator); } } monitor.end(probeForInsert); } protected  ServicesProvidedRepository(); @Autowired  ServicesProvidedRepository(@Qualifier("serviceProvidedIndicatorRepository") IndicatorCacheableRepository indicatorRepository,
                                      AllLocationsRepository locationRepository,
                                      AllServiceProvidersRepository serviceProvidersRepository,
                                      AllServicesProvidedRepository servicesProvidedRepository, Monitor monitor); @Transactional("service_provided") void save(String serviceProviderIdentifier, String serviceProviderType, String externalId, String indicator,
                     String date, String village, String subCenter, String phcIdentifier, String quantity, String dristhiEntityId); @Transactional("service_provided") void update(ReportDataUpdateRequest request); @Transactional("service_provided") List<ServiceProvidedReport> getNewReports(Integer token); @Transactional("service_provided") List<ServiceProvidedReport> getNewReports(Integer token, int numberOfRowsToFetch); @Transactional("service_provided") void delete(ReportDataDeleteRequest request);  }
@Test public void shouldSaveReportDataAndUseCachedRepositories() throws Exception { String anmIdentifier = "ANM X"; String externalId = "12345"; String indicator = "ANC"; String village = "Bherya"; String subCenter = "Sub Center"; Date date = LocalDate.parse("2012-04-04").toDate(); PHC phc = new PHC(34, "PHC X", "PHC"); String dristhiEntityId = "entity id 1"; ServiceProvider serviceProvider = new ServiceProvider(2, 2, ANM); Indicator fetchedIndicator = new Indicator(2, indicator); Location location = new Location(2, village, subCenter, phc, "taluka", "mysore", "karnataka"); when(serviceProvidersRepository.fetchBy("ANM X", ANM)).thenReturn(serviceProvider); when(indicatorRepository.fetch(new Indicator(indicator))).thenReturn(fetchedIndicator); when(locationRepository.fetchBy(village, subCenter, phc.phcIdentifier())).thenReturn(location); repository.save(anmIdentifier, "ANM", externalId, indicator, "2012-04-04", village, subCenter, phc.phcIdentifier(), null, dristhiEntityId); repository.save(anmIdentifier, "ANM", externalId, indicator, "2012-04-04", village, subCenter, phc.phcIdentifier(), null, dristhiEntityId); verifyCallsToReadOnlyCachedRepository(indicatorRepository, new Indicator(indicator)); verify(locationRepository, times(2)).fetchBy(village, subCenter, phc.phcIdentifier()); verify(serviceProvidersRepository, times(2)).fetchBy(anmIdentifier, ANM); verify(servicesProvidedRepository, times(2)).save(serviceProvider, "12345", fetchedIndicator, date, location, dristhiEntityId); }
@Test public void shouldSaveAsPerQuantityIfQuantityIsNotNull() throws Exception { String anmIdentifier = "ANM X"; String externalId = "12345"; String indicator = "ANC"; String village = "Bherya"; String subCenter = "Sub Center"; String dristhiEntityId = "entity id 1"; Date date = LocalDate.parse("2012-04-04").toDate(); PHC phc = new PHC(34, "PHC X", "PHC"); ServiceProvider serviceProvider = new ServiceProvider(2, 2, ANM); Indicator fetchedIndicator = new Indicator(2, indicator); Location location = new Location(2, village, subCenter, phc, "taluka", "mysore", "karnataka"); when(serviceProvidersRepository.fetchBy("ANM X", ANM)).thenReturn(serviceProvider); when(indicatorRepository.fetch(new Indicator(indicator))).thenReturn(fetchedIndicator); when(locationRepository.fetchBy(village, subCenter, phc.phcIdentifier())).thenReturn(location); repository.save(anmIdentifier, "ANM", externalId, indicator, "2012-04-04", village, subCenter, phc.phcIdentifier(), "40", dristhiEntityId); verify(servicesProvidedRepository, times(40)).save(serviceProvider, "12345", fetchedIndicator, date, location, dristhiEntityId); }