move query language to new module (#2864)
* move security content from spring-security-rest-full * swagger update * move query language to new module * rename spring-security-rest-full to spring-rest-full
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
864c2e2190
commit
1c9477390d
+108
@@ -0,0 +1,108 @@
|
||||
package org.baeldung.persistence.query;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.collection.IsIn.isIn;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.dao.IUserDAO;
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.baeldung.web.util.SearchCriteria;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfig.class })
|
||||
@Transactional
|
||||
@Rollback
|
||||
public class JPACriteriaQueryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private IUserDAO userApi;
|
||||
|
||||
private User userJohn;
|
||||
|
||||
private User userTom;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
userJohn = new User();
|
||||
userJohn.setFirstName("john");
|
||||
userJohn.setLastName("doe");
|
||||
userJohn.setEmail("john@doe.com");
|
||||
userJohn.setAge(22);
|
||||
userApi.save(userJohn);
|
||||
|
||||
userTom = new User();
|
||||
userTom.setFirstName("tom");
|
||||
userTom.setLastName("doe");
|
||||
userTom.setEmail("tom@doe.com");
|
||||
userTom.setAge(26);
|
||||
userApi.save(userTom);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("firstName", ":", "john"));
|
||||
params.add(new SearchCriteria("lastName", ":", "doe"));
|
||||
|
||||
final List<User> results = userApi.searchUser(params);
|
||||
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLast_whenGettingListOfUsers_thenCorrect() {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("lastName", ":", "doe"));
|
||||
|
||||
final List<User> results = userApi.searchUser(params);
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, isIn(results));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("lastName", ":", "doe"));
|
||||
params.add(new SearchCriteria("age", ">", "25"));
|
||||
|
||||
final List<User> results = userApi.searchUser(params);
|
||||
|
||||
assertThat(userTom, isIn(results));
|
||||
assertThat(userJohn, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("firstName", ":", "adam"));
|
||||
params.add(new SearchCriteria("lastName", ":", "fox"));
|
||||
|
||||
final List<User> results = userApi.searchUser(params);
|
||||
assertThat(userJohn, not(isIn(results)));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() {
|
||||
final List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
params.add(new SearchCriteria("firstName", ":", "jo"));
|
||||
|
||||
final List<User> results = userApi.searchUser(params);
|
||||
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
}
|
||||
+97
@@ -0,0 +1,97 @@
|
||||
package org.baeldung.persistence.query;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.collection.IsEmptyIterable.emptyIterable;
|
||||
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
|
||||
import static org.hamcrest.collection.IsIterableContainingInOrder.contains;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
|
||||
import org.baeldung.persistence.dao.MyUserPredicatesBuilder;
|
||||
import org.baeldung.persistence.dao.MyUserRepository;
|
||||
import org.baeldung.persistence.model.MyUser;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfig.class })
|
||||
@Transactional
|
||||
@Rollback
|
||||
public class JPAQuerydslIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private MyUserRepository repo;
|
||||
|
||||
private MyUser userJohn;
|
||||
|
||||
private MyUser userTom;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
userJohn = new MyUser();
|
||||
userJohn.setFirstName("john");
|
||||
userJohn.setLastName("doe");
|
||||
userJohn.setEmail("john@doe.com");
|
||||
userJohn.setAge(22);
|
||||
repo.save(userJohn);
|
||||
|
||||
userTom = new MyUser();
|
||||
userTom.setFirstName("tom");
|
||||
userTom.setLastName("doe");
|
||||
userTom.setEmail("tom@doe.com");
|
||||
userTom.setAge(26);
|
||||
repo.save(userTom);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLast_whenGettingListOfUsers_thenCorrect() {
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "doe");
|
||||
|
||||
final Iterable<MyUser> results = repo.findAll(builder.build());
|
||||
assertThat(results, containsInAnyOrder(userJohn, userTom));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "john").with("lastName", ":", "doe");
|
||||
|
||||
final Iterable<MyUser> results = repo.findAll(builder.build());
|
||||
|
||||
assertThat(results, contains(userJohn));
|
||||
assertThat(results, not(contains(userTom)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenLastAndAge_whenGettingListOfUsers_thenCorrect() {
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("lastName", ":", "doe").with("age", ">", "25");
|
||||
|
||||
final Iterable<MyUser> results = repo.findAll(builder.build());
|
||||
|
||||
assertThat(results, contains(userTom));
|
||||
assertThat(results, not(contains(userJohn)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenWrongFirstAndLast_whenGettingListOfUsers_thenCorrect() {
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "adam").with("lastName", ":", "fox");
|
||||
|
||||
final Iterable<MyUser> results = repo.findAll(builder.build());
|
||||
assertThat(results, emptyIterable());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPartialFirst_whenGettingListOfUsers_thenCorrect() {
|
||||
final MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder().with("firstName", ":", "jo");
|
||||
|
||||
final Iterable<MyUser> results = repo.findAll(builder.build());
|
||||
|
||||
assertThat(results, contains(userJohn));
|
||||
assertThat(results, not(contains(userTom)));
|
||||
}
|
||||
}
|
||||
+180
@@ -0,0 +1,180 @@
|
||||
package org.baeldung.persistence.query;
|
||||
|
||||
import org.baeldung.persistence.dao.GenericSpecificationsBuilder;
|
||||
import org.baeldung.persistence.dao.UserRepository;
|
||||
import org.baeldung.persistence.dao.UserSpecification;
|
||||
import org.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.baeldung.web.util.CriteriaParser;
|
||||
import org.baeldung.web.util.SearchOperation;
|
||||
import org.baeldung.web.util.SpecSearchCriteria;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.jpa.domain.Specifications;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
|
||||
import static org.hamcrest.collection.IsIn.isIn;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfig.class })
|
||||
@Transactional
|
||||
@Rollback
|
||||
public class JPASpecificationIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository repository;
|
||||
|
||||
private User userJohn;
|
||||
|
||||
private User userTom;
|
||||
|
||||
private User userPercy;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
userJohn = new User();
|
||||
userJohn.setFirstName("john");
|
||||
userJohn.setLastName("doe");
|
||||
userJohn.setEmail("john@doe.com");
|
||||
userJohn.setAge(22);
|
||||
repository.save(userJohn);
|
||||
|
||||
userTom = new User();
|
||||
userTom.setFirstName("tom");
|
||||
userTom.setLastName("doe");
|
||||
userTom.setEmail("tom@doe.com");
|
||||
userTom.setAge(26);
|
||||
repository.save(userTom);
|
||||
|
||||
userPercy = new User();
|
||||
userPercy.setFirstName("percy");
|
||||
userPercy.setLastName("blackney");
|
||||
userPercy.setEmail("percy@blackney.com");
|
||||
userPercy.setAge(30);
|
||||
repository.save(userPercy);
|
||||
}
|
||||
|
||||
@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)));
|
||||
}
|
||||
|
||||
@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));
|
||||
}
|
||||
|
||||
@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)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstOrLastNameGenericBuilder_whenGettingListOfUsers_thenCorrect() {
|
||||
GenericSpecificationsBuilder<User> builder = new GenericSpecificationsBuilder<>();
|
||||
Function<SpecSearchCriteria, Specification<User>> converter = UserSpecification::new;
|
||||
|
||||
builder.with("firstName", ":", "john", null, null);
|
||||
builder.with("'", "lastName", ":", "doe", null, null);
|
||||
|
||||
List<User> results = repository.findAll(builder.build(converter));
|
||||
|
||||
assertThat(results, hasSize(2));
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, isIn(results));
|
||||
}
|
||||
|
||||
@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)));
|
||||
}
|
||||
|
||||
@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)));
|
||||
}
|
||||
|
||||
@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)));
|
||||
}
|
||||
|
||||
@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)));
|
||||
}
|
||||
|
||||
@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)));
|
||||
}
|
||||
|
||||
@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)));
|
||||
}
|
||||
}
|
||||
+146
@@ -0,0 +1,146 @@
|
||||
package org.baeldung.persistence.query;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
//@RunWith(SpringJUnit4ClassRunner.class)
|
||||
//@ContextConfiguration(classes = { ConfigTest.class,
|
||||
// PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||
@ActiveProfiles("test")
|
||||
public class JPASpecificationLiveTest {
|
||||
|
||||
// @Autowired
|
||||
// private UserRepository repository;
|
||||
|
||||
private User userJohn;
|
||||
|
||||
private User userTom;
|
||||
|
||||
private final String URL_PREFIX = "http://localhost:8082/spring-rest-query-language/auth/users/spec?search=";
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
userJohn = new User();
|
||||
userJohn.setFirstName("john");
|
||||
userJohn.setLastName("doe");
|
||||
userJohn.setEmail("john@doe.com");
|
||||
userJohn.setAge(22);
|
||||
// repository.save(userJohn);
|
||||
|
||||
userTom = new User();
|
||||
userTom.setFirstName("tom");
|
||||
userTom.setLastName("doe");
|
||||
userTom.setEmail("tom@doe.com");
|
||||
userTom.setAge(26);
|
||||
// repository.save(userTom);
|
||||
}
|
||||
|
||||
private final String EURL_PREFIX = "http://localhost:8082/spring-rest-query-language/auth/users/espec?search=";
|
||||
|
||||
@Test
|
||||
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
|
||||
final Response response = RestAssured.get(EURL_PREFIX + "firstName:john,'lastName:doe");
|
||||
final String result = response.body()
|
||||
.asString();
|
||||
assertTrue(result.contains(userJohn.getEmail()));
|
||||
assertTrue(result.contains(userTom.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||
final Response response = RestAssured.get(URL_PREFIX + "firstName:john,lastName:doe");
|
||||
final String result = response.body()
|
||||
.asString();
|
||||
|
||||
assertTrue(result.contains(userJohn.getEmail()));
|
||||
assertFalse(result.contains(userTom.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstNameInverse_whenGettingListOfUsers_thenCorrect() {
|
||||
final Response response = RestAssured.get(URL_PREFIX + "firstName!john");
|
||||
final String result = response.body()
|
||||
.asString();
|
||||
|
||||
assertTrue(result.contains(userTom.getEmail()));
|
||||
assertFalse(result.contains(userJohn.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMinAge_whenGettingListOfUsers_thenCorrect() {
|
||||
final Response response = RestAssured.get(URL_PREFIX + "age>25");
|
||||
final String result = response.body()
|
||||
.asString();
|
||||
|
||||
assertTrue(result.contains(userTom.getEmail()));
|
||||
assertFalse(result.contains(userJohn.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() {
|
||||
final Response response = RestAssured.get(URL_PREFIX + "firstName:jo*");
|
||||
final String result = response.body()
|
||||
.asString();
|
||||
|
||||
assertTrue(result.contains(userJohn.getEmail()));
|
||||
assertFalse(result.contains(userTom.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect() {
|
||||
final Response response = RestAssured.get(URL_PREFIX + "firstName:*n");
|
||||
final String result = response.body()
|
||||
.asString();
|
||||
|
||||
assertTrue(result.contains(userJohn.getEmail()));
|
||||
assertFalse(result.contains(userTom.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstNameSubstring_whenGettingListOfUsers_thenCorrect() {
|
||||
final Response response = RestAssured.get(URL_PREFIX + "firstName:*oh*");
|
||||
final String result = response.body()
|
||||
.asString();
|
||||
|
||||
assertTrue(result.contains(userJohn.getEmail()));
|
||||
assertFalse(result.contains(userTom.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAgeRange_whenGettingListOfUsers_thenCorrect() {
|
||||
final Response response = RestAssured.get(URL_PREFIX + "age>20,age<25");
|
||||
final String result = response.body()
|
||||
.asString();
|
||||
|
||||
assertTrue(result.contains(userJohn.getEmail()));
|
||||
assertFalse(result.contains(userTom.getEmail()));
|
||||
}
|
||||
|
||||
private final String ADV_URL_PREFIX = "http://localhost:8082/spring-rest-query-language/auth/users/spec/adv?search=";
|
||||
|
||||
@Test
|
||||
public void givenFirstOrLastName_whenGettingAdvListOfUsers_thenCorrect() {
|
||||
final Response response = RestAssured.get(ADV_URL_PREFIX + "firstName:john OR lastName:doe");
|
||||
final String result = response.body()
|
||||
.asString();
|
||||
assertTrue(result.contains(userJohn.getEmail()));
|
||||
assertTrue(result.contains(userTom.getEmail()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstOrFirstNameAndAge_whenGettingAdvListOfUsers_thenCorrect() {
|
||||
final Response response = RestAssured.get(ADV_URL_PREFIX + "( firstName:john OR firstName:tom ) AND age>22");
|
||||
final String result = response.body()
|
||||
.asString();
|
||||
assertFalse(result.contains(userJohn.getEmail()));
|
||||
assertTrue(result.contains(userTom.getEmail()));
|
||||
}
|
||||
|
||||
}
|
||||
+105
@@ -0,0 +1,105 @@
|
||||
package org.baeldung.persistence.query;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.collection.IsIn.isIn;
|
||||
import static org.hamcrest.core.IsNot.not;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.baeldung.persistence.dao.UserRepository;
|
||||
import org.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
|
||||
import org.baeldung.persistence.model.User;
|
||||
import org.baeldung.spring.PersistenceConfig;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.test.annotation.Rollback;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import cz.jirutka.rsql.parser.RSQLParser;
|
||||
import cz.jirutka.rsql.parser.ast.Node;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes = { PersistenceConfig.class })
|
||||
@Transactional
|
||||
@Rollback
|
||||
public class RsqlIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private UserRepository repository;
|
||||
|
||||
private User userJohn;
|
||||
|
||||
private User userTom;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
userJohn = new User();
|
||||
userJohn.setFirstName("john");
|
||||
userJohn.setLastName("doe");
|
||||
userJohn.setEmail("john@doe.com");
|
||||
userJohn.setAge(22);
|
||||
repository.save(userJohn);
|
||||
|
||||
userTom = new User();
|
||||
userTom.setFirstName("tom");
|
||||
userTom.setLastName("doe");
|
||||
userTom.setEmail("tom@doe.com");
|
||||
userTom.setAge(26);
|
||||
repository.save(userTom);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||
final Node rootNode = new RSQLParser().parse("firstName==john;lastName==doe");
|
||||
final Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||
final List<User> results = repository.findAll(spec);
|
||||
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstNameInverse_whenGettingListOfUsers_thenCorrect() {
|
||||
final Node rootNode = new RSQLParser().parse("firstName!=john");
|
||||
final Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||
final List<User> results = repository.findAll(spec);
|
||||
|
||||
assertThat(userTom, isIn(results));
|
||||
assertThat(userJohn, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenMinAge_whenGettingListOfUsers_thenCorrect() {
|
||||
final Node rootNode = new RSQLParser().parse("age>25");
|
||||
final Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||
final List<User> results = repository.findAll(spec);
|
||||
|
||||
assertThat(userTom, isIn(results));
|
||||
assertThat(userJohn, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() {
|
||||
final Node rootNode = new RSQLParser().parse("firstName==jo*");
|
||||
final Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||
final List<User> results = repository.findAll(spec);
|
||||
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenListOfFirstName_whenGettingListOfUsers_thenCorrect() {
|
||||
final Node rootNode = new RSQLParser().parse("firstName=in=(john,jack)");
|
||||
final Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||
final List<User> results = repository.findAll(spec);
|
||||
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package org.baeldung.web;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
import io.restassured.specification.RequestSpecification;
|
||||
|
||||
import org.baeldung.persistence.model.MyUser;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
@ActiveProfiles("test")
|
||||
public class MyUserLiveTest {
|
||||
|
||||
private final MyUser userJohn = new MyUser("john", "doe", "john@doe.com", 11);
|
||||
private String URL_PREFIX = "http://localhost:8082/spring-rest-query-language/auth/api/myusers";
|
||||
|
||||
@Test
|
||||
public void whenGettingListOfUsers_thenCorrect() {
|
||||
final Response response = givenAuth().get(URL_PREFIX);
|
||||
final MyUser[] result = response.as(MyUser[].class);
|
||||
assertEquals(result.length, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenFirstName_whenGettingListOfUsers_thenCorrect() {
|
||||
final Response response = givenAuth().get(URL_PREFIX + "?firstName=john");
|
||||
final MyUser[] result = response.as(MyUser[].class);
|
||||
assertEquals(result.length, 1);
|
||||
assertEquals(result[0].getEmail(), userJohn.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenPartialLastName_whenGettingListOfUsers_thenCorrect() {
|
||||
final Response response = givenAuth().get(URL_PREFIX + "?lastName=do");
|
||||
final MyUser[] result = response.as(MyUser[].class);
|
||||
assertEquals(result.length, 2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenEmail_whenGettingListOfUsers_thenIgnored() {
|
||||
final Response response = givenAuth().get(URL_PREFIX + "?email=john");
|
||||
final MyUser[] result = response.as(MyUser[].class);
|
||||
assertEquals(result.length, 2);
|
||||
}
|
||||
|
||||
private RequestSpecification givenAuth() {
|
||||
return RestAssured.given().auth().preemptive().basic("user1", "user1Pass");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user