diff --git a/pom.xml b/pom.xml index 133e8565dcf3..43c6a10503cf 100644 --- a/pom.xml +++ b/pom.xml @@ -207,6 +207,7 @@ context-object thread-local-storage optimistic-offline-lock + vertical-slice-architecture crtp health-check diff --git a/vertical-slice-architecture/README.md b/vertical-slice-architecture/README.md new file mode 100644 index 000000000000..92cf530a72be --- /dev/null +++ b/vertical-slice-architecture/README.md @@ -0,0 +1,62 @@ +--- +title: Vertical Slice Architecture +aka: Layer By Feature +category: Architectural +language: en +tag: +- Decoupling +--- + +## Intent + +Organize the application according to its features. +Each feature will comprise its distinct set of layers (Models, Services, Repository, and Controllers). + +## Explanation + +Real-World Examples (Consider E-commerce) + +> In the context of an e-commerce application, the concept of vertical slice architecture becomes clear. +> Imagine you're building a backend service for an online store. +> Initially, you may organize it with the typical grouping of controllers, models, and other components. +> As the application grows, the need arises to implement new features. + +> For instance, you might have distinct layers for orders, customers, and products. However, as the application +> evolves, you realize the necessity of integrating additional features like a Cart system and wishlists. +> At this point, integrating these new features into the existing structure becomes challenging. +> It demands significant dependency modifications and mocking, which can be time-consuming and error-prone. + +> This is where vertical slice architecture proves its value. +> By structuring the application based on features, +> you create self-contained modules that encapsulate all the necessary components +> (Models, Services, Repository, and Controllers) for a particular feature. +> When you need to add new features, you can do so in a more isolated and manageable manner. + +In Plain Words + +> Vertical slice architecture is like organizing your toolbox. +> Instead of having all your tools mixed together, you group them based on the type of task they perform. +> This way, when you need a specific tool for a particular job, +> you can quickly find it without rummaging through a jumble of items. + +> Similarly, in software development, vertical slice architecture involves organizing the codebase based on features. +> Each feature has its own self-contained set of components, making it easier to add, modify, or remove features without disrupting the entire application. + +## Class diagram + +![Vertical Slice Architecture](./etc/vertical-slice-architecture.urm.png) + +## Applicability + +Use Vertical Slice Architecture when + +* You want future modification ( new addition of features ). +* You want to reduce the amount of mocking. +* You want to make it more modular by feature. + +## Resources + +* [How to Implement Vertical Slice Architecture by Gary Woodfine](https://garywoodfine.com/implementing-vertical-slice-architecture/) +* [youtube](https://www.youtube.com/watch?v=B1d95I7-zsw) +* [medium](https://medium.com/sahibinden-technology/package-by-layer-vs-package-by-feature-7e89cde2ae3a) +* [A reference application](https://github.com/sugan0tech/Event-Manager) diff --git a/vertical-slice-architecture/etc/vertical-slice-architecture.urm.png b/vertical-slice-architecture/etc/vertical-slice-architecture.urm.png new file mode 100644 index 000000000000..1ce57837d7dc Binary files /dev/null and b/vertical-slice-architecture/etc/vertical-slice-architecture.urm.png differ diff --git a/vertical-slice-architecture/etc/vertical-slice-architecture.urm.puml b/vertical-slice-architecture/etc/vertical-slice-architecture.urm.puml new file mode 100644 index 000000000000..9fcc14b64417 --- /dev/null +++ b/vertical-slice-architecture/etc/vertical-slice-architecture.urm.puml @@ -0,0 +1,125 @@ +@startuml +package com.iluwatar.vertical-slice-architecture { + + !define ENTITY class + !define SERVICE class + !define REPOSITORY class + !define VIEW class + + package Customer { + ENTITY Customer { + +id: int + name: String + email: String + +getId(): int + +getName(): String + +getEmail(): String + +builder(): Builder + } + + SERVICE CustomerService { + +createCustomer(name: String, email: String): Customer + +getCustomerById(id: int): Customer + +getAllCustomers(): List + } + + REPOSITORY CustomerRepository { + +save(customer: Customer): Customer + +findById(id: int): Optional + +findAll(): List + } + + + VIEW CustomerView { + -customerService: CustomerService + -LOGGER: logger + +render(): void + } + } + + package Product { + ENTITY Product { + +id: int + name: String + price: double + +getId(): int + +getName(): String + +getPrice(): Double + +builder(): Builder + } + + SERVICE ProductService { + +createProduct(name: String, price: double): Product + +getProductById(id: int): Product + +getAllProducts(): List + } + + REPOSITORY ProductRepository { + +save(product: Product): Product + +findById(id: int): Optional + +findAll(): List + } + + + VIEW ProductView { + -productService: ProductService + -LOGGER: logger + +render(): void + } + } + + package Order { + ENTITY Orders { + +id: int + customer: Customer + product: Product + +getId(): int + +getCustomer(): Customer + +getProduct(): Product + +builder(): Builder + } + + SERVICE OrderService { + +createOrder(customer: Customer, product: Product): void + +getOrderById(id: int): Orders + +getOrdersByCustomer(customer: Customer): List + } + + REPOSITORY OrderRepository { + +save(order: Orders): Orders + +findById(id: int): Optional + +findByCustomer(customer: Customer): List + } + + + VIEW OrderView { + -orderService: OrderService + -LOGGER: logger + +render(customer: Customer): void + +showAllOrders(orders: List): void + } + } + + class App { + +initializeData(): void + +run(): void + } + + Customer.Customer --> Customer.CustomerService + Customer.CustomerService --> Customer.CustomerRepository + Customer.CustomerService --> Customer.CustomerView + + Product.Product --> Product.ProductService + Product.ProductService --> Product.ProductRepository + Product.ProductService --> Product.ProductView + + Order.Orders --> Order.OrderService + Order.OrderService --> Order.OrderRepository + Order.OrderService --> Order.OrderView + + App --> Customer.CustomerService + App --> Product.ProductService + App --> Order.OrderService + +} +@enduml \ No newline at end of file diff --git a/vertical-slice-architecture/pom.xml b/vertical-slice-architecture/pom.xml new file mode 100644 index 000000000000..1a9d15591dd9 --- /dev/null +++ b/vertical-slice-architecture/pom.xml @@ -0,0 +1,90 @@ + + + + 4.0.0 + vertical-slice-architecture + + com.iluwatar + java-design-patterns + 1.26.0-SNAPSHOT + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-test + test + + + org.springframework.boot + spring-boot-starter-data-jpa + + + com.h2database + h2 + runtime + + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + + + com.iluwatar.vertical.slice.architecture.App + + + + + + + + + + diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/App.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/App.java new file mode 100644 index 000000000000..d3b058f8833a --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/App.java @@ -0,0 +1,45 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * This application is designed with a vertical slice architecture, organizing features such as + * customer management, order processing, and product catalog in separate modules. Each feature encapsulates + * its own set of components (Models, Services, Repository, and Controllers), promoting high cohesion + * within each module and low coupling between them. This architecture allows for seamless integration of new + * features and functionalities as the application evolves over time. + */ + +@SpringBootApplication +public class App { + + public static void main(String[] args) { + SpringApplication.run(App.class, args); + } + +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/Runner.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/Runner.java new file mode 100644 index 000000000000..22fe98c1915f --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/Runner.java @@ -0,0 +1,83 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture; + +import com.iluwatar.verticalslicearchitecture.customer.Customer; +import com.iluwatar.verticalslicearchitecture.customer.CustomerService; +import com.iluwatar.verticalslicearchitecture.customer.CustomerView; +import com.iluwatar.verticalslicearchitecture.order.OrderService; +import com.iluwatar.verticalslicearchitecture.order.OrderView; +import com.iluwatar.verticalslicearchitecture.product.Product; +import com.iluwatar.verticalslicearchitecture.product.ProductService; +import com.iluwatar.verticalslicearchitecture.product.ProductView; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.boot.CommandLineRunner; +import org.springframework.stereotype.Component; + +/** + * Seeding test data. + */ + +@Component +@AllArgsConstructor +@Slf4j +public class Runner implements CommandLineRunner { + + CustomerService customerService; + OrderService orderService; + ProductService productService; + + @Override + public void run(String... args) { + initializeData(); + new OrderView(orderService).render(customerService.getCustomerById(1)); + new CustomerView(customerService).render(); + new ProductView(productService).render(); + } + + /** + * method for data seeds. + */ + + public void initializeData() { + + var customer = Customer.builder().id(1).name("sugan0tech").email("sugan@gmail.com").build(); + customerService.createCustomer(customer); + + var oreo = Product.builder().id(1).price(2.00).name("Oreo").build(); + var cone = Product.builder().id(3).price(1.15).name("Ice Cream Cone").build(); + var apple = Product.builder().id(4).price(2.00).name("Apple").build(); + var sandwich = Product.builder().id(2).price(6.00).name("Sandwich").build(); + productService.createProduct(oreo); + productService.createProduct(cone); + productService.createProduct(apple); + productService.createProduct(sandwich); + + orderService.createOrder(1, customer, oreo); + orderService.createOrder(3, customer, apple); + orderService.createOrder(2, customer, sandwich); + } +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/customer/Customer.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/customer/Customer.java new file mode 100644 index 000000000000..47e6d7ac2ed0 --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/customer/Customer.java @@ -0,0 +1,49 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture.customer; + +import javax.persistence.Entity; +import javax.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * Customer Entity. + */ + +@Entity +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Builder +public class Customer { + + @Id + Integer id; + String name; + String email; +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/customer/CustomerRepository.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/customer/CustomerRepository.java new file mode 100644 index 000000000000..3ba569cb4b91 --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/customer/CustomerRepository.java @@ -0,0 +1,36 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture.customer; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +/** + * Customer Repository. + */ + +@Repository +public interface CustomerRepository extends JpaRepository { +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/customer/CustomerService.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/customer/CustomerService.java new file mode 100644 index 000000000000..6d9422d39157 --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/customer/CustomerService.java @@ -0,0 +1,51 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture.customer; + +import java.util.List; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; + +/** + * Customer Service. + */ + +@Service +@AllArgsConstructor +public class CustomerService { + private CustomerRepository customerRepository; + + public void createCustomer(Customer customer) { + customerRepository.save(customer); + } + + public Customer getCustomerById(int id) { + return customerRepository.findById(id).orElse(null); + } + + public List getAllCustomers() { + return customerRepository.findAll(); + } +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/customer/CustomerView.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/customer/CustomerView.java new file mode 100644 index 000000000000..ff6f30ea592c --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/customer/CustomerView.java @@ -0,0 +1,42 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture.customer; + +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +/** + * View for Customer. + */ + +@AllArgsConstructor +@Slf4j +public class CustomerView { + CustomerService customerService; + + public void render() { + customerService.getAllCustomers().forEach(customer -> LOGGER.info(customer.getName())); + } +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/order/OrderRepository.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/order/OrderRepository.java new file mode 100644 index 000000000000..7621eb1e07e7 --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/order/OrderRepository.java @@ -0,0 +1,39 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture.order; + +import com.iluwatar.verticalslicearchitecture.customer.Customer; +import java.util.List; +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +/** + * Orders repository. + */ + +@Repository +public interface OrderRepository extends JpaRepository { + List findByCustomer(Customer customer); +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/order/OrderService.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/order/OrderService.java new file mode 100644 index 000000000000..062c935e63f7 --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/order/OrderService.java @@ -0,0 +1,54 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture.order; + +import com.iluwatar.verticalslicearchitecture.customer.Customer; +import com.iluwatar.verticalslicearchitecture.product.Product; +import java.util.List; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; + +/** + * Order Service. + */ + +@Service +@AllArgsConstructor +public class OrderService { + private OrderRepository orderRepository; + + + public void createOrder(int id, Customer customer, Product product) { + orderRepository.save(new Orders(id, customer, product)); + } + + public Orders getOrderById(int id) { + return orderRepository.findById(id).orElse(null); + } + + public List getOrdersByCustomer(Customer customer) { + return orderRepository.findByCustomer(customer); + } +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/order/OrderView.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/order/OrderView.java new file mode 100644 index 000000000000..0906aef530da --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/order/OrderView.java @@ -0,0 +1,43 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture.order; + +import com.iluwatar.verticalslicearchitecture.customer.Customer; +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +/** + * View for orders. + */ + +@AllArgsConstructor +@Slf4j +public class OrderView { + OrderService orderService; + + public void render(Customer customer) { + orderService.getOrdersByCustomer(customer).forEach(order -> LOGGER.info(order.getId().toString())); + } +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/order/Orders.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/order/Orders.java new file mode 100644 index 000000000000..479cd37e066d --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/order/Orders.java @@ -0,0 +1,56 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture.order; + +import com.iluwatar.verticalslicearchitecture.customer.Customer; +import com.iluwatar.verticalslicearchitecture.product.Product; +import javax.persistence.Entity; +import javax.persistence.Id; +import javax.persistence.JoinColumn; +import javax.persistence.ManyToOne; +import lombok.AllArgsConstructor; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * Customer Entity. + */ + +@Entity +@AllArgsConstructor +@NoArgsConstructor +@Getter +public class Orders { + @Id + private Integer id; + + @ManyToOne + @JoinColumn(name = "customer_id") + private Customer customer; + + @ManyToOne + @JoinColumn(name = "product_id") + private Product product; +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/product/Product.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/product/Product.java new file mode 100644 index 000000000000..45374d71f4a0 --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/product/Product.java @@ -0,0 +1,48 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture.product; + +import javax.persistence.Entity; +import javax.persistence.Id; +import lombok.AllArgsConstructor; +import lombok.Builder; +import lombok.Getter; +import lombok.NoArgsConstructor; + +/** + * Customer Entity. + */ + +@Entity +@AllArgsConstructor +@NoArgsConstructor +@Getter +@Builder +public class Product { + @Id + Integer id; + Double price; + String name; +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/product/ProductRepository.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/product/ProductRepository.java new file mode 100644 index 000000000000..e0deb6445e8f --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/product/ProductRepository.java @@ -0,0 +1,36 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture.product; + +import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.stereotype.Repository; + +/** + * Product Repository. + */ + +@Repository +public interface ProductRepository extends JpaRepository { +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/product/ProductService.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/product/ProductService.java new file mode 100644 index 000000000000..ca54ce3fa004 --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/product/ProductService.java @@ -0,0 +1,50 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture.product; + +import java.util.List; +import lombok.AllArgsConstructor; +import org.springframework.stereotype.Service; + +/** + * Products service. + */ +@Service +@AllArgsConstructor +public class ProductService { + private ProductRepository productRepository; + + public void createProduct(Product product) { + productRepository.save(product); + } + + public Product getProductById(int id) { + return productRepository.findById(id).orElse(null); + } + + public List getAllProducts() { + return productRepository.findAll(); + } +} diff --git a/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/product/ProductView.java b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/product/ProductView.java new file mode 100644 index 000000000000..4d0eee480750 --- /dev/null +++ b/vertical-slice-architecture/src/main/java/com/iluwatar/verticalslicearchitecture/product/ProductView.java @@ -0,0 +1,42 @@ +/* + * This project is licensed under the MIT license. Module model-view-viewmodel is using ZK framework licensed under LGPL (see lgpl-3.0.txt). + * + * The MIT License + * Copyright © 2014-2022 Ilkka Seppälä + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ +package com.iluwatar.verticalslicearchitecture.product; + +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; + +/** + * View for Products. + */ + +@AllArgsConstructor +@Slf4j +public class ProductView { + ProductService productService; + + public void render() { + productService.getAllProducts().forEach(product -> LOGGER.info(product.getName())); + } +} diff --git a/vertical-slice-architecture/src/main/resources/application.properties b/vertical-slice-architecture/src/main/resources/application.properties new file mode 100644 index 000000000000..64f39530555c --- /dev/null +++ b/vertical-slice-architecture/src/main/resources/application.properties @@ -0,0 +1,6 @@ +spring.main.web-application-type=none +#datasource settings +spring.datasource.url=jdbc:h2:mem:testdb +spring.datasource.driverClassName=org.h2.Driver +spring.datasource.username=sa +spring.datasource.password=sa \ No newline at end of file diff --git a/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/AppTests.java b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/AppTests.java new file mode 100644 index 000000000000..4085b102bbb4 --- /dev/null +++ b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/AppTests.java @@ -0,0 +1,23 @@ +package com.iluwatar.verticalslicearchitecture; + +import lombok.AllArgsConstructor; +import org.junit.jupiter.api.Test; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.context.ApplicationContext; + +import static org.junit.jupiter.api.Assertions.assertNotNull; + +@SpringBootTest +@AllArgsConstructor +class AppTests { + + private final ApplicationContext applicationContext; + + @Test + void contextLoads() { + App.main(new String[] {}); + assertNotNull(App.class); + assertNotNull(applicationContext); + } + +} diff --git a/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/customer/CustomerServiceTest.java b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/customer/CustomerServiceTest.java new file mode 100644 index 000000000000..5dca86f9a8b7 --- /dev/null +++ b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/customer/CustomerServiceTest.java @@ -0,0 +1,74 @@ +package com.iluwatar.verticalslicearchitecture.customer; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.*; + + +class CustomerServiceTest { + @Mock + private CustomerRepository customerRepository; + + @InjectMocks + private CustomerService customerService; + + @BeforeEach + public void setUp() { + MockitoAnnotations.openMocks(this); + } + + + @Test + void testCreateCustomer() { + + Customer newCustomer = Customer.builder().build(); + + when(customerRepository.save(any(Customer.class))).thenReturn(newCustomer); + customerService.createCustomer(newCustomer); + verify(customerRepository, times(1)).save(newCustomer); + } + + @Test + void testGetCustomerById() { + int customerId = 1; + Customer existingCustomer = new Customer(customerId, "John Doe", "john.doe@example.com"); + + /* + * Mocking the behavior of customerRepository.findById + */ + when(customerRepository.findById(customerId)).thenReturn(Optional.of(existingCustomer)); + + Customer retrievedCustomer = customerService.getCustomerById(customerId); + + assertNotNull(retrievedCustomer); + assertEquals(existingCustomer, retrievedCustomer); + } + + @Test + void testGetAllCustomers() { + List customers = new ArrayList<>(); + customers.add(new Customer(1, "John Doe", "john.doe@example.com")); + customers.add(new Customer(2, "Jane Smith", "jane.smith@example.com")); + + /* + * Mocking the behavior of customerRepository.findAll + */ + when(customerRepository.findAll()).thenReturn(customers); + + List retrievedCustomers = customerService.getAllCustomers(); + + assertNotNull(retrievedCustomers); + assertEquals(customers.size(), retrievedCustomers.size()); + assertEquals(customers, retrievedCustomers); + } +} \ No newline at end of file diff --git a/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/customer/CustomerTest.java b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/customer/CustomerTest.java new file mode 100644 index 000000000000..9530d6ecb7de --- /dev/null +++ b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/customer/CustomerTest.java @@ -0,0 +1,28 @@ +package com.iluwatar.verticalslicearchitecture.customer; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class CustomerTest { + + @Test + void testCustomerEntity() { + // Arrange + Integer id = 1; + String name = "John Doe"; + String email = "john.doe@example.com"; + + // Act + Customer customer = Customer.builder() + .id(id) + .name(name) + .email(email) + .build(); + + // Assert + assertEquals(id, customer.getId()); + assertEquals(name, customer.getName()); + assertEquals(email, customer.getEmail()); + } +} \ No newline at end of file diff --git a/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/customer/CustomerViewTest.java b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/customer/CustomerViewTest.java new file mode 100644 index 000000000000..a7107161cdc7 --- /dev/null +++ b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/customer/CustomerViewTest.java @@ -0,0 +1,36 @@ +package com.iluwatar.verticalslicearchitecture.customer; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.util.List; + +import static org.mockito.Mockito.*; + +class CustomerViewTest { + + @Mock + private CustomerService customerService; + + private CustomerView customerView; + + @BeforeEach + public void setup() { + MockitoAnnotations.openMocks(this); + customerView = new CustomerView(customerService); + } + + @Test + void testRender() { + when(customerService.getAllCustomers()).thenReturn(List.of( + Customer.builder().id(1).name("John Doe").email("john.doe@example.com").build(), + Customer.builder().id(2).name("Jone Doe").email("jone.doe@example.com").build() + )); + + customerView.render(); + + verify(customerService, times(1)).getAllCustomers(); + } +} \ No newline at end of file diff --git a/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/order/OrderServiceTest.java b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/order/OrderServiceTest.java new file mode 100644 index 000000000000..028c6d0b7b3f --- /dev/null +++ b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/order/OrderServiceTest.java @@ -0,0 +1,74 @@ +package com.iluwatar.verticalslicearchitecture.order; + +import com.iluwatar.verticalslicearchitecture.customer.Customer; +import com.iluwatar.verticalslicearchitecture.product.Product; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +class OrderServiceTest { + + @Mock + private OrderRepository orderRepository; + + @InjectMocks + private OrderService orderService; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + } + + @Test + void testCreateOrder() { + Customer customer = Customer.builder().id(1).name("John Doe").email("john.doe@example.com").build(); + Product product = Product.builder().id(1).name("Sample Product").price(100.0).build(); + Orders order = new Orders(1, customer, product); + + when(orderRepository.save(any(Orders.class))).thenReturn(order); + + orderService.createOrder(1, customer, product); + + Mockito.verify(orderRepository).save(any(Orders.class)); + } + + @Test + void testGetOrderById() { + Customer customer = Customer.builder().id(1).name("John Doe").email("john.doe@example.com").build(); + Product product = Product.builder().id(1).name("Sample Product").price(100.0).build(); + Orders order = new Orders(1, customer, product); + + when(orderRepository.findById(1)).thenReturn(Optional.of(order)); + + Orders result = orderService.getOrderById(1); + + assertEquals(order, result); + } + + @Test + void testGetOrdersByCustomer() { + Customer customer = Customer.builder().id(1).name("John Doe").email("john.doe@example.com").build(); + Product product = Product.builder().id(1).name("Sample Product").price(100.0).build(); + Orders order = new Orders(1, customer, product); + + List ordersList = new ArrayList<>(); + ordersList.add(order); + + when(orderRepository.findByCustomer(customer)).thenReturn(ordersList); + + List result = orderService.getOrdersByCustomer(customer); + + assertEquals(ordersList, result); + } +} diff --git a/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/order/OrderViewTest.java b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/order/OrderViewTest.java new file mode 100644 index 000000000000..98274babdb20 --- /dev/null +++ b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/order/OrderViewTest.java @@ -0,0 +1,40 @@ +package com.iluwatar.verticalslicearchitecture.order; + +import com.iluwatar.verticalslicearchitecture.customer.Customer; +import com.iluwatar.verticalslicearchitecture.product.Product; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.List; + +import static org.mockito.Mockito.*; + +class OrderViewTest { + + private OrderService orderService; + private OrderView orderView; + + @BeforeEach + void setUp() { + orderService = Mockito.mock(OrderService.class); + orderView = new OrderView(orderService); + } + + @Test + void testRender() { + Customer customer = Customer.builder().id(1).name("John Doe").email("john.doe@example.com").build(); + Product product = Product.builder().id(1).name("Sample Product").price(100.0).build(); + Orders order = new Orders(1, customer, product); + + List ordersList = new ArrayList<>(); + ordersList.add(order); + + when(orderService.getOrdersByCustomer(customer)).thenReturn(ordersList); + + orderView.render(customer); + + verify(orderService, times(1)).getOrdersByCustomer(any()); + } +} diff --git a/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/order/OrdersTest.java b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/order/OrdersTest.java new file mode 100644 index 000000000000..767da0bbf919 --- /dev/null +++ b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/order/OrdersTest.java @@ -0,0 +1,27 @@ +package com.iluwatar.verticalslicearchitecture.order; + +import com.iluwatar.verticalslicearchitecture.customer.Customer; +import com.iluwatar.verticalslicearchitecture.product.Product; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class OrdersTest { + + @BeforeEach + void setUp() { + } + + @Test + void testOrdersEntity() { + Customer customer = Customer.builder().id(1).name("John Doe").email("john.doe@example.com").build(); + Product product = Product.builder().id(1).name("Sample Product").price(100.0).build(); + + Orders orders = new Orders(1, customer, product); + + assertEquals(1, orders.getId()); + assertEquals(customer, orders.getCustomer()); + assertEquals(product, orders.getProduct()); + } +} diff --git a/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/product/ProductServiceTest.java b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/product/ProductServiceTest.java new file mode 100644 index 000000000000..8a53db6a1ab2 --- /dev/null +++ b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/product/ProductServiceTest.java @@ -0,0 +1,58 @@ +package com.iluwatar.verticalslicearchitecture.product; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; + +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.when; + +class ProductServiceTest { + + private ProductRepository productRepository; + private ProductService productService; + + @BeforeEach + void setUp() { + productRepository = Mockito.mock(ProductRepository.class); + productService = new ProductService(productRepository); + } + + @Test + void testCreateProduct() { + Product product = Product.builder().id(1).name("Sample Product").price(100.0).build(); + + productService.createProduct(product); + + Mockito.verify(productRepository, Mockito.times(1)).save(product); + } + + @Test + void testGetProductById() { + int productId = 1; + Product product = Product.builder().id(1).name("Sample Product").price(100.0).build(); + + when(productRepository.findById(productId)).thenReturn(Optional.of(product)); + + Product retrievedProduct = productService.getProductById(productId); + + assertEquals(product, retrievedProduct); + } + + @Test + void testGetAllProducts() { + List productList = new ArrayList<>(); + productList.add(Product.builder().id(1).name("Product 1").price(100.0).build()); + productList.add(Product.builder().id(2).name("Product 2").price(50.0).build()); + + when(productRepository.findAll()).thenReturn(productList); + + List retrievedList = productService.getAllProducts(); + + assertEquals(productList, retrievedList); + } +} diff --git a/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/product/ProductTest.java b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/product/ProductTest.java new file mode 100644 index 000000000000..f257c3de0939 --- /dev/null +++ b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/product/ProductTest.java @@ -0,0 +1,21 @@ +package com.iluwatar.verticalslicearchitecture.product; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +class ProductTest { + + @Test + void testProductConstructorAndGetters() { + Integer id = 1; + Double price = 100.0; + String name = "Sample Product"; + + Product product = Product.builder().id(id).price(price).name(name).build(); + + assertEquals(id, product.getId()); + assertEquals(price, product.getPrice()); + assertEquals(name, product.getName()); + } +} diff --git a/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/product/ProductViewTest.java b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/product/ProductViewTest.java new file mode 100644 index 000000000000..6e0422b98740 --- /dev/null +++ b/vertical-slice-architecture/src/test/java/com/iluwatar/verticalslicearchitecture/product/ProductViewTest.java @@ -0,0 +1,40 @@ +package com.iluwatar.verticalslicearchitecture.product; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.MockitoAnnotations; + +import java.util.ArrayList; +import java.util.List; + +import static org.mockito.Mockito.times; + +class ProductViewTest { + + @Mock + ProductService productService; + + ProductView productView; + + @BeforeEach + public void setup() { + MockitoAnnotations.openMocks(this); + productView = new ProductView(productService); + } + + @Test + void testRender() { + + List productList = new ArrayList<>(); + productList.add(Product.builder().id(1).price(100.0).name("Sample Product 1").build()); + productList.add(Product.builder().id(2).price(50.0).name("Sample Product 2").build()); + + Mockito.when(productService.getAllProducts()).thenReturn(productList); + + productView.render(); + + Mockito.verify(productService, times(1)).getAllProducts(); + } +}