Skip to content

Commit ffa8ac8

Browse files
committed
refactoring integration tests to use testcontainers
1 parent ada8b54 commit ffa8ac8

File tree

8 files changed

+96
-17
lines changed

8 files changed

+96
-17
lines changed

pom.xml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
<maven-surefire-plugin.version>3.2.5</maven-surefire-plugin.version>
4444
<log4j-version>2.20.0</log4j-version>
4545
<http2-common.version>11.0.20</http2-common.version>
46+
<test-containers.version>1.19.7</test-containers.version>
4647
<!-- Docker -->
4748
<jib-maven-plugin.version>3.4.1</jib-maven-plugin.version>
4849
</properties>
@@ -60,6 +61,13 @@
6061
</dependencyManagement>
6162

6263
<dependencies>
64+
<dependency>
65+
<groupId>org.testcontainers</groupId>
66+
<artifactId>testcontainers-bom</artifactId>
67+
<version>${test-containers.version}</version>
68+
<type>pom</type>
69+
<scope>import</scope>
70+
</dependency>
6371
<dependency>
6472
<groupId>org.springframework.boot</groupId>
6573
<artifactId>spring-boot-starter-actuator</artifactId>
@@ -159,7 +167,6 @@
159167
</exclusion>
160168
</exclusions>
161169
</dependency>
162-
163170
<dependency>
164171
<groupId>org.springframework.boot</groupId>
165172
<artifactId>spring-boot-starter-test</artifactId>
@@ -171,7 +178,6 @@
171178
</exclusion>
172179
</exclusions>
173180
</dependency>
174-
175181
<dependency>
176182
<groupId>uk.gov.companieshouse</groupId>
177183
<artifactId>structured-logging</artifactId>
@@ -195,7 +201,6 @@
195201
<artifactId>commons-beanutils</artifactId>
196202
<version>${commons.beanutils.version}</version>
197203
</dependency>
198-
199204
<dependency>
200205
<groupId>uk.gov.companieshouse</groupId>
201206
<artifactId>ch-kafka</artifactId>
@@ -206,12 +211,10 @@
206211
<artifactId>kafka-models</artifactId>
207212
<version>${kafka-models.version}</version>
208213
</dependency>
209-
210214
<dependency>
211215
<groupId>org.hibernate.validator</groupId>
212216
<artifactId>hibernate-validator</artifactId>
213217
</dependency>
214-
215218
<dependency>
216219
<groupId>org.springframework.kafka</groupId>
217220
<artifactId>spring-kafka-test</artifactId>
@@ -258,9 +261,13 @@
258261
<version>${api-sdk-manager-java-library.version}</version>
259262
</dependency>
260263
<dependency>
261-
<groupId>de.flapdoodle.embed</groupId>
262-
<artifactId>de.flapdoodle.embed.mongo</artifactId>
263-
<version>${de-flapdoodle-embed-mongo.version}</version>
264+
<groupId>org.testcontainers</groupId>
265+
<artifactId>mongodb</artifactId>
266+
<scope>test</scope>
267+
</dependency>
268+
<dependency>
269+
<groupId>org.testcontainers</groupId>
270+
<artifactId>junit-jupiter</artifactId>
264271
<scope>test</scope>
265272
</dependency>
266273
<dependency>
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package uk.gov.companieshouse.orders.api.config;
2+
3+
import org.springframework.test.context.DynamicPropertyRegistry;
4+
import org.springframework.test.context.DynamicPropertySource;
5+
import org.testcontainers.containers.MongoDBContainer;
6+
import org.testcontainers.utility.DockerImageName;
7+
8+
public class AbstractMongoConfig {
9+
10+
public static final MongoDBContainer mongoDBContainer = new MongoDBContainer(
11+
DockerImageName.parse("mongo:5"));
12+
13+
@DynamicPropertySource
14+
public static void setProperties(DynamicPropertyRegistry registry) {
15+
registry.add("spring.data.mongodb.uri", mongoDBContainer::getReplicaSetUrl);
16+
mongoDBContainer.start();
17+
}
18+
}

src/test/java/uk/gov/companieshouse/orders/api/controller/BasketControllerIntegrationTest.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@
6969
import jakarta.servlet.http.HttpServletRequest;
7070

