Skip to content

Commit d52d792

Browse files
Rajat SharmaRajat Sharma
authored andcommitted
changes
1 parent 979ab42 commit d52d792

3 files changed

Lines changed: 563 additions & 0 deletions

File tree

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package com.johnlpage.memex.cucumber.steps;
2+
3+
import com.fasterxml.jackson.core.type.TypeReference;
4+
import com.fasterxml.jackson.databind.ObjectMapper;
5+
import com.johnlpage.memex.cucumber.service.MacrosRegister;
6+
import com.johnlpage.memex.cucumber.service.VehicleInspectionIdRangeValidator;
7+
import com.johnlpage.memex.model.VehicleInspection;
8+
import io.cucumber.java.en.And;
9+
import io.cucumber.java.en.Given;
10+
import io.cucumber.java.en.Then;
11+
import io.cucumber.java.en.When;
12+
import io.restassured.http.ContentType;
13+
import io.restassured.response.Response;
14+
import org.springframework.beans.factory.annotation.Autowired;
15+
import org.springframework.beans.factory.annotation.Value;
16+
import org.springframework.core.io.ClassPathResource;
17+
18+
import java.io.IOException;
19+
import java.io.InputStream;
20+
import java.nio.charset.StandardCharsets;
21+
import java.util.List;
22+
23+
24+
import static org.junit.jupiter.api.Assertions.*;
25+
26+
import static io.restassured.RestAssured.given;
27+
28+
public class BulkInspectionApiSteps {
29+
30+
@Value("${memex.base-url}")
31+
private String baseUrl;
32+
33+
@Autowired
34+
private ObjectMapper objectMapper;
35+
36+
@Autowired
37+
private VehicleInspectionIdRangeValidator idRangeValidator;
38+
39+
@Autowired
40+
private MacrosRegister macroRegister;
41+
42+
private String jsonString;
43+
private List<VehicleInspection> inspections;
44+
45+
private Response response;
46+
private long durationMs;
47+
48+
@Given("I load vehicle inspections from file {string}")
49+
public void loadVehicleInspectionsFromFile(String fileName) throws IOException {
50+
ClassPathResource resource = new ClassPathResource(fileName);
51+
if (!resource.exists()) {
52+
throw new RuntimeException("File not found: " + fileName);
53+
}
54+
55+
try (InputStream inputStream = resource.getInputStream()) {
56+
jsonString = new String(inputStream.readAllBytes(), StandardCharsets.UTF_8);
57+
inspections = objectMapper.readValue(jsonString, new TypeReference<List<VehicleInspection>>() {
58+
});
59+
}
60+
}
61+
62+
@And("I validate all inspection test IDs")
63+
public void validateAllInspectionTestIds() {
64+
inspections.forEach(inspection -> idRangeValidator.validate(inspection.getTestid()));
65+
}
66+
67+
@When("I send inspections to the bulk API: {string}")
68+
public void sendInspectionsToBulkApi(String localUrl) {
69+
long startTime = System.nanoTime();
70+
71+
String processedUrl = macroRegister.replaceMacros(localUrl);
72+
response = given()
73+
.baseUri(baseUrl)
74+
.contentType(ContentType.JSON)
75+
.body(jsonString)
76+
.post(processedUrl);
77+
78+
long endTime = System.nanoTime();
79+
durationMs = (endTime - startTime) / 1_000_000;
80+
}
81+
82+
@Then("the response status should be 2xx")
83+
public void theResponseStatusShouldBe2xx() {
84+
assertNotNull(response, "Response should not be null");
85+
assertTrue(response.getStatusCode() >= 200 && response.getStatusCode() < 300,
86+
"Expected 2xx response, but got " + response.getStatusCode());
87+
}
88+
89+
@And("the response time should be under {int} seconds")
90+
public void responseTimeShouldBeUnderLimit(int maxAllowedSeconds) {
91+
double actualSeconds = durationMs / 1000.0;
92+
String formattedSeconds = String.format("%.1f", actualSeconds);
93+
94+
assertTrue(actualSeconds <= maxAllowedSeconds,
95+
"API call took too long: " + formattedSeconds + "s (limit: " + maxAllowedSeconds + " s)");
96+
}
97+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
@bulk_api @vehicle_inspection @performance
2+
Feature: Bulk Vehicle Inspection API
3+
This feature tests the bulk vehicle inspection API endpoint.
4+
It ensures that a batch of vehicle inspections can be submitted successfully,
5+
all required IDs are valid, and the API responds within an acceptable time frame.
6+
7+
@sunny_day @fast_response
8+
Scenario: Successfully submit vehicle inspections in bulk within time limit
9+
Given I load vehicle inspections from file "test-data/vehicle-inspections.json"
10+
And I validate all inspection test IDs
11+
When I send inspections to the bulk API: "/api/inspections?updateStrategy=INSERT&futz=true"
12+
Then the response status should be 2xx
13+
And the response time should be under 4 seconds

0 commit comments

Comments
 (0)