Skip to content

Commit 650ffc5

Browse files
Merge pull request #2 from ifeelgood/revert-1-cucumber_kafka_tests
Revert "Cucumber kafka integration tests"
2 parents ff7e5d2 + 872ddb6 commit 650ffc5

9 files changed

Lines changed: 40 additions & 199 deletions

File tree

memex/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,6 @@
131131
<artifactId>junit-jupiter</artifactId>
132132
<scope>test</scope>
133133
</dependency>
134-
<dependency>
135-
<groupId>org.springframework.kafka</groupId>
136-
<artifactId>spring-kafka-test</artifactId>
137-
<scope>test</scope>
138-
</dependency>
139134
<dependency>
140135
<groupId>org.testcontainers</groupId>
141136
<artifactId>mongodb</artifactId>

memex/src/main/java/com/johnlpage/memex/kafka/VehicleInspectionKafkaConsumer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import com.johnlpage.memex.service.generic.PostWriteTriggerService;
1010
import com.johnlpage.memex.service.generic.PreWriteTriggerService;
1111
import com.mongodb.bulk.BulkWriteResult;
12+
import jakarta.annotation.PreDestroy;
1213
import java.util.ArrayList;
1314
import java.util.List;
1415
import java.util.concurrent.CompletableFuture;
@@ -17,8 +18,6 @@
1718
import lombok.RequiredArgsConstructor;
1819
import org.slf4j.LoggerFactory;
1920
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
20-
import org.springframework.context.event.ContextClosedEvent;
21-
import org.springframework.context.event.EventListener;
2221
import org.springframework.kafka.annotation.KafkaListener;
2322
import org.springframework.scheduling.annotation.Scheduled;
2423
import org.springframework.stereotype.Component;
@@ -101,8 +100,8 @@ public void checkForIdle() {
101100
}
102101
}
103102

