Commit 62aba74c authored by Matija Obreza's avatar Matija Obreza

Merge branch 'search-matching-descriptors-without-crop-ui-561' into 'master'

Search matching descriptors without crop

See merge request genesys-pgr/genesys-server!479
parents fc8af706 a3cc08bd
......@@ -205,7 +205,8 @@ public class DescriptorServiceImpl implements DescriptorService {
searches.add(descriptor.dataType.eq(input.getDataType()).and(descriptor.uom.eq(input.getUom())));
}
final Predicate predicate = descriptor.crop.eq(input.getCrop()).andAnyOf(searches.toArray(EMPTY_PREDICATE_ARRAY));
final Predicate predicate = StringUtils.isBlank(input.getCrop()) ? descriptor.crop.isNull().andAnyOf(searches.toArray(EMPTY_PREDICATE_ARRAY)) : descriptor.crop.eq(input.getCrop()).andAnyOf(searches.toArray(EMPTY_PREDICATE_ARRAY));
List<Descriptor> matches = new ArrayList<>();
descriptorRepository.findAll(predicate).forEach(match -> matches.add(lazyLoad(match)));
return matches;
......
......@@ -56,6 +56,7 @@ public class DescriptorServiceTest extends CatalogServiceTest {
private static final String VERSION_2_0 = "2.0";
private static final String DESCRIPTION_1 = "Some description";
private static final String DESCRIPTION_2 = "Some description 2";
private static final String CROP = "maize";
@Override
public void cleanup() throws Exception {
......@@ -452,6 +453,59 @@ public class DescriptorServiceTest extends CatalogServiceTest {
assertThat(result.getMaxValue(), is(nullValue()));
}
@Test
public void testSearchMatchingDescriptor() {
final Descriptor input = setupDescriptor(null, DESCRIPTOR_TITLE_1, Category.PASSPORT, DataType.NUMERIC, VERSION_1_0, PublishState.DRAFT);
input.setCrop(CROP);
Descriptor saved = descriptorService.createDescriptor(input);
assertThat(saved, not(nullValue()));
assertThat(saved.getId(), not(nullValue()));
assertThat(saved.getCrop(), is(input.getCrop()));
saved = descriptorService.loadDescriptor(saved.getUuid());
assertThat(saved, notNullValue());
List<Descriptor> searchResult = descriptorService.searchMatchingDescriptor(new Descriptor().apply(input));
assertFalse(searchResult.isEmpty());
assertThat(searchResult.size(), is(1));
assertEquals(searchResult.get(0).getCrop(), input.getCrop());
assertEquals(searchResult.get(0).getTitle(), DESCRIPTOR_TITLE_1);
assertEquals(searchResult.get(0).getCategory(), Category.PASSPORT);
}
@Test
public void testSearchMatchingDescriptorWithoutCrop() {
final Descriptor input = setupDescriptor(null, DESCRIPTOR_TITLE_1, Category.PASSPORT, DataType.NUMERIC, VERSION_1_0, PublishState.DRAFT);
Descriptor saved1 = descriptorService.createDescriptor(input);
assertThat(saved1, not(nullValue()));
assertThat(saved1.getId(), not(nullValue()));
assertNull(saved1.getCrop());
input.setCrop(CROP);
Descriptor saved2 = descriptorService.createDescriptor(input);
assertThat(saved2, not(nullValue()));
assertThat(saved2.getId(), not(nullValue()));
assertThat(saved2.getCrop(), is(input.getCrop()));
assertThat(descriptorRepository.count(), is(2L));
List<Descriptor> searchResult = descriptorService.searchMatchingDescriptor(input);
assertFalse(searchResult.isEmpty());
assertThat(searchResult.size(), is(1));
assertThat(searchResult.get(0).getCrop(), not(nullValue()));
assertThat(searchResult.get(0).getTitle(), is(DESCRIPTOR_TITLE_1));
assertThat(searchResult.get(0).getCategory(), is(Category.PASSPORT));
input.setCrop(null);
searchResult = descriptorService.searchMatchingDescriptor(input);
assertFalse(searchResult.isEmpty());
assertThat(searchResult.size(), is(1));
assertThat(searchResult.get(0).getCrop(), nullValue());
assertThat(searchResult.get(0).getTitle(), is(DESCRIPTOR_TITLE_1));
assertThat(searchResult.get(0).getCategory(), is(Category.PASSPORT));
}
@Test
public void createDescriptorWithDescriptorLists() {
final DescriptorList descriptorList = setupDescriptorList("DescriptorTitle 1", VERSION_1_0, DESCRIPTION_1);
......
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