7171
import org.junit.jupiter.api.AfterEach;
72+
import org.junit.jupiter.api.BeforeAll;
7273
import org.junit.jupiter.api.BeforeEach;
7374
import org.junit.jupiter.api.DisplayName;
7475
import org.junit.jupiter.api.Test;
@@ -87,9 +88,11 @@
8788
import org.springframework.test.web.servlet.MvcResult;
8889
import org.springframework.test.web.servlet.ResultActions;
8990
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
91+
import org.testcontainers.junit.jupiter.Testcontainers;
9092
import uk.gov.companieshouse.api.error.ApiErrorResponseException;
9193
import uk.gov.companieshouse.api.model.payment.PaymentApi;
9294
import uk.gov.companieshouse.api.util.security.Permission;
95+
import uk.gov.companieshouse.orders.api.config.AbstractMongoConfig;
9396
import uk.gov.companieshouse.orders.api.dto.AddDeliveryDetailsRequestDTO;
9497
import uk.gov.companieshouse.orders.api.dto.BasketItemDTO;
9598
import uk.gov.companieshouse.orders.api.dto.BasketPaymentRequestDTO;
@@ -126,11 +129,12 @@
126129
import uk.gov.companieshouse.orders.api.validator.CheckoutBasketValidator;
127130
import uk.gov.companieshouse.sdk.manager.ApiSdkManager;
128131

