Skip to content

Commit

Permalink
mongo example
Browse files Browse the repository at this point in the history
  • Loading branch information
shelajev committed Mar 27, 2024
1 parent 4d4ddba commit 19beda0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 36 deletions.
25 changes: 17 additions & 8 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -44,18 +44,27 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- <dependency>-->
<!-- <groupId>org.springframework.boot</groupId>-->
<!-- <artifactId>spring-boot-starter-data-jpa</artifactId>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.postgresql</groupId>-->
<!-- <artifactId>postgresql</artifactId>-->
<!-- <scope>runtime</scope>-->
<!-- </dependency>-->
<!-- <dependency>-->
<!-- <groupId>org.flywaydb</groupId>-->
<!-- <artifactId>flyway-core</artifactId>-->
<!-- </dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.flywaydb</groupId>
<artifactId>flyway-core</artifactId>
<groupId>org.testcontainers</groupId>
<artifactId>mongodb</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.kafka</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,36 @@
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;

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;
Expand All @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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<ProductEntity, Long> {
interface ProductRepository extends MongoRepository<ProductEntity, String> {
Optional<ProductEntity> 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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -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) {}
17 changes: 12 additions & 5 deletions src/test/java/com/testcontainers/catalog/ContainersConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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")
Expand Down

0 comments on commit 19beda0

Please sign in to comment.