Search in sources :

Example 1 with UserSpecification

use of org.baeldung.persistence.dao.UserSpecification in project tutorials by eugenp.

the class JPASpecificationIntegrationTest method givenAgeRange_whenGettingListOfUsers_thenCorrect.

@Test
public void givenAgeRange_whenGettingListOfUsers_thenCorrect() {
    final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.GREATER_THAN, "20"));
    final UserSpecification spec1 = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.LESS_THAN, "25"));
    final List<User> results = repository.findAll(Specifications.where(spec).and(spec1));
    assertThat(userJohn, isIn(results));
    assertThat(userTom, not(isIn(results)));
}
Also used : UserSpecification(org.baeldung.persistence.dao.UserSpecification) User(org.baeldung.persistence.model.User) SpecSearchCriteria(org.baeldung.web.util.SpecSearchCriteria) Test(org.junit.Test)

Example 2 with UserSpecification

use of org.baeldung.persistence.dao.UserSpecification in project tutorials by eugenp.

the class JPASpecificationIntegrationTest method givenFirstAndLastName_whenGettingListOfUsers_thenCorrect.

@Test
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
    final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.EQUALITY, "john"));
    final UserSpecification spec1 = new UserSpecification(new SpecSearchCriteria("lastName", SearchOperation.EQUALITY, "doe"));
    final List<User> results = repository.findAll(Specifications.where(spec).and(spec1));
    assertThat(userJohn, isIn(results));
    assertThat(userTom, not(isIn(results)));
}
Also used : UserSpecification(org.baeldung.persistence.dao.UserSpecification) User(org.baeldung.persistence.model.User) SpecSearchCriteria(org.baeldung.web.util.SpecSearchCriteria) Test(org.junit.Test)

Example 3 with UserSpecification

use of org.baeldung.persistence.dao.UserSpecification in project tutorials by eugenp.

the class JPASpecificationIntegrationTest method givenFirstNameInverse_whenGettingListOfUsers_thenCorrect.

@Test
public void givenFirstNameInverse_whenGettingListOfUsers_thenCorrect() {
    final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.NEGATION, "john"));
    final List<User> results = repository.findAll(Specifications.where(spec));
    assertThat(userTom, isIn(results));
    assertThat(userJohn, not(isIn(results)));
}
Also used : UserSpecification(org.baeldung.persistence.dao.UserSpecification) User(org.baeldung.persistence.model.User) SpecSearchCriteria(org.baeldung.web.util.SpecSearchCriteria) Test(org.junit.Test)

Example 4 with UserSpecification

use of org.baeldung.persistence.dao.UserSpecification in project tutorials by eugenp.

the class JPASpecificationIntegrationTest method givenFirstNameSubstring_whenGettingListOfUsers_thenCorrect.

@Test
public void givenFirstNameSubstring_whenGettingListOfUsers_thenCorrect() {
    final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.CONTAINS, "oh"));
    final List<User> results = repository.findAll(spec);
    assertThat(userJohn, isIn(results));
    assertThat(userTom, not(isIn(results)));
}
Also used : UserSpecification(org.baeldung.persistence.dao.UserSpecification) User(org.baeldung.persistence.model.User) SpecSearchCriteria(org.baeldung.web.util.SpecSearchCriteria) Test(org.junit.Test)

Example 5 with UserSpecification

use of org.baeldung.persistence.dao.UserSpecification 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)));
}
Also used : User(org.baeldung.persistence.model.User) CriteriaParser(org.baeldung.web.util.CriteriaParser) GenericSpecificationsBuilder(org.baeldung.persistence.dao.GenericSpecificationsBuilder) UserSpecification(org.baeldung.persistence.dao.UserSpecification) Specification(org.springframework.data.jpa.domain.Specification) SpecSearchCriteria(org.baeldung.web.util.SpecSearchCriteria) Test(org.junit.Test)

Aggregations

UserSpecification (org.baeldung.persistence.dao.UserSpecification)9 User (org.baeldung.persistence.model.User)9 SpecSearchCriteria (org.baeldung.web.util.SpecSearchCriteria)8 Test (org.junit.Test)8 GenericSpecificationsBuilder (org.baeldung.persistence.dao.GenericSpecificationsBuilder)2 CriteriaParser (org.baeldung.web.util.CriteriaParser)2 MyUser (org.baeldung.persistence.model.MyUser)1 Specification (org.springframework.data.jpa.domain.Specification)1