104-
@EventListener
105-
public void onContextClosed(ContextClosedEvent event) {
103+
@PreDestroy
104+
public void onShutdown() {
106105
sendBatch();
107106
System.out.println("Kafka Listener is shutting down.");
108107
CompletableFuture<Void> allFutures =

memex/src/test/java/com/johnlpage/memex/cucumber/CucumberTestsContainersConfig.java

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,20 @@
33
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
44
import org.springframework.boot.test.context.TestConfiguration;
55
import org.springframework.context.annotation.Bean;
6-
import org.springframework.context.annotation.Condition;
7-
import org.springframework.context.annotation.ConditionContext;
8-
import org.springframework.context.annotation.Conditional;
9-
import org.springframework.core.type.AnnotatedTypeMetadata;
106
import org.springframework.test.context.DynamicPropertyRegistrar;
117
import org.testcontainers.mongodb.MongoDBAtlasLocalContainer;
128

13-
class MongoUriMissingCondition implements Condition {
14-
@Override
15-
public boolean matches(ConditionContext context, AnnotatedTypeMetadata metadata) {
16-
String uri = context.getEnvironment().getProperty("spring.data.mongodb.uri");
17-
return uri == null || uri.trim().isEmpty();
18-
}
19-
}
20-
219
@TestConfiguration
2210
public class CucumberTestsContainersConfig {
2311

2412
@Bean
25-
@Conditional(MongoUriMissingCondition.class)
13+
@ConditionalOnProperty(name = "memex.testcontainers.mongodb.enabled", havingValue = "true", matchIfMissing = true)
2614
public MongoDBAtlasLocalContainer mongoDbContainer() {
2715
return new MongoDBAtlasLocalContainer("mongodb/mongodb-atlas-local:8");
2816
}
2917

3018
@Bean
31-
@Conditional(MongoUriMissingCondition.class)
19+
@ConditionalOnProperty(name = "memex.testcontainers.mongodb.enabled", havingValue = "true", matchIfMissing = true)
3220
public DynamicPropertyRegistrar mongoDbProperties(MongoDBAtlasLocalContainer mongoDBContainer) {
3321
mongoDBContainer.start();
3422
return (registry) -> {

memex/src/test/java/com/johnlpage/memex/cucumber/steps/CucumberSpringContextConfig.java

Lines changed: 0 additions & 42 deletions
This file was deleted.

memex/src/test/java/com/johnlpage/memex/cucumber/steps/InspectionSteps.java

Lines changed: 11 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,41 +6,34 @@
66
import static io.restassured.RestAssured.given;
77
import static org.junit.jupiter.api.Assertions.*;
88

9-
import com.fasterxml.jackson.core.JsonProcessingException;
10-
import com.fasterxml.jackson.databind.ObjectMapper;
11-
import com.johnlpage.memex.model.VehicleInspection;
9+
import com.johnlpage.memex.cucumber.CucumberTestsContainersConfig;
1210
import io.cucumber.datatable.DataTable;
1311
import io.cucumber.java.ParameterType;
1412
import io.cucumber.java.en.Given;
1513
import io.cucumber.java.en.When;
1614
import io.cucumber.java.en.Then;
15+
import io.cucumber.spring.CucumberContextConfiguration;
1716
import io.restassured.http.ContentType;
1817
import io.restassured.path.json.JsonPath;
1918
import io.restassured.response.Response;
2019
import java.time.ZonedDateTime;
2120
import java.time.format.DateTimeFormatter;
22-
import java.util.ArrayList;
2321
import java.util.List;
2422
import java.util.Map;
2523
import java.util.concurrent.TimeUnit;
2624

27-
import org.springframework.beans.factory.annotation.Autowired;
25+
import org.springframework.boot.test.context.SpringBootTest;
2826
import org.springframework.boot.test.web.server.LocalServerPort;
29-
import org.springframework.data.mongodb.core.MongoTemplate;
30-
import org.springframework.data.mongodb.core.query.Criteria;
31-
import org.springframework.data.mongodb.core.query.Query;
27+
import org.springframework.test.context.ActiveProfiles;
3228

29+
@CucumberContextConfiguration
30+
@SpringBootTest(classes = CucumberTestsContainersConfig.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
31+
@ActiveProfiles("test")
3332
public class InspectionSteps {
3433

3534
@LocalServerPort
3635
private int port;
3736

38-
@Autowired
39-
private MongoTemplate mongoTemplate;
40-
41-
@Autowired
42-
private ObjectMapper objectMapper;
43-
4437
private Response response;
4538
private ZonedDateTime capturedTimestamp;
4639

@@ -54,47 +47,18 @@ public Boolean bool(String bool){
5447
}
5548

5649
@Given("the following vehicle inspections exist:")
57-
public void givenVehicleInspectionsExist(DataTable dataTable) throws JsonProcessingException {
58-
List<Map<String, String>> rows = dataTable.asMaps(String.class, String.class);
59-
List<VehicleInspection> inspections = new ArrayList<>();
60-
61-
for (Map<String, String> row : rows) {
62-
String json = row.get("json");
63-
VehicleInspection inspection = objectMapper.readValue(json, VehicleInspection.class);
64-
inspections.add(inspection);
65-
}
66-
67-
mongoTemplate.insertAll(inspections);
50+
public void givenVehicleInspectionsExist(DataTable dataTable) {
51+
// test assumes it exists however it would be better to create it here
6852
}
6953

7054
@Given("the following vehicle inspections exist and have historical data as of {string}:")
7155
public void givenVehicleInspectionsExist(String date, DataTable dataTable) {
72-
// TODO
7356
// test assumes it exists however it would be better to create it here
7457
}
7558

7659
@Given("the following vehicle inspections do not exist:")
77-
public void givenTheFollowingVehicleInspectionsDoNotExist(DataTable dataTable) {
78-
for (Map<String, String> row : dataTable.asMaps()) {
79-
80-
if (row.size() != 1) {
81-
throw new IllegalArgumentException("Only one column per row is supported in this step.");
82-
}
83-
84-
String key = row.keySet().iterator().next();
85-
String value = row.get(key);
86-
87-
Query query = switch (key) {
88-
case "testid" -> {
89-
Long testid = Long.parseLong(value);
90-
yield Query.query(Criteria.where("testid").is(testid));
91-
}
92-
case "model" -> Query.query(Criteria.where("vehicle.model").is(value));
93-
default -> throw new UnsupportedOperationException("Unsupported deletion key: " + key);
94-
};
95-
96-
mongoTemplate.remove(query, VehicleInspection.class);
97-
}
60+
public void givenVehicleInspectionsDoNotExist(DataTable dataTable) {
61+
// test assumes it does not exist however it would be better to delete it here
9862
}
9963

10064
@Given("I capture the current timestamp")

memex/src/test/java/com/johnlpage/memex/cucumber/steps/VehicleInspectionKafkaConsumerSteps.java

Lines changed: 0 additions & 49 deletions
This file was deleted.

memex/src/test/resources/application-test.properties

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
memex.testcontainers.mongodb.enabled=true
22

3-
#Keep this empty if you want tests to use Mongo test container
4-
spring.data.mongodb.uri=
3+
spring.data.mongodb.uri=mongodb://localhost:63650
54
spring.data.mongodb.database=memex
6-
7-
data.test.id.range.start=1000
8-
data.test.id.range.end=100000
9-
105
server.shutdown=graceful
116
spring.mvc.async.request-timeout=0
127
spring.lifecycle.timeout-per-shutdown-phase=30s

memex/src/test/resources/features/inspections.feature

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ Feature: Vehicle Inspection API Management
6363
@get @by_id @sunny_day
6464
Scenario: Successfully retrieve a vehicle inspection by ID
6565
Given the following vehicle inspections exist:
66-
| json |
67-
| {"testid": 1001, "vehicle": {"model": "Corolla"}} |
66+
| testid | vehicle.model |
67+
| 1001 | Corolla |
6868
When I send a GET request to "/api/inspections/id/1001"
6969
Then the response status code should be 200
7070
And the response should contain "testid": 1001
@@ -136,10 +136,7 @@ Feature: Vehicle Inspection API Management
136136
| REPLACE | true | 2007 | 2008 |
137137

138138
@post @load_stream @sunny_day
139-
Scenario: Successfully delete a vehicle inspection
140-
Given the following vehicle inspections exist:
141-
| json |
142-
| {"testid": 2007} |
139+
Scenario: Successfully load a stream of vehicle inspections with different update strategies and futz options
143140
When I send a POST request to "/api/inspections?updateStrategy=UPDATEWITHHISTORY&futz=true" with the payload:
144141
"""
145142
[
@@ -176,11 +173,11 @@ Feature: Vehicle Inspection API Management
176173
@get @by_model @sunny_day
177174
Scenario Outline: Successfully retrieve vehicle inspections by model with pagination
178175
Given the following vehicle inspections exist:
179-
| json |
180-
| {"testid": 2002, "vehicle": {"model": "Focus"}} |
181-
| {"testid": 2004, "vehicle": {"model": "Focus"}} |
182-
| {"testid": 2006, "vehicle": {"model": "Focus"}} |
183-
| {"testid": 2008, "vehicle": {"model": "Focus"}} |
176+
| testid | vehicle.model |
177+
| 2002 | Focus |
178+
| 2004 | Focus |
179+
| 2006 | Focus |
180+
| 2008 | Focus |
184181
When I send a GET request to "/api/inspections/model/<model>?page=<page>&size=<size>"
185182
Then the response status code should be 200
186183
And the response should contain "content" with <expected_count> items
@@ -207,9 +204,9 @@ Feature: Vehicle Inspection API Management
207204
@post @mongo_query @sunny_day
208205
Scenario: Successfully execute a native MongoDB query with sorting and filter on non-indexed field
209206
Given the following vehicle inspections exist:
210-
| json |
211-
| {"testid": 1001, "vehicle": {"make": "Toyota"}} |
212-
| {"testid": 2002, "vehicle": {"make": "Ford"}} |
207+
| testid | vehicle.make |
208+
| 1001 | Toyota |
209+
| 2002 | Ford |
213210
When I send a POST request to "/api/inspections/query" with the payload:
214211
"""
215212
{
@@ -228,9 +225,9 @@ Feature: Vehicle Inspection API Management
228225
@post @mongo_query @sunny_day
229226
Scenario: Successfully execute a native MongoDB query with sorting and filter on indexed field
230227
Given the following vehicle inspections exist:
231-
| json |
232-
| {"testid": 1001, "vehicle": {"model": "Corolla"}} |
233-
| {"testid": 2002, "vehicle": {"model": "Focus"}} |
228+
| testid | vehicle.model |
229+
| 1001 | Corolla |
230+
| 2002 | Focus |
234231
When I send a POST request to "/api/inspections/query" with the payload:
235232
"""
236233
{
@@ -261,9 +258,9 @@ Feature: Vehicle Inspection API Management
261258
@get @stream_json @sunny_day
262259
Scenario: Successfully stream all vehicle inspections as JSON
263260
Given the following vehicle inspections exist:
264-
| json |
265-
| {"testid": 1001, "vehicle": {"make": "Toyota"}} |
266-
| {"testid": 2002, "vehicle": {"make": "Ford"}} |
261+
| testid | vehicle.make |
262+
| 1001 | Toyota |
263+
| 2002 | Ford |
267264
When I send a GET request to "/api/inspections/json"
268265
Then the response status code should be 200
269266
And the "Content-Type" header should be "application/json"
@@ -273,9 +270,9 @@ Feature: Vehicle Inspection API Management
273270
@get @stream_json_native @sunny_day
274271
Scenario: Successfully stream all vehicle inspections as native JSON
275272
Given the following vehicle inspections exist:
276-
| json |
277-
| {"testid": 1001, "vehicle": {"make": "Toyota"}} |
278-
| {"testid": 2002, "vehicle": {"make": "Ford"}} |
273+
| testid | vehicle.make |
274+
| 1001 | Toyota |
275+
| 2002 | Ford |
279276
When I send a GET request to "/api/inspections/jsonnative"
280277
Then the response status code should be 200
281278
And the "Content-Type" header should be "application/json"
@@ -284,9 +281,9 @@ Feature: Vehicle Inspection API Management
284281

285282
@get @as_of @sunny_day
286283
Scenario: Successfully retrieve vehicle inspection history as of a specific date
287-
Given the following vehicle inspections exist:
288-
| json |
289-
| {"testid": 2006, "vehicle": {"make": "Ford", "model": "Focus"}} |
284+
Given the following vehicle inspections exist and have historical data as of "2023-10-25 12:00:00":
285+
| testid | vehicle.make |
286+
| 2006 | Ford |
290287
And I wait for 1 second
291288
And I capture the current timestamp
292289
And I wait for 1 second

memex/src/test/resources/features/vehicleInspectionkafkaConsumer.feature

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)