Skip to content

Save response parameter value to configuration #544

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/main/java/com/dougnoel/sentinel/steps/APISteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
22 changes: 21 additions & 1 deletion src/test/java/features/89 API Testing.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
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