-
-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[web-app] Move generic cookie steps to a common parent class (#5604)
- Loading branch information
Showing
9 changed files
with
466 additions
and
380 deletions.
There are no files selected for viewing
127 changes: 127 additions & 0 deletions
127
vividus-extension-web-app/src/main/java/org/vividus/ui/web/CookieSteps.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 2019-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.vividus.ui.web; | ||
|
||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Function; | ||
|
||
import org.apache.commons.lang3.Validate; | ||
import org.hamcrest.Matcher; | ||
import org.jbehave.core.annotations.Then; | ||
import org.jbehave.core.annotations.When; | ||
import org.jbehave.core.model.ExamplesTable; | ||
import org.vividus.context.VariableContext; | ||
import org.vividus.softassert.ISoftAssert; | ||
import org.vividus.steps.StringComparisonRule; | ||
import org.vividus.ui.web.action.CookieManager; | ||
import org.vividus.util.json.JsonUtils; | ||
import org.vividus.variable.VariableScope; | ||
|
||
public class CookieSteps<T> | ||
{ | ||
private final CookieManager<T> cookieManager; | ||
private final Function<T, String> cookieNameMapper; | ||
private final Function<T, Object> cookieToJsonObjectMapper; | ||
private final VariableContext variableContext; | ||
private final JsonUtils jsonUtils; | ||
private final ISoftAssert softAssert; | ||
|
||
public CookieSteps(CookieManager<T> cookieManager, Function<T, String> cookieNameMapper, | ||
Function<T, Object> cookieToJsonObjectMapper, VariableContext variableContext, JsonUtils jsonUtils, | ||
ISoftAssert softAssert) | ||
{ | ||
this.cookieManager = cookieManager; | ||
this.cookieNameMapper = cookieNameMapper; | ||
this.cookieToJsonObjectMapper = cookieToJsonObjectMapper; | ||
this.variableContext = variableContext; | ||
this.jsonUtils = jsonUtils; | ||
this.softAssert = softAssert; | ||
} | ||
|
||
/** | ||
* Checks if cookie with name <code>cookieName</code> matching the comparison rule is set. | ||
* | ||
* @param stringComparisonRule Comparison rule: "is equal to", "contains", "does not contain", "matches". | ||
* @param cookieName The name of the cookie to check presence. | ||
* @return Cookie if it's present | ||
*/ | ||
@Then("cookie with name that $stringComparisonRule `$cookieName` is set") | ||
public Optional<T> assertCookieIsSet(StringComparisonRule stringComparisonRule, String cookieName) | ||
{ | ||
Optional<T> cookie = getCookieMatchingRule(stringComparisonRule, cookieName); | ||
softAssert.assertTrue( | ||
String.format("Cookie with the name that %s '%s' is set", stringComparisonRule, cookieName), | ||
cookie.isPresent()); | ||
return cookie; | ||
} | ||
|
||
/** | ||
* Checks if cookie with name <code>cookieName</code> matching the comparison rule is not set. | ||
* | ||
* @param stringComparisonRule Comparison rule: "is equal to", "contains", "does not contain", "matches". | ||
* @param cookieName The name of the cookie to check absence. | ||
*/ | ||
@Then("cookie with name that $stringComparisonRule `$cookieName` is not set") | ||
public void assertCookieIsNotSet(StringComparisonRule stringComparisonRule, String cookieName) | ||
{ | ||
Optional<T> cookie = getCookieMatchingRule(stringComparisonRule, cookieName); | ||
softAssert.assertTrue( | ||
String.format("Cookie with the name that %s '%s' is not set", stringComparisonRule, cookieName), | ||
cookie.isEmpty()); | ||
} | ||
|
||
/** | ||
* Finds the cookie by the name and saves all its parameters as JSON to a variable. | ||
* | ||
* @param cookieName The name of the cookie to save. | ||
* @param scopes The set (comma separated list of scopes e.g.: STORY, NEXT_BATCHES) of the variable | ||
* scopes.<br> | ||
* <i>Available scopes:</i> | ||
* <ul> | ||
* <li><b>STEP</b> - the variable will be available only within the step, | ||
* <li><b>SCENARIO</b> - the variable will be available only within the scenario, | ||
* <li><b>STORY</b> - the variable will be available within the whole story, | ||
* <li><b>NEXT_BATCHES</b> - the variable will be available starting from next batch | ||
* </ul> | ||
* @param variableName The variable name to save the cookie. | ||
*/ | ||
@When("I save cookie with name `$cookieName` as JSON to $scopes variable `$variableName`") | ||
public void saveCookieAsJson(String cookieName, Set<VariableScope> scopes, String variableName) | ||
{ | ||
Optional.ofNullable(cookieManager.getCookie(cookieName)) | ||
.map(cookieToJsonObjectMapper) | ||
.ifPresent(cam -> variableContext.putVariable(scopes, variableName, jsonUtils.toJson(cam))); | ||
} | ||
|
||
protected void setCookies(String currentPageUrl, ExamplesTable parameters) | ||
{ | ||
Validate.isTrue(null != currentPageUrl, | ||
"Current page URL is not provided. Please make sure to navigate to the correct page"); | ||
parameters.getRows().forEach(row -> | ||
cookieManager.addCookie(row.get("cookieName"), row.get("cookieValue"), row.get("path"), currentPageUrl) | ||
); | ||
} | ||
|
||
private Optional<T> getCookieMatchingRule(StringComparisonRule comparisonRule, String cookieName) | ||
{ | ||
Matcher<String> matcher = comparisonRule.createMatcher(cookieName); | ||
return cookieManager.getCookies().stream() | ||
.filter(cookie -> matcher.matches(cookieNameMapper.apply(cookie))) | ||
.findFirst(); | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
vividus-extension-web-app/src/test/java/org/vividus/ui/web/CookieStepsTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* Copyright 2019-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.vividus.ui.web; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.times; | ||
import static org.mockito.Mockito.verify; | ||
import static org.mockito.Mockito.verifyNoInteractions; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.util.List; | ||
import java.util.Set; | ||
|
||
import org.jbehave.core.model.ExamplesTable; | ||
import org.junit.jupiter.api.BeforeEach; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.extension.ExtendWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.jupiter.MockitoExtension; | ||
import org.vividus.context.VariableContext; | ||
import org.vividus.softassert.ISoftAssert; | ||
import org.vividus.steps.StringComparisonRule; | ||
import org.vividus.ui.web.action.CookieManager; | ||
import org.vividus.util.json.JsonUtils; | ||
import org.vividus.variable.VariableScope; | ||
|
||
@SuppressWarnings("PMD.UnnecessaryBooleanAssertion") | ||
@ExtendWith(MockitoExtension.class) | ||
class CookieStepsTests | ||
{ | ||
private static final String COOKIE_NAME = "SSESSf4342sds23e3t5fs"; | ||
private static final String VARIABLE_NAME = "var"; | ||
|
||
@Mock private CookieManager<Cookie> cookieManager; | ||
@Mock private VariableContext variableContext; | ||
@Mock private JsonUtils jsonUtils; | ||
@Mock private ISoftAssert softAssert; | ||
private CookieSteps<Cookie> cookieSteps; | ||
|
||
@BeforeEach | ||
void beforeEach() | ||
{ | ||
cookieSteps = new CookieSteps<>(cookieManager, Cookie::name, cookie -> cookie, variableContext, jsonUtils, | ||
softAssert); | ||
} | ||
|
||
@Test | ||
void shouldAssertThatCookieWithNameMatchingComparisonRuleIsSet() | ||
{ | ||
var cookie = new Cookie(COOKIE_NAME); | ||
when(cookieManager.getCookies()).thenReturn(List.of(cookie)); | ||
cookieSteps.assertCookieIsSet(StringComparisonRule.MATCHES, "SSESS.*"); | ||
verify(softAssert).assertTrue("Cookie with the name that matches 'SSESS.*' is set", true); | ||
} | ||
|
||
@Test | ||
void shouldAssertThatCookieWithNameMatchingComparisonRuleIsNotSet() | ||
{ | ||
var cookie = new Cookie(COOKIE_NAME); | ||
when(cookieManager.getCookies()).thenReturn(List.of(cookie)); | ||
cookieSteps.assertCookieIsNotSet(StringComparisonRule.CONTAINS, "_ga"); | ||
verify(softAssert).assertTrue("Cookie with the name that contains '_ga' is not set", true); | ||
} | ||
|
||
@Test | ||
void shouldConvertCookieToJsonAndSaveItToContext() | ||
{ | ||
Cookie cookie = mock(); | ||
when(cookieManager.getCookie(COOKIE_NAME)).thenReturn(cookie); | ||
var cookieAsJson = "{\"path\": \"/index\"}"; | ||
when(jsonUtils.toJson(cookie)).thenReturn(cookieAsJson); | ||
Set<VariableScope> scopes = Set.of(VariableScope.STEP); | ||
|
||
cookieSteps.saveCookieAsJson(COOKIE_NAME, scopes, VARIABLE_NAME); | ||
verify(variableContext).putVariable(scopes, VARIABLE_NAME, cookieAsJson); | ||
} | ||
|
||
@Test | ||
void shouldNotConvertNorSaveIfNoCookieReturnedByCookieHelper() | ||
{ | ||
cookieSteps.saveCookieAsJson(COOKIE_NAME, Set.of(VariableScope.STEP), VARIABLE_NAME); | ||
verifyNoInteractions(jsonUtils, variableContext); | ||
} | ||
|
||
@Test | ||
void shouldThrowAnExceptionIfCurrentUrlIsNotDefined() | ||
{ | ||
ExamplesTable table = mock(); | ||
var iae = assertThrows(IllegalArgumentException.class, () -> cookieSteps.setCookies(null, table)); | ||
assertEquals("Current page URL is not provided. Please make sure to navigate to the correct page", | ||
iae.getMessage()); | ||
} | ||
|
||
@Test | ||
void shouldSetAllCookies() | ||
{ | ||
var testUrl = "https://www.vividus.org"; | ||
var tableAsString = """ | ||
|cookieName|cookieValue|path| | ||
|hcpsid |1 |/ | | ||
|hcpsid |1 |/ | | ||
"""; | ||
var table = new ExamplesTable(tableAsString); | ||
cookieSteps.setCookies(testUrl, table); | ||
verify(cookieManager, times(2)).addCookie("hcpsid", "1", "/", testUrl); | ||
} | ||
|
||
record Cookie(String name) | ||
{ | ||
} | ||
} |
Oops, something went wrong.