-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
0 deletions.
There are no files selected for viewing
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
10
src/test/java/com/testcontainers/catalog/ApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
31
src/test/java/com/testcontainers/catalog/BaseIntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
48
src/test/java/com/testcontainers/catalog/ContainersConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
src/test/java/com/testcontainers/catalog/ProductControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
10
src/test/java/com/testcontainers/catalog/TestApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |