diff --git a/spring-data-rest-querydsl/pom.xml b/spring-data-rest-querydsl/pom.xml index f9d5d27773..3b31e1835c 100644 --- a/spring-data-rest-querydsl/pom.xml +++ b/spring-data-rest-querydsl/pom.xml @@ -1,14 +1,13 @@ - + 4.0.0 - com.vivelibre - VLCore - 2.0.31-SNAPSHOT + com.baeldung + spring-data-rest-querydsl + 1.0 - - VLCore - Vivelibre API - Microservices + spring-data-rest-querydsl org.springframework.boot @@ -16,27 +15,11 @@ 1.5.9.RELEASE - - - - org.springframework.cloud - spring-cloud-netflix - 1.3.4.RELEASE - pom - import - - - - org.springframework.boot spring-boot-starter-data-rest - - org.springframework.cloud - spring-cloud-starter-eureka - org.springframework.boot spring-boot-starter-web @@ -49,44 +32,6 @@ mysql mysql-connector-java - - org.mariadb.jdbc - mariadb-java-client - - - org.springframework.cloud - spring-cloud-starter-ribbon - - - org.springframework.cloud - spring-cloud-starter-hystrix - - - org.springframework.boot - spring-boot-autoconfigure - - - org.springframework.boot - spring-boot-actuator - - - org.springframework.boot - spring-boot-devtools - true - - - org.springframework.restdocs - spring-restdocs-mockmvc - test - - - org.springframework.data - spring-data-rest-hal-browser - - - org.springframework.boot - spring-boot-starter-hateoas - com.querydsl querydsl-apt @@ -95,62 +40,6 @@ com.querydsl querydsl-jpa - - org.slf4j - slf4j-log4j12 - 1.6.1 - - - junit - junit - test - - - io.rest-assured - rest-assured - 3.0.6 - test - - - javax.ws.rs - jsr311-api - 1.1.1 - - - com.google.code.gson - gson - 2.8.2 - - - org.springframework.boot - spring-boot-configuration-processor - true - - - org.apache.httpcomponents - httpcore - 4.4.8 - - - org.apache.httpcomponents - httpclient - 4.5.3 - - - com.google.guava - guava - 23.6-jre - - - net.sf.dozer - dozer - 5.5.1 - - - org.apache.xmlrpc - xmlrpc-client - 3.1.3 - org.springframework.boot spring-boot-starter-test @@ -203,34 +92,6 @@ - - maven-release-plugin - 2.5.1 - - - - scm:git:http://jcollado@192.169.170.109:7990/scm/viv/vivelibre-backendside-core-vlcore.git - http://192.169.170.109:7990/projects/VIV/repos/vivelibre-backendside-core-vlcore - scm:git:http://jcollado@192.169.170.109:7990/scm/viv/vivelibre-backendside-core-vlcore.git - - VLCore-2.0.28 - - - - - releases - http://192.169.170.109:3000/nexus/content/repositories/releases - - - - - - nexus-releases - nexus - http://192.169.170.109:3000/nexus/content/repositories/releases/ - - - diff --git a/spring-data-rest-querydsl/src/main/java/com/baeldung/Application.java b/spring-data-rest-querydsl/src/main/java/com/baeldung/Application.java index cae2d62f05..24e4cee057 100644 --- a/spring-data-rest-querydsl/src/main/java/com/baeldung/Application.java +++ b/spring-data-rest-querydsl/src/main/java/com/baeldung/Application.java @@ -1,23 +1,26 @@ package com.baeldung; +import com.baeldung.controller.repository.AddressAvailabilityRepository; import com.baeldung.controller.repository.AddressRepository; -import com.baeldung.controller.repository.PersonRepository; +import com.baeldung.controller.repository.UserRepository; import com.baeldung.entity.Address; -import com.baeldung.entity.Person; +import com.baeldung.entity.AddressAvailability; +import com.baeldung.entity.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.data.jpa.repository.config.EnableJpaRepositories; import javax.annotation.PostConstruct; -import java.util.UUID; @SpringBootApplication @EntityScan("com.baeldung.entity") @EnableJpaRepositories("com.baeldung.controller.repository") -public class Application { +@EnableAutoConfiguration public class Application { - @Autowired private PersonRepository personRepository; + @Autowired private UserRepository personRepository; @Autowired private AddressRepository addressRepository; + @Autowired private AddressAvailabilityRepository addressAvailabilityRepository; public static void main(String[] args) { SpringApplication.run(Application.class, args); @@ -25,15 +28,18 @@ public class Application { @PostConstruct private void initializeData() { // Create John - final Address johnsAddress = new Address(UUID.randomUUID().toString(), "Fake Street 1", "Fake Country"); - addressRepository.save(johnsAddress); - final Person john = new Person(UUID.randomUUID().toString(), "John", johnsAddress); + final AddressAvailability addressOneAvailability = new AddressAvailability(true, true, false, false, false, true, true); + addressAvailabilityRepository.save(addressOneAvailability); + final User john = new User("John"); personRepository.save(john); - + final Address addressOne = new Address("Fake Street 1", "Fake Country", addressOneAvailability, john); + addressRepository.save(addressOne); // Create Lisa - final Address lisasAddress = new Address(UUID.randomUUID().toString(), "Real Street 1", "Real Country"); - addressRepository.save(lisasAddress); - final Person lisa = new Person(UUID.randomUUID().toString(), "Lisa", lisasAddress); + final AddressAvailability addressTwoAvailability = new AddressAvailability(false, false, false, false, false, true, true); + addressAvailabilityRepository.save(addressTwoAvailability); + final User lisa = new User("Lisa"); personRepository.save(lisa); + final Address addressTwo = new Address("Real Street 1", "Real Country", addressTwoAvailability, lisa); + addressRepository.save(addressTwo); } } diff --git a/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/QueryController.java b/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/QueryController.java index 753250e638..4b3818e5f1 100644 --- a/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/QueryController.java +++ b/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/QueryController.java @@ -1,7 +1,11 @@ package com.baeldung.controller; -import com.baeldung.controller.repository.PersonRepository; -import com.baeldung.entity.Person; +import com.baeldung.controller.repository.AddressAvailabilityRepository; +import com.baeldung.controller.repository.AddressRepository; +import com.baeldung.controller.repository.UserRepository; +import com.baeldung.entity.Address; +import com.baeldung.entity.AddressAvailability; +import com.baeldung.entity.User; import com.querydsl.core.BooleanBuilder; import com.querydsl.core.types.Predicate; import org.springframework.beans.factory.annotation.Autowired; @@ -12,11 +16,26 @@ import org.springframework.web.bind.annotation.RestController; @RestController public class QueryController { - @Autowired private PersonRepository personRepository; + @Autowired private UserRepository personRepository; + @Autowired private AddressRepository addressRepository; + @Autowired private AddressAvailabilityRepository addressAvailabilityRepository; - @GetMapping(value = "/personQuery", produces = MediaType.APPLICATION_JSON_VALUE) - public Iterable getFilteredEvents(@QuerydslPredicate(root = Person.class) Predicate predicate) { + @GetMapping(value = "/users", produces = MediaType.APPLICATION_JSON_VALUE) + public Iterable queryOverUser(@QuerydslPredicate(root = User.class) Predicate predicate) { final BooleanBuilder builder = new BooleanBuilder(); return personRepository.findAll(builder.and(predicate)); } + + @GetMapping(value = "/addresses", produces = MediaType.APPLICATION_JSON_VALUE) + public Iterable
queryOverAddress(@QuerydslPredicate(root = Address.class) Predicate predicate) { + final BooleanBuilder builder = new BooleanBuilder(); + return addressRepository.findAll(builder.and(predicate)); + } + + @GetMapping(value = "/addressAvailabilities", produces = MediaType.APPLICATION_JSON_VALUE) + public Iterable queryOverAddressAvailability( + @QuerydslPredicate(root = AddressAvailability.class) Predicate predicate) { + final BooleanBuilder builder = new BooleanBuilder(); + return addressAvailabilityRepository.findAll(builder.and(predicate)); + } } diff --git a/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/AddressAvailabilityRepository.java b/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/AddressAvailabilityRepository.java new file mode 100644 index 0000000000..f396695d56 --- /dev/null +++ b/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/AddressAvailabilityRepository.java @@ -0,0 +1,19 @@ +package com.baeldung.controller.repository; + +import com.baeldung.entity.AddressAvailability; +import com.baeldung.entity.QAddressAvailability; +import com.querydsl.core.types.dsl.StringExpression; +import com.querydsl.core.types.dsl.StringPath; +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; +import org.springframework.data.querydsl.binding.SingleValueBinding; + +public interface AddressAvailabilityRepository + extends JpaRepository, QueryDslPredicateExecutor, + QuerydslBinderCustomizer { + @Override default void customize(final QuerydslBindings bindings, final QAddressAvailability root) { + bindings.bind(String.class).first((SingleValueBinding) StringExpression::eq); + } +} diff --git a/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/PersonRepository.java b/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/UserRepository.java similarity index 72% rename from spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/PersonRepository.java rename to spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/UserRepository.java index 7c37fb6b15..13d33cdac6 100644 --- a/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/PersonRepository.java +++ b/spring-data-rest-querydsl/src/main/java/com/baeldung/controller/repository/UserRepository.java @@ -1,7 +1,7 @@ package com.baeldung.controller.repository; -import com.baeldung.entity.Person; -import com.baeldung.entity.QPerson; +import com.baeldung.entity.QUser; +import com.baeldung.entity.User; import com.querydsl.core.types.dsl.StringExpression; import com.querydsl.core.types.dsl.StringPath; import org.springframework.data.jpa.repository.JpaRepository; @@ -10,9 +10,9 @@ import org.springframework.data.querydsl.binding.QuerydslBinderCustomizer; import org.springframework.data.querydsl.binding.QuerydslBindings; import org.springframework.data.querydsl.binding.SingleValueBinding; -public interface PersonRepository - extends JpaRepository, QueryDslPredicateExecutor, QuerydslBinderCustomizer { - @Override default void customize(final QuerydslBindings bindings, final QPerson root) { +public interface UserRepository + extends JpaRepository, QueryDslPredicateExecutor, QuerydslBinderCustomizer { + @Override default void customize(final QuerydslBindings bindings, final QUser root) { bindings.bind(String.class).first((SingleValueBinding) StringExpression::eq); } } diff --git a/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/Address.java b/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/Address.java index 4f92031473..5d37431ea3 100644 --- a/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/Address.java +++ b/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/Address.java @@ -1,29 +1,36 @@ package com.baeldung.entity; -import javax.persistence.Entity; -import javax.persistence.Id; +import com.fasterxml.jackson.annotation.JsonBackReference; +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; -@Entity public class Address { +import javax.persistence.*; - @Id private String id; - private String address; - private String country; +@Entity @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) public class Address { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, nullable = false) private Long id; + @Column private String address; + @Column private String country; + @OneToOne(fetch = FetchType.LAZY) @JoinColumn(name = "address_id") @JsonBackReference private AddressAvailability + addressAvailability; + @ManyToOne(fetch = FetchType.LAZY) @JoinColumn(name = "user_id") @JsonBackReference private User user; public Address() { } - public Address(String id, String address, String country) { + public Address(String address, String country, AddressAvailability addressAvailability, User user) { this.id = id; this.address = address; this.country = country; + this.addressAvailability = addressAvailability; + this.user = user; } - public String getId() { + public Long getId() { return id; } - public void setId(String id) { + public void setId(Long id) { this.id = id; } @@ -42,4 +49,12 @@ import javax.persistence.Id; public void setCountry(String country) { this.country = country; } + + public AddressAvailability getAddressAvailability() { + return addressAvailability; + } + + public void setAddressAvailability(AddressAvailability addressAvailability) { + this.addressAvailability = addressAvailability; + } } diff --git a/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/AddressAvailability.java b/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/AddressAvailability.java new file mode 100644 index 0000000000..c731c72636 --- /dev/null +++ b/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/AddressAvailability.java @@ -0,0 +1,114 @@ +package com.baeldung.entity; + + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonManagedReference; + +import javax.persistence.*; + +@Table(name = "address_availability") @Entity @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) +public class AddressAvailability { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, nullable = false) private Long id; + @Column private Boolean monday; + @Column private Boolean tuesday; + @Column private Boolean wednesday; + @Column private Boolean thursday; + @Column private Boolean friday; + @Column private Boolean saturday; + @Column private Boolean sunday; + @OneToOne(mappedBy = "addressAvailability") @JsonManagedReference private Address address; + + public AddressAvailability() { + } + + public AddressAvailability(Boolean monday, Boolean tuesday, Boolean wednesday, Boolean thursday, Boolean friday, + Boolean saturday, Boolean sunday) { + this.monday = monday; + this.tuesday = tuesday; + this.wednesday = wednesday; + this.thursday = thursday; + this.friday = friday; + this.saturday = saturday; + this.sunday = sunday; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + + public Boolean getMonday() { + return monday; + } + + public void setMonday(Boolean monday) { + this.monday = monday; + } + + + public Boolean getTuesday() { + return tuesday; + } + + public void setTuesday(Boolean tuesday) { + this.tuesday = tuesday; + } + + + public Boolean getWednesday() { + return wednesday; + } + + public void setWednesday(Boolean wednesday) { + this.wednesday = wednesday; + } + + + public Boolean getThursday() { + return thursday; + } + + public void setThursday(Boolean thursday) { + this.thursday = thursday; + } + + + public Boolean getFriday() { + return friday; + } + + public void setFriday(Boolean friday) { + this.friday = friday; + } + + + public Boolean getSaturday() { + return saturday; + } + + public void setSaturday(Boolean saturday) { + this.saturday = saturday; + } + + + public Boolean getSunday() { + return sunday; + } + + public void setSunday(Boolean sunday) { + this.sunday = sunday; + } + + public Address getAddress() { + return address; + } + + public void setAddress(Address address) { + this.address = address; + } +} diff --git a/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/Person.java b/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/Person.java deleted file mode 100644 index 779b1f84a9..0000000000 --- a/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/Person.java +++ /dev/null @@ -1,48 +0,0 @@ -package com.baeldung.entity; - - -import javax.persistence.Entity; -import javax.persistence.Id; -import javax.persistence.JoinColumn; -import javax.persistence.OneToOne; - - -@Entity public class Person { - - @Id private String id; - private String name; - @OneToOne @JoinColumn(name = "address") private com.baeldung.entity.Address address; - - public Person() { - } - - public Person(String id, String name, com.baeldung.entity.Address address) { - this.id = id; - this.name = name; - this.address = address; - } - - public String getId() { - return id; - } - - public void setId(String id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public com.baeldung.entity.Address getAddress() { - return address; - } - - public void setAddress(com.baeldung.entity.Address address) { - this.address = address; - } -} diff --git a/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/User.java b/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/User.java new file mode 100644 index 0000000000..adc6da724a --- /dev/null +++ b/spring-data-rest-querydsl/src/main/java/com/baeldung/entity/User.java @@ -0,0 +1,46 @@ +package com.baeldung.entity; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; +import com.fasterxml.jackson.annotation.JsonManagedReference; + +import javax.persistence.*; +import java.util.List; + + +@Entity @JsonIgnoreProperties({"hibernateLazyInitializer", "handler"}) public class User { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id", unique = true, nullable = false) private Long id; + @Column private String name; + @OneToMany(fetch = FetchType.LAZY, mappedBy = "user") @JsonManagedReference private List
addresses; + + public User() { + } + + public User(String name) { + this.name = name; + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public List
getAddresses() { + return addresses; + } + + public void setAddresses(List
addresses) { + this.addresses = addresses; + } +} diff --git a/spring-data-rest-querydsl/src/main/resources/application.yml b/spring-data-rest-querydsl/src/main/resources/application.yml index c04c9d9ca0..fe9dae345d 100644 --- a/spring-data-rest-querydsl/src/main/resources/application.yml +++ b/spring-data-rest-querydsl/src/main/resources/application.yml @@ -2,12 +2,10 @@ spring: datasource: driver-class-name: com.mysql.jdbc.Driver hikari: - connection-timeout: 60000 - maximum-pool-size: 5 pool-name: hikari-pool url: jdbc:mysql://localhost:3306/baeldung?verifyServerCertificate=false&useSSL=false&requireSSL=false username: root password: admin jpa: hibernate: - ddl-auto: create-drop + ddl-auto: create diff --git a/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/IntegrationTest.java b/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/IntegrationTest.java deleted file mode 100644 index 63d0f56651..0000000000 --- a/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/IntegrationTest.java +++ /dev/null @@ -1,66 +0,0 @@ -package com.baeldung.springdatarestquerydsl; - -import com.baeldung.Application; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.http.MediaType; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import org.springframework.test.context.web.WebAppConfiguration; -import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.setup.MockMvcBuilders; -import org.springframework.web.context.WebApplicationContext; - -import java.nio.charset.Charset; - -import static org.hamcrest.Matchers.hasSize; -import static org.hamcrest.Matchers.is; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; - -@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) @WebAppConfiguration -public class IntegrationTest { - - final MediaType contentType = - new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); - - @Autowired private WebApplicationContext webApplicationContext; - - private MockMvc mockMvc; - - @Before public void setupMockMvc() { - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); - } - - @Test public void givenRequestHasBeenMade_whenQueryOverNameAttribute_thenGetJohn() throws Exception { - // Get John - mockMvc.perform(get("/personQuery?name=John")).andExpect(status().isOk()).andExpect(content().contentType(contentType)) - .andExpect(jsonPath("$", hasSize(1))).andExpect(jsonPath("$[0].name", is("John"))) - .andExpect(jsonPath("$[0].address.address", is("Fake Street 1"))) - .andExpect(jsonPath("$[0].address.country", is("Fake Country"))); - } - - @Test public void givenRequestHasBeenMade_whenQueryOverNameAttribute_thenGetLisa() throws Exception { - // Get Lisa - mockMvc.perform(get("/personQuery?name=Lisa")).andExpect(status().isOk()).andExpect(content().contentType(contentType)) - .andExpect(jsonPath("$", hasSize(1))).andExpect(jsonPath("$[0].name", is("Lisa"))) - .andExpect(jsonPath("$[0].address.address", is("Real Street 1"))) - .andExpect(jsonPath("$[0].address.country", is("Real Country"))); - } - - @Test public void givenRequestHasBeenMade_whenQueryWithoutAttribute_thenCorrect() throws Exception { - final MediaType contentType = new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), - Charset.forName("utf8")); - - mockMvc.perform(get("/personQuery")).andExpect(status().isOk()).andExpect(content().contentType(contentType)) - .andExpect(jsonPath("$", hasSize(2))) - // Get John - .andExpect(jsonPath("$[0].name", is("John"))).andExpect(jsonPath("$[0].address.address", is("Fake Street 1"))) - .andExpect(jsonPath("$[0].address.country", is("Fake Country"))) - // Get Lisa - .andExpect(jsonPath("$[1].name", is("Lisa"))).andExpect(jsonPath("$[1].address.address", is("Real Street 1"))) - .andExpect(jsonPath("$[1].address.country", is("Real Country"))); - } -} diff --git a/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/QuerydslIntegrationTest.java b/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/QuerydslIntegrationTest.java new file mode 100644 index 0000000000..ae972bcfcf --- /dev/null +++ b/spring-data-rest-querydsl/src/test/java/com/baeldung/springdatarestquerydsl/QuerydslIntegrationTest.java @@ -0,0 +1,52 @@ +package com.baeldung.springdatarestquerydsl; + +import com.baeldung.Application; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; +import org.springframework.test.context.web.WebAppConfiguration; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.setup.MockMvcBuilders; +import org.springframework.web.context.WebApplicationContext; + +import java.nio.charset.Charset; + +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.is; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*; + +@RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) @WebAppConfiguration +public class QuerydslIntegrationTest { + + final MediaType contentType = + new MediaType(MediaType.APPLICATION_JSON.getType(), MediaType.APPLICATION_JSON.getSubtype(), Charset.forName("utf8")); + + @Autowired private WebApplicationContext webApplicationContext; + + private MockMvc mockMvc; + + @Before public void setupMockMvc() { + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); + } + + @Test public void givenRequestHasBeenMade_whenQueryAddressFilteringByUserName_thenGetJohn() throws Exception { + mockMvc.perform(get("/addresses?user.name=John")).andExpect(status().isOk()).andExpect(content().contentType(contentType)) + .andExpect(jsonPath("$", hasSize(1))).andExpect(jsonPath("$[0].address", is("Fake Street 1"))) + .andExpect(jsonPath("$[0].country", is("Fake Country"))); + } + + @Test public void givenRequestHasBeenMade_whenQueryOverAddressAvailabilityFilteringByAddressCountry_thenGetAvailability() + throws Exception { + mockMvc.perform(get("/addressAvailabilities?address.country=Real Country")).andExpect(status().isOk()) + .andExpect(content().contentType(contentType)).andExpect(jsonPath("$", hasSize(1))) + .andExpect(jsonPath("$[0].monday", is(false))).andExpect(jsonPath("$[0].tuesday", is(false))) + .andExpect(jsonPath("$[0].wednesday", is(false))).andExpect(jsonPath("$[0].thursday", is(false))) + .andExpect(jsonPath("$[0].friday", is(false))).andExpect(jsonPath("$[0].saturday", is(true))) + .andExpect(jsonPath("$[0].sunday", is(true))); + } +}