88
99import com .johnlpage .memex .cucumber .service .MacrosRegister ;
1010import io .cucumber .java .ParameterType ;
11+ import io .cucumber .java .en .And ;
1112import io .cucumber .java .en .When ;
1213import io .cucumber .java .en .Then ;
1314import io .restassured .http .ContentType ;
1415import io .restassured .path .json .JsonPath ;
1516import io .restassured .response .Response ;
1617
18+ import java .io .IOException ;
19+ import java .io .InputStream ;
1720import java .util .List ;
1821import java .util .Map ;
22+ import java .util .zip .ZipEntry ;
23+ import java .util .zip .ZipInputStream ;
1924
2025import org .springframework .beans .factory .annotation .Autowired ;
2126import org .springframework .beans .factory .annotation .Value ;
27+ import org .springframework .core .io .ClassPathResource ;
2228
2329public class RestApiSteps {
2430
@@ -30,6 +36,8 @@ public class RestApiSteps {
3036
3137 private Response response ;
3238
39+ private long durationMs ;
40+
3341 @ ParameterType ("true|false" )
3442 public Boolean bool (String bool ) {
3543 return Boolean .parseBoolean (bool );
@@ -160,4 +168,47 @@ public void theResponseShouldBeAStreamOfValidJsonObjectsEachOnANewLine() {
160168 }
161169 }
162170 }
171+
172+ @ When ("I send a POST request to {string} with the payload from {string}" )
173+ public void sendPostRequestWithPayloadFromZip (String endpoint , String zipFilePath ) throws IOException {
174+ ClassPathResource zipResource = new ClassPathResource (zipFilePath );
175+ if (!zipResource .exists ()) {
176+ throw new AssertionError ("ZIP file not found: " + zipFilePath );
177+ }
178+
179+ byte [] payload = extractJsonFromZip (zipResource );
180+
181+ long startTime = System .nanoTime ();
182+
183+ response = given ()
184+ .baseUri (baseUrl )
185+ .contentType (ContentType .JSON )
186+ .body (payload )
187+ .post (endpoint );
188+
189+ long endTime = System .nanoTime ();
190+ durationMs = (endTime - startTime ) / 1_000_000 ;
191+ }
192+
193+
194+ @ And ("the response time should be under {int} milliseconds" )
195+ public void responseTimeShouldBeUnderLimit (int maxAllowedMs ) {
196+
197+ assertTrue (durationMs <= maxAllowedMs ,
198+ "API call took too long: " + durationMs + "ms (limit: " + maxAllowedMs + "ms)" );
199+ }
200+
201+ private byte [] extractJsonFromZip (ClassPathResource zipResource ) throws IOException {
202+ try (InputStream zipInputStream = zipResource .getInputStream ();
203+ ZipInputStream zis = new ZipInputStream (zipInputStream )) {
204+
205+ ZipEntry entry ;
206+ while ((entry = zis .getNextEntry ()) != null ) {
207+ if (entry .getName ().endsWith (".json" )) {
208+ return zis .readAllBytes ();
209+ }
210+ }
211+ }
212+ throw new AssertionError ("No .json file found in zip: " + zipResource .getPath ());
213+ }
163214}
0 commit comments