From dd6378fc75c43a1901ca4dfb277cbc072d448f96 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Mon, 18 Nov 2024 18:06:21 +0000 Subject: [PATCH] feat : rewrite containers config --- .../TestInventoryApplication.java | 30 ++-------------- .../common/AbstractIntegrationTest.java | 8 +++-- ...rsConfig.java => SQLContainersConfig.java} | 2 +- .../config/NonSQLContainersConfig.java | 34 +++++++++++++++++++ .../repositories/InventoryRepositoryTest.java | 4 +-- .../JOOQInventoryRepositoryTest.java | 4 +-- 6 files changed, 48 insertions(+), 34 deletions(-) rename inventory-service/src/test/java/com/example/inventoryservice/common/{ContainersConfig.java => SQLContainersConfig.java} (95%) create mode 100644 inventory-service/src/test/java/com/example/inventoryservice/config/NonSQLContainersConfig.java diff --git a/inventory-service/src/test/java/com/example/inventoryservice/TestInventoryApplication.java b/inventory-service/src/test/java/com/example/inventoryservice/TestInventoryApplication.java index b5d7c50d..7211c4ba 100644 --- a/inventory-service/src/test/java/com/example/inventoryservice/TestInventoryApplication.java +++ b/inventory-service/src/test/java/com/example/inventoryservice/TestInventoryApplication.java @@ -6,40 +6,16 @@ Licensed under MIT License Copyright (c) 2023-2024 Raja Kolli. package com.example.inventoryservice; -import com.example.inventoryservice.common.ContainersConfig; +import com.example.inventoryservice.common.SQLContainersConfig; +import com.example.inventoryservice.config.NonSQLContainersConfig; import org.springframework.boot.SpringApplication; -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.Import; -import org.testcontainers.containers.GenericContainer; -import org.testcontainers.containers.KafkaContainer; -import org.testcontainers.utility.DockerImageName; -@TestConfiguration(proxyBeanMethods = false) -@Import(ContainersConfig.class) public class TestInventoryApplication { - @Bean - @ServiceConnection(name = "openzipkin/zipkin") - GenericContainer zipkContainer() { - return new GenericContainer<>(DockerImageName.parse("openzipkin/zipkin")) - .withExposedPorts(9411) - .withReuse(true); - } - - @Bean - @ServiceConnection - KafkaContainer kafkaContainer() { - return new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka").withTag("7.7.1")) - .withKraft() - .withReuse(true); - } - public static void main(String[] args) { System.setProperty("spring.profiles.active", "test"); SpringApplication.from(InventoryServiceApplication::main) - .with(TestInventoryApplication.class) + .with(NonSQLContainersConfig.class, SQLContainersConfig.class) .run(args); } } diff --git a/inventory-service/src/test/java/com/example/inventoryservice/common/AbstractIntegrationTest.java b/inventory-service/src/test/java/com/example/inventoryservice/common/AbstractIntegrationTest.java index fc24c380..acaca0fe 100644 --- a/inventory-service/src/test/java/com/example/inventoryservice/common/AbstractIntegrationTest.java +++ b/inventory-service/src/test/java/com/example/inventoryservice/common/AbstractIntegrationTest.java @@ -9,7 +9,7 @@ Licensed under MIT License Copyright (c) 2021-2024 Raja Kolli. import static com.example.inventoryservice.utils.AppConstants.PROFILE_TEST; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; -import com.example.inventoryservice.TestInventoryApplication; +import com.example.inventoryservice.config.NonSQLContainersConfig; import com.example.inventoryservice.config.TestStockOrderListenerConfig; import com.example.inventoryservice.repositories.InventoryRepository; import com.fasterxml.jackson.databind.ObjectMapper; @@ -23,7 +23,11 @@ Licensed under MIT License Copyright (c) 2021-2024 Raja Kolli. @ActiveProfiles({PROFILE_TEST}) @SpringBootTest( webEnvironment = RANDOM_PORT, - classes = {TestInventoryApplication.class, TestStockOrderListenerConfig.class}) + classes = { + SQLContainersConfig.class, + NonSQLContainersConfig.class, + TestStockOrderListenerConfig.class + }) @AutoConfigureMockMvc public abstract class AbstractIntegrationTest { diff --git a/inventory-service/src/test/java/com/example/inventoryservice/common/ContainersConfig.java b/inventory-service/src/test/java/com/example/inventoryservice/common/SQLContainersConfig.java similarity index 95% rename from inventory-service/src/test/java/com/example/inventoryservice/common/ContainersConfig.java rename to inventory-service/src/test/java/com/example/inventoryservice/common/SQLContainersConfig.java index 82ba2a12..5710f3e9 100644 --- a/inventory-service/src/test/java/com/example/inventoryservice/common/ContainersConfig.java +++ b/inventory-service/src/test/java/com/example/inventoryservice/common/SQLContainersConfig.java @@ -13,7 +13,7 @@ Licensed under MIT License Copyright (c) 2023 Raja Kolli. import org.testcontainers.utility.DockerImageName; @TestConfiguration(proxyBeanMethods = false) -public class ContainersConfig { +public class SQLContainersConfig { @Bean @ServiceConnection diff --git a/inventory-service/src/test/java/com/example/inventoryservice/config/NonSQLContainersConfig.java b/inventory-service/src/test/java/com/example/inventoryservice/config/NonSQLContainersConfig.java new file mode 100644 index 00000000..61586949 --- /dev/null +++ b/inventory-service/src/test/java/com/example/inventoryservice/config/NonSQLContainersConfig.java @@ -0,0 +1,34 @@ +/*** +

+ Licensed under MIT License Copyright (c) 2024 Raja Kolli. +

+***/ + +package com.example.inventoryservice.config; + +import org.springframework.boot.test.context.TestConfiguration; +import org.springframework.boot.testcontainers.service.connection.ServiceConnection; +import org.springframework.context.annotation.Bean; +import org.testcontainers.containers.GenericContainer; +import org.testcontainers.containers.KafkaContainer; +import org.testcontainers.utility.DockerImageName; + +@TestConfiguration(proxyBeanMethods = false) +public class NonSQLContainersConfig { + + @Bean + @ServiceConnection(name = "openzipkin/zipkin") + GenericContainer zipkContainer() { + return new GenericContainer<>(DockerImageName.parse("openzipkin/zipkin")) + .withExposedPorts(9411) + .withReuse(true); + } + + @Bean + @ServiceConnection + KafkaContainer kafkaContainer() { + return new KafkaContainer(DockerImageName.parse("confluentinc/cp-kafka").withTag("7.7.1")) + .withKraft() + .withReuse(true); + } +} diff --git a/inventory-service/src/test/java/com/example/inventoryservice/repositories/InventoryRepositoryTest.java b/inventory-service/src/test/java/com/example/inventoryservice/repositories/InventoryRepositoryTest.java index 586b19e6..bc9280fc 100644 --- a/inventory-service/src/test/java/com/example/inventoryservice/repositories/InventoryRepositoryTest.java +++ b/inventory-service/src/test/java/com/example/inventoryservice/repositories/InventoryRepositoryTest.java @@ -8,7 +8,7 @@ Licensed under MIT License Copyright (c) 2023 Raja Kolli. import static org.assertj.core.api.Assertions.assertThat; -import com.example.inventoryservice.common.ContainersConfig; +import com.example.inventoryservice.common.SQLContainersConfig; import com.zaxxer.hikari.HikariDataSource; import javax.sql.DataSource; import org.junit.jupiter.api.Test; @@ -22,7 +22,7 @@ Licensed under MIT License Copyright (c) 2023 Raja Kolli. "spring.test.database.replace=none", "spring.cloud.config.enabled=false" }) -@Import(ContainersConfig.class) +@Import(SQLContainersConfig.class) class InventoryRepositoryTest { @Autowired private DataSource datasource; diff --git a/inventory-service/src/test/java/com/example/inventoryservice/repositories/JOOQInventoryRepositoryTest.java b/inventory-service/src/test/java/com/example/inventoryservice/repositories/JOOQInventoryRepositoryTest.java index 51ca653c..93476d82 100644 --- a/inventory-service/src/test/java/com/example/inventoryservice/repositories/JOOQInventoryRepositoryTest.java +++ b/inventory-service/src/test/java/com/example/inventoryservice/repositories/JOOQInventoryRepositoryTest.java @@ -9,7 +9,7 @@ Licensed under MIT License Copyright (c) 2023 Raja Kolli. import static com.example.inventoryservice.jooq.tables.Inventory.INVENTORY; import static org.assertj.core.api.Assertions.assertThat; -import com.example.inventoryservice.common.ContainersConfig; +import com.example.inventoryservice.common.SQLContainersConfig; import com.example.inventoryservice.entities.Inventory; import java.util.List; import org.jooq.DSLContext; @@ -20,7 +20,7 @@ Licensed under MIT License Copyright (c) 2023 Raja Kolli. import org.springframework.context.annotation.Import; @JooqTest(properties = {"spring.test.database.replace=none", "spring.cloud.config.enabled=false"}) -@Import(ContainersConfig.class) +@Import(SQLContainersConfig.class) class JOOQInventoryRepositoryTest { @Autowired private DSLContext dslContext;