Commit b6940a38 authored by Matija Obreza's avatar Matija Obreza

Filter Accessions by list of accessionNumbers

parent 04653bd5
......@@ -3,7 +3,6 @@ package org.genesys2.brapi.service.impl;
import java.util.UUID;
import java.util.stream.Collectors;
import com.google.common.collect.Sets;
import org.apache.commons.lang3.StringUtils;
import org.genesys2.brapi.model.Germplasm;
import org.genesys2.brapi.service.BrAPIService;
......@@ -102,7 +101,7 @@ public class BrAPIServiceImpl implements BrAPIService {
}
if (StringUtils.isNotBlank(germplasmName)) {
accessionFilter.accessionNumber().eq(germplasmName);
accessionFilter.accessionNumbers().add(germplasmName);
}
......
......@@ -75,8 +75,12 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
public InstituteFilter institute;
/** The acce numb. */
@Deprecated
public StringFilter accessionNumber;
/** The accession numbers. */
public Set<String> accessionNumbers;
/** The doi. */
public Set<String> doi;
......@@ -203,6 +207,9 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
if (CollectionUtil.isNotEmpty(doi)) {
predicates.add(accession.doi.in(doi));
}
if (CollectionUtil.isNotEmpty(accessionNumbers)) {
predicates.add(accession.accessionNumber.in(accessionNumbers));
}
if (accessionNumber != null) {
predicates.add(accessionNumber.buildQuery(accession.accessionNumber));
}
......@@ -290,6 +297,9 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
if (CollectionUtil.isNotEmpty(doi)) {
predicates.add(accession.doi.isNotNull().and(accession.doi.in(doi)));
}
if (CollectionUtil.isNotEmpty(accessionNumbers)) {
predicates.add(accession.accessionNumber.in(accessionNumbers));
}
if (accessionNumber != null) {
predicates.add(accessionNumber.buildQuery(accession.accessionNumber));
}
......@@ -442,12 +452,22 @@ public class AccessionFilter extends UuidModelFilter<AccessionFilter, Accession>
/**
* Accession Number filter.
*
* @deprecated Will be removed soon.
* @return the string filter
*/
public synchronized StringFilter accessionNumber() {
return this.accessionNumber == null ? this.accessionNumber = new StringFilter() : this.accessionNumber;
}
/**
* Accession Number filter.
*
* @return the string filter
*/
public synchronized Set<String> accessionNumbers() {
return this.accessionNumbers == null ? this.accessionNumbers = new HashSet<>() : this.accessionNumbers;
}
public synchronized Set<String> doi() {
if (doi == null) {
doi = new HashSet<>();
......
......@@ -282,7 +282,7 @@ public class AccessionControllerTest extends AbstractAccessionControllerTest {
}
@Test
public void filterByAccessionNumberTest1() throws Exception {
public void filterByAccessionNumberTestEquals() throws Exception {
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, null));
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_2, GENUS_2, null));
......@@ -316,11 +316,26 @@ public class AccessionControllerTest extends AbstractAccessionControllerTest {
.andExpect(jsonPath("$.content[0].accessionNumber", is(oneOf(ACCENUMB_1, ACCENUMB_2))))
.andExpect(jsonPath("$.content[1].accessionNumber", is(oneOf(ACCENUMB_1, ACCENUMB_2))))
;
// The new version
mockMvc
.perform(post(AccessionController.CONTROLLER_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(String.format("{\"accessionNumbers\":[\"%1s\",\"%2s\"]}", ACCENUMB_1, ACCENUMB_2)))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.totalElements", is(2)))
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content[0].accessionNumber", is(oneOf(ACCENUMB_1, ACCENUMB_2))))
.andExpect(jsonPath("$.content[1].accessionNumber", is(oneOf(ACCENUMB_1, ACCENUMB_2))))
;
/*@formatter:on*/
}
@Test
public void filterByAccessionNumberTest2() throws Exception {
public void filterByAccessionNumberTestStartsWith() throws Exception {
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, null));
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_2, GENUS_2, null));
......@@ -358,7 +373,7 @@ public class AccessionControllerTest extends AbstractAccessionControllerTest {
}
@Test
public void filterByAccessionNumberTest3() throws Exception {
public void filterByAccessionNumberTestContains() throws Exception {
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, null));
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_2, GENUS_2, null));
......
......@@ -160,7 +160,7 @@ public class AccessionControllerTest extends AbstractAccessionControllerTest {
}
@Test
public void filterByAccessionNumberTest1() throws Exception {
public void filterByAccessionNumberTestEquals() throws Exception {
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, null));
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_2, GENUS_2, null));
......@@ -194,11 +194,26 @@ public class AccessionControllerTest extends AbstractAccessionControllerTest {
.andExpect(jsonPath("$.content[0].accessionNumber", is(oneOf(ACCENUMB_1, ACCENUMB_2))))
.andExpect(jsonPath("$.content[1].accessionNumber", is(oneOf(ACCENUMB_1, ACCENUMB_2))))
;
// The new version
mockMvc
.perform(post(AccessionController.CONTROLLER_URL + "/list")
.contentType(MediaType.APPLICATION_JSON)
.content(String.format("{\"accessionNumbers\":[\"%1s\",\"%2s\"]}", ACCENUMB_1, ACCENUMB_2)))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
.andExpect(jsonPath("$", not(nullValue())))
.andExpect(jsonPath("$.totalElements", is(2)))
.andExpect(jsonPath("$.content").isArray())
.andExpect(jsonPath("$.content[0].accessionNumber", is(oneOf(ACCENUMB_1, ACCENUMB_2))))
.andExpect(jsonPath("$.content[1].accessionNumber", is(oneOf(ACCENUMB_1, ACCENUMB_2))))
;
/*@formatter:on*/
}
@Test
public void filterByAccessionNumberTest2() throws Exception {
public void filterByAccessionNumberTestStartsWith() throws Exception {
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, null));
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_2, GENUS_2, null));
......@@ -236,7 +251,7 @@ public class AccessionControllerTest extends AbstractAccessionControllerTest {
}
@Test
public void filterByAccessionNumberTest3() throws Exception {
public void filterByAccessionNumberTestContains() throws Exception {
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_1, GENUS_1, null));
accessionUploader.upsertAccessions(institute, upsertAccessions(institute.getCode(), ACCENUMB_2, GENUS_2, null));
......
......@@ -16,15 +16,14 @@
package org.genesys.test.simpletest;
import static com.google.common.collect.Sets.*;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.MatcherAssert.*;
import static org.hamcrest.Matchers.*;
import java.io.IOException;
import java.util.List;
import org.elasticsearch.index.query.QueryBuilder;
import org.genesys.blocks.model.filters.NumberFilter;
import org.genesys.blocks.model.filters.StringFilter;
import org.genesys2.server.component.elastic.ElasticQueryBuilder;
import org.genesys2.server.service.filter.AccessionFilter;
import org.genesys2.server.service.filter.AccessionGeoFilter;
......@@ -44,9 +43,7 @@ public class EsQueryTest {
public void test1() {
AccessionFilter af = new AccessionFilter();
af.active = true;
af.accessionNumber();
af.accessionNumber.eq("IRGC 1");
af.accessionNumber.sw("FOO");
af.accessionNumbers().add("IRGC 1");
af.geo = new AccessionGeoFilter();
af.geo.latitude = new NumberFilter<>();
af.geo.latitude.ge = 10d;
......
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