use of org.baeldung.web.util.SpecSearchCriteria in project tutorials by eugenp.
the class JPASpecificationIntegrationTest method givenFirstOrLastNameAndAgeGenericBuilder_whenGettingListOfUsers_thenCorrect.
@Test
public void givenFirstOrLastNameAndAgeGenericBuilder_whenGettingListOfUsers_thenCorrect() {
GenericSpecificationsBuilder<User> builder = new GenericSpecificationsBuilder<>();
Function<SpecSearchCriteria, Specification<User>> converter = UserSpecification::new;
CriteriaParser parser = new CriteriaParser();
List<User> results = repository.findAll(builder.build(parser.parse("( lastName:doe OR firstName:john ) AND age:22"), converter));
assertThat(results, hasSize(1));
assertThat(userJohn, isIn(results));
assertThat(userTom, not(isIn(results)));
}
use of org.baeldung.web.util.SpecSearchCriteria in project tutorials by eugenp.
the class JPASpecificationIntegrationTest method givenMinAge_whenGettingListOfUsers_thenCorrect.
@Test
public void givenMinAge_whenGettingListOfUsers_thenCorrect() {
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.GREATER_THAN, "25"));
final List<User> results = repository.findAll(Specifications.where(spec));
assertThat(userTom, isIn(results));
assertThat(userJohn, not(isIn(results)));
}
use of org.baeldung.web.util.SpecSearchCriteria in project tutorials by eugenp.
the class JPASpecificationIntegrationTest method givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect.
@Test
public void givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect() {
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.ENDS_WITH, "n"));
final List<User> results = repository.findAll(spec);
assertThat(userJohn, isIn(results));
assertThat(userTom, not(isIn(results)));
}
use of org.baeldung.web.util.SpecSearchCriteria in project tutorials by eugenp.
the class JPASpecificationIntegrationTest method givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect.
@Test
public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() {
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.STARTS_WITH, "jo"));
final List<User> results = repository.findAll(spec);
assertThat(userJohn, isIn(results));
assertThat(userTom, not(isIn(results)));
}
use of org.baeldung.web.util.SpecSearchCriteria in project tutorials by eugenp.
the class GenericSpecificationsBuilder method with.
public final GenericSpecificationsBuilder<U> with(final String precedenceIndicator, final String key, final String operation, final Object value, final String prefix, final String suffix) {
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
if (op != null) {
if (// the operation may be complex operation
op == SearchOperation.EQUALITY) {
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
if (startWithAsterisk && endWithAsterisk) {
op = SearchOperation.CONTAINS;
} else if (startWithAsterisk) {
op = SearchOperation.ENDS_WITH;
} else if (endWithAsterisk) {
op = SearchOperation.STARTS_WITH;
}
}
params.add(new SpecSearchCriteria(precedenceIndicator, key, op, value));
}
return this;
}
Aggregations