132+
@Testcontainers
129133
@DirtiesContext
130134
@AutoConfigureMockMvc
131135
@SpringBootTest
132136
@EmbeddedKafka
133-
class BasketControllerIntegrationTest {
137+
class BasketControllerIntegrationTest extends AbstractMongoConfig {
134138

135139
private static final String OLD_CERTIFICATE_URI = "/orderable/certificates/11111111";
136140

@@ -159,11 +163,10 @@ class BasketControllerIntegrationTest {
159163
private static final Boolean POSTAL_DELIVERY = true;
160164
private static final Integer QUANTITY = 1;
161165
private static final LocalDateTime SATISFIED_AT = LocalDateTime.of(2020, Month.JANUARY, 1, 0, 0);
162-
163166
private static final String EXPECTED_TOTAL_ORDER_COST = "15";
164167
private static final String EXPECTED_TOTAL_ORDER_COST_MULTIPLE = "33";
165168
private static final int EXPECTED_CHECKOUT_ITEMS_SIZE = 3;
166-
private static final String DISCOUNT_APPLIED_1 = "0";
169+
private static final String DISCOUNT_APPLIED_1 = "0";
167170
private static final String ITEM_COST_1 = "5";
168171
private static final String CALCULATED_COST_1 = "5";
169172
private static final String DISCOUNT_APPLIED_2 = "10";
@@ -173,7 +176,6 @@ class BasketControllerIntegrationTest {
173176
private static final String ITEM_COST_3 = "5";
174177
private static final String CALCULATED_COST_3 = "5";
175178
private static final String INVALID_ITEM_URI = "invalid_uri";
176-
177179
private static final String PAYMENT_REQUIRED_HEADER = "x-payment-required";
178180
private static final String COSTS_LINK = "payments.service/payments";
179181

@@ -267,6 +269,11 @@ class BasketControllerIntegrationTest {
267269
@Mock
268270
private CheckoutBasketValidator checkoutBasketValidator;
269271

272+
@BeforeAll
273+
static void setup() {
274+
mongoDBContainer.start();
275+
}
276+
270277
@BeforeEach
271278
void setUp() {
272279
timestamps = new TimestampedEntityVerifier();

src/test/java/uk/gov/companieshouse/orders/api/controller/OrderControllerIntegrationTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import org.hamcrest.core.StringContains;
55
import org.junit.jupiter.api.AfterEach;
6+
import org.junit.jupiter.api.BeforeAll;
67
import org.junit.jupiter.api.Assertions;
78
import org.junit.jupiter.api.DisplayName;
89
import org.junit.jupiter.api.Test;
@@ -20,7 +21,9 @@
2021
import org.springframework.test.web.servlet.MockMvc;
2122
import org.springframework.test.web.servlet.MvcResult;
2223
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
24+
import org.testcontainers.junit.jupiter.Testcontainers;
2325
import uk.gov.companieshouse.api.util.security.Permission;
26+
import uk.gov.companieshouse.orders.api.config.AbstractMongoConfig;
2427
import uk.gov.companieshouse.orders.api.dto.PatchOrderedItemDTO;
2528
import uk.gov.companieshouse.orders.api.exceptionhandler.ConstraintValidationError;
2629
import uk.gov.companieshouse.orders.api.model.Certificate;
@@ -80,12 +83,13 @@
8083
import static uk.gov.companieshouse.orders.api.util.TestConstants.TOKEN_REQUEST_ID_VALUE;
8184
import static uk.gov.companieshouse.orders.api.util.TestConstants.WRONG_ERIC_IDENTITY_VALUE;
8285

86+
@Testcontainers
8387
@DirtiesContext
8488
@AutoConfigureMockMvc
8589
@SpringBootTest
8690
@EmbeddedKafka
8791
@ActiveProfiles({"orders-search-enabled", "orders-search-multibasket-enabled"})
88-
class OrderControllerIntegrationTest {
92+
class OrderControllerIntegrationTest extends AbstractMongoConfig {
8993
private static final String ORDER_ID = "0001";
9094
private static final String ORDER_REFERENCE = "0001";
9195
private static final String CHECKOUT_ID = "0002";
@@ -110,6 +114,11 @@ class OrderControllerIntegrationTest {
110114
@Autowired
111115
private ObjectMapper mapper;
112116

117+
@BeforeAll
118+
static void setup() {
119+
mongoDBContainer.start();
120+
}
121+
113122
@AfterEach
114123
void tearDown() {
115124
orderRepository.deleteAll();

src/test/java/uk/gov/companieshouse/orders/api/controller/OrdersSearchMultibasketDisabledIntegrationTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import java.time.LocalDate;
1818
import java.util.Collections;
1919
import org.junit.jupiter.api.AfterEach;
20+
import org.junit.jupiter.api.BeforeAll;
2021
import org.junit.jupiter.api.DisplayName;
2122
import org.junit.jupiter.api.Test;
2223
import org.springframework.beans.factory.annotation.Autowired;
@@ -27,7 +28,9 @@
2728
import org.springframework.test.annotation.DirtiesContext;
2829
import org.springframework.test.context.ActiveProfiles;
2930
import org.springframework.test.web.servlet.MockMvc;
31+
import org.testcontainers.junit.jupiter.Testcontainers;
3032
import uk.gov.companieshouse.api.util.security.Permission;
33+
import uk.gov.companieshouse.orders.api.config.AbstractMongoConfig;
3134
import uk.gov.companieshouse.orders.api.model.CheckoutSearchResults;
3235
import uk.gov.companieshouse.orders.api.model.CheckoutSummary;
3336
import uk.gov.companieshouse.orders.api.model.HRef;
@@ -37,12 +40,13 @@
3740
import uk.gov.companieshouse.orders.api.util.StubHelper;
3841
import uk.gov.companieshouse.orders.api.util.TestConstants;
3942

43+
@Testcontainers
4044
@DirtiesContext
4145
@AutoConfigureMockMvc
4246
@SpringBootTest
4347
@EmbeddedKafka
4448
@ActiveProfiles({"orders-search-enabled", "orders-search-multibasket-disabled"})
45-
class OrdersSearchMultibasketDisabledIntegrationTest {
49+
class OrdersSearchMultibasketDisabledIntegrationTest extends AbstractMongoConfig {
4650

4751
@Autowired
4852
private MockMvc mockMvc;
@@ -53,6 +57,11 @@ class OrdersSearchMultibasketDisabledIntegrationTest {
5357
@Autowired
5458
private ObjectMapper mapper;
5559

60+
@BeforeAll
61+
static void setup() {
62+
mongoDBContainer.start();
63+
}
64+
5665
@AfterEach
5766
void tearDown() {
5867
checkoutRepository.deleteAll();

src/test/java/uk/gov/companieshouse/orders/api/interceptor/OrdersApiAuthenticationTests.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import java.util.Collections;
2424
import java.util.List;
2525
import java.util.Optional;
26+
27+
import org.junit.jupiter.api.BeforeAll;
2628
import org.junit.jupiter.api.DisplayName;
2729
import org.junit.jupiter.api.Test;
2830
import org.springframework.beans.factory.annotation.Autowired;
@@ -32,7 +34,9 @@
3234
import org.springframework.kafka.test.context.EmbeddedKafka;
3335
import org.springframework.test.annotation.DirtiesContext;
3436
import org.springframework.test.web.reactive.server.WebTestClient;
37+
import org.testcontainers.junit.jupiter.Testcontainers;
3538
import uk.gov.companieshouse.api.util.security.Permission;
39+
import uk.gov.companieshouse.orders.api.config.AbstractMongoConfig;
3640
import uk.gov.companieshouse.orders.api.dto.AddDeliveryDetailsRequestDTO;
3741
import uk.gov.companieshouse.orders.api.dto.BasketRequestDTO;
3842
import uk.gov.companieshouse.orders.api.dto.DeliveryDetailsDTO;
@@ -49,10 +53,11 @@
4953
import uk.gov.companieshouse.orders.api.service.CheckoutService;
5054
import uk.gov.companieshouse.sdk.manager.ApiSdkManager;
5155

56+
@Testcontainers
5257
@DirtiesContext
5358
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
5459
@EmbeddedKafka
55-
class OrdersApiAuthenticationTests {
60+
class OrdersApiAuthenticationTests extends AbstractMongoConfig {
5661

5762
private static final String ITEM_URI = "/orderable/certificates/12345678";
5863
private static final String COMPANY_NUMBER = "00006400";
@@ -90,6 +95,11 @@ class OrdersApiAuthenticationTests {
9095
@MockBean
9196
private CheckoutData checkoutData;
9297

98+
@BeforeAll
99+
static void setup() {
100+
mongoDBContainer.start();
101+
}
102+
93103
@Test
94104
@DisplayName("Add item accepts request with signed in user")
95105
void addItemAcceptsRequestWithSignedInUser() {

src/test/java/uk/gov/companieshouse/orders/api/repository/BasketRepositoryIntegrationTest.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,31 @@
44
import static org.junit.jupiter.api.Assertions.assertFalse;
55

66
import org.junit.jupiter.api.AfterEach;
7+
import org.junit.jupiter.api.BeforeAll;
78
import org.junit.jupiter.api.DisplayName;
89
import org.junit.jupiter.api.Test;
910
import org.springframework.beans.factory.annotation.Autowired;
1011
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
12+
import org.testcontainers.junit.jupiter.Testcontainers;
13+
import uk.gov.companieshouse.orders.api.config.AbstractMongoConfig;
1114
import uk.gov.companieshouse.orders.api.model.Basket;
1215
import uk.gov.companieshouse.orders.api.util.StubHelper;
1316

17+
@Testcontainers
1418
@DataMongoTest
15-
class BasketRepositoryIntegrationTest {
19+
class BasketRepositoryIntegrationTest extends AbstractMongoConfig {
1620

1721
@Autowired
1822
private BasketRepository basketRepository;
1923

2024
@Autowired
2125
private BasketRepositoryImpl basketRepositoryImpl;
2226

27+
@BeforeAll
28+
static void setup() {
29+
mongoDBContainer.start();
30+
}
31+
2332
@AfterEach
2433
void teardown() {
2534
basketRepository.deleteAll();

src/test/java/uk/gov/companieshouse/orders/api/repository/CheckoutRepositoryIntegrationTest.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,32 @@
88
import java.util.List;
99

1010
import org.junit.jupiter.api.AfterEach;
11+
import org.junit.jupiter.api.BeforeAll;
1112
import org.junit.jupiter.api.DisplayName;
1213
import org.junit.jupiter.api.Test;
1314
import org.springframework.beans.factory.annotation.Autowired;
1415
import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest;
1516
import org.springframework.data.domain.Page;
1617
import org.springframework.data.domain.PageRequest;
1718
import org.springframework.data.domain.Sort;
19+
import org.testcontainers.junit.jupiter.Testcontainers;
20+
import uk.gov.companieshouse.orders.api.config.AbstractMongoConfig;
1821
import uk.gov.companieshouse.orders.api.model.Checkout;
1922
import uk.gov.companieshouse.orders.api.util.StubHelper;
2023

24+
25+
@Testcontainers
2126
@DataMongoTest
22-
class CheckoutRepositoryIntegrationTest {
27+
class CheckoutRepositoryIntegrationTest extends AbstractMongoConfig {
2328

2429
@Autowired
2530
private CheckoutRepository checkoutRepository;
2631

32+
@BeforeAll
33+
static void setup() {
34+
mongoDBContainer.start();
35+
}
36+
2737
@AfterEach
2838
void teardown() {
2939
checkoutRepository.deleteAll();

0 commit comments

Comments
 (0)