JAVA-3535 Move spring rest query language module
This commit is contained in:
parent
eb6bd4ed18
commit
c99f74a6a1
2
pom.xml
2
pom.xml
|
@ -674,7 +674,6 @@
|
||||||
<module>spring-reactor</module>
|
<module>spring-reactor</module>
|
||||||
<module>spring-remoting</module>
|
<module>spring-remoting</module>
|
||||||
<module>spring-rest-http-2</module>
|
<module>spring-rest-http-2</module>
|
||||||
<module>spring-rest-query-language</module>
|
|
||||||
<module>spring-rest-shell</module>
|
<module>spring-rest-shell</module>
|
||||||
<module>spring-rest-simple</module>
|
<module>spring-rest-simple</module>
|
||||||
<module>spring-resttemplate</module>
|
<module>spring-resttemplate</module>
|
||||||
|
@ -1141,7 +1140,6 @@
|
||||||
|
|
||||||
<module>spring-reactor</module>
|
<module>spring-reactor</module>
|
||||||
<module>spring-remoting</module>
|
<module>spring-remoting</module>
|
||||||
<module>spring-rest-query-language</module>
|
|
||||||
<module>spring-rest-shell</module>
|
<module>spring-rest-shell</module>
|
||||||
<module>spring-rest-simple</module>
|
<module>spring-rest-simple</module>
|
||||||
<module>spring-resttemplate</module>
|
<module>spring-resttemplate</module>
|
||||||
|
|
|
@ -22,6 +22,7 @@
|
||||||
<module>spring-mvc-forms-jsp</module>
|
<module>spring-mvc-forms-jsp</module>
|
||||||
<module>spring-rest-angular</module>
|
<module>spring-rest-angular</module>
|
||||||
<module>spring-rest-http</module>
|
<module>spring-rest-http</module>
|
||||||
|
<module>spring-rest-query-language</module>
|
||||||
<module>spring-resttemplate-2</module>
|
<module>spring-resttemplate-2</module>
|
||||||
<module>spring-thymeleaf</module>
|
<module>spring-thymeleaf</module>
|
||||||
<module>spring-thymeleaf-2</module>
|
<module>spring-thymeleaf-2</module>
|
||||||
|
|
|
@ -11,7 +11,7 @@
|
||||||
<groupId>com.baeldung</groupId>
|
<groupId>com.baeldung</groupId>
|
||||||
<artifactId>parent-boot-2</artifactId>
|
<artifactId>parent-boot-2</artifactId>
|
||||||
<version>0.0.1-SNAPSHOT</version>
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
<relativePath>../parent-boot-2</relativePath>
|
<relativePath>../../parent-boot-2</relativePath>
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
|
@ -1,100 +1,100 @@
|
||||||
package com.baeldung.persistence.dao;
|
package com.baeldung.persistence.dao;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.Deque;
|
import java.util.Deque;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
|
||||||
import com.baeldung.web.util.SearchOperation;
|
import com.baeldung.web.util.SearchOperation;
|
||||||
import com.baeldung.web.util.SpecSearchCriteria;
|
import com.baeldung.web.util.SpecSearchCriteria;
|
||||||
|
|
||||||
public class GenericSpecificationsBuilder<U> {
|
public class GenericSpecificationsBuilder<U> {
|
||||||
|
|
||||||
private final List<SpecSearchCriteria> params;
|
private final List<SpecSearchCriteria> params;
|
||||||
|
|
||||||
public GenericSpecificationsBuilder() {
|
public GenericSpecificationsBuilder() {
|
||||||
this.params = new ArrayList<>();
|
this.params = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
public final GenericSpecificationsBuilder<U> with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
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);
|
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) {
|
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));
|
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||||
if (op != null) {
|
if (op != null) {
|
||||||
if (op == SearchOperation.EQUALITY) // the operation may be complex operation
|
if (op == SearchOperation.EQUALITY) // the operation may be complex operation
|
||||||
{
|
{
|
||||||
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||||
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||||
|
|
||||||
if (startWithAsterisk && endWithAsterisk) {
|
if (startWithAsterisk && endWithAsterisk) {
|
||||||
op = SearchOperation.CONTAINS;
|
op = SearchOperation.CONTAINS;
|
||||||
} else if (startWithAsterisk) {
|
} else if (startWithAsterisk) {
|
||||||
op = SearchOperation.ENDS_WITH;
|
op = SearchOperation.ENDS_WITH;
|
||||||
} else if (endWithAsterisk) {
|
} else if (endWithAsterisk) {
|
||||||
op = SearchOperation.STARTS_WITH;
|
op = SearchOperation.STARTS_WITH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
params.add(new SpecSearchCriteria(precedenceIndicator, key, op, value));
|
params.add(new SpecSearchCriteria(precedenceIndicator, key, op, value));
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Specification<U> build(Function<SpecSearchCriteria, Specification<U>> converter) {
|
public Specification<U> build(Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||||
|
|
||||||
if (params.size() == 0) {
|
if (params.size() == 0) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
final List<Specification<U>> specs = params.stream()
|
final List<Specification<U>> specs = params.stream()
|
||||||
.map(converter)
|
.map(converter)
|
||||||
.collect(Collectors.toCollection(ArrayList::new));
|
.collect(Collectors.toCollection(ArrayList::new));
|
||||||
|
|
||||||
Specification<U> result = specs.get(0);
|
Specification<U> result = specs.get(0);
|
||||||
|
|
||||||
for (int idx = 1; idx < specs.size(); idx++) {
|
for (int idx = 1; idx < specs.size(); idx++) {
|
||||||
result = params.get(idx)
|
result = params.get(idx)
|
||||||
.isOrPredicate()
|
.isOrPredicate()
|
||||||
? Specification.where(result)
|
? Specification.where(result)
|
||||||
.or(specs.get(idx))
|
.or(specs.get(idx))
|
||||||
: Specification.where(result)
|
: Specification.where(result)
|
||||||
.and(specs.get(idx));
|
.and(specs.get(idx));
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Specification<U> build(Deque<?> postFixedExprStack, Function<SpecSearchCriteria, Specification<U>> converter) {
|
public Specification<U> build(Deque<?> postFixedExprStack, Function<SpecSearchCriteria, Specification<U>> converter) {
|
||||||
|
|
||||||
Deque<Specification<U>> specStack = new LinkedList<>();
|
Deque<Specification<U>> specStack = new LinkedList<>();
|
||||||
|
|
||||||
Collections.reverse((List<?>) postFixedExprStack);
|
Collections.reverse((List<?>) postFixedExprStack);
|
||||||
|
|
||||||
while (!postFixedExprStack.isEmpty()) {
|
while (!postFixedExprStack.isEmpty()) {
|
||||||
Object mayBeOperand = postFixedExprStack.pop();
|
Object mayBeOperand = postFixedExprStack.pop();
|
||||||
|
|
||||||
if (!(mayBeOperand instanceof String)) {
|
if (!(mayBeOperand instanceof String)) {
|
||||||
specStack.push(converter.apply((SpecSearchCriteria) mayBeOperand));
|
specStack.push(converter.apply((SpecSearchCriteria) mayBeOperand));
|
||||||
} else {
|
} else {
|
||||||
Specification<U> operand1 = specStack.pop();
|
Specification<U> operand1 = specStack.pop();
|
||||||
Specification<U> operand2 = specStack.pop();
|
Specification<U> operand2 = specStack.pop();
|
||||||
if (mayBeOperand.equals(SearchOperation.AND_OPERATOR))
|
if (mayBeOperand.equals(SearchOperation.AND_OPERATOR))
|
||||||
specStack.push(Specification.where(operand1)
|
specStack.push(Specification.where(operand1)
|
||||||
.and(operand2));
|
.and(operand2));
|
||||||
else if (mayBeOperand.equals(SearchOperation.OR_OPERATOR))
|
else if (mayBeOperand.equals(SearchOperation.OR_OPERATOR))
|
||||||
specStack.push(Specification.where(operand1)
|
specStack.push(Specification.where(operand1)
|
||||||
.or(operand2));
|
.or(operand2));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
return specStack.pop();
|
return specStack.pop();
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,70 +1,70 @@
|
||||||
package com.baeldung.persistence.dao;
|
package com.baeldung.persistence.dao;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
|
|
||||||
import com.baeldung.persistence.model.User;
|
import com.baeldung.persistence.model.User;
|
||||||
import com.baeldung.web.util.SearchOperation;
|
import com.baeldung.web.util.SearchOperation;
|
||||||
import com.baeldung.web.util.SpecSearchCriteria;
|
import com.baeldung.web.util.SpecSearchCriteria;
|
||||||
|
|
||||||
public final class UserSpecificationsBuilder {
|
public final class UserSpecificationsBuilder {
|
||||||
|
|
||||||
private final List<SpecSearchCriteria> params;
|
private final List<SpecSearchCriteria> params;
|
||||||
|
|
||||||
public UserSpecificationsBuilder() {
|
public UserSpecificationsBuilder() {
|
||||||
params = new ArrayList<>();
|
params = new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
|
||||||
// API
|
// API
|
||||||
|
|
||||||
public final UserSpecificationsBuilder with(final String key, final String operation, final Object value, final String prefix, final String suffix) {
|
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);
|
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) {
|
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));
|
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||||
if (op != null) {
|
if (op != null) {
|
||||||
if (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
if (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
||||||
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||||
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||||
|
|
||||||
if (startWithAsterisk && endWithAsterisk) {
|
if (startWithAsterisk && endWithAsterisk) {
|
||||||
op = SearchOperation.CONTAINS;
|
op = SearchOperation.CONTAINS;
|
||||||
} else if (startWithAsterisk) {
|
} else if (startWithAsterisk) {
|
||||||
op = SearchOperation.ENDS_WITH;
|
op = SearchOperation.ENDS_WITH;
|
||||||
} else if (endWithAsterisk) {
|
} else if (endWithAsterisk) {
|
||||||
op = SearchOperation.STARTS_WITH;
|
op = SearchOperation.STARTS_WITH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
params.add(new SpecSearchCriteria(orPredicate, key, op, value));
|
params.add(new SpecSearchCriteria(orPredicate, key, op, value));
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Specification<User> build() {
|
public Specification<User> build() {
|
||||||
if (params.size() == 0)
|
if (params.size() == 0)
|
||||||
return null;
|
return null;
|
||||||
|
|
||||||
Specification<User> result = new UserSpecification(params.get(0));
|
Specification<User> result = new UserSpecification(params.get(0));
|
||||||
|
|
||||||
for (int i = 1; i < params.size(); i++) {
|
for (int i = 1; i < params.size(); i++) {
|
||||||
result = params.get(i).isOrPredicate()
|
result = params.get(i).isOrPredicate()
|
||||||
? Specification.where(result).or(new UserSpecification(params.get(i)))
|
? Specification.where(result).or(new UserSpecification(params.get(i)))
|
||||||
: Specification.where(result).and(new UserSpecification(params.get(i)));
|
: Specification.where(result).and(new UserSpecification(params.get(i)));
|
||||||
}
|
}
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final UserSpecificationsBuilder with(UserSpecification spec) {
|
public final UserSpecificationsBuilder with(UserSpecification spec) {
|
||||||
params.add(spec.getCriteria());
|
params.add(spec.getCriteria());
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
public final UserSpecificationsBuilder with(SpecSearchCriteria criteria) {
|
public final UserSpecificationsBuilder with(SpecSearchCriteria criteria) {
|
||||||
params.add(criteria);
|
params.add(criteria);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,171 +1,171 @@
|
||||||
package com.baeldung.web.controller;
|
package com.baeldung.web.controller;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.regex.Matcher;
|
import java.util.regex.Matcher;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
import org.springframework.data.querydsl.binding.QuerydslPredicate;
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.web.bind.annotation.GetMapping;
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestBody;
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
import org.springframework.web.bind.annotation.RequestMapping;
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.bind.annotation.RequestMethod;
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
import org.springframework.web.bind.annotation.RequestParam;
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
import org.springframework.web.bind.annotation.ResponseBody;
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||||
|
|
||||||
import com.baeldung.persistence.dao.GenericSpecificationsBuilder;
|
import com.baeldung.persistence.dao.GenericSpecificationsBuilder;
|
||||||
import com.baeldung.persistence.dao.IUserDAO;
|
import com.baeldung.persistence.dao.IUserDAO;
|
||||||
import com.baeldung.persistence.dao.MyUserPredicatesBuilder;
|
import com.baeldung.persistence.dao.MyUserPredicatesBuilder;
|
||||||
import com.baeldung.persistence.dao.MyUserRepository;
|
import com.baeldung.persistence.dao.MyUserRepository;
|
||||||
import com.baeldung.persistence.dao.UserRepository;
|
import com.baeldung.persistence.dao.UserRepository;
|
||||||
import com.baeldung.persistence.dao.UserSpecification;
|
import com.baeldung.persistence.dao.UserSpecification;
|
||||||
import com.baeldung.persistence.dao.UserSpecificationsBuilder;
|
import com.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||||
import com.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
|
import com.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
|
||||||
import com.baeldung.persistence.model.MyUser;
|
import com.baeldung.persistence.model.MyUser;
|
||||||
import com.baeldung.persistence.model.User;
|
import com.baeldung.persistence.model.User;
|
||||||
import com.baeldung.web.util.CriteriaParser;
|
import com.baeldung.web.util.CriteriaParser;
|
||||||
import com.baeldung.web.util.SearchCriteria;
|
import com.baeldung.web.util.SearchCriteria;
|
||||||
import com.baeldung.web.util.SearchOperation;
|
import com.baeldung.web.util.SearchOperation;
|
||||||
import com.google.common.base.Joiner;
|
import com.google.common.base.Joiner;
|
||||||
import com.google.common.base.Preconditions;
|
import com.google.common.base.Preconditions;
|
||||||
import com.querydsl.core.types.Predicate;
|
import com.querydsl.core.types.Predicate;
|
||||||
import com.querydsl.core.types.dsl.BooleanExpression;
|
import com.querydsl.core.types.dsl.BooleanExpression;
|
||||||
|
|
||||||
import cz.jirutka.rsql.parser.RSQLParser;
|
import cz.jirutka.rsql.parser.RSQLParser;
|
||||||
import cz.jirutka.rsql.parser.ast.Node;
|
import cz.jirutka.rsql.parser.ast.Node;
|
||||||
|
|
||||||
//@EnableSpringDataWebSupport
|
//@EnableSpringDataWebSupport
|
||||||
@Controller
|
@Controller
|
||||||
@RequestMapping(value = "/auth/")
|
@RequestMapping(value = "/auth/")
|
||||||
public class UserController {
|
public class UserController {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private IUserDAO service;
|
private IUserDAO service;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository dao;
|
private UserRepository dao;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private MyUserRepository myUserRepository;
|
private MyUserRepository myUserRepository;
|
||||||
|
|
||||||
public UserController() {
|
public UserController() {
|
||||||
super();
|
super();
|
||||||
}
|
}
|
||||||
|
|
||||||
// API - READ
|
// API - READ
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/users")
|
@RequestMapping(method = RequestMethod.GET, value = "/users")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public List<User> search(@RequestParam(value = "search", required = false) String search) {
|
public List<User> search(@RequestParam(value = "search", required = false) String search) {
|
||||||
List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
List<SearchCriteria> params = new ArrayList<SearchCriteria>();
|
||||||
if (search != null) {
|
if (search != null) {
|
||||||
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||||
Matcher matcher = pattern.matcher(search + ",");
|
Matcher matcher = pattern.matcher(search + ",");
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
|
params.add(new SearchCriteria(matcher.group(1), matcher.group(2), matcher.group(3)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return service.searchUser(params);
|
return service.searchUser(params);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
|
@RequestMapping(method = RequestMethod.GET, value = "/users/spec")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public List<User> findAllBySpecification(@RequestParam(value = "search") String search) {
|
public List<User> findAllBySpecification(@RequestParam(value = "search") String search) {
|
||||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||||
String operationSetExper = Joiner.on("|")
|
String operationSetExper = Joiner.on("|")
|
||||||
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||||
Pattern pattern = Pattern.compile("(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
Pattern pattern = Pattern.compile("(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||||
Matcher matcher = pattern.matcher(search + ",");
|
Matcher matcher = pattern.matcher(search + ",");
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(4), matcher.group(3), matcher.group(5));
|
builder.with(matcher.group(1), matcher.group(2), matcher.group(4), matcher.group(3), matcher.group(5));
|
||||||
}
|
}
|
||||||
|
|
||||||
Specification<User> spec = builder.build();
|
Specification<User> spec = builder.build();
|
||||||
return dao.findAll(spec);
|
return dao.findAll(spec);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(value = "/users/espec")
|
@GetMapping(value = "/users/espec")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public List<User> findAllByOrPredicate(@RequestParam(value = "search") String search) {
|
public List<User> findAllByOrPredicate(@RequestParam(value = "search") String search) {
|
||||||
Specification<User> spec = resolveSpecification(search);
|
Specification<User> spec = resolveSpecification(search);
|
||||||
return dao.findAll(spec);
|
return dao.findAll(spec);
|
||||||
}
|
}
|
||||||
|
|
||||||
@GetMapping(value = "/users/spec/adv")
|
@GetMapping(value = "/users/spec/adv")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public List<User> findAllByAdvPredicate(@RequestParam(value = "search") String search) {
|
public List<User> findAllByAdvPredicate(@RequestParam(value = "search") String search) {
|
||||||
Specification<User> spec = resolveSpecificationFromInfixExpr(search);
|
Specification<User> spec = resolveSpecificationFromInfixExpr(search);
|
||||||
return dao.findAll(spec);
|
return dao.findAll(spec);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Specification<User> resolveSpecificationFromInfixExpr(String searchParameters) {
|
protected Specification<User> resolveSpecificationFromInfixExpr(String searchParameters) {
|
||||||
CriteriaParser parser = new CriteriaParser();
|
CriteriaParser parser = new CriteriaParser();
|
||||||
GenericSpecificationsBuilder<User> specBuilder = new GenericSpecificationsBuilder<>();
|
GenericSpecificationsBuilder<User> specBuilder = new GenericSpecificationsBuilder<>();
|
||||||
return specBuilder.build(parser.parse(searchParameters), UserSpecification::new);
|
return specBuilder.build(parser.parse(searchParameters), UserSpecification::new);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected Specification<User> resolveSpecification(String searchParameters) {
|
protected Specification<User> resolveSpecification(String searchParameters) {
|
||||||
|
|
||||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||||
String operationSetExper = Joiner.on("|")
|
String operationSetExper = Joiner.on("|")
|
||||||
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
.join(SearchOperation.SIMPLE_OPERATION_SET);
|
||||||
Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
|
||||||
Matcher matcher = pattern.matcher(searchParameters + ",");
|
Matcher matcher = pattern.matcher(searchParameters + ",");
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(5), matcher.group(4), matcher.group(6));
|
builder.with(matcher.group(1), matcher.group(2), matcher.group(3), matcher.group(5), matcher.group(4), matcher.group(6));
|
||||||
}
|
}
|
||||||
return builder.build();
|
return builder.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
@RequestMapping(method = RequestMethod.GET, value = "/myusers")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") String search) {
|
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") String search) {
|
||||||
MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
MyUserPredicatesBuilder builder = new MyUserPredicatesBuilder();
|
||||||
if (search != null) {
|
if (search != null) {
|
||||||
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
Pattern pattern = Pattern.compile("(\\w+?)(:|<|>)(\\w+?),");
|
||||||
Matcher matcher = pattern.matcher(search + ",");
|
Matcher matcher = pattern.matcher(search + ",");
|
||||||
while (matcher.find()) {
|
while (matcher.find()) {
|
||||||
builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
builder.with(matcher.group(1), matcher.group(2), matcher.group(3));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
BooleanExpression exp = builder.build();
|
BooleanExpression exp = builder.build();
|
||||||
return myUserRepository.findAll(exp);
|
return myUserRepository.findAll(exp);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/users/rsql")
|
@RequestMapping(method = RequestMethod.GET, value = "/users/rsql")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public List<User> findAllByRsql(@RequestParam(value = "search") String search) {
|
public List<User> findAllByRsql(@RequestParam(value = "search") String search) {
|
||||||
Node rootNode = new RSQLParser().parse(search);
|
Node rootNode = new RSQLParser().parse(search);
|
||||||
Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
Specification<User> spec = rootNode.accept(new CustomRsqlVisitor<User>());
|
||||||
return dao.findAll(spec);
|
return dao.findAll(spec);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
|
@RequestMapping(method = RequestMethod.GET, value = "/api/myusers")
|
||||||
@ResponseBody
|
@ResponseBody
|
||||||
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) Predicate predicate) {
|
public Iterable<MyUser> findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) Predicate predicate) {
|
||||||
return myUserRepository.findAll(predicate);
|
return myUserRepository.findAll(predicate);
|
||||||
}
|
}
|
||||||
|
|
||||||
// API - WRITE
|
// API - WRITE
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.POST, value = "/users")
|
@RequestMapping(method = RequestMethod.POST, value = "/users")
|
||||||
@ResponseStatus(HttpStatus.CREATED)
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
public void create(@RequestBody User resource) {
|
public void create(@RequestBody User resource) {
|
||||||
Preconditions.checkNotNull(resource);
|
Preconditions.checkNotNull(resource);
|
||||||
dao.save(resource);
|
dao.save(resource);
|
||||||
}
|
}
|
||||||
|
|
||||||
@RequestMapping(method = RequestMethod.POST, value = "/myusers")
|
@RequestMapping(method = RequestMethod.POST, value = "/myusers")
|
||||||
@ResponseStatus(HttpStatus.CREATED)
|
@ResponseStatus(HttpStatus.CREATED)
|
||||||
public void addMyUser(@RequestBody MyUser resource) {
|
public void addMyUser(@RequestBody MyUser resource) {
|
||||||
Preconditions.checkNotNull(resource);
|
Preconditions.checkNotNull(resource);
|
||||||
myUserRepository.save(resource);
|
myUserRepository.save(resource);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,36 +1,36 @@
|
||||||
package com.baeldung.web.util;
|
package com.baeldung.web.util;
|
||||||
|
|
||||||
public enum SearchOperation {
|
public enum SearchOperation {
|
||||||
EQUALITY, NEGATION, GREATER_THAN, LESS_THAN, LIKE, STARTS_WITH, ENDS_WITH, CONTAINS;
|
EQUALITY, NEGATION, GREATER_THAN, LESS_THAN, LIKE, STARTS_WITH, ENDS_WITH, CONTAINS;
|
||||||
|
|
||||||
public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" };
|
public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" };
|
||||||
|
|
||||||
public static final String OR_PREDICATE_FLAG = "'";
|
public static final String OR_PREDICATE_FLAG = "'";
|
||||||
|
|
||||||
public static final String ZERO_OR_MORE_REGEX = "*";
|
public static final String ZERO_OR_MORE_REGEX = "*";
|
||||||
|
|
||||||
public static final String OR_OPERATOR = "OR";
|
public static final String OR_OPERATOR = "OR";
|
||||||
|
|
||||||
public static final String AND_OPERATOR = "AND";
|
public static final String AND_OPERATOR = "AND";
|
||||||
|
|
||||||
public static final String LEFT_PARANTHESIS = "(";
|
public static final String LEFT_PARANTHESIS = "(";
|
||||||
|
|
||||||
public static final String RIGHT_PARANTHESIS = ")";
|
public static final String RIGHT_PARANTHESIS = ")";
|
||||||
|
|
||||||
public static SearchOperation getSimpleOperation(final char input) {
|
public static SearchOperation getSimpleOperation(final char input) {
|
||||||
switch (input) {
|
switch (input) {
|
||||||
case ':':
|
case ':':
|
||||||
return EQUALITY;
|
return EQUALITY;
|
||||||
case '!':
|
case '!':
|
||||||
return NEGATION;
|
return NEGATION;
|
||||||
case '>':
|
case '>':
|
||||||
return GREATER_THAN;
|
return GREATER_THAN;
|
||||||
case '<':
|
case '<':
|
||||||
return LESS_THAN;
|
return LESS_THAN;
|
||||||
case '~':
|
case '~':
|
||||||
return LIKE;
|
return LIKE;
|
||||||
default:
|
default:
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,82 +1,82 @@
|
||||||
package com.baeldung.web.util;
|
package com.baeldung.web.util;
|
||||||
|
|
||||||
public class SpecSearchCriteria {
|
public class SpecSearchCriteria {
|
||||||
|
|
||||||
private String key;
|
private String key;
|
||||||
private SearchOperation operation;
|
private SearchOperation operation;
|
||||||
private Object value;
|
private Object value;
|
||||||
private boolean orPredicate;
|
private boolean orPredicate;
|
||||||
|
|
||||||
public SpecSearchCriteria() {
|
public SpecSearchCriteria() {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpecSearchCriteria(final String key, final SearchOperation operation, final Object value) {
|
public SpecSearchCriteria(final String key, final SearchOperation operation, final Object value) {
|
||||||
super();
|
super();
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpecSearchCriteria(final String orPredicate, final String key, final SearchOperation operation, final Object value) {
|
public SpecSearchCriteria(final String orPredicate, final String key, final SearchOperation operation, final Object value) {
|
||||||
super();
|
super();
|
||||||
this.orPredicate = orPredicate != null && orPredicate.equals(SearchOperation.OR_PREDICATE_FLAG);
|
this.orPredicate = orPredicate != null && orPredicate.equals(SearchOperation.OR_PREDICATE_FLAG);
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SpecSearchCriteria(String key, String operation, String prefix, String value, String suffix) {
|
public SpecSearchCriteria(String key, String operation, String prefix, String value, String suffix) {
|
||||||
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
SearchOperation op = SearchOperation.getSimpleOperation(operation.charAt(0));
|
||||||
if (op != null) {
|
if (op != null) {
|
||||||
if (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
if (op == SearchOperation.EQUALITY) { // the operation may be complex operation
|
||||||
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
final boolean startWithAsterisk = prefix != null && prefix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||||
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
|
||||||
|
|
||||||
if (startWithAsterisk && endWithAsterisk) {
|
if (startWithAsterisk && endWithAsterisk) {
|
||||||
op = SearchOperation.CONTAINS;
|
op = SearchOperation.CONTAINS;
|
||||||
} else if (startWithAsterisk) {
|
} else if (startWithAsterisk) {
|
||||||
op = SearchOperation.ENDS_WITH;
|
op = SearchOperation.ENDS_WITH;
|
||||||
} else if (endWithAsterisk) {
|
} else if (endWithAsterisk) {
|
||||||
op = SearchOperation.STARTS_WITH;
|
op = SearchOperation.STARTS_WITH;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
this.key = key;
|
this.key = key;
|
||||||
this.operation = op;
|
this.operation = op;
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getKey() {
|
public String getKey() {
|
||||||
return key;
|
return key;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setKey(final String key) {
|
public void setKey(final String key) {
|
||||||
this.key = key;
|
this.key = key;
|
||||||
}
|
}
|
||||||
|
|
||||||
public SearchOperation getOperation() {
|
public SearchOperation getOperation() {
|
||||||
return operation;
|
return operation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOperation(final SearchOperation operation) {
|
public void setOperation(final SearchOperation operation) {
|
||||||
this.operation = operation;
|
this.operation = operation;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Object getValue() {
|
public Object getValue() {
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setValue(final Object value) {
|
public void setValue(final Object value) {
|
||||||
this.value = value;
|
this.value = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean isOrPredicate() {
|
public boolean isOrPredicate() {
|
||||||
return orPredicate;
|
return orPredicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setOrPredicate(boolean orPredicate) {
|
public void setOrPredicate(boolean orPredicate) {
|
||||||
this.orPredicate = orPredicate;
|
this.orPredicate = orPredicate;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -1,180 +1,180 @@
|
||||||
package com.baeldung.persistence.query;
|
package com.baeldung.persistence.query;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.data.jpa.domain.Specification;
|
import org.springframework.data.jpa.domain.Specification;
|
||||||
import org.springframework.test.annotation.Rollback;
|
import org.springframework.test.annotation.Rollback;
|
||||||
import org.springframework.test.context.ContextConfiguration;
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.transaction.annotation.Transactional;
|
||||||
|
|
||||||
import com.baeldung.persistence.dao.GenericSpecificationsBuilder;
|
import com.baeldung.persistence.dao.GenericSpecificationsBuilder;
|
||||||
import com.baeldung.persistence.dao.UserRepository;
|
import com.baeldung.persistence.dao.UserRepository;
|
||||||
import com.baeldung.persistence.dao.UserSpecification;
|
import com.baeldung.persistence.dao.UserSpecification;
|
||||||
import com.baeldung.persistence.dao.UserSpecificationsBuilder;
|
import com.baeldung.persistence.dao.UserSpecificationsBuilder;
|
||||||
import com.baeldung.persistence.model.User;
|
import com.baeldung.persistence.model.User;
|
||||||
import com.baeldung.spring.PersistenceConfig;
|
import com.baeldung.spring.PersistenceConfig;
|
||||||
import com.baeldung.web.util.CriteriaParser;
|
import com.baeldung.web.util.CriteriaParser;
|
||||||
import com.baeldung.web.util.SearchOperation;
|
import com.baeldung.web.util.SearchOperation;
|
||||||
import com.baeldung.web.util.SpecSearchCriteria;
|
import com.baeldung.web.util.SpecSearchCriteria;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
|
import static org.hamcrest.collection.IsCollectionWithSize.hasSize;
|
||||||
import static org.hamcrest.collection.IsIn.isIn;
|
import static org.hamcrest.collection.IsIn.isIn;
|
||||||
import static org.hamcrest.core.IsNot.not;
|
import static org.hamcrest.core.IsNot.not;
|
||||||
|
|
||||||
@RunWith(SpringJUnit4ClassRunner.class)
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
@ContextConfiguration(classes = { PersistenceConfig.class })
|
@ContextConfiguration(classes = { PersistenceConfig.class })
|
||||||
@Transactional
|
@Transactional
|
||||||
@Rollback
|
@Rollback
|
||||||
public class JPASpecificationIntegrationTest {
|
public class JPASpecificationIntegrationTest {
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private UserRepository repository;
|
private UserRepository repository;
|
||||||
|
|
||||||
private User userJohn;
|
private User userJohn;
|
||||||
|
|
||||||
private User userTom;
|
private User userTom;
|
||||||
|
|
||||||
private User userPercy;
|
private User userPercy;
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
userJohn = new User();
|
userJohn = new User();
|
||||||
userJohn.setFirstName("john");
|
userJohn.setFirstName("john");
|
||||||
userJohn.setLastName("doe");
|
userJohn.setLastName("doe");
|
||||||
userJohn.setEmail("john@doe.com");
|
userJohn.setEmail("john@doe.com");
|
||||||
userJohn.setAge(22);
|
userJohn.setAge(22);
|
||||||
repository.save(userJohn);
|
repository.save(userJohn);
|
||||||
|
|
||||||
userTom = new User();
|
userTom = new User();
|
||||||
userTom.setFirstName("tom");
|
userTom.setFirstName("tom");
|
||||||
userTom.setLastName("doe");
|
userTom.setLastName("doe");
|
||||||
userTom.setEmail("tom@doe.com");
|
userTom.setEmail("tom@doe.com");
|
||||||
userTom.setAge(26);
|
userTom.setAge(26);
|
||||||
repository.save(userTom);
|
repository.save(userTom);
|
||||||
|
|
||||||
userPercy = new User();
|
userPercy = new User();
|
||||||
userPercy.setFirstName("percy");
|
userPercy.setFirstName("percy");
|
||||||
userPercy.setLastName("blackney");
|
userPercy.setLastName("blackney");
|
||||||
userPercy.setEmail("percy@blackney.com");
|
userPercy.setEmail("percy@blackney.com");
|
||||||
userPercy.setAge(30);
|
userPercy.setAge(30);
|
||||||
repository.save(userPercy);
|
repository.save(userPercy);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.EQUALITY, "john"));
|
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.EQUALITY, "john"));
|
||||||
final UserSpecification spec1 = new UserSpecification(new SpecSearchCriteria("lastName", SearchOperation.EQUALITY, "doe"));
|
final UserSpecification spec1 = new UserSpecification(new SpecSearchCriteria("lastName", SearchOperation.EQUALITY, "doe"));
|
||||||
final List<User> results = repository.findAll(Specification
|
final List<User> results = repository.findAll(Specification
|
||||||
.where(spec)
|
.where(spec)
|
||||||
.and(spec1));
|
.and(spec1));
|
||||||
|
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, not(isIn(results)));
|
assertThat(userTom, not(isIn(results)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
|
||||||
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
|
||||||
|
|
||||||
SpecSearchCriteria spec = new SpecSearchCriteria("firstName", SearchOperation.EQUALITY, "john");
|
SpecSearchCriteria spec = new SpecSearchCriteria("firstName", SearchOperation.EQUALITY, "john");
|
||||||
SpecSearchCriteria spec1 = new SpecSearchCriteria("'","lastName", SearchOperation.EQUALITY, "doe");
|
SpecSearchCriteria spec1 = new SpecSearchCriteria("'","lastName", SearchOperation.EQUALITY, "doe");
|
||||||
|
|
||||||
List<User> results = repository.findAll(builder
|
List<User> results = repository.findAll(builder
|
||||||
.with(spec)
|
.with(spec)
|
||||||
.with(spec1)
|
.with(spec1)
|
||||||
.build());
|
.build());
|
||||||
|
|
||||||
assertThat(results, hasSize(2));
|
assertThat(results, hasSize(2));
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, isIn(results));
|
assertThat(userTom, isIn(results));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstOrLastNameAndAgeGenericBuilder_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstOrLastNameAndAgeGenericBuilder_whenGettingListOfUsers_thenCorrect() {
|
||||||
GenericSpecificationsBuilder<User> builder = new GenericSpecificationsBuilder<>();
|
GenericSpecificationsBuilder<User> builder = new GenericSpecificationsBuilder<>();
|
||||||
Function<SpecSearchCriteria, Specification<User>> converter = UserSpecification::new;
|
Function<SpecSearchCriteria, Specification<User>> converter = UserSpecification::new;
|
||||||
|
|
||||||
CriteriaParser parser=new CriteriaParser();
|
CriteriaParser parser=new CriteriaParser();
|
||||||
List<User> results = repository.findAll(builder.build(parser.parse("( lastName:doe OR firstName:john ) AND age:22"), converter));
|
List<User> results = repository.findAll(builder.build(parser.parse("( lastName:doe OR firstName:john ) AND age:22"), converter));
|
||||||
|
|
||||||
assertThat(results, hasSize(1));
|
assertThat(results, hasSize(1));
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, not(isIn(results)));
|
assertThat(userTom, not(isIn(results)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstOrLastNameGenericBuilder_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstOrLastNameGenericBuilder_whenGettingListOfUsers_thenCorrect() {
|
||||||
GenericSpecificationsBuilder<User> builder = new GenericSpecificationsBuilder<>();
|
GenericSpecificationsBuilder<User> builder = new GenericSpecificationsBuilder<>();
|
||||||
Function<SpecSearchCriteria, Specification<User>> converter = UserSpecification::new;
|
Function<SpecSearchCriteria, Specification<User>> converter = UserSpecification::new;
|
||||||
|
|
||||||
builder.with("firstName", ":", "john", null, null);
|
builder.with("firstName", ":", "john", null, null);
|
||||||
builder.with("'", "lastName", ":", "doe", null, null);
|
builder.with("'", "lastName", ":", "doe", null, null);
|
||||||
|
|
||||||
List<User> results = repository.findAll(builder.build(converter));
|
List<User> results = repository.findAll(builder.build(converter));
|
||||||
|
|
||||||
assertThat(results, hasSize(2));
|
assertThat(results, hasSize(2));
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, isIn(results));
|
assertThat(userTom, isIn(results));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstNameInverse_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstNameInverse_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.NEGATION, "john"));
|
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.NEGATION, "john"));
|
||||||
final List<User> results = repository.findAll(Specification.where(spec));
|
final List<User> results = repository.findAll(Specification.where(spec));
|
||||||
|
|
||||||
assertThat(userTom, isIn(results));
|
assertThat(userTom, isIn(results));
|
||||||
assertThat(userJohn, not(isIn(results)));
|
assertThat(userJohn, not(isIn(results)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMinAge_whenGettingListOfUsers_thenCorrect() {
|
public void givenMinAge_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.GREATER_THAN, "25"));
|
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.GREATER_THAN, "25"));
|
||||||
final List<User> results = repository.findAll(Specification.where(spec));
|
final List<User> results = repository.findAll(Specification.where(spec));
|
||||||
assertThat(userTom, isIn(results));
|
assertThat(userTom, isIn(results));
|
||||||
assertThat(userJohn, not(isIn(results)));
|
assertThat(userJohn, not(isIn(results)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.STARTS_WITH, "jo"));
|
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.STARTS_WITH, "jo"));
|
||||||
final List<User> results = repository.findAll(spec);
|
final List<User> results = repository.findAll(spec);
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, not(isIn(results)));
|
assertThat(userTom, not(isIn(results)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.ENDS_WITH, "n"));
|
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.ENDS_WITH, "n"));
|
||||||
final List<User> results = repository.findAll(spec);
|
final List<User> results = repository.findAll(spec);
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, not(isIn(results)));
|
assertThat(userTom, not(isIn(results)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstNameSubstring_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstNameSubstring_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.CONTAINS, "oh"));
|
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("firstName", SearchOperation.CONTAINS, "oh"));
|
||||||
final List<User> results = repository.findAll(spec);
|
final List<User> results = repository.findAll(spec);
|
||||||
|
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, not(isIn(results)));
|
assertThat(userTom, not(isIn(results)));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAgeRange_whenGettingListOfUsers_thenCorrect() {
|
public void givenAgeRange_whenGettingListOfUsers_thenCorrect() {
|
||||||
final UserSpecification spec = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.GREATER_THAN, "20"));
|
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 UserSpecification spec1 = new UserSpecification(new SpecSearchCriteria("age", SearchOperation.LESS_THAN, "25"));
|
||||||
final List<User> results = repository.findAll(Specification
|
final List<User> results = repository.findAll(Specification
|
||||||
.where(spec)
|
.where(spec)
|
||||||
.and(spec1));
|
.and(spec1));
|
||||||
|
|
||||||
assertThat(userJohn, isIn(results));
|
assertThat(userJohn, isIn(results));
|
||||||
assertThat(userTom, not(isIn(results)));
|
assertThat(userTom, not(isIn(results)));
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,147 +1,147 @@
|
||||||
package com.baeldung.persistence.query;
|
package com.baeldung.persistence.query;
|
||||||
|
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import io.restassured.RestAssured;
|
import io.restassured.RestAssured;
|
||||||
import io.restassured.response.Response;
|
import io.restassured.response.Response;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.test.context.ActiveProfiles;
|
import org.springframework.test.context.ActiveProfiles;
|
||||||
|
|
||||||
import com.baeldung.persistence.model.User;
|
import com.baeldung.persistence.model.User;
|
||||||
|
|
||||||
//@RunWith(SpringJUnit4ClassRunner.class)
|
//@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
//@ContextConfiguration(classes = { ConfigTest.class,
|
//@ContextConfiguration(classes = { ConfigTest.class,
|
||||||
// PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
// PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
|
||||||
@ActiveProfiles("test")
|
@ActiveProfiles("test")
|
||||||
public class JPASpecificationLiveTest {
|
public class JPASpecificationLiveTest {
|
||||||
|
|
||||||
// @Autowired
|
// @Autowired
|
||||||
// private UserRepository repository;
|
// private UserRepository repository;
|
||||||
|
|
||||||
private User userJohn;
|
private User userJohn;
|
||||||
|
|
||||||
private User userTom;
|
private User userTom;
|
||||||
|
|
||||||
private final String URL_PREFIX = "http://localhost:8082/spring-rest-query-language/auth/users/spec?search=";
|
private final String URL_PREFIX = "http://localhost:8082/spring-rest-query-language/auth/users/spec?search=";
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void init() {
|
public void init() {
|
||||||
userJohn = new User();
|
userJohn = new User();
|
||||||
userJohn.setFirstName("john");
|
userJohn.setFirstName("john");
|
||||||
userJohn.setLastName("doe");
|
userJohn.setLastName("doe");
|
||||||
userJohn.setEmail("john@doe.com");
|
userJohn.setEmail("john@doe.com");
|
||||||
userJohn.setAge(22);
|
userJohn.setAge(22);
|
||||||
// repository.save(userJohn);
|
// repository.save(userJohn);
|
||||||
|
|
||||||
userTom = new User();
|
userTom = new User();
|
||||||
userTom.setFirstName("tom");
|
userTom.setFirstName("tom");
|
||||||
userTom.setLastName("doe");
|
userTom.setLastName("doe");
|
||||||
userTom.setEmail("tom@doe.com");
|
userTom.setEmail("tom@doe.com");
|
||||||
userTom.setAge(26);
|
userTom.setAge(26);
|
||||||
// repository.save(userTom);
|
// repository.save(userTom);
|
||||||
}
|
}
|
||||||
|
|
||||||
private final String EURL_PREFIX = "http://localhost:8082/spring-rest-query-language/auth/users/espec?search=";
|
private final String EURL_PREFIX = "http://localhost:8082/spring-rest-query-language/auth/users/espec?search=";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
|
||||||
final Response response = RestAssured.get(EURL_PREFIX + "firstName:john,'lastName:doe");
|
final Response response = RestAssured.get(EURL_PREFIX + "firstName:john,'lastName:doe");
|
||||||
final String result = response.body()
|
final String result = response.body()
|
||||||
.asString();
|
.asString();
|
||||||
assertTrue(result.contains(userJohn.getEmail()));
|
assertTrue(result.contains(userJohn.getEmail()));
|
||||||
assertTrue(result.contains(userTom.getEmail()));
|
assertTrue(result.contains(userTom.getEmail()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
|
||||||
final Response response = RestAssured.get(URL_PREFIX + "firstName:john,lastName:doe");
|
final Response response = RestAssured.get(URL_PREFIX + "firstName:john,lastName:doe");
|
||||||
final String result = response.body()
|
final String result = response.body()
|
||||||
.asString();
|
.asString();
|
||||||
|
|
||||||
assertTrue(result.contains(userJohn.getEmail()));
|
assertTrue(result.contains(userJohn.getEmail()));
|
||||||
assertFalse(result.contains(userTom.getEmail()));
|
assertFalse(result.contains(userTom.getEmail()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstNameInverse_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstNameInverse_whenGettingListOfUsers_thenCorrect() {
|
||||||
final Response response = RestAssured.get(URL_PREFIX + "firstName!john");
|
final Response response = RestAssured.get(URL_PREFIX + "firstName!john");
|
||||||
final String result = response.body()
|
final String result = response.body()
|
||||||
.asString();
|
.asString();
|
||||||
|
|
||||||
assertTrue(result.contains(userTom.getEmail()));
|
assertTrue(result.contains(userTom.getEmail()));
|
||||||
assertFalse(result.contains(userJohn.getEmail()));
|
assertFalse(result.contains(userJohn.getEmail()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenMinAge_whenGettingListOfUsers_thenCorrect() {
|
public void givenMinAge_whenGettingListOfUsers_thenCorrect() {
|
||||||
final Response response = RestAssured.get(URL_PREFIX + "age>25");
|
final Response response = RestAssured.get(URL_PREFIX + "age>25");
|
||||||
final String result = response.body()
|
final String result = response.body()
|
||||||
.asString();
|
.asString();
|
||||||
|
|
||||||
assertTrue(result.contains(userTom.getEmail()));
|
assertTrue(result.contains(userTom.getEmail()));
|
||||||
assertFalse(result.contains(userJohn.getEmail()));
|
assertFalse(result.contains(userJohn.getEmail()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstNamePrefix_whenGettingListOfUsers_thenCorrect() {
|
||||||
final Response response = RestAssured.get(URL_PREFIX + "firstName:jo*");
|
final Response response = RestAssured.get(URL_PREFIX + "firstName:jo*");
|
||||||
final String result = response.body()
|
final String result = response.body()
|
||||||
.asString();
|
.asString();
|
||||||
|
|
||||||
assertTrue(result.contains(userJohn.getEmail()));
|
assertTrue(result.contains(userJohn.getEmail()));
|
||||||
assertFalse(result.contains(userTom.getEmail()));
|
assertFalse(result.contains(userTom.getEmail()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstNameSuffix_whenGettingListOfUsers_thenCorrect() {
|
||||||
final Response response = RestAssured.get(URL_PREFIX + "firstName:*n");
|
final Response response = RestAssured.get(URL_PREFIX + "firstName:*n");
|
||||||
final String result = response.body()
|
final String result = response.body()
|
||||||
.asString();
|
.asString();
|
||||||
|
|
||||||
assertTrue(result.contains(userJohn.getEmail()));
|
assertTrue(result.contains(userJohn.getEmail()));
|
||||||
assertFalse(result.contains(userTom.getEmail()));
|
assertFalse(result.contains(userTom.getEmail()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstNameSubstring_whenGettingListOfUsers_thenCorrect() {
|
public void givenFirstNameSubstring_whenGettingListOfUsers_thenCorrect() {
|
||||||
final Response response = RestAssured.get(URL_PREFIX + "firstName:*oh*");
|
final Response response = RestAssured.get(URL_PREFIX + "firstName:*oh*");
|
||||||
final String result = response.body()
|
final String result = response.body()
|
||||||
.asString();
|
.asString();
|
||||||
|
|
||||||
assertTrue(result.contains(userJohn.getEmail()));
|
assertTrue(result.contains(userJohn.getEmail()));
|
||||||
assertFalse(result.contains(userTom.getEmail()));
|
assertFalse(result.contains(userTom.getEmail()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAgeRange_whenGettingListOfUsers_thenCorrect() {
|
public void givenAgeRange_whenGettingListOfUsers_thenCorrect() {
|
||||||
final Response response = RestAssured.get(URL_PREFIX + "age>20,age<25");
|
final Response response = RestAssured.get(URL_PREFIX + "age>20,age<25");
|
||||||
final String result = response.body()
|
final String result = response.body()
|
||||||
.asString();
|
.asString();
|
||||||
|
|
||||||
assertTrue(result.contains(userJohn.getEmail()));
|
assertTrue(result.contains(userJohn.getEmail()));
|
||||||
assertFalse(result.contains(userTom.getEmail()));
|
assertFalse(result.contains(userTom.getEmail()));
|
||||||
}
|
}
|
||||||
|
|
||||||
private final String ADV_URL_PREFIX = "http://localhost:8082/spring-rest-query-language/auth/users/spec/adv?search=";
|
private final String ADV_URL_PREFIX = "http://localhost:8082/spring-rest-query-language/auth/users/spec/adv?search=";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstOrLastName_whenGettingAdvListOfUsers_thenCorrect() {
|
public void givenFirstOrLastName_whenGettingAdvListOfUsers_thenCorrect() {
|
||||||
final Response response = RestAssured.get(ADV_URL_PREFIX + "firstName:john OR lastName:doe");
|
final Response response = RestAssured.get(ADV_URL_PREFIX + "firstName:john OR lastName:doe");
|
||||||
final String result = response.body()
|
final String result = response.body()
|
||||||
.asString();
|
.asString();
|
||||||
assertTrue(result.contains(userJohn.getEmail()));
|
assertTrue(result.contains(userJohn.getEmail()));
|
||||||
assertTrue(result.contains(userTom.getEmail()));
|
assertTrue(result.contains(userTom.getEmail()));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenFirstOrFirstNameAndAge_whenGettingAdvListOfUsers_thenCorrect() {
|
public void givenFirstOrFirstNameAndAge_whenGettingAdvListOfUsers_thenCorrect() {
|
||||||
final Response response = RestAssured.get(ADV_URL_PREFIX + "( firstName:john OR firstName:tom ) AND age>22");
|
final Response response = RestAssured.get(ADV_URL_PREFIX + "( firstName:john OR firstName:tom ) AND age>22");
|
||||||
final String result = response.body()
|
final String result = response.body()
|
||||||
.asString();
|
.asString();
|
||||||
assertFalse(result.contains(userJohn.getEmail()));
|
assertFalse(result.contains(userJohn.getEmail()));
|
||||||
assertTrue(result.contains(userTom.getEmail()));
|
assertTrue(result.contains(userTom.getEmail()));
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
Loading…
Reference in New Issue