Search in sources :

Example 1 with SpecSearchCriteria

use of org.baeldung.web.util.SpecSearchCriteria in project tutorials by eugenp.

the class JPASpecificationIntegrationTest method givenFirstOrLastName_whenGettingListOfUsers_thenCorrect.

@Test
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
    UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
    SpecSearchCriteria spec = new SpecSearchCriteria("firstName", SearchOperation.EQUALITY, "john");
    SpecSearchCriteria spec1 = new SpecSearchCriteria("'", "lastName", SearchOperation.EQUALITY, "doe");
    List<User> results = repository.findAll(builder.with(spec).with(spec1).build());
    assertThat(results, hasSize(2));
    assertThat(userJohn, isIn(results));
    assertThat(userTom, isIn(results));
}
Also used : User(org.baeldung.persistence.model.User) UserSpecificationsBuilder(org.baeldung.persistence.dao.UserSpecificationsBuilder) SpecSearchCriteria(org.baeldung.web.util.SpecSearchCriteria) Test(org.junit.Test)

Example 2 with SpecSearchCriteria

use of org.baeldung.web.util.SpecSearchCriteria 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 3 with SpecSearchCriteria

use of org.baeldung.web.util.SpecSearchCriteria 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 4 with SpecSearchCriteria

use of org.baeldung.web.util.SpecSearchCriteria 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 5 with SpecSearchCriteria

use of org.baeldung.web.util.SpecSearchCriteria 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)

Aggregations

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