TSnePlotService { public Map<Integer, Set<TSnePoint>> fetchTSnePlotWithClusters(String experimentAccession, int perplexity, int k) { List<TSnePoint.Dto> points = tSnePlotDao.fetchTSnePlotWithClusters(experimentAccession, perplexity, k); return points.stream() .collect(groupingBy(TSnePoint.Dto::clusterId)) .entrySet().stream() .collect(toMap( Map.Entry::getKey, entry -> entry.getValue().stream() .map(pointDto -> TSnePoint.create(pointDto.x(), pointDto.y(), pointDto.name())) .collect(toSet()))); } TSnePlotService(TSnePlotDao tSnePlotDao, CellMetadataDao cellMetadataDao); Set<TSnePoint> fetchTSnePlotWithExpression(String experimentAccession, int perplexity, String geneId); Map<Integer, Set<TSnePoint>> fetchTSnePlotWithClusters(String experimentAccession, int perplexity, int k); Map<String, Set<TSnePoint>> fetchTSnePlotWithMetadata(String experimentAccession,
                                                                 int perplexity,
                                                                 String metadataCategory);  }
@Test @DisplayName("Points retrieved by the DAO class are assigned the right cluster") void testFetchPlotWithClusters() { String experimentAccession = RandomDataTestUtils.generateRandomExperimentAccession(); int[] perplexities = new int[] {1, 5, 10, 15, 20}; int perplexity = perplexities[ThreadLocalRandom.current().nextInt(0, perplexities.length)]; int k = ThreadLocalRandom.current().nextInt(5, 20); Set<TSnePoint.Dto> randomPointDtos = RandomDataTestUtils.generateRandomTSnePointDtosWithClusters(NUMBER_OF_CELLS, k); when(tSnePlotDaoMock.fetchTSnePlotWithClusters(experimentAccession, perplexity, k)) .thenReturn(ImmutableList.copyOf(randomPointDtos)); Map<Integer, Set<TSnePoint>> results = subject.fetchTSnePlotWithClusters(experimentAccession, perplexity, k); for (TSnePoint.Dto tSnePointDto : randomPointDtos) { assertThat(results.get(tSnePointDto.clusterId())) .contains(TSnePoint.create(tSnePointDto.x(), tSnePointDto.y(), tSnePointDto.name())); } assertThat(results) .containsOnlyKeys( randomPointDtos.stream() .collect(groupingBy(TSnePoint.Dto::clusterId)) .keySet() .toArray(new Integer[0])); assertThat(results.values().stream().flatMap(Set::stream).collect(toSet())) .containsExactlyInAnyOrder( randomPointDtos.stream() .map(dto -> TSnePoint.create(dto.x(), dto.y(), dto.name())) .toArray(TSnePoint[]::new)) .extracting("expressionLevel") .allMatch(expressionLevel -> expressionLevel == Optional.empty()); }