Skip to content

Commit

Permalink
Refactor domain to products
Browse files Browse the repository at this point in the history
  • Loading branch information
sivaprasadreddy committed Nov 30, 2023
1 parent 88f6e7a commit 4a70340
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 56 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
6 changes: 0 additions & 6 deletions src/main/java/com/testcontainers/messages/domain/Message.java

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.testcontainers.messages;
package com.testcontainers.products;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<Message> getMessages() {
return messageRepository.getMessages();
List<Product> 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);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.testcontainers.messages.config;
package com.testcontainers.products.config;

import static org.springframework.security.config.Customizer.withDefaults;

Expand All @@ -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())
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/testcontainers/products/domain/Product.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.testcontainers.products.domain;

import jakarta.validation.constraints.NotEmpty;

public record Product(Long id, @NotEmpty String title, String description) {}
Original file line number Diff line number Diff line change
@@ -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<Product> PRODUCTS = new ArrayList<>();

public List<Product> 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;
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package com.testcontainers.messages;
package com.testcontainers.products;

import dasniko.testcontainers.keycloak.KeycloakContainer;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.context.annotation.Bean;
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";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package com.testcontainers.messages;
package com.testcontainers.products;

import org.springframework.boot.SpringApplication;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package com.testcontainers.messages;
package com.testcontainers.products.api;

import static io.restassured.RestAssured.given;
import static io.restassured.RestAssured.when;
import static java.util.Collections.singletonList;
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;
Expand All @@ -23,7 +24,7 @@

@SpringBootTest(webEnvironment = RANDOM_PORT)
@Import(ContainersConfig.class)
class ApplicationTests {
class ProductControllerTests {

@LocalServerPort
private int port;
Expand All @@ -37,25 +38,25 @@ 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)
.contentType("application/json")
.body(
"""
{
"content": "Test Message",
"createdBy": "admin"
"title": "New Product",
"description": "Brand New Product"
}
""")
.when()
.get("/api/messages")
.get("/api/products")
.then()
.statusCode(200);
}
Expand Down

0 comments on commit 4a70340

Please sign in to comment.