Skip to content
Draft
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
88 changes: 58 additions & 30 deletions src/main/java/com/dougnoel/sentinel/steps/APISteps.java
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ public static void addParameter(String parameter, String value) {
* <li>I send a PUT request to the amdins endpoint</li>
* </ul>
* <p>
*
* @param apiCallType
* @param endpoint
*
* @param apiCallType String the type of call to make
* @param endpoint String the endpoint name as referenced in the swagger file
*/
@When("^I send a (DELETE|GET|POST|PUT) request to the (.*?) endpoint$")
public static void sendRequest(String apiCallType, String endpoint) {
Expand Down Expand Up @@ -150,37 +150,65 @@ public static void verifyResponseTime(double time) {

/**
* Validates text in an API response.
*
* <p>
* <b>Gherkin Examples:</b>
* <ul>
* <li>I validate the response <b>contains</b> the text <b>"key":"value"</b></li>
* <li>I validate the response <b>does not contain</b> the text <b>"value":"key"</b></li>
* <li>I validate the response <b>has</b> the text <b>123456789ABCDEFG</b></li>
* </ul>
* <p>
* @param assertion String null to see if the text exists, "does not" to see if it is absent
* @param matchType String use "contains" for a partial match otherwise it will be an exact match
* @param text String the text to match
*/
@Then("^I validate the response( does not)? (has|have|contains?) the text \"([^\"]*)\"$")
public static void verifyResponseContains(String assertion, String matchType, String text) {
boolean negate = !StringUtils.isEmpty(assertion);
boolean partialMatch = matchType.contains("contain");
@Then("^I validate the response( does not)? (has|have|contains?) the text (.*?)$")
Comment thread
tyBouch marked this conversation as resolved.
public static void verifyResponseContains(String assertion, String matchType, String text) {
boolean negate = !StringUtils.isEmpty(assertion);
boolean partialMatch = matchType.contains("contain");

int responseCode = APIManager.getResponse().getResponseCode();
String responseText = APIManager.getResponse().getResponse();
String expectedResult = SentinelStringUtils.format(
"Expected the response to {}{} the text {}. The response had a response code of {} and contained the text: {}",
(negate ? "not " : ""), (partialMatch ? "contain" : "exactly match"), text, responseCode, responseText
.replace("\n", " "));
log.trace(expectedResult);
if (partialMatch) {
if (negate) {
assertFalse(expectedResult, responseText.contains(text));
} else {
assertTrue(expectedResult, responseText.contains(text));
}
} else {
if (negate) {
assertFalse(expectedResult, StringUtils.equals(responseText, text));
} else {
assertTrue(expectedResult, StringUtils.equals(responseText, text));
}
}
}
int responseCode = APIManager.getResponse().getResponseCode();
String responseText = APIManager.getResponse().getResponse();
String expectedResult = SentinelStringUtils.format(
"Expected the response to {}{} the text {}. The response had a response code of {} and contained the text: {}",
(negate ? "not " : ""), (partialMatch ? "contain" : "exactly match"), text, responseCode, responseText
.replace("\n", " "));
log.trace(expectedResult);

boolean negateResult;
boolean result;
boolean textStartEndQuotes = false;
String startEndQuoteStripText = text;
if(text.matches("^\".*?\"$")) {
textStartEndQuotes = true;
startEndQuoteStripText = text.substring(1, text.length() - 1);
}

if (partialMatch) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To reduce code complexity you'll need to break this out into a different function, and at that point it should also be applied to TextVerificationSteps.java to have them follow this new pattern.
Sorry to increase your scope!

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I'm going to have to change the scope for this.

if(textStartEndQuotes) {
result = responseText.contains(startEndQuoteStripText) || responseText.contains(text);
negateResult = responseText.contains(startEndQuoteStripText) && responseText.contains(text);
}
else {
result = responseText.contains(text);
negateResult = result;
}
} else {
if(textStartEndQuotes) {
result = StringUtils.equals(responseText, text) || StringUtils.equals(responseText, startEndQuoteStripText);
negateResult = StringUtils.equals(responseText, text) && StringUtils.equals(responseText, startEndQuoteStripText);
}
else {
result = responseText.contains(text);
negateResult = result;
}
}

if (negate)
assertFalse(expectedResult, negateResult);
else
assertTrue(expectedResult, result);
Comment on lines +177 to +210
Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We had a miscommunictaion. This is what I was attempting to describe:

Suggested change
boolean negateResult;
boolean result;
boolean textStartEndQuotes = false;
String startEndQuoteStripText = text;
if(text.matches("^\".*?\"$")) {
textStartEndQuotes = true;
startEndQuoteStripText = text.substring(1, text.length() - 1);
}
if (partialMatch) {
if(textStartEndQuotes) {
result = responseText.contains(startEndQuoteStripText) || responseText.contains(text);
negateResult = responseText.contains(startEndQuoteStripText) && responseText.contains(text);
}
else {
result = responseText.contains(text);
negateResult = result;
}
} else {
if(textStartEndQuotes) {
result = StringUtils.equals(responseText, text) || StringUtils.equals(responseText, startEndQuoteStripText);
negateResult = StringUtils.equals(responseText, text) && StringUtils.equals(responseText, startEndQuoteStripText);
}
else {
result = responseText.contains(text);
negateResult = result;
}
}
if (negate)
assertFalse(expectedResult, negateResult);
else
assertTrue(expectedResult, result);
if (partialMatch) {
if (negate) {
assertTrue(expectedResult, SentinelStringUtils.doesNotContain(responseText, text));
} else {
assertTrue(expectedResult, SentinelStringUtils.contains(responseText, text));
}
} else {
if (negate) {
assertTrue(expectedResult, SentinelStringUtils.doesNotequal(responseText, text));
} else {
assertTrue(expectedResult, SentinelStringUtils.equals(responseText, text));
}
}

Those four methods need to be created in SentinelStringUtils, based on our conversations. Then we can reuse those methods in our other text verification areas.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same logic at least, but yes Turch and I came to this conclusion. Though where to put it we weren't entirely sure so this helps a lot.

}

/**
* Adds header into API request
Expand Down Expand Up @@ -213,4 +241,4 @@ public void iInitializeTheData(String values) {
Configuration.update(item.split(":")[0], item.split(":")[1]);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,33 @@ public static String format(final String messagePattern, Object... arguments) {
public static String stripSurroundingQuotes(String text) {
if (text == null)
return null;
Pattern pattern = Pattern.compile(SURROUNDING_QUOTES);

if (pattern.matcher(text).find())
if (checkForSurroundingQuotes(text))
return text.substring(1, text.length()-1);
return text;
}

/**
* <p>Checks if a string is encompassed by quotes.
* Returns true only if the string has matching quotes encompassing it; false otherwise.</p>
* <p>
* Examples:
* <ul>
* <li><i>"This is a statement"</i> - <b>True</b></li>
* <li><i>This is a statement"</i> - <b>False</b></li>
* <li><i>"This is a statement</i> - <b>False</b></li>
* <li><i>This is a statement</i> - <b>False</b></li>
* </ul>
* </p>
* @param text String the text to check for encompassing quotes
* @return boolean true if the passed text was encompassed by matching quotes. Otherwise, returns false.
*/
private static boolean checkForSurroundingQuotes(String text) {
Pattern pattern = Pattern.compile(SURROUNDING_QUOTES);

return pattern.matcher(text).find();
}

/**
* Constructs the ordinal string of the given int. For example "1st", "2nd", "111th"
* @param i int the integer to create an ordinal string for
Expand Down Expand Up @@ -164,4 +184,26 @@ public static String replaceStoredVariables(String text) {
}
return text;
}
}

public static boolean textContains(String textToCheck, String textToCheckFor) {
if(textToCheck.contains(textToCheckFor))
return true;
else
return textToCheck.contains(stripSurroundingQuotes(textToCheckFor));
}

public static boolean textEquals(String textToCheck, String textToCheckFor) {
if(textToCheck.equals(textToCheckFor))
return true;
else
return textToCheck.equals(stripSurroundingQuotes(textToCheckFor));
}

public static boolean textDoesNotContain(String textToCheck, String textToCheckFor) {
return !textToCheck.contains(textToCheckFor) && !textToCheck.contains(stripSurroundingQuotes(textToCheckFor));
}

public static boolean textDoesNotEqual(String textToCheck, String textToCheckFor) {
return !textToCheck.equals(textToCheckFor) && !textToCheck.equals(stripSurroundingQuotes(textToCheckFor));
}
}
31 changes: 23 additions & 8 deletions src/test/java/features/89 API Testing.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ Feature: 89 API Testing
And I send a POST request to the pet endpoint
Then I verify the response code equals 200
And I verify the response was received in less than 2 seconds
And I validate the response contains the text "doggie"
And I validate the response contains the text doggie

@89B
Scenario: 89B GET Swagger Test
Given I use the API named Pet Store API
When I GET record {test_id} from the pet endpoint
Then I verify the response code equals 200
And I verify the response was received in less than 0.8 seconds
And I validate the response contains the text "photoUrls"
And I validate the response contains the text photoUrls

@89C
Scenario: 89C PUT Swagger Test
Expand All @@ -43,19 +43,19 @@ Feature: 89 API Testing
}
],
"status": "available"
}
}
"""
And I send a PUT request to the pet endpoint
Then I verify the response code equals 200
And I validate the response contains the text "puppy"
And I validate the response contains the text puppy

@89D
Scenario: 89D Parameter Swagger Test
Given I use the API named Pet Store API
And I add a status parameter with the value available
When I send a GET request to the pet/findByStatus endpoint
Then I verify the response code equals 200
And I validate the response contains the text "photoUrls"
And I validate the response contains the text photoUrls

@89E
Scenario: 89E DELETE Swagger Test
Expand Down Expand Up @@ -101,11 +101,11 @@ Feature: 89 API Testing
}
],
"status": "available"
}
}
"""
And I send a POST request to the pet endpoint
Then I verify the response code equals 200
And I validate the response contains the text "puppy"
And I validate the response contains the text puppy

@89H
Scenario: 89H URL With Parameter Test
Expand All @@ -127,4 +127,19 @@ Feature: 89 API Testing
And I add a status parameter with the value {dog_status}
When I send a GET request to the pet/findByStatus endpoint
Then I verify the response code equals 200
And I validate the response contains the text "sold"
And I validate the response contains the text sold

@89J
Scenario: 89J Verify validate the response comparison
Given I use the API named Pet Store API
When I DELETE record 10 from the pet endpoint
Then I verify the response code equals 200
And I validate the response does not have the text ""Pet deleted""
And I validate the response does not have the text "Pet deleted
And I validate the response does not have the text Pet deleted"
And I validate the response does not contain the text "Pet deleted
And I validate the response does not contain the text Pet deleted"
And I validate the response has the text Pet deleted
And I validate the response has the text "Pet deleted"
And I validate the response contains the text Pet deleted
And I validate the response contains the text "Pet deleted"