Commit 5ae480e4 authored by Matija Obreza's avatar Matija Obreza

Merge branch '449-filter-on-taxonomy' into 'master'

Resolve "Filter on taxonomy"

Closes #449

See merge request genesys-pgr/genesys-server!400
parents b30650b7 bc01759c
...@@ -80,9 +80,7 @@ public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter, ...@@ -80,9 +80,7 @@ public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter,
if (CollectionUtils.isNotEmpty(genus) && taxonomyRepository != null) { if (CollectionUtils.isNotEmpty(genus) && taxonomyRepository != null) {
// We need to look up the taxonomy2.id for provided names so that ES queries also work out of the box // We need to look up the taxonomy2.id for provided names so that ES queries also work out of the box
List<Long> taxGenus = taxonomyRepository.findTaxGenusId(new ArrayList<>(genus)); List<Long> taxGenus = taxonomyRepository.findTaxGenusId(new ArrayList<>(genus));
if (CollectionUtils.isNotEmpty(taxGenus)) { and.and(taxonomy.taxGenus.in(taxGenus));
and.and(taxonomy.taxGenus.in(taxGenus));
}
} }
if (species != null && !species.isEmpty()) { if (species != null && !species.isEmpty()) {
and.and(taxonomy.species.in(species)); and.and(taxonomy.species.in(species));
...@@ -98,9 +96,7 @@ public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter, ...@@ -98,9 +96,7 @@ public class TaxonomyFilter extends AuditedVersionedModelFilter<TaxonomyFilter,
if (CollectionUtils.isNotEmpty(genusSpecies) && taxonomyRepository != null) { if (CollectionUtils.isNotEmpty(genusSpecies) && taxonomyRepository != null) {
// We need to look up the taxonomy2.id for provided names so that ES queries also work out of the box // We need to look up the taxonomy2.id for provided names so that ES queries also work out of the box
List<Long> taxSpecies = taxonomyRepository.findTaxSpeciesId(new ArrayList<>(genusSpecies)); List<Long> taxSpecies = taxonomyRepository.findTaxSpeciesId(new ArrayList<>(genusSpecies));
if (CollectionUtils.isNotEmpty(taxSpecies)) { and.and(taxonomy.taxSpecies.in(taxSpecies));
and.and(taxonomy.taxSpecies.in(taxSpecies));
}
} }
return and; return and;
......
...@@ -29,6 +29,7 @@ import java.util.UUID; ...@@ -29,6 +29,7 @@ import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import com.google.common.collect.Sets;
import org.genesys.test.base.AbstractApiTest; import org.genesys.test.base.AbstractApiTest;
import org.genesys2.server.api.v1.AccessionController; import org.genesys2.server.api.v1.AccessionController;
import org.genesys2.server.api.v1.AccessionUploadController; import org.genesys2.server.api.v1.AccessionUploadController;
...@@ -42,6 +43,7 @@ import org.genesys2.server.persistence.FaoInstituteRepository; ...@@ -42,6 +43,7 @@ import org.genesys2.server.persistence.FaoInstituteRepository;
import org.genesys2.server.persistence.Taxonomy2Repository; import org.genesys2.server.persistence.Taxonomy2Repository;
import org.genesys2.server.service.InstituteService; import org.genesys2.server.service.InstituteService;
import org.genesys2.server.service.TaxonomyService; import org.genesys2.server.service.TaxonomyService;
import org.genesys2.server.service.filter.AccessionFilter;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Rule; import org.junit.Rule;
...@@ -158,6 +160,57 @@ public class AccessionControllerTest extends AbstractApiTest { ...@@ -158,6 +160,57 @@ public class AccessionControllerTest extends AbstractApiTest {
assertThat(accessionRepository.count(), is(1L)); assertThat(accessionRepository.count(), is(1L));
} }
@Test
public void taxonomyFilterTest() throws Exception {
ObjectNode accessionJson = setUpAccession();
final String s = "[" + verboseMapper.writeValueAsString(accessionJson) + "]";
/*@formatter:off*/
mockMvc
.perform(post(AccessionUploadController.CONTROLLER_URL + "/" + institute.getCode() + "/upsert")
.contentType(MediaType.APPLICATION_JSON)
.content(s))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$[0].instCode", is(institute.getCode())))
.andExpect(jsonPath("$[0].acceNumb", is(accessionJson.get("accessionNumber").textValue())))
.andExpect(jsonPath("$[0].genus", is(accessionJson.get("taxonomy").get("genus").textValue())))
.andExpect(jsonPath("$[0].error", nullValue()))
.andExpect(jsonPath("$[0].result.action", equalTo("INSERT")));
/*@formatter:on*/
assertThat(accessionRepository.count(), is(1L));
AccessionFilter filter = new AccessionFilter();
filter.taxonomy = filter.taxa();
filter.taxonomy.genusSpecies = Sets.newHashSet("Manihot sp.");
/*@formatter:off*/
mockMvc
.perform(post(AccessionUploadController.CONTROLLER_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(verboseMapper.writeValueAsString(filter)))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.totalElements", is(1)))
.andExpect(jsonPath("$.content[0].taxonomy.taxonName", is("Manihot sp.")));
/*@formatter:on*/
filter.taxonomy.genusSpecies = Sets.newHashSet("Must not match");
/*@formatter:off*/
mockMvc
.perform(post(AccessionUploadController.CONTROLLER_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(verboseMapper.writeValueAsString(filter)))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.totalElements", is(0)));
/*@formatter:on*/
}
@Test @Test
public void getAccessionsTest() throws Exception { public void getAccessionsTest() throws Exception {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment