From 4a70340bdd56741943ef39249759eec59cc6cb3a Mon Sep 17 00:00:00 2001 From: K Siva Prasad Reddy Date: Thu, 30 Nov 2023 11:04:00 +0530 Subject: [PATCH] Refactor domain to products --- README.md | 2 +- .../messages/domain/Message.java | 6 ----- .../messages/domain/MessageRepository.java | 22 ----------------- .../{messages => products}/Application.java | 2 +- .../api/ProductController.java} | 24 +++++++++---------- .../config/SecurityConfig.java | 6 ++--- .../products/domain/Product.java | 5 ++++ .../products/domain/ProductRepository.java | 22 +++++++++++++++++ .../ContainersConfig.java | 4 ++-- .../TestApplication.java | 2 +- .../api/ProductControllerTests.java} | 17 ++++++------- 11 files changed, 56 insertions(+), 56 deletions(-) delete mode 100644 src/main/java/com/testcontainers/messages/domain/Message.java delete mode 100644 src/main/java/com/testcontainers/messages/domain/MessageRepository.java rename src/main/java/com/testcontainers/{messages => products}/Application.java (88%) rename src/main/java/com/testcontainers/{messages/api/MessageController.java => products/api/ProductController.java} (50%) rename src/main/java/com/testcontainers/{messages => products}/config/SecurityConfig.java (94%) create mode 100644 src/main/java/com/testcontainers/products/domain/Product.java create mode 100644 src/main/java/com/testcontainers/products/domain/ProductRepository.java rename src/test/java/com/testcontainers/{messages => products}/ContainersConfig.java (92%) rename src/test/java/com/testcontainers/{messages => products}/TestApplication.java (86%) rename src/test/java/com/testcontainers/{messages/ApplicationTests.java => products/api/ProductControllerTests.java} (86%) diff --git a/README.md b/README.md index 1282b2f..2014a2c 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ This is sample code for [Securing Spring Boot Microservice using Keycloak and Testcontainers](https://testcontainers.com/guides/securing-spring-boot-microservice-using-keycloak-and-testcontainers) guide. ## 1. Setup Environment -Make sure you have Java 8+ and a [compatible Docker environment](https://www.testcontainers.org/supported_docker_environment/) installed. +Make sure you have Java 8+ and a [compatible Docker environment](https://java.testcontainers.org/supported_docker_environment/) installed. If you are going to use Maven build tool then make sure Java 17+ is installed. For example: diff --git a/src/main/java/com/testcontainers/messages/domain/Message.java b/src/main/java/com/testcontainers/messages/domain/Message.java deleted file mode 100644 index b1d7eb8..0000000 --- a/src/main/java/com/testcontainers/messages/domain/Message.java +++ /dev/null @@ -1,6 +0,0 @@ -package com.testcontainers.messages.domain; - -import jakarta.validation.constraints.NotEmpty; -import java.time.Instant; - -public record Message(Long id, @NotEmpty String content, @NotEmpty String createdBy, Instant createdAt) {} diff --git a/src/main/java/com/testcontainers/messages/domain/MessageRepository.java b/src/main/java/com/testcontainers/messages/domain/MessageRepository.java deleted file mode 100644 index 9c094fe..0000000 --- a/src/main/java/com/testcontainers/messages/domain/MessageRepository.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.testcontainers.messages.domain; - -import java.time.Instant; -import java.util.ArrayList; -import java.util.List; -import java.util.concurrent.atomic.AtomicLong; -import org.springframework.stereotype.Repository; - -@Repository -public class MessageRepository { - private static final AtomicLong ID = new AtomicLong(0L); - private static final List MESSAGES = new ArrayList<>(); - - public List getMessages() { - return List.copyOf(MESSAGES); - } - - public Message createMessage(Message message) { - MESSAGES.add(new Message(ID.incrementAndGet(), message.content(), message.createdBy(), Instant.now())); - return message; - } -} diff --git a/src/main/java/com/testcontainers/messages/Application.java b/src/main/java/com/testcontainers/products/Application.java similarity index 88% rename from src/main/java/com/testcontainers/messages/Application.java rename to src/main/java/com/testcontainers/products/Application.java index be8dca6..368e892 100644 --- a/src/main/java/com/testcontainers/messages/Application.java +++ b/src/main/java/com/testcontainers/products/Application.java @@ -1,4 +1,4 @@ -package com.testcontainers.messages; +package com.testcontainers.products; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; diff --git a/src/main/java/com/testcontainers/messages/api/MessageController.java b/src/main/java/com/testcontainers/products/api/ProductController.java similarity index 50% rename from src/main/java/com/testcontainers/messages/api/MessageController.java rename to src/main/java/com/testcontainers/products/api/ProductController.java index d21d36b..06a75cc 100644 --- a/src/main/java/com/testcontainers/messages/api/MessageController.java +++ b/src/main/java/com/testcontainers/products/api/ProductController.java @@ -1,7 +1,7 @@ -package com.testcontainers.messages.api; +package com.testcontainers.products.api; -import com.testcontainers.messages.domain.Message; -import com.testcontainers.messages.domain.MessageRepository; +import com.testcontainers.products.domain.Product; +import com.testcontainers.products.domain.ProductRepository; import jakarta.validation.Valid; import java.util.List; import org.springframework.http.HttpStatus; @@ -13,23 +13,23 @@ import org.springframework.web.bind.annotation.RestController; @RestController -@RequestMapping("/api/messages") -class MessageController { +@RequestMapping("/api/products") +class ProductController { - private final MessageRepository messageRepository; + private final ProductRepository productRepository; - MessageController(MessageRepository messageRepository) { - this.messageRepository = messageRepository; + ProductController(ProductRepository productRepository) { + this.productRepository = productRepository; } @GetMapping - List getMessages() { - return messageRepository.getMessages(); + List getAll() { + return productRepository.getAll(); } @PostMapping @ResponseStatus(HttpStatus.CREATED) - Message createMessage(@RequestBody @Valid Message message) { - return messageRepository.createMessage(message); + Product createProduct(@RequestBody @Valid Product product) { + return productRepository.create(product); } } diff --git a/src/main/java/com/testcontainers/messages/config/SecurityConfig.java b/src/main/java/com/testcontainers/products/config/SecurityConfig.java similarity index 94% rename from src/main/java/com/testcontainers/messages/config/SecurityConfig.java rename to src/main/java/com/testcontainers/products/config/SecurityConfig.java index 390600c..dfda608 100644 --- a/src/main/java/com/testcontainers/messages/config/SecurityConfig.java +++ b/src/main/java/com/testcontainers/products/config/SecurityConfig.java @@ -1,4 +1,4 @@ -package com.testcontainers.messages.config; +package com.testcontainers.products.config; import static org.springframework.security.config.Customizer.withDefaults; @@ -18,9 +18,9 @@ public class SecurityConfig { @Bean SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { - http.authorizeHttpRequests(c -> c.requestMatchers(HttpMethod.GET, "/api/messages") + http.authorizeHttpRequests(c -> c.requestMatchers(HttpMethod.GET, "/api/products") .permitAll() - .requestMatchers(HttpMethod.POST, "/api/messages") + .requestMatchers(HttpMethod.POST, "/api/products") .authenticated() .anyRequest() .authenticated()) diff --git a/src/main/java/com/testcontainers/products/domain/Product.java b/src/main/java/com/testcontainers/products/domain/Product.java new file mode 100644 index 0000000..276ee93 --- /dev/null +++ b/src/main/java/com/testcontainers/products/domain/Product.java @@ -0,0 +1,5 @@ +package com.testcontainers.products.domain; + +import jakarta.validation.constraints.NotEmpty; + +public record Product(Long id, @NotEmpty String title, String description) {} diff --git a/src/main/java/com/testcontainers/products/domain/ProductRepository.java b/src/main/java/com/testcontainers/products/domain/ProductRepository.java new file mode 100644 index 0000000..600b24a --- /dev/null +++ b/src/main/java/com/testcontainers/products/domain/ProductRepository.java @@ -0,0 +1,22 @@ +package com.testcontainers.products.domain; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.atomic.AtomicLong; +import org.springframework.stereotype.Repository; + +@Repository +public class ProductRepository { + private static final AtomicLong ID = new AtomicLong(0L); + private static final List PRODUCTS = new ArrayList<>(); + + public List getAll() { + return List.copyOf(PRODUCTS); + } + + public Product create(Product product) { + Product p = new Product(ID.incrementAndGet(), product.title(), product.description()); + PRODUCTS.add(p); + return p; + } +} diff --git a/src/test/java/com/testcontainers/messages/ContainersConfig.java b/src/test/java/com/testcontainers/products/ContainersConfig.java similarity index 92% rename from src/test/java/com/testcontainers/messages/ContainersConfig.java rename to src/test/java/com/testcontainers/products/ContainersConfig.java index acd5755..4c531b5 100644 --- a/src/test/java/com/testcontainers/messages/ContainersConfig.java +++ b/src/test/java/com/testcontainers/products/ContainersConfig.java @@ -1,4 +1,4 @@ -package com.testcontainers.messages; +package com.testcontainers.products; import dasniko.testcontainers.keycloak.KeycloakContainer; import org.springframework.boot.test.context.TestConfiguration; @@ -6,7 +6,7 @@ import org.springframework.test.context.DynamicPropertyRegistry; @TestConfiguration(proxyBeanMethods = false) -class ContainersConfig { +public class ContainersConfig { static String KEYCLOAK_IMAGE = "quay.io/keycloak/keycloak:23.0"; static String realmImportFile = "/keycloaktcdemo-realm.json"; static String realmName = "keycloaktcdemo"; diff --git a/src/test/java/com/testcontainers/messages/TestApplication.java b/src/test/java/com/testcontainers/products/TestApplication.java similarity index 86% rename from src/test/java/com/testcontainers/messages/TestApplication.java rename to src/test/java/com/testcontainers/products/TestApplication.java index e1dea14..beccd05 100644 --- a/src/test/java/com/testcontainers/messages/TestApplication.java +++ b/src/test/java/com/testcontainers/products/TestApplication.java @@ -1,4 +1,4 @@ -package com.testcontainers.messages; +package com.testcontainers.products; import org.springframework.boot.SpringApplication; diff --git a/src/test/java/com/testcontainers/messages/ApplicationTests.java b/src/test/java/com/testcontainers/products/api/ProductControllerTests.java similarity index 86% rename from src/test/java/com/testcontainers/messages/ApplicationTests.java rename to src/test/java/com/testcontainers/products/api/ProductControllerTests.java index b700e3f..343f8e1 100644 --- a/src/test/java/com/testcontainers/messages/ApplicationTests.java +++ b/src/test/java/com/testcontainers/products/api/ProductControllerTests.java @@ -1,4 +1,4 @@ -package com.testcontainers.messages; +package com.testcontainers.products.api; import static io.restassured.RestAssured.given; import static io.restassured.RestAssured.when; @@ -6,6 +6,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; import com.fasterxml.jackson.annotation.JsonProperty; +import com.testcontainers.products.ContainersConfig; import io.restassured.RestAssured; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -23,7 +24,7 @@ @SpringBootTest(webEnvironment = RANDOM_PORT) @Import(ContainersConfig.class) -class ApplicationTests { +class ProductControllerTests { @LocalServerPort private int port; @@ -37,12 +38,12 @@ void setup() { } @Test - void shouldGetMessagesWithoutAuthToken() { - when().get("/api/messages").then().statusCode(200); + void shouldGetProductsWithoutAuthToken() { + when().get("/api/products").then().statusCode(200); } @Test - void shouldCreateMessageWithAuthToken() { + void shouldCreateProductWithAuthToken() { String token = getToken(); given().header("Authorization", "Bearer " + token) @@ -50,12 +51,12 @@ void shouldCreateMessageWithAuthToken() { .body( """ { - "content": "Test Message", - "createdBy": "admin" + "title": "New Product", + "description": "Brand New Product" } """) .when() - .get("/api/messages") + .get("/api/products") .then() .statusCode(200); }