SpeciesSummaryService { public ImmutableMap<String, ImmutableList<SpeciesSummary>> getSpeciesSummaryGroupedByKingdom(int limit) { return popularSpeciesInfos.get().stream() .collect(groupingBy(SpeciesSummary::getKingdom)) .entrySet().stream() .collect(toImmutableMap( Map.Entry::getKey, entry -> entry.getValue().stream().limit(limit).collect(toImmutableList()))); } SpeciesSummaryService(SpeciesSummaryDao speciesSummaryDao); ImmutableMap<String, ImmutableList<SpeciesSummary>> getSpeciesSummaryGroupedByKingdom(int limit);  }
@Test void mapsSpeciesSummariesToKingdom() { Species species = generateRandomSpecies(); int baselineExperimentsCount = RNG.nextInt(0, 100); int differentialExperimentsCount = RNG.nextInt(baselineExperimentsCount == 0 ? 1 : 0, 100); SpeciesSummary speciesSummary = SpeciesSummary.create( species.getName(), species.getKingdom(), baselineExperimentsCount, differentialExperimentsCount); when(speciesSummaryDaoMock.getExperimentCountBySpecies()) .thenReturn(ImmutableList.of(speciesSummary)); assertThat(subject.getSpeciesSummaryGroupedByKingdom(10)) .containsAllEntriesOf( ImmutableMap.of(species.getKingdom(), ImmutableList.of(speciesSummary))); }