From 19beda083f052f09a2ba55f5bcb04e67d93bf693 Mon Sep 17 00:00:00 2001 From: shelajev Date: Thu, 28 Mar 2024 00:40:20 +0200 Subject: [PATCH] mongo example --- pom.xml | 25 ++++++++++++------ .../domain/internal/ProductEntity.java | 26 +++++++------------ .../domain/internal/ProductRepository.java | 11 ++++---- .../catalog/domain/models/Product.java | 2 +- .../catalog/ContainersConfig.java | 17 ++++++++---- 5 files changed, 45 insertions(+), 36 deletions(-) diff --git a/pom.xml b/pom.xml index 9747232..31cb724 100644 --- a/pom.xml +++ b/pom.xml @@ -44,18 +44,27 @@ org.springframework.boot spring-boot-starter-actuator + + + + + + + + + + + + + org.springframework.boot - spring-boot-starter-data-jpa + spring-boot-starter-data-mongodb - org.postgresql - postgresql - runtime - - - org.flywaydb - flyway-core + org.testcontainers + mongodb + test org.springframework.kafka diff --git a/src/main/java/com/testcontainers/catalog/domain/internal/ProductEntity.java b/src/main/java/com/testcontainers/catalog/domain/internal/ProductEntity.java index 542da3f..c3895ca 100644 --- a/src/main/java/com/testcontainers/catalog/domain/internal/ProductEntity.java +++ b/src/main/java/com/testcontainers/catalog/domain/internal/ProductEntity.java @@ -1,29 +1,24 @@ package com.testcontainers.catalog.domain.internal; -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.GeneratedValue; -import jakarta.persistence.GenerationType; -import jakarta.persistence.Id; -import jakarta.persistence.Table; import jakarta.validation.constraints.DecimalMin; import jakarta.validation.constraints.NotEmpty; import jakarta.validation.constraints.NotNull; +import org.springframework.data.annotation.Id; +import org.springframework.data.mongodb.core.mapping.Document; + import java.math.BigDecimal; -@Entity -@Table(name = "products") +//@Entity +//@Table(name = "products") +@Document(collection = "products") class ProductEntity { @Id - @GeneratedValue(strategy = GenerationType.IDENTITY) - private Long id; + private String id; - @Column(nullable = false, unique = true) @NotEmpty(message = "Product code must not be null/empty") private String code; @NotEmpty(message = "Product name must not be null/empty") - @Column(nullable = false) private String name; private String description; @@ -31,12 +26,11 @@ class ProductEntity { private String image; @NotNull(message = "Product price must not be null") @DecimalMin("0.1") - @Column(nullable = false) private BigDecimal price; public ProductEntity() {} - public ProductEntity(Long id, String code, String name, String description, String image, BigDecimal price) { + public ProductEntity(String id, String code, String name, String description, String image, BigDecimal price) { this.id = id; this.code = code; this.name = name; @@ -45,11 +39,11 @@ public ProductEntity(Long id, String code, String name, String description, Stri this.price = price; } - public Long getId() { + public String getId() { return id; } - public void setId(Long id) { + public void setId(String id) { this.id = id; } diff --git a/src/main/java/com/testcontainers/catalog/domain/internal/ProductRepository.java b/src/main/java/com/testcontainers/catalog/domain/internal/ProductRepository.java index 09e5ec3..e4dadc4 100644 --- a/src/main/java/com/testcontainers/catalog/domain/internal/ProductRepository.java +++ b/src/main/java/com/testcontainers/catalog/domain/internal/ProductRepository.java @@ -1,15 +1,14 @@ package com.testcontainers.catalog.domain.internal; import java.util.Optional; -import org.springframework.data.jpa.repository.JpaRepository; -import org.springframework.data.jpa.repository.Modifying; -import org.springframework.data.jpa.repository.Query; + +import org.springframework.data.mongodb.repository.MongoRepository; +import org.springframework.data.mongodb.repository.Query; import org.springframework.data.repository.query.Param; -interface ProductRepository extends JpaRepository { +interface ProductRepository extends MongoRepository { Optional findByCode(String code); - @Modifying - @Query("update ProductEntity p set p.image = :image where p.code = :code") + @Query("db.products.updateOne({\"code\": \":code\"}, {$set: {\"image\": \":image\"}})") void updateProductImage(@Param("code") String code, @Param("image") String image); } diff --git a/src/main/java/com/testcontainers/catalog/domain/models/Product.java b/src/main/java/com/testcontainers/catalog/domain/models/Product.java index 829ab9f..f642849 100644 --- a/src/main/java/com/testcontainers/catalog/domain/models/Product.java +++ b/src/main/java/com/testcontainers/catalog/domain/models/Product.java @@ -3,4 +3,4 @@ import java.math.BigDecimal; public record Product( - Long id, String code, String name, String description, String imageUrl, BigDecimal price, boolean available) {} + String id, String code, String name, String description, String imageUrl, BigDecimal price, boolean available) {} diff --git a/src/test/java/com/testcontainers/catalog/ContainersConfig.java b/src/test/java/com/testcontainers/catalog/ContainersConfig.java index c25a1ad..df1fad8 100644 --- a/src/test/java/com/testcontainers/catalog/ContainersConfig.java +++ b/src/test/java/com/testcontainers/catalog/ContainersConfig.java @@ -11,6 +11,7 @@ import org.springframework.test.context.DynamicPropertyRegistry; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.KafkaContainer; +import org.testcontainers.containers.MongoDBContainer; import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.containers.localstack.LocalStackContainer; import org.testcontainers.elasticsearch.ElasticsearchContainer; @@ -19,18 +20,24 @@ @TestConfiguration(proxyBeanMethods = false) public class ContainersConfig { +// @Bean +// @ServiceConnection +// PostgreSQLContainer postgresContainer() { +// PostgreSQLContainer selfPostgreSQLContainer = new PostgreSQLContainer<>(parse("postgres:16-alpine")); +// return selfPostgreSQLContainer; +// } + @Bean @ServiceConnection - PostgreSQLContainer postgresContainer() { - PostgreSQLContainer selfPostgreSQLContainer = new PostgreSQLContainer<>(parse("postgres:16-alpine")); - - return selfPostgreSQLContainer; + MongoDBContainer mongodb() { + var mongo = new MongoDBContainer("mongo:7.0.7-jammy"); + return mongo; } @Bean @ServiceConnection KafkaContainer kafkaContainer() { - return new KafkaContainer(parse("confluentinc/cp-kafka:7.5.0")).withReuse(true); + return new KafkaContainer(parse("confluentinc/cp-kafka:7.5.0")); } @Bean("localstackContainer")