GeneIdSearchService { public Optional<ImmutableSet<String>> search(GeneQuery geneQuery) { if (geneQuery.category().isPresent()) { if (geneQuery.species().isPresent() && !BioentitiesCollectionProxy.SPECIES_OVERRIDE_PROPERTY_NAMES.contains(geneQuery.category().get())) { LOGGER.debug( "Searching {}/{} in species {}", geneQuery.queryTerm(), geneQuery.category().get(), geneQuery.species().get().getEnsemblName()); return geneIdSearchDao.searchGeneIds( geneQuery.queryTerm(), geneQuery.category().get().name, geneQuery.species().get().getEnsemblName()); } LOGGER.debug( "Searching {}/{} ignoring species {}", geneQuery.queryTerm(), geneQuery.category().get(), geneQuery.species().map(Species::getEnsemblName).orElse("(none provided)")); return geneIdSearchDao.searchGeneIds(geneQuery.queryTerm(), geneQuery.category().get().name); } LOGGER.debug( "Searching {} (free text without category) in species {}", geneQuery.queryTerm(), geneQuery.species().map(Species::getEnsemblName).orElse("(none provided)")); return geneQuery.species().isPresent() ? searchIds(propertyName -> geneIdSearchDao.searchGeneIds( geneQuery.queryTerm(), propertyName, geneQuery.species().get().getEnsemblName())) : searchIds(propertyName -> geneIdSearchDao.searchGeneIds(geneQuery.queryTerm(), propertyName)); } GeneIdSearchService(GeneIdSearchDao geneIdSearchDao); Optional<ImmutableSet<String>> search(GeneQuery geneQuery);  }
@Test void speciesSpecificCategoriesIgnoreSpecies() { SPECIES_OVERRIDE_PROPERTY_NAMES.forEach(propertyName -> { subject.search(GeneQuery.create("foobar", propertyName, HUMAN)); inOrder.verify(geneIdSearchDao).searchGeneIds("foobar", propertyName.name); }); SPECIES_OVERRIDE_PROPERTY_NAMES.forEach(propertyName -> verify(geneIdSearchDao, never()).searchGeneIds("foobar", propertyName.name, HUMAN.getEnsemblName())); }
@Test void multiSpeciesCategoriesHonourSpecies() { BioentityPropertyName propertyName = generateRandomKnownBioentityPropertyName(); while (SPECIES_OVERRIDE_PROPERTY_NAMES.contains(propertyName)) { propertyName = generateRandomKnownBioentityPropertyName(); } subject.search(GeneQuery.create("foobar", propertyName, HUMAN)); verify(geneIdSearchDao).searchGeneIds("foobar", propertyName.name, HUMAN.getEnsemblName()); }
@Test void ifNoIdMatchesWeGetEmptyOptional() { assertThat(subject.search(GeneQuery.create("foobar", HUMAN))) .isEqualTo(subject.search(GeneQuery.create("foobar"))) .isEmpty(); }
@Test void ifAtLeastOneIdMatchesWeGetNonEmptyOptional() { BioentityPropertyName randomIdPropertyName = generateRandomKnownBioentityPropertyName(); while (!SPECIES_OVERRIDE_PROPERTY_NAMES.contains(randomIdPropertyName)) { randomIdPropertyName = generateRandomKnownBioentityPropertyName(); } ID_PROPERTY_NAMES.forEach(propertyName -> { when(geneIdSearchDao.searchGeneIds("foobar", propertyName.name, HUMAN.getEnsemblName())) .thenReturn(Optional.empty()); when(geneIdSearchDao.searchGeneIds("foobar", propertyName.name)) .thenReturn(Optional.empty()); }); when(geneIdSearchDao.searchGeneIds("foobar", randomIdPropertyName.name, HUMAN.getEnsemblName())) .thenReturn(Optional.of(ImmutableSet.of())); when(geneIdSearchDao.searchGeneIds("foobar", randomIdPropertyName.name)) .thenReturn(Optional.of(ImmutableSet.of())); assertThat(subject.search(GeneQuery.create("foobar", HUMAN))) .hasValue(ImmutableSet.of()); assertThat(subject.search(GeneQuery.create("foobar"))) .hasValue(ImmutableSet.of()); }
@Test void resultsOfFirstIdThatMatchesAreReturned() { BioentityPropertyName randomIdPropertyName = generateRandomKnownBioentityPropertyName(); while (!ID_PROPERTY_NAMES.contains(randomIdPropertyName)) { randomIdPropertyName = generateRandomKnownBioentityPropertyName(); } ImmutableList<BioentityPropertyName> idPropertyNamesBefore = ID_PROPERTY_NAMES.subList(0, ID_PROPERTY_NAMES.indexOf(randomIdPropertyName)); ImmutableList<BioentityPropertyName> idPropertyNamesAfter = ID_PROPERTY_NAMES.subList( ID_PROPERTY_NAMES.indexOf(randomIdPropertyName) + 1, ID_PROPERTY_NAMES.size()); idPropertyNamesBefore.forEach(propertyName -> { when(geneIdSearchDao.searchGeneIds("foobar", propertyName.name, HUMAN.getEnsemblName())) .thenReturn(Optional.of(ImmutableSet.of())); when(geneIdSearchDao.searchGeneIds("foobar", propertyName.name)) .thenReturn(Optional.of(ImmutableSet.of())); }); idPropertyNamesAfter.forEach(propertyName -> { when(geneIdSearchDao.searchGeneIds("foobar", propertyName.name, HUMAN.getEnsemblName())) .thenReturn(Optional.of(ImmutableSet.of("ENSFOOBAR0000002"))); when(geneIdSearchDao.searchGeneIds("foobar", propertyName.name)) .thenReturn(Optional.of(ImmutableSet.of("ENSFOOBAR0000002"))); }); when(geneIdSearchDao.searchGeneIds("foobar", randomIdPropertyName.name, HUMAN.getEnsemblName())) .thenReturn(Optional.of(ImmutableSet.of("ENSFOOBAR0000001"))); when(geneIdSearchDao.searchGeneIds("foobar", randomIdPropertyName.name)) .thenReturn(Optional.of(ImmutableSet.of("ENSFOOBAR0000001"))); assertThat(subject.search(GeneQuery.create("foobar", HUMAN))) .isEqualTo(subject.search(GeneQuery.create("foobar"))) .hasValue(ImmutableSet.of("ENSFOOBAR0000001")); idPropertyNamesBefore.forEach(propertyName -> inOrder.verify(geneIdSearchDao).searchGeneIds("foobar", propertyName.name, HUMAN.getEnsemblName())); inOrder.verify(geneIdSearchDao).searchGeneIds("foobar", randomIdPropertyName.name, HUMAN.getEnsemblName()); idPropertyNamesBefore.forEach(propertyName -> inOrder.verify(geneIdSearchDao).searchGeneIds("foobar", propertyName.name)); inOrder.verify(geneIdSearchDao).searchGeneIds("foobar", randomIdPropertyName.name); idPropertyNamesAfter.forEach(propertyName -> { verify(geneIdSearchDao, never()).searchGeneIds("foobar", propertyName.name, HUMAN.getEnsemblName()); verify(geneIdSearchDao, never()).searchGeneIds("foobar", propertyName.name); }); }
@Test void geneQueryWithoutCategoryIsSearchedInIdProperties() { subject.search(GeneQuery.create("foobar")); subject.search(GeneQuery.create("foobar", HUMAN)); ID_PROPERTY_NAMES.forEach(propertyName -> inOrder.verify(geneIdSearchDao).searchGeneIds("foobar", propertyName.name)); ID_PROPERTY_NAMES.forEach(propertyName -> inOrder.verify(geneIdSearchDao).searchGeneIds("foobar", propertyName.name, HUMAN.getEnsemblName())); }