From 4f7b0d6a645625d115293133f48eb993a6d7b733 Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Sun, 10 May 2020 18:40:27 +0200 Subject: [PATCH 01/74] First commit --- persistence-modules/spring-data-jpa-5/pom.xml | 58 +++++++++++++++++++ .../main/java/com/baeldung/Application.java | 58 +++++++++++++++++++ .../java/com/baeldung/model/Customer.java | 27 +++++++++ .../java/com/baeldung/model/CustomerDto.java | 34 +++++++++++ .../repository/CustomerRepository.java | 13 +++++ .../com/baeldung/service/CustomerService.java | 50 ++++++++++++++++ .../com/baeldung/util/CustomerMapper.java | 15 +++++ 7 files changed, 255 insertions(+) create mode 100644 persistence-modules/spring-data-jpa-5/pom.xml create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java diff --git a/persistence-modules/spring-data-jpa-5/pom.xml b/persistence-modules/spring-data-jpa-5/pom.xml new file mode 100644 index 0000000000..df1cc6c0c4 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + spring-data-jpa-5 + spring-data-jpa-5 + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + + + org.mapstruct + mapstruct-jdk8 + 1.3.1.Final + provided + + + org.springframework.boot + spring-boot-starter-cache + + + + + src/main/java + + + maven-compiler-plugin + 3.8.1 + + 1.8 + 1.8 + + + org.mapstruct + mapstruct-processor + 1.3.1.Final + + + + + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java new file mode 100644 index 0000000000..64d0a5e6c6 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java @@ -0,0 +1,58 @@ +package com.baeldung; + +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.ApplicationContext; +import org.springframework.context.annotation.Bean; + +import com.baeldung.model.Customer; +import com.baeldung.model.CustomerDto; +import com.baeldung.service.CustomerService; + +@SpringBootApplication @EnableCaching +public class Application { + + @Autowired CustomerService service; + @Autowired CacheManager cacheManager; + private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Application.class); + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + + @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) throws Exception { + logger.info("-- BASIC LOAD AND SAVE --"); + basicLoadAndSave(); + logger.info("-- BASIC LOAD AND SAVE + MAPPER --"); + basicLoadAndSaveWithMapper(); + return null; + } + + private void basicLoadAndSave() { + Customer myCustomer = service.addCustomer("John", "Doe"); + logger.info("Insert -- " + myCustomer.toString()); + myCustomer = service.updateCustomer(myCustomer.id, "john@doe.com", "+00", "Route 66"); + logger.info("Update -- " + myCustomer.toString()); + } + + private void basicLoadAndSaveWithMapper() { + CustomerDto dto = new CustomerDto(); + dto.firstName = "Johnny"; + dto.lastName = "Doe"; + Customer entity = service.addCustomer(dto); + logger.info("Insert -- " + entity.toString()); + CustomerDto dto2 = new CustomerDto(); + dto2.id = entity.id; + dto2.address = "Mountain View"; + dto2.email = "doe@mail.com"; + dto2.phone = "+44"; + entity = service.updateCustomer(dto2); + logger.info("Update -- " + entity.toString()); + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java new file mode 100644 index 0000000000..91808e7971 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java @@ -0,0 +1,27 @@ +package com.baeldung.model; + +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class Customer { + + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + public long id; + public String firstName; + public String lastName; + public String address; + public String email; + public String phone; + //... + public String phone99; + + public Customer() {} + + @Override public String toString() { + return String.format("Customer %s %s, Address: %s, Email: %s, Phone: %s", + this.firstName, this.lastName, this.address, this.email, this.phone); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java new file mode 100644 index 0000000000..8f7803fc32 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java @@ -0,0 +1,34 @@ +package com.baeldung.model; + +public class CustomerDto { + public long id; + public String firstName; + public String lastName; + public String address; + public String email; + public String phone; + //... + public String phone99; + + public CustomerDto() {} + + public CustomerDto(Customer c) { + this.id = c.id; + this.firstName = c.firstName; + this.lastName = c.lastName; + this.address = c.address; + this.email = c.email; + this.phone = c.phone; + } + + public Customer convertToEntity() { + Customer c = new Customer(); + c.id = id; + c.firstName = firstName; + c.lastName = lastName; + c.address = address; + c.email = email; + c.phone = phone; + return c; + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java new file mode 100644 index 0000000000..dcde6c3b46 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java @@ -0,0 +1,13 @@ +package com.baeldung.repository; + +import org.springframework.cache.annotation.Cacheable; +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.model.Customer; + +@Repository +public interface CustomerRepository extends CrudRepository { + @Cacheable("customers") + Customer findById(long id); +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java new file mode 100644 index 0000000000..56afc9e80d --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java @@ -0,0 +1,50 @@ +package com.baeldung.service; + +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.model.Customer; +import com.baeldung.model.CustomerDto; +import com.baeldung.repository.CustomerRepository; +import com.baeldung.util.CustomerMapper; + +@Service @Transactional +public class CustomerService { + + @Autowired CustomerRepository repo; + @Autowired CustomerMapper mapper; + + public Customer addCustomer(String firstName, String lastName) { + Customer myCustomer = new Customer(); + myCustomer.firstName = firstName; + myCustomer.lastName = lastName; + repo.save(myCustomer); + return myCustomer; + } + + public Customer updateCustomer(long id, String email, String phone, String address) { + Customer myCustomer = repo.findById(id); + myCustomer.address = address; + myCustomer.email = email; + myCustomer.phone = phone; + repo.save(myCustomer); + return myCustomer; + } + + public Customer addCustomer(CustomerDto dto) { + Customer myCustomer = new Customer(); + mapper.updateCustomerFromDto(dto, myCustomer); + repo.save(myCustomer); + return myCustomer; + } + + public Customer updateCustomer(CustomerDto dto) { + Customer myCustomer = repo.findById(dto.id); + mapper.updateCustomerFromDto(dto, myCustomer); + repo.save(myCustomer); + return myCustomer; + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java new file mode 100644 index 0000000000..d4f264e33a --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java @@ -0,0 +1,15 @@ +package com.baeldung.util; + +import org.mapstruct.BeanMapping; +import org.mapstruct.Mapper; +import org.mapstruct.MappingTarget; +import org.mapstruct.NullValuePropertyMappingStrategy; + +import com.baeldung.model.Customer; +import com.baeldung.model.CustomerDto; + +@Mapper(componentModel = "spring") +public interface CustomerMapper { + @BeanMapping(nullValuePropertyMappingStrategy = NullValuePropertyMappingStrategy.IGNORE) + void updateCustomerFromDto(CustomerDto dto, @MappingTarget Customer entity); +} From 167655d659ea258532b4d0ea75cb6763452e2ca7 Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Wed, 13 May 2020 01:56:55 +0530 Subject: [PATCH 02/74] add bucket4j deps --- .../spring-boot-libraries/pom.xml | 213 ++++++++++-------- 1 file changed, 122 insertions(+), 91 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries/pom.xml b/spring-boot-modules/spring-boot-libraries/pom.xml index 090967d8a8..36b9ec17c9 100644 --- a/spring-boot-modules/spring-boot-libraries/pom.xml +++ b/spring-boot-modules/spring-boot-libraries/pom.xml @@ -87,7 +87,35 @@ javase ${zxing.version} - + + + com.github.vladimir-bukhtoyarov + bucket4j-core + ${bucket4j.version} + + + com.giffing.bucket4j.spring.boot.starter + bucket4j-spring-boot-starter + ${bucket4j-spring-boot-starter.version} + + + org.springframework.boot + spring-boot-starter-cache + + + javax.cache + cache-api + + + com.github.ben-manes.caffeine + caffeine + ${caffeine.version} + + + com.github.ben-manes.caffeine + jcache + ${caffeine.version} + @@ -97,109 +125,112 @@ - - spring-boot-libraries - - - src/main/resources - true - - + + spring-boot-libraries + + + src/main/resources + true + + - + - - org.apache.maven.plugins - maven-war-plugin - + + org.apache.maven.plugins + maven-war-plugin + - - pl.project13.maven - git-commit-id-plugin - ${git-commit-id-plugin.version} - - - get-the-git-infos - - revision - - initialize - - - validate-the-git-infos - - validateRevision - - package - - - - true - ${project.build.outputDirectory}/git.properties - - + + pl.project13.maven + git-commit-id-plugin + ${git-commit-id-plugin.version} + + + get-the-git-infos + + revision + + initialize + + + validate-the-git-infos + + validateRevision + + package + + + + true + ${project.build.outputDirectory}/git.properties + + - - - autoconfiguration - - - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - **/*LiveTest.java - **/*IntegrationTest.java - **/*IntTest.java - - - **/AutoconfigurationTest.java - - - - - - - json - - - - - - - + + + autoconfiguration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + **/*IntegrationTest.java + **/*IntTest.java + + + **/AutoconfigurationTest.java + + + + + + + json + + + + + + + - - + + com.baeldung.intro.App 8.5.11 2.4.1.Final 1.9.0 2.0.0 - 5.0.2 - 5.0.2 - 5.2.4 - 18.0 - 2.2.4 - 2.3.2 - 0.23.0 - 1.4.200 - 2.1.0 - 1.5-beta1 - 2.1 - 2.6.0 - 3.3.0 - + 5.0.2 + 5.0.2 + 5.2.4 + 18.0 + 2.2.4 + 2.3.2 + 0.23.0 + 1.4.200 + 2.1.0 + 1.5-beta1 + 2.1 + 2.6.0 + 3.3.0 + 4.10.0 + 0.2.0 + 2.8.2 + From e8c383d10f5b15bf741c7ba1e0dd9b66852317f6 Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Wed, 13 May 2020 01:58:05 +0530 Subject: [PATCH 03/74] area api impl --- .../controller/AreaCalculationController.java | 29 +++++++++++++++++++ .../com/baeldung/ratelimiting/dto/AreaV1.java | 20 +++++++++++++ .../dto/RectangleDimensionsV1.java | 15 ++++++++++ .../dto/TriangleDimensionsV1.java | 15 ++++++++++ 4 files changed, 79 insertions(+) create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/controller/AreaCalculationController.java create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/dto/AreaV1.java create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/dto/RectangleDimensionsV1.java create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/dto/TriangleDimensionsV1.java diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/controller/AreaCalculationController.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/controller/AreaCalculationController.java new file mode 100644 index 0000000000..f3fb63ebdd --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/controller/AreaCalculationController.java @@ -0,0 +1,29 @@ +package com.baeldung.ratelimiting.controller; + +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.ratelimiting.dto.AreaV1; +import com.baeldung.ratelimiting.dto.RectangleDimensionsV1; +import com.baeldung.ratelimiting.dto.TriangleDimensionsV1; + +@RestController +@RequestMapping(value = "/api/v1/area", consumes = MediaType.APPLICATION_JSON_VALUE) +class AreaCalculationController { + + @PostMapping(value = "/rectangle", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity rectangle(@RequestBody RectangleDimensionsV1 dimensions) { + + return ResponseEntity.ok(new AreaV1("rectangle", dimensions.getLength() * dimensions.getWidth())); + } + + @PostMapping(value = "/triangle", produces = MediaType.APPLICATION_JSON_VALUE) + public ResponseEntity triangle(@RequestBody TriangleDimensionsV1 dimensions) { + + return ResponseEntity.ok(new AreaV1("triangle", 0.5d * dimensions.getHeight() * dimensions.getBase())); + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/dto/AreaV1.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/dto/AreaV1.java new file mode 100644 index 0000000000..78097f55b2 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/dto/AreaV1.java @@ -0,0 +1,20 @@ +package com.baeldung.ratelimiting.dto; + +public class AreaV1 { + + private String shape; + private Double area; + + public AreaV1(String shape, Double area) { + this.area = area; + this.shape = shape; + } + + public Double getArea() { + return area; + } + + public String getShape() { + return shape; + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/dto/RectangleDimensionsV1.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/dto/RectangleDimensionsV1.java new file mode 100644 index 0000000000..e3c17e1ba7 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/dto/RectangleDimensionsV1.java @@ -0,0 +1,15 @@ +package com.baeldung.ratelimiting.dto; + +public class RectangleDimensionsV1 { + + private double length; + private double width; + + public double getLength() { + return length; + } + + public double getWidth() { + return width; + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/dto/TriangleDimensionsV1.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/dto/TriangleDimensionsV1.java new file mode 100644 index 0000000000..44c954bded --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/dto/TriangleDimensionsV1.java @@ -0,0 +1,15 @@ +package com.baeldung.ratelimiting.dto; + +public class TriangleDimensionsV1 { + + private double base; + private double height; + + public double getBase() { + return base; + } + + public double getHeight() { + return height; + } +} From 4e45b8f44ef8c0b5d0e8c6b3a18f6d07be7cf796 Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Wed, 13 May 2020 01:58:40 +0530 Subject: [PATCH 04/74] spring mvc using interceptor --- .../bucket4japp/Bucket4jRateLimitingApp.java | 29 +++++++++ .../bucket4japp/interceptor/PricingPlan.java | 48 ++++++++++++++ .../interceptor/RateLimitingInterceptor.java | 65 +++++++++++++++++++ .../ratelimiting/application-bucket4j.yml | 10 +++ 4 files changed, 152 insertions(+) create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitingApp.java create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/PricingPlan.java create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitingInterceptor.java create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j.yml diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitingApp.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitingApp.java new file mode 100644 index 0000000000..2a42448b35 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitingApp.java @@ -0,0 +1,29 @@ +package com.baeldung.ratelimiting.bucket4japp; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.web.servlet.config.annotation.InterceptorRegistry; +import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; + +import com.baeldung.ratelimiting.bucket4japp.interceptor.RateLimitingInterceptor; + +@SpringBootApplication(scanBasePackages = "com.baeldung.ratelimiting", exclude = { + DataSourceAutoConfiguration.class, + SecurityAutoConfiguration.class +}) +public class Bucket4jRateLimitingApp implements WebMvcConfigurer { + + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new RateLimitingInterceptor()) + .addPathPatterns("/api/v1/area/**"); + } + + public static void main(String[] args) { + new SpringApplicationBuilder(Bucket4jRateLimitingApp.class) + .properties("spring.config.location=classpath:ratelimiting/application-bucket4j.yml") + .run(args); + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/PricingPlan.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/PricingPlan.java new file mode 100644 index 0000000000..e2b3ccb6c6 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/PricingPlan.java @@ -0,0 +1,48 @@ +package com.baeldung.ratelimiting.bucket4japp.interceptor; + +import java.time.Duration; + +import io.github.bucket4j.Bandwidth; +import io.github.bucket4j.Refill; + +enum PricingPlan { + + FREE { + + @Override + Bandwidth getLimit() { + return Bandwidth.classic(20, Refill.intervally(20, Duration.ofHours(1))); + } + }, + + BASIC { + + @Override + Bandwidth getLimit() { + return Bandwidth.classic(40, Refill.intervally(40, Duration.ofHours(1))); + } + }, + + PROFESSIONAL { + + @Override + Bandwidth getLimit() { + return Bandwidth.classic(100, Refill.intervally(100, Duration.ofHours(1))); + } + }; + + abstract Bandwidth getLimit(); + + static PricingPlan resolvePlanFromApiKey(String apiKey) { + if (apiKey == null || apiKey.isEmpty()) { + return FREE; + + } else if (apiKey.startsWith("PX001-")) { + return PROFESSIONAL; + + } else if (apiKey.startsWith("BX001-")) { + return BASIC; + } + return FREE; + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitingInterceptor.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitingInterceptor.java new file mode 100644 index 0000000000..8aa8de531c --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitingInterceptor.java @@ -0,0 +1,65 @@ +package com.baeldung.ratelimiting.bucket4japp.interceptor; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.http.HttpStatus; +import org.springframework.web.servlet.HandlerInterceptor; + +import io.github.bucket4j.Bandwidth; +import io.github.bucket4j.Bucket; +import io.github.bucket4j.Bucket4j; +import io.github.bucket4j.ConsumptionProbe; + +public class RateLimitingInterceptor implements HandlerInterceptor { + + private static final String HEADER_API_KEY = "X-api-key"; + private static final String HEADER_LIMIT_REMAINING = "X-Rate-Limit-Remaining"; + private static final String HEADER_RETRY_AFTER = "X-Rate-Limit-Retry-After-Milliseconds"; + + private final Map cache = new ConcurrentHashMap<>(); + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + + String apiKey = request.getHeader(HEADER_API_KEY); + + if (apiKey == null || apiKey.isEmpty()) { + response.sendError(HttpStatus.BAD_REQUEST.value(), "Missing Header: " + HEADER_API_KEY); + return false; + } + + Bucket tokenBucket = cache.computeIfAbsent(apiKey, this::resolveBucket); + + ConsumptionProbe probe = tokenBucket.tryConsumeAndReturnRemaining(1); + + if (probe.isConsumed()) { + + response.addHeader(HEADER_LIMIT_REMAINING, String.valueOf(probe.getRemainingTokens())); + return true; + + } else { + + long waitForRefillMilli = probe.getNanosToWaitForRefill() % 1_000_000; + + response.sendError(HttpStatus.TOO_MANY_REQUESTS.value(), "You have exhausted your API Request Quota"); // 429 + response.addHeader(HEADER_RETRY_AFTER, String.valueOf(waitForRefillMilli)); + + return false; + } + } + + private Bucket resolveBucket(String apiKey) { + PricingPlan pricingPlan = PricingPlan.resolvePlanFromApiKey(apiKey); + return bucket(pricingPlan.getLimit()); + } + + private Bucket bucket(Bandwidth limit) { + return Bucket4j.builder() + .addLimit(limit) + .build(); + } +} \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j.yml b/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j.yml new file mode 100644 index 0000000000..1fb4d2cf12 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j.yml @@ -0,0 +1,10 @@ +server: + port: 9000 + +spring: + application: + name: bucket4j-api-rate-limiting-app + mvc: + throw-exception-if-no-handler-found: true + resources: + add-mappings: false From 1b5a64c4a49b7fc9017d487aaab79ad01713a39d Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Wed, 13 May 2020 01:59:03 +0530 Subject: [PATCH 05/74] bucket4j spring boot starter --- .../Bucket4jRateLimitingApp.java | 21 ++++++++++ .../application-bucket4j-starter.yml | 41 +++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jRateLimitingApp.java create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j-starter.yml diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jRateLimitingApp.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jRateLimitingApp.java new file mode 100644 index 0000000000..de2ab41bf0 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jRateLimitingApp.java @@ -0,0 +1,21 @@ +package com.baeldung.ratelimiting.bootstarterapp; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; +import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; +import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.cache.annotation.EnableCaching; + +@SpringBootApplication(scanBasePackages = "com.baeldung.ratelimiting", exclude = { + DataSourceAutoConfiguration.class, + SecurityAutoConfiguration.class, +}) +@EnableCaching +public class Bucket4jRateLimitingApp { + + public static void main(String[] args) { + new SpringApplicationBuilder(Bucket4jRateLimitingApp.class) + .properties("spring.config.location=classpath:ratelimiting/application-bucket4j-starter.yml") + .run(args); + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j-starter.yml b/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j-starter.yml new file mode 100644 index 0000000000..1c1337c611 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j-starter.yml @@ -0,0 +1,41 @@ +server: + port: 9001 + +spring: + application: + name: bucket4j-starter-api-rate-limiting-app + mvc: + throw-exception-if-no-handler-found: true + resources: + add-mappings: false + cache: + cache-names: + - rate-limiting-buckets + caffeine: + spec: maximumSize=100000,expireAfterAccess=3600s + +bucket4j: + enabled: true + filters: + - cache-name: rate-limiting-buckets + url: /api/v1/area.* + http-response-body: "{ \"status\": 429, \"error\": \"Too Many Requests\", \"message\": \"You have exhausted your API Request Quota\" }" + rate-limits: + - expression: "getHeader('X-api-key')" + execute-condition: "getHeader('X-api-key').startsWith('PX001-')" + bandwidths: + - capacity: 100 + time: 1 + unit: hours + - expression: "getHeader('X-api-key')" + execute-condition: "getHeader('X-api-key').startsWith('BX001-')" + bandwidths: + - capacity: 40 + time: 1 + unit: hours + - expression: "getHeader('X-api-key')" + bandwidths: + - capacity: 20 + time: 1 + unit: hours + \ No newline at end of file From 34bbce79950ca2808ee0307fc23c9c109f7b2437 Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Wed, 13 May 2020 23:19:54 +0530 Subject: [PATCH 06/74] refactor to move pricing plan logic out into a separate class --- ...tingApp.java => Bucket4jRateLimitApp.java} | 4 +-- ...tingApp.java => Bucket4jRateLimitApp.java} | 14 +++++--- ...rceptor.java => RateLimitInterceptor.java} | 32 +++++++------------ .../{interceptor => service}/PricingPlan.java | 4 +-- .../service/PricingPlanService.java | 32 +++++++++++++++++++ .../application-bucket4j-starter.yml | 7 ++-- .../ratelimiting/application-bucket4j.yml | 8 ++++- 7 files changed, 67 insertions(+), 34 deletions(-) rename spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bootstarterapp/{Bucket4jRateLimitingApp.java => Bucket4jRateLimitApp.java} (88%) rename spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/{Bucket4jRateLimitingApp.java => Bucket4jRateLimitApp.java} (73%) rename spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/{RateLimitingInterceptor.java => RateLimitInterceptor.java} (68%) rename spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/{interceptor => service}/PricingPlan.java (86%) create mode 100644 spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlanService.java diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jRateLimitingApp.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jRateLimitApp.java similarity index 88% rename from spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jRateLimitingApp.java rename to spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jRateLimitApp.java index de2ab41bf0..f16d347f85 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jRateLimitingApp.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jRateLimitApp.java @@ -11,10 +11,10 @@ import org.springframework.cache.annotation.EnableCaching; SecurityAutoConfiguration.class, }) @EnableCaching -public class Bucket4jRateLimitingApp { +public class Bucket4jRateLimitApp { public static void main(String[] args) { - new SpringApplicationBuilder(Bucket4jRateLimitingApp.class) + new SpringApplicationBuilder(Bucket4jRateLimitApp.class) .properties("spring.config.location=classpath:ratelimiting/application-bucket4j-starter.yml") .run(args); } diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitingApp.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitApp.java similarity index 73% rename from spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitingApp.java rename to spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitApp.java index 2a42448b35..bb179b9b38 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitingApp.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitApp.java @@ -1,28 +1,34 @@ package com.baeldung.ratelimiting.bucket4japp; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.context.annotation.Lazy; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import com.baeldung.ratelimiting.bucket4japp.interceptor.RateLimitingInterceptor; +import com.baeldung.ratelimiting.bucket4japp.interceptor.RateLimitInterceptor; @SpringBootApplication(scanBasePackages = "com.baeldung.ratelimiting", exclude = { DataSourceAutoConfiguration.class, SecurityAutoConfiguration.class }) -public class Bucket4jRateLimitingApp implements WebMvcConfigurer { +public class Bucket4jRateLimitApp implements WebMvcConfigurer { + + @Autowired + @Lazy + private RateLimitInterceptor interceptor; @Override public void addInterceptors(InterceptorRegistry registry) { - registry.addInterceptor(new RateLimitingInterceptor()) + registry.addInterceptor(interceptor) .addPathPatterns("/api/v1/area/**"); } public static void main(String[] args) { - new SpringApplicationBuilder(Bucket4jRateLimitingApp.class) + new SpringApplicationBuilder(Bucket4jRateLimitApp.class) .properties("spring.config.location=classpath:ratelimiting/application-bucket4j.yml") .run(args); } diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitingInterceptor.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitInterceptor.java similarity index 68% rename from spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitingInterceptor.java rename to spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitInterceptor.java index 8aa8de531c..c983251e56 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitingInterceptor.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitInterceptor.java @@ -1,38 +1,39 @@ package com.baeldung.ratelimiting.bucket4japp.interceptor; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; - import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; +import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; -import io.github.bucket4j.Bandwidth; +import com.baeldung.ratelimiting.bucket4japp.service.PricingPlanService; + import io.github.bucket4j.Bucket; -import io.github.bucket4j.Bucket4j; import io.github.bucket4j.ConsumptionProbe; -public class RateLimitingInterceptor implements HandlerInterceptor { +@Component +public class RateLimitInterceptor implements HandlerInterceptor { private static final String HEADER_API_KEY = "X-api-key"; private static final String HEADER_LIMIT_REMAINING = "X-Rate-Limit-Remaining"; private static final String HEADER_RETRY_AFTER = "X-Rate-Limit-Retry-After-Milliseconds"; - private final Map cache = new ConcurrentHashMap<>(); + @Autowired + private PricingPlanService pricingPlanService; @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - + String apiKey = request.getHeader(HEADER_API_KEY); if (apiKey == null || apiKey.isEmpty()) { response.sendError(HttpStatus.BAD_REQUEST.value(), "Missing Header: " + HEADER_API_KEY); return false; } - - Bucket tokenBucket = cache.computeIfAbsent(apiKey, this::resolveBucket); + + Bucket tokenBucket = pricingPlanService.resolveBucket(apiKey); ConsumptionProbe probe = tokenBucket.tryConsumeAndReturnRemaining(1); @@ -51,15 +52,4 @@ public class RateLimitingInterceptor implements HandlerInterceptor { return false; } } - - private Bucket resolveBucket(String apiKey) { - PricingPlan pricingPlan = PricingPlan.resolvePlanFromApiKey(apiKey); - return bucket(pricingPlan.getLimit()); - } - - private Bucket bucket(Bandwidth limit) { - return Bucket4j.builder() - .addLimit(limit) - .build(); - } } \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/PricingPlan.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java similarity index 86% rename from spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/PricingPlan.java rename to spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java index e2b3ccb6c6..85632abf0b 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/PricingPlan.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java @@ -1,4 +1,4 @@ -package com.baeldung.ratelimiting.bucket4japp.interceptor; +package com.baeldung.ratelimiting.bucket4japp.service; import java.time.Duration; @@ -11,7 +11,7 @@ enum PricingPlan { @Override Bandwidth getLimit() { - return Bandwidth.classic(20, Refill.intervally(20, Duration.ofHours(1))); + return Bandwidth.classic(2, Refill.intervally(2 , Duration.ofHours(1))); } }, diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlanService.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlanService.java new file mode 100644 index 0000000000..713f4a6e1a --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlanService.java @@ -0,0 +1,32 @@ +package com.baeldung.ratelimiting.bucket4japp.service; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +import org.springframework.stereotype.Service; + +import io.github.bucket4j.Bandwidth; +import io.github.bucket4j.Bucket; +import io.github.bucket4j.Bucket4j; + +@Service +public class PricingPlanService { + + private final Map cache = new ConcurrentHashMap<>(); + + // @Cacheable("rate-limit-buckets") + public Bucket resolveBucket(String apiKey) { + return cache.computeIfAbsent(apiKey, this::newBucket); + } + + private Bucket newBucket(String apiKey) { + PricingPlan pricingPlan = PricingPlan.resolvePlanFromApiKey(apiKey); + return bucket(pricingPlan.getLimit()); + } + + private Bucket bucket(Bandwidth limit) { + return Bucket4j.builder() + .addLimit(limit) + .build(); + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j-starter.yml b/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j-starter.yml index 1c1337c611..ecc9f22e0a 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j-starter.yml +++ b/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j-starter.yml @@ -3,21 +3,21 @@ server: spring: application: - name: bucket4j-starter-api-rate-limiting-app + name: bucket4j-starter-api-rate-limit-app mvc: throw-exception-if-no-handler-found: true resources: add-mappings: false cache: cache-names: - - rate-limiting-buckets + - rate-limit-buckets caffeine: spec: maximumSize=100000,expireAfterAccess=3600s bucket4j: enabled: true filters: - - cache-name: rate-limiting-buckets + - cache-name: rate-limit-buckets url: /api/v1/area.* http-response-body: "{ \"status\": 429, \"error\": \"Too Many Requests\", \"message\": \"You have exhausted your API Request Quota\" }" rate-limits: @@ -38,4 +38,3 @@ bucket4j: - capacity: 20 time: 1 unit: hours - \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j.yml b/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j.yml index 1fb4d2cf12..0cee593261 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j.yml +++ b/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j.yml @@ -3,8 +3,14 @@ server: spring: application: - name: bucket4j-api-rate-limiting-app + name: bucket4j-api-rate-limit-app mvc: throw-exception-if-no-handler-found: true resources: add-mappings: false + cache: + cache-names: + - rate-limit-buckets + caffeine: + spec: maximumSize=100000,expireAfterAccess=3600s + \ No newline at end of file From 911f840af59281685b6c6c95ad56a4a1777c3a9d Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Wed, 13 May 2020 23:24:55 +0530 Subject: [PATCH 07/74] some cleanup --- .../bucket4japp/service/PricingPlanService.java | 1 - .../main/resources/ratelimiting/application-bucket4j.yml | 6 ------ 2 files changed, 7 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlanService.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlanService.java index 713f4a6e1a..7d8a718601 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlanService.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlanService.java @@ -14,7 +14,6 @@ public class PricingPlanService { private final Map cache = new ConcurrentHashMap<>(); - // @Cacheable("rate-limit-buckets") public Bucket resolveBucket(String apiKey) { return cache.computeIfAbsent(apiKey, this::newBucket); } diff --git a/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j.yml b/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j.yml index 0cee593261..ae19622d9b 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j.yml +++ b/spring-boot-modules/spring-boot-libraries/src/main/resources/ratelimiting/application-bucket4j.yml @@ -8,9 +8,3 @@ spring: throw-exception-if-no-handler-found: true resources: add-mappings: false - cache: - cache-names: - - rate-limit-buckets - caffeine: - spec: maximumSize=100000,expireAfterAccess=3600s - \ No newline at end of file From f304437ed8959bf3d4d3c6440f64ba9afa5e8c38 Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Thu, 14 May 2020 02:26:46 +0530 Subject: [PATCH 08/74] retry after secomds --- .../bucket4japp/interceptor/RateLimitInterceptor.java | 6 +++--- .../ratelimiting/bucket4japp/service/PricingPlan.java | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitInterceptor.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitInterceptor.java index c983251e56..d919214983 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitInterceptor.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitInterceptor.java @@ -18,7 +18,7 @@ public class RateLimitInterceptor implements HandlerInterceptor { private static final String HEADER_API_KEY = "X-api-key"; private static final String HEADER_LIMIT_REMAINING = "X-Rate-Limit-Remaining"; - private static final String HEADER_RETRY_AFTER = "X-Rate-Limit-Retry-After-Milliseconds"; + private static final String HEADER_RETRY_AFTER = "X-Rate-Limit-Retry-After-Seconds"; @Autowired private PricingPlanService pricingPlanService; @@ -44,10 +44,10 @@ public class RateLimitInterceptor implements HandlerInterceptor { } else { - long waitForRefillMilli = probe.getNanosToWaitForRefill() % 1_000_000; + long waitForRefill = probe.getNanosToWaitForRefill() % 1_000_000_000; response.sendError(HttpStatus.TOO_MANY_REQUESTS.value(), "You have exhausted your API Request Quota"); // 429 - response.addHeader(HEADER_RETRY_AFTER, String.valueOf(waitForRefillMilli)); + response.addHeader(HEADER_RETRY_AFTER, String.valueOf(waitForRefill)); return false; } diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java index 85632abf0b..e8b5513e8b 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java @@ -11,7 +11,7 @@ enum PricingPlan { @Override Bandwidth getLimit() { - return Bandwidth.classic(2, Refill.intervally(2 , Duration.ofHours(1))); + return Bandwidth.classic(20, Refill.intervally(20, Duration.ofHours(1))); } }, From faf562e8210bb9e8594c02d28585cc7354ba8c48 Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Sat, 16 May 2020 01:34:34 +0530 Subject: [PATCH 09/74] add basic usage as test --- .../bucket4j/Bucket4jUsageTest.java | 82 +++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageTest.java diff --git a/spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageTest.java b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageTest.java new file mode 100644 index 0000000000..247e493324 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageTest.java @@ -0,0 +1,82 @@ +package com.baledung.ratelimiting.bucket4j; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import java.time.Duration; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.TimeUnit; + +import org.junit.jupiter.api.Test; + +import io.github.bucket4j.Bandwidth; +import io.github.bucket4j.Bucket; +import io.github.bucket4j.Bucket4j; +import io.github.bucket4j.Refill; + +public class Bucket4jUsageTest { + + @Test + public void givenBucketLimit_whenExceedLimit_thenConsumeReturnsFalse() { + Refill refill = Refill.intervally(10, Duration.ofMinutes(1)); + Bandwidth limit = Bandwidth.classic(10, refill); + Bucket bucket = Bucket4j.builder() + .addLimit(limit) + .build(); + + for (int i = 1; i <= 10; i++) { + assertTrue(bucket.tryConsume(1)); + } + assertFalse(bucket.tryConsume(1)); + } + + @Test + public void givenMultipletLimits_whenExceedSmallerLimit_thenConsumeReturnsFalse() { + Bucket bucket = Bucket4j.builder() + .addLimit(Bandwidth.classic(10, Refill.intervally(10, Duration.ofMinutes(1)))) + .addLimit(Bandwidth.classic(5, Refill.intervally(5, Duration.ofSeconds(20)))) + .build(); + + for (int i = 1; i <= 5; i++) { + assertTrue(bucket.tryConsume(1)); + } + assertFalse(bucket.tryConsume(1)); + } + + @Test + public void givenBucketLimit_whenThrottleRequests_thenConsumeReturnsTrue() throws InterruptedException { + Refill refill = Refill.intervally(1, Duration.ofSeconds(2)); + Bandwidth limit = Bandwidth.classic(1, refill); + Bucket bucket = Bucket4j.builder() + .addLimit(limit) + .build(); + + assertTrue(bucket.tryConsume(1)); + + ScheduledExecutorService executor = Executors.newScheduledThreadPool(1); + CountDownLatch latch = new CountDownLatch(1); + + executor.schedule(new AssertTryConsume(bucket, latch), 2, TimeUnit.SECONDS); + + latch.await(); + } + + static class AssertTryConsume implements Runnable { + + private Bucket bucket; + private CountDownLatch latch; + + AssertTryConsume(Bucket bucket, CountDownLatch latch) { + this.bucket = bucket; + this.latch = latch; + } + + @Override + public void run() { + assertTrue(bucket.tryConsume(1)); + latch.countDown(); + } + } +} From 8d79b7c5d6c58a6096c697d9da976c0bb9d0413b Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Sat, 16 May 2020 17:20:15 +0530 Subject: [PATCH 10/74] unit test renamed --- .../{Bucket4jUsageTest.java => Bucket4jUsageUnitTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/{Bucket4jUsageTest.java => Bucket4jUsageUnitTest.java} (98%) diff --git a/spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageTest.java b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageUnitTest.java similarity index 98% rename from spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageTest.java rename to spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageUnitTest.java index 247e493324..e6b774034e 100644 --- a/spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageTest.java +++ b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageUnitTest.java @@ -16,7 +16,7 @@ import io.github.bucket4j.Bucket; import io.github.bucket4j.Bucket4j; import io.github.bucket4j.Refill; -public class Bucket4jUsageTest { +public class Bucket4jUsageUnitTest { @Test public void givenBucketLimit_whenExceedLimit_thenConsumeReturnsFalse() { From 9ea4e4f04ef90d3267f0c2ab7dd6f8431f6a03e1 Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Sat, 16 May 2020 17:56:00 +0200 Subject: [PATCH 11/74] Simplified + refactored --- .../main/java/com/baeldung/Application.java | 14 ++++------- .../java/com/baeldung/model/Customer.java | 9 +++----- .../java/com/baeldung/model/CustomerDto.java | 23 +++++++------------ .../com/baeldung/service/CustomerService.java | 9 +++----- 4 files changed, 19 insertions(+), 36 deletions(-) diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java index 64d0a5e6c6..34e86fe135 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java @@ -34,22 +34,18 @@ public class Application { } private void basicLoadAndSave() { - Customer myCustomer = service.addCustomer("John", "Doe"); + Customer myCustomer = service.addCustomer("John"); logger.info("Insert -- " + myCustomer.toString()); - myCustomer = service.updateCustomer(myCustomer.id, "john@doe.com", "+00", "Route 66"); + myCustomer = service.updateCustomer(myCustomer.id, "+00"); logger.info("Update -- " + myCustomer.toString()); } private void basicLoadAndSaveWithMapper() { - CustomerDto dto = new CustomerDto(); - dto.firstName = "Johnny"; - dto.lastName = "Doe"; + CustomerDto dto = new CustomerDto(null); + dto.name = "Johnny"; Customer entity = service.addCustomer(dto); logger.info("Insert -- " + entity.toString()); - CustomerDto dto2 = new CustomerDto(); - dto2.id = entity.id; - dto2.address = "Mountain View"; - dto2.email = "doe@mail.com"; + CustomerDto dto2 = new CustomerDto(entity.id); dto2.phone = "+44"; entity = service.updateCustomer(dto2); logger.info("Update -- " + entity.toString()); diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java index 91808e7971..28c5d5c76c 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java @@ -10,10 +10,7 @@ public class Customer { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public long id; - public String firstName; - public String lastName; - public String address; - public String email; + public String name; public String phone; //... public String phone99; @@ -21,7 +18,7 @@ public class Customer { public Customer() {} @Override public String toString() { - return String.format("Customer %s %s, Address: %s, Email: %s, Phone: %s", - this.firstName, this.lastName, this.address, this.email, this.phone); + return String.format("Customer %s, Phone: %s", + this.name, this.phone); } } diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java index 8f7803fc32..c4601d5b81 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java @@ -1,33 +1,26 @@ package com.baeldung.model; public class CustomerDto { - public long id; - public String firstName; - public String lastName; - public String address; - public String email; + private long id; + public String name; public String phone; //... - public String phone99; + private String phone99; - public CustomerDto() {} + public CustomerDto(long id) { + this.id = id; + } public CustomerDto(Customer c) { this.id = c.id; - this.firstName = c.firstName; - this.lastName = c.lastName; - this.address = c.address; - this.email = c.email; + this.name = c.name; this.phone = c.phone; } public Customer convertToEntity() { Customer c = new Customer(); c.id = id; - c.firstName = firstName; - c.lastName = lastName; - c.address = address; - c.email = email; + c.name = name; c.phone = phone; return c; } diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java index 56afc9e80d..3101cd8ece 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java @@ -16,18 +16,15 @@ public class CustomerService { @Autowired CustomerRepository repo; @Autowired CustomerMapper mapper; - public Customer addCustomer(String firstName, String lastName) { + public Customer addCustomer(String name) { Customer myCustomer = new Customer(); - myCustomer.firstName = firstName; - myCustomer.lastName = lastName; + myCustomer.name = name; repo.save(myCustomer); return myCustomer; } - public Customer updateCustomer(long id, String email, String phone, String address) { + public Customer updateCustomer(long id, String phone) { Customer myCustomer = repo.findById(id); - myCustomer.address = address; - myCustomer.email = email; myCustomer.phone = phone; repo.save(myCustomer); return myCustomer; From cd51e118df520c67f9e2c58b13c3c1bfca494d82 Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Mon, 18 May 2020 19:15:18 +0200 Subject: [PATCH 12/74] Fixed compilation error --- .../src/main/java/com/baeldung/model/CustomerDto.java | 4 ++++ .../src/main/java/com/baeldung/service/CustomerService.java | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java index c4601d5b81..74b909aad8 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java @@ -17,6 +17,10 @@ public class CustomerDto { this.phone = c.phone; } + public long getId() { + return this.id; + } + public Customer convertToEntity() { Customer c = new Customer(); c.id = id; diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java index 3101cd8ece..9ebbb0c814 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java @@ -38,7 +38,7 @@ public class CustomerService { } public Customer updateCustomer(CustomerDto dto) { - Customer myCustomer = repo.findById(dto.id); + Customer myCustomer = repo.findById(dto.getId()); mapper.updateCustomerFromDto(dto, myCustomer); repo.save(myCustomer); return myCustomer; From 745d5edda1c1018fad25e4e076f54f1fda89b464 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Sun, 12 Apr 2020 16:20:59 -0400 Subject: [PATCH 13/74] upgraded libs, moved to RestHighLevelClient --- .../spring-data-elasticsearch/pom.xml | 32 ++-- .../spring/data/es/config/Config.java | 47 ++---- .../ElasticSearchManualTest.java | 141 +++++++++--------- .../elasticsearch/GeoQueriesManualTest.java | 17 +-- .../data/es/ElasticSearchQueryManualTest.java | 3 +- 5 files changed, 107 insertions(+), 133 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index 3446528323..f322e46c0e 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -25,6 +25,12 @@ ${spring.version} + + org.springframework + spring-web + ${spring.version} + + org.springframework.data spring-data-elasticsearch @@ -32,8 +38,8 @@ - org.elasticsearch - elasticsearch + org.elasticsearch.client + elasticsearch-rest-high-level-client ${elasticsearch.version} @@ -49,15 +55,9 @@ - com.vividsolutions - jts - ${jts.version} - - - xerces - xercesImpl - - + org.locationtech.jts + jts-core + 1.16.1 @@ -66,12 +66,6 @@ ${log4j.version} - - org.elasticsearch.client - transport - ${elasticsearch.version} - - org.springframework spring-test @@ -88,9 +82,9 @@ - 3.0.8.RELEASE + 3.2.6.RELEASE 4.5.2 - 5.6.0 + 7.6.2 1.2.47 0.6 1.13 diff --git a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java index e6ce795b45..f98342486f 100644 --- a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java +++ b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java @@ -1,19 +1,13 @@ package com.baeldung.spring.data.es.config; -import java.net.InetAddress; -import java.net.UnknownHostException; - -import org.elasticsearch.client.Client; -import org.elasticsearch.client.transport.TransportClient; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.transport.InetSocketTransportAddress; -import org.elasticsearch.transport.client.PreBuiltTransportClient; -import org.springframework.beans.factory.annotation.Value; +import org.elasticsearch.client.RestHighLevelClient; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.data.elasticsearch.client.ClientConfiguration; +import org.springframework.data.elasticsearch.client.RestClients; import org.springframework.data.elasticsearch.core.ElasticsearchOperations; -import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.repository.config.EnableElasticsearchRepositories; @Configuration @@ -21,30 +15,15 @@ import org.springframework.data.elasticsearch.repository.config.EnableElasticsea @ComponentScan(basePackages = { "com.baeldung.spring.data.es.service" }) public class Config { - @Value("${elasticsearch.home:/usr/local/Cellar/elasticsearch/5.6.0}") - private String elasticsearchHome; + @Bean + RestHighLevelClient client() { + ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build(); - @Value("${elasticsearch.cluster.name:elasticsearch}") - private String clusterName; + return RestClients.create(clientConfiguration).rest(); + } - @Bean - public Client client() { - TransportClient client = null; - try { - final Settings elasticsearchSettings = Settings.builder() - .put("client.transport.sniff", true) - .put("path.home", elasticsearchHome) - .put("cluster.name", clusterName).build(); - client = new PreBuiltTransportClient(elasticsearchSettings); - client.addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); - } catch (UnknownHostException e) { - e.printStackTrace(); - } - return client; - } - - @Bean - public ElasticsearchOperations elasticsearchTemplate() { - return new ElasticsearchTemplate(client()); - } + @Bean + public ElasticsearchOperations elasticsearchTemplate() { + return new ElasticsearchRestTemplate(client()); + } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index e43dcdf43e..6dc1c42dc1 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -3,7 +3,6 @@ package com.baeldung.elasticsearch; import static org.junit.Assert.assertEquals; import java.io.IOException; -import java.net.InetAddress; import java.net.UnknownHostException; import java.util.ArrayList; import java.util.Arrays; @@ -11,24 +10,28 @@ import java.util.Date; import java.util.List; import java.util.stream.Collectors; +import com.alibaba.fastjson.JSON; + import org.elasticsearch.action.DocWriteResponse.Result; +import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.SearchType; -import org.elasticsearch.client.Client; -import org.elasticsearch.common.settings.Settings; -import org.elasticsearch.common.transport.InetSocketTransportAddress; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentFactory; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; -import org.elasticsearch.transport.client.PreBuiltTransportClient; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Before; import org.junit.Test; - -import com.alibaba.fastjson.JSON; +import org.springframework.data.elasticsearch.client.ClientConfiguration; +import org.springframework.data.elasticsearch.client.RestClients; /** * @@ -39,7 +42,7 @@ import com.alibaba.fastjson.JSON; */ public class ElasticSearchManualTest { private List listOfPersons = new ArrayList<>(); - private Client client = null; + private RestHighLevelClient client = null; @Before public void setUp() throws UnknownHostException { @@ -48,47 +51,44 @@ public class ElasticSearchManualTest { listOfPersons.add(person1); listOfPersons.add(person2); - client = new PreBuiltTransportClient(Settings.builder().put("client.transport.sniff", true) - .put("cluster.name","elasticsearch").build()) - .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300)); + ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build(); + client = RestClients.create(clientConfiguration).rest(); } @Test - public void givenJsonString_whenJavaObject_thenIndexDocument() { + public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; - IndexResponse response = client - .prepareIndex("people", "Doe") - .setSource(jsonObject, XContentType.JSON) - .get(); - String index = response.getIndex(); - String type = response.getType(); + IndexRequest request = new IndexRequest("people"); + request.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(request, RequestOptions.DEFAULT); + String index = response.getIndex(); + assertEquals(Result.CREATED, response.getResult()); assertEquals(index, "people"); - assertEquals(type, "Doe"); } @Test - public void givenDocumentId_whenJavaObject_thenDeleteDocument() { + public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; - IndexResponse response = client - .prepareIndex("people", "Doe") - .setSource(jsonObject, XContentType.JSON) - .get(); + IndexRequest indexRequest = new IndexRequest("people"); + indexRequest.source(jsonObject, XContentType.JSON); + + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); String id = response.getId(); - DeleteResponse deleteResponse = client - .prepareDelete("people", "Doe", id) - .get(); + + DeleteRequest deleteRequest = new DeleteRequest("people"); + deleteRequest.id(id); + + DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); assertEquals(Result.DELETED,deleteResponse.getResult()); } @Test - public void givenSearchRequest_whenMatchAll_thenReturnAllResults() { - SearchResponse response = client - .prepareSearch() - .execute() - .actionGet(); + public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception { + SearchRequest searchRequest = new SearchRequest(); + SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); SearchHit[] searchHits = response .getHits() .getHits(); @@ -98,42 +98,42 @@ public class ElasticSearchManualTest { } @Test - public void givenSearchParameters_thenReturnResults() { - SearchResponse response = client - .prepareSearch() - .setTypes() - .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) - .setPostFilter(QueryBuilders - .rangeQuery("age") - .from(5) - .to(15)) - .setFrom(0) - .setSize(60) - .setExplain(true) - .execute() - .actionGet(); + public void givenSearchParameters_thenReturnResults() throws Exception { + SearchSourceBuilder builder = new SearchSourceBuilder() + .postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)) + .from(0) + .size(60) + .explain(true); + + SearchRequest searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); + + SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - SearchResponse response2 = client - .prepareSearch() - .setTypes() - .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) - .setPostFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")) - .setFrom(0) - .setSize(60) - .setExplain(true) - .execute() - .actionGet(); + builder = new SearchSourceBuilder() + .postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")) + .from(0) + .size(60) + .explain(true); + + searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); + + SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT); + + builder = new SearchSourceBuilder() + .postFilter(QueryBuilders.matchQuery("John", "Name*")) + .from(0) + .size(60) + .explain(true); + searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); + + SearchResponse response3 = client.search(searchRequest, RequestOptions.DEFAULT); - SearchResponse response3 = client - .prepareSearch() - .setTypes() - .setSearchType(SearchType.DFS_QUERY_THEN_FETCH) - .setPostFilter(QueryBuilders.matchQuery("John", "Name*")) - .setFrom(0) - .setSize(60) - .setExplain(true) - .execute() - .actionGet(); response2.getHits(); response3.getHits(); @@ -151,10 +151,11 @@ public class ElasticSearchManualTest { .field("salary", "11500") .field("age", "10") .endObject(); - IndexResponse response = client - .prepareIndex("people", "Doe") - .setSource(builder) - .get(); + + IndexRequest indexRequest = new IndexRequest("people"); + indexRequest.source(builder); + + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); assertEquals(Result.CREATED, response.getResult()); } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java index f9a42050b6..698f605eeb 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java @@ -1,4 +1,5 @@ package com.baeldung.elasticsearch; + import static org.junit.Assert.assertTrue; import java.io.IOException; @@ -12,8 +13,7 @@ import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; import org.elasticsearch.common.geo.GeoPoint; -import org.elasticsearch.common.geo.ShapeRelation; -import org.elasticsearch.common.geo.builders.ShapeBuilders; +import org.elasticsearch.common.geo.builders.EnvelopeBuilder; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilder; @@ -23,13 +23,13 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; +import org.locationtech.jts.geom.Coordinate; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.baeldung.spring.data.es.config.Config; -import com.vividsolutions.jts.geom.Coordinate; /** * @@ -75,13 +75,12 @@ public class GeoQueriesManualTest { .indices() .prepareRefresh(WONDERS_OF_WORLD) .get(); - - Coordinate topLeft =new Coordinate(74, 31.2); - Coordinate bottomRight =new Coordinate(81.1, 24); + + Coordinate topLeft = new Coordinate(74, 31.2); + Coordinate bottomRight = new Coordinate(81.1, 24); QueryBuilder qb = QueryBuilders - .geoShapeQuery("region", ShapeBuilders.newEnvelope(topLeft, bottomRight)) - .relation(ShapeRelation.WITHIN); - + .geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight).buildGeometry()); + //.relation(ShapeRelation.WITHIN)); SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) .setTypes(WONDERS) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index 5e24d8398c..0125a4b624 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -22,6 +22,7 @@ import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; +import org.elasticsearch.search.aggregations.BucketOrder; import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; import org.elasticsearch.search.aggregations.bucket.terms.Terms; @@ -159,7 +160,7 @@ public class ElasticSearchQueryManualTest { @Test public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() { final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags") - .order(Terms.Order.count(false)); + .order(BucketOrder.count(false)); final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation) .execute().actionGet(); From c1d547c5f0a43e79497d3e80f349bc7d1f5467e5 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Mon, 13 Apr 2020 13:02:12 -0400 Subject: [PATCH 14/74] fixed tests --- .../spring-data-elasticsearch/pom.xml | 2 +- .../elasticsearch/ElasticSearchManualTest.java | 10 +++++++--- .../elasticsearch/GeoQueriesManualTest.java | 3 +-- .../spring/data/es/ElasticSearchManualTest.java | 14 +++++++------- .../data/es/ElasticSearchQueryManualTest.java | 15 +++++++-------- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index f322e46c0e..36cd2d4171 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -84,7 +84,7 @@ 3.2.6.RELEASE 4.5.2 - 7.6.2 + 6.8.8 1.2.47 0.6 1.13 diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index 6dc1c42dc1..5928b0ef8e 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -58,7 +58,7 @@ public class ElasticSearchManualTest { @Test public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; - IndexRequest request = new IndexRequest("people"); + IndexRequest request = new IndexRequest("people", "Doe"); request.source(jsonObject, XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); @@ -71,7 +71,7 @@ public class ElasticSearchManualTest { @Test public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; - IndexRequest indexRequest = new IndexRequest("people"); + IndexRequest indexRequest = new IndexRequest("people", "Doe"); indexRequest.source(jsonObject, XContentType.JSON); IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); @@ -79,6 +79,7 @@ public class ElasticSearchManualTest { DeleteRequest deleteRequest = new DeleteRequest("people"); deleteRequest.id(id); + deleteRequest.type("Doe"); DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); @@ -95,6 +96,8 @@ public class ElasticSearchManualTest { List results = Arrays.stream(searchHits) .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) .collect(Collectors.toList()); + + results.forEach(System.out::println); } @Test @@ -140,6 +143,7 @@ public class ElasticSearchManualTest { final List results = Arrays.stream(response.getHits().getHits()) .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) .collect(Collectors.toList()); + results.forEach(System.out::println); } @Test @@ -152,7 +156,7 @@ public class ElasticSearchManualTest { .field("age", "10") .endObject(); - IndexRequest indexRequest = new IndexRequest("people"); + IndexRequest indexRequest = new IndexRequest("people", "Doe"); indexRequest.source(builder); IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java index 698f605eeb..45c643b636 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java @@ -79,8 +79,7 @@ public class GeoQueriesManualTest { Coordinate topLeft = new Coordinate(74, 31.2); Coordinate bottomRight = new Coordinate(81.1, 24); QueryBuilder qb = QueryBuilders - .geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight).buildGeometry()); - //.relation(ShapeRelation.WITHIN)); + .geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight)); SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) .setTypes(WONDERS) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index bed2e2ff25..7c60c98ab7 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -10,23 +10,23 @@ import static org.junit.Assert.assertNotNull; import java.util.List; +import com.baeldung.spring.data.es.config.Config; +import com.baeldung.spring.data.es.model.Article; +import com.baeldung.spring.data.es.model.Author; +import com.baeldung.spring.data.es.service.ArticleService; + import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; -import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.baeldung.spring.data.es.config.Config; -import com.baeldung.spring.data.es.model.Article; -import com.baeldung.spring.data.es.model.Author; -import com.baeldung.spring.data.es.service.ArticleService; - /** * * This Manual test requires: @@ -39,7 +39,7 @@ import com.baeldung.spring.data.es.service.ArticleService; public class ElasticSearchManualTest { @Autowired - private ElasticsearchTemplate elasticsearchTemplate; + private ElasticsearchRestTemplate elasticsearchTemplate; @Autowired private ArticleService articleService; diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index 0125a4b624..b6146ac415 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -14,6 +14,11 @@ import static org.junit.Assert.assertEquals; import java.util.List; import java.util.Map; +import com.baeldung.spring.data.es.config.Config; +import com.baeldung.spring.data.es.model.Article; +import com.baeldung.spring.data.es.model.Author; +import com.baeldung.spring.data.es.service.ArticleService; + import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.Client; @@ -25,23 +30,17 @@ import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.BucketOrder; import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; -import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; +import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.baeldung.spring.data.es.config.Config; -import com.baeldung.spring.data.es.model.Article; -import com.baeldung.spring.data.es.model.Author; -import com.baeldung.spring.data.es.service.ArticleService; - /** * * This Manual test requires: @@ -54,7 +53,7 @@ import com.baeldung.spring.data.es.service.ArticleService; public class ElasticSearchQueryManualTest { @Autowired - private ElasticsearchTemplate elasticsearchTemplate; + private ElasticsearchRestTemplate elasticsearchTemplate; @Autowired private ArticleService articleService; From 2c5c556fedd3610ca3c146bbd58b67a4421e87d4 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Mon, 13 Apr 2020 13:35:47 -0400 Subject: [PATCH 15/74] fixed tests --- .../spring-data-elasticsearch/pom.xml | 5 - .../com/baeldung/SpringContextManualTest.java | 3 +- .../ElasticSearchManualTest.java | 195 ++++++++-------- .../elasticsearch/GeoQueriesManualTest.java | 213 +++++++----------- .../data/es/ElasticSearchManualTest.java | 24 +- .../data/es/ElasticSearchQueryManualTest.java | 36 ++- 6 files changed, 211 insertions(+), 265 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index 36cd2d4171..7d7ffb525d 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -37,11 +37,6 @@ ${spring-data-elasticsearch.version} - - org.elasticsearch.client - elasticsearch-rest-high-level-client - ${elasticsearch.version} - com.alibaba fastjson diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java index c69deeb77c..2e9352b081 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java @@ -9,8 +9,7 @@ import com.baeldung.spring.data.es.config.Config; /** * - * This Manual test requires: - * * Elasticsearch instance running on host + * This Manual test requires: * Elasticsearch instance running on host * */ @RunWith(SpringJUnit4ClassRunner.class) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index 5928b0ef8e..f8f70c58a4 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -35,132 +35,113 @@ import org.springframework.data.elasticsearch.client.RestClients; /** * - * This Manual test requires: - * * Elasticsearch instance running on host - * * with cluster name = elasticsearch + * This Manual test requires: * Elasticsearch instance running on host * with + * cluster name = elasticsearch * */ public class ElasticSearchManualTest { - private List listOfPersons = new ArrayList<>(); - private RestHighLevelClient client = null; + private List listOfPersons = new ArrayList<>(); + private RestHighLevelClient client = null; - @Before - public void setUp() throws UnknownHostException { - Person person1 = new Person(10, "John Doe", new Date()); - Person person2 = new Person(25, "Janette Doe", new Date()); - listOfPersons.add(person1); - listOfPersons.add(person2); - - ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build(); - client = RestClients.create(clientConfiguration).rest(); - } + @Before + public void setUp() throws UnknownHostException { + Person person1 = new Person(10, "John Doe", new Date()); + Person person2 = new Person(25, "Janette Doe", new Date()); + listOfPersons.add(person1); + listOfPersons.add(person2); - @Test - public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { - String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; - IndexRequest request = new IndexRequest("people", "Doe"); - request.source(jsonObject, XContentType.JSON); + ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build(); + client = RestClients.create(clientConfiguration).rest(); + } - IndexResponse response = client.index(request, RequestOptions.DEFAULT); - String index = response.getIndex(); - - assertEquals(Result.CREATED, response.getResult()); - assertEquals(index, "people"); - } + @Test + public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { + String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; + IndexRequest request = new IndexRequest("people", "Doe"); + request.source(jsonObject, XContentType.JSON); - @Test - public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { - String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; - IndexRequest indexRequest = new IndexRequest("people", "Doe"); - indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(request, RequestOptions.DEFAULT); + String index = response.getIndex(); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - String id = response.getId(); + assertEquals(Result.CREATED, response.getResult()); + assertEquals(index, "people"); + } - DeleteRequest deleteRequest = new DeleteRequest("people"); - deleteRequest.id(id); - deleteRequest.type("Doe"); + @Test + public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { + String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; + IndexRequest indexRequest = new IndexRequest("people", "Doe"); + indexRequest.source(jsonObject, XContentType.JSON); - DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + String id = response.getId(); - assertEquals(Result.DELETED,deleteResponse.getResult()); - } + DeleteRequest deleteRequest = new DeleteRequest("people"); + deleteRequest.id(id); + deleteRequest.type("Doe"); - @Test - public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception { - SearchRequest searchRequest = new SearchRequest(); - SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - SearchHit[] searchHits = response - .getHits() - .getHits(); - List results = Arrays.stream(searchHits) - .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) - .collect(Collectors.toList()); - - results.forEach(System.out::println); - } + DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); - @Test - public void givenSearchParameters_thenReturnResults() throws Exception { - SearchSourceBuilder builder = new SearchSourceBuilder() - .postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)) - .from(0) - .size(60) + assertEquals(Result.DELETED, deleteResponse.getResult()); + } + + @Test + public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception { + SearchRequest searchRequest = new SearchRequest(); + SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + SearchHit[] searchHits = response.getHits().getHits(); + List results = Arrays.stream(searchHits).map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) + .collect(Collectors.toList()); + + results.forEach(System.out::println); + } + + @Test + public void givenSearchParameters_thenReturnResults() throws Exception { + SearchSourceBuilder builder = new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)) + .from(0).size(60).explain(true); + + SearchRequest searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); + + SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + + builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")) + .from(0).size(60).explain(true); + + searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); + + SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT); + + builder = new SearchSourceBuilder().postFilter(QueryBuilders.matchQuery("John", "Name*")).from(0).size(60) .explain(true); - - SearchRequest searchRequest = new SearchRequest(); - searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); - searchRequest.source(builder); - - SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); - builder = new SearchSourceBuilder() - .postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")) - .from(0) - .size(60) - .explain(true); + SearchResponse response3 = client.search(searchRequest, RequestOptions.DEFAULT); - searchRequest = new SearchRequest(); - searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); - searchRequest.source(builder); + response2.getHits(); + response3.getHits(); - SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT); + final List results = Arrays.stream(response.getHits().getHits()) + .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)).collect(Collectors.toList()); + results.forEach(System.out::println); + } - builder = new SearchSourceBuilder() - .postFilter(QueryBuilders.matchQuery("John", "Name*")) - .from(0) - .size(60) - .explain(true); - searchRequest = new SearchRequest(); - searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); - searchRequest.source(builder); + @Test + public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException { + XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("fullName", "Test") + .field("salary", "11500").field("age", "10").endObject(); - SearchResponse response3 = client.search(searchRequest, RequestOptions.DEFAULT); + IndexRequest indexRequest = new IndexRequest("people", "Doe"); + indexRequest.source(builder); - response2.getHits(); - response3.getHits(); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - final List results = Arrays.stream(response.getHits().getHits()) - .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) - .collect(Collectors.toList()); - results.forEach(System.out::println); - } - - @Test - public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException { - XContentBuilder builder = XContentFactory - .jsonBuilder() - .startObject() - .field("fullName", "Test") - .field("salary", "11500") - .field("age", "10") - .endObject(); - - IndexRequest indexRequest = new IndexRequest("people", "Doe"); - indexRequest.source(builder); - - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - - assertEquals(Result.CREATED, response.getResult()); - } + assertEquals(Result.CREATED, response.getResult()); + } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java index 45c643b636..e373fef7a6 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java @@ -33,155 +33,110 @@ import com.baeldung.spring.data.es.config.Config; /** * - * This Manual test requires: - * * Elasticsearch instance running on host - * * with cluster name = elasticsearch - * * and further configurations + * This Manual test requires: * Elasticsearch instance running on host * with + * cluster name = elasticsearch * and further configurations * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) public class GeoQueriesManualTest { - private static final String WONDERS_OF_WORLD = "wonders-of-world"; - private static final String WONDERS = "Wonders"; + private static final String WONDERS_OF_WORLD = "wonders-of-world"; + private static final String WONDERS = "Wonders"; - @Autowired - private ElasticsearchTemplate elasticsearchTemplate; + @Autowired + private ElasticsearchTemplate elasticsearchTemplate; - @Autowired - private Client client; + @Autowired + private Client client; - @Before - public void setUp() { - String jsonObject = "{\"Wonders\":{\"properties\":{\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"region\":{\"type\":\"geo_shape\",\"tree\":\"quadtree\",\"precision\":\"1m\"},\"location\":{\"type\":\"geo_point\"}}}}"; - CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD); - req.mapping(WONDERS, jsonObject, XContentType.JSON); - client.admin() - .indices() - .create(req) - .actionGet(); - } + @Before + public void setUp() { + String jsonObject = "{\"Wonders\":{\"properties\":{\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"region\":{\"type\":\"geo_shape\",\"tree\":\"quadtree\",\"precision\":\"1m\"},\"location\":{\"type\":\"geo_point\"}}}}"; + CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD); + req.mapping(WONDERS, jsonObject, XContentType.JSON); + client.admin().indices().create(req).actionGet(); + } - @Test - public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException{ - String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,30.2],[80.1, 25]]}}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) - .setSource(jsonObject, XContentType.JSON) - .get(); - - String tajMahalId = response.getId(); - client.admin() - .indices() - .prepareRefresh(WONDERS_OF_WORLD) - .get(); - - Coordinate topLeft = new Coordinate(74, 31.2); - Coordinate bottomRight = new Coordinate(81.1, 24); - QueryBuilder qb = QueryBuilders - .geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight)); + @Test + public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException { + String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,30.2],[80.1, 25]]}}"; + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) + .get(); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) - .setTypes(WONDERS) - .setQuery(qb) - .execute() - .actionGet(); + String tajMahalId = response.getId(); + client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); - List ids = Arrays.stream(searchResponse.getHits() - .getHits()) - .map(SearchHit::getId) - .collect(Collectors.toList()); + Coordinate topLeft = new Coordinate(74, 31.2); + Coordinate bottomRight = new Coordinate(81.1, 24); + QueryBuilder qb = QueryBuilders.geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight)); - assertTrue(ids.contains(tajMahalId)); - } + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() + .actionGet(); - @Test - public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() { - String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) - .setSource(jsonObject, XContentType.JSON) - .get(); - String pyramidsOfGizaId = response.getId(); - client.admin() - .indices() - .prepareRefresh(WONDERS_OF_WORLD) - .get(); + List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) + .collect(Collectors.toList()); - QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location") - .setCorners(31,30,28,32); - - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) - .setTypes(WONDERS) - .setQuery(qb) - .execute() - .actionGet(); - List ids = Arrays.stream(searchResponse.getHits() - .getHits()) - .map(SearchHit::getId) - .collect(Collectors.toList()); - assertTrue(ids.contains(pyramidsOfGizaId)); - } + assertTrue(ids.contains(tajMahalId)); + } - @Test - public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() { - String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) - .setSource(jsonObject, XContentType.JSON) - .get(); - String lighthouseOfAlexandriaId = response.getId(); - client.admin() - .indices() - .prepareRefresh(WONDERS_OF_WORLD) - .get(); + @Test + public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() { + String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) + .get(); + String pyramidsOfGizaId = response.getId(); + client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); - QueryBuilder qb = QueryBuilders.geoDistanceQuery("location") - .point(29.976, 31.131) - .distance(10, DistanceUnit.MILES); + QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location").setCorners(31, 30, 28, 32); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) - .setTypes(WONDERS) - .setQuery(qb) - .execute() - .actionGet(); - List ids = Arrays.stream(searchResponse.getHits() - .getHits()) - .map(SearchHit::getId) - .collect(Collectors.toList()); - assertTrue(ids.contains(lighthouseOfAlexandriaId)); - } + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() + .actionGet(); + List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(pyramidsOfGizaId)); + } - @Test - public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() { - String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS) - .setSource(jsonObject, XContentType.JSON) - .get(); - String greatRannOfKutchid = response.getId(); - client.admin() - .indices() - .prepareRefresh(WONDERS_OF_WORLD) - .get(); + @Test + public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() { + String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) + .get(); + String lighthouseOfAlexandriaId = response.getId(); + client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); - List allPoints = new ArrayList(); - allPoints.add(new GeoPoint(22.733, 68.859)); - allPoints.add(new GeoPoint(24.733, 68.859)); - allPoints.add(new GeoPoint(23, 70.859)); - QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints); + QueryBuilder qb = QueryBuilders.geoDistanceQuery("location").point(29.976, 31.131).distance(10, DistanceUnit.MILES); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD) - .setTypes(WONDERS) - .setQuery(qb) - .execute() - .actionGet(); - List ids = Arrays.stream(searchResponse.getHits() - .getHits()) - .map(SearchHit::getId) - .collect(Collectors.toList()); - assertTrue(ids.contains(greatRannOfKutchid)); - } + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() + .actionGet(); + List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(lighthouseOfAlexandriaId)); + } - @After - public void destroy() { - elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD); - } + @Test + public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() { + String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; + IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) + .get(); + String greatRannOfKutchid = response.getId(); + client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); + + List allPoints = new ArrayList(); + allPoints.add(new GeoPoint(22.733, 68.859)); + allPoints.add(new GeoPoint(24.733, 68.859)); + allPoints.add(new GeoPoint(23, 70.859)); + QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints); + + SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() + .actionGet(); + List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(greatRannOfKutchid)); + } + + @After + public void destroy() { + elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD); + } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index 7c60c98ab7..751755689f 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -29,9 +29,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * - * This Manual test requires: - * * Elasticsearch instance running on host - * * with cluster name = elasticsearch + * This Manual test requires: * Elasticsearch instance running on host * with + * cluster name = elasticsearch * */ @RunWith(SpringJUnit4ClassRunner.class) @@ -88,26 +87,29 @@ public class ElasticSearchManualTest { @Test public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() { - final Page
articleByAuthorName = articleService - .findByAuthorName(johnSmith.getName(), PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleService.findByAuthorName(johnSmith.getName(), + PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("Smith", PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("Smith", + PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenTagFilterQuery_whenSearchByTag_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByFilteredTagQuery("elasticsearch", PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleService.findByFilteredTagQuery("elasticsearch", + PageRequest.of(0, 10)); assertEquals(3L, articleByAuthorName.getTotalElements()); } @Test public void givenTagFilterQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByAuthorsNameAndFilteredTagQuery("Doe", "elasticsearch", PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleService.findByAuthorsNameAndFilteredTagQuery("Doe", + "elasticsearch", PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @@ -115,7 +117,7 @@ public class ElasticSearchManualTest { public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() { final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")) - .build(); + .build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); @@ -142,7 +144,7 @@ public class ElasticSearchManualTest { final String articleTitle = "Spring Data Elasticsearch"; final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); + .withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); final long count = articleService.count(); @@ -155,7 +157,7 @@ public class ElasticSearchManualTest { @Test public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() { final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); + .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index b6146ac415..629f89f3f1 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -20,8 +20,10 @@ import com.baeldung.spring.data.es.model.Author; import com.baeldung.spring.data.es.service.ArticleService; import org.apache.lucene.search.join.ScoreMode; +import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.client.Client; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.index.query.MultiMatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; @@ -29,8 +31,9 @@ import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.BucketOrder; import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; -import org.elasticsearch.search.aggregations.bucket.terms.StringTerms; +import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -59,7 +62,7 @@ public class ElasticSearchQueryManualTest { private ArticleService articleService; @Autowired - private Client client; + private RestHighLevelClient client; private final Author johnSmith = new Author("John Smith"); private final Author johnDoe = new Author("John Doe"); @@ -141,13 +144,18 @@ public class ElasticSearchQueryManualTest { } @Test - public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() { + public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception { final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); - final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation) - .execute().actionGet(); + + final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); + final SearchRequest searchRequest = new SearchRequest("blog") + .types("article") + .source(builder); + final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + final Map results = response.getAggregations().asMap(); - final StringTerms topTags = (StringTerms) results.get("top_tags"); + final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); final List keys = topTags.getBuckets().stream() .map(MultiBucketsAggregation.Bucket::getKeyAsString) @@ -157,14 +165,20 @@ public class ElasticSearchQueryManualTest { } @Test - public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() { + public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception { final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags") .order(BucketOrder.count(false)); - final SearchResponse response = client.prepareSearch("blog").setTypes("article").addAggregation(aggregation) - .execute().actionGet(); + + final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); + final SearchRequest searchRequest = new SearchRequest() + .indices("blog") + .types("article") + .source(builder); + + final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); final Map results = response.getAggregations().asMap(); - final StringTerms topTags = (StringTerms) results.get("top_tags"); + final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); final List keys = topTags.getBuckets().stream() .map(MultiBucketsAggregation.Bucket::getKeyAsString) From 58e44705b28db75112fe6a65b065e365d6a37b82 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Mon, 13 Apr 2020 13:47:17 -0400 Subject: [PATCH 16/74] fixed format --- .../spring-data-elasticsearch/pom.xml | 1 - .../data/es/ElasticSearchQueryManualTest.java | 277 +++++++++--------- 2 files changed, 136 insertions(+), 142 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index 7d7ffb525d..e2089eb8b1 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -79,7 +79,6 @@ 3.2.6.RELEASE 4.5.2 - 6.8.8 1.2.47 0.6 1.13 diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index 629f89f3f1..438d3adf02 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -46,183 +46,178 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * - * This Manual test requires: - * * Elasticsearch instance running on host - * * with cluster name = elasticsearch + * This Manual test requires: * Elasticsearch instance running on host * with + * cluster name = elasticsearch * */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) public class ElasticSearchQueryManualTest { - @Autowired - private ElasticsearchRestTemplate elasticsearchTemplate; + @Autowired + private ElasticsearchRestTemplate elasticsearchTemplate; - @Autowired - private ArticleService articleService; + @Autowired + private ArticleService articleService; - @Autowired - private RestHighLevelClient client; + @Autowired + private RestHighLevelClient client; - private final Author johnSmith = new Author("John Smith"); - private final Author johnDoe = new Author("John Doe"); + private final Author johnSmith = new Author("John Smith"); + private final Author johnDoe = new Author("John Doe"); - @Before - public void before() { - elasticsearchTemplate.deleteIndex(Article.class); - elasticsearchTemplate.createIndex(Article.class); - elasticsearchTemplate.putMapping(Article.class); - elasticsearchTemplate.refresh(Article.class); + @Before + public void before() { + elasticsearchTemplate.deleteIndex(Article.class); + elasticsearchTemplate.createIndex(Article.class); + elasticsearchTemplate.putMapping(Article.class); + elasticsearchTemplate.refresh(Article.class); - Article article = new Article("Spring Data Elasticsearch"); - article.setAuthors(asList(johnSmith, johnDoe)); - article.setTags("elasticsearch", "spring data"); - articleService.save(article); + Article article = new Article("Spring Data Elasticsearch"); + article.setAuthors(asList(johnSmith, johnDoe)); + article.setTags("elasticsearch", "spring data"); + articleService.save(article); - article = new Article("Search engines"); - article.setAuthors(asList(johnDoe)); - article.setTags("search engines", "tutorial"); - articleService.save(article); + article = new Article("Search engines"); + article.setAuthors(asList(johnDoe)); + article.setTags("search engines", "tutorial"); + articleService.save(article); - article = new Article("Second Article About Elasticsearch"); - article.setAuthors(asList(johnSmith)); - article.setTags("elasticsearch", "spring data"); - articleService.save(article); + article = new Article("Second Article About Elasticsearch"); + article.setAuthors(asList(johnSmith)); + article.setTags("elasticsearch", "spring data"); + articleService.save(article); - article = new Article("Elasticsearch Tutorial"); - article.setAuthors(asList(johnDoe)); - article.setTags("elasticsearch"); - articleService.save(article); - } + article = new Article("Elasticsearch Tutorial"); + article.setAuthors(asList(johnDoe)); + article.setTags("elasticsearch"); + articleService.save(article); + } - @Test - public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); - } + @Test + public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + } - @Test - public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "Engines Solutions")).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); - assertEquals("Search engines", articles.get(0).getTitle()); - } + @Test + public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")) + .build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + assertEquals("Search engines", articles.get(0).getTitle()); + } - @Test - public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "elasticsearch data")).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(3, articles.size()); - } + @Test + public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")) + .build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(3, articles.size()); + } - @Test - public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { - SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build(); - List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + @Test + public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { + SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build(); + List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); - searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")) - .build(); - articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(0, articles.size()); - } + searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")) + .build(); + articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(0, articles.size()); + } - @Test - public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() { - final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), ScoreMode.None); + @Test + public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() { + final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), + ScoreMode.None); - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(2, articles.size()); - } + assertEquals(2, articles.size()); + } - @Test - public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception { - final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); - - final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); - final SearchRequest searchRequest = new SearchRequest("blog") - .types("article") - .source(builder); + @Test + public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception { + final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); - final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - - final Map results = response.getAggregations().asMap(); - final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); + final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); + final SearchRequest searchRequest = new SearchRequest("blog").types("article").source(builder); - final List keys = topTags.getBuckets().stream() - .map(MultiBucketsAggregation.Bucket::getKeyAsString) - .sorted() - .collect(toList()); - assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys); - } + final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - @Test - public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception { - final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags") - .order(BucketOrder.count(false)); + final Map results = response.getAggregations().asMap(); + final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); - final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); - final SearchRequest searchRequest = new SearchRequest() - .indices("blog") - .types("article") - .source(builder); + final List keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).sorted() + .collect(toList()); + assertEquals( + asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys); + } - final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + @Test + public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception { + final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags") + .order(BucketOrder.count(false)); - final Map results = response.getAggregations().asMap(); - final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); + final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); + final SearchRequest searchRequest = new SearchRequest().indices("blog").types("article").source(builder); - final List keys = topTags.getBuckets().stream() - .map(MultiBucketsAggregation.Bucket::getKeyAsString) - .collect(toList()); - assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); - } + final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - @Test - public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); - } + final Map results = response.getAggregations().asMap(); + final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); - @Test - public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE) - .prefixLength(3)).build(); + final List keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString) + .collect(toList()); + assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); + } - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); - } + @Test + public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + } - @Test - public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(multiMatchQuery("tutorial").field("title").field("tags") - .type(MultiMatchQueryBuilder.Type.BEST_FIELDS)).build(); + @Test + public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery( + matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3)) + .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(2, articles.size()); - } + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(1, articles.size()); + } - @Test - public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() { - final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None)) - .filter(termQuery("tags", "elasticsearch")); + @Test + public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { + final SearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery( + multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)) + .build(); - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder) - .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + assertEquals(2, articles.size()); + } - assertEquals(2, articles.size()); - } + @Test + public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() { + final QueryBuilder builder = boolQuery() + .must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None)) + .filter(termQuery("tags", "elasticsearch")); + + final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); + final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + + assertEquals(2, articles.size()); + } } From 95e4e7326d8cd504f0c749e460fd887b6734a53c Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Mon, 20 Apr 2020 20:29:21 -0400 Subject: [PATCH 17/74] removed ArticleService --- .../data/es/service/ArticleService.java | 28 -------- .../data/es/service/ArticleServiceImpl.java | 67 ------------------- .../data/es/ElasticSearchManualTest.java | 32 ++++----- .../data/es/ElasticSearchQueryManualTest.java | 12 ++-- 4 files changed, 22 insertions(+), 117 deletions(-) delete mode 100644 persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java delete mode 100644 persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java diff --git a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java deleted file mode 100644 index a0f72aa5f7..0000000000 --- a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleService.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.baeldung.spring.data.es.service; - -import java.util.Optional; - -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; - -import com.baeldung.spring.data.es.model.Article; - -public interface ArticleService { - Article save(Article article); - - Optional
findOne(String id); - - Iterable
findAll(); - - Page
findByAuthorName(String name, Pageable pageable); - - Page
findByAuthorNameUsingCustomQuery(String name, Pageable pageable); - - Page
findByFilteredTagQuery(String tag, Pageable pageable); - - Page
findByAuthorsNameAndFilteredTagQuery(String name, String tag, Pageable pageable); - - long count(); - - void delete(Article article); -} diff --git a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java deleted file mode 100644 index 5064f16508..0000000000 --- a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/service/ArticleServiceImpl.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.baeldung.spring.data.es.service; - -import java.util.Optional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.domain.Page; -import org.springframework.data.domain.Pageable; -import org.springframework.stereotype.Service; - -import com.baeldung.spring.data.es.model.Article; -import com.baeldung.spring.data.es.repository.ArticleRepository; - -@Service -public class ArticleServiceImpl implements ArticleService { - - private final ArticleRepository articleRepository; - - @Autowired - public ArticleServiceImpl(ArticleRepository articleRepository) { - this.articleRepository = articleRepository; - } - - @Override - public Article save(Article article) { - return articleRepository.save(article); - } - - @Override - public Optional
findOne(String id) { - return articleRepository.findById(id); - } - - @Override - public Iterable
findAll() { - return articleRepository.findAll(); - } - - @Override - public Page
findByAuthorName(String name, Pageable pageable) { - return articleRepository.findByAuthorsName(name, pageable); - } - - @Override - public Page
findByAuthorNameUsingCustomQuery(String name, Pageable pageable) { - return articleRepository.findByAuthorsNameUsingCustomQuery(name, pageable); - } - - @Override - public Page
findByFilteredTagQuery(String tag, Pageable pageable) { - return articleRepository.findByFilteredTagQuery(tag, pageable); - } - - @Override - public Page
findByAuthorsNameAndFilteredTagQuery(String name, String tag, Pageable pageable) { - return articleRepository.findByAuthorsNameAndFilteredTagQuery(name, tag, pageable); - } - - @Override - public long count() { - return articleRepository.count(); - } - - @Override - public void delete(Article article) { - articleRepository.delete(article); - } -} diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index 751755689f..6e1d7d1b7d 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -13,7 +13,7 @@ import java.util.List; import com.baeldung.spring.data.es.config.Config; import com.baeldung.spring.data.es.model.Article; import com.baeldung.spring.data.es.model.Author; -import com.baeldung.spring.data.es.service.ArticleService; +import com.baeldung.spring.data.es.repository.ArticleRepository; import org.junit.Before; import org.junit.Test; @@ -41,7 +41,7 @@ public class ElasticSearchManualTest { private ElasticsearchRestTemplate elasticsearchTemplate; @Autowired - private ArticleService articleService; + private ArticleRepository articleRepository; private final Author johnSmith = new Author("John Smith"); private final Author johnDoe = new Author("John Doe"); @@ -55,22 +55,22 @@ public class ElasticSearchManualTest { Article article = new Article("Spring Data Elasticsearch"); article.setAuthors(asList(johnSmith, johnDoe)); article.setTags("elasticsearch", "spring data"); - articleService.save(article); + articleRepository.save(article); article = new Article("Search engines"); article.setAuthors(asList(johnDoe)); article.setTags("search engines", "tutorial"); - articleService.save(article); + articleRepository.save(article); article = new Article("Second Article About Elasticsearch"); article.setAuthors(asList(johnSmith)); article.setTags("elasticsearch", "spring data"); - articleService.save(article); + articleRepository.save(article); article = new Article("Elasticsearch Tutorial"); article.setAuthors(asList(johnDoe)); article.setTags("elasticsearch"); - articleService.save(article); + articleRepository.save(article); } @Test @@ -80,35 +80,35 @@ public class ElasticSearchManualTest { Article article = new Article("Making Search Elastic"); article.setAuthors(authors); - article = articleService.save(article); + article = articleRepository.save(article); assertNotNull(article.getId()); } @Test public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() { - final Page
articleByAuthorName = articleService.findByAuthorName(johnSmith.getName(), + final Page
articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(), PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByAuthorNameUsingCustomQuery("Smith", + final Page
articleByAuthorName = articleRepository.findByAuthorsNameUsingCustomQuery("Smith", PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenTagFilterQuery_whenSearchByTag_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByFilteredTagQuery("elasticsearch", + final Page
articleByAuthorName = articleRepository.findByFilteredTagQuery("elasticsearch", PageRequest.of(0, 10)); assertEquals(3L, articleByAuthorName.getTotalElements()); } @Test public void givenTagFilterQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleService.findByAuthorsNameAndFilteredTagQuery("Doe", + final Page
articleByAuthorName = articleRepository.findByAuthorsNameAndFilteredTagQuery("Doe", "elasticsearch", PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @@ -133,9 +133,9 @@ public class ElasticSearchManualTest { final Article article = articles.get(0); final String newTitle = "Getting started with Search Engines"; article.setTitle(newTitle); - articleService.save(article); + articleRepository.save(article); - assertEquals(newTitle, articleService.findOne(article.getId()).get().getTitle()); + assertEquals(newTitle, articleRepository.findById(article.getId()).get().getTitle()); } @Test @@ -147,11 +147,11 @@ public class ElasticSearchManualTest { .withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); assertEquals(1, articles.size()); - final long count = articleService.count(); + final long count = articleRepository.count(); - articleService.delete(articles.get(0)); + articleRepository.delete(articles.get(0)); - assertEquals(count - 1, articleService.count()); + assertEquals(count - 1, articleRepository.count()); } @Test diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index 438d3adf02..ff804daa20 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -17,7 +17,7 @@ import java.util.Map; import com.baeldung.spring.data.es.config.Config; import com.baeldung.spring.data.es.model.Article; import com.baeldung.spring.data.es.model.Author; -import com.baeldung.spring.data.es.service.ArticleService; +import com.baeldung.spring.data.es.repository.ArticleRepository; import org.apache.lucene.search.join.ScoreMode; import org.elasticsearch.action.search.SearchRequest; @@ -58,7 +58,7 @@ public class ElasticSearchQueryManualTest { private ElasticsearchRestTemplate elasticsearchTemplate; @Autowired - private ArticleService articleService; + private ArticleRepository articleRepository; @Autowired private RestHighLevelClient client; @@ -76,22 +76,22 @@ public class ElasticSearchQueryManualTest { Article article = new Article("Spring Data Elasticsearch"); article.setAuthors(asList(johnSmith, johnDoe)); article.setTags("elasticsearch", "spring data"); - articleService.save(article); + articleRepository.save(article); article = new Article("Search engines"); article.setAuthors(asList(johnDoe)); article.setTags("search engines", "tutorial"); - articleService.save(article); + articleRepository.save(article); article = new Article("Second Article About Elasticsearch"); article.setAuthors(asList(johnSmith)); article.setTags("elasticsearch", "spring data"); - articleService.save(article); + articleRepository.save(article); article = new Article("Elasticsearch Tutorial"); article.setAuthors(asList(johnDoe)); article.setTags("elasticsearch"); - articleService.save(article); + articleRepository.save(article); } @Test From 6f9e27dc5957a4a30549b8828d619ccda813e365 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Wed, 29 Apr 2020 21:23:48 -0400 Subject: [PATCH 18/74] fixed most tests --- .../spring-data-elasticsearch/pom.xml | 27 ++-- .../com/baeldung/SpringContextManualTest.java | 6 +- .../ElasticSearchManualTest.java | 53 ++++--- .../elasticsearch/GeoQueriesManualTest.java | 136 ++++++++++++------ .../data/es/ElasticSearchManualTest.java | 51 +++---- .../data/es/ElasticSearchQueryManualTest.java | 114 +++++++++------ 6 files changed, 237 insertions(+), 150 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index e2089eb8b1..72860cb136 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -52,13 +52,13 @@ org.locationtech.jts jts-core - 1.16.1 - - - - org.apache.logging.log4j - log4j-core - ${log4j.version} + 1.15.0 + + + xerces + xercesImpl + + @@ -77,12 +77,19 @@ - 3.2.6.RELEASE + 4.0.0.RC1 4.5.2 1.2.47 - 0.6 + 0.7 1.13 - 2.9.1 + + + repository.spring.milestone + Spring Milestone Repository + https://repo.spring.io/milestone + + + \ No newline at end of file diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java index 2e9352b081..5087862061 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java @@ -8,9 +8,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.baeldung.spring.data.es.config.Config; /** - * - * This Manual test requires: * Elasticsearch instance running on host - * + * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * The following docker command can be used: + * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index f8f70c58a4..3402a10b81 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -9,12 +9,15 @@ import java.util.Arrays; import java.util.Date; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; import com.alibaba.fastjson.JSON; import org.elasticsearch.action.DocWriteResponse.Result; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; +import org.elasticsearch.action.get.GetRequest; +import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; @@ -34,10 +37,9 @@ import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.RestClients; /** - * - * This Manual test requires: * Elasticsearch instance running on host * with - * cluster name = elasticsearch - * + * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * The following docker command can be used: + * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ public class ElasticSearchManualTest { private List listOfPersons = new ArrayList<>(); @@ -57,28 +59,35 @@ public class ElasticSearchManualTest { @Test public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; - IndexRequest request = new IndexRequest("people", "Doe"); + IndexRequest request = new IndexRequest("people"); request.source(jsonObject, XContentType.JSON); - + IndexResponse response = client.index(request, RequestOptions.DEFAULT); String index = response.getIndex(); - + long version = response.getVersion(); + assertEquals(Result.CREATED, response.getResult()); - assertEquals(index, "people"); + assertEquals(1, version); + assertEquals("people", index); } @Test public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; - IndexRequest indexRequest = new IndexRequest("people", "Doe"); + IndexRequest indexRequest = new IndexRequest("people"); indexRequest.source(jsonObject, XContentType.JSON); IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); String id = response.getId(); + GetRequest getRequest = new GetRequest("people"); + getRequest.id(id); + + GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); + System.out.println(getResponse.getSourceAsString()); + DeleteRequest deleteRequest = new DeleteRequest("people"); deleteRequest.id(id); - deleteRequest.type("Doe"); DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); @@ -98,8 +107,8 @@ public class ElasticSearchManualTest { @Test public void givenSearchParameters_thenReturnResults() throws Exception { - SearchSourceBuilder builder = new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)) - .from(0).size(60).explain(true); + SearchSourceBuilder builder = + new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)); SearchRequest searchRequest = new SearchRequest(); searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); @@ -107,8 +116,7 @@ public class ElasticSearchManualTest { SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")) - .from(0).size(60).explain(true); + builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")); searchRequest = new SearchRequest(); searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); @@ -116,8 +124,7 @@ public class ElasticSearchManualTest { SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT); - builder = new SearchSourceBuilder().postFilter(QueryBuilders.matchQuery("John", "Name*")).from(0).size(60) - .explain(true); + builder = new SearchSourceBuilder().postFilter(QueryBuilders.matchQuery("John", "Name*")); searchRequest = new SearchRequest(); searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); searchRequest.source(builder); @@ -126,9 +133,15 @@ public class ElasticSearchManualTest { response2.getHits(); response3.getHits(); - - final List results = Arrays.stream(response.getHits().getHits()) - .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)).collect(Collectors.toList()); + + final List results = + Stream.of(response.getHits().getHits(), + response2.getHits().getHits(), + response3.getHits().getHits()) + .flatMap(Arrays::stream) + .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) + .collect(Collectors.toList()); + results.forEach(System.out::println); } @@ -137,7 +150,7 @@ public class ElasticSearchManualTest { XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("fullName", "Test") .field("salary", "11500").field("age", "10").endObject(); - IndexRequest indexRequest = new IndexRequest("people", "Doe"); + IndexRequest indexRequest = new IndexRequest("people"); indexRequest.source(builder); IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java index e373fef7a6..bd54183263 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java @@ -8,71 +8,84 @@ import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; -import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; +import com.baeldung.spring.data.es.config.Config; + +import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; +import org.elasticsearch.action.admin.indices.refresh.RefreshRequest; +import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; +import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; -import org.elasticsearch.client.Client; +import org.elasticsearch.client.RequestOptions; +import org.elasticsearch.client.RestHighLevelClient; +import org.elasticsearch.client.indices.CreateIndexRequest; import org.elasticsearch.common.geo.GeoPoint; +import org.elasticsearch.common.geo.ShapeRelation; import org.elasticsearch.common.geo.builders.EnvelopeBuilder; import org.elasticsearch.common.unit.DistanceUnit; import org.elasticsearch.common.xcontent.XContentType; +import org.elasticsearch.index.query.GeoShapeQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; +import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.locationtech.jts.geom.Coordinate; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; -import com.baeldung.spring.data.es.config.Config; - /** - * - * This Manual test requires: * Elasticsearch instance running on host * with - * cluster name = elasticsearch * and further configurations - * + * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * The following docker command can be used: + * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) public class GeoQueriesManualTest { private static final String WONDERS_OF_WORLD = "wonders-of-world"; - private static final String WONDERS = "Wonders"; @Autowired - private ElasticsearchTemplate elasticsearchTemplate; - - @Autowired - private Client client; + private RestHighLevelClient client; @Before - public void setUp() { - String jsonObject = "{\"Wonders\":{\"properties\":{\"name\":{\"type\":\"string\",\"index\":\"not_analyzed\"},\"region\":{\"type\":\"geo_shape\",\"tree\":\"quadtree\",\"precision\":\"1m\"},\"location\":{\"type\":\"geo_point\"}}}}"; + public void setUp() throws Exception { + String jsonObject = "{\"properties\":{\"name\":{\"type\":\"text\",\"index\":false},\"region\":{\"type\":\"geo_shape\"},\"location\":{\"type\":\"geo_point\"}}}"; + CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD); - req.mapping(WONDERS, jsonObject, XContentType.JSON); - client.admin().indices().create(req).actionGet(); + req.mapping(jsonObject, XContentType.JSON); + + client.indices().create(req, RequestOptions.DEFAULT); } @Test public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException { String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,30.2],[80.1, 25]]}}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) - .get(); + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); String tajMahalId = response.getId(); - client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); + + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); Coordinate topLeft = new Coordinate(74, 31.2); Coordinate bottomRight = new Coordinate(81.1, 24); - QueryBuilder qb = QueryBuilders.geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight)); + + GeoShapeQueryBuilder qb = QueryBuilders.geoShapeQuery("region", + new EnvelopeBuilder(topLeft, bottomRight).buildGeometry()); + qb.relation(ShapeRelation.INTERSECTS); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() - .actionGet(); + SearchSourceBuilder source = new SearchSourceBuilder().query(qb); + SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); + searchRequest.source(source); + + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) .collect(Collectors.toList()); @@ -81,62 +94,93 @@ public class GeoQueriesManualTest { } @Test - public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() { + public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() throws Exception { String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) - .get(); + + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + String pyramidsOfGizaId = response.getId(); - client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); + + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location").setCorners(31, 30, 28, 32); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() - .actionGet(); + SearchSourceBuilder source = new SearchSourceBuilder().query(qb); + SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); + searchRequest.source(source); + + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) .collect(Collectors.toList()); assertTrue(ids.contains(pyramidsOfGizaId)); } @Test - public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() { + public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() throws Exception { String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) - .get(); + + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + String lighthouseOfAlexandriaId = response.getId(); - client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); - QueryBuilder qb = QueryBuilders.geoDistanceQuery("location").point(29.976, 31.131).distance(10, DistanceUnit.MILES); + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); + + QueryBuilder qb = + QueryBuilders.geoDistanceQuery("location") + .point(29.976, 31.131) + .distance(10, DistanceUnit.MILES); + + SearchSourceBuilder source = new SearchSourceBuilder().query(qb); + SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); + searchRequest.source(source); + + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() - .actionGet(); List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) .collect(Collectors.toList()); assertTrue(ids.contains(lighthouseOfAlexandriaId)); } @Test - public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() { + public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() throws Exception { String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; - IndexResponse response = client.prepareIndex(WONDERS_OF_WORLD, WONDERS).setSource(jsonObject, XContentType.JSON) - .get(); - String greatRannOfKutchid = response.getId(); - client.admin().indices().prepareRefresh(WONDERS_OF_WORLD).get(); + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + + String greatRannOfKutchid = response.getId(); + + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); + List allPoints = new ArrayList(); allPoints.add(new GeoPoint(22.733, 68.859)); allPoints.add(new GeoPoint(24.733, 68.859)); allPoints.add(new GeoPoint(23, 70.859)); QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints); - SearchResponse searchResponse = client.prepareSearch(WONDERS_OF_WORLD).setTypes(WONDERS).setQuery(qb).execute() - .actionGet(); + SearchSourceBuilder source = new SearchSourceBuilder().query(qb); + SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); + searchRequest.source(source); + + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) .collect(Collectors.toList()); assertTrue(ids.contains(greatRannOfKutchid)); } @After - public void destroy() { - elasticsearchTemplate.deleteIndex(WONDERS_OF_WORLD); + public void destroy() throws Exception { + DeleteIndexRequest deleteIndex = new DeleteIndexRequest(WONDERS_OF_WORLD); + client.indices().delete(deleteIndex, RequestOptions.DEFAULT); } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index 6e1d7d1b7d..e1c0994d3e 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -22,16 +22,17 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.domain.Page; import org.springframework.data.domain.PageRequest; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; +import org.springframework.data.elasticsearch.core.SearchHits; +import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; -import org.springframework.data.elasticsearch.core.query.SearchQuery; +import org.springframework.data.elasticsearch.core.query.Query; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * - * This Manual test requires: * Elasticsearch instance running on host * with - * cluster name = elasticsearch - * + * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * The following docker command can be used: + * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) @@ -48,8 +49,8 @@ public class ElasticSearchManualTest { @Before public void before() { - elasticsearchTemplate.deleteIndex(Article.class); - elasticsearchTemplate.createIndex(Article.class); + elasticsearchTemplate.indexOps(Article.class).delete(); + elasticsearchTemplate.indexOps(Article.class).create(); // don't call putMapping() to test the default mappings Article article = new Article("Spring Data Elasticsearch"); @@ -86,7 +87,6 @@ public class ElasticSearchManualTest { @Test public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() { - final Page
articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(), PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); @@ -115,22 +115,23 @@ public class ElasticSearchManualTest { @Test public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() { - - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")) + final Query searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")) .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + final SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); } @Test public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - assertEquals(1, articles.size()); + assertEquals(1, articles.getTotalHits()); - final Article article = articles.get(0); + final Article article = articles.getSearchHit(0).getContent(); final String newTitle = "Getting started with Search Engines"; article.setTitle(newTitle); articleRepository.save(article); @@ -140,25 +141,27 @@ public class ElasticSearchManualTest { @Test public void givenSavedDoc_whenDelete_thenRemovedFromIndex() { - final String articleTitle = "Spring Data Elasticsearch"; - final SearchQuery searchQuery = new NativeSearchQueryBuilder() + final Query searchQuery = new NativeSearchQueryBuilder() .withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + final SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); final long count = articleRepository.count(); - articleRepository.delete(articles.get(0)); + articleRepository.delete(articles.getSearchHit(0).getContent()); assertEquals(count - 1, articleRepository.count()); } @Test public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() + final Query searchQuery = new NativeSearchQueryBuilder() .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + final SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + assertEquals(1, articles.getTotalHits()); } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index ff804daa20..2300ff50db 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -2,7 +2,6 @@ package com.baeldung.spring.data.es; import static java.util.Arrays.asList; import static java.util.stream.Collectors.toList; -import static org.elasticsearch.index.query.Operator.AND; import static org.elasticsearch.index.query.QueryBuilders.boolQuery; import static org.elasticsearch.index.query.QueryBuilders.matchPhraseQuery; import static org.elasticsearch.index.query.QueryBuilders.matchQuery; @@ -26,6 +25,7 @@ import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.unit.Fuzziness; import org.elasticsearch.index.query.MultiMatchQueryBuilder; +import org.elasticsearch.index.query.Operator; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.search.aggregations.Aggregation; import org.elasticsearch.search.aggregations.AggregationBuilders; @@ -39,16 +39,17 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate; +import org.springframework.data.elasticsearch.core.SearchHits; +import org.springframework.data.elasticsearch.core.mapping.IndexCoordinates; +import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder; -import org.springframework.data.elasticsearch.core.query.SearchQuery; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * - * This Manual test requires: * Elasticsearch instance running on host * with - * cluster name = elasticsearch - * + * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * The following docker command can be used: + * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) @@ -68,10 +69,10 @@ public class ElasticSearchQueryManualTest { @Before public void before() { - elasticsearchTemplate.deleteIndex(Article.class); - elasticsearchTemplate.createIndex(Article.class); - elasticsearchTemplate.putMapping(Article.class); - elasticsearchTemplate.refresh(Article.class); + elasticsearchTemplate.indexOps(Article.class).delete(); + elasticsearchTemplate.indexOps(Article.class).create(); + elasticsearchTemplate.indexOps(Article.class).createMapping(); + elasticsearchTemplate.indexOps(Article.class).refresh();; Article article = new Article("Spring Data Elasticsearch"); article.setAuthors(asList(johnSmith, johnDoe)); @@ -96,40 +97,55 @@ public class ElasticSearchQueryManualTest { @Test public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title", "Search engines").operator(Operator.AND)).build(); + final SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + assertEquals(1, articles.getTotalHits()); } @Test public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")) + final NativeSearchQuery searchQuery = + new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")) .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); - assertEquals("Search engines", articles.get(0).getTitle()); + + final SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); + assertEquals("Search engines", articles.getSearchHit(0).getContent().getTitle()); } @Test public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")) + final NativeSearchQuery searchQuery = + new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")) .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(3, articles.size()); + + final SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(3, articles.getTotalHits()); } @Test public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { - SearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")).build(); - List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")) + .build(); - searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")) - .build(); - articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(0, articles.size()); + SearchHits
articles = + elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); + + searchQuery = new NativeSearchQueryBuilder() + .withQuery(matchQuery("title.verbatim", "Second Article About")) + .build(); + + articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + assertEquals(0, articles.getTotalHits()); } @Test @@ -137,10 +153,10 @@ public class ElasticSearchQueryManualTest { final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), ScoreMode.None); - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - assertEquals(2, articles.size()); + assertEquals(2, articles.getTotalHits()); } @Test @@ -148,7 +164,7 @@ public class ElasticSearchQueryManualTest { final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); - final SearchRequest searchRequest = new SearchRequest("blog").types("article").source(builder); + final SearchRequest searchRequest = new SearchRequest("blog").source(builder); final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); @@ -167,7 +183,7 @@ public class ElasticSearchQueryManualTest { .order(BucketOrder.count(false)); final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); - final SearchRequest searchRequest = new SearchRequest().indices("blog").types("article").source(builder); + final SearchRequest searchRequest = new SearchRequest().indices("blog").source(builder); final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); @@ -181,32 +197,36 @@ public class ElasticSearchQueryManualTest { @Test public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); } @Test public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery( - matchQuery("title", "spring date elasticserch").operator(AND).fuzziness(Fuzziness.ONE).prefixLength(3)) + matchQuery("title", "spring date elasticserch").operator(Operator.AND).fuzziness(Fuzziness.ONE).prefixLength(3)) .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(1, articles.size()); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); } @Test public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { - final SearchQuery searchQuery = new NativeSearchQueryBuilder() + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() .withQuery( multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)) .build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); - assertEquals(2, articles.size()); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(2, articles.getTotalHits()); } @Test @@ -215,9 +235,9 @@ public class ElasticSearchQueryManualTest { .must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None)) .filter(termQuery("tags", "elasticsearch")); - final SearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); - final List
articles = elasticsearchTemplate.queryForList(searchQuery, Article.class); + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - assertEquals(2, articles.size()); + assertEquals(2, articles.getTotalHits()); } } From 1ed9c7db328be10c999c321f3dbca82f29b011dc Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 12 May 2020 21:41:51 -0400 Subject: [PATCH 19/74] fixed mapping and tests --- .../com/baeldung/spring/data/es/model/Author.java | 5 +++++ .../com/baeldung/SpringContextManualTest.java | 4 +++- .../elasticsearch/ElasticSearchManualTest.java | 4 +++- .../elasticsearch/GeoQueriesManualTest.java | 4 +++- .../spring/data/es/ElasticSearchManualTest.java | 14 +++++++++----- .../data/es/ElasticSearchQueryManualTest.java | 15 +++++++++------ 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Author.java b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Author.java index 38f50e1614..1d596cd92b 100644 --- a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Author.java +++ b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/model/Author.java @@ -1,7 +1,12 @@ package com.baeldung.spring.data.es.model; +import static org.springframework.data.elasticsearch.annotations.FieldType.Text; + +import org.springframework.data.elasticsearch.annotations.Field; + public class Author { + @Field(type = Text) private String name; public Author() { diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java index 5087862061..27b41649ee 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java @@ -8,7 +8,9 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.baeldung.spring.data.es.config.Config; /** - * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * This Manual test requires: + * Elasticsearch instance running on localhost:9200. + * * The following docker command can be used: * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index 3402a10b81..d19a7a379f 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -37,7 +37,9 @@ import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.RestClients; /** - * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * This Manual test requires: + * Elasticsearch instance running on localhost:9200. + * * The following docker command can be used: * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java index bd54183263..ed67818d6c 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java @@ -39,7 +39,9 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * This Manual test requires: + * Elasticsearch instance running on localhost:9200. + * * The following docker command can be used: * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index e1c0994d3e..87eab00da0 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -15,6 +15,7 @@ import com.baeldung.spring.data.es.model.Article; import com.baeldung.spring.data.es.model.Author; import com.baeldung.spring.data.es.repository.ArticleRepository; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -30,7 +31,9 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * This Manual test requires: + * Elasticsearch instance running on localhost:9200. + * * The following docker command can be used: * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @@ -49,10 +52,6 @@ public class ElasticSearchManualTest { @Before public void before() { - elasticsearchTemplate.indexOps(Article.class).delete(); - elasticsearchTemplate.indexOps(Article.class).create(); - // don't call putMapping() to test the default mappings - Article article = new Article("Spring Data Elasticsearch"); article.setAuthors(asList(johnSmith, johnDoe)); article.setTags("elasticsearch", "spring data"); @@ -74,6 +73,11 @@ public class ElasticSearchManualTest { articleRepository.save(article); } + @After + public void after() { + articleRepository.deleteAll(); + } + @Test public void givenArticleService_whenSaveArticle_thenIdIsAssigned() { final List authors = asList(new Author("John Smith"), johnDoe); diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index 2300ff50db..b9de9d51af 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -34,6 +34,7 @@ import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation; import org.elasticsearch.search.aggregations.bucket.terms.ParsedStringTerms; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.builder.SearchSourceBuilder; +import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; @@ -47,7 +48,9 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * This Manual test requires: * Elasticsearch instance running on localhost:9200. + * This Manual test requires: + * Elasticsearch instance running on localhost:9200. + * * The following docker command can be used: * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @@ -69,11 +72,6 @@ public class ElasticSearchQueryManualTest { @Before public void before() { - elasticsearchTemplate.indexOps(Article.class).delete(); - elasticsearchTemplate.indexOps(Article.class).create(); - elasticsearchTemplate.indexOps(Article.class).createMapping(); - elasticsearchTemplate.indexOps(Article.class).refresh();; - Article article = new Article("Spring Data Elasticsearch"); article.setAuthors(asList(johnSmith, johnDoe)); article.setTags("elasticsearch", "spring data"); @@ -95,6 +93,11 @@ public class ElasticSearchQueryManualTest { articleRepository.save(article); } + @After + public void after() { + articleRepository.deleteAll(); + } + @Test public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() From 40aa75eec10e32fd16237a5f0fbb3f583ca012c2 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 19 May 2020 19:21:51 -0400 Subject: [PATCH 20/74] fixed formatting according to eclipse formatter --- .../spring/data/es/config/Config.java | 21 +- .../com/baeldung/SpringContextManualTest.java | 7 +- .../ElasticSearchManualTest.java | 199 ++++++------ .../elasticsearch/GeoQueriesManualTest.java | 228 ++++++------- .../data/es/ElasticSearchManualTest.java | 55 ++-- .../data/es/ElasticSearchQueryManualTest.java | 304 +++++++++--------- 6 files changed, 419 insertions(+), 395 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java index f98342486f..51bbe73e9e 100644 --- a/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java +++ b/persistence-modules/spring-data-elasticsearch/src/main/java/com/baeldung/spring/data/es/config/Config.java @@ -15,15 +15,18 @@ import org.springframework.data.elasticsearch.repository.config.EnableElasticsea @ComponentScan(basePackages = { "com.baeldung.spring.data.es.service" }) public class Config { - @Bean - RestHighLevelClient client() { - ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build(); + @Bean + RestHighLevelClient client() { + ClientConfiguration clientConfiguration = ClientConfiguration.builder() + .connectedTo("localhost:9200") + .build(); - return RestClients.create(clientConfiguration).rest(); - } + return RestClients.create(clientConfiguration) + .rest(); + } - @Bean - public ElasticsearchOperations elasticsearchTemplate() { - return new ElasticsearchRestTemplate(client()); - } + @Bean + public ElasticsearchOperations elasticsearchTemplate() { + return new ElasticsearchRestTemplate(client()); + } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java index 27b41649ee..bff910cf93 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java @@ -8,11 +8,10 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import com.baeldung.spring.data.es.config.Config; /** - * This Manual test requires: - * Elasticsearch instance running on localhost:9200. + * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: - * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es761 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index d19a7a379f..e4b3f6d801 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -37,126 +37,137 @@ import org.springframework.data.elasticsearch.client.ClientConfiguration; import org.springframework.data.elasticsearch.client.RestClients; /** - * This Manual test requires: - * Elasticsearch instance running on localhost:9200. + * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: - * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es761 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ public class ElasticSearchManualTest { - private List listOfPersons = new ArrayList<>(); - private RestHighLevelClient client = null; + private List listOfPersons = new ArrayList<>(); + private RestHighLevelClient client = null; - @Before - public void setUp() throws UnknownHostException { - Person person1 = new Person(10, "John Doe", new Date()); - Person person2 = new Person(25, "Janette Doe", new Date()); - listOfPersons.add(person1); - listOfPersons.add(person2); + @Before + public void setUp() throws UnknownHostException { + Person person1 = new Person(10, "John Doe", new Date()); + Person person2 = new Person(25, "Janette Doe", new Date()); + listOfPersons.add(person1); + listOfPersons.add(person2); - ClientConfiguration clientConfiguration = ClientConfiguration.builder().connectedTo("localhost:9200").build(); - client = RestClients.create(clientConfiguration).rest(); - } + ClientConfiguration clientConfiguration = ClientConfiguration.builder() + .connectedTo("localhost:9200") + .build(); + client = RestClients.create(clientConfiguration) + .rest(); + } - @Test - public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { - String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; - IndexRequest request = new IndexRequest("people"); - request.source(jsonObject, XContentType.JSON); - - IndexResponse response = client.index(request, RequestOptions.DEFAULT); - String index = response.getIndex(); - long version = response.getVersion(); - - assertEquals(Result.CREATED, response.getResult()); - assertEquals(1, version); - assertEquals("people", index); - } + @Test + public void givenJsonString_whenJavaObject_thenIndexDocument() throws Exception { + String jsonObject = "{\"age\":20,\"dateOfBirth\":1471466076564,\"fullName\":\"John Doe\"}"; + IndexRequest request = new IndexRequest("people"); + request.source(jsonObject, XContentType.JSON); - @Test - public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { - String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; - IndexRequest indexRequest = new IndexRequest("people"); - indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(request, RequestOptions.DEFAULT); + String index = response.getIndex(); + long version = response.getVersion(); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - String id = response.getId(); + assertEquals(Result.CREATED, response.getResult()); + assertEquals(1, version); + assertEquals("people", index); + } - GetRequest getRequest = new GetRequest("people"); - getRequest.id(id); + @Test + public void givenDocumentId_whenJavaObject_thenDeleteDocument() throws Exception { + String jsonObject = "{\"age\":10,\"dateOfBirth\":1471455886564,\"fullName\":\"Johan Doe\"}"; + IndexRequest indexRequest = new IndexRequest("people"); + indexRequest.source(jsonObject, XContentType.JSON); - GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); - System.out.println(getResponse.getSourceAsString()); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + String id = response.getId(); - DeleteRequest deleteRequest = new DeleteRequest("people"); - deleteRequest.id(id); + GetRequest getRequest = new GetRequest("people"); + getRequest.id(id); - DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); + GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT); + System.out.println(getResponse.getSourceAsString()); - assertEquals(Result.DELETED, deleteResponse.getResult()); - } + DeleteRequest deleteRequest = new DeleteRequest("people"); + deleteRequest.id(id); - @Test - public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception { - SearchRequest searchRequest = new SearchRequest(); - SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - SearchHit[] searchHits = response.getHits().getHits(); - List results = Arrays.stream(searchHits).map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) - .collect(Collectors.toList()); + DeleteResponse deleteResponse = client.delete(deleteRequest, RequestOptions.DEFAULT); - results.forEach(System.out::println); - } + assertEquals(Result.DELETED, deleteResponse.getResult()); + } - @Test - public void givenSearchParameters_thenReturnResults() throws Exception { - SearchSourceBuilder builder = - new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age").from(5).to(15)); + @Test + public void givenSearchRequest_whenMatchAll_thenReturnAllResults() throws Exception { + SearchRequest searchRequest = new SearchRequest(); + SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + SearchHit[] searchHits = response.getHits() + .getHits(); + List results = Arrays.stream(searchHits) + .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) + .collect(Collectors.toList()); - SearchRequest searchRequest = new SearchRequest(); - searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); - searchRequest.source(builder); + results.forEach(System.out::println); + } - SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + @Test + public void givenSearchParameters_thenReturnResults() throws Exception { + SearchSourceBuilder builder = new SearchSourceBuilder().postFilter(QueryBuilders.rangeQuery("age") + .from(5) + .to(15)); - builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")); + SearchRequest searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); - searchRequest = new SearchRequest(); - searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); - searchRequest.source(builder); + SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT); + builder = new SearchSourceBuilder().postFilter(QueryBuilders.simpleQueryStringQuery("+John -Doe OR Janette")); - builder = new SearchSourceBuilder().postFilter(QueryBuilders.matchQuery("John", "Name*")); - searchRequest = new SearchRequest(); - searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); - searchRequest.source(builder); + searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); - SearchResponse response3 = client.search(searchRequest, RequestOptions.DEFAULT); + SearchResponse response2 = client.search(searchRequest, RequestOptions.DEFAULT); - response2.getHits(); - response3.getHits(); - - final List results = - Stream.of(response.getHits().getHits(), - response2.getHits().getHits(), - response3.getHits().getHits()) - .flatMap(Arrays::stream) - .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) - .collect(Collectors.toList()); - - results.forEach(System.out::println); - } + builder = new SearchSourceBuilder().postFilter(QueryBuilders.matchQuery("John", "Name*")); + searchRequest = new SearchRequest(); + searchRequest.searchType(SearchType.DFS_QUERY_THEN_FETCH); + searchRequest.source(builder); - @Test - public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException { - XContentBuilder builder = XContentFactory.jsonBuilder().startObject().field("fullName", "Test") - .field("salary", "11500").field("age", "10").endObject(); + SearchResponse response3 = client.search(searchRequest, RequestOptions.DEFAULT); - IndexRequest indexRequest = new IndexRequest("people"); - indexRequest.source(builder); + response2.getHits(); + response3.getHits(); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + final List results = Stream.of(response.getHits() + .getHits(), + response2.getHits() + .getHits(), + response3.getHits() + .getHits()) + .flatMap(Arrays::stream) + .map(hit -> JSON.parseObject(hit.getSourceAsString(), Person.class)) + .collect(Collectors.toList()); - assertEquals(Result.CREATED, response.getResult()); - } + results.forEach(System.out::println); + } + + @Test + public void givenContentBuilder_whenHelpers_thanIndexJson() throws IOException { + XContentBuilder builder = XContentFactory.jsonBuilder() + .startObject() + .field("fullName", "Test") + .field("salary", "11500") + .field("age", "10") + .endObject(); + + IndexRequest indexRequest = new IndexRequest("people"); + indexRequest.source(builder); + + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + + assertEquals(Result.CREATED, response.getResult()); + } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java index ed67818d6c..a36e08d6d2 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java @@ -39,150 +39,162 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * This Manual test requires: - * Elasticsearch instance running on localhost:9200. + * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: - * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es761 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) public class GeoQueriesManualTest { - private static final String WONDERS_OF_WORLD = "wonders-of-world"; + private static final String WONDERS_OF_WORLD = "wonders-of-world"; - @Autowired - private RestHighLevelClient client; + @Autowired + private RestHighLevelClient client; - @Before - public void setUp() throws Exception { - String jsonObject = "{\"properties\":{\"name\":{\"type\":\"text\",\"index\":false},\"region\":{\"type\":\"geo_shape\"},\"location\":{\"type\":\"geo_point\"}}}"; - - CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD); - req.mapping(jsonObject, XContentType.JSON); + @Before + public void setUp() throws Exception { + String jsonObject = "{\"properties\":{\"name\":{\"type\":\"text\",\"index\":false},\"region\":{\"type\":\"geo_shape\"},\"location\":{\"type\":\"geo_point\"}}}"; - client.indices().create(req, RequestOptions.DEFAULT); - } + CreateIndexRequest req = new CreateIndexRequest(WONDERS_OF_WORLD); + req.mapping(jsonObject, XContentType.JSON); - @Test - public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException { - String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,30.2],[80.1, 25]]}}"; - IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); - indexRequest.source(jsonObject, XContentType.JSON); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + client.indices() + .create(req, RequestOptions.DEFAULT); + } - String tajMahalId = response.getId(); + @Test + public void givenGeoShapeData_whenExecutedGeoShapeQuery_thenResultNonEmpty() throws IOException { + String jsonObject = "{\"name\":\"Agra\",\"region\":{\"type\":\"envelope\",\"coordinates\":[[75,30.2],[80.1, 25]]}}"; + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); - client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); + String tajMahalId = response.getId(); - Coordinate topLeft = new Coordinate(74, 31.2); - Coordinate bottomRight = new Coordinate(81.1, 24); - - GeoShapeQueryBuilder qb = QueryBuilders.geoShapeQuery("region", - new EnvelopeBuilder(topLeft, bottomRight).buildGeometry()); - qb.relation(ShapeRelation.INTERSECTS); + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices() + .refresh(refreshRequest, RequestOptions.DEFAULT); - SearchSourceBuilder source = new SearchSourceBuilder().query(qb); - SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); - searchRequest.source(source); - - SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + Coordinate topLeft = new Coordinate(74, 31.2); + Coordinate bottomRight = new Coordinate(81.1, 24); - List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) - .collect(Collectors.toList()); + GeoShapeQueryBuilder qb = QueryBuilders.geoShapeQuery("region", new EnvelopeBuilder(topLeft, bottomRight).buildGeometry()); + qb.relation(ShapeRelation.INTERSECTS); - assertTrue(ids.contains(tajMahalId)); - } + SearchSourceBuilder source = new SearchSourceBuilder().query(qb); + SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); + searchRequest.source(source); - @Test - public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() throws Exception { - String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); - IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); - indexRequest.source(jsonObject, XContentType.JSON); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + List ids = Arrays.stream(searchResponse.getHits() + .getHits()) + .map(SearchHit::getId) + .collect(Collectors.toList()); - String pyramidsOfGizaId = response.getId(); + assertTrue(ids.contains(tajMahalId)); + } - RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); - client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); + @Test + public void givenGeoPointData_whenExecutedGeoBoundingBoxQuery_thenResultNonEmpty() throws Exception { + String jsonObject = "{\"name\":\"Pyramids of Giza\",\"location\":[31.131302,29.976480]}"; - QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location").setCorners(31, 30, 28, 32); + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - SearchSourceBuilder source = new SearchSourceBuilder().query(qb); - SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); - searchRequest.source(source); + String pyramidsOfGizaId = response.getId(); - SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices() + .refresh(refreshRequest, RequestOptions.DEFAULT); - List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) - .collect(Collectors.toList()); - assertTrue(ids.contains(pyramidsOfGizaId)); - } + QueryBuilder qb = QueryBuilders.geoBoundingBoxQuery("location") + .setCorners(31, 30, 28, 32); - @Test - public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() throws Exception { - String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; - - IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); - indexRequest.source(jsonObject, XContentType.JSON); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); + SearchSourceBuilder source = new SearchSourceBuilder().query(qb); + SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); + searchRequest.source(source); - String lighthouseOfAlexandriaId = response.getId(); + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); - RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); - client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); + List ids = Arrays.stream(searchResponse.getHits() + .getHits()) + .map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(pyramidsOfGizaId)); + } - QueryBuilder qb = - QueryBuilders.geoDistanceQuery("location") - .point(29.976, 31.131) - .distance(10, DistanceUnit.MILES); + @Test + public void givenGeoPointData_whenExecutedGeoDistanceQuery_thenResultNonEmpty() throws Exception { + String jsonObject = "{\"name\":\"Lighthouse of alexandria\",\"location\":[31.131302,29.976480]}"; - SearchSourceBuilder source = new SearchSourceBuilder().query(qb); - SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); - searchRequest.source(source); + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + String lighthouseOfAlexandriaId = response.getId(); - List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) - .collect(Collectors.toList()); - assertTrue(ids.contains(lighthouseOfAlexandriaId)); - } + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices() + .refresh(refreshRequest, RequestOptions.DEFAULT); - @Test - public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() throws Exception { - String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; + QueryBuilder qb = QueryBuilders.geoDistanceQuery("location") + .point(29.976, 31.131) + .distance(10, DistanceUnit.MILES); - IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); - indexRequest.source(jsonObject, XContentType.JSON); - IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - - String greatRannOfKutchid = response.getId(); + SearchSourceBuilder source = new SearchSourceBuilder().query(qb); + SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); + searchRequest.source(source); - RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); - client.indices().refresh(refreshRequest, RequestOptions.DEFAULT); - - List allPoints = new ArrayList(); - allPoints.add(new GeoPoint(22.733, 68.859)); - allPoints.add(new GeoPoint(24.733, 68.859)); - allPoints.add(new GeoPoint(23, 70.859)); - QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints); + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); - SearchSourceBuilder source = new SearchSourceBuilder().query(qb); - SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); - searchRequest.source(source); + List ids = Arrays.stream(searchResponse.getHits() + .getHits()) + .map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(lighthouseOfAlexandriaId)); + } - SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + @Test + public void givenGeoPointData_whenExecutedGeoPolygonQuery_thenResultNonEmpty() throws Exception { + String jsonObject = "{\"name\":\"The Great Rann of Kutch\",\"location\":[69.859741,23.733732]}"; - List ids = Arrays.stream(searchResponse.getHits().getHits()).map(SearchHit::getId) - .collect(Collectors.toList()); - assertTrue(ids.contains(greatRannOfKutchid)); - } + IndexRequest indexRequest = new IndexRequest(WONDERS_OF_WORLD); + indexRequest.source(jsonObject, XContentType.JSON); + IndexResponse response = client.index(indexRequest, RequestOptions.DEFAULT); - @After - public void destroy() throws Exception { - DeleteIndexRequest deleteIndex = new DeleteIndexRequest(WONDERS_OF_WORLD); - client.indices().delete(deleteIndex, RequestOptions.DEFAULT); - } + String greatRannOfKutchid = response.getId(); + + RefreshRequest refreshRequest = new RefreshRequest(WONDERS_OF_WORLD); + client.indices() + .refresh(refreshRequest, RequestOptions.DEFAULT); + + List allPoints = new ArrayList(); + allPoints.add(new GeoPoint(22.733, 68.859)); + allPoints.add(new GeoPoint(24.733, 68.859)); + allPoints.add(new GeoPoint(23, 70.859)); + QueryBuilder qb = QueryBuilders.geoPolygonQuery("location", allPoints); + + SearchSourceBuilder source = new SearchSourceBuilder().query(qb); + SearchRequest searchRequest = new SearchRequest(WONDERS_OF_WORLD); + searchRequest.source(source); + + SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT); + + List ids = Arrays.stream(searchResponse.getHits() + .getHits()) + .map(SearchHit::getId) + .collect(Collectors.toList()); + assertTrue(ids.contains(greatRannOfKutchid)); + } + + @After + public void destroy() throws Exception { + DeleteIndexRequest deleteIndex = new DeleteIndexRequest(WONDERS_OF_WORLD); + client.indices() + .delete(deleteIndex, RequestOptions.DEFAULT); + } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index 87eab00da0..9f713a276f 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -31,11 +31,10 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * This Manual test requires: - * Elasticsearch instance running on localhost:9200. + * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: - * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es761 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) @@ -77,7 +76,7 @@ public class ElasticSearchManualTest { public void after() { articleRepository.deleteAll(); } - + @Test public void givenArticleService_whenSaveArticle_thenIdIsAssigned() { final List authors = asList(new Author("John Smith"), johnDoe); @@ -91,81 +90,79 @@ public class ElasticSearchManualTest { @Test public void givenPersistedArticles_whenSearchByAuthorsName_thenRightFound() { - final Page
articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(), - PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleRepository.findByAuthorsName(johnSmith.getName(), PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenCustomQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleRepository.findByAuthorsNameUsingCustomQuery("Smith", - PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleRepository.findByAuthorsNameUsingCustomQuery("Smith", PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenTagFilterQuery_whenSearchByTag_thenArticleIsFound() { - final Page
articleByAuthorName = articleRepository.findByFilteredTagQuery("elasticsearch", - PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleRepository.findByFilteredTagQuery("elasticsearch", PageRequest.of(0, 10)); assertEquals(3L, articleByAuthorName.getTotalElements()); } @Test public void givenTagFilterQuery_whenSearchByAuthorsName_thenArticleIsFound() { - final Page
articleByAuthorName = articleRepository.findByAuthorsNameAndFilteredTagQuery("Doe", - "elasticsearch", PageRequest.of(0, 10)); + final Page
articleByAuthorName = articleRepository.findByAuthorsNameAndFilteredTagQuery("Doe", "elasticsearch", PageRequest.of(0, 10)); assertEquals(2L, articleByAuthorName.getTotalElements()); } @Test public void givenPersistedArticles_whenUseRegexQuery_thenRightArticlesFound() { final Query searchQuery = new NativeSearchQueryBuilder().withFilter(regexpQuery("title", ".*data.*")) - .build(); + .build(); - final SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); assertEquals(1, articles.getTotalHits()); } @Test public void givenSavedDoc_whenTitleUpdated_thenCouldFindByUpdatedTitle() { - final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")).build(); + final Query searchQuery = new NativeSearchQueryBuilder().withQuery(fuzzyQuery("title", "serch")) + .build(); final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); assertEquals(1, articles.getTotalHits()); - final Article article = articles.getSearchHit(0).getContent(); + final Article article = articles.getSearchHit(0) + .getContent(); final String newTitle = "Getting started with Search Engines"; article.setTitle(newTitle); articleRepository.save(article); - assertEquals(newTitle, articleRepository.findById(article.getId()).get().getTitle()); + assertEquals(newTitle, articleRepository.findById(article.getId()) + .get() + .getTitle()); } @Test public void givenSavedDoc_whenDelete_thenRemovedFromIndex() { final String articleTitle = "Spring Data Elasticsearch"; - final Query searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")).build(); - final SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - + final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", articleTitle).minimumShouldMatch("75%")) + .build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + assertEquals(1, articles.getTotalHits()); final long count = articleRepository.count(); - articleRepository.delete(articles.getSearchHit(0).getContent()); + articleRepository.delete(articles.getSearchHit(0) + .getContent()); assertEquals(count - 1, articleRepository.count()); } @Test public void givenSavedDoc_whenOneTermMatches_thenFindByTitle() { - final Query searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "Search engines").operator(AND)).build(); - final SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + final Query searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(AND)) + .build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); assertEquals(1, articles.getTotalHits()); } } diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index b9de9d51af..3cedab00e4 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -48,199 +48,201 @@ import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** - * This Manual test requires: - * Elasticsearch instance running on localhost:9200. + * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: - * docker run -d --name es761 -p 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es761 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) public class ElasticSearchQueryManualTest { - @Autowired - private ElasticsearchRestTemplate elasticsearchTemplate; + @Autowired + private ElasticsearchRestTemplate elasticsearchTemplate; - @Autowired - private ArticleRepository articleRepository; + @Autowired + private ArticleRepository articleRepository; - @Autowired - private RestHighLevelClient client; + @Autowired + private RestHighLevelClient client; - private final Author johnSmith = new Author("John Smith"); - private final Author johnDoe = new Author("John Doe"); + private final Author johnSmith = new Author("John Smith"); + private final Author johnDoe = new Author("John Doe"); - @Before - public void before() { - Article article = new Article("Spring Data Elasticsearch"); - article.setAuthors(asList(johnSmith, johnDoe)); - article.setTags("elasticsearch", "spring data"); - articleRepository.save(article); + @Before + public void before() { + Article article = new Article("Spring Data Elasticsearch"); + article.setAuthors(asList(johnSmith, johnDoe)); + article.setTags("elasticsearch", "spring data"); + articleRepository.save(article); - article = new Article("Search engines"); - article.setAuthors(asList(johnDoe)); - article.setTags("search engines", "tutorial"); - articleRepository.save(article); + article = new Article("Search engines"); + article.setAuthors(asList(johnDoe)); + article.setTags("search engines", "tutorial"); + articleRepository.save(article); - article = new Article("Second Article About Elasticsearch"); - article.setAuthors(asList(johnSmith)); - article.setTags("elasticsearch", "spring data"); - articleRepository.save(article); + article = new Article("Second Article About Elasticsearch"); + article.setAuthors(asList(johnSmith)); + article.setTags("elasticsearch", "spring data"); + articleRepository.save(article); - article = new Article("Elasticsearch Tutorial"); - article.setAuthors(asList(johnDoe)); - article.setTags("elasticsearch"); - articleRepository.save(article); - } + article = new Article("Elasticsearch Tutorial"); + article.setAuthors(asList(johnDoe)); + article.setTags("elasticsearch"); + articleRepository.save(article); + } - @After - public void after() { - articleRepository.deleteAll(); - } + @After + public void after() { + articleRepository.deleteAll(); + } - @Test - public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { - final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title", "Search engines").operator(Operator.AND)).build(); - final SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - assertEquals(1, articles.getTotalHits()); - } + @Test + public void givenFullTitle_whenRunMatchQuery_thenDocIsFound() { + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Search engines").operator(Operator.AND)) + .build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + assertEquals(1, articles.getTotalHits()); + } - @Test - public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { - final NativeSearchQuery searchQuery = - new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")) - .build(); - - final SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - - assertEquals(1, articles.getTotalHits()); - assertEquals("Search engines", articles.getSearchHit(0).getContent().getTitle()); - } + @Test + public void givenOneTermFromTitle_whenRunMatchQuery_thenDocIsFound() { + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "Engines Solutions")) + .build(); - @Test - public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { - final NativeSearchQuery searchQuery = - new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")) - .build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - final SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - - assertEquals(3, articles.getTotalHits()); - } + assertEquals(1, articles.getTotalHits()); + assertEquals("Search engines", articles.getSearchHit(0) + .getContent() + .getTitle()); + } - @Test - public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { - NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")) - .build(); + @Test + public void givenPartTitle_whenRunMatchQuery_thenDocIsFound() { + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "elasticsearch data")) + .build(); - SearchHits
articles = - elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - - assertEquals(1, articles.getTotalHits()); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchQuery("title.verbatim", "Second Article About")) - .build(); - - articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - assertEquals(0, articles.getTotalHits()); - } + assertEquals(3, articles.getTotalHits()); + } - @Test - public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() { - final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), - ScoreMode.None); + @Test + public void givenFullTitle_whenRunMatchQueryOnVerbatimField_thenDocIsFound() { + NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About Elasticsearch")) + .build(); - final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); - final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - assertEquals(2, articles.getTotalHits()); - } + assertEquals(1, articles.getTotalHits()); - @Test - public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception { - final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("title"); + searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title.verbatim", "Second Article About")) + .build(); - final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); - final SearchRequest searchRequest = new SearchRequest("blog").source(builder); + articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + assertEquals(0, articles.getTotalHits()); + } - final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + @Test + public void givenNestedObject_whenQueryByAuthorsName_thenFoundArticlesByThatAuthor() { + final QueryBuilder builder = nestedQuery("authors", boolQuery().must(termQuery("authors.name", "smith")), ScoreMode.None); - final Map results = response.getAggregations().asMap(); - final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder) + .build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - final List keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString).sorted() - .collect(toList()); - assertEquals( - asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys); - } + assertEquals(2, articles.getTotalHits()); + } - @Test - public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception { - final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags").field("tags") - .order(BucketOrder.count(false)); + @Test + public void givenAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTokenCountsSeparately() throws Exception { + final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags") + .field("title"); - final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); - final SearchRequest searchRequest = new SearchRequest().indices("blog").source(builder); + final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); + final SearchRequest searchRequest = new SearchRequest("blog").source(builder); - final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); + final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - final Map results = response.getAggregations().asMap(); - final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); + final Map results = response.getAggregations() + .asMap(); + final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); - final List keys = topTags.getBuckets().stream().map(MultiBucketsAggregation.Bucket::getKeyAsString) - .collect(toList()); - assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); - } + final List keys = topTags.getBuckets() + .stream() + .map(MultiBucketsAggregation.Bucket::getKeyAsString) + .sorted() + .collect(toList()); + assertEquals(asList("about", "article", "data", "elasticsearch", "engines", "search", "second", "spring", "tutorial"), keys); + } - @Test - public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { - final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)).build(); + @Test + public void givenNotAnalyzedQuery_whenMakeAggregationOnTermCount_thenEachTermCountsIndividually() throws Exception { + final TermsAggregationBuilder aggregation = AggregationBuilders.terms("top_tags") + .field("tags") + .order(BucketOrder.count(false)); - final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - - assertEquals(1, articles.getTotalHits()); - } + final SearchSourceBuilder builder = new SearchSourceBuilder().aggregation(aggregation); + final SearchRequest searchRequest = new SearchRequest().indices("blog") + .source(builder); - @Test - public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { - final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery( - matchQuery("title", "spring date elasticserch").operator(Operator.AND).fuzziness(Fuzziness.ONE).prefixLength(3)) - .build(); + final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT); - final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - - assertEquals(1, articles.getTotalHits()); - } + final Map results = response.getAggregations() + .asMap(); + final ParsedStringTerms topTags = (ParsedStringTerms) results.get("top_tags"); - @Test - public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { - final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder() - .withQuery( - multiMatchQuery("tutorial").field("title").field("tags").type(MultiMatchQueryBuilder.Type.BEST_FIELDS)) - .build(); + final List keys = topTags.getBuckets() + .stream() + .map(MultiBucketsAggregation.Bucket::getKeyAsString) + .collect(toList()); + assertEquals(asList("elasticsearch", "spring data", "search engines", "tutorial"), keys); + } - final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + @Test + public void givenNotExactPhrase_whenUseSlop_thenQueryMatches() { + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchPhraseQuery("title", "spring elasticsearch").slop(1)) + .build(); - assertEquals(2, articles.getTotalHits()); - } + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); - @Test - public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() { - final QueryBuilder builder = boolQuery() - .must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None)) - .filter(termQuery("tags", "elasticsearch")); + assertEquals(1, articles.getTotalHits()); + } - final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder).build(); - final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + @Test + public void givenPhraseWithType_whenUseFuzziness_thenQueryMatches() { + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(matchQuery("title", "spring date elasticserch").operator(Operator.AND) + .fuzziness(Fuzziness.ONE) + .prefixLength(3)) + .build(); - assertEquals(2, articles.getTotalHits()); - } + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(1, articles.getTotalHits()); + } + + @Test + public void givenMultimatchQuery_whenDoSearch_thenAllProvidedFieldsMatch() { + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(multiMatchQuery("tutorial").field("title") + .field("tags") + .type(MultiMatchQueryBuilder.Type.BEST_FIELDS)) + .build(); + + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(2, articles.getTotalHits()); + } + + @Test + public void givenBoolQuery_whenQueryByAuthorsName_thenFoundArticlesByThatAuthorAndFilteredTag() { + final QueryBuilder builder = boolQuery().must(nestedQuery("authors", boolQuery().must(termQuery("authors.name", "doe")), ScoreMode.None)) + .filter(termQuery("tags", "elasticsearch")); + + final NativeSearchQuery searchQuery = new NativeSearchQueryBuilder().withQuery(builder) + .build(); + final SearchHits
articles = elasticsearchTemplate.search(searchQuery, Article.class, IndexCoordinates.of("blog")); + + assertEquals(2, articles.getTotalHits()); + } } From 3fc4ed23a46468dd0c9d257c4f4356f0a1347b49 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 19 May 2020 19:44:42 -0400 Subject: [PATCH 21/74] upgrade to 4.0.0.RELEASE --- .../spring-data-elasticsearch/pom.xml | 23 +++---------------- .../com/baeldung/SpringContextManualTest.java | 4 ++-- .../ElasticSearchManualTest.java | 4 ++-- .../elasticsearch/GeoQueriesManualTest.java | 4 ++-- .../data/es/ElasticSearchManualTest.java | 4 ++-- .../data/es/ElasticSearchQueryManualTest.java | 4 ++-- 6 files changed, 13 insertions(+), 30 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index 72860cb136..fc435b1f48 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -52,7 +52,7 @@ org.locationtech.jts jts-core - 1.15.0 + ${jts.version} xerces @@ -67,29 +67,12 @@ ${spring.version} test - - - net.java.dev.jna - jna - ${jna.version} - test - - 4.0.0.RC1 - 4.5.2 + 4.0.0.RELEASE 1.2.47 0.7 - 1.13 + 1.15.0 - - - - repository.spring.milestone - Spring Milestone Repository - https://repo.spring.io/milestone - - - \ No newline at end of file diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java index bff910cf93..6572896eca 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/SpringContextManualTest.java @@ -10,8 +10,8 @@ import com.baeldung.spring.data.es.config.Config; /** * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: docker run -d --name es761 -p - * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es762 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java index e4b3f6d801..2ca5f28f13 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/ElasticSearchManualTest.java @@ -39,8 +39,8 @@ import org.springframework.data.elasticsearch.client.RestClients; /** * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: docker run -d --name es761 -p - * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es762 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2 */ public class ElasticSearchManualTest { private List listOfPersons = new ArrayList<>(); diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java index a36e08d6d2..64b2ea2437 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/elasticsearch/GeoQueriesManualTest.java @@ -41,8 +41,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: docker run -d --name es761 -p - * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es762 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java index 9f713a276f..412cd04e09 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchManualTest.java @@ -33,8 +33,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: docker run -d --name es761 -p - * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es762 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) diff --git a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java index 3cedab00e4..aaf0c80097 100644 --- a/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java +++ b/persistence-modules/spring-data-elasticsearch/src/test/java/com/baeldung/spring/data/es/ElasticSearchQueryManualTest.java @@ -50,8 +50,8 @@ import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; /** * This Manual test requires: Elasticsearch instance running on localhost:9200. * - * The following docker command can be used: docker run -d --name es761 -p - * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.1 + * The following docker command can be used: docker run -d --name es762 -p + * 9200:9200 -e "discovery.type=single-node" elasticsearch:7.6.2 */ @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = Config.class) From 458699cb5bc2e83be818a62bf86e784512832ede Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 19 May 2020 19:48:11 -0400 Subject: [PATCH 22/74] added back elasticsearch dependency --- persistence-modules/spring-data-elasticsearch/pom.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index fc435b1f48..cf77f858d0 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -37,6 +37,12 @@ ${spring-data-elasticsearch.version} + + org.elasticsearch + elasticsearch + ${elasticsearch.version} + + com.alibaba fastjson @@ -71,6 +77,7 @@ 4.0.0.RELEASE + 7.6.2 1.2.47 0.7 1.15.0 From cffeff11abde9ba1ff2d912bde9cf60d33444bf6 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 19 May 2020 19:53:23 -0400 Subject: [PATCH 23/74] removed unecessary dependencies --- persistence-modules/spring-data-elasticsearch/pom.xml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index cf77f858d0..79363d7352 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -13,7 +13,7 @@ - + org.springframework From 177bb9c8977909e585c4dbcc92522709073e95d3 Mon Sep 17 00:00:00 2001 From: Mathieu Fortin Date: Tue, 19 May 2020 19:54:29 -0400 Subject: [PATCH 24/74] removed unecessary dependencies --- .../spring-data-elasticsearch/pom.xml | 12 ------------ 1 file changed, 12 deletions(-) diff --git a/persistence-modules/spring-data-elasticsearch/pom.xml b/persistence-modules/spring-data-elasticsearch/pom.xml index 79363d7352..6a983145ee 100644 --- a/persistence-modules/spring-data-elasticsearch/pom.xml +++ b/persistence-modules/spring-data-elasticsearch/pom.xml @@ -13,18 +13,6 @@ - - org.springframework spring-web From 6eaf9840a0305d524822928753df6415ed2b441f Mon Sep 17 00:00:00 2001 From: mikr Date: Thu, 21 May 2020 12:08:11 +0200 Subject: [PATCH 25/74] BAEL-1524 Change http into https --- persistence-modules/spring-boot-persistence-2/README.md | 2 +- persistence-modules/spring-boot-persistence/README.MD | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/persistence-modules/spring-boot-persistence-2/README.md b/persistence-modules/spring-boot-persistence-2/README.md index e0479ffe10..392218d2bf 100644 --- a/persistence-modules/spring-boot-persistence-2/README.md +++ b/persistence-modules/spring-boot-persistence-2/README.md @@ -3,6 +3,6 @@ - [Using JDBI with Spring Boot](https://www.baeldung.com/spring-boot-jdbi) - [Configuring a Tomcat Connection Pool in Spring Boot](https://www.baeldung.com/spring-boot-tomcat-connection-pool) - [Integrating Spring Boot with HSQLDB](https://www.baeldung.com/spring-boot-hsqldb) -- [List of In-Memory Databases](http://www.baeldung.com/java-in-memory-databases) +- [List of In-Memory Databases](https://www.baeldung.com/java-in-memory-databases) - [Oracle Connection Pooling With Spring](https://www.baeldung.com/spring-oracle-connection-pooling) - More articles: [[<-- prev]](../spring-boot-persistence) diff --git a/persistence-modules/spring-boot-persistence/README.MD b/persistence-modules/spring-boot-persistence/README.MD index d6ef239448..5b9fbf7b79 100644 --- a/persistence-modules/spring-boot-persistence/README.MD +++ b/persistence-modules/spring-boot-persistence/README.MD @@ -1,8 +1,8 @@ ### Relevant Articles: -- [Spring Boot with Multiple SQL Import Files](http://www.baeldung.com/spring-boot-sql-import-files) -- [Configuring Separate Spring DataSource for Tests](http://www.baeldung.com/spring-testing-separate-data-source) -- [Quick Guide on Loading Initial Data with Spring Boot](http://www.baeldung.com/spring-boot-data-sql-and-schema-sql) +- [Spring Boot with Multiple SQL Import Files](https://www.baeldung.com/spring-boot-sql-import-files) +- [Configuring Separate Spring DataSource for Tests](https://www.baeldung.com/spring-testing-separate-data-source) +- [Quick Guide on Loading Initial Data with Spring Boot](https://www.baeldung.com/spring-boot-data-sql-and-schema-sql) - [Configuring a DataSource Programmatically in Spring Boot](https://www.baeldung.com/spring-boot-configure-data-source-programmatic) - [Resolving “Failed to Configure a DataSource” Error](https://www.baeldung.com/spring-boot-failed-to-configure-data-source) - [Hibernate Field Naming with Spring Boot](https://www.baeldung.com/hibernate-field-naming-spring-boot) From dd0a8e2d11ff2d80acb22c4848c86d6f0cae247b Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Thu, 21 May 2020 17:36:25 +0530 Subject: [PATCH 26/74] add tests --- .../interceptor/RateLimitInterceptor.java | 6 +- .../bucket4japp/service/PricingPlan.java | 42 ++++++------- ...4jBootStarterRateLimitIntegrationTest.java | 63 +++++++++++++++++++ .../Bucket4jRateLimitIntegrationTest.java | 62 ++++++++++++++++++ .../bucket4japp}/Bucket4jUsageUnitTest.java | 2 +- .../PricingPlanServiceUnitTest.java | 36 +++++++++++ 6 files changed, 184 insertions(+), 27 deletions(-) create mode 100644 spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jBootStarterRateLimitIntegrationTest.java create mode 100644 spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitIntegrationTest.java rename spring-boot-modules/spring-boot-libraries/src/test/java/com/{baledung/ratelimiting/bucket4j => baeldung/ratelimiting/bucket4japp}/Bucket4jUsageUnitTest.java (98%) create mode 100644 spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/PricingPlanServiceUnitTest.java diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitInterceptor.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitInterceptor.java index d919214983..8a18d6c2b5 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitInterceptor.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/interceptor/RateLimitInterceptor.java @@ -5,6 +5,7 @@ import javax.servlet.http.HttpServletResponse; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; import org.springframework.stereotype.Component; import org.springframework.web.servlet.HandlerInterceptor; @@ -44,10 +45,11 @@ public class RateLimitInterceptor implements HandlerInterceptor { } else { - long waitForRefill = probe.getNanosToWaitForRefill() % 1_000_000_000; + long waitForRefill = probe.getNanosToWaitForRefill() / 1_000_000_000; - response.sendError(HttpStatus.TOO_MANY_REQUESTS.value(), "You have exhausted your API Request Quota"); // 429 + response.setContentType(MediaType.APPLICATION_JSON_VALUE); response.addHeader(HEADER_RETRY_AFTER, String.valueOf(waitForRefill)); + response.sendError(HttpStatus.TOO_MANY_REQUESTS.value(), "You have exhausted your API Request Quota"); // 429 return false; } diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java index e8b5513e8b..2f225a83aa 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java @@ -5,34 +5,28 @@ import java.time.Duration; import io.github.bucket4j.Bandwidth; import io.github.bucket4j.Refill; -enum PricingPlan { +public enum PricingPlan { - FREE { + FREE(20), - @Override - Bandwidth getLimit() { - return Bandwidth.classic(20, Refill.intervally(20, Duration.ofHours(1))); - } - }, + BASIC(40), - BASIC { - - @Override - Bandwidth getLimit() { - return Bandwidth.classic(40, Refill.intervally(40, Duration.ofHours(1))); - } - }, - - PROFESSIONAL { - - @Override - Bandwidth getLimit() { - return Bandwidth.classic(100, Refill.intervally(100, Duration.ofHours(1))); - } - }; - - abstract Bandwidth getLimit(); + PROFESSIONAL(100);; + private int bucketCapacity; + + private PricingPlan(int bucketCapacity) { + this.bucketCapacity = bucketCapacity; + } + + Bandwidth getLimit() { + return Bandwidth.classic(bucketCapacity, Refill.intervally(bucketCapacity, Duration.ofHours(1))); + } + + public int bucketCapacity() { + return bucketCapacity; + } + static PricingPlan resolvePlanFromApiKey(String apiKey) { if (apiKey == null || apiKey.isEmpty()) { return FREE; diff --git a/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jBootStarterRateLimitIntegrationTest.java b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jBootStarterRateLimitIntegrationTest.java new file mode 100644 index 0000000000..d93e61988b --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bootstarterapp/Bucket4jBootStarterRateLimitIntegrationTest.java @@ -0,0 +1,63 @@ +package com.baeldung.ratelimiting.bootstarterapp; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.RequestBuilder; + +import com.baeldung.ratelimiting.bucket4japp.service.PricingPlan; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Bucket4jRateLimitApp.class) +@TestPropertySource(properties = "spring.config.location=classpath:ratelimiting/application-bucket4j-starter.yml") +@AutoConfigureMockMvc +public class Bucket4jBootStarterRateLimitIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void givenTriangleAreaCalculator_whenRequestsWithinRateLimit_thenAccepted() throws Exception { + + RequestBuilder request = post("/api/v1/area/triangle").contentType(MediaType.APPLICATION_JSON_VALUE) + .content("{ \"height\": 8, \"base\": 10 }") + .header("X-api-key", "FX001-UBSZ5YRYQ"); + + for (int i = 1; i <= PricingPlan.FREE.bucketCapacity(); i++) { + mockMvc.perform(request) + .andExpect(status().isOk()) + .andExpect(header().exists("X-Rate-Limit-Remaining")) + .andExpect(jsonPath("$.shape", equalTo("triangle"))) + .andExpect(jsonPath("$.area", equalTo(40d))); + } + } + + @Test + public void givenTriangleAreaCalculator_whenRequestRateLimitTriggered_thenRejected() throws Exception { + + RequestBuilder request = post("/api/v1/area/triangle").contentType(MediaType.APPLICATION_JSON_VALUE) + .content("{ \"height\": 8, \"base\": 10 }") + .header("X-api-key", "FX001-ZBSY6YSLP"); + + for (int i = 1; i <= PricingPlan.FREE.bucketCapacity(); i++) { + mockMvc.perform(request); // exhaust limit + } + + mockMvc.perform(request) + .andExpect(status().isTooManyRequests()) + .andExpect(jsonPath("$.message", equalTo("You have exhausted your API Request Quota"))) + .andExpect(header().exists("X-Rate-Limit-Retry-After-Seconds")); + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitIntegrationTest.java b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitIntegrationTest.java new file mode 100644 index 0000000000..b410b7b2c5 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitIntegrationTest.java @@ -0,0 +1,62 @@ +package com.baeldung.ratelimiting.bucket4japp; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.RequestBuilder; + +import com.baeldung.ratelimiting.bucket4japp.service.PricingPlan; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = Bucket4jRateLimitApp.class) +@AutoConfigureMockMvc +public class Bucket4jRateLimitIntegrationTest { + + @Autowired + private MockMvc mockMvc; + + @Test + public void givenRectangleAreaCalculator_whenRequestsWithinRateLimit_thenAccepted() throws Exception { + + RequestBuilder request = post("/api/v1/area/rectangle").contentType(MediaType.APPLICATION_JSON_VALUE) + .content("{ \"length\": 12, \"width\": 10 }") + .header("X-api-key", "FX001-UBSZ5YRYQ"); + + for (int i = 1; i <= PricingPlan.FREE.bucketCapacity(); i++) { + mockMvc.perform(request) + .andExpect(status().isOk()) + .andExpect(header().exists("X-Rate-Limit-Remaining")) + .andExpect(jsonPath("$.shape", equalTo("rectangle"))) + .andExpect(jsonPath("$.area", equalTo(120d))); + } + } + + @Test + public void givenReactangleAreaCalculator_whenRequestRateLimitTriggered_thenRejected() throws Exception { + + RequestBuilder request = post("/api/v1/area/rectangle").contentType(MediaType.APPLICATION_JSON_VALUE) + .content("{ \"length\": 12, \"width\": 10 }") + .header("X-api-key", "FX001-ZBSY6YSLP"); + + for (int i = 1; i <= PricingPlan.FREE.bucketCapacity(); i++) { + mockMvc.perform(request); // exhaust limit + } + + mockMvc.perform(request) + .andExpect(status().isTooManyRequests()) + .andExpect(status().reason("You have exhausted your API Request Quota")) + .andExpect(header().exists("X-Rate-Limit-Retry-After-Seconds")); + } +} diff --git a/spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageUnitTest.java b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jUsageUnitTest.java similarity index 98% rename from spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageUnitTest.java rename to spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jUsageUnitTest.java index e6b774034e..fbf63ba403 100644 --- a/spring-boot-modules/spring-boot-libraries/src/test/java/com/baledung/ratelimiting/bucket4j/Bucket4jUsageUnitTest.java +++ b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jUsageUnitTest.java @@ -1,4 +1,4 @@ -package com.baledung.ratelimiting.bucket4j; +package com.baeldung.ratelimiting.bucket4japp; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; diff --git a/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/PricingPlanServiceUnitTest.java b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/PricingPlanServiceUnitTest.java new file mode 100644 index 0000000000..325b898779 --- /dev/null +++ b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/PricingPlanServiceUnitTest.java @@ -0,0 +1,36 @@ +package com.baeldung.ratelimiting.bucket4japp; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.junit.jupiter.api.Test; + +import com.baeldung.ratelimiting.bucket4japp.service.PricingPlan; +import com.baeldung.ratelimiting.bucket4japp.service.PricingPlanService; + +import io.github.bucket4j.Bucket; + +public class PricingPlanServiceUnitTest { + + private PricingPlanService service = new PricingPlanService(); + + @Test + public void givenAPIKey_whenFreePlan_thenReturnFreePlanBucket() { + Bucket bucket = service.resolveBucket("FX001-UBSZ5YRYQ"); + + assertEquals(PricingPlan.FREE.bucketCapacity(), bucket.getAvailableTokens()); + } + + @Test + public void givenAPIKey_whenBasiclan_thenReturnBasicPlanBucket() { + Bucket bucket = service.resolveBucket("BX001-MBSZ5YRYP"); + + assertEquals(PricingPlan.BASIC.bucketCapacity(), bucket.getAvailableTokens()); + } + + @Test + public void givenAPIKey_whenProfessionalPlan_thenReturnProfessionalPlanBucket() { + Bucket bucket = service.resolveBucket("PX001-NBSZ5YRYY"); + + assertEquals(PricingPlan.PROFESSIONAL.bucketCapacity(), bucket.getAvailableTokens()); + } +} From 92c446ce4cc444f865ef4bd53b8876344647ba68 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Thu, 21 May 2020 21:25:37 +0430 Subject: [PATCH 27/74] Adding Super Type Token --- .../com/baeldung/supertype/TypeReference.java | 18 ++++++++++++++ .../supertype/TypeReferenceUnitTest.java | 24 +++++++++++++++++++ 2 files changed, 42 insertions(+) create mode 100644 core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/supertype/TypeReference.java create mode 100644 core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/supertype/TypeReferenceUnitTest.java diff --git a/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/supertype/TypeReference.java b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/supertype/TypeReference.java new file mode 100644 index 0000000000..2021f42239 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/main/java/com/baeldung/supertype/TypeReference.java @@ -0,0 +1,18 @@ +package com.baeldung.supertype; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; + +public abstract class TypeReference { + + private final Type type; + + public TypeReference() { + Type superclass = getClass().getGenericSuperclass(); + type = ((ParameterizedType) superclass).getActualTypeArguments()[0]; + } + + public Type getType() { + return type; + } +} diff --git a/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/supertype/TypeReferenceUnitTest.java b/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/supertype/TypeReferenceUnitTest.java new file mode 100644 index 0000000000..24e3b698e2 --- /dev/null +++ b/core-java-modules/core-java-lang-oop-generics/src/test/java/com/baeldung/supertype/TypeReferenceUnitTest.java @@ -0,0 +1,24 @@ +package com.baeldung.supertype; + +import org.junit.Test; + +import java.lang.reflect.ParameterizedType; +import java.lang.reflect.Type; +import java.util.Map; + +import static org.junit.Assert.assertEquals; + +public class TypeReferenceUnitTest { + + @Test + public void givenGenericToken_whenUsingSuperTypeToken_thenPreservesTheTypeInfo() { + TypeReference> token = new TypeReference>() {}; + Type type = token.getType(); + + assertEquals("java.util.Map", type.getTypeName()); + + Type[] typeArguments = ((ParameterizedType) type).getActualTypeArguments(); + assertEquals("java.lang.String", typeArguments[0].getTypeName()); + assertEquals("java.lang.Integer", typeArguments[1].getTypeName()); + } +} From 8ecf039b6b539ae626c93e8e4aa7b30b13b55e98 Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Thu, 21 May 2020 22:53:20 +0530 Subject: [PATCH 28/74] remove unused import --- .../bucket4japp/Bucket4jRateLimitIntegrationTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitIntegrationTest.java b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitIntegrationTest.java index b410b7b2c5..20f57a7021 100644 --- a/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitIntegrationTest.java +++ b/spring-boot-modules/spring-boot-libraries/src/test/java/com/baeldung/ratelimiting/bucket4japp/Bucket4jRateLimitIntegrationTest.java @@ -11,7 +11,6 @@ import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; import org.springframework.http.MediaType; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.web.servlet.MockMvc; From 020fcd9229cf29513632bf816b1faea171c35db3 Mon Sep 17 00:00:00 2001 From: musibs Date: Sat, 23 May 2020 15:23:26 +0530 Subject: [PATCH 29/74] BAEL 4064 Out of Memory Error --- .../error/oom/TestExecutorService.java | 39 +++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 core-java-modules/core-java-lang/src/test/java/com/baeldung/error/oom/TestExecutorService.java diff --git a/core-java-modules/core-java-lang/src/test/java/com/baeldung/error/oom/TestExecutorService.java b/core-java-modules/core-java-lang/src/test/java/com/baeldung/error/oom/TestExecutorService.java new file mode 100644 index 0000000000..d51faa055d --- /dev/null +++ b/core-java-modules/core-java-lang/src/test/java/com/baeldung/error/oom/TestExecutorService.java @@ -0,0 +1,39 @@ +package com.baeldung.error.oom; + +import static org.hamcrest.CoreMatchers.equalTo; +import static org.hamcrest.CoreMatchers.is; +import static org.junit.Assert.assertThat; + +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.ThreadPoolExecutor; +import java.util.concurrent.TimeUnit; +import java.util.stream.IntStream; + +import org.junit.jupiter.api.Test; + +public class TestExecutorService { + + @Test + public void givenAnExecutorService_WhenMoreTasksSubmitted_ThenAdditionalTasksWait() { + + //Given + int noOfThreads = 5; + ExecutorService executorService = Executors.newFixedThreadPool(noOfThreads); + + Runnable runnableTask = () -> { + try { + TimeUnit.HOURS.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }; + + //When + IntStream.rangeClosed(1, 10) + .forEach(i -> executorService.submit(runnableTask)); + + //Then + assertThat(((ThreadPoolExecutor )executorService).getQueue().size(), is(equalTo(5))); + } +} From 05a4d4e188c1242b72675332df77bcc0769841a3 Mon Sep 17 00:00:00 2001 From: Vikas Ramsingh Rajput Date: Sun, 24 May 2020 10:33:17 +0300 Subject: [PATCH 30/74] BAEL-3067: Article - Convert BufferedReader to JSONObject - Completed --- .../core-java-io-conversions-2/pom.xml | 5 ++ ...avaBufferedReaderToJSONObjectUnitTest.java | 48 +++++++++++++++++++ 2 files changed, 53 insertions(+) create mode 100644 core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/bufferedreadertojsonobject/JavaBufferedReaderToJSONObjectUnitTest.java diff --git a/core-java-modules/core-java-io-conversions-2/pom.xml b/core-java-modules/core-java-io-conversions-2/pom.xml index e95d1f4b67..7a83d4b502 100644 --- a/core-java-modules/core-java-io-conversions-2/pom.xml +++ b/core-java-modules/core-java-io-conversions-2/pom.xml @@ -22,6 +22,11 @@ commons-lang3 ${commons-lang3.version} + + org.json + json + 20190722 + diff --git a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/bufferedreadertojsonobject/JavaBufferedReaderToJSONObjectUnitTest.java b/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/bufferedreadertojsonobject/JavaBufferedReaderToJSONObjectUnitTest.java new file mode 100644 index 0000000000..d42c3975db --- /dev/null +++ b/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/bufferedreadertojsonobject/JavaBufferedReaderToJSONObjectUnitTest.java @@ -0,0 +1,48 @@ +package com.baeldung.bufferedreadertojsonobject; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; + +import org.json.JSONObject; +import org.json.JSONTokener; +import org.junit.Test; + +public class JavaBufferedReaderToJSONObjectUnitTest { + + @Test + public void givenUsingNewVersion_whenConvertBufferedReaderToJSONObject_thenCorrect() { + byte[] b = "{ \"name\" : \"John\", \"age\" : 18 }".getBytes(StandardCharsets.UTF_8); + InputStream is = new ByteArrayInputStream(b); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); + JSONTokener tokener = new JSONTokener(bufferedReader); + JSONObject json = new JSONObject(tokener); + + assertNotNull(json); + assertEquals("John", json.get("name")); + assertEquals(18, json.get("age")); + } + + @Test + public void givenUsingOldVersion_whenConvertBufferedReaderToJSONObject_thenCorrect() throws IOException { + byte[] b = "{ \"name\" : \"John\", \"age\" : 18 }".getBytes(StandardCharsets.UTF_8); + InputStream is = new ByteArrayInputStream(b); + BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); + StringBuilder sb = new StringBuilder(); + String line; + while ((line = bufferedReader.readLine()) != null) { + sb.append(line); + } + JSONObject json = new JSONObject(sb.toString()); + + assertNotNull(json); + assertEquals("John", json.get("name")); + assertEquals(18, json.get("age")); + } +} From 715147e7dc2ef0f13cf8b9845b2de049bc66073f Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Sun, 24 May 2020 11:44:26 +0200 Subject: [PATCH 31/74] Refactored packages, added test cases, added structured class case --- .../main/java/com/baeldung/Application.java | 54 ------------- .../PartialUpdateApplication.java | 20 +++++ .../partialupdate/model/ContactPhone.java | 22 ++++++ .../{ => partialupdate}/model/Customer.java | 0 .../model/CustomerDto.java | 0 .../model/CustomerStructured.java | 26 ++++++ .../repository/ContactPhoneRepository.java | 12 +++ .../repository/CustomerRepository.java | 4 +- .../CustomerStructuredRepository.java | 11 +++ .../service/CustomerService.java | 79 +++++++++++++++++++ .../util/CustomerMapper.java | 6 +- .../com/baeldung/service/CustomerService.java | 47 ----------- .../partialupdate/PartialUpdateUnitTest.java | 56 +++++++++++++ 13 files changed, 231 insertions(+), 106 deletions(-) delete mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java rename persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/{ => partialupdate}/model/Customer.java (100%) rename persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/{ => partialupdate}/model/CustomerDto.java (100%) create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java rename persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/{ => partialupdate}/repository/CustomerRepository.java (76%) create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerStructuredRepository.java create mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java rename persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/{ => partialupdate}/util/CustomerMapper.java (73%) delete mode 100644 persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java create mode 100644 persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java deleted file mode 100644 index 34e86fe135..0000000000 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/Application.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.baeldung; - -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.CommandLineRunner; -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cache.CacheManager; -import org.springframework.cache.annotation.EnableCaching; -import org.springframework.context.ApplicationContext; -import org.springframework.context.annotation.Bean; - -import com.baeldung.model.Customer; -import com.baeldung.model.CustomerDto; -import com.baeldung.service.CustomerService; - -@SpringBootApplication @EnableCaching -public class Application { - - @Autowired CustomerService service; - @Autowired CacheManager cacheManager; - private static final org.slf4j.Logger logger = LoggerFactory.getLogger(Application.class); - - public static void main(String[] args) { - SpringApplication.run(Application.class, args); - } - - @Bean public CommandLineRunner commandLineRunner(ApplicationContext ctx) throws Exception { - logger.info("-- BASIC LOAD AND SAVE --"); - basicLoadAndSave(); - logger.info("-- BASIC LOAD AND SAVE + MAPPER --"); - basicLoadAndSaveWithMapper(); - return null; - } - - private void basicLoadAndSave() { - Customer myCustomer = service.addCustomer("John"); - logger.info("Insert -- " + myCustomer.toString()); - myCustomer = service.updateCustomer(myCustomer.id, "+00"); - logger.info("Update -- " + myCustomer.toString()); - } - - private void basicLoadAndSaveWithMapper() { - CustomerDto dto = new CustomerDto(null); - dto.name = "Johnny"; - Customer entity = service.addCustomer(dto); - logger.info("Insert -- " + entity.toString()); - CustomerDto dto2 = new CustomerDto(entity.id); - dto2.phone = "+44"; - entity = service.updateCustomer(dto2); - logger.info("Update -- " + entity.toString()); - } - -} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java new file mode 100644 index 0000000000..30f5dfa03d --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java @@ -0,0 +1,20 @@ +package com.baeldung.partialupdate; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; + +@SpringBootApplication +@EnableCaching +public class PartialUpdateApplication { + + @Autowired + CacheManager cacheManager; + + public static void main(String[] args) { + SpringApplication.run(PartialUpdateApplication.class, args); + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java new file mode 100644 index 0000000000..9d611fa755 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java @@ -0,0 +1,22 @@ +package com.baeldung.partialupdate.model; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; + +@Entity +public class ContactPhone { + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public long id; + @Column(nullable=false) + public long customerId; + public String phone; + + @Override + public String toString() { + return phone; + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java similarity index 100% rename from persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/Customer.java rename to persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java similarity index 100% rename from persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/model/CustomerDto.java rename to persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java new file mode 100644 index 0000000000..67b534add2 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java @@ -0,0 +1,26 @@ +package com.baeldung.partialupdate.model; + +import java.util.List; + +import javax.persistence.Entity; +import javax.persistence.FetchType; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.OneToMany; + +@Entity +public class CustomerStructured { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + public long id; + public String name; + @OneToMany(fetch = FetchType.EAGER, targetEntity=ContactPhone.class, mappedBy="customerId") + public List contactPhones; + + @Override public String toString() { + return String.format("Customer %s, Phone: %s", + this.name, this.contactPhones.stream().map(e -> e.toString()).reduce("", String::concat)); + } +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java new file mode 100644 index 0000000000..2e0b687227 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java @@ -0,0 +1,12 @@ +package com.baeldung.partialupdate.repository; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.partialupdate.model.ContactPhone; + +@Repository +public interface ContactPhoneRepository extends CrudRepository { + ContactPhone findById(long id); + ContactPhone findByCustomerId(long id); +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java similarity index 76% rename from persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java rename to persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java index dcde6c3b46..bd33c03b6b 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/repository/CustomerRepository.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java @@ -1,10 +1,10 @@ -package com.baeldung.repository; +package com.baeldung.partialupdate.repository; import org.springframework.cache.annotation.Cacheable; import org.springframework.data.repository.CrudRepository; import org.springframework.stereotype.Repository; -import com.baeldung.model.Customer; +import com.baeldung.partialupdate.model.Customer; @Repository public interface CustomerRepository extends CrudRepository { diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerStructuredRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerStructuredRepository.java new file mode 100644 index 0000000000..0f9fd1e92e --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerStructuredRepository.java @@ -0,0 +1,11 @@ +package com.baeldung.partialupdate.repository; + +import org.springframework.data.repository.CrudRepository; +import org.springframework.stereotype.Repository; + +import com.baeldung.partialupdate.model.CustomerStructured; + +@Repository +public interface CustomerStructuredRepository extends CrudRepository { + CustomerStructured findById(long id); +} \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java new file mode 100644 index 0000000000..f3a1c65ef1 --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java @@ -0,0 +1,79 @@ +package com.baeldung.partialupdate.service; + +import javax.transaction.Transactional; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.partialupdate.model.ContactPhone; +import com.baeldung.partialupdate.model.Customer; +import com.baeldung.partialupdate.model.CustomerDto; +import com.baeldung.partialupdate.model.CustomerStructured; +import com.baeldung.partialupdate.repository.ContactPhoneRepository; +import com.baeldung.partialupdate.repository.CustomerRepository; +import com.baeldung.partialupdate.repository.CustomerStructuredRepository; +import com.baeldung.partialupdate.util.CustomerMapper; + +@Service +@Transactional +public class CustomerService { + + @Autowired + CustomerRepository repo; + @Autowired + CustomerStructuredRepository repo2; + @Autowired + ContactPhoneRepository repo3; + @Autowired + CustomerMapper mapper; + + public Customer addCustomer(String name) { + Customer myCustomer = new Customer(); + myCustomer.name = name; + repo.save(myCustomer); + return myCustomer; + } + + public Customer updateCustomer(long id, String phone) { + Customer myCustomer = repo.findById(id); + myCustomer.phone = phone; + repo.save(myCustomer); + return myCustomer; + } + + public Customer addCustomer(CustomerDto dto) { + Customer myCustomer = new Customer(); + mapper.updateCustomerFromDto(dto, myCustomer); + repo.save(myCustomer); + return myCustomer; + } + + public Customer updateCustomer(CustomerDto dto) { + Customer myCustomer = repo.findById(dto.getId()); + mapper.updateCustomerFromDto(dto, myCustomer); + repo.save(myCustomer); + return myCustomer; + } + + public CustomerStructured addCustomerStructured(String name) { + CustomerStructured myCustomer = new CustomerStructured(); + myCustomer.name = name; + repo2.save(myCustomer); + return myCustomer; + } + + public void addCustomerPhone(long customerId, String phone) { + ContactPhone myPhone = new ContactPhone(); + myPhone.phone = phone; + myPhone.customerId = customerId; + repo3.save(myPhone); + } + + public CustomerStructured updateCustomerStructured(long id, String name) { + CustomerStructured myCustomer = repo2.findById(id); + myCustomer.name = name; + repo2.save(myCustomer); + return myCustomer; + } + +} diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/util/CustomerMapper.java similarity index 73% rename from persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java rename to persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/util/CustomerMapper.java index d4f264e33a..8a666e3e6c 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/util/CustomerMapper.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/util/CustomerMapper.java @@ -1,12 +1,12 @@ -package com.baeldung.util; +package com.baeldung.partialupdate.util; import org.mapstruct.BeanMapping; import org.mapstruct.Mapper; import org.mapstruct.MappingTarget; import org.mapstruct.NullValuePropertyMappingStrategy; -import com.baeldung.model.Customer; -import com.baeldung.model.CustomerDto; +import com.baeldung.partialupdate.model.Customer; +import com.baeldung.partialupdate.model.CustomerDto; @Mapper(componentModel = "spring") public interface CustomerMapper { diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java deleted file mode 100644 index 9ebbb0c814..0000000000 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/service/CustomerService.java +++ /dev/null @@ -1,47 +0,0 @@ -package com.baeldung.service; - -import javax.transaction.Transactional; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.baeldung.model.Customer; -import com.baeldung.model.CustomerDto; -import com.baeldung.repository.CustomerRepository; -import com.baeldung.util.CustomerMapper; - -@Service @Transactional -public class CustomerService { - - @Autowired CustomerRepository repo; - @Autowired CustomerMapper mapper; - - public Customer addCustomer(String name) { - Customer myCustomer = new Customer(); - myCustomer.name = name; - repo.save(myCustomer); - return myCustomer; - } - - public Customer updateCustomer(long id, String phone) { - Customer myCustomer = repo.findById(id); - myCustomer.phone = phone; - repo.save(myCustomer); - return myCustomer; - } - - public Customer addCustomer(CustomerDto dto) { - Customer myCustomer = new Customer(); - mapper.updateCustomerFromDto(dto, myCustomer); - repo.save(myCustomer); - return myCustomer; - } - - public Customer updateCustomer(CustomerDto dto) { - Customer myCustomer = repo.findById(dto.getId()); - mapper.updateCustomerFromDto(dto, myCustomer); - repo.save(myCustomer); - return myCustomer; - } - -} diff --git a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java new file mode 100644 index 0000000000..dc9c8821ac --- /dev/null +++ b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java @@ -0,0 +1,56 @@ +package com.baeldung.partialupdate; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +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.test.context.junit4.SpringRunner; + +import com.baeldung.partialupdate.model.Customer; +import com.baeldung.partialupdate.model.CustomerDto; +import com.baeldung.partialupdate.model.CustomerStructured; +import com.baeldung.partialupdate.service.CustomerService; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = PartialUpdateApplication.class) +public class PartialUpdateUnitTest { + + @Autowired + CustomerService service; + + @Test + public void loadAndSave_whenUpdate_thenSuccess() { + Customer myCustomer = service.addCustomer("John"); + myCustomer = service.updateCustomer(myCustomer.id, "+00"); + + assertEquals("+00", myCustomer.phone); + } + + @Test + public void loadAndSaveWithMapper_whenUpdate_thenSuccess() { + CustomerDto dto = new CustomerDto(new Customer()); + dto.name = "Johnny"; + Customer entity = service.addCustomer(dto); + + CustomerDto dto2 = new CustomerDto(entity.id); + dto2.phone = "+44"; + entity = service.updateCustomer(dto2); + + assertEquals("Johnny", entity.name); + } + + @Test + public void loadAndSaveStructuredEntity_whenUpdate_thenSuccess() { + CustomerStructured myCustomer = service.addCustomerStructured("John"); + assertEquals(null, myCustomer.contactPhones); + + service.addCustomerPhone(myCustomer.id, "+44"); + myCustomer = service.updateCustomerStructured(myCustomer.id, "Mr. John"); + + assertNotEquals(null, myCustomer.contactPhones); + assertEquals(1, myCustomer.contactPhones.size()); + } +} From ab1114fb7de6d78811c84132e461c0855255cf74 Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Sun, 24 May 2020 11:45:17 +0200 Subject: [PATCH 32/74] Refactored packages --- .../java/com/baeldung/partialupdate/model/Customer.java | 7 +++---- .../java/com/baeldung/partialupdate/model/CustomerDto.java | 2 +- 2 files changed, 4 insertions(+), 5 deletions(-) diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java index 28c5d5c76c..b19d0b7952 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/Customer.java @@ -1,4 +1,4 @@ -package com.baeldung.model; +package com.baeldung.partialupdate.model; import javax.persistence.Entity; import javax.persistence.GeneratedValue; @@ -8,15 +8,14 @@ import javax.persistence.Id; @Entity public class Customer { - @Id @GeneratedValue(strategy = GenerationType.IDENTITY) + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) public long id; public String name; public String phone; //... public String phone99; - public Customer() {} - @Override public String toString() { return String.format("Customer %s, Phone: %s", this.name, this.phone); diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java index 74b909aad8..4087838f9a 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java @@ -1,4 +1,4 @@ -package com.baeldung.model; +package com.baeldung.partialupdate.model; public class CustomerDto { private long id; From d76966dab73c71c540d56262c9d0b58a60748ab5 Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Sun, 24 May 2020 16:53:24 +0530 Subject: [PATCH 33/74] review comments - indentation in pom --- .../spring-boot-libraries/pom.xml | 244 +++++++++--------- .../bucket4japp/service/PricingPlan.java | 2 +- 2 files changed, 123 insertions(+), 123 deletions(-) diff --git a/spring-boot-modules/spring-boot-libraries/pom.xml b/spring-boot-modules/spring-boot-libraries/pom.xml index 36b9ec17c9..da4e979c17 100644 --- a/spring-boot-modules/spring-boot-libraries/pom.xml +++ b/spring-boot-modules/spring-boot-libraries/pom.xml @@ -88,34 +88,34 @@ ${zxing.version} - - com.github.vladimir-bukhtoyarov - bucket4j-core - ${bucket4j.version} - - - com.giffing.bucket4j.spring.boot.starter - bucket4j-spring-boot-starter - ${bucket4j-spring-boot-starter.version} - - - org.springframework.boot - spring-boot-starter-cache - - - javax.cache - cache-api - - - com.github.ben-manes.caffeine - caffeine - ${caffeine.version} - - - com.github.ben-manes.caffeine - jcache - ${caffeine.version} - + + com.github.vladimir-bukhtoyarov + bucket4j-core + ${bucket4j.version} + + + com.giffing.bucket4j.spring.boot.starter + bucket4j-spring-boot-starter + ${bucket4j-spring-boot-starter.version} + + + org.springframework.boot + spring-boot-starter-cache + + + javax.cache + cache-api + + + com.github.ben-manes.caffeine + caffeine + ${caffeine.version} + + + com.github.ben-manes.caffeine + jcache + ${caffeine.version} + @@ -125,112 +125,112 @@ - - spring-boot-libraries - - - src/main/resources - true - - + + spring-boot-libraries + + + src/main/resources + true + + - + - - org.apache.maven.plugins - maven-war-plugin - + + org.apache.maven.plugins + maven-war-plugin + - - pl.project13.maven - git-commit-id-plugin - ${git-commit-id-plugin.version} - - - get-the-git-infos - - revision - - initialize - - - validate-the-git-infos - - validateRevision - - package - - - - true - ${project.build.outputDirectory}/git.properties - - + + pl.project13.maven + git-commit-id-plugin + ${git-commit-id-plugin.version} + + + get-the-git-infos + + revision + + initialize + + + validate-the-git-infos + + validateRevision + + package + + + + true + ${project.build.outputDirectory}/git.properties + + - - - autoconfiguration - - - - org.apache.maven.plugins - maven-surefire-plugin - - - integration-test - - test - - - - **/*LiveTest.java - **/*IntegrationTest.java - **/*IntTest.java - - - **/AutoconfigurationTest.java - - - - - - - json - - - - - - - + + + autoconfiguration + + + + org.apache.maven.plugins + maven-surefire-plugin + + + integration-test + + test + + + + **/*LiveTest.java + **/*IntegrationTest.java + **/*IntTest.java + + + **/AutoconfigurationTest.java + + + + + + + json + + + + + + + - - + + com.baeldung.intro.App 8.5.11 2.4.1.Final 1.9.0 2.0.0 - 5.0.2 - 5.0.2 - 5.2.4 - 18.0 - 2.2.4 - 2.3.2 - 0.23.0 - 1.4.200 - 2.1.0 - 1.5-beta1 - 2.1 - 2.6.0 - 3.3.0 - 4.10.0 - 0.2.0 - 2.8.2 - + 5.0.2 + 5.0.2 + 5.2.4 + 18.0 + 2.2.4 + 2.3.2 + 0.23.0 + 1.4.200 + 2.1.0 + 1.5-beta1 + 2.1 + 2.6.0 + 3.3.0 + 4.10.0 + 0.2.0 + 2.8.2 + - + \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java index 2f225a83aa..27c30ba3a0 100644 --- a/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java +++ b/spring-boot-modules/spring-boot-libraries/src/main/java/com/baeldung/ratelimiting/bucket4japp/service/PricingPlan.java @@ -11,7 +11,7 @@ public enum PricingPlan { BASIC(40), - PROFESSIONAL(100);; + PROFESSIONAL(100); private int bucketCapacity; From b7c9e649825fee3377c07cf9cdbbec77e591e84c Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Sun, 24 May 2020 16:55:06 +0530 Subject: [PATCH 34/74] review comments - indentation in pom --- spring-boot-modules/spring-boot-libraries/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-libraries/pom.xml b/spring-boot-modules/spring-boot-libraries/pom.xml index da4e979c17..4e7fd205d5 100644 --- a/spring-boot-modules/spring-boot-libraries/pom.xml +++ b/spring-boot-modules/spring-boot-libraries/pom.xml @@ -233,4 +233,4 @@ 2.8.2 - \ No newline at end of file + From e839798f0da0d2e598f64c5d20bba6973428ba41 Mon Sep 17 00:00:00 2001 From: priyank-sriv Date: Sun, 24 May 2020 16:56:57 +0530 Subject: [PATCH 35/74] review comments - indentation in pom --- spring-boot-modules/spring-boot-libraries/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-libraries/pom.xml b/spring-boot-modules/spring-boot-libraries/pom.xml index 4e7fd205d5..189eb4cf1a 100644 --- a/spring-boot-modules/spring-boot-libraries/pom.xml +++ b/spring-boot-modules/spring-boot-libraries/pom.xml @@ -87,7 +87,7 @@ javase ${zxing.version} - + com.github.vladimir-bukhtoyarov bucket4j-core From ce8d289cd9abdaadcf38225e25e747c62b309c51 Mon Sep 17 00:00:00 2001 From: musibs Date: Mon, 25 May 2020 09:45:10 +0530 Subject: [PATCH 36/74] Moved the testcase to core-java-lang-2 module --- .../src/test/java/com/baeldung/error/oom/TestExecutorService.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename core-java-modules/{core-java-lang => core-java-lang-2}/src/test/java/com/baeldung/error/oom/TestExecutorService.java (100%) diff --git a/core-java-modules/core-java-lang/src/test/java/com/baeldung/error/oom/TestExecutorService.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/error/oom/TestExecutorService.java similarity index 100% rename from core-java-modules/core-java-lang/src/test/java/com/baeldung/error/oom/TestExecutorService.java rename to core-java-modules/core-java-lang-2/src/test/java/com/baeldung/error/oom/TestExecutorService.java From 17d2a2f052e13b053d3cee3ac5897e9f237e8b59 Mon Sep 17 00:00:00 2001 From: Juan Moreno Date: Mon, 25 May 2020 01:43:34 -0300 Subject: [PATCH 37/74] FEAT Added sample code for BAEL-3494 --- .../core-java-9-new-features/pom.xml | 69 ++++++++++++++++++- .../com/baeldung/multireleaseapp/App.java | 14 ++++ .../multireleaseapp/DefaultVersion.java | 9 +++ .../com/baeldung/multireleaseapp/Version.java | 5 ++ .../multireleaseapp/DefaultVersion.java | 9 +++ 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 core-java-modules/core-java-9-new-features/src/main/java8/com/baeldung/multireleaseapp/App.java create mode 100644 core-java-modules/core-java-9-new-features/src/main/java8/com/baeldung/multireleaseapp/DefaultVersion.java create mode 100644 core-java-modules/core-java-9-new-features/src/main/java8/com/baeldung/multireleaseapp/Version.java create mode 100644 core-java-modules/core-java-9-new-features/src/main/java9/com/baeldung/multireleaseapp/DefaultVersion.java diff --git a/core-java-modules/core-java-9-new-features/pom.xml b/core-java-modules/core-java-9-new-features/pom.xml index b0fb6ab7f9..029d0ca2de 100644 --- a/core-java-modules/core-java-9-new-features/pom.xml +++ b/core-java-modules/core-java-9-new-features/pom.xml @@ -29,7 +29,73 @@ test - + + + mrjar-generation + + + + org.apache.maven.plugins + maven-compiler-plugin + + + compile-java-8 + + compile + + + 1.8 + 1.8 + + ${project.basedir}/src/main/java8 + + + + + compile-java-9 + compile + + compile + + + 9 + + ${project.basedir}/src/main/java9 + + ${project.build.outputDirectory}/META-INF/versions/9 + + + + default-testCompile + test-compile + + testCompile + + + true + + + + + + org.apache.maven.plugins + maven-jar-plugin + ${maven-jar-plugin.version} + + + + true + + + com.baeldung.multireleaseapp.App + + + + + + + + core-java-9-new-features @@ -58,6 +124,7 @@ 1.2.0 1.9 1.9 + 3.2.0 diff --git a/core-java-modules/core-java-9-new-features/src/main/java8/com/baeldung/multireleaseapp/App.java b/core-java-modules/core-java-9-new-features/src/main/java8/com/baeldung/multireleaseapp/App.java new file mode 100644 index 0000000000..cc00223e05 --- /dev/null +++ b/core-java-modules/core-java-9-new-features/src/main/java8/com/baeldung/multireleaseapp/App.java @@ -0,0 +1,14 @@ +package com.baeldung.multireleaseapp; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class App { + + private static final Logger logger = LoggerFactory.getLogger(App.class); + + public static void main(String[] args) { + logger.info(String.format("Running on %s", new DefaultVersion().version())); + } + +} diff --git a/core-java-modules/core-java-9-new-features/src/main/java8/com/baeldung/multireleaseapp/DefaultVersion.java b/core-java-modules/core-java-9-new-features/src/main/java8/com/baeldung/multireleaseapp/DefaultVersion.java new file mode 100644 index 0000000000..b24be606de --- /dev/null +++ b/core-java-modules/core-java-9-new-features/src/main/java8/com/baeldung/multireleaseapp/DefaultVersion.java @@ -0,0 +1,9 @@ +package com.baeldung.multireleaseapp; + +public class DefaultVersion implements Version { + + @Override + public String version() { + return System.getProperty("java.version"); + } +} diff --git a/core-java-modules/core-java-9-new-features/src/main/java8/com/baeldung/multireleaseapp/Version.java b/core-java-modules/core-java-9-new-features/src/main/java8/com/baeldung/multireleaseapp/Version.java new file mode 100644 index 0000000000..ef95f08205 --- /dev/null +++ b/core-java-modules/core-java-9-new-features/src/main/java8/com/baeldung/multireleaseapp/Version.java @@ -0,0 +1,5 @@ +package com.baeldung.multireleaseapp; + +interface Version { + public String version(); +} \ No newline at end of file diff --git a/core-java-modules/core-java-9-new-features/src/main/java9/com/baeldung/multireleaseapp/DefaultVersion.java b/core-java-modules/core-java-9-new-features/src/main/java9/com/baeldung/multireleaseapp/DefaultVersion.java new file mode 100644 index 0000000000..0842f578dd --- /dev/null +++ b/core-java-modules/core-java-9-new-features/src/main/java9/com/baeldung/multireleaseapp/DefaultVersion.java @@ -0,0 +1,9 @@ +package com.baeldung.multireleaseapp; + +public class DefaultVersion implements Version { + + @Override + public String version() { + return Runtime.version().toString(); + } +} From 102a487aaa3288280e595dd7c623c0eea66207d3 Mon Sep 17 00:00:00 2001 From: Juan Moreno Date: Mon, 25 May 2020 02:09:21 -0300 Subject: [PATCH 38/74] FIX PMD bugs, dependency missing, compilation incubator features, fix awaitility asserts --- .../core-java-9-new-features/pom.xml | 31 +++++++++++++++++ ...st.java => HttpClientIntegrationTest.java} | 4 +-- ...t.java => HttpRequestIntegrationTest.java} | 2 +- ....java => HttpResponseIntegrationTest.java} | 2 +- ...Test.java => ReactiveStreamsUnitTest.java} | 12 ++++--- ...Test.java => VariableHandlesUnitTest.java} | 34 +++++++++---------- 6 files changed, 59 insertions(+), 26 deletions(-) rename core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/{HttpClientTest.java => HttpClientIntegrationTest.java} (98%) rename core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/{HttpRequestTest.java => HttpRequestIntegrationTest.java} (99%) rename core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/{HttpResponseTest.java => HttpResponseIntegrationTest.java} (97%) rename core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/streams.reactive/{ReactiveStreamsTest.java => ReactiveStreamsUnitTest.java} (86%) rename core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/varhandles/{VariableHandlesTest.java => VariableHandlesUnitTest.java} (75%) diff --git a/core-java-modules/core-java-9-new-features/pom.xml b/core-java-modules/core-java-9-new-features/pom.xml index 029d0ca2de..70fae73bd5 100644 --- a/core-java-modules/core-java-9-new-features/pom.xml +++ b/core-java-modules/core-java-9-new-features/pom.xml @@ -28,8 +28,38 @@ ${junit.platform.version} test + + org.awaitility + awaitility + ${awaitility.version} + test + + + incubator-features + + core-java-9-new-features + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + --add-modules=jdk.incubator.httpclient + + + + maven-surefire-plugin + + --add-modules=jdk.incubator.httpclient + + + + + mrjar-generation @@ -122,6 +152,7 @@ 3.10.0 1.2.0 + 4.0.2 1.9 1.9 3.2.0 diff --git a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientIntegrationTest.java similarity index 98% rename from core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java rename to core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientIntegrationTest.java index 5cf3b9098f..fc59ae8d8d 100644 --- a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientTest.java +++ b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpClientIntegrationTest.java @@ -24,7 +24,7 @@ import static org.junit.Assert.assertThat; /** * Created by adam. */ -public class HttpClientTest { +public class HttpClientIntegrationTest { @Test public void shouldReturnSampleDataContentWhenConnectViaSystemProxy() throws IOException, InterruptedException, URISyntaxException { @@ -55,7 +55,7 @@ public class HttpClientTest { .send(request, HttpResponse.BodyHandler.asString()); assertThat(response.statusCode(), equalTo(HttpURLConnection.HTTP_MOVED_PERM)); - assertThat(response.body(), containsString("https://stackoverflow.com/")); + assertThat(response.body(), containsString("")); } @Test diff --git a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestTest.java b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestIntegrationTest.java similarity index 99% rename from core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestTest.java rename to core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestIntegrationTest.java index 7c0e9a90e0..17af7bd8ba 100644 --- a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestTest.java +++ b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpRequestIntegrationTest.java @@ -22,7 +22,7 @@ import static org.junit.Assert.assertThat; /** * Created by adam. */ -public class HttpRequestTest { +public class HttpRequestIntegrationTest { @Test public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException { diff --git a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseTest.java b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseIntegrationTest.java similarity index 97% rename from core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseTest.java rename to core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseIntegrationTest.java index 80295ff34c..5c6f9c8a52 100644 --- a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseTest.java +++ b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/httpclient/HttpResponseIntegrationTest.java @@ -18,7 +18,7 @@ import static org.junit.Assert.assertThat; /** * Created by adam. */ -public class HttpResponseTest { +public class HttpResponseIntegrationTest { @Test public void shouldReturnStatusOKWhenSendGetRequest() throws IOException, InterruptedException, URISyntaxException { diff --git a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/streams.reactive/ReactiveStreamsTest.java b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/streams.reactive/ReactiveStreamsUnitTest.java similarity index 86% rename from core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/streams.reactive/ReactiveStreamsTest.java rename to core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/streams.reactive/ReactiveStreamsUnitTest.java index 647557532d..92cdc1c074 100644 --- a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/streams.reactive/ReactiveStreamsTest.java +++ b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/streams.reactive/ReactiveStreamsUnitTest.java @@ -5,10 +5,12 @@ import org.junit.Test; import java.util.List; import java.util.concurrent.SubmissionPublisher; +import java.util.concurrent.TimeUnit; -import static org.assertj.core.api.Java6Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; -public class ReactiveStreamsTest { +public class ReactiveStreamsUnitTest { @Test public void givenPublisher_whenSubscribeToIt_thenShouldConsumeAllElements() throws InterruptedException { @@ -25,7 +27,7 @@ public class ReactiveStreamsTest { //then - await().atMost(1000, TimeUnit.MILLISECONDS).until( + await().atMost(1000, TimeUnit.MILLISECONDS).untilAsserted( () -> assertThat(subscriber.consumedElements).containsExactlyElementsOf(items) ); } @@ -46,7 +48,7 @@ public class ReactiveStreamsTest { publisher.close(); //then - await().atMost(1000, TimeUnit.MILLISECONDS).until( + await().atMost(1000, TimeUnit.MILLISECONDS).untilAsserted( () -> assertThat(subscriber.consumedElements).containsExactlyElementsOf(expectedResult) ); } @@ -66,7 +68,7 @@ public class ReactiveStreamsTest { publisher.close(); //then - await().atMost(1000, TimeUnit.MILLISECONDS).until( + await().atMost(1000, TimeUnit.MILLISECONDS).untilAsserted( () -> assertThat(subscriber.consumedElements).containsExactlyElementsOf(expected) ); } diff --git a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/varhandles/VariableHandlesTest.java b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/varhandles/VariableHandlesUnitTest.java similarity index 75% rename from core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/varhandles/VariableHandlesTest.java rename to core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/varhandles/VariableHandlesUnitTest.java index 50766502ec..56e34f06a0 100644 --- a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/varhandles/VariableHandlesTest.java +++ b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/varhandles/VariableHandlesUnitTest.java @@ -7,7 +7,7 @@ import java.lang.invoke.VarHandle; import static org.assertj.core.api.Assertions.assertThat; -public class VariableHandlesTest { +public class VariableHandlesUnitTest { public int publicTestVariable = 1; private int privateTestVariable = 1; @@ -20,22 +20,22 @@ public class VariableHandlesTest { public void whenVariableHandleForPublicVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException { VarHandle publicIntHandle = MethodHandles .lookup() - .in(VariableHandlesTest.class) - .findVarHandle(VariableHandlesTest.class, "publicTestVariable", int.class); + .in(VariableHandlesUnitTest.class) + .findVarHandle(VariableHandlesUnitTest.class, "publicTestVariable", int.class); assertThat(publicIntHandle.coordinateTypes().size() == 1); - assertThat(publicIntHandle.coordinateTypes().get(0) == VariableHandles.class); + assertThat(publicIntHandle.coordinateTypes().get(0) == VariableHandlesUnitTest.class); } @Test public void whenVariableHandleForPrivateVariableIsCreated_ThenItIsInitializedProperly() throws NoSuchFieldException, IllegalAccessException { VarHandle privateIntHandle = MethodHandles - .privateLookupIn(VariableHandlesTest.class, MethodHandles.lookup()) - .findVarHandle(VariableHandlesTest.class, "privateTestVariable", int.class); + .privateLookupIn(VariableHandlesUnitTest.class, MethodHandles.lookup()) + .findVarHandle(VariableHandlesUnitTest.class, "privateTestVariable", int.class); assertThat(privateIntHandle.coordinateTypes().size() == 1); - assertThat(privateIntHandle.coordinateTypes().get(0) == VariableHandlesTest.class); + assertThat(privateIntHandle.coordinateTypes().get(0) == VariableHandlesUnitTest.class); } @@ -52,8 +52,8 @@ public class VariableHandlesTest { public void givenVarHandle_whenGetIsInvoked_ThenValueOfVariableIsReturned() throws NoSuchFieldException, IllegalAccessException { VarHandle publicIntHandle = MethodHandles .lookup() - .in(VariableHandlesTest.class) - .findVarHandle(VariableHandlesTest.class, "publicTestVariable", int.class); + .in(VariableHandlesUnitTest.class) + .findVarHandle(VariableHandlesUnitTest.class, "publicTestVariable", int.class); assertThat((int) publicIntHandle.get(this) == 1); } @@ -62,8 +62,8 @@ public class VariableHandlesTest { public void givenVarHandle_whenSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { VarHandle publicIntHandle = MethodHandles .lookup() - .in(VariableHandlesTest.class) - .findVarHandle(VariableHandlesTest.class, "variableToSet", int.class); + .in(VariableHandlesUnitTest.class) + .findVarHandle(VariableHandlesUnitTest.class, "variableToSet", int.class); publicIntHandle.set(this, 15); assertThat((int) publicIntHandle.get(this) == 15); @@ -73,8 +73,8 @@ public class VariableHandlesTest { public void givenVarHandle_whenCompareAndSetIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { VarHandle publicIntHandle = MethodHandles .lookup() - .in(VariableHandlesTest.class) - .findVarHandle(VariableHandlesTest.class, "variableToCompareAndSet", int.class); + .in(VariableHandlesUnitTest.class) + .findVarHandle(VariableHandlesUnitTest.class, "variableToCompareAndSet", int.class); publicIntHandle.compareAndSet(this, 1, 100); assertThat((int) publicIntHandle.get(this) == 100); @@ -84,8 +84,8 @@ public class VariableHandlesTest { public void givenVarHandle_whenGetAndAddIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { VarHandle publicIntHandle = MethodHandles .lookup() - .in(VariableHandlesTest.class) - .findVarHandle(VariableHandlesTest.class, "variableToGetAndAdd", int.class); + .in(VariableHandlesUnitTest.class) + .findVarHandle(VariableHandlesUnitTest.class, "variableToGetAndAdd", int.class); int before = (int) publicIntHandle.getAndAdd(this, 200); assertThat(before == 0); @@ -96,8 +96,8 @@ public class VariableHandlesTest { public void givenVarHandle_whenGetAndBitwiseOrIsInvoked_ThenValueOfVariableIsChanged() throws NoSuchFieldException, IllegalAccessException { VarHandle publicIntHandle = MethodHandles .lookup() - .in(VariableHandlesTest.class) - .findVarHandle(VariableHandlesTest.class, "variableToBitwiseOr", byte.class); + .in(VariableHandlesUnitTest.class) + .findVarHandle(VariableHandlesUnitTest.class, "variableToBitwiseOr", byte.class); byte before = (byte) publicIntHandle.getAndBitwiseOr(this, (byte) 127); assertThat(before == 0); From bddea5222bb03cdb266db1ba55ca91d8b4ba4de3 Mon Sep 17 00:00:00 2001 From: Bogdan Feraru Date: Sun, 10 May 2020 22:30:54 +0300 Subject: [PATCH 39/74] [BAEL-3890] Using Kafka MockConsumer --- libraries-data-2/pom.xml | 6 ++ .../kafka/consumer/CountryPopulation.java | 28 +++++ .../consumer/CountryPopulationConsumer.java | 60 +++++++++++ .../CountryPopulationConsumerUnitTest.java | 100 ++++++++++++++++++ 4 files changed, 194 insertions(+) create mode 100644 libraries-data-2/src/main/java/com/baeldung/kafka/consumer/CountryPopulation.java create mode 100644 libraries-data-2/src/main/java/com/baeldung/kafka/consumer/CountryPopulationConsumer.java create mode 100644 libraries-data-2/src/test/java/com/baeldung/kafka/consumer/CountryPopulationConsumerUnitTest.java diff --git a/libraries-data-2/pom.xml b/libraries-data-2/pom.xml index cbb24edd3f..bdfb2c5ed6 100644 --- a/libraries-data-2/pom.xml +++ b/libraries-data-2/pom.xml @@ -121,6 +121,11 @@ univocity-parsers ${univocity.version} + + org.apache.kafka + kafka-clients + ${kafka.version} + org.awaitility awaitility @@ -184,6 +189,7 @@ RELEASE 3.0 1.8.1 + 2.5.0 diff --git a/libraries-data-2/src/main/java/com/baeldung/kafka/consumer/CountryPopulation.java b/libraries-data-2/src/main/java/com/baeldung/kafka/consumer/CountryPopulation.java new file mode 100644 index 0000000000..8c1351642f --- /dev/null +++ b/libraries-data-2/src/main/java/com/baeldung/kafka/consumer/CountryPopulation.java @@ -0,0 +1,28 @@ +package com.baeldung.kafka.consumer; + +class CountryPopulation { + + private String country; + private Integer population; + + public CountryPopulation(String country, Integer population) { + this.country = country; + this.population = population; + } + + public String getCountry() { + return country; + } + + public void setCountry(String country) { + this.country = country; + } + + public Integer getPopulation() { + return population; + } + + public void setPopulation(Integer population) { + this.population = population; + } +} \ No newline at end of file diff --git a/libraries-data-2/src/main/java/com/baeldung/kafka/consumer/CountryPopulationConsumer.java b/libraries-data-2/src/main/java/com/baeldung/kafka/consumer/CountryPopulationConsumer.java new file mode 100644 index 0000000000..ba4dfe6f3b --- /dev/null +++ b/libraries-data-2/src/main/java/com/baeldung/kafka/consumer/CountryPopulationConsumer.java @@ -0,0 +1,60 @@ +package com.baeldung.kafka.consumer; + +import java.time.Duration; +import java.util.Collections; +import java.util.stream.StreamSupport; + +import org.apache.kafka.clients.consumer.Consumer; +import org.apache.kafka.clients.consumer.ConsumerRecords; +import org.apache.kafka.common.TopicPartition; +import org.apache.kafka.common.errors.WakeupException; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class CountryPopulationConsumer { + + private static Logger logger = LoggerFactory.getLogger(CountryPopulationConsumer.class); + + private Consumer consumer; + private java.util.function.Consumer exceptionConsumer; + private java.util.function.Consumer countryPopulationConsumer; + + public CountryPopulationConsumer( + Consumer consumer, java.util.function.Consumer exceptionConsumer, + java.util.function.Consumer countryPopulationConsumer) { + this.consumer = consumer; + this.exceptionConsumer = exceptionConsumer; + this.countryPopulationConsumer = countryPopulationConsumer; + } + + void startBySubscribing(String topic) { + consume(() -> consumer.subscribe(Collections.singleton(topic))); + } + + void startByAssigning(String topic, int partition) { + consume(() -> consumer.assign(Collections.singleton(new TopicPartition(topic, partition)))); + } + + private void consume(Runnable beforePollingTask) { + try { + beforePollingTask.run(); + while (true) { + ConsumerRecords records = consumer.poll(Duration.ofMillis(1000)); + StreamSupport.stream(records.spliterator(), false) + .map(record -> new CountryPopulation(record.key(), record.value())) + .forEach(countryPopulationConsumer); + consumer.commitSync(); + } + } catch (WakeupException e) { + logger.info("Shutting down..."); + } catch (RuntimeException ex) { + exceptionConsumer.accept(ex); + } finally { + consumer.close(); + } + } + + public void stop() { + consumer.wakeup(); + } +} \ No newline at end of file diff --git a/libraries-data-2/src/test/java/com/baeldung/kafka/consumer/CountryPopulationConsumerUnitTest.java b/libraries-data-2/src/test/java/com/baeldung/kafka/consumer/CountryPopulationConsumerUnitTest.java new file mode 100644 index 0000000000..1b49c71716 --- /dev/null +++ b/libraries-data-2/src/test/java/com/baeldung/kafka/consumer/CountryPopulationConsumerUnitTest.java @@ -0,0 +1,100 @@ +package com.baeldung.kafka.consumer; + +import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat; + +import java.util.ArrayList; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; + +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.apache.kafka.clients.consumer.MockConsumer; +import org.apache.kafka.clients.consumer.OffsetResetStrategy; +import org.apache.kafka.common.KafkaException; +import org.apache.kafka.common.TopicPartition; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +class CountryPopulationConsumerUnitTest { + + private static final String TOPIC = "topic"; + private static final int PARTITION = 0; + + private CountryPopulationConsumer countryPopulationConsumer; + + private List updates; + private Throwable pollException; + + private MockConsumer consumer; + + @BeforeEach + void setUp() { + consumer = new MockConsumer<>(OffsetResetStrategy.EARLIEST); + updates = new ArrayList<>(); + countryPopulationConsumer = new CountryPopulationConsumer(consumer, ex -> this.pollException = ex, updates::add); + } + + @Test + void whenStartingByAssigningTopicPartition_thenExpectUpdatesAreConsumedCorrectly() { + // GIVEN + consumer.schedulePollTask(() -> consumer.addRecord(record(TOPIC, PARTITION, "Romania", 19_410_000))); + consumer.schedulePollTask(() -> countryPopulationConsumer.stop()); + + HashMap startOffsets = new HashMap<>(); + TopicPartition tp = new TopicPartition(TOPIC, PARTITION); + startOffsets.put(tp, 0L); + consumer.updateBeginningOffsets(startOffsets); + + // WHEN + countryPopulationConsumer.startByAssigning(TOPIC, PARTITION); + + // THEN + assertThat(updates).hasSize(1); + assertThat(consumer.closed()).isTrue(); + } + + @Test + void whenStartingBySubscribingToTopic_thenExpectUpdatesAreConsumedCorrectly() { + // GIVEN + consumer.schedulePollTask(() -> { + consumer.rebalance(Collections.singletonList(new TopicPartition(TOPIC, 0))); + consumer.addRecord(record(TOPIC, PARTITION, "Romania", 19_410_000)); + }); + consumer.schedulePollTask(() -> countryPopulationConsumer.stop()); + + HashMap startOffsets = new HashMap<>(); + TopicPartition tp = new TopicPartition(TOPIC, PARTITION); + startOffsets.put(tp, 0L); + consumer.updateBeginningOffsets(startOffsets); + + // WHEN + countryPopulationConsumer.startBySubscribing(TOPIC); + + // THEN + assertThat(updates).hasSize(1); + assertThat(consumer.closed()).isTrue(); + } + + @Test + void whenStartingBySubscribingToTopicAndExceptionOccurs_thenExpectExceptionIsHandledCorrectly() { + // GIVEN + consumer.schedulePollTask(() -> consumer.setPollException(new KafkaException("poll exception"))); + consumer.schedulePollTask(() -> countryPopulationConsumer.stop()); + + HashMap startOffsets = new HashMap<>(); + TopicPartition tp = new TopicPartition(TOPIC, 0); + startOffsets.put(tp, 0L); + consumer.updateBeginningOffsets(startOffsets); + + // WHEN + countryPopulationConsumer.startBySubscribing(TOPIC); + + // THEN + assertThat(pollException).isInstanceOf(KafkaException.class).hasMessage("poll exception"); + assertThat(consumer.closed()).isTrue(); + } + + private ConsumerRecord record(String topic, int partition, String country, int population) { + return new ConsumerRecord<>(topic, partition, 0, country, population); + } +} \ No newline at end of file From 3c455c5c56e13f3905834785d9a89c8cdf09dcc8 Mon Sep 17 00:00:00 2001 From: Krzysztof Woyke Date: Mon, 25 May 2020 13:03:25 +0200 Subject: [PATCH 40/74] JAVA-1635: Remove unnecessary dependencies and overridden spring-boot.version --- .../spring-boot-persistence-h2/pom.xml | 12 +++++------- .../src/main/resources/application.properties | 3 +-- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/persistence-modules/spring-boot-persistence-h2/pom.xml b/persistence-modules/spring-boot-persistence-h2/pom.xml index 777bc6cb2d..7070b5e674 100644 --- a/persistence-modules/spring-boot-persistence-h2/pom.xml +++ b/persistence-modules/spring-boot-persistence-h2/pom.xml @@ -35,23 +35,21 @@ ${lombok.version} compile - - org.hibernate - hibernate-core - ${hibernate.version} - com.vladmihalcea db-util ${db-util.version} + + net.bytebuddy + byte-buddy + ${byte-buddy.version} + com.baeldung.h2db.demo.server.SpringBootApp - 2.0.4.RELEASE - 5.3.11.Final 1.0.4 diff --git a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.properties b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.properties index ed1ffc63c3..0466eaac79 100644 --- a/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.properties +++ b/persistence-modules/spring-boot-persistence-h2/src/main/resources/application.properties @@ -8,5 +8,4 @@ spring.jpa.properties.hibernate.format_sql=true spring.jpa.properties.hibernate.validator.apply_to_ddl=false #spring.jpa.properties.hibernate.check_nullability=true spring.h2.console.enabled=true -spring.h2.console.path=/h2-console -debug=true \ No newline at end of file +spring.h2.console.path=/h2-console \ No newline at end of file From 8a771a8b467baf06aca796296fe5e85bafbd1421 Mon Sep 17 00:00:00 2001 From: Ashley Frieze Date: Fri, 22 May 2020 12:42:39 +0100 Subject: [PATCH 41/74] Rework the delete everything in Redis example --- persistence-modules/redis/README.md | 1 + persistence-modules/redis/pom.xml | 3 +- ...eleteEverythingInRedisIntegrationTest.java | 91 ++++++++++++++++++ .../spring-data-redis/README.md | 2 - .../spring/data/redis/config/RedisConfig.java | 12 --- .../RedisFlushDatabaseIntegrationTest.java | 92 ------------------- 6 files changed, 94 insertions(+), 107 deletions(-) create mode 100644 persistence-modules/redis/src/test/java/com/baeldung/redis/deleteeverything/DeleteEverythingInRedisIntegrationTest.java delete mode 100644 persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/delete/RedisFlushDatabaseIntegrationTest.java diff --git a/persistence-modules/redis/README.md b/persistence-modules/redis/README.md index 71d009241a..75c1c18de4 100644 --- a/persistence-modules/redis/README.md +++ b/persistence-modules/redis/README.md @@ -4,3 +4,4 @@ - [Introduction to Lettuce – the Java Redis Client](https://www.baeldung.com/java-redis-lettuce) - [List All Available Redis Keys](https://www.baeldung.com/redis-list-available-keys) - [Spring Data Redis’s Property-Based Configuration](https://www.baeldung.com/spring-data-redis-properties) +- [Delete Everything in Redis](https://www.baeldung.com/redis-delete-data) diff --git a/persistence-modules/redis/pom.xml b/persistence-modules/redis/pom.xml index c9206e5f92..dab7fc5654 100644 --- a/persistence-modules/redis/pom.xml +++ b/persistence-modules/redis/pom.xml @@ -33,7 +33,8 @@ redis.clients jedis - + 3.3.0 + com.github.kstyrc embedded-redis diff --git a/persistence-modules/redis/src/test/java/com/baeldung/redis/deleteeverything/DeleteEverythingInRedisIntegrationTest.java b/persistence-modules/redis/src/test/java/com/baeldung/redis/deleteeverything/DeleteEverythingInRedisIntegrationTest.java new file mode 100644 index 0000000000..e0376fc6a5 --- /dev/null +++ b/persistence-modules/redis/src/test/java/com/baeldung/redis/deleteeverything/DeleteEverythingInRedisIntegrationTest.java @@ -0,0 +1,91 @@ +package com.baeldung.redis.deleteeverything; + +import org.junit.*; +import redis.clients.jedis.Jedis; +import redis.embedded.RedisServer; + +import java.io.IOException; +import java.net.ServerSocket; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class DeleteEverythingInRedisIntegrationTest { + private Jedis jedis; + private RedisServer redisServer; + private int port; + + @Before + public void setUp() throws IOException { + + // Take an available port + ServerSocket s = new ServerSocket(0); + port = s.getLocalPort(); + s.close(); + + redisServer = new RedisServer(port); + redisServer.start(); + + // Configure JEDIS + jedis = new Jedis("localhost", port); + } + + @After + public void destroy() { + redisServer.stop(); + } + + @Test + public void whenPutDataIntoRedis_thenCanBeFound() { + String key = "key"; + String value = "value"; + + jedis.set(key, value); + String received = jedis.get(key); + + assertEquals(value, received); + } + + @Test + public void whenPutDataIntoRedisAndThenFlush_thenCannotBeFound() { + String key = "key"; + String value = "value"; + + jedis.set(key, value); + + jedis.flushDB(); + + String received = jedis.get(key); + + assertNull(received); + } + + @Test + public void whenPutDataIntoMultipleDatabases_thenFlushAllRemovesAll() { + // add keys in different databases + jedis.select(0); + jedis.set("key1", "value1"); + jedis.select(1); + jedis.set("key2", "value2"); + + // we'll find the correct keys in the correct dbs + jedis.select(0); + assertEquals("value1", jedis.get("key1")); + assertNull(jedis.get("key2")); + + jedis.select(1); + assertEquals("value2", jedis.get("key2")); + assertNull(jedis.get("key1")); + + // then, when we flush + jedis.flushAll(); + + // the keys will have gone + jedis.select(0); + assertNull(jedis.get("key1")); + assertNull(jedis.get("key2")); + jedis.select(1); + assertNull(jedis.get("key1")); + assertNull(jedis.get("key2")); + } +} diff --git a/persistence-modules/spring-data-redis/README.md b/persistence-modules/spring-data-redis/README.md index 175634376b..95cba2c159 100644 --- a/persistence-modules/spring-data-redis/README.md +++ b/persistence-modules/spring-data-redis/README.md @@ -4,7 +4,6 @@ - [Introduction to Spring Data Redis](https://www.baeldung.com/spring-data-redis-tutorial) - [PubSub Messaging with Spring Data Redis](https://www.baeldung.com/spring-data-redis-pub-sub) - [An Introduction to Spring Data Redis Reactive](https://www.baeldung.com/spring-data-redis-reactive) -- [Delete Everything in Redis](https://www.baeldung.com/redis-delete-data) ### Build the Project with Tests Running ``` @@ -15,4 +14,3 @@ mvn clean install ``` mvn test ``` - diff --git a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java index fdc279be42..497e1506bd 100644 --- a/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java +++ b/persistence-modules/spring-data-redis/src/main/java/com/baeldung/spring/data/redis/config/RedisConfig.java @@ -35,18 +35,6 @@ public class RedisConfig { template.setValueSerializer(new GenericToStringSerializer(Object.class)); return template; } - - @Bean - public LettuceConnectionFactory lettuceConnectionFactory() { - return new LettuceConnectionFactory(); - } - - @Bean(name = "flushRedisTemplate") - public RedisTemplate flushRedisTemplate() { - RedisTemplate template = new RedisTemplate<>(); - template.setConnectionFactory(lettuceConnectionFactory()); - return template; - } @Bean MessageListenerAdapter messageListener() { diff --git a/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/delete/RedisFlushDatabaseIntegrationTest.java b/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/delete/RedisFlushDatabaseIntegrationTest.java deleted file mode 100644 index 1f56cbb25d..0000000000 --- a/persistence-modules/spring-data-redis/src/test/java/com/baeldung/spring/data/redis/delete/RedisFlushDatabaseIntegrationTest.java +++ /dev/null @@ -1,92 +0,0 @@ -package com.baeldung.spring.data.redis.delete; - -import static org.assertj.core.api.Assertions.assertThat; - -import java.io.IOException; - -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.beans.factory.annotation.Qualifier; -import org.springframework.dao.DataAccessException; -import org.springframework.data.redis.connection.RedisConnection; -import org.springframework.data.redis.core.RedisCallback; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.data.redis.core.ValueOperations; -import org.springframework.test.annotation.DirtiesContext; -import org.springframework.test.annotation.DirtiesContext.ClassMode; -import org.springframework.test.context.ContextConfiguration; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.baeldung.spring.data.redis.config.RedisConfig; - -import redis.embedded.RedisServer; - -@RunWith(SpringJUnit4ClassRunner.class) -@ContextConfiguration(classes = { RedisConfig.class }) -@DirtiesContext(classMode = ClassMode.BEFORE_CLASS) -public class RedisFlushDatabaseIntegrationTest { - - private RedisServer redisServer; - - @Autowired - @Qualifier("flushRedisTemplate") - private RedisTemplate flushRedisTemplate; - - @Before - public void setup() throws IOException { - redisServer = new RedisServer(6390); - redisServer.start(); - } - - @After - public void tearDown() { - redisServer.stop(); - } - - @Test - public void whenFlushDB_thenAllKeysInDatabaseAreCleared() { - - ValueOperations simpleValues = flushRedisTemplate.opsForValue(); - String key = "key"; - String value = "value"; - simpleValues.set(key, value); - assertThat(simpleValues.get(key)).isEqualTo(value); - - flushRedisTemplate.execute(new RedisCallback() { - - @Override - public Void doInRedis(RedisConnection connection) throws DataAccessException { - connection.flushDb(); - return null; - } - }); - - assertThat(simpleValues.get(key)).isNull(); - - } - - @Test - public void whenFlushAll_thenAllKeysInDatabasesAreCleared() { - - ValueOperations simpleValues = flushRedisTemplate.opsForValue(); - String key = "key"; - String value = "value"; - simpleValues.set(key, value); - assertThat(simpleValues.get(key)).isEqualTo(value); - - flushRedisTemplate.execute(new RedisCallback() { - - @Override - public Void doInRedis(RedisConnection connection) throws DataAccessException { - connection.flushAll(); - return null; - } - }); - - assertThat(simpleValues.get(key)).isNull(); - - } -} \ No newline at end of file From 11d0ab1b4984095d52a379c16aad81d19af2a7fe Mon Sep 17 00:00:00 2001 From: Vikas Ramsingh Rajput Date: Tue, 26 May 2020 12:59:40 +0300 Subject: [PATCH 42/74] BAEL-3067: Changed method name and added latest maven dependency of org.json --- core-java-modules/core-java-io-conversions-2/pom.xml | 2 +- .../JavaBufferedReaderToJSONObjectUnitTest.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core-java-modules/core-java-io-conversions-2/pom.xml b/core-java-modules/core-java-io-conversions-2/pom.xml index 7a83d4b502..c1148509f3 100644 --- a/core-java-modules/core-java-io-conversions-2/pom.xml +++ b/core-java-modules/core-java-io-conversions-2/pom.xml @@ -25,7 +25,7 @@ org.json json - 20190722 + 20200518 diff --git a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/bufferedreadertojsonobject/JavaBufferedReaderToJSONObjectUnitTest.java b/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/bufferedreadertojsonobject/JavaBufferedReaderToJSONObjectUnitTest.java index d42c3975db..80007e9c2f 100644 --- a/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/bufferedreadertojsonobject/JavaBufferedReaderToJSONObjectUnitTest.java +++ b/core-java-modules/core-java-io-conversions-2/src/test/java/com/baeldung/bufferedreadertojsonobject/JavaBufferedReaderToJSONObjectUnitTest.java @@ -17,7 +17,7 @@ import org.junit.Test; public class JavaBufferedReaderToJSONObjectUnitTest { @Test - public void givenUsingNewVersion_whenConvertBufferedReaderToJSONObject_thenCorrect() { + public void givenValidJson_whenUsingBufferedReader_thenJSONTokenerConverts() { byte[] b = "{ \"name\" : \"John\", \"age\" : 18 }".getBytes(StandardCharsets.UTF_8); InputStream is = new ByteArrayInputStream(b); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); @@ -30,7 +30,7 @@ public class JavaBufferedReaderToJSONObjectUnitTest { } @Test - public void givenUsingOldVersion_whenConvertBufferedReaderToJSONObject_thenCorrect() throws IOException { + public void givenValidJson_whenUsingString_thenJSONObjectConverts() throws IOException { byte[] b = "{ \"name\" : \"John\", \"age\" : 18 }".getBytes(StandardCharsets.UTF_8); InputStream is = new ByteArrayInputStream(b); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is)); From 7b53c3cb55b110470ae9e4528aef3a165fa1a873 Mon Sep 17 00:00:00 2001 From: Anshul Bansal Date: Tue, 26 May 2020 18:58:17 +0300 Subject: [PATCH 43/74] BAEL-3996_Spring_Security_with_Auth0 (#9300) * BAEL-3996 - Spring Security with Auth0 * BAEL-3996 - Spring Security with Auth0 code fixes * BAEL-3996 - Spring Security with Auth0 * BAEL-3996 - removed spaces * BAEL-3996 - RequestMapping is updated to GetMapping --- spring-security-modules/pom.xml | 1 + .../spring-security-auth0/pom.xml | 75 ++++++++++++ .../java/com/baeldung/auth0/Application.java | 13 ++ .../java/com/baeldung/auth0/AuthConfig.java | 114 ++++++++++++++++++ .../auth0/controller/AuthController.java | 77 ++++++++++++ .../auth0/controller/HomeController.java | 37 ++++++ .../auth0/controller/LogoutController.java | 35 ++++++ .../auth0/controller/UserController.java | 57 +++++++++ .../baeldung/auth0/service/ApiService.java | 44 +++++++ .../src/main/resources/application.properties | 7 ++ 10 files changed, 460 insertions(+) create mode 100644 spring-security-modules/spring-security-auth0/pom.xml create mode 100644 spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/Application.java create mode 100644 spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/AuthConfig.java create mode 100644 spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/AuthController.java create mode 100644 spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/HomeController.java create mode 100755 spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/LogoutController.java create mode 100644 spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/UserController.java create mode 100644 spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/service/ApiService.java create mode 100644 spring-security-modules/spring-security-auth0/src/main/resources/application.properties diff --git a/spring-security-modules/pom.xml b/spring-security-modules/pom.xml index 7ce33dd3e3..60a662781f 100644 --- a/spring-security-modules/pom.xml +++ b/spring-security-modules/pom.xml @@ -15,6 +15,7 @@ spring-security-acl + spring-security-auth0 spring-security-angular/server spring-security-cache-control spring-security-core diff --git a/spring-security-modules/spring-security-auth0/pom.xml b/spring-security-modules/spring-security-auth0/pom.xml new file mode 100644 index 0000000000..0bd879a40b --- /dev/null +++ b/spring-security-modules/spring-security-auth0/pom.xml @@ -0,0 +1,75 @@ + + + 4.0.0 + spring-security-auth0 + 1.0-SNAPSHOT + spring-security-auth0 + war + + + com.baeldung + parent-boot-2 + 0.0.1-SNAPSHOT + ../../parent-boot-2 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-security + + + org.springframework.security + spring-security-core + + + org.springframework.security + spring-security-oauth2-resource-server + + + com.auth0 + mvc-auth-commons + ${mvc-auth-commons.version} + + + org.json + json + ${json.version} + + + + + spring-security-auth0 + + + src/main/resources + + + + + org.springframework.boot + spring-boot-maven-plugin + + true + + + + + repackage + + + + + + + + + 20190722 + 1.2.0 + + \ No newline at end of file diff --git a/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/Application.java b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/Application.java new file mode 100644 index 0000000000..42f8d946b5 --- /dev/null +++ b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/Application.java @@ -0,0 +1,13 @@ +package com.baeldung.auth0; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } + +} diff --git a/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/AuthConfig.java b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/AuthConfig.java new file mode 100644 index 0000000000..69cf8b3071 --- /dev/null +++ b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/AuthConfig.java @@ -0,0 +1,114 @@ +package com.baeldung.auth0; + +import java.io.UnsupportedEncodingException; + +import javax.servlet.http.HttpServletRequest; + +import org.springframework.beans.factory.annotation.Value; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity; +import org.springframework.security.config.annotation.web.builders.HttpSecurity; +import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; +import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; +import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; + +import com.auth0.AuthenticationController; +import com.baeldung.auth0.controller.LogoutController; +import com.auth0.jwk.JwkProvider; +import com.auth0.jwk.JwkProviderBuilder; + +@Configuration +@EnableWebSecurity +public class AuthConfig extends WebSecurityConfigurerAdapter { + + @Value(value = "${com.auth0.domain}") + private String domain; + + @Value(value = "${com.auth0.clientId}") + private String clientId; + + @Value(value = "${com.auth0.clientSecret}") + private String clientSecret; + + @Value(value = "${com.auth0.managementApi.clientId}") + private String managementApiClientId; + + @Value(value = "${com.auth0.managementApi.clientSecret}") + private String managementApiClientSecret; + + @Value(value = "${com.auth0.managementApi.grantType}") + private String grantType; + + @Bean + public LogoutSuccessHandler logoutSuccessHandler() { + return new LogoutController(); + } + + @Bean + public AuthenticationController authenticationController() throws UnsupportedEncodingException { + JwkProvider jwkProvider = new JwkProviderBuilder(domain).build(); + return AuthenticationController.newBuilder(domain, clientId, clientSecret) + .withJwkProvider(jwkProvider) + .build(); + } + + @Override + protected void configure(HttpSecurity http) throws Exception { + http.csrf().disable(); + http + .authorizeRequests() + .antMatchers("/callback", "/login", "/").permitAll() + .anyRequest().authenticated() + .and() + .formLogin() + .loginPage("/login") + .and() + .logout().logoutSuccessHandler(logoutSuccessHandler()).permitAll(); + } + + public String getDomain() { + return domain; + } + + public String getClientId() { + return clientId; + } + + public String getClientSecret() { + return clientSecret; + } + + public String getManagementApiClientId() { + return managementApiClientId; + } + + public String getManagementApiClientSecret() { + return managementApiClientSecret; + } + + public String getGrantType() { + return grantType; + } + + public String getUserInfoUrl() { + return "https://" + getDomain() + "/userinfo"; + } + + public String getUsersUrl() { + return "https://" + getDomain() + "/api/v2/users"; + } + + public String getUsersByEmailUrl() { + return "https://" + getDomain() + "/api/v2/users-by-email?email="; + } + + public String getLogoutUrl() { + return "https://" + getDomain() +"/v2/logout"; + } + + public String getContextPath(HttpServletRequest request) { + String path = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort(); + return path; + } +} diff --git a/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/AuthController.java b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/AuthController.java new file mode 100644 index 0000000000..48d09db155 --- /dev/null +++ b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/AuthController.java @@ -0,0 +1,77 @@ +package com.baeldung.auth0.controller; + +import java.io.IOException; +import java.util.HashMap; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.json.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.security.authentication.TestingAuthenticationToken; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.client.RestTemplate; + +import com.auth0.AuthenticationController; +import com.auth0.IdentityVerificationException; +import com.auth0.Tokens; +import com.auth0.jwt.JWT; +import com.auth0.jwt.interfaces.DecodedJWT; +import com.baeldung.auth0.AuthConfig; + +@Controller +public class AuthController { + + @Autowired + private AuthenticationController authenticationController; + + @Autowired + private AuthConfig config; + + private static final String AUTH0_TOKEN_URL = "https://dev-example.auth0.com/oauth/token"; + + @GetMapping(value = "/login") + protected void login(HttpServletRequest request, HttpServletResponse response) throws IOException { + String redirectUri = config.getContextPath(request) + "/callback"; + String authorizeUrl = authenticationController.buildAuthorizeUrl(request, response, redirectUri) + .withScope("openid email") + .build(); + response.sendRedirect(authorizeUrl); + } + + @GetMapping(value="/callback") + public void callback(HttpServletRequest request, HttpServletResponse response) throws IOException, IdentityVerificationException { + Tokens tokens = authenticationController.handle(request, response); + + DecodedJWT jwt = JWT.decode(tokens.getIdToken()); + TestingAuthenticationToken authToken2 = new TestingAuthenticationToken(jwt.getSubject(), jwt.getToken()); + authToken2.setAuthenticated(true); + + SecurityContextHolder.getContext().setAuthentication(authToken2); + response.sendRedirect(config.getContextPath(request) + "/"); + } + + public String getManagementApiToken() { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + + JSONObject requestBody = new JSONObject(); + requestBody.put("client_id", config.getManagementApiClientId()); + requestBody.put("client_secret", config.getManagementApiClientSecret()); + requestBody.put("audience", "https://dev-example.auth0.com/api/v2/"); + requestBody.put("grant_type", config.getGrantType()); + + HttpEntity request = new HttpEntity(requestBody.toString(), headers); + + RestTemplate restTemplate = new RestTemplate(); + HashMap result = restTemplate.postForObject(AUTH0_TOKEN_URL, request, HashMap.class); + + return result.get("access_token"); + } + +} diff --git a/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/HomeController.java b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/HomeController.java new file mode 100644 index 0000000000..8a4e650846 --- /dev/null +++ b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/HomeController.java @@ -0,0 +1,37 @@ +package com.baeldung.auth0.controller; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.security.authentication.TestingAuthenticationToken; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.auth0.jwt.JWT; +import com.auth0.jwt.interfaces.DecodedJWT; + +@Controller +public class HomeController { + + @GetMapping(value = "/") + @ResponseBody + public String home(HttpServletRequest request, HttpServletResponse response, final Authentication authentication) throws IOException { + + if (authentication!= null && authentication instanceof TestingAuthenticationToken) { + TestingAuthenticationToken token = (TestingAuthenticationToken) authentication; + + DecodedJWT jwt = JWT.decode(token.getCredentials().toString()); + String email = jwt.getClaims().get("email").asString(); + + return "Welcome, " + email + "!"; + } else { + response.sendRedirect("http://localhost:8080/login"); + return null; + } + } + +} diff --git a/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/LogoutController.java b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/LogoutController.java new file mode 100755 index 0000000000..d508fe2c44 --- /dev/null +++ b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/LogoutController.java @@ -0,0 +1,35 @@ +package com.baeldung.auth0.controller; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.security.core.Authentication; +import org.springframework.security.web.authentication.logout.LogoutSuccessHandler; +import org.springframework.stereotype.Controller; + +import com.baeldung.auth0.AuthConfig; + +@Controller +public class LogoutController implements LogoutSuccessHandler { + + @Autowired + private AuthConfig config; + + @Override + public void onLogoutSuccess(HttpServletRequest req, HttpServletResponse res, Authentication authentication) { + if (req.getSession() != null) { + req.getSession().invalidate(); + } + String returnTo = config.getContextPath(req); + String logoutUrl = config.getLogoutUrl() + "?client_id=" + config.getClientId() + "&returnTo=" +returnTo; + try { + res.sendRedirect(logoutUrl); + } catch(IOException e){ + e.printStackTrace(); + } + } + +} diff --git a/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/UserController.java b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/UserController.java new file mode 100644 index 0000000000..86601a06d3 --- /dev/null +++ b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/controller/UserController.java @@ -0,0 +1,57 @@ +package com.baeldung.auth0.controller; + +import java.io.IOException; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.json.JSONObject; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestParam; +import org.springframework.web.bind.annotation.ResponseBody; + +import com.auth0.IdentityVerificationException; +import com.baeldung.auth0.AuthConfig; +import com.baeldung.auth0.service.ApiService; + +@Controller +public class UserController { + + @Autowired + private ApiService apiService; + + @Autowired + private AuthConfig config; + + @GetMapping(value="/users") + @ResponseBody + public ResponseEntity users(HttpServletRequest request, HttpServletResponse response) throws IOException, IdentityVerificationException { + ResponseEntity result = apiService.getCall(config.getUsersUrl()); + return result; + } + + @GetMapping(value = "/userByEmail") + @ResponseBody + public ResponseEntity userByEmail(HttpServletResponse response, @RequestParam String email) { + ResponseEntity result = apiService.getCall(config.getUsersByEmailUrl()+email); + return result; + } + + @GetMapping(value = "/createUser") + @ResponseBody + public ResponseEntity createUser(HttpServletResponse response) { + JSONObject request = new JSONObject(); + request.put("email", "norman.lewis@email.com"); + request.put("given_name", "Norman"); + request.put("family_name", "Lewis"); + request.put("connection", "Username-Password-Authentication"); + request.put("password", "Pa33w0rd"); + + ResponseEntity result = apiService.postCall(config.getUsersUrl(), request.toString()); + return result; + } + +} diff --git a/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/service/ApiService.java b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/service/ApiService.java new file mode 100644 index 0000000000..0d8263ae19 --- /dev/null +++ b/spring-security-modules/spring-security-auth0/src/main/java/com/baeldung/auth0/service/ApiService.java @@ -0,0 +1,44 @@ +package com.baeldung.auth0.service; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; +import org.springframework.stereotype.Service; +import org.springframework.web.client.RestTemplate; + +import com.baeldung.auth0.controller.AuthController; + +@Service +public class ApiService { + + @Autowired + private AuthController controller; + + public ResponseEntity getCall(String url) { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.set("Authorization", "Bearer "+controller.getManagementApiToken()); + + HttpEntity entity = new HttpEntity(headers); + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity result = restTemplate.exchange(url, HttpMethod.GET, entity, String.class); + + return result; + } + + public ResponseEntity postCall(String url, String requestBody) { + HttpHeaders headers = new HttpHeaders(); + headers.setContentType(MediaType.APPLICATION_JSON); + headers.set("Authorization", "Bearer "+controller.getManagementApiToken()); + + HttpEntity request = new HttpEntity(requestBody, headers); + RestTemplate restTemplate = new RestTemplate(); + ResponseEntity result = restTemplate.postForEntity(url, request, String.class); + + return result; + } + +} diff --git a/spring-security-modules/spring-security-auth0/src/main/resources/application.properties b/spring-security-modules/spring-security-auth0/src/main/resources/application.properties new file mode 100644 index 0000000000..45492c5c00 --- /dev/null +++ b/spring-security-modules/spring-security-auth0/src/main/resources/application.properties @@ -0,0 +1,7 @@ +com.auth0.domain: dev-example.auth0.com +com.auth0.clientId: exampleClientId +com.auth0.clientSecret: exampleClientSecret + +com.auth0.managementApi.clientId: exampleManagementApiClientId +com.auth0.managementApi.clientSecret: exampleManagementApiClientSecret +com.auth0.managementApi.grantType: client_credentials \ No newline at end of file From 771b415b8708bc6282b16b71cadb9706a88a9276 Mon Sep 17 00:00:00 2001 From: Krzysztof Woyke Date: Wed, 27 May 2020 13:48:16 +0200 Subject: [PATCH 44/74] JAVA-1641: Remove overriden spring-boot.version property --- spring-boot-modules/spring-boot-mvc-birt/pom.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/spring-boot-modules/spring-boot-mvc-birt/pom.xml b/spring-boot-modules/spring-boot-mvc-birt/pom.xml index f65b851f30..0e8e231a84 100644 --- a/spring-boot-modules/spring-boot-mvc-birt/pom.xml +++ b/spring-boot-modules/spring-boot-mvc-birt/pom.xml @@ -75,7 +75,6 @@ - 2.1.1.RELEASE com.baeldung.birt.engine.ReportEngineApplication 1.8 1.8 From 9dc7483313d0bd04a61e755c30218f1d1aaff14e Mon Sep 17 00:00:00 2001 From: kwoyke Date: Wed, 27 May 2020 17:27:11 +0200 Subject: [PATCH 45/74] BAEL-3448: Downgrade logback version & fix config (#9354) --- jee-kotlin/pom.xml | 3 ++- jee-kotlin/src/main/resources/META-INF/persistence.xml | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/jee-kotlin/pom.xml b/jee-kotlin/pom.xml index 9191885bd4..14c47a205e 100644 --- a/jee-kotlin/pom.xml +++ b/jee-kotlin/pom.xml @@ -278,9 +278,10 @@ 2.0.1.Final 1.0.0.Alpha4 + 1.1.7 + 3.8.0.Final 3.1.3 - diff --git a/jee-kotlin/src/main/resources/META-INF/persistence.xml b/jee-kotlin/src/main/resources/META-INF/persistence.xml index daac86868b..0093792810 100644 --- a/jee-kotlin/src/main/resources/META-INF/persistence.xml +++ b/jee-kotlin/src/main/resources/META-INF/persistence.xml @@ -7,7 +7,7 @@ java:jboss/datasources/ExampleDS - com.enpy.entity.Student + com.baeldung.jeekotlin.entity.Student From 75b741954514f58b1e1359b331d58648fdd08147 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Wed, 27 May 2020 17:33:29 +0200 Subject: [PATCH 46/74] KTLN-149: Get rid of the invalid buildString example (#9373) --- .../com/baeldung/stringcomparison/StringComparisonTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/core-kotlin-modules/core-kotlin-strings/src/test/kotlin/com/baeldung/stringcomparison/StringComparisonTest.kt b/core-kotlin-modules/core-kotlin-strings/src/test/kotlin/com/baeldung/stringcomparison/StringComparisonTest.kt index 9528f62df5..49ff798faa 100644 --- a/core-kotlin-modules/core-kotlin-strings/src/test/kotlin/com/baeldung/stringcomparison/StringComparisonTest.kt +++ b/core-kotlin-modules/core-kotlin-strings/src/test/kotlin/com/baeldung/stringcomparison/StringComparisonTest.kt @@ -19,9 +19,9 @@ class StringComparisonUnitTest { fun `compare using referential equals operator`() { val first = "kotlin" val second = "kotlin" - val copyOfFirst = buildString { "kotlin" } + val third = String("kotlin".toCharArray()) assertTrue { first === second } - assertFalse { first === copyOfFirst } + assertFalse { first === third } } @Test From e1c3aed23b75c41a28e479ad01a31dcdfc051c66 Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Wed, 27 May 2020 19:43:04 +0200 Subject: [PATCH 47/74] Added @Query method + clean --- .../PartialUpdateApplication.java | 8 ------- .../partialupdate/model/ContactPhone.java | 4 ++-- .../partialupdate/model/CustomerDto.java | 12 +++++----- .../model/CustomerStructured.java | 7 +++--- .../repository/ContactPhoneRepository.java | 4 ++-- .../repository/CustomerRepository.java | 9 ++++++-- .../service/CustomerService.java | 8 +++++++ .../partialupdate/PartialUpdateUnitTest.java | 23 ++++++++++++------- 8 files changed, 44 insertions(+), 31 deletions(-) diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java index 30f5dfa03d..a750fcadf7 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/PartialUpdateApplication.java @@ -1,20 +1,12 @@ package com.baeldung.partialupdate; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.cache.CacheManager; -import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication -@EnableCaching public class PartialUpdateApplication { - @Autowired - CacheManager cacheManager; - public static void main(String[] args) { SpringApplication.run(PartialUpdateApplication.class, args); } - } diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java index 9d611fa755..352e361bd9 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/ContactPhone.java @@ -8,11 +8,11 @@ import javax.persistence.Id; @Entity public class ContactPhone { - @Id + @Id @GeneratedValue(strategy = GenerationType.IDENTITY) public long id; @Column(nullable=false) - public long customerId; + public long customerId; public String phone; @Override diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java index 4087838f9a..0ecf206d9a 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerDto.java @@ -6,21 +6,21 @@ public class CustomerDto { public String phone; //... private String phone99; - + public CustomerDto(long id) { - this.id = id; + this.id = id; } - + public CustomerDto(Customer c) { this.id = c.id; this.name = c.name; this.phone = c.phone; } - + public long getId() { - return this.id; + return this.id; } - + public Customer convertToEntity() { Customer c = new Customer(); c.id = id; diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java index 67b534add2..dd053a963d 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/model/CustomerStructured.java @@ -16,11 +16,12 @@ public class CustomerStructured { @GeneratedValue(strategy = GenerationType.IDENTITY) public long id; public String name; - @OneToMany(fetch = FetchType.EAGER, targetEntity=ContactPhone.class, mappedBy="customerId") + @OneToMany(fetch = FetchType.EAGER, targetEntity = ContactPhone.class, mappedBy = "customerId") public List contactPhones; - + @Override public String toString() { return String.format("Customer %s, Phone: %s", - this.name, this.contactPhones.stream().map(e -> e.toString()).reduce("", String::concat)); + this.name, this.contactPhones.stream() + .map(e -> e.toString()).reduce("", String::concat)); } } diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java index 2e0b687227..4668181e05 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/ContactPhoneRepository.java @@ -7,6 +7,6 @@ import com.baeldung.partialupdate.model.ContactPhone; @Repository public interface ContactPhoneRepository extends CrudRepository { - ContactPhone findById(long id); - ContactPhone findByCustomerId(long id); + ContactPhone findById(long id); + ContactPhone findByCustomerId(long id); } \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java index bd33c03b6b..43e61df8ab 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/repository/CustomerRepository.java @@ -1,13 +1,18 @@ package com.baeldung.partialupdate.repository; -import org.springframework.cache.annotation.Cacheable; +import org.springframework.data.jpa.repository.Modifying; +import org.springframework.data.jpa.repository.Query; import org.springframework.data.repository.CrudRepository; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; import com.baeldung.partialupdate.model.Customer; @Repository public interface CustomerRepository extends CrudRepository { - @Cacheable("customers") Customer findById(long id); + + @Modifying + @Query("update Customer u set u.phone = :phone where u.id = :id") + void updatePhone(@Param(value = "id") long id, @Param(value = "phone") String phone); } \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java index f3a1c65ef1..9da97a7775 100644 --- a/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java +++ b/persistence-modules/spring-data-jpa-5/src/main/java/com/baeldung/partialupdate/service/CustomerService.java @@ -27,6 +27,14 @@ public class CustomerService { @Autowired CustomerMapper mapper; + public Customer getCustomer(long id) { + return repo.findById(id); + } + + public void updateCustomerWithCustomQuery(long id, String phone) { + repo.updatePhone(id, phone); + } + public Customer addCustomer(String name) { Customer myCustomer = new Customer(); myCustomer.name = name; diff --git a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java index dc9c8821ac..7eea3a87e9 100644 --- a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java +++ b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java @@ -25,11 +25,18 @@ public class PartialUpdateUnitTest { public void loadAndSave_whenUpdate_thenSuccess() { Customer myCustomer = service.addCustomer("John"); myCustomer = service.updateCustomer(myCustomer.id, "+00"); - assertEquals("+00", myCustomer.phone); } - @Test + @Test + public void customQuery_whenUpdate_thenSuccess() { + Customer myCustomer = service.addCustomer("John"); + service.updateCustomerWithCustomQuery(myCustomer.id, "+88"); + myCustomer = service.getCustomer(myCustomer.id); + assertEquals("+88", myCustomer.phone); + } + + @Test public void loadAndSaveWithMapper_whenUpdate_thenSuccess() { CustomerDto dto = new CustomerDto(new Customer()); dto.name = "Johnny"; @@ -38,19 +45,19 @@ public class PartialUpdateUnitTest { CustomerDto dto2 = new CustomerDto(entity.id); dto2.phone = "+44"; entity = service.updateCustomer(dto2); - + assertEquals("Johnny", entity.name); } - - @Test - public void loadAndSaveStructuredEntity_whenUpdate_thenSuccess() { + + @Test + public void loadAndSaveStructuredEntity_whenUpdate_thenSuccess() { CustomerStructured myCustomer = service.addCustomerStructured("John"); assertEquals(null, myCustomer.contactPhones); service.addCustomerPhone(myCustomer.id, "+44"); myCustomer = service.updateCustomerStructured(myCustomer.id, "Mr. John"); - + assertNotEquals(null, myCustomer.contactPhones); assertEquals(1, myCustomer.contactPhones.size()); - } + } } From 35822806087c704f6c3d3e7d743247285032b5d1 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Wed, 27 May 2020 22:25:08 +0430 Subject: [PATCH 48/74] Fixed the Datasource Assertion Issue --- .../SpringBootTomcatConnectionPoolIntegrationTest.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/tomcatconnectionpool/test/application/SpringBootTomcatConnectionPoolIntegrationTest.java b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/tomcatconnectionpool/test/application/SpringBootTomcatConnectionPoolIntegrationTest.java index 5400d76fbe..4422c27150 100644 --- a/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/tomcatconnectionpool/test/application/SpringBootTomcatConnectionPoolIntegrationTest.java +++ b/persistence-modules/spring-boot-persistence-2/src/test/java/com/baeldung/tomcatconnectionpool/test/application/SpringBootTomcatConnectionPoolIntegrationTest.java @@ -4,6 +4,7 @@ import javax.sql.DataSource; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringRunner; import com.baeldung.tomcatconnectionpool.application.SpringBootConsoleApplication; @@ -13,6 +14,7 @@ import org.springframework.boot.test.context.SpringBootTest; @RunWith(SpringRunner.class) @SpringBootTest(classes = {SpringBootConsoleApplication.class}) +@TestPropertySource(properties = "spring.datasource.type=org.apache.tomcat.jdbc.pool.DataSource") public class SpringBootTomcatConnectionPoolIntegrationTest { @Autowired From cd293d0cb634752400ec6398ba168707c006451c Mon Sep 17 00:00:00 2001 From: kwoyke Date: Wed, 27 May 2020 22:16:44 +0200 Subject: [PATCH 49/74] BAEL-3448: Change default http port (#9380) --- jee-kotlin/pom.xml | 1 + 1 file changed, 1 insertion(+) diff --git a/jee-kotlin/pom.xml b/jee-kotlin/pom.xml index 14c47a205e..45d5d8ece1 100644 --- a/jee-kotlin/pom.xml +++ b/jee-kotlin/pom.xml @@ -221,6 +221,7 @@ org.jboss.logmanager.LogManager ${project.basedir}/target/wildfly-${wildfly.version} + 8756 ${project.basedir}/target/wildfly-${wildfly.version}/modules false From 547a57c3c836f5293784999d71b2d0ab734d6c65 Mon Sep 17 00:00:00 2001 From: andrebrowne <42154231+andrebrowne@users.noreply.github.com> Date: Wed, 27 May 2020 20:42:38 -0400 Subject: [PATCH 50/74] BAEL-3341 Rename test dependent on database --- ...ateTypesIntegrationTest.java => HibernateTypesLiveTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename persistence-modules/hibernate-libraries/src/test/java/com/baeldung/hibernate/types/{HibernateTypesIntegrationTest.java => HibernateTypesLiveTest.java} (99%) diff --git a/persistence-modules/hibernate-libraries/src/test/java/com/baeldung/hibernate/types/HibernateTypesIntegrationTest.java b/persistence-modules/hibernate-libraries/src/test/java/com/baeldung/hibernate/types/HibernateTypesLiveTest.java similarity index 99% rename from persistence-modules/hibernate-libraries/src/test/java/com/baeldung/hibernate/types/HibernateTypesIntegrationTest.java rename to persistence-modules/hibernate-libraries/src/test/java/com/baeldung/hibernate/types/HibernateTypesLiveTest.java index 9d7479e77d..4b551386ad 100644 --- a/persistence-modules/hibernate-libraries/src/test/java/com/baeldung/hibernate/types/HibernateTypesIntegrationTest.java +++ b/persistence-modules/hibernate-libraries/src/test/java/com/baeldung/hibernate/types/HibernateTypesLiveTest.java @@ -13,7 +13,7 @@ import java.util.UUID; import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest -public class HibernateTypesIntegrationTest { +public class HibernateTypesLiveTest { @Autowired AlbumRepository albumRepository; From cde01486d750e118829b6e26de68758f858fd60c Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Thu, 28 May 2020 06:41:53 +0430 Subject: [PATCH 51/74] Changing to a More Stable Currency! --- .../currencies/CurrenciesControllerIntegrationTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/spring-thymeleaf-3/src/test/java/com/baeldung/thymeleaf/currencies/CurrenciesControllerIntegrationTest.java b/spring-thymeleaf-3/src/test/java/com/baeldung/thymeleaf/currencies/CurrenciesControllerIntegrationTest.java index 02bf8a9ee0..c1e3cf7458 100644 --- a/spring-thymeleaf-3/src/test/java/com/baeldung/thymeleaf/currencies/CurrenciesControllerIntegrationTest.java +++ b/spring-thymeleaf-3/src/test/java/com/baeldung/thymeleaf/currencies/CurrenciesControllerIntegrationTest.java @@ -27,7 +27,7 @@ public class CurrenciesControllerIntegrationTest { .header("Accept-Language", "es-ES") .param("amount", "10032.5")) .andExpect(status().isOk()) - .andExpect(content().string(containsString("10.032,50 €"))); + .andExpect(content().string(containsString("10.032,50"))); } @Test @@ -42,10 +42,10 @@ public class CurrenciesControllerIntegrationTest { @Test public void whenCallCurrencyWithRomanianLocaleWithArrays_ThenReturnLocaleCurrencies() throws Exception { mockMvc.perform(MockMvcRequestBuilders.get("/currency") - .header("Accept-Language", "ro-RO") + .header("Accept-Language", "en-GB") .param("amountList", "10", "20", "30")) .andExpect(status().isOk()) - .andExpect(content().string(containsString("10,00 RON, 20,00 RON, 30,00 RON"))); + .andExpect(content().string(containsString("£10.00, £20.00, £30.00"))); } @Test From 6fb0ebb95c81069a318ff7a017866dcef19d7efa Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Thu, 28 May 2020 20:00:23 +0430 Subject: [PATCH 52/74] Dependency Upgrade for HikariCP --- libraries-data-db/pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/libraries-data-db/pom.xml b/libraries-data-db/pom.xml index f028ffe8c3..d51580ccbc 100644 --- a/libraries-data-db/pom.xml +++ b/libraries-data-db/pom.xml @@ -211,7 +211,7 @@ 5.0.2 5.0.4 3.2.0-m7 - 2.7.2 + 3.4.5 11.22.4 From 6cd7bfba80b1ec9927099c93f2c85155fff17580 Mon Sep 17 00:00:00 2001 From: vatsalgosar Date: Thu, 28 May 2020 22:07:23 +0530 Subject: [PATCH 53/74] BAEL-3987 (#9139) * BAEL-3987 - Cast an int to an enum value * refactoring * code refactored * BAEL-3987 - Updated code snippets * bAEL-3987 - Fixed indentation --- .../com/baeldung/inttoenum/PizzaStatus.java | 36 +++++++++++++++++++ .../baeldung/inttoenum/IntToEnumUnitTest.java | 27 ++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 core-java-modules/core-java-lang-2/src/main/java/com/baeldung/inttoenum/PizzaStatus.java create mode 100644 core-java-modules/core-java-lang-2/src/test/java/com/baeldung/inttoenum/IntToEnumUnitTest.java diff --git a/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/inttoenum/PizzaStatus.java b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/inttoenum/PizzaStatus.java new file mode 100644 index 0000000000..8d7c626521 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/main/java/com/baeldung/inttoenum/PizzaStatus.java @@ -0,0 +1,36 @@ +package com.baeldung.inttoenum; + +import java.util.HashMap; +import java.util.Map; + +public enum PizzaStatus { + ORDERED(5), + READY(2), + DELIVERED(0); + + private int timeToDelivery; + + PizzaStatus(int timeToDelivery) { + this.timeToDelivery = timeToDelivery; + } + + public int getTimeToDelivery() { + return timeToDelivery; + } + + private static Map timeToDeliveryToEnumValuesMapping = new HashMap<>(); + + static { + PizzaStatus[] pizzaStatuses = PizzaStatus.values(); + for (int pizzaStatusIndex = 0; pizzaStatusIndex < pizzaStatuses.length; pizzaStatusIndex++) { + timeToDeliveryToEnumValuesMapping.put( + pizzaStatuses[pizzaStatusIndex].getTimeToDelivery(), + pizzaStatuses[pizzaStatusIndex] + ); + } + } + + public static PizzaStatus castIntToEnum(int timeToDelivery) { + return timeToDeliveryToEnumValuesMapping.get(timeToDelivery); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/inttoenum/IntToEnumUnitTest.java b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/inttoenum/IntToEnumUnitTest.java new file mode 100644 index 0000000000..876c230827 --- /dev/null +++ b/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/inttoenum/IntToEnumUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.inttoenum; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class IntToEnumUnitTest { + + @Test + public void whenIntToEnumUsingValuesMethod_thenReturnEnumObject() { + int timeToDeliveryForOrderedPizzaStatus = 5; + PizzaStatus[] pizzaStatuses = PizzaStatus.values(); + PizzaStatus pizzaOrderedStatus = null; + for (int pizzaStatusIndex = 0; pizzaStatusIndex < pizzaStatuses.length; pizzaStatusIndex++) { + if (pizzaStatuses[pizzaStatusIndex].getTimeToDelivery() == timeToDeliveryForOrderedPizzaStatus) { + pizzaOrderedStatus = pizzaStatuses[pizzaStatusIndex]; + } + } + assertEquals(pizzaOrderedStatus, PizzaStatus.ORDERED); + } + + @Test + public void whenIntToEnumUsingMap_thenReturnEnumObject() { + int timeToDeliveryForOrderedPizzaStatus = 5; + assertEquals(PizzaStatus.castIntToEnum(timeToDeliveryForOrderedPizzaStatus), PizzaStatus.ORDERED); + } +} \ No newline at end of file From 470d047e8dd21a4055e78d079666475075375296 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Fri, 29 May 2020 00:29:27 +0430 Subject: [PATCH 54/74] Introducing Constant Folding & Dead Code Elimination --- jmh/src/main/java/com/baeldung/BenchMark.java | 46 ++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/jmh/src/main/java/com/baeldung/BenchMark.java b/jmh/src/main/java/com/baeldung/BenchMark.java index b0e1caf4dc..55157848ab 100644 --- a/jmh/src/main/java/com/baeldung/BenchMark.java +++ b/jmh/src/main/java/com/baeldung/BenchMark.java @@ -1,14 +1,20 @@ package com.baeldung; -import com.google.common.hash.HashFunction; import com.google.common.hash.Hasher; import com.google.common.hash.Hashing; import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.infra.Blackhole; import java.nio.charset.Charset; +import java.util.concurrent.TimeUnit; public class BenchMark { + @State(Scope.Benchmark) + public static class Log { + public int x = 8; + } + @State(Scope.Benchmark) public static class ExecutionPlan { @@ -45,4 +51,42 @@ public class BenchMark { // Do nothing } + @Benchmark + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @BenchmarkMode(Mode.AverageTime) + public void doNothing() {} + + @Benchmark + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @BenchmarkMode(Mode.AverageTime) + public void objectCreation() { + new Object(); + } + + @Benchmark + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @BenchmarkMode(Mode.AverageTime) + public Object pillarsOfCreation() { + return new Object(); + } + + @Benchmark + @OutputTimeUnit(TimeUnit.NANOSECONDS) + @BenchmarkMode(Mode.AverageTime) + public void blackHole(Blackhole blackhole) { + blackhole.consume(new Object()); + } + + @Benchmark + public double foldedLog() { + int x = 8; + + return Math.log(x); + } + + @Benchmark + public double log(Log input) { + return Math.log(input.x); + } + } From 135c3160ac0016ecb0244a9c20b6f065fcab632a Mon Sep 17 00:00:00 2001 From: Mark Thomas Date: Thu, 28 May 2020 15:34:00 -0500 Subject: [PATCH 55/74] BAEL-3768 - spatialguru.net@gmail.com (#9385) * Add implementation of BeanPostProcessor and BeanFactoryPostProcessor for ticket * Continue processing beans if annotation not found on one or more * Add integration test * Simplify code --- spring-core-4/pom.xml | 24 ++++- .../postprocessor/GlobalEventBus.java | 39 +++++++++ ...GuavaEventBusBeanFactoryPostProcessor.java | 63 ++++++++++++++ .../GuavaEventBusBeanPostProcessor.java | 87 +++++++++++++++++++ .../baeldung/postprocessor/StockTrade.java | 34 ++++++++ .../postprocessor/StockTradeListener.java | 7 ++ .../postprocessor/StockTradePublisher.java | 36 ++++++++ .../baeldung/postprocessor/Subscriber.java | 21 +++++ .../PostProcessorConfiguration.java | 23 +++++ .../StockTradeIntegrationTest.java | 46 ++++++++++ 10 files changed, 379 insertions(+), 1 deletion(-) create mode 100644 spring-core-4/src/main/java/com/baeldung/postprocessor/GlobalEventBus.java create mode 100644 spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanFactoryPostProcessor.java create mode 100644 spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanPostProcessor.java create mode 100644 spring-core-4/src/main/java/com/baeldung/postprocessor/StockTrade.java create mode 100644 spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradeListener.java create mode 100644 spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradePublisher.java create mode 100644 spring-core-4/src/main/java/com/baeldung/postprocessor/Subscriber.java create mode 100644 spring-core-4/src/test/java/com/baeldung/postprocessor/PostProcessorConfiguration.java create mode 100644 spring-core-4/src/test/java/com/baeldung/postprocessor/StockTradeIntegrationTest.java diff --git a/spring-core-4/pom.xml b/spring-core-4/pom.xml index 53f7ca6912..fbec5ea9eb 100644 --- a/spring-core-4/pom.xml +++ b/spring-core-4/pom.xml @@ -24,6 +24,16 @@ spring-core ${spring.version} + + org.springframework + spring-expression + ${spring.version} + + + com.google.guava + guava + 28.2-jre + org.springframework spring-test @@ -42,6 +52,18 @@ ${junit-jupiter.version} test + + org.awaitility + awaitility + 4.0.2 + test + + + org.assertj + assertj-core + 2.9.1 + test + @@ -60,4 +82,4 @@ 2.2.2.RELEASE - \ No newline at end of file + diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/GlobalEventBus.java b/spring-core-4/src/main/java/com/baeldung/postprocessor/GlobalEventBus.java new file mode 100644 index 0000000000..8b95ea7c6f --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/postprocessor/GlobalEventBus.java @@ -0,0 +1,39 @@ +package com.baeldung.postprocessor; + +import com.google.common.eventbus.AsyncEventBus; +import com.google.common.eventbus.EventBus; + +import java.util.concurrent.Executors; + +@SuppressWarnings("ALL") +public final class GlobalEventBus { + + public static final String GLOBAL_EVENT_BUS_EXPRESSION = "T(com.baeldung.postprocessor.GlobalEventBus).getEventBus()"; + + private static final String IDENTIFIER = "global-event-bus"; + + private static final GlobalEventBus GLOBAL_EVENT_BUS = new GlobalEventBus(); + + private final EventBus eventBus = new AsyncEventBus(IDENTIFIER, Executors.newCachedThreadPool()); + + private GlobalEventBus() { + } + + public static GlobalEventBus getInstance() { + return GlobalEventBus.GLOBAL_EVENT_BUS; + } + + public static EventBus getEventBus() { + return GlobalEventBus.GLOBAL_EVENT_BUS.eventBus; + } + + public static void subscribe(Object obj) { + getEventBus().register(obj); + } + public static void unsubscribe(Object obj) { + getEventBus().unregister(obj); + } + public static void post(Object event) { + getEventBus().post(event); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanFactoryPostProcessor.java b/spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanFactoryPostProcessor.java new file mode 100644 index 0000000000..fba31fde6a --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanFactoryPostProcessor.java @@ -0,0 +1,63 @@ +package com.baeldung.postprocessor; + +import com.google.common.eventbus.EventBus; + +import java.util.Iterator; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.aop.framework.Advised; +import org.springframework.aop.support.AopUtils; +import org.springframework.beans.BeansException; +import org.springframework.beans.FatalBeanException; +import org.springframework.beans.factory.config.BeanFactoryPostProcessor; +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.expression.Expression; +import org.springframework.expression.ExpressionException; +import org.springframework.expression.spel.standard.SpelExpressionParser; + +@SuppressWarnings("ALL") +public class GuavaEventBusBeanFactoryPostProcessor implements BeanFactoryPostProcessor { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final SpelExpressionParser expressionParser = new SpelExpressionParser(); + + @Override + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { + for (Iterator names = beanFactory.getBeanNamesIterator(); names.hasNext(); ) { + Object proxy = this.getTargetObject(beanFactory.getBean(names.next())); + final Subscriber annotation = AnnotationUtils.getAnnotation(proxy.getClass(), Subscriber.class); + if (annotation == null) + continue; + this.logger.info("{}: processing bean of type {} during initialization", this.getClass().getSimpleName(), + proxy.getClass().getName()); + final String annotationValue = annotation.value(); + try { + final Expression expression = this.expressionParser.parseExpression(annotationValue); + final Object value = expression.getValue(); + if (!(value instanceof EventBus)) { + this.logger.error("{}: expression {} did not evaluate to an instance of EventBus for bean of type {}", + this.getClass().getSimpleName(), annotationValue, proxy.getClass().getSimpleName()); + return; + } + final EventBus eventBus = (EventBus)value; + eventBus.register(proxy); + } catch (ExpressionException ex) { + this.logger.error("{}: unable to parse/evaluate expression {} for bean of type {}", this.getClass().getSimpleName(), + annotationValue, proxy.getClass().getName()); + } + } + } + + private Object getTargetObject(Object proxy) throws BeansException { + if (AopUtils.isJdkDynamicProxy(proxy)) { + try { + return ((Advised)proxy).getTargetSource().getTarget(); + } catch (Exception e) { + throw new FatalBeanException("Error getting target of JDK proxy", e); + } + } + return proxy; + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanPostProcessor.java b/spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanPostProcessor.java new file mode 100644 index 0000000000..677c839444 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanPostProcessor.java @@ -0,0 +1,87 @@ +package com.baeldung.postprocessor; + +import com.google.common.eventbus.EventBus; + +import java.util.function.BiConsumer; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.aop.framework.Advised; +import org.springframework.aop.support.AopUtils; +import org.springframework.beans.BeansException; +import org.springframework.beans.FatalBeanException; +import org.springframework.beans.factory.config.DestructionAwareBeanPostProcessor; +import org.springframework.core.annotation.AnnotationUtils; +import org.springframework.expression.Expression; +import org.springframework.expression.ExpressionException; +import org.springframework.expression.spel.standard.SpelExpressionParser; + +/** + * A {@link DestructionAwareBeanPostProcessor} which registers/un-registers subscribers to a Guava {@link EventBus}. The class must + * be annotated with {@link Subscriber} and each subscribing method must be annotated with + * {@link com.google.common.eventbus.Subscribe}. + */ +@SuppressWarnings("ALL") +public class GuavaEventBusBeanPostProcessor implements DestructionAwareBeanPostProcessor { + + private final Logger logger = LoggerFactory.getLogger(this.getClass()); + private final SpelExpressionParser expressionParser = new SpelExpressionParser(); + + @Override + public void postProcessBeforeDestruction(final Object bean, final String beanName) throws BeansException { + this.process(bean, EventBus::unregister, "destruction"); + } + + @Override + public boolean requiresDestruction(Object bean) { + return true; + } + + @Override + public Object postProcessBeforeInitialization(final Object bean, final String beanName) throws BeansException { + return bean; + } + + @Override + public Object postProcessAfterInitialization(final Object bean, final String beanName) throws BeansException { + this.process(bean, EventBus::register, "initialization"); + return bean; + } + + private void process(final Object bean, final BiConsumer consumer, final String action) { + Object proxy = this.getTargetObject(bean); + final Subscriber annotation = AnnotationUtils.getAnnotation(proxy.getClass(), Subscriber.class); + if (annotation == null) + return; + this.logger.info("{}: processing bean of type {} during {}", this.getClass().getSimpleName(), proxy.getClass().getName(), + action); + final String annotationValue = annotation.value(); + try { + final Expression expression = this.expressionParser.parseExpression(annotationValue); + final Object value = expression.getValue(); + if (!(value instanceof EventBus)) { + this.logger.error("{}: expression {} did not evaluate to an instance of EventBus for bean of type {}", + this.getClass().getSimpleName(), annotationValue, proxy.getClass().getSimpleName()); + return; + } + final EventBus eventBus = (EventBus)value; + consumer.accept(eventBus, proxy); + } catch (ExpressionException ex) { + this.logger.error("{}: unable to parse/evaluate expression {} for bean of type {}", this.getClass().getSimpleName(), + annotationValue, proxy.getClass().getName()); + } + } + + private Object getTargetObject(Object proxy) throws BeansException { + if (AopUtils.isJdkDynamicProxy(proxy)) { + try { + return ((Advised)proxy).getTargetSource().getTarget(); + } catch (Exception e) { + throw new FatalBeanException("Error getting target of JDK proxy", e); + } + } + return proxy; + } +} + + diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTrade.java b/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTrade.java new file mode 100644 index 0000000000..7711cf7101 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTrade.java @@ -0,0 +1,34 @@ +package com.baeldung.postprocessor; + +import java.util.Date; + +public class StockTrade { + + private final String symbol; + private final int quantity; + private final double price; + private final Date tradeDate; + + public StockTrade(String symbol, int quantity, double price, Date tradeDate) { + this.symbol = symbol; + this.quantity = quantity; + this.price = price; + this.tradeDate = tradeDate; + } + + public String getSymbol() { + return this.symbol; + } + + public int getQuantity() { + return this.quantity; + } + + public double getPrice() { + return this.price; + } + + public Date getTradeDate() { + return this.tradeDate; + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradeListener.java b/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradeListener.java new file mode 100644 index 0000000000..bf34d66f24 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradeListener.java @@ -0,0 +1,7 @@ +package com.baeldung.postprocessor; + +@FunctionalInterface +public interface StockTradeListener { + + void stockTradePublished(StockTrade trade); +} diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradePublisher.java b/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradePublisher.java new file mode 100644 index 0000000000..bf339872d9 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradePublisher.java @@ -0,0 +1,36 @@ +package com.baeldung.postprocessor; + +import com.google.common.eventbus.AllowConcurrentEvents; +import com.google.common.eventbus.Subscribe; + +import java.util.HashSet; +import java.util.Set; + +@Subscriber +public class StockTradePublisher { + + private final Set stockTradeListeners = new HashSet<>(); + + public void addStockTradeListener(StockTradeListener listener) { + synchronized (this.stockTradeListeners) { + this.stockTradeListeners.add(listener); + } + } + + public void removeStockTradeListener(StockTradeListener listener) { + synchronized (this.stockTradeListeners) { + this.stockTradeListeners.remove(listener); + } + } + + @Subscribe + @AllowConcurrentEvents + private void handleNewStockTradeEvent(StockTrade trade) { + // publish to DB, send to PubNub, whatever you want here + final Set listeners; + synchronized (this.stockTradeListeners) { + listeners = new HashSet<>(this.stockTradeListeners); + } + listeners.forEach(li -> li.stockTradePublished(trade)); + } +} diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/Subscriber.java b/spring-core-4/src/main/java/com/baeldung/postprocessor/Subscriber.java new file mode 100644 index 0000000000..bef38333d6 --- /dev/null +++ b/spring-core-4/src/main/java/com/baeldung/postprocessor/Subscriber.java @@ -0,0 +1,21 @@ +package com.baeldung.postprocessor; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Inherited; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * An annotation which indicates which Guava {@link com.google.common.eventbus.EventBus} a Spring bean wishes to subscribe to. + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +@Inherited +public @interface Subscriber { + + /** + * A SpEL expression which selects the {@link com.google.common.eventbus.EventBus}. + */ + String value() default GlobalEventBus.GLOBAL_EVENT_BUS_EXPRESSION; +} diff --git a/spring-core-4/src/test/java/com/baeldung/postprocessor/PostProcessorConfiguration.java b/spring-core-4/src/test/java/com/baeldung/postprocessor/PostProcessorConfiguration.java new file mode 100644 index 0000000000..b28e36663a --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/postprocessor/PostProcessorConfiguration.java @@ -0,0 +1,23 @@ +package com.baeldung.postprocessor; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +public class PostProcessorConfiguration { + + @Bean + public GlobalEventBus eventBus() { + return GlobalEventBus.getInstance(); + } + + @Bean + public GuavaEventBusBeanPostProcessor eventBusBeanPostProcessor() { + return new GuavaEventBusBeanPostProcessor(); + } + + @Bean + public StockTradePublisher stockTradePublisher() { + return new StockTradePublisher(); + } +} diff --git a/spring-core-4/src/test/java/com/baeldung/postprocessor/StockTradeIntegrationTest.java b/spring-core-4/src/test/java/com/baeldung/postprocessor/StockTradeIntegrationTest.java new file mode 100644 index 0000000000..ae3cd968dc --- /dev/null +++ b/spring-core-4/src/test/java/com/baeldung/postprocessor/StockTradeIntegrationTest.java @@ -0,0 +1,46 @@ +package com.baeldung.postprocessor; + +import java.time.Duration; +import java.util.Date; +import java.util.Objects; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.awaitility.Awaitility.await; + +@RunWith(SpringJUnit4ClassRunner.class) +@ContextConfiguration(classes = {PostProcessorConfiguration.class}) +public class StockTradeIntegrationTest { + + @Autowired + private StockTradePublisher stockTradePublisher; + + @Test + public void givenValidConfig_whenTradePublished_thenTradeReceived() { + Date tradeDate = new Date(); + StockTrade stockTrade = new StockTrade("AMZN", 100, 2483.52d, tradeDate); + AtomicBoolean assertionsPassed = new AtomicBoolean(false); + StockTradeListener listener = trade -> assertionsPassed.set(this.verifyExact(stockTrade, trade)); + this.stockTradePublisher.addStockTradeListener(listener); + try { + GlobalEventBus.post(stockTrade); + await().atMost(Duration.ofSeconds(2L)) + .untilAsserted(() -> assertThat(assertionsPassed.get()).isTrue()); + } finally { + this.stockTradePublisher.removeStockTradeListener(listener); + } + } + + private boolean verifyExact(StockTrade stockTrade, StockTrade trade) { + return Objects.equals(stockTrade.getSymbol(), trade.getSymbol()) + && Objects.equals(stockTrade.getTradeDate(), trade.getTradeDate()) + && stockTrade.getQuantity() == trade.getQuantity() + && stockTrade.getPrice() == trade.getPrice(); + } +} From 6076d4da3b11817b868c6ea7d5769df9a5fa97c7 Mon Sep 17 00:00:00 2001 From: Carlos Cavero Date: Fri, 29 May 2020 00:02:11 +0200 Subject: [PATCH 56/74] BAEL-3756-YAML-for-Spring-DevOps (#9368) * Add modifications to include the configuration for YAML and DevOps * Clean the Dockerfile * Modify the name of testing environment and include YAML tests --- .../spring-boot-properties/.dockerignore | 13 ++++++++++ .../spring-boot-properties/Dockerfile | 10 ++++++++ .../spring-boot-properties/pom.xml | 3 ++- .../src/main/resources/application.yml | 19 +++++++++++++- .../baeldung/yaml/YAMLDevIntegrationTest.java | 25 +++++++++++++++++++ .../baeldung/yaml/YAMLIntegrationTest.java | 24 ++++++++++++++++++ 6 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 spring-boot-modules/spring-boot-properties/.dockerignore create mode 100644 spring-boot-modules/spring-boot-properties/Dockerfile create mode 100644 spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLDevIntegrationTest.java create mode 100644 spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLIntegrationTest.java diff --git a/spring-boot-modules/spring-boot-properties/.dockerignore b/spring-boot-modules/spring-boot-properties/.dockerignore new file mode 100644 index 0000000000..df36044e46 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/.dockerignore @@ -0,0 +1,13 @@ +# Logs +logs +*.log + +# Git +.git +.cache + +# Classes +**/*.class + +# Ignore md files +*.md diff --git a/spring-boot-modules/spring-boot-properties/Dockerfile b/spring-boot-modules/spring-boot-properties/Dockerfile new file mode 100644 index 0000000000..d6bd2a95ae --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/Dockerfile @@ -0,0 +1,10 @@ +FROM maven:3.6.0-jdk-11 +WORKDIR /code/spring-boot-modules/spring-boot-properties/ +COPY ./spring-boot-modules/spring-boot-properties/pom.xml . +COPY ./spring-boot-modules/spring-boot-properties/src ./src +COPY ./parent-boot-2/pom.xml /code/parent-boot-2/pom.xml +COPY ./pom.xml /code/pom.xml +COPY ./custom-pmd-0.0.1.jar /code/custom-pmd-0.0.1.jar +COPY ./baeldung-pmd-rules.xml /code/baeldung-pmd-rules.xml +RUN mvn dependency:resolve +CMD ["mvn", "spring-boot:run"] \ No newline at end of file diff --git a/spring-boot-modules/spring-boot-properties/pom.xml b/spring-boot-modules/spring-boot-properties/pom.xml index ef9c084f4c..98d328bd19 100644 --- a/spring-boot-modules/spring-boot-properties/pom.xml +++ b/spring-boot-modules/spring-boot-properties/pom.xml @@ -128,7 +128,8 @@ 4.4.11 @ 2.2.4.RELEASE - com.baeldung.buildproperties.Application + + com.baeldung.yaml.MyApplication diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/application.yml b/spring-boot-modules/spring-boot-properties/src/main/resources/application.yml index 6fc6f67cd0..4914ff15f7 100644 --- a/spring-boot-modules/spring-boot-properties/src/main/resources/application.yml +++ b/spring-boot-modules/spring-boot-properties/src/main/resources/application.yml @@ -1,7 +1,14 @@ +spring: + profiles: + active: + - test + +--- + spring: profiles: test name: test-YAML -environment: test +environment: testing servers: - www.abc.test.com - www.xyz.test.com @@ -15,3 +22,13 @@ environment: production servers: - www.abc.com - www.xyz.com + +--- + +spring: + profiles: dev +name: ${DEV_NAME:dev-YAML} +environment: development +servers: + - www.abc.dev.com + - www.xyz.dev.com diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLDevIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLDevIntegrationTest.java new file mode 100644 index 0000000000..8dfc4c2208 --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLDevIntegrationTest.java @@ -0,0 +1,25 @@ +package com.baeldung.yaml; + +import static org.junit.Assert.assertTrue; + +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = MyApplication.class) +@TestPropertySource(properties = {"spring.profiles.active = dev"}) +class YAMLDevIntegrationTest { + + @Autowired + private YAMLConfig config; + + @Test + void whenProfileTest_thenNameTesting() { + assertTrue("development".equalsIgnoreCase(config.getEnvironment())); + assertTrue("dev-YAML".equalsIgnoreCase(config.getName())); + } +} diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLIntegrationTest.java new file mode 100644 index 0000000000..090d5c592e --- /dev/null +++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLIntegrationTest.java @@ -0,0 +1,24 @@ +package com.baeldung.yaml; + +import static org.junit.Assert.assertTrue; + +import org.junit.jupiter.api.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.test.context.TestPropertySource; +import org.springframework.test.context.junit4.SpringRunner; + +@RunWith(SpringRunner.class) +@SpringBootTest(classes = MyApplication.class) +class YAMLIntegrationTest { + + @Autowired + private YAMLConfig config; + + @Test + void whenProfileTest_thenNameTesting() { + assertTrue("testing".equalsIgnoreCase(config.getEnvironment())); + assertTrue("test-YAML".equalsIgnoreCase(config.getName())); + } +} From 0986bbcd622c6fd284b674bf85b65223b0c328e9 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Sat, 30 May 2020 09:23:53 +0430 Subject: [PATCH 57/74] On the next line --- jmh/src/main/java/com/baeldung/BenchMark.java | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/jmh/src/main/java/com/baeldung/BenchMark.java b/jmh/src/main/java/com/baeldung/BenchMark.java index 55157848ab..3c5c840db2 100644 --- a/jmh/src/main/java/com/baeldung/BenchMark.java +++ b/jmh/src/main/java/com/baeldung/BenchMark.java @@ -54,7 +54,9 @@ public class BenchMark { @Benchmark @OutputTimeUnit(TimeUnit.NANOSECONDS) @BenchmarkMode(Mode.AverageTime) - public void doNothing() {} + public void doNothing() { + + } @Benchmark @OutputTimeUnit(TimeUnit.NANOSECONDS) From 99768749b416bd24ad4eb606d2e0ff43c5057dad Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Sat, 30 May 2020 13:56:37 +0800 Subject: [PATCH 58/74] Create README.md --- aws-app-sync/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 aws-app-sync/README.md diff --git a/aws-app-sync/README.md b/aws-app-sync/README.md new file mode 100644 index 0000000000..976a999f40 --- /dev/null +++ b/aws-app-sync/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [AWS AppSync With Spring Boot](https://www.baeldung.com/aws-appsync-spring) From 8fc7ced36e5ae5c1c8e95dcb87c1931d29e92e9d Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Sat, 30 May 2020 13:57:50 +0800 Subject: [PATCH 59/74] Update README.md --- language-interop/README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/language-interop/README.md b/language-interop/README.md index a28c4a5405..458f34a119 100644 --- a/language-interop/README.md +++ b/language-interop/README.md @@ -3,3 +3,5 @@ This module contains articles about Java interop with other language integrations. ### Relevant Articles: + +- [How to Call Python From Java](https://www.baeldung.com/java-working-with-python) From 2d8fc1c3a35ad36404bd307b32c835cc37d5e027 Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Sat, 30 May 2020 13:59:45 +0800 Subject: [PATCH 60/74] Create README.md --- netflix-modules/genie/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 netflix-modules/genie/README.md diff --git a/netflix-modules/genie/README.md b/netflix-modules/genie/README.md new file mode 100644 index 0000000000..f6e15ba403 --- /dev/null +++ b/netflix-modules/genie/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Introduction to Netflix Genie](https://www.baeldung.com/netflix-genie-intro) From 70081cb544f9a6f626d29e1355a2542b23f6078c Mon Sep 17 00:00:00 2001 From: johnA1331 <53036378+johnA1331@users.noreply.github.com> Date: Sat, 30 May 2020 14:01:14 +0800 Subject: [PATCH 61/74] Create README.md --- libraries-rpc/README.md | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 libraries-rpc/README.md diff --git a/libraries-rpc/README.md b/libraries-rpc/README.md new file mode 100644 index 0000000000..472aa883ad --- /dev/null +++ b/libraries-rpc/README.md @@ -0,0 +1,3 @@ +### Relevant Articles: + +- [Introduction to Finagle](https://www.baeldung.com/java-finagle) From 5a2bf2d09ff11800802ff6bbcdb762c4daf6a1e3 Mon Sep 17 00:00:00 2001 From: Mark Thomas Date: Sun, 31 May 2020 11:46:32 -0500 Subject: [PATCH 62/74] BAEL-3768 - spatialguru.net@gmail.com (#9402) * Add implementation of BeanPostProcessor and BeanFactoryPostProcessor for ticket * Continue processing beans if annotation not found on one or more * Add integration test * Simplify code * Rename package --- .../{postprocessor => beanpostprocessor}/GlobalEventBus.java | 2 +- .../GuavaEventBusBeanFactoryPostProcessor.java | 2 +- .../GuavaEventBusBeanPostProcessor.java | 2 +- .../{postprocessor => beanpostprocessor}/StockTrade.java | 2 +- .../StockTradeListener.java | 2 +- .../StockTradePublisher.java | 2 +- .../{postprocessor => beanpostprocessor}/Subscriber.java | 2 +- .../PostProcessorConfiguration.java | 2 +- .../StockTradeIntegrationTest.java | 2 +- 9 files changed, 9 insertions(+), 9 deletions(-) rename spring-core-4/src/main/java/com/baeldung/{postprocessor => beanpostprocessor}/GlobalEventBus.java (96%) rename spring-core-4/src/main/java/com/baeldung/{postprocessor => beanpostprocessor}/GuavaEventBusBeanFactoryPostProcessor.java (98%) rename spring-core-4/src/main/java/com/baeldung/{postprocessor => beanpostprocessor}/GuavaEventBusBeanPostProcessor.java (98%) rename spring-core-4/src/main/java/com/baeldung/{postprocessor => beanpostprocessor}/StockTrade.java (94%) rename spring-core-4/src/main/java/com/baeldung/{postprocessor => beanpostprocessor}/StockTradeListener.java (73%) rename spring-core-4/src/main/java/com/baeldung/{postprocessor => beanpostprocessor}/StockTradePublisher.java (96%) rename spring-core-4/src/main/java/com/baeldung/{postprocessor => beanpostprocessor}/Subscriber.java (93%) rename spring-core-4/src/test/java/com/baeldung/{postprocessor => beanpostprocessor}/PostProcessorConfiguration.java (92%) rename spring-core-4/src/test/java/com/baeldung/{postprocessor => beanpostprocessor}/StockTradeIntegrationTest.java (97%) diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/GlobalEventBus.java b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GlobalEventBus.java similarity index 96% rename from spring-core-4/src/main/java/com/baeldung/postprocessor/GlobalEventBus.java rename to spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GlobalEventBus.java index 8b95ea7c6f..8b3c528c4d 100644 --- a/spring-core-4/src/main/java/com/baeldung/postprocessor/GlobalEventBus.java +++ b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GlobalEventBus.java @@ -1,4 +1,4 @@ -package com.baeldung.postprocessor; +package com.baeldung.beanpostprocessor; import com.google.common.eventbus.AsyncEventBus; import com.google.common.eventbus.EventBus; diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanFactoryPostProcessor.java b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GuavaEventBusBeanFactoryPostProcessor.java similarity index 98% rename from spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanFactoryPostProcessor.java rename to spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GuavaEventBusBeanFactoryPostProcessor.java index fba31fde6a..e0108655cf 100644 --- a/spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanFactoryPostProcessor.java +++ b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GuavaEventBusBeanFactoryPostProcessor.java @@ -1,4 +1,4 @@ -package com.baeldung.postprocessor; +package com.baeldung.beanpostprocessor; import com.google.common.eventbus.EventBus; diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanPostProcessor.java b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GuavaEventBusBeanPostProcessor.java similarity index 98% rename from spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanPostProcessor.java rename to spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GuavaEventBusBeanPostProcessor.java index 677c839444..be3800c40a 100644 --- a/spring-core-4/src/main/java/com/baeldung/postprocessor/GuavaEventBusBeanPostProcessor.java +++ b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GuavaEventBusBeanPostProcessor.java @@ -1,4 +1,4 @@ -package com.baeldung.postprocessor; +package com.baeldung.beanpostprocessor; import com.google.common.eventbus.EventBus; diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTrade.java b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/StockTrade.java similarity index 94% rename from spring-core-4/src/main/java/com/baeldung/postprocessor/StockTrade.java rename to spring-core-4/src/main/java/com/baeldung/beanpostprocessor/StockTrade.java index 7711cf7101..f27f9e7c9b 100644 --- a/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTrade.java +++ b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/StockTrade.java @@ -1,4 +1,4 @@ -package com.baeldung.postprocessor; +package com.baeldung.beanpostprocessor; import java.util.Date; diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradeListener.java b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/StockTradeListener.java similarity index 73% rename from spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradeListener.java rename to spring-core-4/src/main/java/com/baeldung/beanpostprocessor/StockTradeListener.java index bf34d66f24..a0ee293293 100644 --- a/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradeListener.java +++ b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/StockTradeListener.java @@ -1,4 +1,4 @@ -package com.baeldung.postprocessor; +package com.baeldung.beanpostprocessor; @FunctionalInterface public interface StockTradeListener { diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradePublisher.java b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/StockTradePublisher.java similarity index 96% rename from spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradePublisher.java rename to spring-core-4/src/main/java/com/baeldung/beanpostprocessor/StockTradePublisher.java index bf339872d9..0058944b53 100644 --- a/spring-core-4/src/main/java/com/baeldung/postprocessor/StockTradePublisher.java +++ b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/StockTradePublisher.java @@ -1,4 +1,4 @@ -package com.baeldung.postprocessor; +package com.baeldung.beanpostprocessor; import com.google.common.eventbus.AllowConcurrentEvents; import com.google.common.eventbus.Subscribe; diff --git a/spring-core-4/src/main/java/com/baeldung/postprocessor/Subscriber.java b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/Subscriber.java similarity index 93% rename from spring-core-4/src/main/java/com/baeldung/postprocessor/Subscriber.java rename to spring-core-4/src/main/java/com/baeldung/beanpostprocessor/Subscriber.java index bef38333d6..1aca507555 100644 --- a/spring-core-4/src/main/java/com/baeldung/postprocessor/Subscriber.java +++ b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/Subscriber.java @@ -1,4 +1,4 @@ -package com.baeldung.postprocessor; +package com.baeldung.beanpostprocessor; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; diff --git a/spring-core-4/src/test/java/com/baeldung/postprocessor/PostProcessorConfiguration.java b/spring-core-4/src/test/java/com/baeldung/beanpostprocessor/PostProcessorConfiguration.java similarity index 92% rename from spring-core-4/src/test/java/com/baeldung/postprocessor/PostProcessorConfiguration.java rename to spring-core-4/src/test/java/com/baeldung/beanpostprocessor/PostProcessorConfiguration.java index b28e36663a..842283f563 100644 --- a/spring-core-4/src/test/java/com/baeldung/postprocessor/PostProcessorConfiguration.java +++ b/spring-core-4/src/test/java/com/baeldung/beanpostprocessor/PostProcessorConfiguration.java @@ -1,4 +1,4 @@ -package com.baeldung.postprocessor; +package com.baeldung.beanpostprocessor; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; diff --git a/spring-core-4/src/test/java/com/baeldung/postprocessor/StockTradeIntegrationTest.java b/spring-core-4/src/test/java/com/baeldung/beanpostprocessor/StockTradeIntegrationTest.java similarity index 97% rename from spring-core-4/src/test/java/com/baeldung/postprocessor/StockTradeIntegrationTest.java rename to spring-core-4/src/test/java/com/baeldung/beanpostprocessor/StockTradeIntegrationTest.java index ae3cd968dc..74d6765ecd 100644 --- a/spring-core-4/src/test/java/com/baeldung/postprocessor/StockTradeIntegrationTest.java +++ b/spring-core-4/src/test/java/com/baeldung/beanpostprocessor/StockTradeIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.postprocessor; +package com.baeldung.beanpostprocessor; import java.time.Duration; import java.util.Date; From 8477fd927913b5fceabdba4e367e54def66c4fae Mon Sep 17 00:00:00 2001 From: Michele Guarnaccia Date: Mon, 1 Jun 2020 11:09:35 +0200 Subject: [PATCH 63/74] Renamed test methods --- .../com/baeldung/partialupdate/PartialUpdateUnitTest.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java index 7eea3a87e9..874e18c4ad 100644 --- a/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java +++ b/persistence-modules/spring-data-jpa-5/src/test/java/com/baeldung/partialupdate/PartialUpdateUnitTest.java @@ -22,14 +22,14 @@ public class PartialUpdateUnitTest { CustomerService service; @Test - public void loadAndSave_whenUpdate_thenSuccess() { + public void givenCustomer_whenUpdate_thenSuccess() { Customer myCustomer = service.addCustomer("John"); myCustomer = service.updateCustomer(myCustomer.id, "+00"); assertEquals("+00", myCustomer.phone); } @Test - public void customQuery_whenUpdate_thenSuccess() { + public void givenCustomer_whenUpdateWithQuery_thenSuccess() { Customer myCustomer = service.addCustomer("John"); service.updateCustomerWithCustomQuery(myCustomer.id, "+88"); myCustomer = service.getCustomer(myCustomer.id); @@ -37,7 +37,7 @@ public class PartialUpdateUnitTest { } @Test - public void loadAndSaveWithMapper_whenUpdate_thenSuccess() { + public void givenCustomerDto_whenUpdateWithMapper_thenSuccess() { CustomerDto dto = new CustomerDto(new Customer()); dto.name = "Johnny"; Customer entity = service.addCustomer(dto); @@ -50,7 +50,7 @@ public class PartialUpdateUnitTest { } @Test - public void loadAndSaveStructuredEntity_whenUpdate_thenSuccess() { + public void givenCustomerStructured_whenUpdateCustomerPhone_thenSuccess() { CustomerStructured myCustomer = service.addCustomerStructured("John"); assertEquals(null, myCustomer.contactPhones); From ec34dde27b22dec40854966c9ca8550e3157f35e Mon Sep 17 00:00:00 2001 From: Juan Moreno Date: Mon, 1 Jun 2020 09:06:57 -0300 Subject: [PATCH 64/74] FIX Package Name --- .../reactive}/ReactiveStreamsUnitTest.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/{streams.reactive => streams/reactive}/ReactiveStreamsUnitTest.java (100%) diff --git a/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/streams.reactive/ReactiveStreamsUnitTest.java b/core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/streams/reactive/ReactiveStreamsUnitTest.java similarity index 100% rename from core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/streams.reactive/ReactiveStreamsUnitTest.java rename to core-java-modules/core-java-9-new-features/src/test/java/com/baeldung/java9/streams/reactive/ReactiveStreamsUnitTest.java From ef13de20619da164951d49ae04f1f555188b584e Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Mon, 1 Jun 2020 09:38:52 -0600 Subject: [PATCH 65/74] Update README.md Issue BAEL-3067 --- core-java-modules/core-java-io-conversions-2/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-io-conversions-2/README.md b/core-java-modules/core-java-io-conversions-2/README.md index 5cb9c21c54..9ce36e7437 100644 --- a/core-java-modules/core-java-io-conversions-2/README.md +++ b/core-java-modules/core-java-io-conversions-2/README.md @@ -5,4 +5,5 @@ This module contains articles about core Java input/output(IO) conversions. ### Relevant Articles: - [Java InputStream to String](https://www.baeldung.com/convert-input-stream-to-string) - [Java – Write an InputStream to a File](https://www.baeldung.com/convert-input-stream-to-a-file) +- [Converting a BufferedReader to a JSONObject](https://www.baeldung.com/java-bufferedreader-to-jsonobject) - More articles: [[<-- prev]](/core-java-modules/core-java-io-conversions) From 1627873f8810b14fe4ef772ae8e3f49478956c23 Mon Sep 17 00:00:00 2001 From: Josh Cummings Date: Mon, 1 Jun 2020 09:41:50 -0600 Subject: [PATCH 66/74] Update README.md Issue BAEL-3494 --- core-java-modules/core-java-9-new-features/README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/core-java-modules/core-java-9-new-features/README.md b/core-java-modules/core-java-9-new-features/README.md index d547b9a221..c2ef63a530 100644 --- a/core-java-modules/core-java-9-new-features/README.md +++ b/core-java-modules/core-java-9-new-features/README.md @@ -12,3 +12,4 @@ This module contains articles about core Java features that have been introduced - [Introduction to Java 9 StackWalking API](https://www.baeldung.com/java-9-stackwalking-api) - [Java 9 Platform Logging API](https://www.baeldung.com/java-9-logging-api) - [Java 9 Reactive Streams](https://www.baeldung.com/java-9-reactive-streams) +- [Multi-Release JAR Files with Maven](https://www.baeldung.com/maven-multi-release-jars) From 5b68d315b86a415178a6de8fcaac47ac0166bd3a Mon Sep 17 00:00:00 2001 From: Ankur Gupta Date: Mon, 1 Jun 2020 22:29:02 +0530 Subject: [PATCH 67/74] Updating Test Class name for multiplecachemanager module (#9399) * Java Example of Hexagonal Architecture * removing unnecessary maven files from pull request and spaces in code * Removing unnecessary lines * Example code for using Multiple Cache Manager in SpringBoot * BAEL-3963:Using multiple cache managers in Spring * removing new module created in last pull request * Fixes as per editor Suggestions * fixing spacing issue in pom.xml * Fixing spacing issue in pom.xml * Fixing spacing issue 2 * Formatting space issues in pom.xml * Changing the Test class name from IntegrationUnitTest to IntegrationTest Co-authored-by: Ankur Gupta --- ...onUnitTest.java => MultipleCacheManagerIntegrationTest.java} | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) rename spring-caching/src/test/java/com/baeldung/multiplecachemanager/{MultipleCacheManagerIntegrationUnitTest.java => MultipleCacheManagerIntegrationTest.java} (97%) diff --git a/spring-caching/src/test/java/com/baeldung/multiplecachemanager/MultipleCacheManagerIntegrationUnitTest.java b/spring-caching/src/test/java/com/baeldung/multiplecachemanager/MultipleCacheManagerIntegrationTest.java similarity index 97% rename from spring-caching/src/test/java/com/baeldung/multiplecachemanager/MultipleCacheManagerIntegrationUnitTest.java rename to spring-caching/src/test/java/com/baeldung/multiplecachemanager/MultipleCacheManagerIntegrationTest.java index e02e5da246..c83d4f9e96 100644 --- a/spring-caching/src/test/java/com/baeldung/multiplecachemanager/MultipleCacheManagerIntegrationUnitTest.java +++ b/spring-caching/src/test/java/com/baeldung/multiplecachemanager/MultipleCacheManagerIntegrationTest.java @@ -17,7 +17,7 @@ import com.baeldung.multiplecachemanager.repository.OrderDetailRepository; @SpringBootApplication @SpringBootTest -public class MultipleCacheManagerIntegrationUnitTest { +public class MultipleCacheManagerIntegrationTest { @MockBean private OrderDetailRepository orderDetailRepository; From 41a09bbf23c7bf1a7677dfc69bd51faf42df68a0 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Tue, 2 Jun 2020 15:46:57 +0530 Subject: [PATCH 68/74] JAVA-937: Migrate spring-cloud-bus to parent-boot-2 --- spring-cloud-bus/pom.xml | 6 +++--- spring-cloud-bus/spring-cloud-config-client/pom.xml | 5 +++++ .../src/main/resources/application.yml | 12 +++++++++++- .../src/main/resources/application.properties | 9 +++------ .../src/main/resources/bootstrap.properties | 4 ++++ 5 files changed, 26 insertions(+), 10 deletions(-) create mode 100644 spring-cloud-bus/spring-cloud-config-server/src/main/resources/bootstrap.properties diff --git a/spring-cloud-bus/pom.xml b/spring-cloud-bus/pom.xml index 513c8bade6..ec56e23ac7 100644 --- a/spring-cloud-bus/pom.xml +++ b/spring-cloud-bus/pom.xml @@ -11,9 +11,9 @@ com.baeldung - parent-boot-1 + parent-boot-2 0.0.1-SNAPSHOT - ../parent-boot-1 + ../parent-boot-2 @@ -34,7 +34,7 @@ - Brixton.SR7 + Hoxton.SR4 diff --git a/spring-cloud-bus/spring-cloud-config-client/pom.xml b/spring-cloud-bus/spring-cloud-config-client/pom.xml index 7e1185415b..cc1c237646 100644 --- a/spring-cloud-bus/spring-cloud-config-client/pom.xml +++ b/spring-cloud-bus/spring-cloud-config-client/pom.xml @@ -33,6 +33,11 @@ org.springframework.boot spring-boot-actuator + + + org.springframework.boot + spring-boot-actuator-autoconfigure + org.springframework.cloud diff --git a/spring-cloud-bus/spring-cloud-config-client/src/main/resources/application.yml b/spring-cloud-bus/spring-cloud-config-client/src/main/resources/application.yml index 547e0284f3..fbbc6d138f 100644 --- a/spring-cloud-bus/spring-cloud-config-client/src/main/resources/application.yml +++ b/spring-cloud-bus/spring-cloud-config-client/src/main/resources/application.yml @@ -4,4 +4,14 @@ spring: host: localhost port: 5672 username: guest - password: guest \ No newline at end of file + password: guest + cloud: + bus: + enabled: true + refresh: + enabled: true +management: + endpoints: + web: + exposure: + include: "*" \ No newline at end of file diff --git a/spring-cloud-bus/spring-cloud-config-server/src/main/resources/application.properties b/spring-cloud-bus/spring-cloud-config-server/src/main/resources/application.properties index 4c18c192c0..6d7a945612 100644 --- a/spring-cloud-bus/spring-cloud-config-server/src/main/resources/application.properties +++ b/spring-cloud-bus/spring-cloud-config-server/src/main/resources/application.properties @@ -1,11 +1,8 @@ server.port=8888 spring.cloud.config.server.git.uri= -security.user.name=root -security.user.password=s3cr3t -encrypt.key-store.location=classpath:/config-server.jks -encrypt.key-store.password=my-s70r3-s3cr3t -encrypt.key-store.alias=config-server-key -encrypt.key-store.secret=my-k34-s3cr3t +spring.cloud.bus.enabled=true +spring.security.user.name=root +spring.security.user.password=s3cr3t spring.rabbitmq.host=localhost spring.rabbitmq.port=5672 spring.rabbitmq.username=guest diff --git a/spring-cloud-bus/spring-cloud-config-server/src/main/resources/bootstrap.properties b/spring-cloud-bus/spring-cloud-config-server/src/main/resources/bootstrap.properties new file mode 100644 index 0000000000..b0c35c72a6 --- /dev/null +++ b/spring-cloud-bus/spring-cloud-config-server/src/main/resources/bootstrap.properties @@ -0,0 +1,4 @@ +encrypt.key-store.location=classpath:/config-server.jks +encrypt.key-store.password=my-s70r3-s3cr3t +encrypt.key-store.alias=config-server-key +encrypt.key-store.secret=my-k34-s3cr3t \ No newline at end of file From 4998662d45be50bbf79f84b6f14c52b638039bdc Mon Sep 17 00:00:00 2001 From: musibs Date: Tue, 2 Jun 2020 17:58:52 +0530 Subject: [PATCH 69/74] Moved the testcase to core-java-jvm module --- .../error/oom/ExecutorServiceUnitTest.java} | 28 +++++++++++++++---- 1 file changed, 22 insertions(+), 6 deletions(-) rename core-java-modules/{core-java-lang-2/src/test/java/com/baeldung/error/oom/TestExecutorService.java => core-java-jvm/src/test/java/com/baeldung/error/oom/ExecutorServiceUnitTest.java} (62%) diff --git a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/error/oom/TestExecutorService.java b/core-java-modules/core-java-jvm/src/test/java/com/baeldung/error/oom/ExecutorServiceUnitTest.java similarity index 62% rename from core-java-modules/core-java-lang-2/src/test/java/com/baeldung/error/oom/TestExecutorService.java rename to core-java-modules/core-java-jvm/src/test/java/com/baeldung/error/oom/ExecutorServiceUnitTest.java index d51faa055d..4e7dea370e 100644 --- a/core-java-modules/core-java-lang-2/src/test/java/com/baeldung/error/oom/TestExecutorService.java +++ b/core-java-modules/core-java-jvm/src/test/java/com/baeldung/error/oom/ExecutorServiceUnitTest.java @@ -12,12 +12,12 @@ import java.util.stream.IntStream; import org.junit.jupiter.api.Test; -public class TestExecutorService { +public class ExecutorServiceUnitTest { @Test public void givenAnExecutorService_WhenMoreTasksSubmitted_ThenAdditionalTasksWait() { - //Given + // Given int noOfThreads = 5; ExecutorService executorService = Executors.newFixedThreadPool(noOfThreads); @@ -29,11 +29,27 @@ public class TestExecutorService { } }; - //When + // When IntStream.rangeClosed(1, 10) .forEach(i -> executorService.submit(runnableTask)); - - //Then - assertThat(((ThreadPoolExecutor )executorService).getQueue().size(), is(equalTo(5))); + + // Then + assertThat(((ThreadPoolExecutor) executorService).getQueue() + .size(), is(equalTo(5))); + } + + @Test + public void givenAnExecutorService() throws Exception { + + while (true) { + TimeUnit.SECONDS.sleep(5); + new Thread(() -> { + try { + TimeUnit.HOURS.sleep(1); + } catch (InterruptedException e) { + e.printStackTrace(); + } + }).start(); + } } } From 6c6dbd6e96eacfeee56176bf2ad45ad5ad350b20 Mon Sep 17 00:00:00 2001 From: musibs Date: Tue, 2 Jun 2020 18:13:56 +0530 Subject: [PATCH 70/74] Removed the long running test case --- .../error/oom/ExecutorServiceUnitTest.java | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/core-java-modules/core-java-jvm/src/test/java/com/baeldung/error/oom/ExecutorServiceUnitTest.java b/core-java-modules/core-java-jvm/src/test/java/com/baeldung/error/oom/ExecutorServiceUnitTest.java index 4e7dea370e..47bb668727 100644 --- a/core-java-modules/core-java-jvm/src/test/java/com/baeldung/error/oom/ExecutorServiceUnitTest.java +++ b/core-java-modules/core-java-jvm/src/test/java/com/baeldung/error/oom/ExecutorServiceUnitTest.java @@ -37,19 +37,4 @@ public class ExecutorServiceUnitTest { assertThat(((ThreadPoolExecutor) executorService).getQueue() .size(), is(equalTo(5))); } - - @Test - public void givenAnExecutorService() throws Exception { - - while (true) { - TimeUnit.SECONDS.sleep(5); - new Thread(() -> { - try { - TimeUnit.HOURS.sleep(1); - } catch (InterruptedException e) { - e.printStackTrace(); - } - }).start(); - } - } } From 6dbd0c1d6cd61d81bf10413a09af83270eca69f2 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Tue, 2 Jun 2020 22:41:01 +0530 Subject: [PATCH 71/74] JAVA-932: Migrate spring-cloud-kubernetes to parent-boot-2 --- .../kubernetes-selfhealing/liveness-example/pom.xml | 9 ++++----- .../kubernetes-selfhealing/readiness-example/pom.xml | 9 ++++----- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/spring-cloud/spring-cloud-kubernetes/kubernetes-selfhealing/liveness-example/pom.xml b/spring-cloud/spring-cloud-kubernetes/kubernetes-selfhealing/liveness-example/pom.xml index 66d8f096ce..f0d34d2231 100644 --- a/spring-cloud/spring-cloud-kubernetes/kubernetes-selfhealing/liveness-example/pom.xml +++ b/spring-cloud/spring-cloud-kubernetes/kubernetes-selfhealing/liveness-example/pom.xml @@ -8,10 +8,10 @@ 1.0-SNAPSHOT - com.baeldung - parent-boot-1 - 0.0.1-SNAPSHOT - ../../../../parent-boot-1 + com.baeldung.spring.cloud + spring-cloud-kubernetes + 1.0-SNAPSHOT + ../../../spring-cloud-kubernetes @@ -44,7 +44,6 @@ UTF-8 UTF-8 - 1.5.17.RELEASE \ No newline at end of file diff --git a/spring-cloud/spring-cloud-kubernetes/kubernetes-selfhealing/readiness-example/pom.xml b/spring-cloud/spring-cloud-kubernetes/kubernetes-selfhealing/readiness-example/pom.xml index fbb9e09d07..8bfd4d305d 100644 --- a/spring-cloud/spring-cloud-kubernetes/kubernetes-selfhealing/readiness-example/pom.xml +++ b/spring-cloud/spring-cloud-kubernetes/kubernetes-selfhealing/readiness-example/pom.xml @@ -8,10 +8,10 @@ 1.0-SNAPSHOT - com.baeldung - parent-boot-1 - 0.0.1-SNAPSHOT - ../../../../parent-boot-1 + com.baeldung.spring.cloud + spring-cloud-kubernetes + 1.0-SNAPSHOT + ../../../spring-cloud-kubernetes @@ -44,7 +44,6 @@ UTF-8 UTF-8 - 1.5.17.RELEASE \ No newline at end of file From 2af1e30a8f70394d6914a4a2b43aa9b0b68ca78b Mon Sep 17 00:00:00 2001 From: Mona Mohamadinia Date: Tue, 2 Jun 2020 23:01:56 +0430 Subject: [PATCH 72/74] KTLN-141: Not-Null Assertion (!!) Operator in Kotlin (#9371) * Added the Test Samples * Renamed the Class --- .../nullassertion/NotNullAssertionUnitTest.kt | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 core-kotlin-modules/core-kotlin-2/src/test/kotlin/com/baeldung/nullassertion/NotNullAssertionUnitTest.kt diff --git a/core-kotlin-modules/core-kotlin-2/src/test/kotlin/com/baeldung/nullassertion/NotNullAssertionUnitTest.kt b/core-kotlin-modules/core-kotlin-2/src/test/kotlin/com/baeldung/nullassertion/NotNullAssertionUnitTest.kt new file mode 100644 index 0000000000..434f177927 --- /dev/null +++ b/core-kotlin-modules/core-kotlin-2/src/test/kotlin/com/baeldung/nullassertion/NotNullAssertionUnitTest.kt @@ -0,0 +1,20 @@ +package com.baeldung.nullassertion + +import org.junit.Test +import kotlin.test.assertEquals + +class NotNullAssertionUnitTest { + + @Test + fun givenNullableValue_WhenNotNull_ShouldExtractTheValue() { + val answer: String? = "42" + + assertEquals(42, answer!!.toInt()) + } + + @Test(expected = KotlinNullPointerException::class) + fun givenNullableValue_WhenIsNull_ThenShouldThrow() { + val noAnswer: String? = null + noAnswer!! + } +} From d59d2e3793d951853fea5b7e5ae67cd8c1f3f695 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Tue, 2 Jun 2020 21:04:09 +0200 Subject: [PATCH 73/74] BAEL-4171: Add SecurityAdviceTrait to the ExceptionTranslator (#9418) --- .../jhipster/gateway/web/rest/errors/ExceptionTranslator.java | 3 ++- .../jhipster/quotes/web/rest/errors/ExceptionTranslator.java | 3 ++- .../jhipster/uaa/web/rest/errors/ExceptionTranslator.java | 3 ++- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/jhipster/jhipster-uaa/gateway/src/main/java/com/baeldung/jhipster/gateway/web/rest/errors/ExceptionTranslator.java b/jhipster/jhipster-uaa/gateway/src/main/java/com/baeldung/jhipster/gateway/web/rest/errors/ExceptionTranslator.java index d5c9e577a6..fd6e04dadc 100644 --- a/jhipster/jhipster-uaa/gateway/src/main/java/com/baeldung/jhipster/gateway/web/rest/errors/ExceptionTranslator.java +++ b/jhipster/jhipster-uaa/gateway/src/main/java/com/baeldung/jhipster/gateway/web/rest/errors/ExceptionTranslator.java @@ -14,6 +14,7 @@ import org.zalando.problem.Problem; import org.zalando.problem.ProblemBuilder; import org.zalando.problem.Status; import org.zalando.problem.spring.web.advice.ProblemHandling; +import org.zalando.problem.spring.web.advice.security.SecurityAdviceTrait; import org.zalando.problem.violations.ConstraintViolationProblem; import javax.annotation.Nonnull; @@ -28,7 +29,7 @@ import java.util.stream.Collectors; * The error response follows RFC7807 - Problem Details for HTTP APIs (https://tools.ietf.org/html/rfc7807) */ @ControllerAdvice -public class ExceptionTranslator implements ProblemHandling { +public class ExceptionTranslator implements ProblemHandling, SecurityAdviceTrait { /** * Post-process the Problem payload to add the message key for the front-end if needed diff --git a/jhipster/jhipster-uaa/quotes/src/main/java/com/baeldung/jhipster/quotes/web/rest/errors/ExceptionTranslator.java b/jhipster/jhipster-uaa/quotes/src/main/java/com/baeldung/jhipster/quotes/web/rest/errors/ExceptionTranslator.java index 18baa42736..3bf4995405 100644 --- a/jhipster/jhipster-uaa/quotes/src/main/java/com/baeldung/jhipster/quotes/web/rest/errors/ExceptionTranslator.java +++ b/jhipster/jhipster-uaa/quotes/src/main/java/com/baeldung/jhipster/quotes/web/rest/errors/ExceptionTranslator.java @@ -14,6 +14,7 @@ import org.zalando.problem.Problem; import org.zalando.problem.ProblemBuilder; import org.zalando.problem.Status; import org.zalando.problem.spring.web.advice.ProblemHandling; +import org.zalando.problem.spring.web.advice.security.SecurityAdviceTrait; import org.zalando.problem.violations.ConstraintViolationProblem; import javax.annotation.Nonnull; @@ -28,7 +29,7 @@ import java.util.stream.Collectors; * The error response follows RFC7807 - Problem Details for HTTP APIs (https://tools.ietf.org/html/rfc7807) */ @ControllerAdvice -public class ExceptionTranslator implements ProblemHandling { +public class ExceptionTranslator implements ProblemHandling, SecurityAdviceTrait { /** * Post-process the Problem payload to add the message key for the front-end if needed diff --git a/jhipster/jhipster-uaa/uaa/src/main/java/com/baeldung/jhipster/uaa/web/rest/errors/ExceptionTranslator.java b/jhipster/jhipster-uaa/uaa/src/main/java/com/baeldung/jhipster/uaa/web/rest/errors/ExceptionTranslator.java index 320636c51d..6af9fd126c 100644 --- a/jhipster/jhipster-uaa/uaa/src/main/java/com/baeldung/jhipster/uaa/web/rest/errors/ExceptionTranslator.java +++ b/jhipster/jhipster-uaa/uaa/src/main/java/com/baeldung/jhipster/uaa/web/rest/errors/ExceptionTranslator.java @@ -14,6 +14,7 @@ import org.zalando.problem.Problem; import org.zalando.problem.ProblemBuilder; import org.zalando.problem.Status; import org.zalando.problem.spring.web.advice.ProblemHandling; +import org.zalando.problem.spring.web.advice.security.SecurityAdviceTrait; import org.zalando.problem.violations.ConstraintViolationProblem; import javax.annotation.Nonnull; @@ -28,7 +29,7 @@ import java.util.stream.Collectors; * The error response follows RFC7807 - Problem Details for HTTP APIs (https://tools.ietf.org/html/rfc7807) */ @ControllerAdvice -public class ExceptionTranslator implements ProblemHandling { +public class ExceptionTranslator implements ProblemHandling, SecurityAdviceTrait { /** * Post-process the Problem payload to add the message key for the front-end if needed From 9d4a0b1f516dbe591c1d62c7f404826c72ef7a4c Mon Sep 17 00:00:00 2001 From: kwoyke Date: Tue, 2 Jun 2020 21:15:49 +0200 Subject: [PATCH 74/74] BAEL-4100: Move HTTP/2 in Jetty to the new libraries-server-2 module (#9394) --- libraries-server-2/.gitignore | 9 ++ libraries-server-2/README.md | 8 ++ libraries-server-2/pom.xml | 77 ++++++++++++++++++ .../src/main/config/jetty.xml | 0 .../jetty/http2/Http2JettyServlet.java | 0 .../src/main/resources/keystore.jks | Bin .../src/main/resources/logback.xml | 13 +++ .../src/main/resources/truststore.jks | Bin .../src/main/webapp/WEB-INF/web.xml | 0 .../src/main/webapp/http2.html | 0 .../images/homepage-latest_articles.jpg | Bin .../images/homepage-rest_with_spring.jpg | Bin .../webapp/images/homepage-weekly_reviews.jpg | Bin .../src/main/webapp/index.html | 0 libraries-server/README.md | 2 +- libraries-server/pom.xml | 40 --------- pom.xml | 2 + 17 files changed, 110 insertions(+), 41 deletions(-) create mode 100644 libraries-server-2/.gitignore create mode 100644 libraries-server-2/README.md create mode 100644 libraries-server-2/pom.xml rename {libraries-server => libraries-server-2}/src/main/config/jetty.xml (100%) rename {libraries-server => libraries-server-2}/src/main/java/com/baeldung/jetty/http2/Http2JettyServlet.java (100%) rename {libraries-server => libraries-server-2}/src/main/resources/keystore.jks (100%) create mode 100644 libraries-server-2/src/main/resources/logback.xml rename {libraries-server => libraries-server-2}/src/main/resources/truststore.jks (100%) rename {libraries-server => libraries-server-2}/src/main/webapp/WEB-INF/web.xml (100%) rename {libraries-server => libraries-server-2}/src/main/webapp/http2.html (100%) rename {libraries-server => libraries-server-2}/src/main/webapp/images/homepage-latest_articles.jpg (100%) rename {libraries-server => libraries-server-2}/src/main/webapp/images/homepage-rest_with_spring.jpg (100%) rename {libraries-server => libraries-server-2}/src/main/webapp/images/homepage-weekly_reviews.jpg (100%) rename {libraries-server => libraries-server-2}/src/main/webapp/index.html (100%) diff --git a/libraries-server-2/.gitignore b/libraries-server-2/.gitignore new file mode 100644 index 0000000000..e594daf27a --- /dev/null +++ b/libraries-server-2/.gitignore @@ -0,0 +1,9 @@ +*.class + +# Folders # +/gensrc +/target + +# Packaged files # +*.jar +/bin/ diff --git a/libraries-server-2/README.md b/libraries-server-2/README.md new file mode 100644 index 0000000000..38166bcd77 --- /dev/null +++ b/libraries-server-2/README.md @@ -0,0 +1,8 @@ +## Server + +This module contains articles about server libraries. + +### Relevant Articles: + +- [HTTP/2 in Jetty](https://www.baeldung.com/jetty-http-2) +- More articles: [[<-- prev]](../libraries-server) diff --git a/libraries-server-2/pom.xml b/libraries-server-2/pom.xml new file mode 100644 index 0000000000..5f500a7ced --- /dev/null +++ b/libraries-server-2/pom.xml @@ -0,0 +1,77 @@ + + + 4.0.0 + libraries-server-2 + 0.0.1-SNAPSHOT + libraries-server-2 + war + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + + + + org.eclipse.jetty + jetty-server + ${jetty.version} + + + org.eclipse.jetty + jetty-servlet + ${jetty.version} + + + org.eclipse.jetty + jetty-webapp + ${jetty.version} + + + + + + + org.eclipse.jetty + jetty-maven-plugin + ${jetty.version} + + 8888 + quit + + -Xbootclasspath/p:${settings.localRepository}/org/mortbay/jetty/alpn/alpn-boot/${alpn.version}/alpn-boot-${alpn.version}.jar + + ${basedir}/src/main/config/jetty.xml + + / + + + + + org.eclipse.jetty.http2 + http2-server + ${jetty.version} + + + org.eclipse.jetty + jetty-alpn-openjdk8-server + ${jetty.version} + + + org.eclipse.jetty + jetty-servlets + ${jetty.version} + + + + + + + + 9.4.27.v20200227 + 8.1.11.v20170118 + + + \ No newline at end of file diff --git a/libraries-server/src/main/config/jetty.xml b/libraries-server-2/src/main/config/jetty.xml similarity index 100% rename from libraries-server/src/main/config/jetty.xml rename to libraries-server-2/src/main/config/jetty.xml diff --git a/libraries-server/src/main/java/com/baeldung/jetty/http2/Http2JettyServlet.java b/libraries-server-2/src/main/java/com/baeldung/jetty/http2/Http2JettyServlet.java similarity index 100% rename from libraries-server/src/main/java/com/baeldung/jetty/http2/Http2JettyServlet.java rename to libraries-server-2/src/main/java/com/baeldung/jetty/http2/Http2JettyServlet.java diff --git a/libraries-server/src/main/resources/keystore.jks b/libraries-server-2/src/main/resources/keystore.jks similarity index 100% rename from libraries-server/src/main/resources/keystore.jks rename to libraries-server-2/src/main/resources/keystore.jks diff --git a/libraries-server-2/src/main/resources/logback.xml b/libraries-server-2/src/main/resources/logback.xml new file mode 100644 index 0000000000..7d900d8ea8 --- /dev/null +++ b/libraries-server-2/src/main/resources/logback.xml @@ -0,0 +1,13 @@ + + + + + %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n + + + + + + + + \ No newline at end of file diff --git a/libraries-server/src/main/resources/truststore.jks b/libraries-server-2/src/main/resources/truststore.jks similarity index 100% rename from libraries-server/src/main/resources/truststore.jks rename to libraries-server-2/src/main/resources/truststore.jks diff --git a/libraries-server/src/main/webapp/WEB-INF/web.xml b/libraries-server-2/src/main/webapp/WEB-INF/web.xml similarity index 100% rename from libraries-server/src/main/webapp/WEB-INF/web.xml rename to libraries-server-2/src/main/webapp/WEB-INF/web.xml diff --git a/libraries-server/src/main/webapp/http2.html b/libraries-server-2/src/main/webapp/http2.html similarity index 100% rename from libraries-server/src/main/webapp/http2.html rename to libraries-server-2/src/main/webapp/http2.html diff --git a/libraries-server/src/main/webapp/images/homepage-latest_articles.jpg b/libraries-server-2/src/main/webapp/images/homepage-latest_articles.jpg similarity index 100% rename from libraries-server/src/main/webapp/images/homepage-latest_articles.jpg rename to libraries-server-2/src/main/webapp/images/homepage-latest_articles.jpg diff --git a/libraries-server/src/main/webapp/images/homepage-rest_with_spring.jpg b/libraries-server-2/src/main/webapp/images/homepage-rest_with_spring.jpg similarity index 100% rename from libraries-server/src/main/webapp/images/homepage-rest_with_spring.jpg rename to libraries-server-2/src/main/webapp/images/homepage-rest_with_spring.jpg diff --git a/libraries-server/src/main/webapp/images/homepage-weekly_reviews.jpg b/libraries-server-2/src/main/webapp/images/homepage-weekly_reviews.jpg similarity index 100% rename from libraries-server/src/main/webapp/images/homepage-weekly_reviews.jpg rename to libraries-server-2/src/main/webapp/images/homepage-weekly_reviews.jpg diff --git a/libraries-server/src/main/webapp/index.html b/libraries-server-2/src/main/webapp/index.html similarity index 100% rename from libraries-server/src/main/webapp/index.html rename to libraries-server-2/src/main/webapp/index.html diff --git a/libraries-server/README.md b/libraries-server/README.md index 7e41f33a0c..570806611f 100644 --- a/libraries-server/README.md +++ b/libraries-server/README.md @@ -13,4 +13,4 @@ This module contains articles about server libraries. - [MQTT Client in Java](https://www.baeldung.com/java-mqtt-client) - [Guide to XMPP Smack Client](https://www.baeldung.com/xmpp-smack-chat-client) - [A Guide to NanoHTTPD](https://www.baeldung.com/nanohttpd) -- [HTTP/2 in Jetty](https://www.baeldung.com/jetty-http-2) +- More articles: [[more -->]](../libraries-server-2) \ No newline at end of file diff --git a/libraries-server/pom.xml b/libraries-server/pom.xml index eb9cb61e56..d9546f1678 100644 --- a/libraries-server/pom.xml +++ b/libraries-server/pom.xml @@ -5,7 +5,6 @@ libraries-server 0.0.1-SNAPSHOT libraries-server - war com.baeldung @@ -107,50 +106,11 @@ - - - - org.eclipse.jetty - jetty-maven-plugin - ${jetty.version} - - 8888 - quit - - -Xbootclasspath/p:${settings.localRepository}/org/mortbay/jetty/alpn/alpn-boot/${alpn.version}/alpn-boot-${alpn.version}.jar - - ${basedir}/src/main/config/jetty.xml - - / - - - - - org.eclipse.jetty.http2 - http2-server - ${jetty.version} - - - org.eclipse.jetty - jetty-alpn-openjdk8-server - ${jetty.version} - - - org.eclipse.jetty - jetty-servlets - ${jetty.version} - - - - - - 3.6.2 4.5.3 9.4.27.v20200227 4.1.20.Final - 8.1.11.v20170118 8.5.24 4.3.1 1.2.0 diff --git a/pom.xml b/pom.xml index 2efea40c91..f797f1bbce 100644 --- a/pom.xml +++ b/pom.xml @@ -513,6 +513,7 @@ libraries-rpc libraries-security libraries-server + libraries-server-2 libraries-testing linkrest logging-modules @@ -1032,6 +1033,7 @@ libraries-primitive libraries-security libraries-server + libraries-server-2 libraries-testing linkrest logging-modules