From 211d1c98869a976f85caee93e06a83d94f4fdf0c Mon Sep 17 00:00:00 2001 From: akeshri Date: Sun, 28 Jun 2020 22:31:13 +0530 Subject: [PATCH 01/40] Create pom.xml --- hexagonal-architecture/pom.xml | 46 ++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 hexagonal-architecture/pom.xml diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml new file mode 100644 index 0000000000..87e599318c --- /dev/null +++ b/hexagonal-architecture/pom.xml @@ -0,0 +1,46 @@ + + 4.0.0 + + com.article + hexagonal-architecture + 0.0.1-SNAPSHOT + jar + + org.springframework.boot + spring-boot-starter-parent + 1.3.1.RELEASE + + + hexagonal-architecture + http://maven.apache.org + + + UTF-8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + + + org.springframework.boot + spring-boot-starter-test + test + + + com.fasterxml.jackson.dataformat + jackson-dataformat-csv + + + From 8b8554eb84e9d02e73affde7b302b0a87ccbf1b5 Mon Sep 17 00:00:00 2001 From: akeshri Date: Sun, 28 Jun 2020 22:32:39 +0530 Subject: [PATCH 02/40] Add files via upload --- .../baeldung/hexagonal/architecture/App.java | 18 ++++ .../hexagonal/architecture/ConsoleApp.java | 48 ++++++++++ .../controller/ProductController.java | 52 +++++++++++ .../architecture/dtos/ProductDto.java | 71 +++++++++++++++ .../hexagonal/architecture/model/Product.java | 87 +++++++++++++++++++ .../repository/ProductRepository.java | 14 +++ .../architecture/service/ProductService.java | 21 +++++ .../service/ProductServiceImpl.java | 44 ++++++++++ .../resources/application-batch.properties | 9 ++ .../resources/application-test.properties | 8 ++ .../src/main/resources/application.properties | 8 ++ .../src/main/resources/db/PRODUCT.sql | 9 ++ .../service/ProductServiceTest.java | 43 +++++++++ 13 files changed, 432 insertions(+) create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java create mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java create mode 100644 hexagonal-architecture/src/main/resources/application-batch.properties create mode 100644 hexagonal-architecture/src/main/resources/application-test.properties create mode 100644 hexagonal-architecture/src/main/resources/application.properties create mode 100644 hexagonal-architecture/src/main/resources/db/PRODUCT.sql create mode 100644 hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java new file mode 100644 index 0000000000..3563e3a0ec --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java @@ -0,0 +1,18 @@ +package com.baeldung.hexagonal.architecture; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author AshwiniKeshri + * + */ + +@SpringBootApplication +public class App +{ + public static void main( String[] args ) + { + SpringApplication.run(App.class, args); + } +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java new file mode 100644 index 0000000000..70edb8f9ed --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java @@ -0,0 +1,48 @@ +package com.baeldung.hexagonal.architecture; + +import java.io.File; +import java.util.List; + +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.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; +import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; + +import com.baeldung.hexagonal.architecture.model.Product; +import com.baeldung.hexagonal.architecture.service.ProductService; +import com.fasterxml.jackson.dataformat.csv.CsvMapper; +import com.fasterxml.jackson.dataformat.csv.CsvSchema; + +/** + * @author AshwiniKeshri + * + */ + +@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class,WebMvcAutoConfiguration.class}) +public class ConsoleApp implements CommandLineRunner { + @Autowired + private ProductService productService; + + public static void main(String[] args) { + SpringApplication.run(ConsoleApp.class, args); + } + + @Override + public void run(String... args) throws Exception { + String filePath = ""; + if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) + && (filePath = args[1]).length() > 0) { + File sourceFile = new File(filePath); + if (sourceFile.exists()) { + CsvMapper mapper = new CsvMapper(); + List products = mapper.readerFor(Product.class).with(CsvSchema.emptySchema().withHeader()) + .readValues(sourceFile).readAll(); + productService.saveAll(products); + } + + } + + } +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java new file mode 100644 index 0000000000..6645c379c2 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java @@ -0,0 +1,52 @@ +package com.baeldung.hexagonal.architecture.controller; + +import java.util.List; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import com.baeldung.hexagonal.architecture.dtos.ProductDto; +import com.baeldung.hexagonal.architecture.model.Product; +import com.baeldung.hexagonal.architecture.service.ProductService; + +/** + * @author AshwiniKeshri + * + */ + +@RestController +@RequestMapping("api/v1/product") +public class ProductController { + + @Autowired + private ProductService productService; + + @RequestMapping(value = "/all", method = RequestMethod.GET) + public List list() { + return productService.findAll().stream().map(p -> new ProductDto(p)).collect(Collectors.toList()); + } + + @RequestMapping(value = "/{productId}", method = RequestMethod.GET) + public ProductDto get(@PathVariable long productId) { + Product p = productService.findById(productId); + return p != null ? new ProductDto(p) : null; + } + + @RequestMapping(value = "/add", method = RequestMethod.POST) + public ProductDto create(@RequestBody ProductDto product) { + Product p = new Product(); + p.setDescription(product.getDescription()); + p.setName(product.getName()); + p.setPrice(product.getPrice()); + p.setQuantity(product.getQuantity()); + Long id = productService.create(p); + product.setId(id); + return product; + } + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java new file mode 100644 index 0000000000..336631fb10 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java @@ -0,0 +1,71 @@ +package com.baeldung.hexagonal.architecture.dtos; + +import com.baeldung.hexagonal.architecture.model.Product; +/** + * @author AshwiniKeshri + * + */ +public class ProductDto { + + private Long id; + + private String name; + + private Long quantity; + + private Double price; + + private String description; + + public ProductDto() {} + + public ProductDto(Product product) { + this.description = product.getDescription(); + this.id = product.getId(); + this.name = product.getName(); + this.price = product.getPrice(); + this.quantity = product.getQuantity(); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java new file mode 100644 index 0000000000..dec4548283 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java @@ -0,0 +1,87 @@ +/** + * + */ +package com.baeldung.hexagonal.architecture.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author AshwiniKeshri + * + */ + +@Entity +@Table(name = "PRODUCT") +public class Product implements Serializable{ + + /** + * + */ + private static final long serialVersionUID = 4000353732860709995L; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(name="NAME") + private String name; + + @Column(name = "QUANTITY") + private Long quantity; + + @Column(name = "PRICE") + private Double price; + + @Column(name = "DESCRIPTION") + private String description; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity > 0 ? quantity : 0; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price == null ? 0.0 : price; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java new file mode 100644 index 0000000000..76c888ab59 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java @@ -0,0 +1,14 @@ +package com.baeldung.hexagonal.architecture.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.baeldung.hexagonal.architecture.model.Product; + +/** + * @author AshwiniKeshri + * + */ + +public interface ProductRepository extends JpaRepository{ + +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java new file mode 100644 index 0000000000..b1d05a7db4 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java @@ -0,0 +1,21 @@ +package com.baeldung.hexagonal.architecture.service; + +import java.util.List; + +import com.baeldung.hexagonal.architecture.model.Product; + +/** + * @author AshwiniKeshri + * + */ + +public interface ProductService { + + List findAll(); + + Product findById(long id); + + Long create(Product product); + + void saveAll(List products); +} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java new file mode 100644 index 0000000000..1005b5753d --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java @@ -0,0 +1,44 @@ +package com.baeldung.hexagonal.architecture.service; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.baeldung.hexagonal.architecture.model.Product; +import com.baeldung.hexagonal.architecture.repository.ProductRepository; + +/** + * @author AshwiniKeshri + * + */ + +@Service +public class ProductServiceImpl implements ProductService { + + private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); + + @Autowired + private ProductRepository productRepository; + + public List findAll() { + return productRepository.findAll(); + } + + public Product findById(long id) { + return productRepository.findOne(id); + } + + public Long create(Product product) { + product = productRepository.saveAndFlush(product); + return product.getId(); + } + + @Override + public void saveAll(List products) { + productRepository.save(products); + } + +} diff --git a/hexagonal-architecture/src/main/resources/application-batch.properties b/hexagonal-architecture/src/main/resources/application-batch.properties new file mode 100644 index 0000000000..8c83d19f74 --- /dev/null +++ b/hexagonal-architecture/src/main/resources/application-batch.properties @@ -0,0 +1,9 @@ +spring.main.web-environment=false +spring.jpa.hibernate.ddl-auto=false; +spring.h2.console.enabled=true +spring.h2.console.path=/h2 + +spring.datasource.url=jdbc:h2:file:~/hexagonal +spring.datasource.username=sa +spring.datasource.password= +spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/hexagonal-architecture/src/main/resources/application-test.properties b/hexagonal-architecture/src/main/resources/application-test.properties new file mode 100644 index 0000000000..701313a878 --- /dev/null +++ b/hexagonal-architecture/src/main/resources/application-test.properties @@ -0,0 +1,8 @@ +spring.jpa.hibernate.ddl-auto=false; +spring.h2.console.enabled=true +spring.h2.console.path=/h2 + +spring.datasource.url=jdbc:h2:file:~/hexagonal_test +spring.datasource.username=sa +spring.datasource.password= +spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/hexagonal-architecture/src/main/resources/application.properties b/hexagonal-architecture/src/main/resources/application.properties new file mode 100644 index 0000000000..14c80a1af4 --- /dev/null +++ b/hexagonal-architecture/src/main/resources/application.properties @@ -0,0 +1,8 @@ +spring.jpa.hibernate.ddl-auto=false; +spring.h2.console.enabled=true +spring.h2.console.path=/h2 + +spring.datasource.url=jdbc:h2:file:~/hexagonal +spring.datasource.username=sa +spring.datasource.password= +spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/hexagonal-architecture/src/main/resources/db/PRODUCT.sql b/hexagonal-architecture/src/main/resources/db/PRODUCT.sql new file mode 100644 index 0000000000..a0fef3a710 --- /dev/null +++ b/hexagonal-architecture/src/main/resources/db/PRODUCT.sql @@ -0,0 +1,9 @@ +CREATE TABLE PRODUCT( + ID INT AUTO_INCREMENT, + NAME VARCHAR(255), + QUANTITY INTEGER, + PRICE DOUBLE, + DESCRIPTION VARCHAR(1000), +); + +INSERT INTO PRODUCT(NAME,QUANTITY,PRICE,DESCRIPTION) VALUES ('iPhone 11 Pro',10,300,'First triple camera system'); \ No newline at end of file diff --git a/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java b/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java new file mode 100644 index 0000000000..021fdf1289 --- /dev/null +++ b/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java @@ -0,0 +1,43 @@ +/** + * + */ +package com.baeldung.hexagonal.architecture.service; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.PropertySources; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.baeldung.hexagonal.architecture.App; +import com.baeldung.hexagonal.architecture.model.Product; +import com.baeldung.hexagonal.architecture.service.ProductService; + +/** + * @author AshwiniKeshri + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(App.class) +@ActiveProfiles(value = "test") +public class ProductServiceTest { + + @Autowired + private ProductService productService; + + @Test + public void testCreateProduct() { + Product product = new Product(); + product.setDescription("test product"); + product.setName("Product1"); + product.setPrice(10.0); + product.setQuantity(100l); + Long id = productService.create(product); + Assert.assertTrue(id >0); + } + +} From 6a1e528bfd049506a31125476268f47ea74f67be Mon Sep 17 00:00:00 2001 From: akeshri Date: Sun, 28 Jun 2020 22:51:35 +0530 Subject: [PATCH 03/40] Create README.md Read me changes --- hexagonal-architecture/README.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 hexagonal-architecture/README.md diff --git a/hexagonal-architecture/README.md b/hexagonal-architecture/README.md new file mode 100644 index 0000000000..4dd10368e0 --- /dev/null +++ b/hexagonal-architecture/README.md @@ -0,0 +1,17 @@ +# Hexagonal Architecture +A quick and practical example of Hexagonal Architecture using Spring boot. + +This application is using h2 database,which can be accessible http:/localhost:8080/h2 + +Main Application schema : hexagonal + +Test Application Schema : hexagonal_test + +1. Rest Api : execute [App](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java) + - Get All products : http://localhost:8080/api/v1/product/all + - Get product by id : http://localhost:8080/api/v1/product/{productId} + - Add a product : http://localhost:8080/api/v1/product/add + For more detail refer [ProductController](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java) + +2. Batch processing : We need to configure active profile as batch i.e. -Dspring.profiles.active=batch and execute [ConsoleApp](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java) +3. Test case : [ProductServiceTest](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java) From 36d37894420227c3e56ff244822b239c268b6db0 Mon Sep 17 00:00:00 2001 From: akeshri Date: Fri, 3 Jul 2020 22:32:44 +0530 Subject: [PATCH 04/40] 1.fixed code format 2. resolved build issue --- hexagonal-architecture/pom.xml | 2 + .../article/hexagonal/architecture/App.java | 16 ++++ .../hexagonal/architecture/ConsoleApp.java | 50 +++++++++++ .../controller/ProductController.java | 54 ++++++++++++ .../architecture/dtos/ProductDto.java | 72 ++++++++++++++++ .../hexagonal/architecture/model/Product.java | 85 +++++++++++++++++++ .../repository/ProductRepository.java | 14 +++ .../architecture/service/ProductService.java | 21 +++++ .../service/ProductServiceImpl.java | 44 ++++++++++ .../service/ProductServiceTest.java | 43 ++++++++++ 10 files changed, 401 insertions(+) create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java create mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java create mode 100644 hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml index 87e599318c..d014617639 100644 --- a/hexagonal-architecture/pom.xml +++ b/hexagonal-architecture/pom.xml @@ -18,6 +18,8 @@ UTF-8 + 1.8 + 1.8 diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java new file mode 100644 index 0000000000..ebc661bfdb --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java @@ -0,0 +1,16 @@ +package com.article.hexagonal.architecture; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author AshwiniKeshri + * + */ + +@SpringBootApplication +public class App { + public static void main(String[] args) { + SpringApplication.run(App.class, args); + } +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java new file mode 100644 index 0000000000..0024438737 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java @@ -0,0 +1,50 @@ +package com.article.hexagonal.architecture; + +import java.io.File; +import java.util.List; + +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.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; +import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; + +import com.article.hexagonal.architecture.model.Product; +import com.article.hexagonal.architecture.service.ProductService; +import com.fasterxml.jackson.dataformat.csv.CsvMapper; +import com.fasterxml.jackson.dataformat.csv.CsvSchema; + +/** + * @author AshwiniKeshri + * + */ + +@SpringBootApplication(exclude = { EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class }) +public class ConsoleApp implements CommandLineRunner { + @Autowired + private ProductService productService; + + public static void main(String[] args) { + SpringApplication.run(ConsoleApp.class, args); + } + + @Override + public void run(String... args) throws Exception { + String filePath = ""; + if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) && (filePath = args[1]).length() > 0) { + File sourceFile = new File(filePath); + if (sourceFile.exists()) { + CsvMapper mapper = new CsvMapper(); + List products = mapper.readerFor(Product.class) + .with(CsvSchema.emptySchema() + .withHeader()) + . readValues(sourceFile) + .readAll(); + productService.saveAll(products); + } + + } + + } +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java new file mode 100644 index 0000000000..66372980d0 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java @@ -0,0 +1,54 @@ +package com.article.hexagonal.architecture.controller; + +import java.util.List; +import java.util.stream.Collectors; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.RestController; + +import com.article.hexagonal.architecture.dtos.ProductDto; +import com.article.hexagonal.architecture.model.Product; +import com.article.hexagonal.architecture.service.ProductService; + +/** + * @author AshwiniKeshri + * + */ + +@RestController +@RequestMapping("api/v1/product") +public class ProductController { + + @Autowired + private ProductService productService; + + @RequestMapping(value = "/all", method = RequestMethod.GET) + public List list() { + productService.findAll().stream().map(x->x.getDescription()) + return null; + //return productService.findAll().stream().map(p -> new ProductDto(p)).collect(Collectors.toList()); + } + + @RequestMapping(value = "/{productId}", method = RequestMethod.GET) + public ProductDto get(@PathVariable long productId) { + Product p = productService.findById(productId); + return p != null ? new ProductDto(p) : null; + } + + @RequestMapping(value = "/add", method = RequestMethod.POST) + public ProductDto create(@RequestBody ProductDto product) { + Product p = new Product(); + p.setDescription(product.getDescription()); + p.setName(product.getName()); + p.setPrice(product.getPrice()); + p.setQuantity(product.getQuantity()); + Long id = productService.create(p); + product.setId(id); + return product; + } + +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java new file mode 100644 index 0000000000..209ae69b0a --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java @@ -0,0 +1,72 @@ +package com.article.hexagonal.architecture.dtos; + +import com.article.hexagonal.architecture.model.Product; + +/** + * @author AshwiniKeshri + * + */ +public class ProductDto { + + private Long id; + + private String name; + + private Long quantity; + + private Double price; + + private String description; + + public ProductDto() { + } + + public ProductDto(Product product) { + this.description = product.getDescription(); + this.id = product.getId(); + this.name = product.getName(); + this.price = product.getPrice(); + this.quantity = product.getQuantity(); + } + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java new file mode 100644 index 0000000000..f0f95d4d11 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java @@ -0,0 +1,85 @@ +/** + * + */ +package com.article.hexagonal.architecture.model; + +import java.io.Serializable; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.GeneratedValue; +import javax.persistence.GenerationType; +import javax.persistence.Id; +import javax.persistence.Table; + +/** + * @author AshwiniKeshri + * + */ + +@Entity +@Table(name = "PRODUCT") +public class Product implements Serializable { + + /** + * + */ + private static final long serialVersionUID = 4000353732860709995L; + + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; + + @Column(name = "NAME") + private String name; + + @Column(name = "QUANTITY") + private Long quantity; + + @Column(name = "PRICE") + private Double price; + + @Column(name = "DESCRIPTION") + private String description; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity > 0 ? quantity : 0; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price == null ? 0.0 : price; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java new file mode 100644 index 0000000000..fec151780c --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java @@ -0,0 +1,14 @@ +package com.article.hexagonal.architecture.repository; + +import org.springframework.data.jpa.repository.JpaRepository; + +import com.article.hexagonal.architecture.model.Product; + +/** + * @author AshwiniKeshri + * + */ + +public interface ProductRepository extends JpaRepository { + +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java new file mode 100644 index 0000000000..5ed1e7de96 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java @@ -0,0 +1,21 @@ +package com.article.hexagonal.architecture.service; + +import java.util.List; + +import com.article.hexagonal.architecture.model.Product; + +/** + * @author AshwiniKeshri + * + */ + +public interface ProductService { + + List findAll(); + + Product findById(long id); + + Long create(Product product); + + void saveAll(List products); +} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java new file mode 100644 index 0000000000..ccd1599392 --- /dev/null +++ b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java @@ -0,0 +1,44 @@ +package com.article.hexagonal.architecture.service; + +import java.util.List; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.stereotype.Service; + +import com.article.hexagonal.architecture.model.Product; +import com.article.hexagonal.architecture.repository.ProductRepository; + +/** + * @author AshwiniKeshri + * + */ + +@Service +public class ProductServiceImpl implements ProductService { + + private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); + + @Autowired + private ProductRepository productRepository; + + public List findAll() { + return productRepository.findAll(); + } + + public Product findById(long id) { + return productRepository.findOne(id); + } + + public Long create(Product product) { + product = productRepository.saveAndFlush(product); + return product.getId(); + } + + @Override + public void saveAll(List products) { + productRepository.save(products); + } + +} diff --git a/hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java b/hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java new file mode 100644 index 0000000000..6635fef2da --- /dev/null +++ b/hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java @@ -0,0 +1,43 @@ +/** + * + */ +package com.article.hexagonal.architecture.service; + +import org.junit.Assert; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.SpringApplicationConfiguration; +import org.springframework.context.annotation.PropertySource; +import org.springframework.context.annotation.PropertySources; +import org.springframework.test.context.ActiveProfiles; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +import com.article.hexagonal.architecture.App; +import com.article.hexagonal.architecture.model.Product; +import com.article.hexagonal.architecture.service.ProductService; + +/** + * @author AshwiniKeshri + * + */ +@RunWith(SpringJUnit4ClassRunner.class) +@SpringApplicationConfiguration(App.class) +@ActiveProfiles(value = "test") +public class ProductServiceTest { + + @Autowired + private ProductService productService; + + @Test + public void testCreateProduct() { + Product product = new Product(); + product.setDescription("test product"); + product.setName("Product1"); + product.setPrice(10.0); + product.setQuantity(100l); + Long id = productService.create(product); + Assert.assertTrue(id > 0); + } + +} From 18894f5c03049ea18703dda029140227f95e45aa Mon Sep 17 00:00:00 2001 From: akeshri Date: Sat, 4 Jul 2020 00:10:07 +0530 Subject: [PATCH 05/40] remove duplicate code --- .../article/hexagonal/architecture/App.java | 16 ---- .../hexagonal/architecture/ConsoleApp.java | 50 ----------- .../controller/ProductController.java | 54 ------------ .../architecture/dtos/ProductDto.java | 72 ---------------- .../hexagonal/architecture/model/Product.java | 85 ------------------- .../repository/ProductRepository.java | 14 --- .../architecture/service/ProductService.java | 21 ----- .../service/ProductServiceImpl.java | 44 ---------- .../service/ProductServiceTest.java | 43 ---------- 9 files changed, 399 deletions(-) delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java delete mode 100644 hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java delete mode 100644 hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java deleted file mode 100644 index ebc661bfdb..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/App.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.article.hexagonal.architecture; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -/** - * @author AshwiniKeshri - * - */ - -@SpringBootApplication -public class App { - public static void main(String[] args) { - SpringApplication.run(App.class, args); - } -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java deleted file mode 100644 index 0024438737..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/ConsoleApp.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.article.hexagonal.architecture; - -import java.io.File; -import java.util.List; - -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.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; -import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; - -import com.article.hexagonal.architecture.model.Product; -import com.article.hexagonal.architecture.service.ProductService; -import com.fasterxml.jackson.dataformat.csv.CsvMapper; -import com.fasterxml.jackson.dataformat.csv.CsvSchema; - -/** - * @author AshwiniKeshri - * - */ - -@SpringBootApplication(exclude = { EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class }) -public class ConsoleApp implements CommandLineRunner { - @Autowired - private ProductService productService; - - public static void main(String[] args) { - SpringApplication.run(ConsoleApp.class, args); - } - - @Override - public void run(String... args) throws Exception { - String filePath = ""; - if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) && (filePath = args[1]).length() > 0) { - File sourceFile = new File(filePath); - if (sourceFile.exists()) { - CsvMapper mapper = new CsvMapper(); - List products = mapper.readerFor(Product.class) - .with(CsvSchema.emptySchema() - .withHeader()) - . readValues(sourceFile) - .readAll(); - productService.saveAll(products); - } - - } - - } -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java deleted file mode 100644 index 66372980d0..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/controller/ProductController.java +++ /dev/null @@ -1,54 +0,0 @@ -package com.article.hexagonal.architecture.controller; - -import java.util.List; -import java.util.stream.Collectors; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -import com.article.hexagonal.architecture.dtos.ProductDto; -import com.article.hexagonal.architecture.model.Product; -import com.article.hexagonal.architecture.service.ProductService; - -/** - * @author AshwiniKeshri - * - */ - -@RestController -@RequestMapping("api/v1/product") -public class ProductController { - - @Autowired - private ProductService productService; - - @RequestMapping(value = "/all", method = RequestMethod.GET) - public List list() { - productService.findAll().stream().map(x->x.getDescription()) - return null; - //return productService.findAll().stream().map(p -> new ProductDto(p)).collect(Collectors.toList()); - } - - @RequestMapping(value = "/{productId}", method = RequestMethod.GET) - public ProductDto get(@PathVariable long productId) { - Product p = productService.findById(productId); - return p != null ? new ProductDto(p) : null; - } - - @RequestMapping(value = "/add", method = RequestMethod.POST) - public ProductDto create(@RequestBody ProductDto product) { - Product p = new Product(); - p.setDescription(product.getDescription()); - p.setName(product.getName()); - p.setPrice(product.getPrice()); - p.setQuantity(product.getQuantity()); - Long id = productService.create(p); - product.setId(id); - return product; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java deleted file mode 100644 index 209ae69b0a..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/dtos/ProductDto.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.article.hexagonal.architecture.dtos; - -import com.article.hexagonal.architecture.model.Product; - -/** - * @author AshwiniKeshri - * - */ -public class ProductDto { - - private Long id; - - private String name; - - private Long quantity; - - private Double price; - - private String description; - - public ProductDto() { - } - - public ProductDto(Product product) { - this.description = product.getDescription(); - this.id = product.getId(); - this.name = product.getName(); - this.price = product.getPrice(); - this.quantity = product.getQuantity(); - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Long getQuantity() { - return quantity; - } - - public void setQuantity(Long quantity) { - this.quantity = quantity; - } - - public Double getPrice() { - return price; - } - - public void setPrice(Double price) { - this.price = price; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java deleted file mode 100644 index f0f95d4d11..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/model/Product.java +++ /dev/null @@ -1,85 +0,0 @@ -/** - * - */ -package com.article.hexagonal.architecture.model; - -import java.io.Serializable; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; - -/** - * @author AshwiniKeshri - * - */ - -@Entity -@Table(name = "PRODUCT") -public class Product implements Serializable { - - /** - * - */ - private static final long serialVersionUID = 4000353732860709995L; - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; - - @Column(name = "NAME") - private String name; - - @Column(name = "QUANTITY") - private Long quantity; - - @Column(name = "PRICE") - private Double price; - - @Column(name = "DESCRIPTION") - private String description; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Long getQuantity() { - return quantity; - } - - public void setQuantity(Long quantity) { - this.quantity = quantity > 0 ? quantity : 0; - } - - public Double getPrice() { - return price; - } - - public void setPrice(Double price) { - this.price = price == null ? 0.0 : price; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java deleted file mode 100644 index fec151780c..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/repository/ProductRepository.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.article.hexagonal.architecture.repository; - -import org.springframework.data.jpa.repository.JpaRepository; - -import com.article.hexagonal.architecture.model.Product; - -/** - * @author AshwiniKeshri - * - */ - -public interface ProductRepository extends JpaRepository { - -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java deleted file mode 100644 index 5ed1e7de96..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductService.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.article.hexagonal.architecture.service; - -import java.util.List; - -import com.article.hexagonal.architecture.model.Product; - -/** - * @author AshwiniKeshri - * - */ - -public interface ProductService { - - List findAll(); - - Product findById(long id); - - Long create(Product product); - - void saveAll(List products); -} diff --git a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java b/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java deleted file mode 100644 index ccd1599392..0000000000 --- a/hexagonal-architecture/src/main/java/com/article/hexagonal/architecture/service/ProductServiceImpl.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.article.hexagonal.architecture.service; - -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.article.hexagonal.architecture.model.Product; -import com.article.hexagonal.architecture.repository.ProductRepository; - -/** - * @author AshwiniKeshri - * - */ - -@Service -public class ProductServiceImpl implements ProductService { - - private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); - - @Autowired - private ProductRepository productRepository; - - public List findAll() { - return productRepository.findAll(); - } - - public Product findById(long id) { - return productRepository.findOne(id); - } - - public Long create(Product product) { - product = productRepository.saveAndFlush(product); - return product.getId(); - } - - @Override - public void saveAll(List products) { - productRepository.save(products); - } - -} diff --git a/hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java b/hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java deleted file mode 100644 index 6635fef2da..0000000000 --- a/hexagonal-architecture/src/test/java/com/article/hexagonal/architecture/service/ProductServiceTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * - */ -package com.article.hexagonal.architecture.service; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.context.annotation.PropertySources; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.article.hexagonal.architecture.App; -import com.article.hexagonal.architecture.model.Product; -import com.article.hexagonal.architecture.service.ProductService; - -/** - * @author AshwiniKeshri - * - */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(App.class) -@ActiveProfiles(value = "test") -public class ProductServiceTest { - - @Autowired - private ProductService productService; - - @Test - public void testCreateProduct() { - Product product = new Product(); - product.setDescription("test product"); - product.setName("Product1"); - product.setPrice(10.0); - product.setQuantity(100l); - Long id = productService.create(product); - Assert.assertTrue(id > 0); - } - -} From b59f4d32b4456d3496be6c80e5d66fdaa15eab12 Mon Sep 17 00:00:00 2001 From: akeshri Date: Sun, 5 Jul 2020 21:33:50 +0530 Subject: [PATCH 06/40] fixed code formatting issue --- .../baeldung/hexagonal/architecture/App.java | 6 +- .../hexagonal/architecture/ConsoleApp.java | 42 +++---- .../controller/ProductController.java | 47 ++++---- .../architecture/dtos/ProductDto.java | 103 ++++++++--------- .../hexagonal/architecture/model/Product.java | 104 +++++++++--------- .../repository/ProductRepository.java | 2 +- .../architecture/service/ProductService.java | 14 +-- .../service/ProductServiceImpl.java | 38 +++---- .../service/ProductServiceTest.java | 28 ++--- 9 files changed, 193 insertions(+), 191 deletions(-) diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java index 3563e3a0ec..29dbec470f 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java @@ -9,10 +9,8 @@ import org.springframework.boot.autoconfigure.SpringBootApplication; */ @SpringBootApplication -public class App -{ - public static void main( String[] args ) - { +public class App { + public static void main(String[] args) { SpringApplication.run(App.class, args); } } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java index 70edb8f9ed..6aef791893 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java @@ -20,29 +20,31 @@ import com.fasterxml.jackson.dataformat.csv.CsvSchema; * */ -@SpringBootApplication(exclude = {EmbeddedServletContainerAutoConfiguration.class,WebMvcAutoConfiguration.class}) +@SpringBootApplication(exclude = { EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class }) public class ConsoleApp implements CommandLineRunner { - @Autowired - private ProductService productService; + @Autowired + private ProductService productService; - public static void main(String[] args) { - SpringApplication.run(ConsoleApp.class, args); - } + public static void main(String[] args) { + SpringApplication.run(ConsoleApp.class, args); + } - @Override - public void run(String... args) throws Exception { - String filePath = ""; - if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) - && (filePath = args[1]).length() > 0) { - File sourceFile = new File(filePath); - if (sourceFile.exists()) { - CsvMapper mapper = new CsvMapper(); - List products = mapper.readerFor(Product.class).with(CsvSchema.emptySchema().withHeader()) - .readValues(sourceFile).readAll(); - productService.saveAll(products); - } + @Override + public void run(String... args) throws Exception { + String filePath = ""; + if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) && (filePath = args[1]).length() > 0) { + File sourceFile = new File(filePath); + if (sourceFile.exists()) { + CsvMapper mapper = new CsvMapper(); + List products = mapper.readerFor(Product.class) + .with(CsvSchema.emptySchema() + .withHeader()) + . readValues(sourceFile) + .readAll(); + productService.saveAll(products); + } - } + } - } + } } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java index 6645c379c2..f54d3268df 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java @@ -23,30 +23,33 @@ import com.baeldung.hexagonal.architecture.service.ProductService; @RequestMapping("api/v1/product") public class ProductController { - @Autowired - private ProductService productService; + @Autowired + private ProductService productService; - @RequestMapping(value = "/all", method = RequestMethod.GET) - public List list() { - return productService.findAll().stream().map(p -> new ProductDto(p)).collect(Collectors.toList()); - } + @RequestMapping(value = "/all", method = RequestMethod.GET) + public List list() { + return productService.findAll() + .stream() + .map(p -> new ProductDto(p)) + .collect(Collectors.toList()); + } - @RequestMapping(value = "/{productId}", method = RequestMethod.GET) - public ProductDto get(@PathVariable long productId) { - Product p = productService.findById(productId); - return p != null ? new ProductDto(p) : null; - } + @RequestMapping(value = "/{productId}", method = RequestMethod.GET) + public ProductDto get(@PathVariable long productId) { + Product p = productService.findById(productId); + return p != null ? new ProductDto(p) : null; + } - @RequestMapping(value = "/add", method = RequestMethod.POST) - public ProductDto create(@RequestBody ProductDto product) { - Product p = new Product(); - p.setDescription(product.getDescription()); - p.setName(product.getName()); - p.setPrice(product.getPrice()); - p.setQuantity(product.getQuantity()); - Long id = productService.create(p); - product.setId(id); - return product; - } + @RequestMapping(value = "/add", method = RequestMethod.POST) + public ProductDto create(@RequestBody ProductDto product) { + Product p = new Product(); + p.setDescription(product.getDescription()); + p.setName(product.getName()); + p.setPrice(product.getPrice()); + p.setQuantity(product.getQuantity()); + Long id = productService.create(p); + product.setId(id); + return product; + } } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java index 336631fb10..51692f56aa 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java @@ -1,71 +1,72 @@ package com.baeldung.hexagonal.architecture.dtos; import com.baeldung.hexagonal.architecture.model.Product; + /** * @author AshwiniKeshri * */ public class ProductDto { - private Long id; - - private String name; - - private Long quantity; - - private Double price; - - private String description; - - public ProductDto() {} - - public ProductDto(Product product) { - this.description = product.getDescription(); - this.id = product.getId(); - this.name = product.getName(); - this.price = product.getPrice(); - this.quantity = product.getQuantity(); - } + private Long id; - public Long getId() { - return id; - } + private String name; - public void setId(Long id) { - this.id = id; - } + private Long quantity; - public String getName() { - return name; - } + private Double price; - public void setName(String name) { - this.name = name; - } + private String description; - public Long getQuantity() { - return quantity; - } + public ProductDto() { + } - public void setQuantity(Long quantity) { - this.quantity = quantity; - } + public ProductDto(Product product) { + this.description = product.getDescription(); + this.id = product.getId(); + this.name = product.getName(); + this.price = product.getPrice(); + this.quantity = product.getQuantity(); + } - public Double getPrice() { - return price; - } + public Long getId() { + return id; + } - public void setPrice(Double price) { - this.price = price; - } + public void setId(Long id) { + this.id = id; + } - public String getDescription() { - return description; - } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } - public void setDescription(String description) { - this.description = description; - } - - } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java index dec4548283..697ff68b50 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java @@ -19,69 +19,67 @@ import javax.persistence.Table; @Entity @Table(name = "PRODUCT") -public class Product implements Serializable{ +public class Product implements Serializable { - /** - * - */ - private static final long serialVersionUID = 4000353732860709995L; + /** + * + */ + private static final long serialVersionUID = 4000353732860709995L; - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; - - @Column(name="NAME") - private String name; - - @Column(name = "QUANTITY") - private Long quantity; - - @Column(name = "PRICE") - private Double price; - - @Column(name = "DESCRIPTION") - private String description; + @Id + @GeneratedValue(strategy = GenerationType.AUTO) + private Long id; - public Long getId() { - return id; - } + @Column(name = "NAME") + private String name; - public void setId(Long id) { - this.id = id; - } + @Column(name = "QUANTITY") + private Long quantity; - public String getName() { - return name; - } + @Column(name = "PRICE") + private Double price; - public void setName(String name) { - this.name = name; - } + @Column(name = "DESCRIPTION") + private String description; - public Long getQuantity() { - return quantity; - } + public Long getId() { + return id; + } - public void setQuantity(Long quantity) { - this.quantity = quantity > 0 ? quantity : 0; - } + public void setId(Long id) { + this.id = id; + } - public Double getPrice() { - return price; - } + public String getName() { + return name; + } - public void setPrice(Double price) { - this.price = price == null ? 0.0 : price; - } + public void setName(String name) { + this.name = name; + } - public String getDescription() { - return description; - } + public Long getQuantity() { + return quantity; + } + + public void setQuantity(Long quantity) { + this.quantity = quantity > 0 ? quantity : 0; + } + + public Double getPrice() { + return price; + } + + public void setPrice(Double price) { + this.price = price == null ? 0.0 : price; + } + + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } - public void setDescription(String description) { - this.description = description; - } - - - } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java index 76c888ab59..ec50e86dc3 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java @@ -9,6 +9,6 @@ import com.baeldung.hexagonal.architecture.model.Product; * */ -public interface ProductRepository extends JpaRepository{ +public interface ProductRepository extends JpaRepository { } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java index b1d05a7db4..4844723140 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java @@ -11,11 +11,11 @@ import com.baeldung.hexagonal.architecture.model.Product; public interface ProductService { - List findAll(); - - Product findById(long id); - - Long create(Product product); - - void saveAll(List products); + List findAll(); + + Product findById(long id); + + Long create(Product product); + + void saveAll(List products); } diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java index 1005b5753d..e2d182a954 100644 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java +++ b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java @@ -18,27 +18,27 @@ import com.baeldung.hexagonal.architecture.repository.ProductRepository; @Service public class ProductServiceImpl implements ProductService { - private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); - - @Autowired - private ProductRepository productRepository; - - public List findAll() { - return productRepository.findAll(); - } + private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); - public Product findById(long id) { - return productRepository.findOne(id); - } + @Autowired + private ProductRepository productRepository; - public Long create(Product product) { - product = productRepository.saveAndFlush(product); - return product.getId(); - } + public List findAll() { + return productRepository.findAll(); + } - @Override - public void saveAll(List products) { - productRepository.save(products); - } + public Product findById(long id) { + return productRepository.findOne(id); + } + + public Long create(Product product) { + product = productRepository.saveAndFlush(product); + return product.getId(); + } + + @Override + public void saveAll(List products) { + productRepository.save(products); + } } diff --git a/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java b/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java index 021fdf1289..748df3c0d0 100644 --- a/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java +++ b/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java @@ -25,19 +25,19 @@ import com.baeldung.hexagonal.architecture.service.ProductService; @SpringApplicationConfiguration(App.class) @ActiveProfiles(value = "test") public class ProductServiceTest { - - @Autowired - private ProductService productService; - @Test - public void testCreateProduct() { - Product product = new Product(); - product.setDescription("test product"); - product.setName("Product1"); - product.setPrice(10.0); - product.setQuantity(100l); - Long id = productService.create(product); - Assert.assertTrue(id >0); - } - + @Autowired + private ProductService productService; + + @Test + public void testCreateProduct() { + Product product = new Product(); + product.setDescription("test product"); + product.setName("Product1"); + product.setPrice(10.0); + product.setQuantity(100l); + Long id = productService.create(product); + Assert.assertTrue(id > 0); + } + } From eab67c04b5bb0357b587eb1c34033253203ff269 Mon Sep 17 00:00:00 2001 From: akeshri Date: Sat, 29 Aug 2020 21:02:52 +0530 Subject: [PATCH 07/40] changes --- .../com/baeldung/article/HashMapExample.java | 36 ++++++++++++++++++ .../article/LinkedHashMapExample.java | 37 +++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 MapFirstKeyExample/src/com/baeldung/article/HashMapExample.java create mode 100644 MapFirstKeyExample/src/com/baeldung/article/LinkedHashMapExample.java diff --git a/MapFirstKeyExample/src/com/baeldung/article/HashMapExample.java b/MapFirstKeyExample/src/com/baeldung/article/HashMapExample.java new file mode 100644 index 0000000000..3292d995ab --- /dev/null +++ b/MapFirstKeyExample/src/com/baeldung/article/HashMapExample.java @@ -0,0 +1,36 @@ +package com.baeldung.article; + +import java.util.AbstractMap; +import java.util.HashMap; +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * @author AshwiniKeshri + * + */ +public class HashMapExample { + public static void main(String[] args) { + + Map hashmap = new HashMap<>(); + hashmap.put(5, "A"); + hashmap.put(1, "B"); + hashmap.put(2, "C"); + // hashmap.put(0, "D"); + + System.out.println(hashmap); + + Set> entrySet = hashmap.entrySet(); + + Iterator> iterator = entrySet.iterator(); + + if (iterator.hasNext()) + System.out.println("Using Iteraor: " + iterator.next()); + + System.out.println("Using Stream: " + entrySet.stream() + .findFirst() + .orElse(new AbstractMap.SimpleEntry(-1, "DEFAULT"))); + + } +} diff --git a/MapFirstKeyExample/src/com/baeldung/article/LinkedHashMapExample.java b/MapFirstKeyExample/src/com/baeldung/article/LinkedHashMapExample.java new file mode 100644 index 0000000000..249b9a0585 --- /dev/null +++ b/MapFirstKeyExample/src/com/baeldung/article/LinkedHashMapExample.java @@ -0,0 +1,37 @@ +package com.baeldung.article; + +import java.util.AbstractMap; +import java.util.Iterator; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.Set; + +/** + * @author AshwiniKeshri + * + */ +public class LinkedHashMapExample { + + public static void main(String[] args) { + + LinkedHashMap linkedHashMap = new LinkedHashMap<>(); + linkedHashMap.put(5, "A"); + linkedHashMap.put(1, "B"); + linkedHashMap.put(2, "C"); + // linkedHashMap.put(0, "D"); + + System.out.println(linkedHashMap); + + Set> entrySet = linkedHashMap.entrySet(); + + Iterator> iterator = entrySet.iterator(); + + if (iterator.hasNext()) + System.out.println("Using Iterator: " + iterator.next()); + + System.out.println("Using Stream: " + entrySet.stream() + .findFirst() + .orElse(new AbstractMap.SimpleEntry(0, "DEFAULT"))); + } + +} From c7b2ab3f46cabc6aedc7cf1b4d6c6ebb32ccfc9b Mon Sep 17 00:00:00 2001 From: akeshri Date: Sat, 29 Aug 2020 23:42:56 +0530 Subject: [PATCH 08/40] changes --- .../src/com/baeldung/article/HashMapExample.java | 0 .../src/com/baeldung/article/LinkedHashMapExample.java | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename {MapFirstKeyExample => map-first-key-tutorial}/src/com/baeldung/article/HashMapExample.java (100%) rename {MapFirstKeyExample => map-first-key-tutorial}/src/com/baeldung/article/LinkedHashMapExample.java (100%) diff --git a/MapFirstKeyExample/src/com/baeldung/article/HashMapExample.java b/map-first-key-tutorial/src/com/baeldung/article/HashMapExample.java similarity index 100% rename from MapFirstKeyExample/src/com/baeldung/article/HashMapExample.java rename to map-first-key-tutorial/src/com/baeldung/article/HashMapExample.java diff --git a/MapFirstKeyExample/src/com/baeldung/article/LinkedHashMapExample.java b/map-first-key-tutorial/src/com/baeldung/article/LinkedHashMapExample.java similarity index 100% rename from MapFirstKeyExample/src/com/baeldung/article/LinkedHashMapExample.java rename to map-first-key-tutorial/src/com/baeldung/article/LinkedHashMapExample.java From 976862de1a42f9886e66abffd8764b501635bf3b Mon Sep 17 00:00:00 2001 From: akeshri Date: Sat, 29 Aug 2020 23:48:29 +0530 Subject: [PATCH 09/40] changes --- map-first-key-tutorial/README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 map-first-key-tutorial/README.md diff --git a/map-first-key-tutorial/README.md b/map-first-key-tutorial/README.md new file mode 100644 index 0000000000..13fb71643b --- /dev/null +++ b/map-first-key-tutorial/README.md @@ -0,0 +1,4 @@ +# HashMap - Getting First Key And Value +A example on how to get first key and value from HashMap. + +- JDK 1.8 and above \ No newline at end of file From d8d2ecc4b4962ec9811bede5c6689bb6480542a2 Mon Sep 17 00:00:00 2001 From: Meysam Tamkin Date: Sun, 30 Aug 2020 19:59:24 +0430 Subject: [PATCH 10/40] Add some changes --- .../BeforeAfterAllNonStaticTest.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/BeforeAfterAllNonStaticTest.java diff --git a/testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/BeforeAfterAllNonStaticTest.java b/testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/BeforeAfterAllNonStaticTest.java new file mode 100644 index 0000000000..8dbe95ddf8 --- /dev/null +++ b/testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/BeforeAfterAllNonStaticTest.java @@ -0,0 +1,25 @@ +package com.baeldung.junit5.nonstatic; + +import org.junit.jupiter.api.*; + +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +public class BeforeAfterAllNonStaticTest { + + String input; + Long result; + + @BeforeAll + public void setup() { + input = "77"; + } + + @AfterAll + public void teardown() { + Assertions.assertEquals(77l, result); + } + + @Test + public void testConvertStringToLong() { + result = Long.valueOf(input); + } +} From 661027c771768e0d7d1da95d9e893bb8ae41d746 Mon Sep 17 00:00:00 2001 From: Jordan Simpson Date: Mon, 31 Aug 2020 13:42:50 -0500 Subject: [PATCH 11/40] Added code examples for BAEL-4534 --- .../CheckClassExistenceUnitTest.java | 33 +++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java diff --git a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java new file mode 100644 index 0000000000..10402af970 --- /dev/null +++ b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java @@ -0,0 +1,33 @@ +package com.baeldung.checkclassexistence; + +import org.junit.Test; + +public class CheckClassExistenceUnitTest { + + public static class InitializingClass { + static { + if (true) //enable throwing of an exception in a static initialization block + throw new RuntimeException(); + } + } + + @Test(expected = ClassNotFoundException.class) //thrown when class does not exist + public void givenNonExistingClass_whenUsingForName_classNotFound() throws ClassNotFoundException { + Class.forName("class.that.does.not.exist"); + } + + @Test + public void givenExistingClass_whenUsingForName_noException() throws ClassNotFoundException { + Class.forName("java.lang.String"); + } + + @Test(expected = ExceptionInInitializerError.class) //thrown when exception occurs inside of a static initialization block + public void givenInitializingClass_whenUsingForName_initializationError() throws ClassNotFoundException { + Class.forName("com.baeldung.checkclassexistence.CheckClassExistenceUnitTest$InitializingClass"); + } + + @Test + public void givenInitializingClass_whenUsingForNameWithoutInitialization_noException() throws ClassNotFoundException { + Class.forName("com.baeldung.checkclassexistence.CheckClassExistenceUnitTest$InitializingClass", false, getClass().getClassLoader()); + } +} From 691a948c5f55d20f98e3d96d5d201f8a098ffd6e Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Mon, 31 Aug 2020 22:53:30 +0300 Subject: [PATCH 12/40] BAEL-4371: Initial commit --- gradle-5/cmd-line-args/build.gradle | 16 ++++++++++++++++ gradle-5/settings.gradle | 3 ++- 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 gradle-5/cmd-line-args/build.gradle diff --git a/gradle-5/cmd-line-args/build.gradle b/gradle-5/cmd-line-args/build.gradle new file mode 100644 index 0000000000..aed57d775b --- /dev/null +++ b/gradle-5/cmd-line-args/build.gradle @@ -0,0 +1,16 @@ +apply plugin: "java" +description = "Gradle Command Line Arguments examples" + + + +task propertyTypes(){ + doLast{ + if (project.hasProperty('input')){ + println "This is my property ["+ project.getProperty('input')+"]" + } else { + println "No property was passed" + } + + } +} + diff --git a/gradle-5/settings.gradle b/gradle-5/settings.gradle index 5384d071e7..ede73daf0a 100644 --- a/gradle-5/settings.gradle +++ b/gradle-5/settings.gradle @@ -1,4 +1,5 @@ rootProject.name='gradle-5-articles' include 'java-exec' include 'unused-dependencies' -include 'source-sets' \ No newline at end of file +include 'source-sets' +include 'cmd-line-args' \ No newline at end of file From d19ac51aaa6e19426e1a43cd1c9eb88f7884850e Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Tue, 1 Sep 2020 18:52:06 +0300 Subject: [PATCH 13/40] BAEL-4371: Finished code samples --- gradle-5/cmd-line-args/build.gradle | 40 ++++++++++++++++--- .../main/java/com/baeldung/cmd/MainClass.java | 10 +++++ 2 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 gradle-5/cmd-line-args/src/main/java/com/baeldung/cmd/MainClass.java diff --git a/gradle-5/cmd-line-args/build.gradle b/gradle-5/cmd-line-args/build.gradle index aed57d775b..57c48e36c3 100644 --- a/gradle-5/cmd-line-args/build.gradle +++ b/gradle-5/cmd-line-args/build.gradle @@ -1,16 +1,46 @@ apply plugin: "java" +apply plugin: "application" description = "Gradle Command Line Arguments examples" +ext.javaMainClass = "com.baeldung.cmd.MainClass" +application { + mainClassName = javaMainClass +} task propertyTypes(){ doLast{ - if (project.hasProperty('input')){ - println "This is my property ["+ project.getProperty('input')+"]" - } else { - println "No property was passed" + project.getProperties().values().each { + println "Project property ["+it+"]" + } + + System.getProperties().each { + println "System property ["+it+"]" } - } } +if (project.hasProperty("args")) { + ext.cmdargs = project.getProperty("args") + ext.cmdargsarray = cmdargs.split() +} else { + ext.cmdargs = "" +} + +task cmdLineJavaExec(type: JavaExec) { + group = "Execution" + description = "Run the main class with JavaExecTask" + classpath = sourceSets.main.runtimeClasspath + main = javaMainClass + args cmdargsarray +} + +ext.cmdarray = ["java", "-classpath", sourceSets.main.runtimeClasspath.getAsPath(), javaMainClass] +cmdarray = (cmdarray << cmdargsarray).flatten() + +task cmdLineExec(type: Exec) { + dependsOn build + group = "Execution" + description = "Run the main class with ExecTask" + commandLine cmdarray +} diff --git a/gradle-5/cmd-line-args/src/main/java/com/baeldung/cmd/MainClass.java b/gradle-5/cmd-line-args/src/main/java/com/baeldung/cmd/MainClass.java new file mode 100644 index 0000000000..f00aa07d72 --- /dev/null +++ b/gradle-5/cmd-line-args/src/main/java/com/baeldung/cmd/MainClass.java @@ -0,0 +1,10 @@ +package com.baeldung.cmd; + +public class MainClass { + public static void main(String[] args) { + System.out.println("Gradle command line arguments example"); + for (String arg : args) { + System.out.println("Got argument [" + arg + "]"); + } + } +} From b27e6a047016b112fc47ab5ac7d6a63e0fe54ab6 Mon Sep 17 00:00:00 2001 From: Jordan Simpson Date: Wed, 2 Sep 2020 09:00:42 -0500 Subject: [PATCH 14/40] Added braces to the if block --- .../checkclassexistence/CheckClassExistenceUnitTest.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java index 10402af970..2e500b390f 100644 --- a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java +++ b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java @@ -6,8 +6,9 @@ public class CheckClassExistenceUnitTest { public static class InitializingClass { static { - if (true) //enable throwing of an exception in a static initialization block + if (true) { //enable throwing of an exception in a static initialization block throw new RuntimeException(); + } } } From 3146226399b94c536a51e6f87bbe21099b185f23 Mon Sep 17 00:00:00 2001 From: Meysam Tamkin Date: Thu, 3 Sep 2020 10:16:45 +0430 Subject: [PATCH 15/40] Fix it some changes --- ...icTest.java => BeforeAndAfterAnnotationsUnitTest.java} | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) rename testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/{BeforeAfterAllNonStaticTest.java => BeforeAndAfterAnnotationsUnitTest.java} (70%) diff --git a/testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/BeforeAfterAllNonStaticTest.java b/testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/BeforeAndAfterAnnotationsUnitTest.java similarity index 70% rename from testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/BeforeAfterAllNonStaticTest.java rename to testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/BeforeAndAfterAnnotationsUnitTest.java index 8dbe95ddf8..1bfea8447b 100644 --- a/testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/BeforeAfterAllNonStaticTest.java +++ b/testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/BeforeAndAfterAnnotationsUnitTest.java @@ -3,7 +3,7 @@ package com.baeldung.junit5.nonstatic; import org.junit.jupiter.api.*; @TestInstance(TestInstance.Lifecycle.PER_CLASS) -public class BeforeAfterAllNonStaticTest { +public class BeforeAndAfterAnnotationsUnitTest { String input; Long result; @@ -15,11 +15,13 @@ public class BeforeAfterAllNonStaticTest { @AfterAll public void teardown() { - Assertions.assertEquals(77l, result); + input = null; + result = null; } @Test - public void testConvertStringToLong() { + public void whenConvertStringToLong_thenResultShouldBeLong() { result = Long.valueOf(input); + Assertions.assertEquals(77l, result); } } From 1d5b20e70589e7311ebfdf353e8ca232a7c1f3ea Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Thu, 3 Sep 2020 15:46:48 +0300 Subject: [PATCH 16/40] BAEL-4371: Simplified examples --- gradle-5/cmd-line-args/build.gradle | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/gradle-5/cmd-line-args/build.gradle b/gradle-5/cmd-line-args/build.gradle index 57c48e36c3..5399db4815 100644 --- a/gradle-5/cmd-line-args/build.gradle +++ b/gradle-5/cmd-line-args/build.gradle @@ -10,21 +10,17 @@ application { task propertyTypes(){ doLast{ - project.getProperties().values().each { - println "Project property ["+it+"]" - } - - System.getProperties().each { - println "System property ["+it+"]" + if (project.hasProperty("args")) { + println "Our input argument with project property ["+project.getProperty("args")+"]" } + println "Our input argument with system property ["+System.getProperty("args")+"]" } } if (project.hasProperty("args")) { ext.cmdargs = project.getProperty("args") - ext.cmdargsarray = cmdargs.split() } else { - ext.cmdargs = "" + ext.cmdargs = "ls" } task cmdLineJavaExec(type: JavaExec) { @@ -32,15 +28,11 @@ task cmdLineJavaExec(type: JavaExec) { description = "Run the main class with JavaExecTask" classpath = sourceSets.main.runtimeClasspath main = javaMainClass - args cmdargsarray + args cmdargs.split() } -ext.cmdarray = ["java", "-classpath", sourceSets.main.runtimeClasspath.getAsPath(), javaMainClass] -cmdarray = (cmdarray << cmdargsarray).flatten() - task cmdLineExec(type: Exec) { - dependsOn build group = "Execution" - description = "Run the main class with ExecTask" - commandLine cmdarray + description = "Run the an external program with ExecTask" + commandLine cmdargs.split() } From d994a5bdbf1353382cccc88a7fc2db51bbd8268f Mon Sep 17 00:00:00 2001 From: Meysam Tamkin Date: Mon, 7 Sep 2020 13:35:07 +0430 Subject: [PATCH 17/40] Fix it some changes --- .../junit5/nonstatic/BeforeAndAfterAnnotationsUnitTest.java | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename testing-modules/junit-5/src/{main => test}/java/com/baeldung/junit5/nonstatic/BeforeAndAfterAnnotationsUnitTest.java (100%) diff --git a/testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/BeforeAndAfterAnnotationsUnitTest.java b/testing-modules/junit-5/src/test/java/com/baeldung/junit5/nonstatic/BeforeAndAfterAnnotationsUnitTest.java similarity index 100% rename from testing-modules/junit-5/src/main/java/com/baeldung/junit5/nonstatic/BeforeAndAfterAnnotationsUnitTest.java rename to testing-modules/junit-5/src/test/java/com/baeldung/junit5/nonstatic/BeforeAndAfterAnnotationsUnitTest.java From b331b1779ffbc6fdb004333d212f314c4a2c993d Mon Sep 17 00:00:00 2001 From: akeshri Date: Tue, 8 Sep 2020 21:04:43 +0530 Subject: [PATCH 18/40] first pair code --- .../mapfirstpair/MapFirstPairService.java | 17 +++ .../mapfirstpair/MapFirstPairServiceImpl.java | 40 ++++++ .../MapFirstPairServiceUnitTest.java | 125 ++++++++++++++++++ hexagonal-architecture/README.md | 17 --- hexagonal-architecture/pom.xml | 48 ------- .../baeldung/hexagonal/architecture/App.java | 16 --- .../hexagonal/architecture/ConsoleApp.java | 50 ------- .../controller/ProductController.java | 55 -------- .../architecture/dtos/ProductDto.java | 72 ---------- .../hexagonal/architecture/model/Product.java | 85 ------------ .../repository/ProductRepository.java | 14 -- .../architecture/service/ProductService.java | 21 --- .../service/ProductServiceImpl.java | 44 ------ .../resources/application-batch.properties | 9 -- .../resources/application-test.properties | 8 -- .../src/main/resources/application.properties | 8 -- .../src/main/resources/db/PRODUCT.sql | 9 -- .../service/ProductServiceTest.java | 43 ------ 18 files changed, 182 insertions(+), 499 deletions(-) create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java create mode 100644 core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java delete mode 100644 hexagonal-architecture/README.md delete mode 100644 hexagonal-architecture/pom.xml delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java delete mode 100644 hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java delete mode 100644 hexagonal-architecture/src/main/resources/application-batch.properties delete mode 100644 hexagonal-architecture/src/main/resources/application-test.properties delete mode 100644 hexagonal-architecture/src/main/resources/application.properties delete mode 100644 hexagonal-architecture/src/main/resources/db/PRODUCT.sql delete mode 100644 hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java new file mode 100644 index 0000000000..5d3c4fac32 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java @@ -0,0 +1,17 @@ +/** + * + */ +package com.baeldung.collections.mapfirstpair; + +import java.util.Map; + +/** + * @author ASHWINI + * + */ +public interface MapFirstPairService { + + Map.Entry getFirstPairUsingIterator(Map map); + + Map.Entry getFirstPairUsingStream(Map map); +} diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java new file mode 100644 index 0000000000..aff6430216 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java @@ -0,0 +1,40 @@ +/** + * + */ +package com.baeldung.collections.mapfirstpair; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +/** + * @author ASHWINI + * + */ +public class MapFirstPairServiceImpl implements MapFirstPairService { + + public Map.Entry getFirstPairUsingIterator(Map map) { + if (map == null || map.size() == 0) + return null; + + Iterator> iterator = map.entrySet() + .iterator(); + + if (iterator.hasNext()) + return iterator.next(); + + return null; + } + + public Map.Entry getFirstPairUsingStream(Map map) { + if (map == null || map.size() == 0) + return null; + + Set> entrySet = map.entrySet(); + + return entrySet.stream() + .findFirst() + .get(); + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java new file mode 100644 index 0000000000..56e293ffc5 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java @@ -0,0 +1,125 @@ +package com.baeldung.collections.mapfirstpair; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotEquals; + +import java.util.AbstractMap; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.junit.Before; +import org.junit.Test; + +public class MapFirstPairServiceUnitTest { + + private MapFirstPairService mapFirstPairService; + + @Before + public void Setup() { + + mapFirstPairService = new MapFirstPairServiceImpl(); + } + + private void populateMapValues(Map map) { + if (map != null) { + map.put(5, "A"); + map.put(1, "B"); + map.put(2, "C"); + } + } + + @Test + public void whenUsingIteratorForHashMap() { + Map hashMap = new HashMap<>(); + populateMapValues(hashMap); + + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(hashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); + Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + assertNotEquals(pairInsertedFirst, actualValue); + } + + @Test + public void whenUsingStreamForHashMap() { + Map hashMap = new HashMap<>(); + populateMapValues(hashMap); + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(hashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); + Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + assertNotEquals(pairInsertedFirst, actualValue); + } + + @Test + public void whenUsingIteratorForLinkedHashMap() { + Map linkedHashMap = new LinkedHashMap<>(); + populateMapValues(linkedHashMap); + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(linkedHashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenUsingStreamForLinkedHashMap() { + Map linkedHashMap = new LinkedHashMap<>(); + populateMapValues(linkedHashMap); + + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(linkedHashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() { + Map hashMap = new HashMap<>(); + populateMapValues(hashMap); + + hashMap.put(0, "D"); + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(hashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() { + Map hashMap = new HashMap<>(); + populateMapValues(hashMap); + + hashMap.put(0, "D"); + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(hashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() { + Map linkedHashMap = new LinkedHashMap<>(); + populateMapValues(linkedHashMap); + + linkedHashMap.put(0, "D"); + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(linkedHashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + } + + @Test + public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() { + Map linkedHashMap = new LinkedHashMap<>(); + populateMapValues(linkedHashMap); + + linkedHashMap.put(0, "D"); + Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(linkedHashMap); + Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); + + assertEquals(expectedValue, actualValue); + } +} diff --git a/hexagonal-architecture/README.md b/hexagonal-architecture/README.md deleted file mode 100644 index 4dd10368e0..0000000000 --- a/hexagonal-architecture/README.md +++ /dev/null @@ -1,17 +0,0 @@ -# Hexagonal Architecture -A quick and practical example of Hexagonal Architecture using Spring boot. - -This application is using h2 database,which can be accessible http:/localhost:8080/h2 - -Main Application schema : hexagonal - -Test Application Schema : hexagonal_test - -1. Rest Api : execute [App](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java) - - Get All products : http://localhost:8080/api/v1/product/all - - Get product by id : http://localhost:8080/api/v1/product/{productId} - - Add a product : http://localhost:8080/api/v1/product/add - For more detail refer [ProductController](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java) - -2. Batch processing : We need to configure active profile as batch i.e. -Dspring.profiles.active=batch and execute [ConsoleApp](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java) -3. Test case : [ProductServiceTest](https://github.com/akeshri/tutorials/blob/master/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java) diff --git a/hexagonal-architecture/pom.xml b/hexagonal-architecture/pom.xml deleted file mode 100644 index d014617639..0000000000 --- a/hexagonal-architecture/pom.xml +++ /dev/null @@ -1,48 +0,0 @@ - - 4.0.0 - - com.article - hexagonal-architecture - 0.0.1-SNAPSHOT - jar - - org.springframework.boot - spring-boot-starter-parent - 1.3.1.RELEASE - - - hexagonal-architecture - http://maven.apache.org - - - UTF-8 - 1.8 - 1.8 - - - - - org.springframework.boot - spring-boot-starter-web - - - org.springframework.boot - spring-boot-starter-data-jpa - - - com.h2database - h2 - - - org.springframework.boot - spring-boot-starter-test - test - - - com.fasterxml.jackson.dataformat - jackson-dataformat-csv - - - diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java deleted file mode 100644 index 29dbec470f..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/App.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.baeldung.hexagonal.architecture; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -/** - * @author AshwiniKeshri - * - */ - -@SpringBootApplication -public class App { - public static void main(String[] args) { - SpringApplication.run(App.class, args); - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java deleted file mode 100644 index 6aef791893..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/ConsoleApp.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.baeldung.hexagonal.architecture; - -import java.io.File; -import java.util.List; - -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.boot.autoconfigure.web.EmbeddedServletContainerAutoConfiguration; -import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; - -import com.baeldung.hexagonal.architecture.model.Product; -import com.baeldung.hexagonal.architecture.service.ProductService; -import com.fasterxml.jackson.dataformat.csv.CsvMapper; -import com.fasterxml.jackson.dataformat.csv.CsvSchema; - -/** - * @author AshwiniKeshri - * - */ - -@SpringBootApplication(exclude = { EmbeddedServletContainerAutoConfiguration.class, WebMvcAutoConfiguration.class }) -public class ConsoleApp implements CommandLineRunner { - @Autowired - private ProductService productService; - - public static void main(String[] args) { - SpringApplication.run(ConsoleApp.class, args); - } - - @Override - public void run(String... args) throws Exception { - String filePath = ""; - if (args != null && args.length == 2 && "Product".equalsIgnoreCase(args[0]) && (filePath = args[1]).length() > 0) { - File sourceFile = new File(filePath); - if (sourceFile.exists()) { - CsvMapper mapper = new CsvMapper(); - List products = mapper.readerFor(Product.class) - .with(CsvSchema.emptySchema() - .withHeader()) - . readValues(sourceFile) - .readAll(); - productService.saveAll(products); - } - - } - - } -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java deleted file mode 100644 index f54d3268df..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/controller/ProductController.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.baeldung.hexagonal.architecture.controller; - -import java.util.List; -import java.util.stream.Collectors; - -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestBody; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RequestMethod; -import org.springframework.web.bind.annotation.RestController; - -import com.baeldung.hexagonal.architecture.dtos.ProductDto; -import com.baeldung.hexagonal.architecture.model.Product; -import com.baeldung.hexagonal.architecture.service.ProductService; - -/** - * @author AshwiniKeshri - * - */ - -@RestController -@RequestMapping("api/v1/product") -public class ProductController { - - @Autowired - private ProductService productService; - - @RequestMapping(value = "/all", method = RequestMethod.GET) - public List list() { - return productService.findAll() - .stream() - .map(p -> new ProductDto(p)) - .collect(Collectors.toList()); - } - - @RequestMapping(value = "/{productId}", method = RequestMethod.GET) - public ProductDto get(@PathVariable long productId) { - Product p = productService.findById(productId); - return p != null ? new ProductDto(p) : null; - } - - @RequestMapping(value = "/add", method = RequestMethod.POST) - public ProductDto create(@RequestBody ProductDto product) { - Product p = new Product(); - p.setDescription(product.getDescription()); - p.setName(product.getName()); - p.setPrice(product.getPrice()); - p.setQuantity(product.getQuantity()); - Long id = productService.create(p); - product.setId(id); - return product; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java deleted file mode 100644 index 51692f56aa..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/dtos/ProductDto.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.baeldung.hexagonal.architecture.dtos; - -import com.baeldung.hexagonal.architecture.model.Product; - -/** - * @author AshwiniKeshri - * - */ -public class ProductDto { - - private Long id; - - private String name; - - private Long quantity; - - private Double price; - - private String description; - - public ProductDto() { - } - - public ProductDto(Product product) { - this.description = product.getDescription(); - this.id = product.getId(); - this.name = product.getName(); - this.price = product.getPrice(); - this.quantity = product.getQuantity(); - } - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Long getQuantity() { - return quantity; - } - - public void setQuantity(Long quantity) { - this.quantity = quantity; - } - - public Double getPrice() { - return price; - } - - public void setPrice(Double price) { - this.price = price; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java deleted file mode 100644 index 697ff68b50..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/model/Product.java +++ /dev/null @@ -1,85 +0,0 @@ -/** - * - */ -package com.baeldung.hexagonal.architecture.model; - -import java.io.Serializable; - -import javax.persistence.Column; -import javax.persistence.Entity; -import javax.persistence.GeneratedValue; -import javax.persistence.GenerationType; -import javax.persistence.Id; -import javax.persistence.Table; - -/** - * @author AshwiniKeshri - * - */ - -@Entity -@Table(name = "PRODUCT") -public class Product implements Serializable { - - /** - * - */ - private static final long serialVersionUID = 4000353732860709995L; - - @Id - @GeneratedValue(strategy = GenerationType.AUTO) - private Long id; - - @Column(name = "NAME") - private String name; - - @Column(name = "QUANTITY") - private Long quantity; - - @Column(name = "PRICE") - private Double price; - - @Column(name = "DESCRIPTION") - private String description; - - public Long getId() { - return id; - } - - public void setId(Long id) { - this.id = id; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Long getQuantity() { - return quantity; - } - - public void setQuantity(Long quantity) { - this.quantity = quantity > 0 ? quantity : 0; - } - - public Double getPrice() { - return price; - } - - public void setPrice(Double price) { - this.price = price == null ? 0.0 : price; - } - - public String getDescription() { - return description; - } - - public void setDescription(String description) { - this.description = description; - } - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java deleted file mode 100644 index ec50e86dc3..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/repository/ProductRepository.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.baeldung.hexagonal.architecture.repository; - -import org.springframework.data.jpa.repository.JpaRepository; - -import com.baeldung.hexagonal.architecture.model.Product; - -/** - * @author AshwiniKeshri - * - */ - -public interface ProductRepository extends JpaRepository { - -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java deleted file mode 100644 index 4844723140..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductService.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.baeldung.hexagonal.architecture.service; - -import java.util.List; - -import com.baeldung.hexagonal.architecture.model.Product; - -/** - * @author AshwiniKeshri - * - */ - -public interface ProductService { - - List findAll(); - - Product findById(long id); - - Long create(Product product); - - void saveAll(List products); -} diff --git a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java b/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java deleted file mode 100644 index e2d182a954..0000000000 --- a/hexagonal-architecture/src/main/java/com/baeldung/hexagonal/architecture/service/ProductServiceImpl.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.baeldung.hexagonal.architecture.service; - -import java.util.List; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.stereotype.Service; - -import com.baeldung.hexagonal.architecture.model.Product; -import com.baeldung.hexagonal.architecture.repository.ProductRepository; - -/** - * @author AshwiniKeshri - * - */ - -@Service -public class ProductServiceImpl implements ProductService { - - private Logger logger = LoggerFactory.getLogger(ProductServiceImpl.class); - - @Autowired - private ProductRepository productRepository; - - public List findAll() { - return productRepository.findAll(); - } - - public Product findById(long id) { - return productRepository.findOne(id); - } - - public Long create(Product product) { - product = productRepository.saveAndFlush(product); - return product.getId(); - } - - @Override - public void saveAll(List products) { - productRepository.save(products); - } - -} diff --git a/hexagonal-architecture/src/main/resources/application-batch.properties b/hexagonal-architecture/src/main/resources/application-batch.properties deleted file mode 100644 index 8c83d19f74..0000000000 --- a/hexagonal-architecture/src/main/resources/application-batch.properties +++ /dev/null @@ -1,9 +0,0 @@ -spring.main.web-environment=false -spring.jpa.hibernate.ddl-auto=false; -spring.h2.console.enabled=true -spring.h2.console.path=/h2 - -spring.datasource.url=jdbc:h2:file:~/hexagonal -spring.datasource.username=sa -spring.datasource.password= -spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/hexagonal-architecture/src/main/resources/application-test.properties b/hexagonal-architecture/src/main/resources/application-test.properties deleted file mode 100644 index 701313a878..0000000000 --- a/hexagonal-architecture/src/main/resources/application-test.properties +++ /dev/null @@ -1,8 +0,0 @@ -spring.jpa.hibernate.ddl-auto=false; -spring.h2.console.enabled=true -spring.h2.console.path=/h2 - -spring.datasource.url=jdbc:h2:file:~/hexagonal_test -spring.datasource.username=sa -spring.datasource.password= -spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/hexagonal-architecture/src/main/resources/application.properties b/hexagonal-architecture/src/main/resources/application.properties deleted file mode 100644 index 14c80a1af4..0000000000 --- a/hexagonal-architecture/src/main/resources/application.properties +++ /dev/null @@ -1,8 +0,0 @@ -spring.jpa.hibernate.ddl-auto=false; -spring.h2.console.enabled=true -spring.h2.console.path=/h2 - -spring.datasource.url=jdbc:h2:file:~/hexagonal -spring.datasource.username=sa -spring.datasource.password= -spring.datasource.driver-class-name=org.h2.Driver \ No newline at end of file diff --git a/hexagonal-architecture/src/main/resources/db/PRODUCT.sql b/hexagonal-architecture/src/main/resources/db/PRODUCT.sql deleted file mode 100644 index a0fef3a710..0000000000 --- a/hexagonal-architecture/src/main/resources/db/PRODUCT.sql +++ /dev/null @@ -1,9 +0,0 @@ -CREATE TABLE PRODUCT( - ID INT AUTO_INCREMENT, - NAME VARCHAR(255), - QUANTITY INTEGER, - PRICE DOUBLE, - DESCRIPTION VARCHAR(1000), -); - -INSERT INTO PRODUCT(NAME,QUANTITY,PRICE,DESCRIPTION) VALUES ('iPhone 11 Pro',10,300,'First triple camera system'); \ No newline at end of file diff --git a/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java b/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java deleted file mode 100644 index 748df3c0d0..0000000000 --- a/hexagonal-architecture/src/test/java/com/baeldung/hexagonal/architecture/service/ProductServiceTest.java +++ /dev/null @@ -1,43 +0,0 @@ -/** - * - */ -package com.baeldung.hexagonal.architecture.service; - -import org.junit.Assert; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.SpringApplicationConfiguration; -import org.springframework.context.annotation.PropertySource; -import org.springframework.context.annotation.PropertySources; -import org.springframework.test.context.ActiveProfiles; -import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; - -import com.baeldung.hexagonal.architecture.App; -import com.baeldung.hexagonal.architecture.model.Product; -import com.baeldung.hexagonal.architecture.service.ProductService; - -/** - * @author AshwiniKeshri - * - */ -@RunWith(SpringJUnit4ClassRunner.class) -@SpringApplicationConfiguration(App.class) -@ActiveProfiles(value = "test") -public class ProductServiceTest { - - @Autowired - private ProductService productService; - - @Test - public void testCreateProduct() { - Product product = new Product(); - product.setDescription("test product"); - product.setName("Product1"); - product.setPrice(10.0); - product.setQuantity(100l); - Long id = productService.create(product); - Assert.assertTrue(id > 0); - } - -} From 64b7dec1b0c761e322c304a1176ecdc8b38579ab Mon Sep 17 00:00:00 2001 From: akeshri Date: Tue, 8 Sep 2020 21:14:28 +0530 Subject: [PATCH 19/40] changes --- map-first-key-tutorial/README.md | 4 -- .../com/baeldung/article/HashMapExample.java | 36 ------------------ .../article/LinkedHashMapExample.java | 37 ------------------- 3 files changed, 77 deletions(-) delete mode 100644 map-first-key-tutorial/README.md delete mode 100644 map-first-key-tutorial/src/com/baeldung/article/HashMapExample.java delete mode 100644 map-first-key-tutorial/src/com/baeldung/article/LinkedHashMapExample.java diff --git a/map-first-key-tutorial/README.md b/map-first-key-tutorial/README.md deleted file mode 100644 index 13fb71643b..0000000000 --- a/map-first-key-tutorial/README.md +++ /dev/null @@ -1,4 +0,0 @@ -# HashMap - Getting First Key And Value -A example on how to get first key and value from HashMap. - -- JDK 1.8 and above \ No newline at end of file diff --git a/map-first-key-tutorial/src/com/baeldung/article/HashMapExample.java b/map-first-key-tutorial/src/com/baeldung/article/HashMapExample.java deleted file mode 100644 index 3292d995ab..0000000000 --- a/map-first-key-tutorial/src/com/baeldung/article/HashMapExample.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.baeldung.article; - -import java.util.AbstractMap; -import java.util.HashMap; -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -/** - * @author AshwiniKeshri - * - */ -public class HashMapExample { - public static void main(String[] args) { - - Map hashmap = new HashMap<>(); - hashmap.put(5, "A"); - hashmap.put(1, "B"); - hashmap.put(2, "C"); - // hashmap.put(0, "D"); - - System.out.println(hashmap); - - Set> entrySet = hashmap.entrySet(); - - Iterator> iterator = entrySet.iterator(); - - if (iterator.hasNext()) - System.out.println("Using Iteraor: " + iterator.next()); - - System.out.println("Using Stream: " + entrySet.stream() - .findFirst() - .orElse(new AbstractMap.SimpleEntry(-1, "DEFAULT"))); - - } -} diff --git a/map-first-key-tutorial/src/com/baeldung/article/LinkedHashMapExample.java b/map-first-key-tutorial/src/com/baeldung/article/LinkedHashMapExample.java deleted file mode 100644 index 249b9a0585..0000000000 --- a/map-first-key-tutorial/src/com/baeldung/article/LinkedHashMapExample.java +++ /dev/null @@ -1,37 +0,0 @@ -package com.baeldung.article; - -import java.util.AbstractMap; -import java.util.Iterator; -import java.util.LinkedHashMap; -import java.util.Map; -import java.util.Set; - -/** - * @author AshwiniKeshri - * - */ -public class LinkedHashMapExample { - - public static void main(String[] args) { - - LinkedHashMap linkedHashMap = new LinkedHashMap<>(); - linkedHashMap.put(5, "A"); - linkedHashMap.put(1, "B"); - linkedHashMap.put(2, "C"); - // linkedHashMap.put(0, "D"); - - System.out.println(linkedHashMap); - - Set> entrySet = linkedHashMap.entrySet(); - - Iterator> iterator = entrySet.iterator(); - - if (iterator.hasNext()) - System.out.println("Using Iterator: " + iterator.next()); - - System.out.println("Using Stream: " + entrySet.stream() - .findFirst() - .orElse(new AbstractMap.SimpleEntry(0, "DEFAULT"))); - } - -} From ece4d65065f96f074e21c3ab59fbf6258fc9f6b9 Mon Sep 17 00:00:00 2001 From: akeshri Date: Wed, 9 Sep 2020 09:34:10 +0530 Subject: [PATCH 20/40] changes --- .../mapfirstpair/MapFirstPairExample.java | 42 +++++++++++++++ .../mapfirstpair/MapFirstPairService.java | 17 ------ .../mapfirstpair/MapFirstPairServiceImpl.java | 40 -------------- ...nitTest.java => MapFirstPairUnitTest.java} | 54 ++++++++----------- 4 files changed, 65 insertions(+), 88 deletions(-) create mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java delete mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java delete mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java rename core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/{MapFirstPairServiceUnitTest.java => MapFirstPairUnitTest.java} (61%) diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java new file mode 100644 index 0000000000..8e5fc296d4 --- /dev/null +++ b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java @@ -0,0 +1,42 @@ +package com.baeldung.collections.mapfirstpair; + +import java.util.Iterator; +import java.util.Map; +import java.util.Set; + +public class MapFirstPairExample { + + public Map.Entry getFirstPairUsingIterator(Map map) { + if (map == null || map.size() == 0) + return null; + + Iterator> iterator = map.entrySet() + .iterator(); + + if (iterator.hasNext()) + return iterator.next(); + + return null; + } + + public Map.Entry getFirstPairUsingStream(Map map) { + if (map == null || map.size() == 0) + return null; + + Set> entrySet = map.entrySet(); + + return entrySet.stream() + .findFirst() + .get(); + } + + public Map populateMapValues(Map map) { + if (map != null) { + map.put(5, "A"); + map.put(1, "B"); + map.put(2, "C"); + } + return map; + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java deleted file mode 100644 index 5d3c4fac32..0000000000 --- a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairService.java +++ /dev/null @@ -1,17 +0,0 @@ -/** - * - */ -package com.baeldung.collections.mapfirstpair; - -import java.util.Map; - -/** - * @author ASHWINI - * - */ -public interface MapFirstPairService { - - Map.Entry getFirstPairUsingIterator(Map map); - - Map.Entry getFirstPairUsingStream(Map map); -} diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java deleted file mode 100644 index aff6430216..0000000000 --- a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceImpl.java +++ /dev/null @@ -1,40 +0,0 @@ -/** - * - */ -package com.baeldung.collections.mapfirstpair; - -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -/** - * @author ASHWINI - * - */ -public class MapFirstPairServiceImpl implements MapFirstPairService { - - public Map.Entry getFirstPairUsingIterator(Map map) { - if (map == null || map.size() == 0) - return null; - - Iterator> iterator = map.entrySet() - .iterator(); - - if (iterator.hasNext()) - return iterator.next(); - - return null; - } - - public Map.Entry getFirstPairUsingStream(Map map) { - if (map == null || map.size() == 0) - return null; - - Set> entrySet = map.entrySet(); - - return entrySet.stream() - .findFirst() - .get(); - } - -} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java similarity index 61% rename from core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java rename to core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java index 56e293ffc5..c8198bcc02 100644 --- a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairServiceUnitTest.java +++ b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java @@ -11,30 +11,22 @@ import java.util.Map; import org.junit.Before; import org.junit.Test; -public class MapFirstPairServiceUnitTest { +public class MapFirstPairUnitTest { - private MapFirstPairService mapFirstPairService; + private MapFirstPairExample mapFirstPairExample; @Before public void Setup() { - mapFirstPairService = new MapFirstPairServiceImpl(); - } - - private void populateMapValues(Map map) { - if (map != null) { - map.put(5, "A"); - map.put(1, "B"); - map.put(2, "C"); - } + mapFirstPairExample = new MapFirstPairExample(); } @Test - public void whenUsingIteratorForHashMap() { + public void whenUsingIteratorForHashMap_thenFirstPairWhichWasNotInsertedFirst() { Map hashMap = new HashMap<>(); - populateMapValues(hashMap); + hashMap = mapFirstPairExample.populateMapValues(hashMap); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(hashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); @@ -43,10 +35,10 @@ public class MapFirstPairServiceUnitTest { } @Test - public void whenUsingStreamForHashMap() { + public void whenUsingStreamForHashMap_thenFirstPairWhichWasNotInsertedFirst() { Map hashMap = new HashMap<>(); - populateMapValues(hashMap); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(hashMap); + hashMap = mapFirstPairExample.populateMapValues(hashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); @@ -55,21 +47,21 @@ public class MapFirstPairServiceUnitTest { } @Test - public void whenUsingIteratorForLinkedHashMap() { + public void whenUsingIteratorForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() { Map linkedHashMap = new LinkedHashMap<>(); - populateMapValues(linkedHashMap); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(linkedHashMap); + linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); } @Test - public void whenUsingStreamForLinkedHashMap() { + public void whenUsingStreamForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() { Map linkedHashMap = new LinkedHashMap<>(); - populateMapValues(linkedHashMap); + linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(linkedHashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -78,10 +70,10 @@ public class MapFirstPairServiceUnitTest { @Test public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() { Map hashMap = new HashMap<>(); - populateMapValues(hashMap); + hashMap = mapFirstPairExample.populateMapValues(hashMap); hashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(hashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); assertEquals(expectedValue, actualValue); @@ -90,10 +82,10 @@ public class MapFirstPairServiceUnitTest { @Test public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() { Map hashMap = new HashMap<>(); - populateMapValues(hashMap); + hashMap = mapFirstPairExample.populateMapValues(hashMap); hashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(hashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); assertEquals(expectedValue, actualValue); @@ -102,10 +94,10 @@ public class MapFirstPairServiceUnitTest { @Test public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() { Map linkedHashMap = new LinkedHashMap<>(); - populateMapValues(linkedHashMap); + linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); linkedHashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingIterator(linkedHashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -114,10 +106,10 @@ public class MapFirstPairServiceUnitTest { @Test public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() { Map linkedHashMap = new LinkedHashMap<>(); - populateMapValues(linkedHashMap); + linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); linkedHashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairService.getFirstPairUsingStream(linkedHashMap); + Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); From 1624cd3387f6a2f02d59d7fd75d72d0cee4d4470 Mon Sep 17 00:00:00 2001 From: Jordan Simpson Date: Wed, 9 Sep 2020 08:18:38 -0500 Subject: [PATCH 21/40] Update core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java Co-authored-by: KevinGilmore --- .../checkclassexistence/CheckClassExistenceUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java index 2e500b390f..04c1ad0115 100644 --- a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java +++ b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java @@ -13,7 +13,7 @@ public class CheckClassExistenceUnitTest { } @Test(expected = ClassNotFoundException.class) //thrown when class does not exist - public void givenNonExistingClass_whenUsingForName_classNotFound() throws ClassNotFoundException { + public void givenNonExistingClass_whenUsingForName_thenClassNotFound() throws ClassNotFoundException { Class.forName("class.that.does.not.exist"); } From 576d3c5f425ae49caf9f5f0eb255bd70ef0a05c5 Mon Sep 17 00:00:00 2001 From: Jordan Simpson Date: Wed, 9 Sep 2020 08:18:46 -0500 Subject: [PATCH 22/40] Update core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java Co-authored-by: KevinGilmore --- .../checkclassexistence/CheckClassExistenceUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java index 04c1ad0115..aef7e686b5 100644 --- a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java +++ b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java @@ -18,7 +18,7 @@ public class CheckClassExistenceUnitTest { } @Test - public void givenExistingClass_whenUsingForName_noException() throws ClassNotFoundException { + public void givenExistingClass_whenUsingForName_thenNoException() throws ClassNotFoundException { Class.forName("java.lang.String"); } From 10efba315d22e7d69e8ee1b775b753a94d54d95c Mon Sep 17 00:00:00 2001 From: Jordan Simpson Date: Wed, 9 Sep 2020 08:18:51 -0500 Subject: [PATCH 23/40] Update core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java Co-authored-by: KevinGilmore --- .../checkclassexistence/CheckClassExistenceUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java index aef7e686b5..cf455a6e35 100644 --- a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java +++ b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java @@ -23,7 +23,7 @@ public class CheckClassExistenceUnitTest { } @Test(expected = ExceptionInInitializerError.class) //thrown when exception occurs inside of a static initialization block - public void givenInitializingClass_whenUsingForName_initializationError() throws ClassNotFoundException { + public void givenInitializingClass_whenUsingForName_thenInitializationError() throws ClassNotFoundException { Class.forName("com.baeldung.checkclassexistence.CheckClassExistenceUnitTest$InitializingClass"); } From cf15c1b002d0aa5ff917f6f906631298ac424e05 Mon Sep 17 00:00:00 2001 From: Jordan Simpson Date: Wed, 9 Sep 2020 08:18:57 -0500 Subject: [PATCH 24/40] Update core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java Co-authored-by: KevinGilmore --- .../checkclassexistence/CheckClassExistenceUnitTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java index cf455a6e35..b565f9de31 100644 --- a/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java +++ b/core-java-modules/core-java-lang-3/src/test/java/com/baeldung/checkclassexistence/CheckClassExistenceUnitTest.java @@ -28,7 +28,7 @@ public class CheckClassExistenceUnitTest { } @Test - public void givenInitializingClass_whenUsingForNameWithoutInitialization_noException() throws ClassNotFoundException { + public void givenInitializingClass_whenUsingForNameWithoutInitialization_thenNoException() throws ClassNotFoundException { Class.forName("com.baeldung.checkclassexistence.CheckClassExistenceUnitTest$InitializingClass", false, getClass().getClassLoader()); } } From 2f918653634e0c3a10b425118496cc54d8a7c05b Mon Sep 17 00:00:00 2001 From: akeshri Date: Thu, 10 Sep 2020 09:26:24 +0530 Subject: [PATCH 25/40] changes --- .../mapfirstpair/MapFirstPairExample.java | 42 ------------ .../mapfirstpair/MapFirstPairUnitTest.java | 68 +++++++++++++------ 2 files changed, 47 insertions(+), 63 deletions(-) delete mode 100644 core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java diff --git a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java b/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java deleted file mode 100644 index 8e5fc296d4..0000000000 --- a/core-java-modules/core-java-collections-3/src/main/java/com/baeldung/collections/mapfirstpair/MapFirstPairExample.java +++ /dev/null @@ -1,42 +0,0 @@ -package com.baeldung.collections.mapfirstpair; - -import java.util.Iterator; -import java.util.Map; -import java.util.Set; - -public class MapFirstPairExample { - - public Map.Entry getFirstPairUsingIterator(Map map) { - if (map == null || map.size() == 0) - return null; - - Iterator> iterator = map.entrySet() - .iterator(); - - if (iterator.hasNext()) - return iterator.next(); - - return null; - } - - public Map.Entry getFirstPairUsingStream(Map map) { - if (map == null || map.size() == 0) - return null; - - Set> entrySet = map.entrySet(); - - return entrySet.stream() - .findFirst() - .get(); - } - - public Map populateMapValues(Map map) { - if (map != null) { - map.put(5, "A"); - map.put(1, "B"); - map.put(2, "C"); - } - return map; - } - -} \ No newline at end of file diff --git a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java index c8198bcc02..b25e0932d8 100644 --- a/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java +++ b/core-java-modules/core-java-collections-3/src/test/java/com/baeldung/collections/mapfirstpair/MapFirstPairUnitTest.java @@ -5,28 +5,54 @@ import static org.junit.Assert.assertNotEquals; import java.util.AbstractMap; import java.util.HashMap; +import java.util.Iterator; import java.util.LinkedHashMap; import java.util.Map; +import java.util.Set; -import org.junit.Before; import org.junit.Test; public class MapFirstPairUnitTest { - private MapFirstPairExample mapFirstPairExample; + private Map.Entry getFirstPairUsingIterator(Map map) { + if (map == null || map.size() == 0) + return null; - @Before - public void Setup() { + Iterator> iterator = map.entrySet() + .iterator(); - mapFirstPairExample = new MapFirstPairExample(); + if (iterator.hasNext()) + return iterator.next(); + + return null; + } + + private Map.Entry getFirstPairUsingStream(Map map) { + if (map == null || map.size() == 0) + return null; + + Set> entrySet = map.entrySet(); + + return entrySet.stream() + .findFirst() + .get(); + } + + private Map populateMapValues(Map map) { + if (map != null) { + map.put(5, "A"); + map.put(1, "B"); + map.put(2, "C"); + } + return map; } @Test public void whenUsingIteratorForHashMap_thenFirstPairWhichWasNotInsertedFirst() { Map hashMap = new HashMap<>(); - hashMap = mapFirstPairExample.populateMapValues(hashMap); + hashMap = populateMapValues(hashMap); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap); + Map.Entry actualValue = getFirstPairUsingIterator(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); @@ -37,8 +63,8 @@ public class MapFirstPairUnitTest { @Test public void whenUsingStreamForHashMap_thenFirstPairWhichWasNotInsertedFirst() { Map hashMap = new HashMap<>(); - hashMap = mapFirstPairExample.populateMapValues(hashMap); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap); + hashMap = populateMapValues(hashMap); + Map.Entry actualValue = getFirstPairUsingStream(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(1, "B"); Map.Entry pairInsertedFirst = new AbstractMap.SimpleEntry(5, "A"); @@ -49,8 +75,8 @@ public class MapFirstPairUnitTest { @Test public void whenUsingIteratorForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() { Map linkedHashMap = new LinkedHashMap<>(); - linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap); + linkedHashMap = populateMapValues(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingIterator(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -59,9 +85,9 @@ public class MapFirstPairUnitTest { @Test public void whenUsingStreamForLinkedHashMap_thenFirstPairWhichWasInsertedFirst() { Map linkedHashMap = new LinkedHashMap<>(); - linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); + linkedHashMap = populateMapValues(linkedHashMap); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingStream(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -70,10 +96,10 @@ public class MapFirstPairUnitTest { @Test public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingIterator() { Map hashMap = new HashMap<>(); - hashMap = mapFirstPairExample.populateMapValues(hashMap); + hashMap = populateMapValues(hashMap); hashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(hashMap); + Map.Entry actualValue = getFirstPairUsingIterator(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); assertEquals(expectedValue, actualValue); @@ -82,10 +108,10 @@ public class MapFirstPairUnitTest { @Test public void whenAddedAnElementInHashMap_thenFirstPairChangedUsingStream() { Map hashMap = new HashMap<>(); - hashMap = mapFirstPairExample.populateMapValues(hashMap); + hashMap = populateMapValues(hashMap); hashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(hashMap); + Map.Entry actualValue = getFirstPairUsingStream(hashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(0, "D"); assertEquals(expectedValue, actualValue); @@ -94,10 +120,10 @@ public class MapFirstPairUnitTest { @Test public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingIterator() { Map linkedHashMap = new LinkedHashMap<>(); - linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); + linkedHashMap = populateMapValues(linkedHashMap); linkedHashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingIterator(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingIterator(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); @@ -106,10 +132,10 @@ public class MapFirstPairUnitTest { @Test public void whenAddedAnElementInLinkedHashMap_thenFirstPairRemainUnchangedUsingStream() { Map linkedHashMap = new LinkedHashMap<>(); - linkedHashMap = mapFirstPairExample.populateMapValues(linkedHashMap); + linkedHashMap = populateMapValues(linkedHashMap); linkedHashMap.put(0, "D"); - Map.Entry actualValue = mapFirstPairExample.getFirstPairUsingStream(linkedHashMap); + Map.Entry actualValue = getFirstPairUsingStream(linkedHashMap); Map.Entry expectedValue = new AbstractMap.SimpleEntry(5, "A"); assertEquals(expectedValue, actualValue); From 19f754618ab43d2ee0ce3261292f883806620c67 Mon Sep 17 00:00:00 2001 From: fdpro Date: Thu, 10 Sep 2020 12:22:23 +0200 Subject: [PATCH 26/40] [JAVA-1662] Migrated maven-surefire-plugin version to 2.22.2 --- logging-modules/flogger/pom.xml | 1 + logging-modules/log-mdc/pom.xml | 11 ++++++++++- logging-modules/log4j/pom.xml | 4 ++-- logging-modules/log4j2/pom.xml | 4 ++-- logging-modules/logback/pom.xml | 4 ++-- logging-modules/pom.xml | 11 ++++++++++- 6 files changed, 27 insertions(+), 8 deletions(-) diff --git a/logging-modules/flogger/pom.xml b/logging-modules/flogger/pom.xml index f553a4a961..e9189c8460 100644 --- a/logging-modules/flogger/pom.xml +++ b/logging-modules/flogger/pom.xml @@ -9,6 +9,7 @@ com.baeldung logging-modules 1.0.0-SNAPSHOT + ../pom.xml diff --git a/logging-modules/log-mdc/pom.xml b/logging-modules/log-mdc/pom.xml index bc4800ea37..bc018690f9 100644 --- a/logging-modules/log-mdc/pom.xml +++ b/logging-modules/log-mdc/pom.xml @@ -79,6 +79,8 @@ + logging-service + @@ -93,7 +95,14 @@ - logging-service + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.2 + + diff --git a/logging-modules/log4j/pom.xml b/logging-modules/log4j/pom.xml index cc0996a45a..15cd2d530f 100644 --- a/logging-modules/log4j/pom.xml +++ b/logging-modules/log4j/pom.xml @@ -9,9 +9,9 @@ com.baeldung - parent-modules + logging-modules 1.0.0-SNAPSHOT - ../../ + ../pom.xml diff --git a/logging-modules/log4j2/pom.xml b/logging-modules/log4j2/pom.xml index 03a4fd8ab0..e09cbd5d33 100644 --- a/logging-modules/log4j2/pom.xml +++ b/logging-modules/log4j2/pom.xml @@ -7,9 +7,9 @@ com.baeldung - parent-modules + logging-modules 1.0.0-SNAPSHOT - ../../ + ../pom.xml diff --git a/logging-modules/logback/pom.xml b/logging-modules/logback/pom.xml index ee430949df..9f5a3ef294 100644 --- a/logging-modules/logback/pom.xml +++ b/logging-modules/logback/pom.xml @@ -9,9 +9,9 @@ com.baeldung - parent-modules + logging-modules 1.0.0-SNAPSHOT - ../../ + ../pom.xml diff --git a/logging-modules/pom.xml b/logging-modules/pom.xml index b9a1fe77c6..ad78fd20b9 100644 --- a/logging-modules/pom.xml +++ b/logging-modules/pom.xml @@ -10,7 +10,7 @@ com.baeldung parent-modules 1.0.0-SNAPSHOT - .. + ../pom.xml @@ -21,4 +21,13 @@ flogger + + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.2 + + + From 1fd989f4a8f731a2312838af839a5588cb1e4ab1 Mon Sep 17 00:00:00 2001 From: fdpro Date: Thu, 10 Sep 2020 12:26:56 +0200 Subject: [PATCH 27/40] [JAVA-1662] Upgraded junit-jupiter version to 5.6.2 --- logging-modules/log-mdc/pom.xml | 3 ++- logging-modules/pom.xml | 4 ++++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/logging-modules/log-mdc/pom.xml b/logging-modules/log-mdc/pom.xml index bc018690f9..5e2155fde9 100644 --- a/logging-modules/log-mdc/pom.xml +++ b/logging-modules/log-mdc/pom.xml @@ -80,7 +80,7 @@ logging-service - + @@ -109,6 +109,7 @@ 2.7 3.3.6 3.3.0.Final + 5.6.2 diff --git a/logging-modules/pom.xml b/logging-modules/pom.xml index ad78fd20b9..b5354c7c23 100644 --- a/logging-modules/pom.xml +++ b/logging-modules/pom.xml @@ -30,4 +30,8 @@ + + + 5.6.2 + From 64d5c717cb769ebfda351b2a82f682291d1d42bd Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Thu, 10 Sep 2020 22:43:55 +0530 Subject: [PATCH 28/40] JAVA-2412: Update "Spring Template Engine" article --- spring-mvc-basics-2/pom.xml | 4 ++-- .../baeldung/spring/configuration/EmailConfiguration.java | 4 ++-- .../spring/configuration/ThymeleafConfiguration.java | 6 +++--- .../java/com/baeldung/spring/mail/EmailServiceImpl.java | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/spring-mvc-basics-2/pom.xml b/spring-mvc-basics-2/pom.xml index 026ddf8e72..6bcb1e90e5 100644 --- a/spring-mvc-basics-2/pom.xml +++ b/spring-mvc-basics-2/pom.xml @@ -69,7 +69,7 @@ org.thymeleaf - thymeleaf-spring4 + thymeleaf-spring5 ${org.thymeleaf-version} @@ -164,7 +164,7 @@ 6.0.10.Final enter-location-of-server 1.3.2 - 3.0.7.RELEASE + 3.0.11.RELEASE 2.4.12 2.3.27-incubating 1.2.5 diff --git a/spring-mvc-basics-2/src/main/java/com/baeldung/spring/configuration/EmailConfiguration.java b/spring-mvc-basics-2/src/main/java/com/baeldung/spring/configuration/EmailConfiguration.java index 1bbbc51304..4bd692f609 100644 --- a/spring-mvc-basics-2/src/main/java/com/baeldung/spring/configuration/EmailConfiguration.java +++ b/spring-mvc-basics-2/src/main/java/com/baeldung/spring/configuration/EmailConfiguration.java @@ -11,8 +11,8 @@ import org.springframework.mail.javamail.JavaMailSender; import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver; -import org.thymeleaf.spring4.SpringTemplateEngine; -import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver; +import org.thymeleaf.spring5.SpringTemplateEngine; +import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; @Configuration @ComponentScan(basePackages = { "com.baeldung.spring.mail" }) diff --git a/spring-mvc-basics-2/src/main/java/com/baeldung/spring/configuration/ThymeleafConfiguration.java b/spring-mvc-basics-2/src/main/java/com/baeldung/spring/configuration/ThymeleafConfiguration.java index 257dbc718a..2f025c1ad2 100644 --- a/spring-mvc-basics-2/src/main/java/com/baeldung/spring/configuration/ThymeleafConfiguration.java +++ b/spring-mvc-basics-2/src/main/java/com/baeldung/spring/configuration/ThymeleafConfiguration.java @@ -4,9 +4,9 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; -import org.thymeleaf.spring4.SpringTemplateEngine; -import org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver; -import org.thymeleaf.spring4.view.ThymeleafViewResolver; +import org.thymeleaf.spring5.SpringTemplateEngine; +import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; +import org.thymeleaf.spring5.view.ThymeleafViewResolver; @Configuration @EnableWebMvc diff --git a/spring-mvc-basics-2/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java b/spring-mvc-basics-2/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java index cbcb8f4e34..1eb7a5f8b4 100644 --- a/spring-mvc-basics-2/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java +++ b/spring-mvc-basics-2/src/main/java/com/baeldung/spring/mail/EmailServiceImpl.java @@ -19,7 +19,7 @@ import org.springframework.stereotype.Service; import org.springframework.ui.freemarker.FreeMarkerTemplateUtils; import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer; import org.thymeleaf.context.Context; -import org.thymeleaf.spring4.SpringTemplateEngine; +import org.thymeleaf.spring5.SpringTemplateEngine; import freemarker.template.Template; import freemarker.template.TemplateException; From 2ccba495c562bfc2736448c6009d654210b5796a Mon Sep 17 00:00:00 2001 From: Loredana Date: Thu, 10 Sep 2020 20:19:19 +0300 Subject: [PATCH 29/40] fix package --- .../src/main/java/com/baeldung/optionalpathvars/Article.java | 2 +- .../baeldung/optionalpathvars/ArticleViewerController.java | 4 ++-- .../optionalpathvars/ArticleViewerWithMapParamController.java | 4 ++-- .../ArticleViewerWithOptionalParamController.java | 4 ++-- .../ArticleViewerWithRequiredAttributeController.java | 4 ++-- .../ArticleViewerWithTwoSeparateMethodsController.java | 4 ++-- .../ArticleViewerControllerIntegrationTest.java | 2 +- ...ticleViewerControllerWithOptionalParamIntegrationTest.java | 2 +- ...eViewerControllerWithRequiredAttributeIntegrationTest.java | 2 +- .../ArticleViewerWithMapParamIntegrationTest.java | 2 +- .../ArticleViewerWithTwoSeparateMethodsIntegrationTest.java | 2 +- 11 files changed, 16 insertions(+), 16 deletions(-) diff --git a/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/Article.java b/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/Article.java index f6675295ed..5a9f406b56 100644 --- a/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/Article.java +++ b/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/Article.java @@ -1,4 +1,4 @@ -package com.baeldung.controller.optionalpathvars; +package com.baeldung.optionalpathvars; public class Article { diff --git a/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerController.java b/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerController.java index 14b16e148b..1876798bd6 100644 --- a/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerController.java +++ b/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerController.java @@ -1,6 +1,6 @@ -package com.baeldung.controller.optionalpathvars; +package com.baeldung.optionalpathvars; -import static com.baeldung.controller.optionalpathvars.Article.DEFAULT_ARTICLE; +import static com.baeldung.optionalpathvars.Article.DEFAULT_ARTICLE; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithMapParamController.java b/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithMapParamController.java index 50744b6067..c989ddfa52 100644 --- a/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithMapParamController.java +++ b/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithMapParamController.java @@ -1,6 +1,6 @@ -package com.baeldung.controller.optionalpathvars; +package com.baeldung.optionalpathvars; -import static com.baeldung.controller.optionalpathvars.Article.DEFAULT_ARTICLE; +import static com.baeldung.optionalpathvars.Article.DEFAULT_ARTICLE; import java.util.Map; diff --git a/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithOptionalParamController.java b/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithOptionalParamController.java index ff645fbcc7..75e35bf799 100644 --- a/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithOptionalParamController.java +++ b/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithOptionalParamController.java @@ -1,6 +1,6 @@ -package com.baeldung.controller.optionalpathvars; +package com.baeldung.optionalpathvars; -import static com.baeldung.controller.optionalpathvars.Article.DEFAULT_ARTICLE; +import static com.baeldung.optionalpathvars.Article.DEFAULT_ARTICLE; import java.util.Optional; diff --git a/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithRequiredAttributeController.java b/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithRequiredAttributeController.java index 8cd1539391..7548747f05 100644 --- a/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithRequiredAttributeController.java +++ b/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithRequiredAttributeController.java @@ -1,6 +1,6 @@ -package com.baeldung.controller.optionalpathvars; +package com.baeldung.optionalpathvars; -import static com.baeldung.controller.optionalpathvars.Article.DEFAULT_ARTICLE; +import static com.baeldung.optionalpathvars.Article.DEFAULT_ARTICLE; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithTwoSeparateMethodsController.java b/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithTwoSeparateMethodsController.java index 0ea401a589..beb520c1b4 100644 --- a/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithTwoSeparateMethodsController.java +++ b/spring-mvc-basics-4/src/main/java/com/baeldung/optionalpathvars/ArticleViewerWithTwoSeparateMethodsController.java @@ -1,6 +1,6 @@ -package com.baeldung.controller.optionalpathvars; +package com.baeldung.optionalpathvars; -import static com.baeldung.controller.optionalpathvars.Article.DEFAULT_ARTICLE; +import static com.baeldung.optionalpathvars.Article.DEFAULT_ARTICLE; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; diff --git a/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerControllerIntegrationTest.java b/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerControllerIntegrationTest.java index 173ac165c3..0e2313c2ac 100644 --- a/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerControllerIntegrationTest.java +++ b/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerControllerIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.controller.optionalpathvars; +package com.baeldung.optionalpathvars; import org.junit.Before; import org.junit.Test; diff --git a/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerControllerWithOptionalParamIntegrationTest.java b/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerControllerWithOptionalParamIntegrationTest.java index 1fadfb7038..094995ba67 100644 --- a/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerControllerWithOptionalParamIntegrationTest.java +++ b/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerControllerWithOptionalParamIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.controller.optionalpathvars; +package com.baeldung.optionalpathvars; import org.junit.Before; import org.junit.Test; diff --git a/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerControllerWithRequiredAttributeIntegrationTest.java b/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerControllerWithRequiredAttributeIntegrationTest.java index 00d620ef9a..a4b12c7163 100644 --- a/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerControllerWithRequiredAttributeIntegrationTest.java +++ b/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerControllerWithRequiredAttributeIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.controller.optionalpathvars; +package com.baeldung.optionalpathvars; import org.junit.Before; import org.junit.Test; diff --git a/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerWithMapParamIntegrationTest.java b/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerWithMapParamIntegrationTest.java index f7fff714a9..044a1c8bce 100644 --- a/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerWithMapParamIntegrationTest.java +++ b/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerWithMapParamIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.controller.optionalpathvars; +package com.baeldung.optionalpathvars; import org.junit.Before; import org.junit.Test; diff --git a/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerWithTwoSeparateMethodsIntegrationTest.java b/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerWithTwoSeparateMethodsIntegrationTest.java index b4e4c9ade5..1ca926277d 100644 --- a/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerWithTwoSeparateMethodsIntegrationTest.java +++ b/spring-mvc-basics-4/src/test/java/com/baeldung/optionalpathvars/ArticleViewerWithTwoSeparateMethodsIntegrationTest.java @@ -1,4 +1,4 @@ -package com.baeldung.controller.optionalpathvars; +package com.baeldung.optionalpathvars; import org.junit.Before; import org.junit.Test; From c2ac81100963b02f318de566bfb0d7aaaf389151 Mon Sep 17 00:00:00 2001 From: Ashley Frieze Date: Sat, 29 Aug 2020 00:07:53 +0100 Subject: [PATCH 30/40] BAEL-3998-Aws Lambda with Hibernate --- aws-lambda/lambda/pom.xml | 100 ++++++++++++++++ .../sam-templates/template-implicit.yaml | 0 .../template-inline-swagger.yaml | 0 .../baeldung/lambda/LambdaMethodHandler.java | 0 .../baeldung/lambda/LambdaRequestHandler.java | 0 .../lambda/LambdaRequestStreamHandler.java | 0 .../lambda/apigateway/APIDemoHandler.java | 0 .../lambda/apigateway/model/Person.java | 0 .../lambda/dynamodb/SavePersonHandler.java | 0 .../lambda/dynamodb/bean/PersonRequest.java | 0 .../lambda/dynamodb/bean/PersonResponse.java | 0 .../src/main/resources/logback.xml | 0 aws-lambda/pom.xml | 90 ++------------- aws-lambda/shipping-tracker/.gitignore | 1 + .../shipping-tracker/ShippingFunction/pom.xml | 73 ++++++++++++ .../com/baeldung/lambda/shipping/App.java | 108 ++++++++++++++++++ .../com/baeldung/lambda/shipping/Checkin.java | 28 +++++ .../baeldung/lambda/shipping/Consignment.java | 77 +++++++++++++ .../com/baeldung/lambda/shipping/Item.java | 38 ++++++ .../baeldung/lambda/shipping/ShippingDao.java | 25 ++++ .../lambda/shipping/ShippingService.java | 62 ++++++++++ aws-lambda/shipping-tracker/template.yaml | 47 ++++++++ 22 files changed, 566 insertions(+), 83 deletions(-) create mode 100644 aws-lambda/lambda/pom.xml rename aws-lambda/{ => lambda}/sam-templates/template-implicit.yaml (100%) rename aws-lambda/{ => lambda}/sam-templates/template-inline-swagger.yaml (100%) rename aws-lambda/{ => lambda}/src/main/java/com/baeldung/lambda/LambdaMethodHandler.java (100%) rename aws-lambda/{ => lambda}/src/main/java/com/baeldung/lambda/LambdaRequestHandler.java (100%) rename aws-lambda/{ => lambda}/src/main/java/com/baeldung/lambda/LambdaRequestStreamHandler.java (100%) rename aws-lambda/{ => lambda}/src/main/java/com/baeldung/lambda/apigateway/APIDemoHandler.java (100%) rename aws-lambda/{ => lambda}/src/main/java/com/baeldung/lambda/apigateway/model/Person.java (100%) rename aws-lambda/{ => lambda}/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java (100%) rename aws-lambda/{ => lambda}/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java (100%) rename aws-lambda/{ => lambda}/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java (100%) rename aws-lambda/{ => lambda}/src/main/resources/logback.xml (100%) create mode 100644 aws-lambda/shipping-tracker/.gitignore create mode 100644 aws-lambda/shipping-tracker/ShippingFunction/pom.xml create mode 100644 aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/App.java create mode 100644 aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/Checkin.java create mode 100644 aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/Consignment.java create mode 100644 aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/Item.java create mode 100644 aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/ShippingDao.java create mode 100644 aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/ShippingService.java create mode 100644 aws-lambda/shipping-tracker/template.yaml diff --git a/aws-lambda/lambda/pom.xml b/aws-lambda/lambda/pom.xml new file mode 100644 index 0000000000..2d903aabc5 --- /dev/null +++ b/aws-lambda/lambda/pom.xml @@ -0,0 +1,100 @@ + + + 4.0.0 + aws-lambda-examples + 0.1.0-SNAPSHOT + aws-lambda-examples + jar + + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + ../../ + + + + + com.amazonaws + aws-java-sdk-dynamodb + ${aws-java-sdk.version} + + + com.amazonaws + aws-java-sdk-core + ${aws-java-sdk.version} + + + com.amazonaws + aws-lambda-java-core + ${aws-lambda-java-core.version} + + + commons-logging + commons-logging + + + + + com.amazonaws + aws-lambda-java-events + ${aws-lambda-java-events.version} + + + commons-logging + commons-logging + + + + + com.google.code.gson + gson + ${gson.version} + + + commons-io + commons-io + ${commons-io.version} + + + com.googlecode.json-simple + json-simple + ${json-simple.version} + + + + + + + org.apache.maven.plugins + maven-shade-plugin + ${maven-shade-plugin.version} + + false + + + + package + + shade + + + + + + + + + 1.1.1 + 2.5 + 1.3.0 + 1.2.0 + 2.8.2 + 1.11.241 + 3.0.0 + + + diff --git a/aws-lambda/sam-templates/template-implicit.yaml b/aws-lambda/lambda/sam-templates/template-implicit.yaml similarity index 100% rename from aws-lambda/sam-templates/template-implicit.yaml rename to aws-lambda/lambda/sam-templates/template-implicit.yaml diff --git a/aws-lambda/sam-templates/template-inline-swagger.yaml b/aws-lambda/lambda/sam-templates/template-inline-swagger.yaml similarity index 100% rename from aws-lambda/sam-templates/template-inline-swagger.yaml rename to aws-lambda/lambda/sam-templates/template-inline-swagger.yaml diff --git a/aws-lambda/src/main/java/com/baeldung/lambda/LambdaMethodHandler.java b/aws-lambda/lambda/src/main/java/com/baeldung/lambda/LambdaMethodHandler.java similarity index 100% rename from aws-lambda/src/main/java/com/baeldung/lambda/LambdaMethodHandler.java rename to aws-lambda/lambda/src/main/java/com/baeldung/lambda/LambdaMethodHandler.java diff --git a/aws-lambda/src/main/java/com/baeldung/lambda/LambdaRequestHandler.java b/aws-lambda/lambda/src/main/java/com/baeldung/lambda/LambdaRequestHandler.java similarity index 100% rename from aws-lambda/src/main/java/com/baeldung/lambda/LambdaRequestHandler.java rename to aws-lambda/lambda/src/main/java/com/baeldung/lambda/LambdaRequestHandler.java diff --git a/aws-lambda/src/main/java/com/baeldung/lambda/LambdaRequestStreamHandler.java b/aws-lambda/lambda/src/main/java/com/baeldung/lambda/LambdaRequestStreamHandler.java similarity index 100% rename from aws-lambda/src/main/java/com/baeldung/lambda/LambdaRequestStreamHandler.java rename to aws-lambda/lambda/src/main/java/com/baeldung/lambda/LambdaRequestStreamHandler.java diff --git a/aws-lambda/src/main/java/com/baeldung/lambda/apigateway/APIDemoHandler.java b/aws-lambda/lambda/src/main/java/com/baeldung/lambda/apigateway/APIDemoHandler.java similarity index 100% rename from aws-lambda/src/main/java/com/baeldung/lambda/apigateway/APIDemoHandler.java rename to aws-lambda/lambda/src/main/java/com/baeldung/lambda/apigateway/APIDemoHandler.java diff --git a/aws-lambda/src/main/java/com/baeldung/lambda/apigateway/model/Person.java b/aws-lambda/lambda/src/main/java/com/baeldung/lambda/apigateway/model/Person.java similarity index 100% rename from aws-lambda/src/main/java/com/baeldung/lambda/apigateway/model/Person.java rename to aws-lambda/lambda/src/main/java/com/baeldung/lambda/apigateway/model/Person.java diff --git a/aws-lambda/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java b/aws-lambda/lambda/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java similarity index 100% rename from aws-lambda/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java rename to aws-lambda/lambda/src/main/java/com/baeldung/lambda/dynamodb/SavePersonHandler.java diff --git a/aws-lambda/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java b/aws-lambda/lambda/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java similarity index 100% rename from aws-lambda/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java rename to aws-lambda/lambda/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonRequest.java diff --git a/aws-lambda/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java b/aws-lambda/lambda/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java similarity index 100% rename from aws-lambda/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java rename to aws-lambda/lambda/src/main/java/com/baeldung/lambda/dynamodb/bean/PersonResponse.java diff --git a/aws-lambda/src/main/resources/logback.xml b/aws-lambda/lambda/src/main/resources/logback.xml similarity index 100% rename from aws-lambda/src/main/resources/logback.xml rename to aws-lambda/lambda/src/main/resources/logback.xml diff --git a/aws-lambda/pom.xml b/aws-lambda/pom.xml index e1d2c7df27..116fc801aa 100644 --- a/aws-lambda/pom.xml +++ b/aws-lambda/pom.xml @@ -5,95 +5,19 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 aws-lambda - 0.1.0-SNAPSHOT aws-lambda - jar + pom com.baeldung parent-modules 1.0.0-SNAPSHOT + ../ - - - com.amazonaws - aws-java-sdk-dynamodb - ${aws-java-sdk.version} - - - com.amazonaws - aws-java-sdk-core - ${aws-java-sdk.version} - - - com.amazonaws - aws-lambda-java-core - ${aws-lambda-java-core.version} - - - commons-logging - commons-logging - - - - - com.amazonaws - aws-lambda-java-events - ${aws-lambda-java-events.version} - - - commons-logging - commons-logging - - - - - com.google.code.gson - gson - ${gson.version} - - - commons-io - commons-io - ${commons-io.version} - - - com.googlecode.json-simple - json-simple - ${json-simple.version} - - + + lambda + shipping-tracker/ShippingFunction + - - - - org.apache.maven.plugins - maven-shade-plugin - ${maven-shade-plugin.version} - - false - - - - package - - shade - - - - - - - - - 1.1.1 - 2.5 - 1.3.0 - 1.2.0 - 2.8.2 - 1.11.241 - 3.0.0 - - - \ No newline at end of file + diff --git a/aws-lambda/shipping-tracker/.gitignore b/aws-lambda/shipping-tracker/.gitignore new file mode 100644 index 0000000000..9984c2e554 --- /dev/null +++ b/aws-lambda/shipping-tracker/.gitignore @@ -0,0 +1 @@ +.aws-sam/ diff --git a/aws-lambda/shipping-tracker/ShippingFunction/pom.xml b/aws-lambda/shipping-tracker/ShippingFunction/pom.xml new file mode 100644 index 0000000000..ac39c9ea54 --- /dev/null +++ b/aws-lambda/shipping-tracker/ShippingFunction/pom.xml @@ -0,0 +1,73 @@ + + 4.0.0 + com.baeldung + ShippingFunction + 1.0 + jar + Shipping Tracker Lambda Function + + 1.8 + 1.8 + 5.4.21.Final + + + + + com.amazonaws + aws-lambda-java-core + 1.2.0 + + + com.amazonaws + aws-lambda-java-events + 3.1.0 + + + com.fasterxml.jackson.core + jackson-databind + 2.11.2 + + + junit + junit + 4.12 + test + + + org.hibernate + hibernate-core + ${hibernate.version} + + + org.hibernate + hibernate-hikaricp + ${hibernate.version} + + + org.postgresql + postgresql + 42.2.16 + + + + + + + org.apache.maven.plugins + maven-shade-plugin + 3.1.1 + + + + + package + + shade + + + + + + + diff --git a/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/App.java b/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/App.java new file mode 100644 index 0000000000..719725598c --- /dev/null +++ b/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/App.java @@ -0,0 +1,108 @@ +package com.baeldung.lambda.shipping; + +import com.amazonaws.services.lambda.runtime.Context; +import com.amazonaws.services.lambda.runtime.RequestHandler; +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent; +import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import org.hibernate.SessionFactory; +import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.StandardServiceRegistry; +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; + +import java.util.HashMap; +import java.util.Map; + +import static org.hibernate.cfg.AvailableSettings.*; +import static org.hibernate.cfg.AvailableSettings.PASS; + +/** + * Handler for requests to Lambda function. + */ +public class App implements RequestHandler { +private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper(); + + public APIGatewayProxyResponseEvent handleRequest(APIGatewayProxyRequestEvent input, Context context) { + try (SessionFactory sessionFactory = createSessionFactory()) { + ShippingService service = new ShippingService(sessionFactory, new ShippingDao()); + return routeRequest(input, service); + } + } + + private APIGatewayProxyResponseEvent routeRequest(APIGatewayProxyRequestEvent input, ShippingService service) { + Map headers = new HashMap<>(); + headers.put("Content-Type", "application/json"); + headers.put("X-Custom-Header", "application/json"); + + Object result = "OK"; + + switch (input.getResource()) { + case "/consignment": + result = service.createConsignment( + fromJson(input.getBody(), Consignment.class)); + break; + case "/consignment/{id}": + result = service.view(input.getPathParameters().get("id")); + break; + case "/consignment/{id}/item": + service.addItem(input.getPathParameters().get("id"), + fromJson(input.getBody(), Item.class)); + break; + case "/consignment/{id}/checkin": + service.checkIn(input.getPathParameters().get("id"), + fromJson(input.getBody(), Checkin.class)); + break; + } + + return new APIGatewayProxyResponseEvent() + .withHeaders(headers) + .withStatusCode(200) + .withBody(toJson(result)); + } + + private static String toJson(T object) { + try { + return OBJECT_MAPPER.writeValueAsString(object); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + + private static T fromJson(String json, Class type) { + try { + return OBJECT_MAPPER.readValue(json, type); + } catch (JsonProcessingException e) { + throw new RuntimeException(e); + } + } + + private static SessionFactory createSessionFactory() { + Map settings = new HashMap<>(); + settings.put(URL, System.getenv("DB_URL")); + settings.put(DIALECT, "org.hibernate.dialect.PostgreSQLDialect"); + settings.put(DEFAULT_SCHEMA, "shipping"); + settings.put(DRIVER, "org.postgresql.Driver"); + settings.put(USER, System.getenv("DB_USER")); + settings.put(PASS, System.getenv("DB_PASSWORD")); + settings.put("hibernate.hikari.connectionTimeout", "20000"); + settings.put("hibernate.hikari.minimumIdle", "1"); + settings.put("hibernate.hikari.maximumPoolSize", "2"); + settings.put("hibernate.hikari.idleTimeout", "30000"); + +// commented out as we only need them on first use +// settings.put(HBM2DDL_AUTO, "create-only"); +// settings.put(HBM2DDL_DATABASE_ACTION, "create"); + + StandardServiceRegistry registry = new StandardServiceRegistryBuilder() + .applySettings(settings) + .build(); + + return new MetadataSources(registry) + .addAnnotatedClass(Consignment.class) + .addAnnotatedClass(Item.class) + .addAnnotatedClass(Checkin.class) + .buildMetadata() + .buildSessionFactory(); + } +} diff --git a/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/Checkin.java b/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/Checkin.java new file mode 100644 index 0000000000..93e6404749 --- /dev/null +++ b/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/Checkin.java @@ -0,0 +1,28 @@ +package com.baeldung.lambda.shipping; + +import javax.persistence.Column; +import javax.persistence.Embeddable; + +@Embeddable +public class Checkin { + private String timeStamp; + private String location; + + @Column(name = "timestamp") + public String getTimeStamp() { + return timeStamp; + } + + public void setTimeStamp(String timeStamp) { + this.timeStamp = timeStamp; + } + + @Column(name = "location") + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } +} diff --git a/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/Consignment.java b/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/Consignment.java new file mode 100644 index 0000000000..1a2371b37f --- /dev/null +++ b/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/Consignment.java @@ -0,0 +1,77 @@ +package com.baeldung.lambda.shipping; + +import javax.persistence.*; +import java.util.ArrayList; +import java.util.List; + +import static javax.persistence.FetchType.EAGER; + +@Entity(name = "consignment") +@Table(name = "consignment") +public class Consignment { + private String id; + private String source; + private String destination; + private boolean isDelivered; + private List items = new ArrayList<>(); + private List checkins = new ArrayList<>(); + + @Id + @Column(name = "consignment_id") + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + @Column(name = "source") + public String getSource() { + return source; + } + + public void setSource(String source) { + this.source = source; + } + + @Column(name = "destination") + public String getDestination() { + return destination; + } + + public void setDestination(String destination) { + this.destination = destination; + } + + @Column(name = "delivered", columnDefinition = "boolean") + public boolean isDelivered() { + return isDelivered; + } + + public void setDelivered(boolean delivered) { + isDelivered = delivered; + } + + @ElementCollection(fetch = EAGER) + @CollectionTable(name = "consignment_item", joinColumns = @JoinColumn(name = "consignment_id")) + @OrderColumn(name = "item_index") + public List getItems() { + return items; + } + + public void setItems(List items) { + this.items = items; + } + + @ElementCollection(fetch = EAGER) + @CollectionTable(name = "consignment_checkin", joinColumns = @JoinColumn(name = "consignment_id")) + @OrderColumn(name = "checkin_index") + public List getCheckins() { + return checkins; + } + + public void setCheckins(List checkins) { + this.checkins = checkins; + } +} diff --git a/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/Item.java b/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/Item.java new file mode 100644 index 0000000000..de6194e180 --- /dev/null +++ b/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/Item.java @@ -0,0 +1,38 @@ +package com.baeldung.lambda.shipping; + +import javax.persistence.Column; +import javax.persistence.Embeddable; + +@Embeddable +public class Item { + private String location; + private String description; + private String timeStamp; + + @Column(name = "location") + public String getLocation() { + return location; + } + + public void setLocation(String location) { + this.location = location; + } + + @Column(name = "description") + public String getDescription() { + return description; + } + + public void setDescription(String description) { + this.description = description; + } + + @Column(name = "timestamp") + public String getTimeStamp() { + return timeStamp; + } + + public void setTimeStamp(String timeStamp) { + this.timeStamp = timeStamp; + } +} diff --git a/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/ShippingDao.java b/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/ShippingDao.java new file mode 100644 index 0000000000..369dc33935 --- /dev/null +++ b/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/ShippingDao.java @@ -0,0 +1,25 @@ +package com.baeldung.lambda.shipping; + +import org.hibernate.Session; +import org.hibernate.Transaction; + +import java.util.Optional; + +public class ShippingDao { + /** + * Save a consignment to the database + * @param consignment the consignment to save + */ + public void save(Session session, Consignment consignment) { + Transaction transaction = session.beginTransaction(); + session.save(consignment); + transaction.commit(); + } + + /** + * Find a consignment in the database by id + */ + public Optional find(Session session, String id) { + return Optional.ofNullable(session.get(Consignment.class, id)); + } +} diff --git a/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/ShippingService.java b/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/ShippingService.java new file mode 100644 index 0000000000..4c951068ea --- /dev/null +++ b/aws-lambda/shipping-tracker/ShippingFunction/src/main/java/com/baeldung/lambda/shipping/ShippingService.java @@ -0,0 +1,62 @@ +package com.baeldung.lambda.shipping; + +import org.hibernate.Session; +import org.hibernate.SessionFactory; + +import java.util.Comparator; +import java.util.UUID; + +public class ShippingService { + private SessionFactory sessionFactory; + private ShippingDao shippingDao; + + public ShippingService(SessionFactory sessionFactory, ShippingDao shippingDao) { + this.sessionFactory = sessionFactory; + this.shippingDao = shippingDao; + } + + public String createConsignment(Consignment consignment) { + try (Session session = sessionFactory.openSession()) { + consignment.setDelivered(false); + consignment.setId(UUID.randomUUID().toString()); + shippingDao.save(session, consignment); + return consignment.getId(); + } + } + + public void addItem(String consignmentId, Item item) { + try (Session session = sessionFactory.openSession()) { + shippingDao.find(session, consignmentId) + .ifPresent(consignment -> addItem(session, consignment, item)); + } + } + + private void addItem(Session session, Consignment consignment, Item item) { + consignment.getItems() + .add(item); + shippingDao.save(session, consignment); + } + + public void checkIn(String consignmentId, Checkin checkin) { + try (Session session = sessionFactory.openSession()) { + shippingDao.find(session, consignmentId) + .ifPresent(consignment -> checkIn(session, consignment, checkin)); + } + } + + private void checkIn(Session session, Consignment consignment, Checkin checkin) { + consignment.getCheckins().add(checkin); + consignment.getCheckins().sort(Comparator.comparing(Checkin::getTimeStamp)); + if (checkin.getLocation().equals(consignment.getDestination())) { + consignment.setDelivered(true); + } + shippingDao.save(session, consignment); + } + + public Consignment view(String consignmentId) { + try (Session session = sessionFactory.openSession()) { + return shippingDao.find(session, consignmentId) + .orElseGet(Consignment::new); + } + } +} diff --git a/aws-lambda/shipping-tracker/template.yaml b/aws-lambda/shipping-tracker/template.yaml new file mode 100644 index 0000000000..ec75c51ba1 --- /dev/null +++ b/aws-lambda/shipping-tracker/template.yaml @@ -0,0 +1,47 @@ +AWSTemplateFormatVersion: '2010-09-09' +Transform: AWS::Serverless-2016-10-31 +Description: > + shipping-tracker + + Sample SAM Template for shipping-tracker + +# More info about Globals: https://github.com/awslabs/serverless-application-model/blob/master/docs/globals.rst +Globals: + Function: + Timeout: 20 + +Resources: + ShippingFunction: + Type: AWS::Serverless::Function + Properties: + CodeUri: ShippingFunction + Handler: com.baeldung.lambda.shipping.App::handleRequest + Runtime: java8 + MemorySize: 512 + Environment: + Variables: + DB_URL: jdbc:postgresql://postgres/postgres + DB_USER: postgres + DB_PASSWORD: password + Events: + CreateConsignment: + Type: Api + Properties: + Path: /consignment + Method: post + AddItem: + Type: Api + Properties: + Path: /consignment/{id}/item + Method: post + CheckIn: + Type: Api + Properties: + Path: /consignment/{id}/checkin + Method: post + ViewConsignment: + Type: Api + Properties: + Path: /consignment/{id} + Method: get + From 4893af40f61ce902ea60a897a55e586661fb3972 Mon Sep 17 00:00:00 2001 From: Sebastian Luna Date: Thu, 10 Sep 2020 19:55:56 -0500 Subject: [PATCH 31/40] BAEL-4387 Add AssertJ assertions --- java-collections-conversions-2/pom.xml | 6 ++++++ .../ArrayToListConversionUnitTest.java | 14 +++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/java-collections-conversions-2/pom.xml b/java-collections-conversions-2/pom.xml index 0f7cdadeb2..23f20276a3 100644 --- a/java-collections-conversions-2/pom.xml +++ b/java-collections-conversions-2/pom.xml @@ -15,6 +15,12 @@ + + org.assertj + assertj-core + 3.17.2 + test + org.apache.commons commons-lang3 diff --git a/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversionUnitTest.java b/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversionUnitTest.java index 80e7b93b0c..565c938d48 100644 --- a/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversionUnitTest.java +++ b/java-collections-conversions-2/src/test/java/com/baeldung/arrayconversion/ArrayToListConversionUnitTest.java @@ -1,5 +1,6 @@ package com.baeldung.arrayconversion; +import org.assertj.core.api.ListAssert; import org.hamcrest.CoreMatchers; import org.junit.Test; @@ -7,8 +8,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.Assert.assertArrayEquals; +import static org.assertj.core.api.Assertions.assertThat; public class ArrayToListConversionUnitTest { @@ -17,8 +17,8 @@ public class ArrayToListConversionUnitTest { String[] stringArray = new String[] { "A", "B", "C", "D" }; List stringList = Arrays.asList(stringArray); stringList.set(0, "E"); - assertThat(stringList, CoreMatchers.hasItems("E", "B", "C", "D")); - assertArrayEquals(stringArray, new String[] { "E", "B", "C", "D" }); + assertThat(stringList).containsExactly("E", "B", "C", "D"); + assertThat(stringArray).containsExactly("E", "B", "C", "D"); stringList.add("F"); } @@ -27,9 +27,9 @@ public class ArrayToListConversionUnitTest { String[] stringArray = new String[] { "A", "B", "C", "D" }; List stringList = new ArrayList<>(Arrays.asList(stringArray)); stringList.set(0, "E"); - assertThat(stringList, CoreMatchers.hasItems("E", "B", "C", "D")); - assertArrayEquals(stringArray, new String[] { "A", "B", "C", "D" }); + assertThat(stringList).containsExactly("E", "B", "C", "D"); + assertThat(stringArray).containsExactly("A", "B", "C", "D"); stringList.add("F"); - assertThat(stringList, CoreMatchers.hasItems("E", "B", "C", "D", "F")); + assertThat(stringList).containsExactly("E", "B", "C", "D", "F"); } } From c3cd93fcdbdd4033dda418c8ac0bad89aacedb63 Mon Sep 17 00:00:00 2001 From: kwoyke Date: Fri, 11 Sep 2020 14:49:02 +0200 Subject: [PATCH 32/40] JAVA-2563: Upgrade Spring Boot version to 2.3.3.RELEASE (#9985) * JAVA-2563: Upgrade Spring Boot version to 2.3.3.RELEASE * JAVA-2563: Downgrade spring-cloud-connectors-heroku to Spring Boot 2.2.6.RELEASE * JAVA-2563: Add joda-time version * JAVA-2563: Add spring-boot-starter-validation dependency * JAVA-2563: Add spring-boot-starter-validation dependency * JAVA-2563: Add spring-boot-starter-validation dependency * JAVA-2563: Add spring-boot-starter-validation dependency * JAVA-2563: Add spring-boot-starter-validation dependency * JAVA-2563: Use MongoClients factory instead of MongoClient directly * JAVA-2563: Downgrade spring-5-data-reactive to Spring Boot 2.2.6.RELEASE * JAVA-2563: Switch back to default bootstrap mode for JPA * JAVA-2563: Add spring-boot-starter-validation dependency * JAVA-2563: Switch from ServerHttpRequest to ServerWebExchange interface * JAVA-2563: Use OutputCaptureRule instead of OutputCapture * JAVA-2563: Add spring-boot-starter-validation dependency * JAVA-2563: Add spring-boot-starter-validation dependency * JAVA-2563: Add spring-boot-starter-validation dependency * JAVA-2563: Fix Jackson dependency * JAVA-2563: Fix ManualEmbeddedMongoDbIntegrationTest * JAVA-2563: Replace validation-api with spring-boot-starter-validation * JAVA-2563: Fix usage of deprecated getErrorAttributes method * JAVA-2563: Downgrade spring-data-cassandra-reactive to Spring Boot 2.2.6.RELEASE * JAVA-2563: Set spring.datasource.generate-unique-name to false in spring-session-jdbc --- ddd/pom.xml | 4 +++ jmeter/pom.xml | 4 +++ parent-boot-2/pom.xml | 2 +- persistence-modules/r2dbc/pom.xml | 4 +++ .../ManualEmbeddedMongoDbIntegrationTest.java | 25 ++++++++++--------- .../spring-data-cassandra-reactive/pom.xml | 1 + .../src/main/resources/application.properties | 2 +- spring-5-data-reactive/pom.xml | 1 + spring-5-reactive-2/pom.xml | 4 +++ .../authresolver/CustomWebSecurityConfig.java | 9 ++++--- .../errorhandling/GlobalErrorAttributes.java | 10 +++----- .../GlobalErrorWebExceptionHandler.java | 3 ++- .../spring-boot-angular/pom.xml | 4 +++ .../spring-boot-autoconfiguration/pom.xml | 4 +++ spring-boot-modules/spring-boot-crud/pom.xml | 4 +++ spring-boot-modules/spring-boot-mvc-2/pom.xml | 5 ++++ .../spring-boot-properties/pom.xml | 4 +++ .../spring-boot-runtime/pom.xml | 5 ++++ .../spring-boot-springdoc/pom.xml | 4 +++ .../spring-boot-testing/pom.xml | 4 +++ ...ltiProfileTestLogLevelIntegrationTest.java | 4 +-- .../LogbackTestLogLevelIntegrationTest.java | 4 +-- ...estLogLevelWithProfileIntegrationTest.java | 4 +-- spring-boot-modules/spring-boot/pom.xml | 9 +++---- .../spring-cloud-connectors-heroku/pom.xml | 1 + spring-cloud/spring-cloud-vault/pom.xml | 4 +++ spring-jenkins-pipeline/pom.xml | 4 +++ spring-mvc-basics-3/pom.xml | 5 ++++ spring-mvc-basics/pom.xml | 4 +++ spring-mvc-java/pom.xml | 6 ----- spring-rest-hal-browser/pom.xml | 4 +++ spring-rest-http/pom.xml | 4 +++ .../src/main/resources/application.properties | 1 + spring-thymeleaf-2/pom.xml | 4 +++ spring-thymeleaf-3/pom.xml | 4 +++ testing-modules/rest-assured/pom.xml | 1 + 36 files changed, 124 insertions(+), 42 deletions(-) diff --git a/ddd/pom.xml b/ddd/pom.xml index 9f960502a3..a67719f8a6 100644 --- a/ddd/pom.xml +++ b/ddd/pom.xml @@ -78,6 +78,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-test diff --git a/jmeter/pom.xml b/jmeter/pom.xml index 945210edd7..e2830baef5 100644 --- a/jmeter/pom.xml +++ b/jmeter/pom.xml @@ -23,6 +23,10 @@ org.springframework.boot spring-boot-starter-data-rest + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-test diff --git a/parent-boot-2/pom.xml b/parent-boot-2/pom.xml index ed0f327b8c..dab9f015b3 100644 --- a/parent-boot-2/pom.xml +++ b/parent-boot-2/pom.xml @@ -82,7 +82,7 @@ 3.3.0 1.0.22.RELEASE - 2.2.6.RELEASE + 2.3.3.RELEASE 1.9.1 diff --git a/persistence-modules/r2dbc/pom.xml b/persistence-modules/r2dbc/pom.xml index 2da81cba06..119d0547e3 100644 --- a/persistence-modules/r2dbc/pom.xml +++ b/persistence-modules/r2dbc/pom.xml @@ -21,6 +21,10 @@ org.springframework.boot spring-boot-starter-webflux + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot diff --git a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/mongodb/ManualEmbeddedMongoDbIntegrationTest.java b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/mongodb/ManualEmbeddedMongoDbIntegrationTest.java index c49b99ed99..21cf56172e 100644 --- a/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/mongodb/ManualEmbeddedMongoDbIntegrationTest.java +++ b/persistence-modules/spring-boot-persistence-mongodb/src/test/java/com/baeldung/mongodb/ManualEmbeddedMongoDbIntegrationTest.java @@ -1,18 +1,8 @@ package com.baeldung.mongodb; -import static org.assertj.core.api.Assertions.assertThat; - -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.DisplayName; -import org.junit.jupiter.api.Test; -import org.springframework.data.mongodb.core.MongoTemplate; -import org.springframework.util.SocketUtils; - import com.mongodb.BasicDBObjectBuilder; import com.mongodb.DBObject; -import com.mongodb.MongoClient; - +import com.mongodb.client.MongoClients; import de.flapdoodle.embed.mongo.MongodExecutable; import de.flapdoodle.embed.mongo.MongodStarter; import de.flapdoodle.embed.mongo.config.IMongodConfig; @@ -20,8 +10,19 @@ import de.flapdoodle.embed.mongo.config.MongodConfigBuilder; import de.flapdoodle.embed.mongo.config.Net; import de.flapdoodle.embed.mongo.distribution.Version; import de.flapdoodle.embed.process.runtime.Network; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.util.SocketUtils; + +import static org.assertj.core.api.Assertions.assertThat; class ManualEmbeddedMongoDbIntegrationTest { + + private static final String CONNECTION_STRING = "mongodb://%s:%d"; + private MongodExecutable mongodExecutable; private MongoTemplate mongoTemplate; @@ -42,7 +43,7 @@ class ManualEmbeddedMongoDbIntegrationTest { MongodStarter starter = MongodStarter.getDefaultInstance(); mongodExecutable = starter.prepare(mongodConfig); mongodExecutable.start(); - mongoTemplate = new MongoTemplate(new MongoClient(ip, randomPort), "test"); + mongoTemplate = new MongoTemplate(MongoClients.create(String.format(CONNECTION_STRING, ip, randomPort)),"test"); } @DisplayName("Given object When save object using MongoDB template Then object can be found") diff --git a/persistence-modules/spring-data-cassandra-reactive/pom.xml b/persistence-modules/spring-data-cassandra-reactive/pom.xml index 70a5f556e2..16486bf380 100644 --- a/persistence-modules/spring-data-cassandra-reactive/pom.xml +++ b/persistence-modules/spring-data-cassandra-reactive/pom.xml @@ -51,6 +51,7 @@ + 2.2.6.RELEASE 3.11.2.0 diff --git a/software-security/sql-injection-samples/src/main/resources/application.properties b/software-security/sql-injection-samples/src/main/resources/application.properties index 8b13789179..953384eac4 100644 --- a/software-security/sql-injection-samples/src/main/resources/application.properties +++ b/software-security/sql-injection-samples/src/main/resources/application.properties @@ -1 +1 @@ - +spring.data.jpa.repositories.bootstrap-mode=default diff --git a/spring-5-data-reactive/pom.xml b/spring-5-data-reactive/pom.xml index aeaf6daf1a..396f7f5959 100644 --- a/spring-5-data-reactive/pom.xml +++ b/spring-5-data-reactive/pom.xml @@ -224,6 +224,7 @@ 1.4.200 1.5.23 3.3.1.RELEASE + 2.2.6.RELEASE diff --git a/spring-5-reactive-2/pom.xml b/spring-5-reactive-2/pom.xml index 4cb85d879e..093be0f03c 100644 --- a/spring-5-reactive-2/pom.xml +++ b/spring-5-reactive-2/pom.xml @@ -20,6 +20,10 @@ org.springframework.boot spring-boot-starter-webflux + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-security diff --git a/spring-5-reactive-security/src/main/java/com/baeldung/reactive/authresolver/CustomWebSecurityConfig.java b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/authresolver/CustomWebSecurityConfig.java index d07a991089..dc5eab3dd5 100644 --- a/spring-5-reactive-security/src/main/java/com/baeldung/reactive/authresolver/CustomWebSecurityConfig.java +++ b/spring-5-reactive-security/src/main/java/com/baeldung/reactive/authresolver/CustomWebSecurityConfig.java @@ -2,7 +2,6 @@ package com.baeldung.reactive.authresolver; import java.util.Collections; import org.springframework.context.annotation.Bean; -import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.security.authentication.ReactiveAuthenticationManager; import org.springframework.security.authentication.ReactiveAuthenticationManagerResolver; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; @@ -15,6 +14,7 @@ import org.springframework.security.core.authority.SimpleGrantedAuthority; import org.springframework.security.core.userdetails.UsernameNotFoundException; import org.springframework.security.web.server.SecurityWebFilterChain; import org.springframework.security.web.server.authentication.AuthenticationWebFilter; +import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; @EnableWebFluxSecurity @@ -38,9 +38,10 @@ public class CustomWebSecurityConfig { return new AuthenticationWebFilter(resolver()); } - public ReactiveAuthenticationManagerResolver resolver() { - return request -> { - if (request + public ReactiveAuthenticationManagerResolver resolver() { + return exchange -> { + if (exchange + .getRequest() .getPath() .subPath(0) .value() diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/errorhandling/GlobalErrorAttributes.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/errorhandling/GlobalErrorAttributes.java index a50651ced7..5885ac50d0 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/errorhandling/GlobalErrorAttributes.java +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/errorhandling/GlobalErrorAttributes.java @@ -2,6 +2,8 @@ package com.baeldung.reactive.errorhandling; import java.util.Map; + +import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.reactive.error.DefaultErrorAttributes; import org.springframework.http.HttpStatus; import org.springframework.stereotype.Component; @@ -13,13 +15,9 @@ public class GlobalErrorAttributes extends DefaultErrorAttributes{ private HttpStatus status = HttpStatus.BAD_REQUEST; private String message = "please provide a name"; - public GlobalErrorAttributes() { - super(false); - } - @Override - public Map getErrorAttributes(ServerRequest request, boolean includeStackTrace) { - Map map = super.getErrorAttributes(request, includeStackTrace); + public Map getErrorAttributes(ServerRequest request, ErrorAttributeOptions options) { + Map map = super.getErrorAttributes(request, options); map.put("status", getStatus()); map.put("message", getMessage()); return map; diff --git a/spring-5-reactive/src/main/java/com/baeldung/reactive/errorhandling/GlobalErrorWebExceptionHandler.java b/spring-5-reactive/src/main/java/com/baeldung/reactive/errorhandling/GlobalErrorWebExceptionHandler.java index 09bccb0d5e..051e4b8df5 100644 --- a/spring-5-reactive/src/main/java/com/baeldung/reactive/errorhandling/GlobalErrorWebExceptionHandler.java +++ b/spring-5-reactive/src/main/java/com/baeldung/reactive/errorhandling/GlobalErrorWebExceptionHandler.java @@ -4,6 +4,7 @@ package com.baeldung.reactive.errorhandling; import java.util.Map; import org.springframework.boot.autoconfigure.web.ResourceProperties; import org.springframework.boot.autoconfigure.web.reactive.error.AbstractErrorWebExceptionHandler; +import org.springframework.boot.web.error.ErrorAttributeOptions; import org.springframework.boot.web.reactive.error.ErrorAttributes; import org.springframework.context.ApplicationContext; import org.springframework.core.annotation.Order; @@ -37,7 +38,7 @@ public class GlobalErrorWebExceptionHandler extends AbstractErrorWebExceptionHan private Mono renderErrorResponse(final ServerRequest request) { - final Map errorPropertiesMap = getErrorAttributes(request, false); + final Map errorPropertiesMap = getErrorAttributes(request, ErrorAttributeOptions.defaults()); return ServerResponse.status(HttpStatus.BAD_REQUEST) .contentType(MediaType.APPLICATION_JSON_UTF8) diff --git a/spring-boot-modules/spring-boot-angular/pom.xml b/spring-boot-modules/spring-boot-angular/pom.xml index 4b3ac27834..ac63d21bb8 100644 --- a/spring-boot-modules/spring-boot-angular/pom.xml +++ b/spring-boot-modules/spring-boot-angular/pom.xml @@ -26,6 +26,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + com.h2database h2 diff --git a/spring-boot-modules/spring-boot-autoconfiguration/pom.xml b/spring-boot-modules/spring-boot-autoconfiguration/pom.xml index 5709d1d796..269d87bbb9 100644 --- a/spring-boot-modules/spring-boot-autoconfiguration/pom.xml +++ b/spring-boot-modules/spring-boot-autoconfiguration/pom.xml @@ -26,6 +26,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-data-jpa diff --git a/spring-boot-modules/spring-boot-crud/pom.xml b/spring-boot-modules/spring-boot-crud/pom.xml index a4be360b0f..cf1bfe6da0 100644 --- a/spring-boot-modules/spring-boot-crud/pom.xml +++ b/spring-boot-modules/spring-boot-crud/pom.xml @@ -20,6 +20,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-thymeleaf diff --git a/spring-boot-modules/spring-boot-mvc-2/pom.xml b/spring-boot-modules/spring-boot-mvc-2/pom.xml index 0b9213a7ea..580224cfd0 100644 --- a/spring-boot-modules/spring-boot-mvc-2/pom.xml +++ b/spring-boot-modules/spring-boot-mvc-2/pom.xml @@ -23,6 +23,11 @@ spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + + org.springframework.boot spring-boot-devtools diff --git a/spring-boot-modules/spring-boot-properties/pom.xml b/spring-boot-modules/spring-boot-properties/pom.xml index a5594ee2de..cfdc71b8d6 100644 --- a/spring-boot-modules/spring-boot-properties/pom.xml +++ b/spring-boot-modules/spring-boot-properties/pom.xml @@ -27,6 +27,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-actuator diff --git a/spring-boot-modules/spring-boot-runtime/pom.xml b/spring-boot-modules/spring-boot-runtime/pom.xml index d3966beb65..ce6fa7ea93 100644 --- a/spring-boot-modules/spring-boot-runtime/pom.xml +++ b/spring-boot-modules/spring-boot-runtime/pom.xml @@ -29,6 +29,11 @@ spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + + org.springframework.boot spring-boot-starter-thymeleaf diff --git a/spring-boot-modules/spring-boot-springdoc/pom.xml b/spring-boot-modules/spring-boot-springdoc/pom.xml index 3e8d5175f7..ed272200da 100644 --- a/spring-boot-modules/spring-boot-springdoc/pom.xml +++ b/spring-boot-modules/spring-boot-springdoc/pom.xml @@ -24,6 +24,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-data-jpa diff --git a/spring-boot-modules/spring-boot-testing/pom.xml b/spring-boot-modules/spring-boot-testing/pom.xml index bd5ef901dd..5bf626f165 100644 --- a/spring-boot-modules/spring-boot-testing/pom.xml +++ b/spring-boot-modules/spring-boot-testing/pom.xml @@ -26,6 +26,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-tomcat diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java index ffe99672be..b3d80a7d8b 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackMultiProfileTestLogLevelIntegrationTest.java @@ -8,7 +8,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -29,7 +29,7 @@ public class LogbackMultiProfileTestLogLevelIntegrationTest { private TestRestTemplate restTemplate; @Rule - public OutputCapture outputCapture = new OutputCapture(); + public OutputCaptureRule outputCapture = new OutputCaptureRule(); private String baseUrl = "/testLogLevel"; diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java index cbd22e8087..f60a5e0ee3 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/LogbackTestLogLevelIntegrationTest.java @@ -8,7 +8,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -29,7 +29,7 @@ public class LogbackTestLogLevelIntegrationTest { private TestRestTemplate restTemplate; @Rule - public OutputCapture outputCapture = new OutputCapture(); + public OutputCaptureRule outputCapture = new OutputCaptureRule(); private String baseUrl = "/testLogLevel"; diff --git a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java index 571b826b80..e59b673a1c 100644 --- a/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java +++ b/spring-boot-modules/spring-boot-testing/src/test/java/com/baeldung/testloglevel/TestLogLevelWithProfileIntegrationTest.java @@ -8,7 +8,7 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.boot.test.context.SpringBootTest.WebEnvironment; -import org.springframework.boot.test.rule.OutputCapture; +import org.springframework.boot.test.system.OutputCaptureRule; import org.springframework.boot.test.web.client.TestRestTemplate; import org.springframework.http.ResponseEntity; import org.springframework.test.annotation.DirtiesContext; @@ -29,7 +29,7 @@ public class TestLogLevelWithProfileIntegrationTest { private TestRestTemplate restTemplate; @Rule - public OutputCapture outputCapture = new OutputCapture(); + public OutputCaptureRule outputCapture = new OutputCaptureRule(); private String baseUrl = "/testLogLevel"; diff --git a/spring-boot-modules/spring-boot/pom.xml b/spring-boot-modules/spring-boot/pom.xml index 5efcffdf03..c1f1ea3072 100644 --- a/spring-boot-modules/spring-boot/pom.xml +++ b/spring-boot-modules/spring-boot/pom.xml @@ -27,6 +27,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-data-jpa @@ -98,11 +102,6 @@ rome ${rome.version} - - - javax.validation - validation-api - diff --git a/spring-cloud/spring-cloud-connectors-heroku/pom.xml b/spring-cloud/spring-cloud-connectors-heroku/pom.xml index c09a282197..7d85e07bb8 100644 --- a/spring-cloud/spring-cloud-connectors-heroku/pom.xml +++ b/spring-cloud/spring-cloud-connectors-heroku/pom.xml @@ -60,6 +60,7 @@ + 2.2.6.RELEASE Hoxton.SR4 42.2.10 1.10.10 diff --git a/spring-cloud/spring-cloud-vault/pom.xml b/spring-cloud/spring-cloud-vault/pom.xml index a5a29d9024..d9ae6b515f 100644 --- a/spring-cloud/spring-cloud-vault/pom.xml +++ b/spring-cloud/spring-cloud-vault/pom.xml @@ -62,6 +62,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-data-jpa diff --git a/spring-jenkins-pipeline/pom.xml b/spring-jenkins-pipeline/pom.xml index 38d4ed15de..152e107409 100644 --- a/spring-jenkins-pipeline/pom.xml +++ b/spring-jenkins-pipeline/pom.xml @@ -24,6 +24,10 @@ org.springframework.boot spring-boot-starter-data-rest + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-test diff --git a/spring-mvc-basics-3/pom.xml b/spring-mvc-basics-3/pom.xml index 1dea8f9e93..a929337b25 100644 --- a/spring-mvc-basics-3/pom.xml +++ b/spring-mvc-basics-3/pom.xml @@ -21,6 +21,11 @@ spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + + org.springframework.boot spring-boot-starter-test diff --git a/spring-mvc-basics/pom.xml b/spring-mvc-basics/pom.xml index 159dda955f..d212bc425a 100644 --- a/spring-mvc-basics/pom.xml +++ b/spring-mvc-basics/pom.xml @@ -20,6 +20,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.apache.tomcat.embed diff --git a/spring-mvc-java/pom.xml b/spring-mvc-java/pom.xml index 9e3457aa8a..a45e9c8521 100644 --- a/spring-mvc-java/pom.xml +++ b/spring-mvc-java/pom.xml @@ -42,12 +42,6 @@ tomcat-embed-jasper provided - - - com.fasterxml.jackson.core - jackson-databind - ${jackson.version} - net.sourceforge.htmlunit diff --git a/spring-rest-hal-browser/pom.xml b/spring-rest-hal-browser/pom.xml index 7b629dba44..c8066b89a4 100644 --- a/spring-rest-hal-browser/pom.xml +++ b/spring-rest-hal-browser/pom.xml @@ -20,6 +20,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot diff --git a/spring-rest-http/pom.xml b/spring-rest-http/pom.xml index 32d2804220..18b7e0af05 100644 --- a/spring-rest-http/pom.xml +++ b/spring-rest-http/pom.xml @@ -20,6 +20,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework spring-oxm diff --git a/spring-session/spring-session-jdbc/src/main/resources/application.properties b/spring-session/spring-session-jdbc/src/main/resources/application.properties index 119638de31..bb9dd24590 100644 --- a/spring-session/spring-session-jdbc/src/main/resources/application.properties +++ b/spring-session/spring-session-jdbc/src/main/resources/application.properties @@ -1,3 +1,4 @@ +spring.datasource.generate-unique-name=false spring.session.store-type=jdbc spring.h2.console.enabled=true spring.h2.console.path=/h2-console \ No newline at end of file diff --git a/spring-thymeleaf-2/pom.xml b/spring-thymeleaf-2/pom.xml index 24c159dab9..43f36d9887 100644 --- a/spring-thymeleaf-2/pom.xml +++ b/spring-thymeleaf-2/pom.xml @@ -18,6 +18,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-thymeleaf diff --git a/spring-thymeleaf-3/pom.xml b/spring-thymeleaf-3/pom.xml index 7677e50d79..7c58115d11 100644 --- a/spring-thymeleaf-3/pom.xml +++ b/spring-thymeleaf-3/pom.xml @@ -18,6 +18,10 @@ org.springframework.boot spring-boot-starter-web + + org.springframework.boot + spring-boot-starter-validation + org.springframework.boot spring-boot-starter-thymeleaf diff --git a/testing-modules/rest-assured/pom.xml b/testing-modules/rest-assured/pom.xml index 0b027312d6..eeb5389f49 100644 --- a/testing-modules/rest-assured/pom.xml +++ b/testing-modules/rest-assured/pom.xml @@ -102,6 +102,7 @@ joda-time joda-time + ${joda-time.version} From ebf5019635ff6745b4947b42e1ba26798f626b6e Mon Sep 17 00:00:00 2001 From: kwoyke Date: Sat, 12 Sep 2020 09:44:47 +0200 Subject: [PATCH 33/40] BAEL-4589: Update Guide to Java 8's Collectors (#10009) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * BAEL-4589: Move Guide to Java 8’s Collectors to core-java-11-2 * BAEL-4589: Add Java 10 unmodifiable* collectors examples --- core-java-modules/core-java-11-2/README.md | 1 + core-java-modules/core-java-11-2/pom.xml | 6 +++ .../collectors/Java8CollectorsUnitTest.java | 50 +++++++++++-------- .../core-java-streams-3/README.md | 1 - 4 files changed, 35 insertions(+), 23 deletions(-) rename core-java-modules/{core-java-streams-3/src/test/java/com/baeldung/streams => core-java-11-2/src/test/java/com/baeldung}/collectors/Java8CollectorsUnitTest.java (87%) diff --git a/core-java-modules/core-java-11-2/README.md b/core-java-modules/core-java-11-2/README.md index f65a043819..d77bf748a8 100644 --- a/core-java-modules/core-java-11-2/README.md +++ b/core-java-modules/core-java-11-2/README.md @@ -4,4 +4,5 @@ This module contains articles about Java 11 core features ### Relevant articles - [Guide to Java 8 Optional](https://www.baeldung.com/java-optional) +- [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors) diff --git a/core-java-modules/core-java-11-2/pom.xml b/core-java-modules/core-java-11-2/pom.xml index d20b0f23f0..e2b129ae00 100644 --- a/core-java-modules/core-java-11-2/pom.xml +++ b/core-java-modules/core-java-11-2/pom.xml @@ -17,6 +17,11 @@ + + com.google.guava + guava + ${guava.version} + org.assertj assertj-core @@ -42,6 +47,7 @@ 11 11 + 29.0-jre 3.17.2 diff --git a/core-java-modules/core-java-streams-3/src/test/java/com/baeldung/streams/collectors/Java8CollectorsUnitTest.java b/core-java-modules/core-java-11-2/src/test/java/com/baeldung/collectors/Java8CollectorsUnitTest.java similarity index 87% rename from core-java-modules/core-java-streams-3/src/test/java/com/baeldung/streams/collectors/Java8CollectorsUnitTest.java rename to core-java-modules/core-java-11-2/src/test/java/com/baeldung/collectors/Java8CollectorsUnitTest.java index 6afc9f4182..7990b1fdb7 100644 --- a/core-java-modules/core-java-streams-3/src/test/java/com/baeldung/streams/collectors/Java8CollectorsUnitTest.java +++ b/core-java-modules/core-java-11-2/src/test/java/com/baeldung/collectors/Java8CollectorsUnitTest.java @@ -1,18 +1,11 @@ -package com.baeldung.streams.collectors; +package com.baeldung.collectors; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Sets; import org.junit.Test; -import java.util.Arrays; -import java.util.Comparator; -import java.util.DoubleSummaryStatistics; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; +import java.util.*; import java.util.function.BiConsumer; import java.util.function.BinaryOperator; import java.util.function.Function; @@ -20,19 +13,7 @@ import java.util.function.Supplier; import java.util.stream.Collector; import static com.google.common.collect.Sets.newHashSet; -import static java.util.stream.Collectors.averagingDouble; -import static java.util.stream.Collectors.collectingAndThen; -import static java.util.stream.Collectors.counting; -import static java.util.stream.Collectors.groupingBy; -import static java.util.stream.Collectors.joining; -import static java.util.stream.Collectors.maxBy; -import static java.util.stream.Collectors.partitioningBy; -import static java.util.stream.Collectors.summarizingDouble; -import static java.util.stream.Collectors.summingDouble; -import static java.util.stream.Collectors.toCollection; -import static java.util.stream.Collectors.toList; -import static java.util.stream.Collectors.toMap; -import static java.util.stream.Collectors.toSet; +import static java.util.stream.Collectors.*; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -48,6 +29,14 @@ public class Java8CollectorsUnitTest { assertThat(result).containsAll(givenList); } + @Test + public void whenCollectingToUnmodifiableList_shouldCollectToUnmodifiableList() { + final List result = givenList.stream().collect(toUnmodifiableList()); + + assertThatThrownBy(() -> result.add("foo")) + .isInstanceOf(UnsupportedOperationException.class); + } + @Test public void whenCollectingToSet_shouldCollectToSet() throws Exception { final Set result = givenList.stream().collect(toSet()); @@ -55,6 +44,14 @@ public class Java8CollectorsUnitTest { assertThat(result).containsAll(givenList); } + @Test + public void whenCollectingToUnmodifiableSet_shouldCollectToUnmodifiableSet() { + final Set result = givenList.stream().collect(toUnmodifiableSet()); + + assertThatThrownBy(() -> result.add("foo")) + .isInstanceOf(UnsupportedOperationException.class); + } + @Test public void givenContainsDuplicateElements_whenCollectingToSet_shouldAddDuplicateElementsOnlyOnce() throws Exception { final Set result = listWithDuplicates.stream().collect(toSet()); @@ -84,6 +81,15 @@ public class Java8CollectorsUnitTest { assertThat(result).containsEntry("a", 1).containsEntry("bb", 2).containsEntry("ccc", 3).containsEntry("dd", 2); } + @Test + public void whenCollectingToUnmodifiableMap_shouldCollectToUnmodifiableMap() { + final Map result = givenList.stream() + .collect(toUnmodifiableMap(Function.identity(), String::length)); + + assertThatThrownBy(() -> result.put("foo", 3)) + .isInstanceOf(UnsupportedOperationException.class); + } + @Test public void whenCollectingToMapwWithDuplicates_shouldCollectToMapMergingTheIdenticalItems() throws Exception { final Map result = listWithDuplicates.stream().collect( diff --git a/core-java-modules/core-java-streams-3/README.md b/core-java-modules/core-java-streams-3/README.md index 65713aa04f..9adde005e6 100644 --- a/core-java-modules/core-java-streams-3/README.md +++ b/core-java-modules/core-java-streams-3/README.md @@ -6,7 +6,6 @@ This module contains articles about the Stream API in Java. - [The Difference Between map() and flatMap()](https://www.baeldung.com/java-difference-map-and-flatmap) - [How to Use if/else Logic in Java 8 Streams](https://www.baeldung.com/java-8-streams-if-else-logic) - [The Difference Between Collection.stream().forEach() and Collection.forEach()](https://www.baeldung.com/java-collection-stream-foreach) -- [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors) - [Primitive Type Streams in Java 8](https://www.baeldung.com/java-8-primitive-streams) - [Debugging Java 8 Streams with IntelliJ](https://www.baeldung.com/intellij-debugging-java-streams) - [Add BigDecimals using the Stream API](https://www.baeldung.com/java-stream-add-bigdecimals) From 8f19db031e2f999d9dde8b3b9c587e2ed6c3dcf6 Mon Sep 17 00:00:00 2001 From: sampadawagde Date: Sat, 12 Sep 2020 17:54:33 +0530 Subject: [PATCH 34/40] JAVA-2411: Update spring-thymeleaf module and articles --- spring-thymeleaf/pom.xml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-thymeleaf/pom.xml b/spring-thymeleaf/pom.xml index c37c66a36d..30f77dd73e 100644 --- a/spring-thymeleaf/pom.xml +++ b/spring-thymeleaf/pom.xml @@ -145,10 +145,10 @@ - 2.0.9.RELEASE - 3.0.9.RELEASE - 3.0.1.RELEASE - 2.3.0 + 2.3.2.RELEASE + 3.0.11.RELEASE + 3.0.4.RELEASE + 2.4.1 2.0.1.Final 6.0.11.Final From d910807be21a6973092f1b91738154033c6b7a24 Mon Sep 17 00:00:00 2001 From: Sorin Zamfir Date: Sat, 12 Sep 2020 18:59:15 +0300 Subject: [PATCH 35/40] Update gradle-5/cmd-line-args/build.gradle Co-authored-by: KevinGilmore --- gradle-5/cmd-line-args/build.gradle | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle-5/cmd-line-args/build.gradle b/gradle-5/cmd-line-args/build.gradle index 5399db4815..15c9288024 100644 --- a/gradle-5/cmd-line-args/build.gradle +++ b/gradle-5/cmd-line-args/build.gradle @@ -33,6 +33,6 @@ task cmdLineJavaExec(type: JavaExec) { task cmdLineExec(type: Exec) { group = "Execution" - description = "Run the an external program with ExecTask" + description = "Run an external program with ExecTask" commandLine cmdargs.split() } From 972ff966996282d8bfc4bf34dcf03592a53ea58c Mon Sep 17 00:00:00 2001 From: kwoyke Date: Sat, 12 Sep 2020 19:08:22 +0200 Subject: [PATCH 36/40] BAEL-4587: Update Guide to Java Reflection (#9997) * BAEL-4587: Migrate Guide to Java Reflection to core-java-11-2 * BAEL-4587: Replace deprecated isAccessible method with canAccess --- core-java-modules/core-java-11-2/README.md | 2 +- .../java/com/baeldung}/reflection/Animal.java | 2 +- .../java/com/baeldung}/reflection/Bird.java | 2 +- .../baeldung}/reflection/DynamicGreeter.java | 2 +- .../java/com/baeldung}/reflection/Eating.java | 2 +- .../java/com/baeldung}/reflection/Goat.java | 2 +- .../com/baeldung}/reflection/Greeter.java | 2 +- .../reflection/GreetingAnnotation.java | 2 +- .../com/baeldung}/reflection/Greetings.java | 2 +- .../com/baeldung}/reflection/Locomotion.java | 2 +- .../com/baeldung}/reflection/Operations.java | 2 +- .../java/com/baeldung}/reflection/Person.java | 2 +- .../reflection/OperationsUnitTest.java | 2 +- .../reflection/ReflectionUnitTest.java | 73 +++++++++---------- .../core-java-reflection/README.MD | 1 - 15 files changed, 46 insertions(+), 54 deletions(-) rename core-java-modules/{core-java-reflection/src/main/java/com/baeldung/reflection/java => core-java-11-2/src/main/java/com/baeldung}/reflection/Animal.java (90%) rename core-java-modules/{core-java-reflection/src/main/java/com/baeldung/reflection/java => core-java-11-2/src/main/java/com/baeldung}/reflection/Bird.java (93%) rename core-java-modules/{core-java-reflection/src/main/java/com/baeldung/reflection/java => core-java-11-2/src/main/java/com/baeldung}/reflection/DynamicGreeter.java (91%) rename core-java-modules/{core-java-reflection/src/main/java/com/baeldung/reflection/java => core-java-11-2/src/main/java/com/baeldung}/reflection/Eating.java (55%) rename core-java-modules/{core-java-reflection/src/main/java/com/baeldung/reflection/java => core-java-11-2/src/main/java/com/baeldung}/reflection/Goat.java (90%) rename core-java-modules/{core-java-reflection/src/main/java/com/baeldung/reflection/java => core-java-11-2/src/main/java/com/baeldung}/reflection/Greeter.java (83%) rename core-java-modules/{core-java-reflection/src/main/java/com/baeldung/reflection/java => core-java-11-2/src/main/java/com/baeldung}/reflection/GreetingAnnotation.java (98%) rename core-java-modules/{core-java-reflection/src/main/java/com/baeldung/reflection/java => core-java-11-2/src/main/java/com/baeldung}/reflection/Greetings.java (61%) rename core-java-modules/{core-java-reflection/src/main/java/com/baeldung/reflection/java => core-java-11-2/src/main/java/com/baeldung}/reflection/Locomotion.java (61%) rename core-java-modules/{core-java-reflection/src/main/java/com/baeldung/reflection/java => core-java-11-2/src/main/java/com/baeldung}/reflection/Operations.java (90%) rename core-java-modules/{core-java-reflection/src/main/java/com/baeldung/reflection/java => core-java-11-2/src/main/java/com/baeldung}/reflection/Person.java (65%) rename core-java-modules/{core-java-reflection/src/test/java/com/baeldung/reflection/java => core-java-11-2/src/test/java/com/baeldung}/reflection/OperationsUnitTest.java (98%) rename core-java-modules/{core-java-reflection/src/test/java/com/baeldung/reflection/java => core-java-11-2/src/test/java/com/baeldung}/reflection/ReflectionUnitTest.java (77%) diff --git a/core-java-modules/core-java-11-2/README.md b/core-java-modules/core-java-11-2/README.md index d77bf748a8..834f310fce 100644 --- a/core-java-modules/core-java-11-2/README.md +++ b/core-java-modules/core-java-11-2/README.md @@ -4,5 +4,5 @@ This module contains articles about Java 11 core features ### Relevant articles - [Guide to Java 8 Optional](https://www.baeldung.com/java-optional) +- [Guide to Java Reflection](http://www.baeldung.com/java-reflection) - [Guide to Java 8’s Collectors](https://www.baeldung.com/java-8-collectors) - diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Animal.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Animal.java similarity index 90% rename from core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Animal.java rename to core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Animal.java index 3f36243c29..364246ae64 100644 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Animal.java +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Animal.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; public abstract class Animal implements Eating { diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Bird.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Bird.java similarity index 93% rename from core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Bird.java rename to core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Bird.java index bd6f13094c..f5bb0f9b19 100644 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Bird.java +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Bird.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; public class Bird extends Animal { private boolean walks; diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/DynamicGreeter.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/DynamicGreeter.java similarity index 91% rename from core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/DynamicGreeter.java rename to core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/DynamicGreeter.java index 3776ef82e2..b7ff083daf 100644 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/DynamicGreeter.java +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/DynamicGreeter.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; import java.lang.annotation.Annotation; diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Eating.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Eating.java similarity index 55% rename from core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Eating.java rename to core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Eating.java index 479425cad4..c959becf00 100644 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Eating.java +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Eating.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; public interface Eating { String eats(); diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Goat.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Goat.java similarity index 90% rename from core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Goat.java rename to core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Goat.java index 503717ae5e..086d09d543 100644 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Goat.java +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Goat.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; public class Goat extends Animal implements Locomotion { diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Greeter.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Greeter.java similarity index 83% rename from core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Greeter.java rename to core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Greeter.java index 57aefdd169..d06a719312 100644 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Greeter.java +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Greeter.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/GreetingAnnotation.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/GreetingAnnotation.java similarity index 98% rename from core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/GreetingAnnotation.java rename to core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/GreetingAnnotation.java index 601306f5d2..f23c407c52 100644 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/GreetingAnnotation.java +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/GreetingAnnotation.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; import java.lang.annotation.Annotation; import java.lang.reflect.Field; diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Greetings.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Greetings.java similarity index 61% rename from core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Greetings.java rename to core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Greetings.java index 4f3a20c3b9..fc6dfe949b 100644 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Greetings.java +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Greetings.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; @Greeter(greet="Good morning") public class Greetings { diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Locomotion.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Locomotion.java similarity index 61% rename from core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Locomotion.java rename to core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Locomotion.java index 047c00cb13..230fd9a466 100644 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Locomotion.java +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Locomotion.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; public interface Locomotion { String getLocomotion(); diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Operations.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Operations.java similarity index 90% rename from core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Operations.java rename to core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Operations.java index da4b479b02..5264378524 100644 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Operations.java +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Operations.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; public class Operations { diff --git a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Person.java b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Person.java similarity index 65% rename from core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Person.java rename to core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Person.java index f3d7f9f001..1a1fafef93 100644 --- a/core-java-modules/core-java-reflection/src/main/java/com/baeldung/reflection/java/reflection/Person.java +++ b/core-java-modules/core-java-11-2/src/main/java/com/baeldung/reflection/Person.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; public class Person { private String name; diff --git a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/OperationsUnitTest.java b/core-java-modules/core-java-11-2/src/test/java/com/baeldung/reflection/OperationsUnitTest.java similarity index 98% rename from core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/OperationsUnitTest.java rename to core-java-modules/core-java-11-2/src/test/java/com/baeldung/reflection/OperationsUnitTest.java index 217910bffd..7584d5da94 100644 --- a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/OperationsUnitTest.java +++ b/core-java-modules/core-java-11-2/src/test/java/com/baeldung/reflection/OperationsUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; import static org.hamcrest.CoreMatchers.equalTo; import static org.junit.Assert.assertFalse; diff --git a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/ReflectionUnitTest.java b/core-java-modules/core-java-11-2/src/test/java/com/baeldung/reflection/ReflectionUnitTest.java similarity index 77% rename from core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/ReflectionUnitTest.java rename to core-java-modules/core-java-11-2/src/test/java/com/baeldung/reflection/ReflectionUnitTest.java index a791d64874..c73fa5f8e0 100644 --- a/core-java-modules/core-java-reflection/src/test/java/com/baeldung/reflection/java/reflection/ReflectionUnitTest.java +++ b/core-java-modules/core-java-11-2/src/test/java/com/baeldung/reflection/ReflectionUnitTest.java @@ -1,4 +1,4 @@ -package com.baeldung.java.reflection; +package com.baeldung.reflection; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -32,23 +32,23 @@ public class ReflectionUnitTest { final Class clazz = goat.getClass(); assertEquals("Goat", clazz.getSimpleName()); - assertEquals("com.baeldung.java.reflection.Goat", clazz.getName()); - assertEquals("com.baeldung.java.reflection.Goat", clazz.getCanonicalName()); + assertEquals("com.baeldung.reflection.Goat", clazz.getName()); + assertEquals("com.baeldung.reflection.Goat", clazz.getCanonicalName()); } @Test public void givenClassName_whenCreatesObject_thenCorrect() throws ClassNotFoundException { - final Class clazz = Class.forName("com.baeldung.java.reflection.Goat"); + final Class clazz = Class.forName("com.baeldung.reflection.Goat"); assertEquals("Goat", clazz.getSimpleName()); - assertEquals("com.baeldung.java.reflection.Goat", clazz.getName()); - assertEquals("com.baeldung.java.reflection.Goat", clazz.getCanonicalName()); + assertEquals("com.baeldung.reflection.Goat", clazz.getName()); + assertEquals("com.baeldung.reflection.Goat", clazz.getCanonicalName()); } @Test public void givenClass_whenRecognisesModifiers_thenCorrect() throws ClassNotFoundException { - final Class goatClass = Class.forName("com.baeldung.java.reflection.Goat"); - final Class animalClass = Class.forName("com.baeldung.java.reflection.Animal"); + final Class goatClass = Class.forName("com.baeldung.reflection.Goat"); + final Class animalClass = Class.forName("com.baeldung.reflection.Animal"); final int goatMods = goatClass.getModifiers(); final int animalMods = animalClass.getModifiers(); @@ -63,7 +63,7 @@ public class ReflectionUnitTest { final Class goatClass = goat.getClass(); final Package pkg = goatClass.getPackage(); - assertEquals("com.baeldung.java.reflection", pkg.getName()); + assertEquals("com.baeldung.reflection", pkg.getName()); } @Test @@ -81,8 +81,8 @@ public class ReflectionUnitTest { @Test public void givenClass_whenGetsImplementedInterfaces_thenCorrect() throws ClassNotFoundException { - final Class goatClass = Class.forName("com.baeldung.java.reflection.Goat"); - final Class animalClass = Class.forName("com.baeldung.java.reflection.Animal"); + final Class goatClass = Class.forName("com.baeldung.reflection.Goat"); + final Class animalClass = Class.forName("com.baeldung.reflection.Animal"); final Class[] goatInterfaces = goatClass.getInterfaces(); final Class[] animalInterfaces = animalClass.getInterfaces(); @@ -94,16 +94,16 @@ public class ReflectionUnitTest { @Test public void givenClass_whenGetsConstructor_thenCorrect() throws ClassNotFoundException { - final Class goatClass = Class.forName("com.baeldung.java.reflection.Goat"); + final Class goatClass = Class.forName("com.baeldung.reflection.Goat"); final Constructor[] constructors = goatClass.getConstructors(); assertEquals(1, constructors.length); - assertEquals("com.baeldung.java.reflection.Goat", constructors[0].getName()); + assertEquals("com.baeldung.reflection.Goat", constructors[0].getName()); } @Test public void givenClass_whenGetsFields_thenCorrect() throws ClassNotFoundException { - final Class animalClass = Class.forName("com.baeldung.java.reflection.Animal"); + final Class animalClass = Class.forName("com.baeldung.reflection.Animal"); final Field[] fields = animalClass.getDeclaredFields(); final List actualFields = getFieldNames(fields); @@ -114,7 +114,7 @@ public class ReflectionUnitTest { @Test public void givenClass_whenGetsMethods_thenCorrect() throws ClassNotFoundException { - final Class animalClass = Class.forName("com.baeldung.java.reflection.Animal"); + final Class animalClass = Class.forName("com.baeldung.reflection.Animal"); final Method[] methods = animalClass.getDeclaredMethods(); final List actualMethods = getMethodNames(methods); @@ -124,7 +124,7 @@ public class ReflectionUnitTest { @Test public void givenClass_whenGetsAllConstructors_thenCorrect() throws ClassNotFoundException { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); + final Class birdClass = Class.forName("com.baeldung.reflection.Bird"); final Constructor[] constructors = birdClass.getConstructors(); assertEquals(3, constructors.length); @@ -132,7 +132,7 @@ public class ReflectionUnitTest { @Test public void givenClass_whenGetsEachConstructorByParamTypes_thenCorrect() throws Exception { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); + final Class birdClass = Class.forName("com.baeldung.reflection.Bird"); birdClass.getConstructor(); birdClass.getConstructor(String.class); birdClass.getConstructor(String.class, boolean.class); @@ -140,7 +140,7 @@ public class ReflectionUnitTest { @Test public void givenClass_whenInstantiatesObjectsAtRuntime_thenCorrect() throws Exception { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); + final Class birdClass = Class.forName("com.baeldung.reflection.Bird"); final Constructor cons1 = birdClass.getConstructor(); final Constructor cons2 = birdClass.getConstructor(String.class); @@ -159,7 +159,7 @@ public class ReflectionUnitTest { @Test public void givenClass_whenGetsPublicFields_thenCorrect() throws ClassNotFoundException { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); + final Class birdClass = Class.forName("com.baeldung.reflection.Bird"); final Field[] fields = birdClass.getFields(); assertEquals(1, fields.length); assertEquals("CATEGORY", fields[0].getName()); @@ -168,7 +168,7 @@ public class ReflectionUnitTest { @Test public void givenClass_whenGetsPublicFieldByName_thenCorrect() throws Exception { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); + final Class birdClass = Class.forName("com.baeldung.reflection.Bird"); final Field field = birdClass.getField("CATEGORY"); assertEquals("CATEGORY", field.getName()); @@ -176,7 +176,7 @@ public class ReflectionUnitTest { @Test public void givenClass_whenGetsDeclaredFields_thenCorrect() throws ClassNotFoundException { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); + final Class birdClass = Class.forName("com.baeldung.reflection.Bird"); final Field[] fields = birdClass.getDeclaredFields(); assertEquals(1, fields.length); assertEquals("walks", fields[0].getName()); @@ -184,7 +184,7 @@ public class ReflectionUnitTest { @Test public void givenClass_whenGetsFieldsByName_thenCorrect() throws Exception { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); + final Class birdClass = Class.forName("com.baeldung.reflection.Bird"); final Field field = birdClass.getDeclaredField("walks"); assertEquals("walks", field.getName()); @@ -192,14 +192,14 @@ public class ReflectionUnitTest { @Test public void givenClassField_whenGetsType_thenCorrect() throws Exception { - final Field field = Class.forName("com.baeldung.java.reflection.Bird").getDeclaredField("walks"); + final Field field = Class.forName("com.baeldung.reflection.Bird").getDeclaredField("walks"); final Class fieldClass = field.getType(); assertEquals("boolean", fieldClass.getSimpleName()); } @Test public void givenClassField_whenSetsAndGetsValue_thenCorrect() throws Exception { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); + final Class birdClass = Class.forName("com.baeldung.reflection.Bird"); final Bird bird = (Bird) birdClass.getConstructor().newInstance(); final Field field = birdClass.getDeclaredField("walks"); field.setAccessible(true); @@ -216,7 +216,7 @@ public class ReflectionUnitTest { @Test public void givenClassField_whenGetsAndSetsWithNull_thenCorrect() throws Exception { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); + final Class birdClass = Class.forName("com.baeldung.reflection.Bird"); final Field field = birdClass.getField("CATEGORY"); field.setAccessible(true); @@ -225,7 +225,7 @@ public class ReflectionUnitTest { @Test public void givenClass_whenGetsAllPublicMethods_thenCorrect() throws ClassNotFoundException { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); + final Class birdClass = Class.forName("com.baeldung.reflection.Bird"); final Method[] methods = birdClass.getMethods(); final List methodNames = getMethodNames(methods); @@ -235,7 +235,7 @@ public class ReflectionUnitTest { @Test public void givenClass_whenGetsOnlyDeclaredMethods_thenCorrect() throws ClassNotFoundException { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); + final Class birdClass = Class.forName("com.baeldung.reflection.Bird"); final List actualMethodNames = getMethodNames(birdClass.getDeclaredMethods()); final List expectedMethodNames = Arrays.asList("setWalks", "walks", "getSound", "eats"); @@ -248,24 +248,17 @@ public class ReflectionUnitTest { @Test public void givenMethodName_whenGetsMethod_thenCorrect() throws Exception { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); - final Method walksMethod = birdClass.getDeclaredMethod("walks"); - final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class); - - assertFalse(walksMethod.isAccessible()); - assertFalse(setWalksMethod.isAccessible()); - - walksMethod.setAccessible(true); - setWalksMethod.setAccessible(true); - - assertTrue(walksMethod.isAccessible()); - assertTrue(setWalksMethod.isAccessible()); + final Bird bird = new Bird(); + final Method walksMethod = bird.getClass().getDeclaredMethod("walks"); + final Method setWalksMethod = bird.getClass().getDeclaredMethod("setWalks", boolean.class); + assertTrue(walksMethod.canAccess(bird)); + assertTrue(setWalksMethod.canAccess(bird)); } @Test public void givenMethod_whenInvokes_thenCorrect() throws Exception { - final Class birdClass = Class.forName("com.baeldung.java.reflection.Bird"); + final Class birdClass = Class.forName("com.baeldung.reflection.Bird"); final Bird bird = (Bird) birdClass.getConstructor().newInstance(); final Method setWalksMethod = birdClass.getDeclaredMethod("setWalks", boolean.class); final Method walksMethod = birdClass.getDeclaredMethod("walks"); diff --git a/core-java-modules/core-java-reflection/README.MD b/core-java-modules/core-java-reflection/README.MD index 5d8c54414b..62d8719981 100644 --- a/core-java-modules/core-java-reflection/README.MD +++ b/core-java-modules/core-java-reflection/README.MD @@ -3,7 +3,6 @@ - [Void Type in Java](https://www.baeldung.com/java-void-type) - [Retrieve Fields from a Java Class Using Reflection](https://www.baeldung.com/java-reflection-class-fields) - [Method Parameter Reflection in Java](http://www.baeldung.com/java-parameter-reflection) -- [Guide to Java Reflection](http://www.baeldung.com/java-reflection) - [Call Methods at Runtime Using Java Reflection](http://www.baeldung.com/java-method-reflection) - [Changing Annotation Parameters At Runtime](http://www.baeldung.com/java-reflection-change-annotation-params) - [Dynamic Proxies in Java](http://www.baeldung.com/java-dynamic-proxies) From ed7ce4ce3469944b48c40de45364f32d56fc135d Mon Sep 17 00:00:00 2001 From: gupta-ashu01 <30566001+gupta-ashu01@users.noreply.github.com> Date: Sun, 13 Sep 2020 22:35:45 +0530 Subject: [PATCH 37/40] BAEL-4567 (#10007) * BAEL-4567 BAEL-4567 * Add files via upload * Add files via upload * Delete SendMailWithAttachment.java * Update pom.xml * Formatting fixes --- .../core-java-networking-2/pom.xml | 6 ++ .../MailWithAttachmentService.java | 83 +++++++++++++++++++ .../MailWithAttachmentServiceLiveTest.java | 48 +++++++++++ 3 files changed, 137 insertions(+) create mode 100644 core-java-modules/core-java-networking-2/src/main/java/com/baeldung/mail/mailwithattachment/MailWithAttachmentService.java create mode 100644 core-java-modules/core-java-networking-2/src/test/java/com/baeldung/mail/mailwithattachment/MailWithAttachmentServiceLiveTest.java diff --git a/core-java-modules/core-java-networking-2/pom.xml b/core-java-modules/core-java-networking-2/pom.xml index d79320eaef..89a98bbf8b 100644 --- a/core-java-modules/core-java-networking-2/pom.xml +++ b/core-java-modules/core-java-networking-2/pom.xml @@ -35,6 +35,12 @@ async-http-client ${async-http-client.version} + + com.icegreen + greenmail + 1.5.8 + test + diff --git a/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/mail/mailwithattachment/MailWithAttachmentService.java b/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/mail/mailwithattachment/MailWithAttachmentService.java new file mode 100644 index 0000000000..7d4dc57f10 --- /dev/null +++ b/core-java-modules/core-java-networking-2/src/main/java/com/baeldung/mail/mailwithattachment/MailWithAttachmentService.java @@ -0,0 +1,83 @@ +package com.baeldung.mail.mailwithattachment; + +import java.io.File; +import java.io.IOException; +import java.util.Properties; +import javax.mail.BodyPart; +import javax.mail.Message; +import javax.mail.MessagingException; +import javax.mail.Multipart; +import javax.mail.PasswordAuthentication; +import javax.mail.Session; +import javax.mail.Transport; +import javax.mail.internet.AddressException; +import javax.mail.internet.InternetAddress; +import javax.mail.internet.MimeBodyPart; +import javax.mail.internet.MimeMessage; +import javax.mail.internet.MimeMultipart; + +public class MailWithAttachmentService { + + private String username = ""; + private String password = ""; + private String host = ""; + private String port = ""; + + MailWithAttachmentService() { + } + + MailWithAttachmentService(String username, String password, String host, String port) { + this.username = username; + this.password = password; + this.host = host; + this.port = port; + } + + public Session getSession() { + Properties props = new Properties(); + props.put("mail.smtp.auth", "true"); + props.put("mail.smtp.starttls.enable", "true"); + props.put("mail.smtp.host", this.host); + props.put("mail.smtp.port", this.port); + + Session session = Session.getInstance(props, new javax.mail.Authenticator() { + protected PasswordAuthentication getPasswordAuthentication() { + return new PasswordAuthentication(username, password); + } + }); + return session; + } + + public Message createMail(Session session) throws AddressException, MessagingException, IOException { + Message message = new MimeMessage(session); + message.setFrom(new InternetAddress("mail@gmail.com")); + message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("mail@gmail.com")); + message.setSubject("Testing Subject"); + + BodyPart messageBodyPart = new MimeBodyPart(); + messageBodyPart.setText("This is message body"); + + Multipart multipart = new MimeMultipart(); + multipart.addBodyPart(messageBodyPart); + + MimeBodyPart attachmentPart = new MimeBodyPart(); + MimeBodyPart attachmentPart2 = new MimeBodyPart(); + + attachmentPart.attachFile(new File("C:\\Document1.txt")); + attachmentPart2.attachFile(new File("C:\\Document2.txt")); + + multipart.addBodyPart(attachmentPart); + multipart.addBodyPart(attachmentPart2); + + message.setContent(multipart); + + return message; + } + + public void sendMail(Session session) throws MessagingException, IOException { + + Message message = createMail(session); + Transport.send(message); + } + +} \ No newline at end of file diff --git a/core-java-modules/core-java-networking-2/src/test/java/com/baeldung/mail/mailwithattachment/MailWithAttachmentServiceLiveTest.java b/core-java-modules/core-java-networking-2/src/test/java/com/baeldung/mail/mailwithattachment/MailWithAttachmentServiceLiveTest.java new file mode 100644 index 0000000000..ef82657ab5 --- /dev/null +++ b/core-java-modules/core-java-networking-2/src/test/java/com/baeldung/mail/mailwithattachment/MailWithAttachmentServiceLiveTest.java @@ -0,0 +1,48 @@ +package com.baeldung.mail.mailwithattachment; + +import static org.junit.Assert.*; +import javax.annotation.Resource; +import javax.mail.Session; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import com.baeldung.mail.mailwithattachment.MailWithAttachmentService; +import com.icegreen.greenmail.util.GreenMail; +import com.icegreen.greenmail.util.ServerSetupTest; + +public class MailWithAttachmentServiceLiveTest { + + @Resource + private MailWithAttachmentService emailService; + private GreenMail greenMail; + + @Before + public void startMailServer() { + emailService = new MailWithAttachmentService(); + greenMail = new GreenMail(ServerSetupTest.SMTP); + greenMail.start(); + } + + @After + public void stopMailServer() { + greenMail.stop(); + emailService = null; + } + + @Test + public void canSendMail() { + try { + Session testSession = greenMail.getSmtp() + .createSession(); + emailService.sendMail(testSession); + assertEquals(1, greenMail.getReceivedMessages().length); + + } catch (Exception e) { + e.printStackTrace(); + } + + } + +} From fb115abd62fede31a07c8a9630dadeb2c2e4b2ed Mon Sep 17 00:00:00 2001 From: Krzysiek Date: Sun, 13 Sep 2020 20:29:43 +0200 Subject: [PATCH 38/40] JAVA-2400: Fix EmailAnnotationPlugin implementation --- .../baeldung/swagger2boot/plugin/EmailAnnotationPlugin.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/plugin/EmailAnnotationPlugin.java b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/plugin/EmailAnnotationPlugin.java index 22ca144fb4..59a7c97080 100644 --- a/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/plugin/EmailAnnotationPlugin.java +++ b/spring-boot-modules/spring-boot-mvc/src/main/java/com/baeldung/swagger2boot/plugin/EmailAnnotationPlugin.java @@ -10,6 +10,7 @@ import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; import springfox.bean.validators.plugins.Validators; +import springfox.documentation.builders.StringElementFacetBuilder; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin; import springfox.documentation.spi.schema.contexts.ModelPropertyContext; @@ -30,8 +31,9 @@ public class EmailAnnotationPlugin implements ModelPropertyBuilderPlugin { public void apply(ModelPropertyContext context) { Optional email = annotationFromBean(context, Email.class); if (email.isPresent()) { - context.getBuilder().pattern(email.get().regexp()); - context.getBuilder().example("email@email.com"); + context.getSpecificationBuilder().facetBuilder(StringElementFacetBuilder.class) + .pattern(email.get().regexp()); + context.getSpecificationBuilder().example("email@email.com"); } } From 4e081b4fb65a03ccfa100f8f329e2e202e7b1b78 Mon Sep 17 00:00:00 2001 From: michaelisvy Date: Mon, 14 Sep 2020 08:16:56 +0800 Subject: [PATCH 39/40] added advanced Spring Retry example and removed Spring Retry xml config --- .../com/baeldung/springretry/AppConfig.java | 5 +- .../com/baeldung/springretry/MyService.java | 11 +++- .../baeldung/springretry/MyServiceImpl.java | 16 ++++++ .../src/main/resources/retryConfig.properties | 2 + .../src/main/resources/retryadvice.xml | 50 ------------------- .../SpringRetryIntegrationTest.java | 10 ++++ 6 files changed, 40 insertions(+), 54 deletions(-) create mode 100644 spring-scheduling/src/main/resources/retryConfig.properties delete mode 100644 spring-scheduling/src/main/resources/retryadvice.xml diff --git a/spring-scheduling/src/main/java/com/baeldung/springretry/AppConfig.java b/spring-scheduling/src/main/java/com/baeldung/springretry/AppConfig.java index e79beb370b..2ca9104e89 100644 --- a/spring-scheduling/src/main/java/com/baeldung/springretry/AppConfig.java +++ b/spring-scheduling/src/main/java/com/baeldung/springretry/AppConfig.java @@ -3,6 +3,7 @@ package com.baeldung.springretry; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; import org.springframework.retry.annotation.EnableRetry; import org.springframework.retry.backoff.FixedBackOffPolicy; import org.springframework.retry.policy.SimpleRetryPolicy; @@ -11,9 +12,7 @@ import org.springframework.retry.support.RetryTemplate; @Configuration @ComponentScan(basePackages = "com.baeldung.springretry") @EnableRetry -// Uncomment this two lines if we need XML configuration -// @EnableAspectJAutoProxy -// @ImportResource("classpath:/retryadvice.xml") +@PropertySource("classpath:retryConfig.properties") public class AppConfig { @Bean diff --git a/spring-scheduling/src/main/java/com/baeldung/springretry/MyService.java b/spring-scheduling/src/main/java/com/baeldung/springretry/MyService.java index 409bf25845..40e2c419fc 100644 --- a/spring-scheduling/src/main/java/com/baeldung/springretry/MyService.java +++ b/spring-scheduling/src/main/java/com/baeldung/springretry/MyService.java @@ -2,18 +2,27 @@ package com.baeldung.springretry; import java.sql.SQLException; +import org.springframework.context.annotation.PropertySource; import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Recover; import org.springframework.retry.annotation.Retryable; + public interface MyService { @Retryable void retryService(); - @Retryable(value = { SQLException.class }, maxAttempts = 2, backoff = @Backoff(delay = 5000)) + @Retryable(value = SQLException.class) void retryServiceWithRecovery(String sql) throws SQLException; + @Retryable(value = { SQLException.class }, maxAttempts = 2, backoff = @Backoff(delay = 100)) + void retryServiceWithCustomization(String sql) throws SQLException; + + @Retryable( value = SQLException.class, maxAttemptsExpression = "${retry.maxAttempts}", + backoff = @Backoff(delayExpression = "${retry.maxDelay}")) + void retryServiceWithExternalConfiguration(String sql) throws SQLException; + @Recover void recover(SQLException e, String sql); diff --git a/spring-scheduling/src/main/java/com/baeldung/springretry/MyServiceImpl.java b/spring-scheduling/src/main/java/com/baeldung/springretry/MyServiceImpl.java index 3e4b5ed00d..7eb4328a47 100644 --- a/spring-scheduling/src/main/java/com/baeldung/springretry/MyServiceImpl.java +++ b/spring-scheduling/src/main/java/com/baeldung/springretry/MyServiceImpl.java @@ -26,6 +26,22 @@ public class MyServiceImpl implements MyService { } } + @Override + public void retryServiceWithCustomization(String sql) throws SQLException { + if (StringUtils.isEmpty(sql)) { + logger.info("throw SQLException in method retryServiceWithCustomization()"); + throw new SQLException(); + } + } + + @Override + public void retryServiceWithExternalConfiguration(String sql) throws SQLException { + if (StringUtils.isEmpty(sql)) { + logger.info("throw SQLException in method retryServiceWithExternalConfiguration()"); + throw new SQLException(); + } + } + @Override public void recover(SQLException e, String sql) { logger.info("In recover method"); diff --git a/spring-scheduling/src/main/resources/retryConfig.properties b/spring-scheduling/src/main/resources/retryConfig.properties new file mode 100644 index 0000000000..7cc360adc6 --- /dev/null +++ b/spring-scheduling/src/main/resources/retryConfig.properties @@ -0,0 +1,2 @@ +retry.maxAttempts=2 +retry.maxDelay=100 \ No newline at end of file diff --git a/spring-scheduling/src/main/resources/retryadvice.xml b/spring-scheduling/src/main/resources/retryadvice.xml deleted file mode 100644 index 8de7801a58..0000000000 --- a/spring-scheduling/src/main/resources/retryadvice.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Initial sleep interval value, default 300 ms - - - - - The maximum value of the backoff period in milliseconds. - - - - - The value to increment the exp seed with for each retry attempt. - - - - - \ No newline at end of file diff --git a/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java b/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java index 2e5fb75482..33ce2fff74 100644 --- a/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java +++ b/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java @@ -30,6 +30,16 @@ public class SpringRetryIntegrationTest { myService.retryServiceWithRecovery(null); } + @Test + public void givenRetryServiceWithCustomization_whenCallWithException_thenRetryRecover() throws SQLException { + myService.retryServiceWithCustomization(null); + } + + @Test + public void givenRetryServiceWithExternalConfiguration_whenCallWithException_thenRetryRecover() throws SQLException { + myService.retryServiceWithExternalConfiguration(null); + } + @Test(expected = RuntimeException.class) public void givenTemplateRetryService_whenCallWithException_thenRetry() { retryTemplate.execute(arg0 -> { From bd9ac72a7cc8bef1583c6f7761b3ce5dcf8fdc7b Mon Sep 17 00:00:00 2001 From: kwoyke Date: Mon, 14 Sep 2020 11:24:29 +0200 Subject: [PATCH 40/40] BAEL-3712: Fix entities names (#10028) --- .../multipledb/dao/user/PossessionRepository.java | 4 ++-- .../multipledb/dao/user/UserRepository.java | 4 ++-- .../{PossessionMultipleDB.java => Possession.java} | 8 ++++---- .../model/user/{UserMultipleDB.java => User.java} | 12 ++++++------ .../multipledb/JpaMultipleDBIntegrationTest.java | 14 +++++++------- 5 files changed, 21 insertions(+), 21 deletions(-) rename persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/{PossessionMultipleDB.java => Possession.java} (89%) rename persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/{UserMultipleDB.java => User.java} (82%) diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/user/PossessionRepository.java b/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/user/PossessionRepository.java index ae37fde20d..a5ceee5b1a 100644 --- a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/user/PossessionRepository.java +++ b/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/user/PossessionRepository.java @@ -2,8 +2,8 @@ package com.baeldung.multipledb.dao.user; import org.springframework.data.jpa.repository.JpaRepository; -import com.baeldung.multipledb.model.user.PossessionMultipleDB; +import com.baeldung.multipledb.model.user.Possession; -public interface PossessionRepository extends JpaRepository { +public interface PossessionRepository extends JpaRepository { } diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/user/UserRepository.java b/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/user/UserRepository.java index 267a61a93f..d3109bd311 100644 --- a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/user/UserRepository.java +++ b/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/dao/user/UserRepository.java @@ -2,7 +2,7 @@ package com.baeldung.multipledb.dao.user; import org.springframework.data.jpa.repository.JpaRepository; -import com.baeldung.multipledb.model.user.UserMultipleDB; +import com.baeldung.multipledb.model.user.User; -public interface UserRepository extends JpaRepository { +public interface UserRepository extends JpaRepository { } \ No newline at end of file diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/PossessionMultipleDB.java b/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/Possession.java similarity index 89% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/PossessionMultipleDB.java rename to persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/Possession.java index a6a3c88bd0..af646ffd04 100644 --- a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/PossessionMultipleDB.java +++ b/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/Possession.java @@ -4,7 +4,7 @@ import javax.persistence.*; @Entity @Table -public class PossessionMultipleDB { +public class Possession { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -12,11 +12,11 @@ public class PossessionMultipleDB { private String name; - public PossessionMultipleDB() { + public Possession() { super(); } - public PossessionMultipleDB(final String name) { + public Possession(final String name) { super(); this.name = name; @@ -58,7 +58,7 @@ public class PossessionMultipleDB { if (getClass() != obj.getClass()) { return false; } - final PossessionMultipleDB other = (PossessionMultipleDB) obj; + final Possession other = (Possession) obj; if (id != other.id) { return false; } diff --git a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/UserMultipleDB.java b/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/User.java similarity index 82% rename from persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/UserMultipleDB.java rename to persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/User.java index c7cd07f7a1..1985c543d3 100644 --- a/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/UserMultipleDB.java +++ b/persistence-modules/spring-data-jpa-enterprise/src/main/java/com/baeldung/multipledb/model/user/User.java @@ -6,7 +6,7 @@ import java.util.List; @Entity @Table(name = "users") -public class UserMultipleDB { +public class User { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @@ -18,13 +18,13 @@ public class UserMultipleDB { private Integer status; @OneToMany - List possessionList; + List possessionList; - public UserMultipleDB() { + public User() { super(); } - public UserMultipleDB(String name, String email, Integer status) { + public User(String name, String email, Integer status) { this.name = name; this.email = email; this.status = status; @@ -70,11 +70,11 @@ public class UserMultipleDB { this.age = age; } - public List getPossessionList() { + public List getPossessionList() { return possessionList; } - public void setPossessionList(List possessionList) { + public void setPossessionList(List possessionList) { this.possessionList = possessionList; } diff --git a/persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/multipledb/JpaMultipleDBIntegrationTest.java b/persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/multipledb/JpaMultipleDBIntegrationTest.java index a1f4a3fa2c..fb363e2ab3 100644 --- a/persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/multipledb/JpaMultipleDBIntegrationTest.java +++ b/persistence-modules/spring-data-jpa-enterprise/src/test/java/com/baeldung/multipledb/JpaMultipleDBIntegrationTest.java @@ -20,8 +20,8 @@ import com.baeldung.multipledb.dao.product.ProductRepository; import com.baeldung.multipledb.dao.user.PossessionRepository; import com.baeldung.multipledb.dao.user.UserRepository; import com.baeldung.multipledb.model.product.Product; -import com.baeldung.multipledb.model.user.PossessionMultipleDB; -import com.baeldung.multipledb.model.user.UserMultipleDB; +import com.baeldung.multipledb.model.user.Possession; +import com.baeldung.multipledb.model.user.User; @RunWith(SpringRunner.class) @SpringBootTest(classes=MultipleDbApplication.class) @@ -42,15 +42,15 @@ public class JpaMultipleDBIntegrationTest { @Test @Transactional("userTransactionManager") public void whenCreatingUser_thenCreated() { - UserMultipleDB user = new UserMultipleDB(); + User user = new User(); user.setName("John"); user.setEmail("john@test.com"); user.setAge(20); - PossessionMultipleDB p = new PossessionMultipleDB("sample"); + Possession p = new Possession("sample"); p = possessionRepository.save(p); user.setPossessionList(Collections.singletonList(p)); user = userRepository.save(user); - final Optional result = userRepository.findById(user.getId()); + final Optional result = userRepository.findById(user.getId()); assertTrue(result.isPresent()); System.out.println(result.get().getPossessionList()); assertEquals(1, result.get().getPossessionList().size()); @@ -59,14 +59,14 @@ public class JpaMultipleDBIntegrationTest { @Test @Transactional("userTransactionManager") public void whenCreatingUsersWithSameEmail_thenRollback() { - UserMultipleDB user1 = new UserMultipleDB(); + User user1 = new User(); user1.setName("John"); user1.setEmail("john@test.com"); user1.setAge(20); user1 = userRepository.save(user1); assertTrue(userRepository.findById(user1.getId()).isPresent()); - UserMultipleDB user2 = new UserMultipleDB(); + User user2 = new User(); user2.setName("Tom"); user2.setEmail("john@test.com"); user2.setAge(10);