LanguageService { public List<Language> getMainLanguages(final String organizations, final Comparator<Language> c, Optional<String> filterLanguage) { Collection<String> organizationList = StringParser.parseStringList(organizations, ","); List<Project> projectList = new ArrayList<>(); for (String org : organizationList) { Iterable<Project> projects = repository.findProjects(org, Optional.empty(), filterLanguage); for (Project project : projects) { projectList.add(project); } } List<String> languageList = new ArrayList<>(); for (Project p : projectList) { if (StringUtils.isEmpty(p.getPrimaryLanguage())) { logger.info(String.format("No primary programming language set for project [%s].", p.getName())); continue; } languageList.add(p.getPrimaryLanguage()); } List<Language> languages = new ArrayList<>(); Set<String> languageSet = new HashSet<>(languageList); int frequency; for (String language : languageSet) { Language l = new Language(language); frequency = Collections.frequency(languageList, language); l.setPercentage((int) Math.round(((double) frequency) / languageList.size() * 100)); l.setProjectsCount(frequency); languages.add(l); } if (languages.size() > 1) { Collections.sort(languages, c); } return languages; } @Autowired  LanguageService(ProjectRepository repository); List<Language> filterLanguages(List<Language> languages, int limit,  int offset); List<Language> getMainLanguages(final String organizations, final Comparator<Language> c, Optional<String> filterLanguage); static final Logger logger; }
@Test public void checkProgrammingLanguage() { logger.info("Setting up the projects..."); Project p1 = new ProjectBuilder().commitsCount(10) .contributorsCount(5) .forksCount(1) .gitHubProjectId(12345) .description("bogus project 1") .name("bogus project 1") .primaryLanguage("Java") .organizationName("zalando-stups") .getProject(); Project p2 = new ProjectBuilder().commitsCount(10) .contributorsCount(5) .forksCount(1) .gitHubProjectId(12345) .description("bogus project 2") .name("bogus project 2") .primaryLanguage("Scala") .organizationName("zalando-stups") .getProject(); Project p3 = new ProjectBuilder().commitsCount(10) .contributorsCount(5) .forksCount(1) .gitHubProjectId(12345) .description("bogus project 3") .name("bogus project 3") .primaryLanguage("C++") .organizationName("zalando") .getProject(); Project p4 = new ProjectBuilder().commitsCount(10) .contributorsCount(5) .forksCount(1) .gitHubProjectId(12345) .description("bogus project 4") .name("bogus project 4") .primaryLanguage(null) .organizationName("zalando") .getProject(); projectRepository.save(p1); projectRepository.save(p2); projectRepository.save(p3); projectRepository.save(p4); String organizations = "zalando,zalando-stups"; logger.info("Calling language service..."); List<Project> projectsZalando = new ArrayList<>(); List<Project> projectsZalandoStups = new ArrayList<>(); projectsZalandoStups.add(p1); projectsZalandoStups.add(p2); projectsZalando.add(p3); projectsZalando.add(p4); when(projectRepository.findProjects("zalando", empty(), empty())).thenReturn(projectsZalando); when(projectRepository.findProjects("zalando-stups", empty(), empty())).thenReturn(projectsZalandoStups); List<Language> result = languageService.getMainLanguages(organizations, new LanguagePercentComparator(), empty()); Assert.assertEquals(3, result.size()); }