BAEL-696 - Implement OR in the REST API Query Language (#1404)

* Dependency Injection Types, XML-Config, Java-Config, Test Classes

* Formatting done with Formatter Configuration in Eclipse

* REST Query Lang - Adv Search Ops - Improvement - C1

* REST Query Lang - Adv Search Ops - Improvement - C2

* add update to rest api (#1401)

* upgrade to spring boot 1.5.2

* add full update to REST API

* BAEL-634 javassist (#1349)

* BEEL-634 javassist dependency

* BEEL-634 code for javassist article

* BEEL-634 test refinement

* BEEL-634 increment lib to newest version

* add test that uses reflection to verify

* add field

* add bytecode to different class

* adding following modules with updated testcase : DB, Filter, Json (#1410)

* adding ratpack module

* adding pom.xml

* adding following modules with updated testcase : DB, Filter, Json

* add entry points (#1413)

* spring boot custom banner (#1412)

* adding ratpack module

* adding pom.xml

* adding following modules with updated testcase : DB, Filter, Json

* adding spring-boot custom banner tutorial

* BALE-707 Refactoring changes (#1418)

* BAEL-707 Add the changes as per review comment

* BAEL-707 Refactored the code as per review comments

* BAEL-696 Code formatting
This commit is contained in:
ahamedm 2017-03-16 14:49:13 +04:00 committed by pedja4
parent de29f6af4b
commit 26b5ab860a
20 changed files with 898 additions and 413 deletions

View File

@ -0,0 +1,17 @@
package com.baeldung.beaninjection;
public class AnotherSampleDAOBean implements IAnotherSampleDAO {
private String propertyY;
public AnotherSampleDAOBean(String propertyY) {
this.propertyY = propertyY;
}
// standard setters and getters
public String getPropertyY() {
return propertyY;
}
}

View File

@ -0,0 +1,39 @@
package com.baeldung.beaninjection;
import java.util.List;
public class ExampleDAOBean implements IExampleDAO {
private String propertyX;
public ExampleDAOBean(String propertyX) {
this.propertyX = propertyX;
}
public String getPropertyX() {
return propertyX;
}
public void setPropertyX(String propertyX) {
this.propertyX = propertyX;
}
@Override
public List<SampleDomainObject> getDomainList() {
// TODO Auto-generated method stub
return null;
}
@Override
public SampleDomainObject createNewDomain(SampleDomainObject inputDomain) {
// TODO Auto-generated method stub
return null;
}
@Override
public SampleDomainObject getSomeDomain() {
// TODO Auto-generated method stub
return new SampleDomainObject();
}
}

View File

@ -0,0 +1,47 @@
package com.baeldung.beaninjection;
import java.util.List;
public class ExampleServiceBean implements IExampleService {
private IExampleDAO exampleDAO;
private IAnotherSampleDAO anotherSampleDAO;
public ExampleServiceBean(IExampleDAO exampleDAO) {
this.exampleDAO = exampleDAO;
}
public void setAnotherSampleDAO(IAnotherSampleDAO anotherSampleDAO) {
this.anotherSampleDAO = anotherSampleDAO;
}
// standard setters and getters
public IAnotherSampleDAO getAnotherSampleDAO() {
return anotherSampleDAO;
}
public void setExampleDAO(ExampleDAOBean exampleDAO) {
this.exampleDAO = exampleDAO;
}
public IExampleDAO getExampleDAO() {
return exampleDAO;
}
private String propertyX;
public String getPropertyX() {
return propertyX;
}
public void setPropertyX(String propertyX) {
this.propertyX = propertyX;
}
public List<SampleDomainObject> serviceMethodX() {
/*get domain list from DAO .. business logic on domain objects..return*/
return exampleDAO.getDomainList();
}
}

View File

@ -0,0 +1,5 @@
package com.baeldung.beaninjection;
public interface IAnotherSampleDAO {
}

View File

@ -0,0 +1,12 @@
package com.baeldung.beaninjection;
import java.util.List;
public interface IExampleDAO {
List<SampleDomainObject> getDomainList();
SampleDomainObject createNewDomain(SampleDomainObject domainObject);
SampleDomainObject getSomeDomain();
}

View File

@ -0,0 +1,9 @@
package com.baeldung.beaninjection;
import java.util.List;
public interface IExampleService {
List<SampleDomainObject> serviceMethodX();
}

View File

@ -0,0 +1,31 @@
package com.baeldung.beaninjection;
import java.io.Serializable;
public class SampleDomainObject implements Serializable {
/**
*
*/
private static final long serialVersionUID = 449859763481296747L;
private String domainPropX;
private String domainPropY;
public String getDomainPropX() {
return domainPropX;
}
public void setDomainPropX(String domainPropX) {
this.domainPropX = domainPropX;
}
public String getDomainPropY() {
return domainPropY;
}
public void setDomainPropY(String domainPropY) {
this.domainPropY = domainPropY;
}
}

View File

@ -0,0 +1,36 @@
package com.baeldung.configuration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import com.baeldung.beaninjection.AnotherSampleDAOBean;
import com.baeldung.beaninjection.ExampleDAOBean;
import com.baeldung.beaninjection.ExampleServiceBean;
import com.baeldung.beaninjection.IAnotherSampleDAO;
import com.baeldung.beaninjection.IExampleDAO;
import com.baeldung.beaninjection.IExampleService;
@Configuration
@ComponentScan(basePackages = { "com.baeldung.beaninjection" })
public class ApplicationContextTestBeanInjectionTypes {
@Bean
public IExampleDAO exampleDAO() {
return new ExampleDAOBean("Mandatory DAO Property X");
}
@Bean
public IExampleService exampleServiceBean() {
ExampleServiceBean serviceBean = new ExampleServiceBean(exampleDAO());
serviceBean.setAnotherSampleDAO(anotherSampleDAO());
serviceBean.setPropertyX("Some Service Property X");
return serviceBean;
}
@Bean
public IAnotherSampleDAO anotherSampleDAO() {
return new AnotherSampleDAOBean("Mandatory DAO Property Y");
}
}

View File

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="exampleDAO" class="com.baeldung.beaninjection.ExampleDAOBean">
<constructor-arg index="0" type="java.lang.String"
value="Mandatory DAO Property X" />
</bean>
<bean id="anotherSampleDAO" class="com.baeldung.beaninjection.AnotherSampleDAOBean">
<constructor-arg index="0" type="java.lang.String"
value="Mandatory DAO Property Y" />
</bean>
<bean id="exampleService" class="com.baeldung.beaninjection.ExampleServiceBean">
<constructor-arg index="0" ref="exampleDAO" />
<property name="propertyX" value="Some Service Property X"></property>
<property name="anotherSampleDAO" ref="anotherSampleDAO"></property>
</bean>
</beans>

View File

@ -0,0 +1,50 @@
package com.baeldung.test.beaninjection;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.beaninjection.AnotherSampleDAOBean;
import com.baeldung.beaninjection.ExampleDAOBean;
import com.baeldung.beaninjection.ExampleServiceBean;
import com.baeldung.configuration.ApplicationContextTestBeanInjectionTypes;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = ApplicationContextTestBeanInjectionTypes.class)
public class BeanInjectionJavaConfigTest implements ApplicationContextAware {
private ApplicationContext beanInjectedContext;
@Test
public void testDAOInjectionByJava() {
ExampleServiceBean serviceBean = beanInjectedContext.getBean(ExampleServiceBean.class);
assertNotNull("Failed: Constructor Injection,Bean Reference Injection,Java Config, ExampleServiceBean", serviceBean.getExampleDAO());
assertNotNull("Failed: Constructor Injection,Bean Reference Injection,Java Config, ExampleServiceBean", serviceBean.getAnotherSampleDAO());
assertTrue("Failed: Constructor Injection,String Property , Java Config", serviceBean.getPropertyX()
.equals("Some Service Property X"));
}
@Test
public void testPropertyInjectioninDAOByJava() {
ExampleDAOBean daoBean = beanInjectedContext.getBean(ExampleDAOBean.class);
assertTrue("Failed: Constructor Injection,String Property , Java Config", daoBean.getPropertyX()
.equals("Mandatory DAO Property X"));
AnotherSampleDAOBean anotherDAOBean = beanInjectedContext.getBean(AnotherSampleDAOBean.class);
assertTrue("Failed: Constructor Injection,String Property , XML Config", anotherDAOBean.getPropertyY()
.equals("Mandatory DAO Property Y"));
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
this.beanInjectedContext = applicationContext;
}
}

View File

@ -0,0 +1,49 @@
package com.baeldung.test.beaninjection;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.baeldung.beaninjection.AnotherSampleDAOBean;
import com.baeldung.beaninjection.ExampleDAOBean;
import com.baeldung.beaninjection.ExampleServiceBean;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:beaninjectiontypes-context.xml")
public class BeanInjectionXMLConfigTest implements ApplicationContextAware {
private ApplicationContext beanInjectedContext;
@Test
public void testDAOInjectionByXML() {
ExampleServiceBean serviceBean = beanInjectedContext.getBean(ExampleServiceBean.class);
assertNotNull("Failed: Constructor Injection,Bean Reference Injection,XML Config, ExampleServiceBean", serviceBean.getExampleDAO());
assertNotNull("Failed: Constructor Injection,Bean Reference Injection,XML Config, ExampleServiceBean", serviceBean.getAnotherSampleDAO());
assertTrue("Failed: Constructor Injection,String Property , XML Config", serviceBean.getPropertyX()
.equals("Some Service Property X"));
}
@Test
public void testPropertyInjectioninDAOByXML() {
ExampleDAOBean daoBean = beanInjectedContext.getBean(ExampleDAOBean.class);
assertTrue("Failed: Constructor Injection,String Property , XML Config", daoBean.getPropertyX()
.equals("Mandatory DAO Property X"));
AnotherSampleDAOBean anotherDAOBean = beanInjectedContext.getBean(AnotherSampleDAOBean.class);
assertTrue("Failed: Constructor Injection,String Property , XML Config", anotherDAOBean.getPropertyY()
.equals("Mandatory DAO Property Y"));
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
// TODO Auto-generated method stub
this.beanInjectedContext = applicationContext;
}
}

View File

@ -0,0 +1,10 @@
package org.baeldung.persistence;
import org.springframework.data.jpa.domain.Specification;
public interface IEnhancedSpecification<T> extends Specification<T> {
default boolean isOfLowPrecedence() {
return false;
}
}

View File

@ -0,0 +1,64 @@
package org.baeldung.persistence.dao;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.baeldung.web.util.SearchOperation;
import org.baeldung.web.util.SpecSearchCriteria;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications;
public class GenericSpecificationsBuilder {
private final List<SpecSearchCriteria> params;
public GenericSpecificationsBuilder() {
this.params = new ArrayList<>();
}
public final GenericSpecificationsBuilder 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 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 <U> Specification<U> build(Function<SpecSearchCriteria, Specification<U>> converter) {
if (params.size() == 0)
return null;
params.sort((spec0, spec1) -> Boolean.compare(spec0.isLowPrecedence(), spec1.isLowPrecedence()));
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).isLowPrecedence()? Specifications.where(result).or(specs.get(idx)): Specifications.where(result).and(specs.get(idx));
}
return result;
}
}

View File

@ -11,39 +11,39 @@ import org.springframework.data.jpa.domain.Specification;
public class UserSpecification implements Specification<User> { public class UserSpecification implements Specification<User> {
private SpecSearchCriteria criteria; private SpecSearchCriteria criteria;
public UserSpecification(final SpecSearchCriteria criteria) { public UserSpecification(final SpecSearchCriteria criteria) {
super(); super();
this.criteria = criteria; this.criteria = criteria;
} }
public SpecSearchCriteria getCriteria() { public SpecSearchCriteria getCriteria() {
return criteria; return criteria;
} }
@Override @Override
public Predicate toPredicate(final Root<User> root, final CriteriaQuery<?> query, final CriteriaBuilder builder) { public Predicate toPredicate(final Root<User> root, final CriteriaQuery<?> query, final CriteriaBuilder builder) {
switch (criteria.getOperation()) { switch (criteria.getOperation()) {
case EQUALITY: case EQUALITY:
return builder.equal(root.get(criteria.getKey()), criteria.getValue()); return builder.equal(root.get(criteria.getKey()), criteria.getValue());
case NEGATION: case NEGATION:
return builder.notEqual(root.get(criteria.getKey()), criteria.getValue()); return builder.notEqual(root.get(criteria.getKey()), criteria.getValue());
case GREATER_THAN: case GREATER_THAN:
return builder.greaterThan(root.<String> get(criteria.getKey()), criteria.getValue().toString()); return builder.greaterThan(root.<String> get(criteria.getKey()), criteria.getValue().toString());
case LESS_THAN: case LESS_THAN:
return builder.lessThan(root.<String> get(criteria.getKey()), criteria.getValue().toString()); return builder.lessThan(root.<String> get(criteria.getKey()), criteria.getValue().toString());
case LIKE: case LIKE:
return builder.like(root.<String> get(criteria.getKey()), criteria.getValue().toString()); return builder.like(root.<String> get(criteria.getKey()), criteria.getValue().toString());
case STARTS_WITH: case STARTS_WITH:
return builder.like(root.<String> get(criteria.getKey()), criteria.getValue() + "%"); return builder.like(root.<String> get(criteria.getKey()), criteria.getValue() + "%");
case ENDS_WITH: case ENDS_WITH:
return builder.like(root.<String> get(criteria.getKey()), "%" + criteria.getValue()); return builder.like(root.<String> get(criteria.getKey()), "%" + criteria.getValue());
case CONTAINS: case CONTAINS:
return builder.like(root.<String> get(criteria.getKey()), "%" + criteria.getValue() + "%"); return builder.like(root.<String> get(criteria.getKey()), "%" + criteria.getValue() + "%");
default: default:
return null; return null;
} }
} }
} }

View File

@ -1,14 +1,14 @@
package org.baeldung.persistence.dao; package org.baeldung.persistence.dao;
import java.util.ArrayList;
import java.util.List;
import org.baeldung.persistence.model.User; import org.baeldung.persistence.model.User;
import org.baeldung.web.util.SearchOperation; import org.baeldung.web.util.SearchOperation;
import org.baeldung.web.util.SpecSearchCriteria; import org.baeldung.web.util.SpecSearchCriteria;
import org.springframework.data.jpa.domain.Specification; import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.domain.Specifications; import org.springframework.data.jpa.domain.Specifications;
import java.util.ArrayList;
import java.util.List;
public final class UserSpecificationsBuilder { public final class UserSpecificationsBuilder {
private final List<SpecSearchCriteria> params; private final List<SpecSearchCriteria> params;
@ -20,12 +20,15 @@ public final class UserSpecificationsBuilder {
// 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);
}
public final UserSpecificationsBuilder 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.contains("*"); final boolean endWithAsterisk = suffix != null && suffix.contains(SearchOperation.ZERO_OR_MORE_REGEX);
final boolean endWithAsterisk = suffix.contains("*");
if (startWithAsterisk && endWithAsterisk) { if (startWithAsterisk && endWithAsterisk) {
op = SearchOperation.CONTAINS; op = SearchOperation.CONTAINS;
@ -35,26 +38,37 @@ public final class UserSpecificationsBuilder {
op = SearchOperation.STARTS_WITH; op = SearchOperation.STARTS_WITH;
} }
} }
params.add(new SpecSearchCriteria(key, op, value)); params.add(new SpecSearchCriteria(precedenceIndicator, 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;
params.sort((spec0, spec1) -> {
return Boolean.compare(spec0.isLowPrecedence(), spec1.isLowPrecedence());
});
Specification<User> result = new UserSpecification(params.get(0));
for (int i = 1; i < params.size(); i++) {
result = params.get(i).isLowPrecedence() ? Specifications.where(result).or(new UserSpecification(params.get(i))) : Specifications.where(result).and(new UserSpecification(params.get(i)));
} }
final List<Specification<User>> specs = new ArrayList<Specification<User>>();
for (final SpecSearchCriteria param : params) {
specs.add(new UserSpecification(param));
}
Specification<User> result = specs.get(0);
for (int i = 1; i < specs.size(); i++) {
result = Specifications.where(result).and(specs.get(i));
}
return result; 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;
}
} }

View File

@ -1,15 +1,12 @@
package org.baeldung.web.controller; package org.baeldung.web.controller;
import java.util.ArrayList; import com.google.common.base.Joiner;
import java.util.List; import com.google.common.base.Preconditions;
import java.util.regex.Matcher; import com.querydsl.core.types.Predicate;
import java.util.regex.Pattern; import com.querydsl.core.types.dsl.BooleanExpression;
import cz.jirutka.rsql.parser.RSQLParser;
import org.baeldung.persistence.dao.IUserDAO; import cz.jirutka.rsql.parser.ast.Node;
import org.baeldung.persistence.dao.MyUserPredicatesBuilder; import org.baeldung.persistence.dao.*;
import org.baeldung.persistence.dao.MyUserRepository;
import org.baeldung.persistence.dao.UserRepository;
import org.baeldung.persistence.dao.UserSpecificationsBuilder;
import org.baeldung.persistence.dao.rsql.CustomRsqlVisitor; import org.baeldung.persistence.dao.rsql.CustomRsqlVisitor;
import org.baeldung.persistence.model.MyUser; import org.baeldung.persistence.model.MyUser;
import org.baeldung.persistence.model.User; import org.baeldung.persistence.model.User;
@ -20,20 +17,12 @@ 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.RequestBody; import org.springframework.web.bind.annotation.*;
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.google.common.base.Joiner; import java.util.ArrayList;
import com.google.common.base.Preconditions; import java.util.List;
import com.querydsl.core.types.Predicate; import java.util.regex.Matcher;
import com.querydsl.core.types.dsl.BooleanExpression; import java.util.regex.Pattern;
import cz.jirutka.rsql.parser.RSQLParser;
import cz.jirutka.rsql.parser.ast.Node;
//@EnableSpringDataWebSupport //@EnableSpringDataWebSupport
@Controller @Controller
@ -84,6 +73,25 @@ public class UserController {
return dao.findAll(spec); return dao.findAll(spec);
} }
@RequestMapping(method = RequestMethod.GET, value = "/users/espec")
@ResponseBody
public List<User> findAllByOptionalSpecification(@RequestParam(value = "search") final String search) {
final Specification<User> spec = resolveSpecification(search);
return dao.findAll(spec);
}
protected Specification<User> resolveSpecification(String searchParameters) {
final UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
final String operationSetExper = Joiner.on("|").join(SearchOperation.SIMPLE_OPERATION_SET);
final Pattern pattern = Pattern.compile("(\\p{Punct}?)(\\w+?)(" + operationSetExper + ")(\\p{Punct}?)(\\w+?)(\\p{Punct}?),");
final 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") @RequestMapping(method = RequestMethod.GET, value = "/myusers")
@ResponseBody @ResponseBody
public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") final String search) { public Iterable<MyUser> findAllByQuerydsl(@RequestParam(value = "search") final String search) {

View File

@ -5,6 +5,10 @@ public enum SearchOperation {
public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" }; public static final String[] SIMPLE_OPERATION_SET = { ":", "!", ">", "<", "~" };
public static final String LOW_PRECEDENCE_INDICATOR="'";
public static final String ZERO_OR_MORE_REGEX="*";
public static SearchOperation getSimpleOperation(final char input) { public static SearchOperation getSimpleOperation(final char input) {
switch (input) { switch (input) {
case ':': case ':':

View File

@ -5,6 +5,7 @@ public class SpecSearchCriteria {
private String key; private String key;
private SearchOperation operation; private SearchOperation operation;
private Object value; private Object value;
private boolean lowPrecedence;
public SpecSearchCriteria() { public SpecSearchCriteria() {
@ -17,6 +18,14 @@ public class SpecSearchCriteria {
this.value = value; this.value = value;
} }
public SpecSearchCriteria(final String lowPrecedenceIndicator, final String key, final SearchOperation operation, final Object value) {
super();
this.lowPrecedence = lowPrecedenceIndicator != null && lowPrecedenceIndicator.equals(SearchOperation.LOW_PRECEDENCE_INDICATOR);
this.key = key;
this.operation = operation;
this.value = value;
}
public String getKey() { public String getKey() {
return key; return key;
} }
@ -41,4 +50,12 @@ public class SpecSearchCriteria {
this.value = value; this.value = value;
} }
public boolean isLowPrecedence() {
return lowPrecedence;
}
public void setLowPrecedence(boolean lowPrecedence) {
this.lowPrecedence = lowPrecedence;
}
} }

View File

@ -1,13 +1,9 @@
package org.baeldung.persistence.query; package org.baeldung.persistence.query;
import static org.hamcrest.MatcherAssert.assertThat; import org.baeldung.persistence.dao.GenericSpecificationsBuilder;
import static org.hamcrest.collection.IsIn.isIn;
import static org.hamcrest.core.IsNot.not;
import java.util.List;
import org.baeldung.persistence.dao.UserRepository; import org.baeldung.persistence.dao.UserRepository;
import org.baeldung.persistence.dao.UserSpecification; import org.baeldung.persistence.dao.UserSpecification;
import org.baeldung.persistence.dao.UserSpecificationsBuilder;
import org.baeldung.persistence.model.User; import org.baeldung.persistence.model.User;
import org.baeldung.spring.PersistenceConfig; import org.baeldung.spring.PersistenceConfig;
import org.baeldung.web.util.SearchOperation; import org.baeldung.web.util.SearchOperation;
@ -16,12 +12,21 @@ 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.Specifications; import org.springframework.data.jpa.domain.Specifications;
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 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) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = { PersistenceConfig.class }) @ContextConfiguration(classes = { PersistenceConfig.class })
@Transactional @Transactional
@ -35,6 +40,8 @@ public class JPASpecificationIntegrationTest {
private User userTom; private User userTom;
private User userPercy;
@Before @Before
public void init() { public void init() {
userJohn = new User(); userJohn = new User();
@ -50,6 +57,13 @@ public class JPASpecificationIntegrationTest {
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.setFirstName("percy");
userPercy.setLastName("blackney");
userPercy.setEmail("percy@blackney.com");
userPercy.setAge(30);
repository.save(userPercy);
} }
@Test @Test
@ -62,6 +76,33 @@ public class JPASpecificationIntegrationTest {
assertThat(userTom, not(isIn(results))); assertThat(userTom, not(isIn(results)));
} }
@Test
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
UserSpecificationsBuilder builder = new UserSpecificationsBuilder();
final SpecSearchCriteria spec = new SpecSearchCriteria("'", "firstName", SearchOperation.EQUALITY, "john");
final SpecSearchCriteria spec1 = new SpecSearchCriteria("lastName", SearchOperation.EQUALITY, "doe");
final List<User> results = repository.findAll(builder.with(spec1).with(spec).build());
assertThat(results, hasSize(2));
assertThat(userJohn, isIn(results));
assertThat(userTom, isIn(results));
}
@Test
public void givenFirstOrLastNameGenericBuilder_whenGettingListOfUsers_thenCorrect() {
GenericSpecificationsBuilder builder = new GenericSpecificationsBuilder();
Function<SpecSearchCriteria, Specification<User>> converter = UserSpecification::new;
builder.with("'", "firstName", ":", "john", null, null);
builder.with(null, "lastName", ":", "doe", null, null);
final List<User> results = repository.findAll(builder.build(converter));
assertThat(results, hasSize(2));
assertThat(userJohn, 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"));

View File

@ -1,19 +1,19 @@
package org.baeldung.persistence.query; package org.baeldung.persistence.query;
import static org.junit.Assert.assertFalse; import com.jayway.restassured.RestAssured;
import static org.junit.Assert.assertTrue; import com.jayway.restassured.response.Response;
import com.jayway.restassured.specification.RequestSpecification;
import org.baeldung.persistence.model.User; import org.baeldung.persistence.model.User;
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.jayway.restassured.RestAssured; import static org.junit.Assert.assertFalse;
import com.jayway.restassured.response.Response; import static org.junit.Assert.assertTrue;
import com.jayway.restassured.specification.RequestSpecification;
//@RunWith(SpringJUnit4ClassRunner.class) //@RunWith(SpringJUnit4ClassRunner.class)
//@ContextConfiguration(classes = { ConfigTest.class, PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class) //@ContextConfiguration(classes = { ConfigTest.class,
// PersistenceConfig.class }, loader = AnnotationConfigContextLoader.class)
@ActiveProfiles("test") @ActiveProfiles("test")
public class JPASpecificationLiveTest { public class JPASpecificationLiveTest {
@ -43,6 +43,16 @@ public class JPASpecificationLiveTest {
// repository.save(userTom); // repository.save(userTom);
} }
private final String EURL_PREFIX = "http://localhost:8082/spring-security-rest-full/auth/users/espec?search=";
@Test
public void givenFirstOrLastName_whenGettingListOfUsers_thenCorrect() {
final Response response = givenAuth().get(EURL_PREFIX + "'firstName:john,lastName:doe");
final String result = response.body().asString();
assertTrue(result.contains(userJohn.getEmail()));
assertTrue(result.contains(userTom.getEmail()));
}
@Test @Test
public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() { public void givenFirstAndLastName_whenGettingListOfUsers_thenCorrect() {
final Response response = givenAuth().get(URL_PREFIX + "firstName:john,lastName:doe"); final Response response = givenAuth().get(URL_PREFIX + "firstName:john,lastName:doe");