RepositoryStatisticService { public void updateStatistics(Long repositoryId) { Repository repository = repositoryRepository.findById(repositoryId).orElse(null); logger.debug("Get the current repository statistics"); RepositoryStatistic repositoryStatistic = repository.getRepositoryStatistic(); if (repositoryStatistic == null) { logger.debug("No current repository statistics (shouldn't happen if repository was created via the service), Create it."); repositoryStatistic = new RepositoryStatistic(); } logger.debug("Update current entity with new statitsitcs"); RepositoryStatistic newRepositoryStatistics = computeBaseStatistics(repositoryId); repositoryStatistic.setUsedTextUnitCount(newRepositoryStatistics.getUsedTextUnitCount()); repositoryStatistic.setUsedTextUnitWordCount(newRepositoryStatistics.getUsedTextUnitWordCount()); repositoryStatistic.setUnusedTextUnitCount(newRepositoryStatistics.getUnusedTextUnitCount()); repositoryStatistic.setUnusedTextUnitWordCount(newRepositoryStatistics.getUnusedTextUnitWordCount()); repositoryStatistic.setPluralTextUnitCount(newRepositoryStatistics.getPluralTextUnitCount()); repositoryStatistic.setPluralTextUnitWordCount(newRepositoryStatistics.getPluralTextUnitWordCount()); repositoryStatistic.setOoslaCreatedBefore(newRepositoryStatistics.getOoslaCreatedBefore()); repositoryStatistic.setOoslaTextUnitCount(newRepositoryStatistics.getOoslaTextUnitCount()); repositoryStatistic.setOoslaTextUnitWordCount(newRepositoryStatistics.getOoslaTextUnitWordCount()); repositoryStatistic.setLastModifiedDate(DateTime.now()); repositoryStatisticRepository.save(repositoryStatistic); logger.debug("Update locale statistics"); for (RepositoryLocale repositoryLocale : repositoryService.getRepositoryLocalesWithoutRootLocale(repository)) { updateLocaleStatistics(repositoryLocale, repositoryStatistic); } logger.debug("Update branch statistics"); branchStatisticService.computeAndSaveBranchStatistics(repositoryId); logger.debug("Stats updated"); }  void updateStatistics(Long repositoryId); RepositoryStatistic computeBaseStatistics(Long repositoryId); RepositoryLocaleStatistic computeLocaleStatistics(RepositoryLocale repositoryLocale);  }
@Test public void testUpdateStatistics() throws Exception { TMTestData tmTestData = new TMTestData(testIdWatcher); logger.debug("First run: compute and save statistics"); repositoryStatisticService.updateStatistics(tmTestData.repository.getId()); Repository repository = repositoryRepository.findById(tmTestData.repository.getId()).orElse(null); RepositoryStatistic repositoryStatistic = repositoryStatisticRepository.findById(repository.getRepositoryStatistic().getId()).orElse(null); Map<String, RepositoryLocaleStatistic> repositoryLocaleStatistics = new HashMap<>(); for (RepositoryLocaleStatistic repositoryLocaleStatistic : repositoryLocaleStatisticRepository.findByRepositoryStatisticId(repositoryStatistic.getId())) { repositoryLocaleStatistics.put(repositoryLocaleStatistic.getLocale().getBcp47Tag(), repositoryLocaleStatistic); } checkTextUnitCounts(repositoryStatistic); int i = 0; checkRepositoryLocaleStatistic(repositoryLocaleStatistics.get("fr-FR"), "fr-FR", 1, 8, 1, 8, 0, 0, 0, 0); checkRepositoryLocaleStatistic(repositoryLocaleStatistics.get("fr-CA"), "fr-CA", 1, 1, 1, 1, 0, 0, 0, 0); checkRepositoryLocaleStatistic(repositoryLocaleStatistics.get("ja-JP"), "ja-JP", 0, 0, 0, 0, 0, 0, 0, 0); checkRepositoryLocaleStatistic(repositoryLocaleStatistics.get("ko-KR"), "ko-KR", 1, 8, 1, 8, 0, 0, 0, 0); }