diff --git a/.gitignore b/.gitignore index d1c9a67..e6b9fd0 100644 --- a/.gitignore +++ b/.gitignore @@ -30,6 +30,8 @@ *.tar.gz *.rar +!memex/src/test/resources/test-data/*.zip + # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* replay_pid* diff --git a/memex/src/test/java/com/johnlpage/memex/cucumber/steps/RestApiSteps.java b/memex/src/test/java/com/johnlpage/memex/cucumber/steps/RestApiSteps.java index 1a610fc..737d26a 100644 --- a/memex/src/test/java/com/johnlpage/memex/cucumber/steps/RestApiSteps.java +++ b/memex/src/test/java/com/johnlpage/memex/cucumber/steps/RestApiSteps.java @@ -8,17 +8,23 @@ import com.johnlpage.memex.cucumber.service.MacrosRegister; import io.cucumber.java.ParameterType; +import io.cucumber.java.en.And; import io.cucumber.java.en.When; import io.cucumber.java.en.Then; import io.restassured.http.ContentType; import io.restassured.path.json.JsonPath; import io.restassured.response.Response; +import java.io.IOException; +import java.io.InputStream; import java.util.List; import java.util.Map; +import java.util.zip.ZipEntry; +import java.util.zip.ZipInputStream; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; +import org.springframework.core.io.ClassPathResource; public class RestApiSteps { @@ -30,6 +36,8 @@ public class RestApiSteps { private Response response; + private long durationMs; + @ParameterType("true|false") public Boolean bool(String bool) { return Boolean.parseBoolean(bool); @@ -160,4 +168,47 @@ public void theResponseShouldBeAStreamOfValidJsonObjectsEachOnANewLine() { } } } + + @When("I send a POST request to {string} with the payload from {string}") + public void sendPostRequestWithPayloadFromZip(String endpoint, String zipFilePath) throws IOException { + ClassPathResource zipResource = new ClassPathResource(zipFilePath); + if (!zipResource.exists()) { + throw new AssertionError("ZIP file not found: " + zipFilePath); + } + + byte[] payload = extractJsonFromZip(zipResource); + + long startTime = System.nanoTime(); + + response = given() + .baseUri(baseUrl) + .contentType(ContentType.JSON) + .body(payload) + .post(endpoint); + + long endTime = System.nanoTime(); + durationMs = (endTime - startTime) / 1_000_000; + } + + + @And("the response time should be under {int} milliseconds") + public void responseTimeShouldBeUnderLimit(int maxAllowedMs) { + + assertTrue(durationMs <= maxAllowedMs, + "API call took too long: " + durationMs + "ms (limit: " + maxAllowedMs + "ms)"); + } + + private byte[] extractJsonFromZip(ClassPathResource zipResource) throws IOException { + try (InputStream zipInputStream = zipResource.getInputStream(); + ZipInputStream zis = new ZipInputStream(zipInputStream)) { + + ZipEntry entry; + while ((entry = zis.getNextEntry()) != null) { + if (entry.getName().endsWith(".json")) { + return zis.readAllBytes(); + } + } + } + throw new AssertionError("No .json file found in zip: " + zipResource.getPath()); + } } diff --git a/memex/src/test/resources/features/inspections.rest.bulk_api_response_time.feature b/memex/src/test/resources/features/inspections.rest.bulk_api_response_time.feature new file mode 100644 index 0000000..1936445 --- /dev/null +++ b/memex/src/test/resources/features/inspections.rest.bulk_api_response_time.feature @@ -0,0 +1,11 @@ +@bulk_api @vehicle_inspection @performance +Feature: Bulk Vehicle Inspection API + This feature tests the bulk vehicle inspection API endpoint. + It ensures that a batch of vehicle inspections can be submitted successfully, + all required IDs are valid, and the API responds within an acceptable time frame. + + @sunny_day @fast_response + Scenario: Successfully submit vehicle inspections in bulk within time limit + When I send a POST request to "/api/inspections?updateStrategy=INSERT&futz=true" with the payload from "test-data/vehicle-inspections.zip" + Then the response status code should be 200 + And the response time should be under 5000 milliseconds diff --git a/memex/src/test/resources/test-data/vehicle-inspections.zip b/memex/src/test/resources/test-data/vehicle-inspections.zip new file mode 100644 index 0000000..7d98371 Binary files /dev/null and b/memex/src/test/resources/test-data/vehicle-inspections.zip differ