diff --git a/src/main/java/com/dougnoel/sentinel/steps/APISteps.java b/src/main/java/com/dougnoel/sentinel/steps/APISteps.java index 888040df..74c0e958 100644 --- a/src/main/java/com/dougnoel/sentinel/steps/APISteps.java +++ b/src/main/java/com/dougnoel/sentinel/steps/APISteps.java @@ -10,6 +10,9 @@ import java.security.SecureRandom; import java.time.Duration; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; import org.apache.commons.lang3.RandomStringUtils; import org.apache.commons.lang3.StringUtils; import org.apache.logging.log4j.LogManager; @@ -228,4 +231,23 @@ public void iInitializeTheData(String values) { Configuration.update(item.split(":")[0], item.split(":")[1]); } } + + /** + * Saves the value from response to Configuration for later use, for nested values use / or . delimiter + * @param responseKey String response attribute + * @param key String name under which will be the value saved + * Example: + * I save the id value from response as dogId + * I save the dog/id value from response as dogId + * I save the dog.0.id value from response as dogId + */ + @When("^I save the (.*?) value from response as (.*?)$") + public static void iSaveTheResponseValue(String responseKey, String key) throws JsonProcessingException { + var response = APIManager.getResponse().getResponse(); + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode json = objectMapper.readTree(response); + responseKey = "/" + responseKey.replace(".","/"); + var value = json.at(responseKey).toString().replaceAll("\"",""); + Configuration.update(key, value); + } } diff --git a/src/test/java/features/89 API Testing.feature b/src/test/java/features/89 API Testing.feature index 6de86ebc..edcb75e1 100644 --- a/src/test/java/features/89 API Testing.feature +++ b/src/test/java/features/89 API Testing.feature @@ -139,4 +139,24 @@ Feature: 89 API Testing id: 10 """ And I send a POST request to the pet/{id}/uploadImage endpoint - Then I verify the response code equals 415 \ No newline at end of file + Then I verify the response code equals 415 + + @89K + Scenario: 89K Save response parameter value to configuration + Given I use the API named Pet Store API + When I set the request body to + """ + { + "id": 10, + "petId": 198772, + "quantity": 7, + "shipDate": "2023-04-28T07:23:34.840Z", + "status": "approved", + "complete": true + } + """ + And I send a POST request to the store/order endpoint + Then I verify the response code equals 200 + And I save the id value from response as orderId + When I send a GET request to the store/order/{orderId} endpoint + Then I verify the response code equals 200 \ No newline at end of file