-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #28 from emeraldhieu/integration-test-with-testcon…
…tainers Integration-test with testcontainers
- Loading branch information
Showing
11 changed files
with
270 additions
and
2 deletions.
There are no files selected for viewing
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
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
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
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
59 changes: 59 additions & 0 deletions
59
order/src/test/java/com/emeraldhieu/vinci/order/logic/BaseTestContainersTest.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,59 @@ | ||
package com.emeraldhieu.vinci.order.logic; | ||
|
||
import com.emeraldhieu.vinci.order.logic.config.KafkaTestConfiguration; | ||
import org.springframework.boot.testcontainers.service.connection.ServiceConnection; | ||
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.KafkaContainer; | ||
import org.testcontainers.containers.Network; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
import org.testcontainers.junit.jupiter.Container; | ||
import org.testcontainers.junit.jupiter.Testcontainers; | ||
import org.testcontainers.utility.DockerImageName; | ||
|
||
@Testcontainers | ||
@Import({ | ||
KafkaTestConfiguration.class, | ||
}) | ||
public class BaseTestContainersTest { | ||
|
||
@Container | ||
@ServiceConnection | ||
private static PostgreSQLContainer<?> postgres = | ||
new PostgreSQLContainer<>(DockerImageName.parse("postgres:15.3-alpine")) | ||
.withReuse(true); | ||
|
||
private static Network network = Network.newNetwork(); | ||
|
||
// Cluster ID is created by "kafka-storage random-uuid" | ||
private static String clusterId = "qYoMEZXcS_SKP2PzAl8-WA"; | ||
|
||
@Container | ||
@ServiceConnection | ||
private static KafkaContainer kafka = | ||
new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka:7.4.0")) | ||
.withNetwork(network) | ||
.withKraft() | ||
.withClusterId(clusterId) | ||
.withReuse(true); | ||
|
||
@Container | ||
private static GenericContainer schemaRegistry = | ||
new GenericContainer(DockerImageName.parse("confluentinc/cp-schema-registry:7.4.0")) | ||
.withNetwork(network) | ||
.withExposedPorts(8081) // Exposed port is used to get a mapped port. Otherwise, the error "Container doesn't expose any ports" occurs. | ||
.withEnv("SCHEMA_REGISTRY_HOST_NAME", "schema-registry") // To be resolved by Docker | ||
.withEnv("SCHEMA_REGISTRY_LISTENERS", "http://0.0.0.0:8081") // Seems optional | ||
.withEnv("SCHEMA_REGISTRY_KAFKASTORE_BOOTSTRAP_SERVERS", | ||
"PLAINTEXT://" + kafka.getNetworkAliases().get(0) + ":9092") // A list of Kafka brokers to connect to | ||
.dependsOn(kafka) | ||
.withReuse(true); | ||
|
||
@DynamicPropertySource | ||
private static void properties(DynamicPropertyRegistry registry) { | ||
registry.add("spring.kafka.properties.schema.registry.url", | ||
() -> "http://" + schemaRegistry.getHost() + ":" + schemaRegistry.getFirstMappedPort()); | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
order/src/test/java/com/emeraldhieu/vinci/order/logic/OrderServiceIT.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,41 @@ | ||
package com.emeraldhieu.vinci.order.logic; | ||
|
||
import com.emeraldhieu.vinci.order.OrderApp; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.test.context.ActiveProfiles; | ||
|
||
import java.util.List; | ||
|
||
import static org.assertj.core.api.AssertionsForClassTypes.assertThat; | ||
|
||
@ActiveProfiles("test") | ||
@SpringBootTest( | ||
classes = OrderApp.class | ||
) | ||
class OrderServiceIT extends BaseTestContainersTest { | ||
|
||
@Autowired | ||
private OrderService orderService; | ||
|
||
@Test | ||
public void givenRequest_whenCreate_thenReturnResponse() { | ||
// GIVEN | ||
var products = List.of("pepperoni", "margherita", "marinara"); | ||
var request = OrderRequest.builder() | ||
.products(products) | ||
.build(); | ||
|
||
// WHEN | ||
var createdOrder = orderService.create(request); | ||
|
||
// THEN | ||
assertThat(createdOrder.getId()).isNotNull(); | ||
assertThat(createdOrder.getProducts()).isEqualTo(products); | ||
assertThat(createdOrder.getCreatedBy()).isNotNull(); | ||
assertThat(createdOrder.getCreatedAt()).isNotNull(); | ||
assertThat(createdOrder.getUpdatedBy()).isNotNull(); | ||
assertThat(createdOrder.getUpdatedAt()).isNotNull(); | ||
} | ||
} |
30 changes: 30 additions & 0 deletions
30
order/src/test/java/com/emeraldhieu/vinci/order/logic/config/KafkaTestConfiguration.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,30 @@ | ||
package com.emeraldhieu.vinci.order.logic.config; | ||
|
||
import lombok.RequiredArgsConstructor; | ||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.springframework.boot.autoconfigure.kafka.DefaultKafkaConsumerFactoryCustomizer; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
|
||
import java.util.Map; | ||
|
||
/** | ||
* A class that customizes Kafka for testing. | ||
*/ | ||
@TestConfiguration | ||
@RequiredArgsConstructor | ||
public class KafkaTestConfiguration { | ||
|
||
private final KafkaTestProperties kafkaTestProperties; | ||
|
||
@Bean | ||
DefaultKafkaConsumerFactoryCustomizer defaultKafkaConsumerFactoryCustomizer() { | ||
return consumerFactory -> { | ||
Map<String, Object> additionalConfigs = Map.of( | ||
// Set consumer's groupId which hasn't been set anywhere. | ||
ConsumerConfig.GROUP_ID_CONFIG, kafkaTestProperties.getGroupId() | ||
); | ||
consumerFactory.updateConfigs(additionalConfigs); | ||
}; | ||
} | ||
} |
21 changes: 21 additions & 0 deletions
21
order/src/test/java/com/emeraldhieu/vinci/order/logic/config/KafkaTestProperties.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,21 @@ | ||
package com.emeraldhieu.vinci.order.logic.config; | ||
|
||
import com.emeraldhieu.vinci.order.config.KafkaProperties; | ||
import lombok.Data; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.boot.context.properties.ConfigurationPropertiesScan; | ||
|
||
/** | ||
* A properties class used for testing because it contains some properties not existed in {@link KafkaProperties}. | ||
* --- | ||
* {@link ConfigurationProperties} is scanned by {@link ConfigurationPropertiesScan}. | ||
*/ | ||
@ConfigurationProperties(prefix = "application.kafka") | ||
@Data | ||
public class KafkaTestProperties { | ||
private String bootstrapAddress; | ||
private String topic; | ||
private int partitions; | ||
private int replicationFactor; | ||
private String groupId; | ||
} |
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,9 @@ | ||
spring: | ||
jpa: | ||
hibernate: | ||
ddl-auto: create-drop | ||
|
||
logging: | ||
level: | ||
org.springframework.core.io.support: DEBUG | ||
org.springframework.context.annotation: DEBUG |
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,64 @@ | ||
spring: | ||
profiles: | ||
active: test # Activate profile "test" by default | ||
jackson: | ||
default-property-inclusion: non_null | ||
messages: | ||
basename: i18n/messages | ||
mvc: | ||
# Throw NoHandlerFoundException | ||
# See https://stackoverflow.com/questions/36733254/spring-boot-rest-how-to-configure-404-resource-not-found#36734193 | ||
throw-exception-if-no-handler-found: true | ||
problemdetails: | ||
enabled: true | ||
web: | ||
resources: | ||
# Throw NoHandlerFoundException | ||
# See https://stackoverflow.com/questions/36733254/spring-boot-rest-how-to-configure-404-resource-not-found#36734193 | ||
add-mappings: false | ||
datasource: | ||
type: com.zaxxer.hikari.HikariDataSource | ||
hikari: | ||
poolName: Hikari | ||
auto-commit: false | ||
jpa: | ||
database-platform: org.hibernate.dialect.PostgreSQLDialect | ||
open-in-view: false | ||
liquibase: | ||
change-log: classpath:liquibase/master.yml | ||
kafka: | ||
producer: | ||
key-serializer: io.confluent.kafka.serializers.KafkaAvroSerializer | ||
value-serializer: io.confluent.kafka.serializers.KafkaAvroSerializer | ||
consumer: | ||
key-deserializer: io.confluent.kafka.serializers.KafkaAvroDeserializer | ||
value-deserializer: io.confluent.kafka.serializers.KafkaAvroDeserializer | ||
# Determine which offset to start reading from if the offset doesn't exist on the server. | ||
# See https://docs.confluent.io/platform/current/installation/configuration/consumer-configs.html#auto-offset-reset | ||
auto-offset-reset: latest | ||
properties: | ||
# Deserialize to the generated Avro class rather than a GenericRecord type | ||
specific.avro.reader: true | ||
server: | ||
tomcat: | ||
# Allow pipe character in URL to support passing commas such as "createdAt,asc" | ||
relaxed-query-chars: "|" | ||
management: | ||
endpoints: | ||
web: | ||
exposure: | ||
# Expose all actuator endpoints | ||
include: "*" | ||
endpoint: | ||
health: | ||
# Enable liveness and readiness probes | ||
probes: | ||
enabled: true | ||
# Show full health details | ||
show-details: always | ||
application: | ||
kafka: | ||
topic: orders | ||
partitions: 1 | ||
replicationFactor: 1 | ||
groupId: consumer |
1 change: 1 addition & 0 deletions
1
order/src/test/resources/testcontainers/.testcontainers.properties
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 @@ | ||
testcontainers.reuse.enable=true |