From 8b8554eb84e9d02e73affde7b302b0a87ccbf1b5 Mon Sep 17 00:00:00 2001 From: akeshri Date: Sun, 28 Jun 2020 22:32:39 +0530 Subject: [PATCH] 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); + } + +}