Skip to content

Commit

Permalink
containers set up
Browse files Browse the repository at this point in the history
  • Loading branch information
shelajev committed Jan 18, 2024
1 parent 64bc0a2 commit 655fb4f
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 0 deletions.
Binary file added src/main/resources/P101.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/test/java/com/testcontainers/catalog/ApplicationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.testcontainers.catalog;

import org.junit.jupiter.api.Test;

class ApplicationTest extends BaseIntegrationTest {
@Test
void contextLoads() {

}
}
31 changes: 31 additions & 0 deletions src/test/java/com/testcontainers/catalog/BaseIntegrationTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package com.testcontainers.catalog;

import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;

import io.restassured.RestAssured;
import org.junit.jupiter.api.BeforeEach;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.server.LocalServerPort;
import org.springframework.context.annotation.Import;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.utility.DockerImageName;

@SpringBootTest(
webEnvironment = RANDOM_PORT,
properties = {
"spring.kafka.consumer.auto-offset-reset=earliest"
})
@Import(ContainersConfig.class)
public abstract class BaseIntegrationTest {

@LocalServerPort
private int port;

@BeforeEach
void setUpBase() {
RestAssured.port = port;
}
}
48 changes: 48 additions & 0 deletions src/test/java/com/testcontainers/catalog/ContainersConfig.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.testcontainers.catalog;

import static org.testcontainers.utility.DockerImageName.parse;

import com.testcontainers.catalog.domain.FileStorageService;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.testcontainers.service.connection.ServiceConnection;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.DependsOn;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.testcontainers.containers.GenericContainer;
import org.testcontainers.containers.KafkaContainer;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.containers.localstack.LocalStackContainer;
import org.testcontainers.elasticsearch.ElasticsearchContainer;

@TestConfiguration(proxyBeanMethods = false)
public class ContainersConfig {

@Bean
@ServiceConnection
PostgreSQLContainer<?> postgresContainer() {
return new PostgreSQLContainer<>(parse("postgres:16-alpine"));
}

@Bean
@ServiceConnection
KafkaContainer kafkaContainer() {
return new KafkaContainer(parse("confluentinc/cp-kafka:7.5.0")).withReuse(true);
}

@Bean("localstackContainer")
LocalStackContainer localstackContainer(DynamicPropertyRegistry registry) {
LocalStackContainer localStack = new LocalStackContainer(parse("localstack/localstack:2.3")).withReuse(true);
registry.add("spring.cloud.aws.credentials.access-key", localStack::getAccessKey);
registry.add("spring.cloud.aws.credentials.secret-key", localStack::getSecretKey);
registry.add("spring.cloud.aws.region.static", localStack::getRegion);
registry.add("spring.cloud.aws.endpoint", localStack::getEndpoint);
return localStack;
}

@Bean
@DependsOn("localstackContainer")
ApplicationRunner awsInitializer(ApplicationProperties properties, FileStorageService fileStorageService) {
return args -> fileStorageService.createBucket(properties.productImagesBucketName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.testcontainers.catalog;

import com.testcontainers.catalog.BaseIntegrationTest;
import com.testcontainers.catalog.domain.ProductService;
import com.testcontainers.catalog.domain.models.Product;
import io.restassured.http.ContentType;
import org.junit.jupiter.api.Test;
import java.util.UUID;

import static io.restassured.RestAssured.given;
import static org.hamcrest.CoreMatchers.endsWith;

class ProductControllerTest extends BaseIntegrationTest {

@Test
void createProductSuccessfully() {
String code = UUID.randomUUID().toString();
given().contentType(ContentType.JSON)
.body(
"""
{
"code": "%s",
"name": "Product %s",
"description": "Product %s description",
"price": 10.0
}
"""
.formatted(code, code, code))
.when()
.post("/api/products")
.then()
.statusCode(201)
.header("Location", endsWith("/api/products/%s".formatted(code)));
}

}
10 changes: 10 additions & 0 deletions src/test/java/com/testcontainers/catalog/TestApplication.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.testcontainers.catalog;

import org.springframework.boot.SpringApplication;

public class TestApplication {

public static void main(String[] args) {
SpringApplication.from(Application::main).with(ContainersConfig.class).run(args);
}
}

0 comments on commit 655fb4f

Please sign in to comment.