From becfced8a8eb278bb9aebfb7e8a89518eddc63a8 Mon Sep 17 00:00:00 2001 From: DOHA Date: Wed, 18 Nov 2015 13:08:50 +0200 Subject: [PATCH] querydsl web support --- spring-security-rest-full/pom.xml | 9 ++- .../persistence/dao/MyUserRepository.java | 12 ++- .../baeldung/persistence/model/MyUser.java | 10 +++ .../web/controller/UserController.java | 16 +++- .../web/metric/ActuatorMetricService.java | 3 +- .../src/main/resources/webSecurityConfig.xml | 2 + .../java/org/baeldung/web/MyUserLiveTest.java | 77 +++++++++++++++++++ 7 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 spring-security-rest-full/src/test/java/org/baeldung/web/MyUserLiveTest.java diff --git a/spring-security-rest-full/pom.xml b/spring-security-rest-full/pom.xml index f5c86a816e..04ade99195 100644 --- a/spring-security-rest-full/pom.xml +++ b/spring-security-rest-full/pom.xml @@ -10,7 +10,7 @@ org.springframework.boot spring-boot-starter-parent - 1.2.6.RELEASE + 1.3.0.RELEASE @@ -92,6 +92,13 @@ org.springframework spring-webmvc + + + org.springframework.data + spring-data-commons + + + diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserRepository.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserRepository.java index 795f2c3c6c..db3627817a 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserRepository.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/dao/MyUserRepository.java @@ -1,9 +1,19 @@ package org.baeldung.persistence.dao; import org.baeldung.persistence.model.MyUser; +import org.baeldung.persistence.model.QMyUser; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.querydsl.QueryDslPredicateExecutor; +import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer; +import org.springframework.data.querydsl.binding.QuerydslBindings; -public interface MyUserRepository extends JpaRepository, QueryDslPredicateExecutor { +import com.mysema.query.types.path.StringPath; + +public interface MyUserRepository extends JpaRepository, QueryDslPredicateExecutor, QuerydslBinderCustomizer { + @Override + default public void customize(final QuerydslBindings bindings, final QMyUser root) { + bindings.bind(String.class).first((final StringPath path, final String value) -> path.containsIgnoreCase(value)); + bindings.excluding(root.email); + } } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/MyUser.java b/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/MyUser.java index 66fa90da9c..9a7bb4da3d 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/MyUser.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/persistence/model/MyUser.java @@ -25,6 +25,16 @@ public class MyUser { super(); } + public MyUser(final String firstName, final String lastName, final String email, final int age) { + super(); + this.firstName = firstName; + this.lastName = lastName; + this.email = email; + this.age = age; + } + + // + public Long getId() { return id; } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java index 08b2042182..7c28ed8e80 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/controller/UserController.java @@ -17,6 +17,7 @@ import org.baeldung.web.util.SearchCriteria; import org.baeldung.web.util.SearchOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.jpa.domain.Specification; +import org.springframework.data.querydsl.binding.QuerydslPredicate; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; @@ -28,11 +29,13 @@ import org.springframework.web.bind.annotation.ResponseStatus; import com.google.common.base.Joiner; import com.google.common.base.Preconditions; +import com.mysema.query.types.Predicate; import com.mysema.query.types.expr.BooleanExpression; import cz.jirutka.rsql.parser.RSQLParser; import cz.jirutka.rsql.parser.ast.Node; +//@EnableSpringDataWebSupport @Controller public class UserController { @@ -43,7 +46,7 @@ public class UserController { private UserRepository dao; @Autowired - private MyUserRepository mydao; + private MyUserRepository myUserRepository; public UserController() { super(); @@ -92,7 +95,7 @@ public class UserController { } } final BooleanExpression exp = builder.build(); - return mydao.findAll(exp); + return myUserRepository.findAll(exp); } @RequestMapping(method = RequestMethod.GET, value = "/users/rsql") @@ -103,6 +106,12 @@ public class UserController { return dao.findAll(spec); } + @RequestMapping(method = RequestMethod.GET, value = "/api/myusers") + @ResponseBody + public Iterable findAllByWebQuerydsl(@QuerydslPredicate(root = MyUser.class) final Predicate predicate) { + return myUserRepository.findAll(predicate); + } + // API - WRITE @RequestMapping(method = RequestMethod.POST, value = "/users") @@ -116,7 +125,8 @@ public class UserController { @ResponseStatus(HttpStatus.CREATED) public void addMyUser(@RequestBody final MyUser resource) { Preconditions.checkNotNull(resource); - mydao.save(resource); + myUserRepository.save(resource); + } } diff --git a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java index ea28c1e5f4..3d13fb5b59 100644 --- a/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java +++ b/spring-security-rest-full/src/main/java/org/baeldung/web/metric/ActuatorMetricService.java @@ -8,6 +8,7 @@ import java.util.List; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.metrics.CounterService; import org.springframework.boot.actuate.metrics.Metric; +import org.springframework.boot.actuate.metrics.repository.InMemoryMetricRepository; import org.springframework.boot.actuate.metrics.repository.MetricRepository; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -15,7 +16,6 @@ import org.springframework.stereotype.Service; @Service public class ActuatorMetricService implements IActuatorMetricService { - @Autowired private MetricRepository repo; @Autowired @@ -27,6 +27,7 @@ public class ActuatorMetricService implements IActuatorMetricService { public ActuatorMetricService() { super(); + repo = new InMemoryMetricRepository(); statusMetric = new ArrayList>(); statusList = new ArrayList(); } diff --git a/spring-security-rest-full/src/main/resources/webSecurityConfig.xml b/spring-security-rest-full/src/main/resources/webSecurityConfig.xml index 4557430124..f8ab5e3813 100644 --- a/spring-security-rest-full/src/main/resources/webSecurityConfig.xml +++ b/spring-security-rest-full/src/main/resources/webSecurityConfig.xml @@ -14,6 +14,8 @@ + + diff --git a/spring-security-rest-full/src/test/java/org/baeldung/web/MyUserLiveTest.java b/spring-security-rest-full/src/test/java/org/baeldung/web/MyUserLiveTest.java new file mode 100644 index 0000000000..ea5f609677 --- /dev/null +++ b/spring-security-rest-full/src/test/java/org/baeldung/web/MyUserLiveTest.java @@ -0,0 +1,77 @@ +package org.baeldung.web; + +import static org.junit.Assert.assertEquals; + +import org.baeldung.persistence.model.MyUser; +import org.baeldung.spring.Application; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.jayway.restassured.RestAssured; +import com.jayway.restassured.response.Response; +import com.jayway.restassured.specification.RequestSpecification; + +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(classes = Application.class) +@WebAppConfiguration +public class MyUserLiveTest { + + private ObjectMapper mapper = new ObjectMapper(); + private MyUser userJohn = new MyUser("john", "doe", "john@test.com", 11); + private MyUser userTom = new MyUser("tom", "doe", "tom@test.com", 20); + + private static boolean setupDataCreated = false; + + @Before + public void setupData() throws JsonProcessingException { + if (!setupDataCreated) { + withRequestBody(givenAuth(), userJohn).post("http://localhost:8080/myusers"); + withRequestBody(givenAuth(), userTom).post("http://localhost:8080/myusers"); + setupDataCreated = true; + } + } + + @Test + public void whenGettingListOfUsers_thenCorrect() { + final Response response = givenAuth().get("http://localhost:8080/api/myusers"); + final MyUser[] result = response.as(MyUser[].class); + assertEquals(result.length, 2); + } + + @Test + public void givenFirstName_whenGettingListOfUsers_thenCorrect() { + final Response response = givenAuth().get("http://localhost:8080/api/myusers?firstName=john"); + final MyUser[] result = response.as(MyUser[].class); + assertEquals(result.length, 1); + assertEquals(result[0].getEmail(), userJohn.getEmail()); + } + + @Test + public void givenPartialLastName_whenGettingListOfUsers_thenCorrect() { + final Response response = givenAuth().get("http://localhost:8080/api/myusers?lastName=do"); + final MyUser[] result = response.as(MyUser[].class); + assertEquals(result.length, 2); + } + + @Test + public void givenEmail_whenGettingListOfUsers_thenIgnored() { + final Response response = givenAuth().get("http://localhost:8080/api/myusers?email=john"); + final MyUser[] result = response.as(MyUser[].class); + assertEquals(result.length, 2); + } + + private RequestSpecification givenAuth() { + return RestAssured.given().auth().preemptive().basic("user1", "user1Pass"); + } + + private RequestSpecification withRequestBody(final RequestSpecification req, final Object obj) throws JsonProcessingException { + return req.contentType(MediaType.APPLICATION_JSON_VALUE).body(mapper.writeValueAsString(obj)); + } +}