GeneSetEnrichmentClient { public Pair<Optional<Exception>, Optional<JsonArray>> fetchEnrichedGenes(String speciesName, Collection<String> bioentityIdentifiers) { try { Pair<Optional<String>, Optional<JsonArray>> errorOrResponse = formatResponse(fetchResponse(speciesName, bioentityIdentifiers)); return errorOrResponse.getLeft().isPresent() ? Pair.of(errorOrResponse.getLeft().map(RuntimeException::new), Optional.empty()) : Pair.of(Optional.empty(), errorOrResponse.getRight()); } catch (Exception e) { LOGGER.error(e.getMessage()); return Pair.of(Optional.of(e), Optional.empty()); } } @Inject  GeneSetEnrichmentClient(RestTemplate restTemplate); Pair<Optional<Exception>, Optional<JsonArray>> fetchEnrichedGenes(String speciesName,
                                                                             Collection<String> bioentityIdentifiers);  }
@Test public void testSuccessWith2Results() { String url = UriComponentsBuilder .fromUriString(GSA_URL_PATTERN) .buildAndExpand(ARABIDOPSIS_SPECIES, GENE_IDS) .encode() .toUriString(); mockServer.expect(requestTo(url)) .andExpect(method(HttpMethod.GET)) .andRespond(withSuccess(SUCCESS_TSV, MediaType.TEXT_PLAIN)); List<String> bioentityIdentifiers = Arrays.asList(GENE_IDS.split("\\W+")); Pair<Optional<Exception>, Optional<JsonArray>> result = subject.fetchEnrichedGenes(ARABIDOPSIS_SPECIES, bioentityIdentifiers); assertThat(result.getLeft()).isEmpty(); assertThat(result.getRight()).isPresent(); JsonArray jsonArray = result.getRight().orElseThrow(RuntimeException::new); assertThat(jsonArray).hasSize(2); assertThat(jsonArray.get(0).getAsJsonObject().get("experiment_accession").getAsString()).isEqualTo("EXP_01"); assertThat(jsonArray.get(0).getAsJsonObject().get("comparison_id").getAsString()).isEqualTo("g1_g2"); assertThat(jsonArray.get(0).getAsJsonObject().get("p-value").getAsDouble()).isEqualTo(1.55); assertThat(jsonArray.get(0).getAsJsonObject().get("observed").getAsInt()).isEqualTo(10); assertThat(jsonArray.get(0).getAsJsonObject().get("adjusted_p-value").getAsDouble()).isEqualTo(0.01); assertThat(jsonArray.get(0).getAsJsonObject().get("effect_size").getAsDouble()).isEqualTo(4.55); assertThat(jsonArray.get(0).getAsJsonObject().get("comparison_title").getAsJsonObject().entrySet()).isEmpty(); assertThat(jsonArray.get(0).getAsJsonObject().get("experiment").getAsString()).isEmpty(); assertThat(jsonArray.get(1).getAsJsonObject().get("experiment_accession").getAsString()).isEqualTo("EXP_02"); assertThat(jsonArray.get(1).getAsJsonObject().get("comparison_id").getAsString()).isEqualTo("g3_g4"); assertThat(jsonArray.get(1).getAsJsonObject().get("p-value").getAsDouble()).isEqualTo(0.1); assertThat(jsonArray.get(1).getAsJsonObject().get("observed").getAsInt()).isEqualTo(5); assertThat(jsonArray.get(1).getAsJsonObject().get("adjusted_p-value").getAsDouble()).isEqualTo(0.005); assertThat(jsonArray.get(1).getAsJsonObject().get("effect_size").getAsDouble()).isEqualTo(3.2); assertThat(jsonArray.get(1).getAsJsonObject().get("comparison_title").getAsJsonObject().entrySet()).isEmpty(); assertThat(jsonArray.get(1).getAsJsonObject().get("experiment").getAsString()).isEmpty(); }
@Test public void testNoSignificantResult() { String geneIdsSubset = "AT1G48030 AT1G53240 AT2G17130"; String url = UriComponentsBuilder .fromUriString(GSA_URL_PATTERN) .buildAndExpand(ARABIDOPSIS_SPECIES, geneIdsSubset) .encode() .toUriString(); mockServer.expect(requestTo(url)) .andExpect(method(HttpMethod.GET)) .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR)); List<String> bioentityIdentifiers = Arrays.asList(geneIdsSubset.split("\\W+")); Pair<Optional<Exception>, Optional<JsonArray>> result = subject.fetchEnrichedGenes(ARABIDOPSIS_SPECIES, bioentityIdentifiers); assertThat(result.getRight()).isEmpty(); assertThat(result.getLeft()).isPresent(); }
@Test public void testEmptyResult() { String url = UriComponentsBuilder .fromUriString(GSA_URL_PATTERN) .buildAndExpand(ARABIDOPSIS_SPECIES, GENE_IDS) .encode() .toUriString(); mockServer.expect(requestTo(url)) .andExpect(method(HttpMethod.GET)) .andRespond(withSuccess()); List<String> bioentityIdentifiers = Arrays.asList(GENE_IDS.split("\\W+")); Pair<Optional<Exception>, Optional<JsonArray>> result = subject.fetchEnrichedGenes(ARABIDOPSIS_SPECIES, bioentityIdentifiers); assertThat(result.getRight()).isEmpty(); assertThat(result.getLeft()).isPresent(); }
@Test public void testMalformedTsvHeaderResult() { String url = UriComponentsBuilder .fromUriString(GSA_URL_PATTERN) .buildAndExpand(ARABIDOPSIS_SPECIES, GENE_IDS) .encode() .toUriString(); mockServer.expect(requestTo(url)) .andExpect(method(HttpMethod.GET)) .andRespond(withSuccess(TSV_WITH_BAD_HEADER, MediaType.TEXT_PLAIN)); List<String> bioentityIdentifiers = Arrays.asList(GENE_IDS.split("\\W+")); Pair<Optional<Exception>, Optional<JsonArray>> result = subject.fetchEnrichedGenes(ARABIDOPSIS_SPECIES, bioentityIdentifiers); assertThat(result.getRight()).isEmpty(); assertThat(result.getLeft()).isPresent(); }
@Test public void testMalformedTsvFileResult() { String url = UriComponentsBuilder .fromUriString(GSA_URL_PATTERN) .buildAndExpand(ARABIDOPSIS_SPECIES, GENE_IDS) .encode() .toUriString(); mockServer.expect(requestTo(url)) .andExpect(method(HttpMethod.GET)) .andRespond(withSuccess(TSV_WITH_DIFFERENT_LINE_SIZES, MediaType.TEXT_PLAIN)); List<String> bioentityIdentifiers = Arrays.asList(GENE_IDS.split("\\W+")); Pair<Optional<Exception>, Optional<JsonArray>> result = subject.fetchEnrichedGenes(ARABIDOPSIS_SPECIES, bioentityIdentifiers); assertThat(result.getRight()).isEmpty(); assertThat(result.getLeft()).isPresent(); }
@Test public void testNaNValuesResult() { String url = UriComponentsBuilder .fromUriString(GSA_URL_PATTERN) .buildAndExpand(ARABIDOPSIS_SPECIES, GENE_IDS) .encode() .toUriString(); mockServer.expect(requestTo(url)) .andExpect(method(HttpMethod.GET)) .andRespond(withSuccess(TSV_WITH_NAN, MediaType.TEXT_PLAIN)); List<String> bioentityIdentifiers = Arrays.asList(GENE_IDS.split("\\W+")); Pair<Optional<Exception>, Optional<JsonArray>> result = subject.fetchEnrichedGenes(ARABIDOPSIS_SPECIES, bioentityIdentifiers); assertThat(result.getLeft()).isEmpty(); assertThat(result.getRight()).isPresent(); JsonArray jsonArray = result.getRight().orElseThrow(RuntimeException::new); assertThat(jsonArray).hasSize(2); assertThat(jsonArray.get(0).getAsJsonObject().get("effect_size").getAsString()).isEqualTo("NaN"); assertThat(jsonArray.get(1).getAsJsonObject().get("effect_size").getAsDouble()).isEqualTo(3.2); }