Merge pull request #10369 from Maiklins/JAVA-3535-move-spring-rest-query-language
Java 3535 move spring rest query language
This commit is contained in:
commit
8bf42fafc7
2
pom.xml
2
pom.xml
|
@ -672,7 +672,6 @@
|
|||
<module>spring-reactor</module>
|
||||
<module>spring-remoting</module>
|
||||
<module>spring-rest-http-2</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-rest-shell</module>
|
||||
<module>spring-rest-simple</module>
|
||||
<module>spring-resttemplate</module>
|
||||
|
@ -1137,7 +1136,6 @@
|
|||
|
||||
<module>spring-reactor</module>
|
||||
<module>spring-remoting</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-rest-shell</module>
|
||||
<module>spring-rest-simple</module>
|
||||
<module>spring-resttemplate</module>
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
<module>spring-mvc-webflow</module>
|
||||
<module>spring-rest-angular</module>
|
||||
<module>spring-rest-http</module>
|
||||
<module>spring-rest-query-language</module>
|
||||
<module>spring-resttemplate-2</module>
|
||||
<module>spring-thymeleaf</module>
|
||||
<module>spring-thymeleaf-2</module>
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../parent-boot-2</relativePath>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
|
@ -1,100 +1,100 @@
|
|||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.baeldung.web.util.SpecSearchCriteria;
|
||||
|
||||
public class GenericSpecificationsBuilder<U> {
|
||||
|
||||
private final List<SpecSearchCriteria> params;
|
||||
|
||||
public GenericSpecificationsBuilder() {
|
||||
this.params = new ArrayList<>();
|
||||
}
|
||||
|
||||
public final GenericSpecificationsBuilder<U> with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
return with(null, key, operation, value, prefix, suffix);
|
||||
}
|
||||
|
||||
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 (op == SearchOperation.EQUALITY) // the operation may be complex operation
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public Specification<U> build(Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||
|
||||
if (params.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final List<Specification<U>> specs = params.stream()
|
||||
.map(converter)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
|
||||
Specification<U> result = specs.get(0);
|
||||
|
||||
for (int idx = 1; idx < specs.size(); idx++) {
|
||||
result = params.get(idx)
|
||||
.isOrPredicate()
|
||||
? Specification.where(result)
|
||||
.or(specs.get(idx))
|
||||
: Specification.where(result)
|
||||
.and(specs.get(idx));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Specification<U> build(Deque<?> postFixedExprStack, Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||
|
||||
Deque<Specification<U>> specStack = new LinkedList<>();
|
||||
|
||||
Collections.reverse((List<?>) postFixedExprStack);
|
||||
|
||||
while (!postFixedExprStack.isEmpty()) {
|
||||
Object mayBeOperand = postFixedExprStack.pop();
|
||||
|
||||
if (!(mayBeOperand instanceof String)) {
|
||||
specStack.push(converter.apply((SpecSearchCriteria) mayBeOperand));
|
||||
} else {
|
||||
Specification<U> operand1 = specStack.pop();
|
||||
Specification<U> operand2 = specStack.pop();
|
||||
if (mayBeOperand.equals(SearchOperation.AND_OPERATOR))
|
||||
specStack.push(Specification.where(operand1)
|
||||
.and(operand2));
|
||||
else if (mayBeOperand.equals(SearchOperation.OR_OPERATOR))
|
||||
specStack.push(Specification.where(operand1)
|
||||
.or(operand2));
|
||||
}
|
||||
|
||||
}
|
||||
return specStack.pop();
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.Deque;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.baeldung.web.util.SpecSearchCriteria;
|
||||
|
||||
public class GenericSpecificationsBuilder<U> {
|
||||
|
||||
private final List<SpecSearchCriteria> params;
|
||||
|
||||
public GenericSpecificationsBuilder() {
|
||||
this.params = new ArrayList<>();
|
||||
}
|
||||
|
||||
public final GenericSpecificationsBuilder<U> with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
return with(null, key, operation, value, prefix, suffix);
|
||||
}
|
||||
|
||||
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 (op == SearchOperation.EQUALITY) // the operation may be complex operation
|
||||
{
|
||||
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;
|
||||
}
|
||||
|
||||
public Specification<U> build(Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||
|
||||
if (params.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
final List<Specification<U>> specs = params.stream()
|
||||
.map(converter)
|
||||
.collect(Collectors.toCollection(ArrayList::new));
|
||||
|
||||
Specification<U> result = specs.get(0);
|
||||
|
||||
for (int idx = 1; idx < specs.size(); idx++) {
|
||||
result = params.get(idx)
|
||||
.isOrPredicate()
|
||||
? Specification.where(result)
|
||||
.or(specs.get(idx))
|
||||
: Specification.where(result)
|
||||
.and(specs.get(idx));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public Specification<U> build(Deque<?> postFixedExprStack, Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||
|
||||
Deque<Specification<U>> specStack = new LinkedList<>();
|
||||
|
||||
Collections.reverse((List<?>) postFixedExprStack);
|
||||
|
||||
while (!postFixedExprStack.isEmpty()) {
|
||||
Object mayBeOperand = postFixedExprStack.pop();
|
||||
|
||||
if (!(mayBeOperand instanceof String)) {
|
||||
specStack.push(converter.apply((SpecSearchCriteria) mayBeOperand));
|
||||
} else {
|
||||
Specification<U> operand1 = specStack.pop();
|
||||
Specification<U> operand2 = specStack.pop();
|
||||
if (mayBeOperand.equals(SearchOperation.AND_OPERATOR))
|
||||
specStack.push(Specification.where(operand1)
|
||||
.and(operand2));
|
||||
else if (mayBeOperand.equals(SearchOperation.OR_OPERATOR))
|
||||
specStack.push(Specification.where(operand1)
|
||||
.or(operand2));
|
||||
}
|
||||
|
||||
}
|
||||
return specStack.pop();
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,70 +1,70 @@
|
|||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import com.baeldung.persistence.model.User;
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.baeldung.web.util.SpecSearchCriteria;
|
||||
|
||||
public final class UserSpecificationsBuilder {
|
||||
|
||||
private final List<SpecSearchCriteria> params;
|
||||
|
||||
public UserSpecificationsBuilder() {
|
||||
params = new ArrayList<>();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public final UserSpecificationsBuilder with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
return with(null, key, operation, value, prefix, suffix);
|
||||
}
|
||||
|
||||
public final UserSpecificationsBuilder with(final String orPredicate, 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 (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
||||
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(orPredicate, key, op, value));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Specification<User> build() {
|
||||
if (params.size() == 0)
|
||||
return null;
|
||||
|
||||
Specification<User> result = new UserSpecification(params.get(0));
|
||||
|
||||
for (int i = 1; i < params.size(); i++) {
|
||||
result = params.get(i).isOrPredicate()
|
||||
? Specification.where(result).or(new UserSpecification(params.get(i)))
|
||||
: Specification.where(result).and(new UserSpecification(params.get(i)));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public final UserSpecificationsBuilder with(UserSpecification spec) {
|
||||
params.add(spec.getCriteria());
|
||||
return this;
|
||||
}
|
||||
|
||||
public final UserSpecificationsBuilder with(SpecSearchCriteria criteria) {
|
||||
params.add(criteria);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
package com.baeldung.persistence.dao;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
|
||||
import com.baeldung.persistence.model.User;
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.baeldung.web.util.SpecSearchCriteria;
|
||||
|
||||
public final class UserSpecificationsBuilder {
|
||||
|
||||
private final List<SpecSearchCriteria> params;
|
||||
|
||||
public UserSpecificationsBuilder() {
|
||||
params = new ArrayList<>();
|
||||
}
|
||||
|
||||
// API
|
||||
|
||||
public final UserSpecificationsBuilder with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
||||
return with(null, key, operation, value, prefix, suffix);
|
||||
}
|
||||
|
||||
public final UserSpecificationsBuilder with(final String orPredicate, 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 (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
||||
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(orPredicate, key, op, value));
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Specification<User> build() {
|
||||
if (params.size() == 0)
|
||||
return null;
|
||||
|
||||
Specification<User> result = new UserSpecification(params.get(0));
|
||||
|
||||
for (int i = 1; i < params.size(); i++) {
|
||||
result = params.get(i).isOrPredicate()
|
||||
? Specification.where(result).or(new UserSpecification(params.get(i)))
|
||||
: Specification.where(result).and(new UserSpecification(params.get(i)));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
public final UserSpecificationsBuilder with(UserSpecification spec) {
|
||||
params.add(spec.getCriteria());
|
||||
return this;
|
||||
}
|
||||
|
||||
public final UserSpecificationsBuilder with(SpecSearchCriteria criteria) {
|
||||
params.add(criteria);
|
||||
return this;
|
||||
}
|
||||
}
|
|
@ -1,171 +1,171 @@
|
|||
package com.baeldung.web.controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import com.baeldung.persistence.dao.GenericSpecificationsBuilder;
|
||||
import com.baeldung.persistence.dao.IUserDAO;
|
||||
import com.baeldung.persistence.dao.MyUserPredicatesBuilder;
|
||||
import com.baeldung.persistence.dao.MyUserRepository;
|
||||
import com.baeldung.persistence.dao.UserRepository;
|
||||
import com.baeldung.persistence.dao.UserSpecification;
|
||||
import com.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||
import com.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
|
||||
import com.baeldung.persistence.model.MyUser;
|
||||
import com.baeldung.persistence.model.User;
|
||||
import com.baeldung.web.util.CriteriaParser;
|
||||
import com.baeldung.web.util.SearchCriteria;
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||
|
||||
import cz.jirutka.rsql.parser.RSQLParser;
|
||||
import cz.jirutka.rsql.parser.ast.Node;
|
||||
|
||||
//@EnableSpringDataWebSupport
|
||||
@Controller
|
||||
@RequestMapping(value = "/auth/")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private IUserDAO service;
|
||||
|
||||
@Autowired
|
||||
private UserRepository dao;
|
||||
|
||||
@Autowired
|
||||
private MyUserRepository myUserRepository;
|
||||
|
||||
public UserController() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API - READ
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users")
|
||||
@ResponseBody
|
||||
public List<User> search(@RequestParam(value = "search", required = false) String search) {
|
||||
List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
if (search != null) {
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
|
||||
}
|
||||
}
|
||||
return service.searchUser(params);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
|
||||
@ResponseBody
|
||||
public List<User> findAllBySpecification(@RequestParam(value = "search") String search) {
|
||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
String operationSetExper = Joiner.on("|")
|
||||
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(4), matcher.group(3), matcher.group(5));
|
||||
}
|
||||
|
||||
Specification<User> spec = builder.build();
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/users/espec")
|
||||
@ResponseBody
|
||||
public List<User> findAllByOrPredicate(@RequestParam(value = "search") String search) {
|
||||
Specification<User> spec = resolveSpecification(search);
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/users/spec/adv")
|
||||
@ResponseBody
|
||||
public List<User> findAllByAdvPredicate(@RequestParam(value = "search") String search) {
|
||||
Specification<User> spec = resolveSpecificationFromInfixExpr(search);
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
protected Specification<User> resolveSpecificationFromInfixExpr(String searchParameters) {
|
||||
CriteriaParser parser = new CriteriaParser();
|
||||
GenericSpecificationsBuilder<User> specBuilder = new GenericSpecificationsBuilder<>();
|
||||
return specBuilder.build(parser.parse(searchParameters), UserSpecification::new);
|
||||
}
|
||||
|
||||
protected Specification<User> resolveSpecification(String searchParameters) {
|
||||
|
||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
String operationSetExper = Joiner.on("|")
|
||||
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||
Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||
Matcher matcher = pattern.matcher(searchParameters + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(5), matcher.group(4), matcher.group(6));
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
||||
@ResponseBody
|
||||
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") String search) {
|
||||
MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
||||
if (search != null) {
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
||||
}
|
||||
}
|
||||
BooleanExpression exp = builder.build();
|
||||
return myUserRepository.findAll(exp);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/rsql")
|
||||
@ResponseBody
|
||||
public List<User> findAllByRsql(@RequestParam(value = "search") String search) {
|
||||
Node rootNode = new RSQLParser().parse(search);
|
||||
Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
|
||||
@ResponseBody
|
||||
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) Predicate predicate) {
|
||||
return myUserRepository.findAll(predicate);
|
||||
}
|
||||
|
||||
// API - WRITE
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/users")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void create(@RequestBody User resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
dao.save(resource);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/myusers")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void addMyUser(@RequestBody MyUser resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
myUserRepository.save(resource);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.web.controller;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMethod;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
|
||||
import com.baeldung.persistence.dao.GenericSpecificationsBuilder;
|
||||
import com.baeldung.persistence.dao.IUserDAO;
|
||||
import com.baeldung.persistence.dao.MyUserPredicatesBuilder;
|
||||
import com.baeldung.persistence.dao.MyUserRepository;
|
||||
import com.baeldung.persistence.dao.UserRepository;
|
||||
import com.baeldung.persistence.dao.UserSpecification;
|
||||
import com.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||
import com.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
|
||||
import com.baeldung.persistence.model.MyUser;
|
||||
import com.baeldung.persistence.model.User;
|
||||
import com.baeldung.web.util.CriteriaParser;
|
||||
import com.baeldung.web.util.SearchCriteria;
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.google.common.base.Joiner;
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.querydsl.core.types.Predicate;
|
||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||
|
||||
import cz.jirutka.rsql.parser.RSQLParser;
|
||||
import cz.jirutka.rsql.parser.ast.Node;
|
||||
|
||||
//@EnableSpringDataWebSupport
|
||||
@Controller
|
||||
@RequestMapping(value = "/auth/")
|
||||
public class UserController {
|
||||
|
||||
@Autowired
|
||||
private IUserDAO service;
|
||||
|
||||
@Autowired
|
||||
private UserRepository dao;
|
||||
|
||||
@Autowired
|
||||
private MyUserRepository myUserRepository;
|
||||
|
||||
public UserController() {
|
||||
super();
|
||||
}
|
||||
|
||||
// API - READ
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users")
|
||||
@ResponseBody
|
||||
public List<User> search(@RequestParam(value = "search", required = false) String search) {
|
||||
List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||
if (search != null) {
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
|
||||
}
|
||||
}
|
||||
return service.searchUser(params);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
|
||||
@ResponseBody
|
||||
public List<User> findAllBySpecification(@RequestParam(value = "search") String search) {
|
||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
String operationSetExper = Joiner.on("|")
|
||||
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(4), matcher.group(3), matcher.group(5));
|
||||
}
|
||||
|
||||
Specification<User> spec = builder.build();
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/users/espec")
|
||||
@ResponseBody
|
||||
public List<User> findAllByOrPredicate(@RequestParam(value = "search") String search) {
|
||||
Specification<User> spec = resolveSpecification(search);
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@GetMapping(value = "/users/spec/adv")
|
||||
@ResponseBody
|
||||
public List<User> findAllByAdvPredicate(@RequestParam(value = "search") String search) {
|
||||
Specification<User> spec = resolveSpecificationFromInfixExpr(search);
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
protected Specification<User> resolveSpecificationFromInfixExpr(String searchParameters) {
|
||||
CriteriaParser parser = new CriteriaParser();
|
||||
GenericSpecificationsBuilder<User> specBuilder = new GenericSpecificationsBuilder<>();
|
||||
return specBuilder.build(parser.parse(searchParameters), UserSpecification::new);
|
||||
}
|
||||
|
||||
protected Specification<User> resolveSpecification(String searchParameters) {
|
||||
|
||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||
String operationSetExper = Joiner.on("|")
|
||||
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||
Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||
Matcher matcher = pattern.matcher(searchParameters + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(5), matcher.group(4), matcher.group(6));
|
||||
}
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
||||
@ResponseBody
|
||||
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") String search) {
|
||||
MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
||||
if (search != null) {
|
||||
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||
Matcher matcher = pattern.matcher(search + ",");
|
||||
while (matcher.find()) {
|
||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
||||
}
|
||||
}
|
||||
BooleanExpression exp = builder.build();
|
||||
return myUserRepository.findAll(exp);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/users/rsql")
|
||||
@ResponseBody
|
||||
public List<User> findAllByRsql(@RequestParam(value = "search") String search) {
|
||||
Node rootNode = new RSQLParser().parse(search);
|
||||
Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||
return dao.findAll(spec);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
|
||||
@ResponseBody
|
||||
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) Predicate predicate) {
|
||||
return myUserRepository.findAll(predicate);
|
||||
}
|
||||
|
||||
// API - WRITE
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/users")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void create(@RequestBody User resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
dao.save(resource);
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST, value = "/myusers")
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public void addMyUser(@RequestBody MyUser resource) {
|
||||
Preconditions.checkNotNull(resource);
|
||||
myUserRepository.save(resource);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,36 +1,36 @@
|
|||
package com.baeldung.web.util;
|
||||
|
||||
public enum SearchOperation {
|
||||
EQUALITY, NEGATION, GREATER_THAN, LESS_THAN, LIKE, STARTS_WITH, ENDS_WITH, CONTAINS;
|
||||
|
||||
public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" };
|
||||
|
||||
public static final String OR_PREDICATE_FLAG = "'";
|
||||
|
||||
public static final String ZERO_OR_MORE_REGEX = "*";
|
||||
|
||||
public static final String OR_OPERATOR = "OR";
|
||||
|
||||
public static final String AND_OPERATOR = "AND";
|
||||
|
||||
public static final String LEFT_PARANTHESIS = "(";
|
||||
|
||||
public static final String RIGHT_PARANTHESIS = ")";
|
||||
|
||||
public static SearchOperation getSimpleOperation(final char input) {
|
||||
switch (input) {
|
||||
case ':':
|
||||
return EQUALITY;
|
||||
case '!':
|
||||
return NEGATION;
|
||||
case '>':
|
||||
return GREATER_THAN;
|
||||
case '<':
|
||||
return LESS_THAN;
|
||||
case '~':
|
||||
return LIKE;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
package com.baeldung.web.util;
|
||||
|
||||
public enum SearchOperation {
|
||||
EQUALITY, NEGATION, GREATER_THAN, LESS_THAN, LIKE, STARTS_WITH, ENDS_WITH, CONTAINS;
|
||||
|
||||
public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" };
|
||||
|
||||
public static final String OR_PREDICATE_FLAG = "'";
|
||||
|
||||
public static final String ZERO_OR_MORE_REGEX = "*";
|
||||
|
||||
public static final String OR_OPERATOR = "OR";
|
||||
|
||||
public static final String AND_OPERATOR = "AND";
|
||||
|
||||
public static final String LEFT_PARANTHESIS = "(";
|
||||
|
||||
public static final String RIGHT_PARANTHESIS = ")";
|
||||
|
||||
public static SearchOperation getSimpleOperation(final char input) {
|
||||
switch (input) {
|
||||
case ':':
|
||||
return EQUALITY;
|
||||
case '!':
|
||||
return NEGATION;
|
||||
case '>':
|
||||
return GREATER_THAN;
|
||||
case '<':
|
||||
return LESS_THAN;
|
||||
case '~':
|
||||
return LIKE;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,82 +1,82 @@
|
|||
package com.baeldung.web.util;
|
||||
|
||||
public class SpecSearchCriteria {
|
||||
|
||||
private String key;
|
||||
private SearchOperation operation;
|
||||
private Object value;
|
||||
private boolean orPredicate;
|
||||
|
||||
public SpecSearchCriteria() {
|
||||
|
||||
}
|
||||
|
||||
public SpecSearchCriteria(final String key, final SearchOperation operation, final Object value) {
|
||||
super();
|
||||
this.key = key;
|
||||
this.operation = operation;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public SpecSearchCriteria(final String orPredicate, final String key, final SearchOperation operation, final Object value) {
|
||||
super();
|
||||
this.orPredicate = orPredicate != null && orPredicate.equals(SearchOperation.OR_PREDICATE_FLAG);
|
||||
this.key = key;
|
||||
this.operation = operation;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public SpecSearchCriteria(String key, String operation, String prefix, String value, String suffix) {
|
||||
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||
if (op != null) {
|
||||
if (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.key = key;
|
||||
this.operation = op;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(final String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public SearchOperation getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
public void setOperation(final SearchOperation operation) {
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(final Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean isOrPredicate() {
|
||||
return orPredicate;
|
||||
}
|
||||
|
||||
public void setOrPredicate(boolean orPredicate) {
|
||||
this.orPredicate = orPredicate;
|
||||
}
|
||||
|
||||
}
|
||||
package com.baeldung.web.util;
|
||||
|
||||
public class SpecSearchCriteria {
|
||||
|
||||
private String key;
|
||||
private SearchOperation operation;
|
||||
private Object value;
|
||||
private boolean orPredicate;
|
||||
|
||||
public SpecSearchCriteria() {
|
||||
|
||||
}
|
||||
|
||||
public SpecSearchCriteria(final String key, final SearchOperation operation, final Object value) {
|
||||
super();
|
||||
this.key = key;
|
||||
this.operation = operation;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public SpecSearchCriteria(final String orPredicate, final String key, final SearchOperation operation, final Object value) {
|
||||
super();
|
||||
this.orPredicate = orPredicate != null && orPredicate.equals(SearchOperation.OR_PREDICATE_FLAG);
|
||||
this.key = key;
|
||||
this.operation = operation;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public SpecSearchCriteria(String key, String operation, String prefix, String value, String suffix) {
|
||||
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||
if (op != null) {
|
||||
if (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.key = key;
|
||||
this.operation = op;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(final String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public SearchOperation getOperation() {
|
||||
return operation;
|
||||
}
|
||||
|
||||
public void setOperation(final SearchOperation operation) {
|
||||
this.operation = operation;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(final Object value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public boolean isOrPredicate() {
|
||||
return orPredicate;
|
||||
}
|
||||
|
||||
public void setOrPredicate(boolean orPredicate) {
|
||||
this.orPredicate = orPredicate;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,180 +1,180 @@
|
|||
package com.baeldung.persistence.query;
|
||||
|
||||
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 com.baeldung.persistence.dao.GenericSpecificationsBuilder;
|
||||
import com.baeldung.persistence.dao.UserRepository;
|
||||
import com.baeldung.persistence.dao.UserSpecification;
|
||||
import com.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||
import com.baeldung.persistence.model.User;
|
||||
import com.baeldung.spring.PersistenceConfig;
|
||||
import com.baeldung.web.util.CriteriaParser;
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.baeldung.web.util.SpecSearchCriteria;
|
||||
|
||||
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(Specification
|
||||
.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(Specification.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(Specification.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(Specification
|
||||
.where(spec)
|
||||
.and(spec1));
|
||||
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
}
|
||||
package com.baeldung.persistence.query;
|
||||
|
||||
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 com.baeldung.persistence.dao.GenericSpecificationsBuilder;
|
||||
import com.baeldung.persistence.dao.UserRepository;
|
||||
import com.baeldung.persistence.dao.UserSpecification;
|
||||
import com.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||
import com.baeldung.persistence.model.User;
|
||||
import com.baeldung.spring.PersistenceConfig;
|
||||
import com.baeldung.web.util.CriteriaParser;
|
||||
import com.baeldung.web.util.SearchOperation;
|
||||
import com.baeldung.web.util.SpecSearchCriteria;
|
||||
|
||||
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(Specification
|
||||
.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(Specification.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(Specification.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(Specification
|
||||
.where(spec)
|
||||
.and(spec1));
|
||||
|
||||
assertThat(userJohn, isIn(results));
|
||||
assertThat(userTom, not(isIn(results)));
|
||||
}
|
||||
}
|
|
@ -1,147 +1,147 @@
|
|||
package com.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.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import com.baeldung.persistence.model.User;
|
||||
|
||||
//@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()));
|
||||
}
|
||||
|
||||
}
|
||||
package com.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.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
|
||||
import com.baeldung.persistence.model.User;
|
||||
|
||||
//@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()));
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue