Skip to content

Commit

Permalink
Bug/43 cast json value to double (#44)
Browse files Browse the repository at this point in the history
* #43 add cast to double in JsonSettingsFile
rename ElementActionRetrier to ActionRetrier

* #43 update version of lib

* #43 add deprecated ElementActionRetrier

* #43 revert ElementActionRetrier
split with ActionRetrier

* #43 add tests for ActionRetrier
fix comments for methods

* #43 add tests to suite

* #43 add isExceptionHandled method
update version to 1.0.0
  • Loading branch information
knysh authored Apr 21, 2020
1 parent 0fe7e5b commit 9e066cb
Show file tree
Hide file tree
Showing 18 changed files with 389 additions and 89 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.github.aquality-automation</groupId>
<artifactId>aquality-selenium-core</artifactId>
<version>0.0.3</version>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Aquality Selenium Core</name>
<description>Library with core functions simplifying work with Selenium-controlled applications.</description>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import aquality.selenium.core.localization.ILocalizationModule;
import aquality.selenium.core.localization.ILocalizedLogger;
import aquality.selenium.core.logging.Logger;
import aquality.selenium.core.utilities.IActionRetrier;
import aquality.selenium.core.utilities.IElementActionRetrier;
import aquality.selenium.core.utilities.ISettingsFile;
import aquality.selenium.core.utilities.IUtilitiesModule;
Expand Down Expand Up @@ -42,6 +43,7 @@ protected void configure() {
bind(IRetryConfiguration.class).to(getRetryConfigurationImplementation()).in(Singleton.class);
bind(IElementCacheConfiguration.class).to(getElementCacheConfigurationImplementation()).in(Singleton.class);
bind(IElementActionRetrier.class).to(getElementActionRetrierImplementation()).in(Singleton.class);
bind(IActionRetrier.class).to(getActionRetrierImplementation()).in(Singleton.class);
bind(ILocalizationManager.class).to(getLocalizationManagerImplementation()).in(Singleton.class);
bind(ILocalizedLogger.class).to(getLocalizedLoggerImplementation()).in(Singleton.class);
bind(IConditionalWait.class).to(getConditionalWaitImplementation());
Expand Down
55 changes: 55 additions & 0 deletions src/main/java/aquality/selenium/core/utilities/ActionRetrier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package aquality.selenium.core.utilities;

import aquality.selenium.core.configurations.IRetryConfiguration;
import com.google.inject.Inject;

import java.util.Collection;
import java.util.Optional;
import java.util.function.Supplier;

public class ActionRetrier implements IActionRetrier {

private IRetryConfiguration retryConfiguration;

@Inject
public ActionRetrier(IRetryConfiguration retryConfiguration) {
this.retryConfiguration = retryConfiguration;
}

@Override
public void doWithRetry(Runnable runnable, Collection<Class<? extends Throwable>> handledExceptions) {
Supplier<?> supplier = () -> {
runnable.run();
return true;
};
doWithRetry(supplier, handledExceptions);
}

@Override
public <T> T doWithRetry(Supplier<T> function, Collection<Class<? extends Throwable>> handledExceptions) {
int retryAttemptsLeft = retryConfiguration.getNumber();
Optional<T> result = Optional.empty();
while (retryAttemptsLeft >= 0) {
try {
result = Optional.of(function.get());
break;
} catch (Exception exception) {
if (isExceptionHandled(handledExceptions, exception) && retryAttemptsLeft != 0) {
try {
Thread.sleep(retryConfiguration.getPollingInterval().toMillis());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
retryAttemptsLeft--;
} else {
throw exception;
}
}
}
return result.orElse(null);
}

protected boolean isExceptionHandled(Collection<Class<? extends Throwable>> handledExceptions, Exception exception) {
return handledExceptions.contains(exception.getClass());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,22 @@
import aquality.selenium.core.configurations.IRetryConfiguration;
import com.google.inject.Inject;

import java.util.Optional;
import java.util.function.Supplier;

public class ElementActionRetrier implements IElementActionRetrier {

private IRetryConfiguration retryConfiguration;
public class ElementActionRetrier extends ActionRetrier implements IElementActionRetrier {

@Inject
public ElementActionRetrier(IRetryConfiguration retryConfiguration) {
this.retryConfiguration = retryConfiguration;
super(retryConfiguration);
}

@Override
public void doWithRetry(Runnable runnable) {
Supplier<?> supplier = () -> {
runnable.run();
return true;
};
doWithRetry(supplier);
doWithRetry(runnable, getHandledExceptions());
}

@Override
public <T> T doWithRetry(Supplier<T> function) {
int retryAttemptsLeft = retryConfiguration.getNumber();
Optional<T> result = Optional.empty();
while (retryAttemptsLeft >= 0) {
try {
result = Optional.of(function.get());
break;
} catch (Exception exception) {
if (isExceptionHandled(exception) && retryAttemptsLeft != 0) {
try {
Thread.sleep(retryConfiguration.getPollingInterval().toMillis());
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
retryAttemptsLeft--;
} else {
throw exception;
}
}
}
return result.orElse(null);
}

protected boolean isExceptionHandled(Exception exception) {
return getHandledExceptions().contains(exception.getClass());
return doWithRetry(function, getHandledExceptions());
}
}
26 changes: 26 additions & 0 deletions src/main/java/aquality/selenium/core/utilities/IActionRetrier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package aquality.selenium.core.utilities;

import java.util.Collection;
import java.util.function.Supplier;

/**
* Retries an action or function when handledExceptions occurs.
*/
public interface IActionRetrier {

/**
* Retries the action when the handled exception occurs.
* @param runnable Action to be applied.
* @param handledExceptions Exceptions to be handled.
*/
void doWithRetry(Runnable runnable, Collection<Class<? extends Throwable>> handledExceptions);

/**
* Retries the function when the handled exception occurs.
* @param function Function to be applied.
* @param handledExceptions Exceptions to be handled.
* @param <T> Return type of function.
* @return Result of the function.
*/
<T> T doWithRetry(Supplier<T> function, Collection<Class<? extends Throwable>> handledExceptions);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
/**
* Retries an action or function when {@link #getHandledExceptions()} occurs.
*/
public interface IElementActionRetrier {
public interface IElementActionRetrier extends IActionRetrier {


/**
* Retries the action when the handled exception {@link #getHandledExceptions()} occurs.
Expand All @@ -30,7 +31,7 @@ public interface IElementActionRetrier {
* Exceptions to be ignored during action retrying.
* @return By the default implementation, {@link StaleElementReferenceException} and {@link InvalidElementStateException} are handled.
*/
default List<Class<? extends Exception>> getHandledExceptions() {
default List<Class<? extends Throwable>> getHandledExceptions() {
return Arrays.asList(StaleElementReferenceException.class, InvalidElementStateException.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
*/
public interface IUtilitiesModule {

/**
* @return implementation of {@link IActionRetrier}.
*/
default Class<? extends IActionRetrier> getActionRetrierImplementation() {
return ActionRetrier.class;
}

/**
* @return implementation of {@link IElementActionRetrier}.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ private Object castEnvOrDefaulValue(JsonNode node, String envVar) {
return envVar == null ? node.asLong() : Long.parseLong(envVar);
} else if (node.isInt()) {
return envVar == null ? node.asInt() : Integer.parseInt(envVar);
} else if (node.isDouble()) {
return envVar == null ? node.asDouble() : Double.parseDouble(envVar);
} else {
return envVar == null ? node.asText() : envVar;
}
Expand Down
21 changes: 21 additions & 0 deletions src/test/java/tests/applications/CustomConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package tests.applications;

import aquality.selenium.core.utilities.ISettingsFile;
import com.google.inject.Inject;

public class CustomConfiguration implements ICustomConfiguration {
private final ISettingsFile settingsFile;

@Inject
public CustomConfiguration(ISettingsFile settingsFile){
this.settingsFile = settingsFile;
}

public double getDoubleValue() {
return (double) settingsFile.getValue("/custom/doubleValue");
}

public String getStringValue() {
return settingsFile.getValue("/custom/stringValue").toString();
}
}
8 changes: 8 additions & 0 deletions src/test/java/tests/applications/ICustomConfiguration.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package tests.applications;

public interface ICustomConfiguration {

double getDoubleValue();

String getStringValue();
}
2 changes: 2 additions & 0 deletions src/test/java/tests/applications/TestModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import aquality.selenium.core.applications.AqualityModule;
import com.google.inject.Provider;
import com.google.inject.Singleton;
import tests.applications.browser.ChromeApplication;

public class TestModule extends AqualityModule<ChromeApplication> {
Expand All @@ -13,5 +14,6 @@ public TestModule(Provider<ChromeApplication> applicationProvider) {
protected void configure() {
super.configure();
bind(ICustomDependency.class).to(CustomDependency.class);
bind(ICustomConfiguration.class).to(CustomConfiguration.class).in(Singleton.class);
}
}
4 changes: 4 additions & 0 deletions src/test/java/tests/configurations/BaseProfileTest.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package tests.configurations;

import aquality.selenium.core.applications.AqualityModule;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import tests.applications.CustomAqualityServices;

public class BaseProfileTest {

Expand All @@ -20,5 +22,7 @@ public void restoreProfile() {
} else {
System.setProperty(PROFILE_KEY, previousProfile);
}

CustomAqualityServices.initInjector(new AqualityModule<>(CustomAqualityServices::getApplication));
}
}
50 changes: 50 additions & 0 deletions src/test/java/tests/configurations/CustomConfigurationTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package tests.configurations;

import aquality.selenium.core.utilities.ISettingsFile;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
import tests.applications.CustomAqualityServices;
import tests.applications.CustomConfiguration;
import tests.applications.TestModule;

import static org.testng.Assert.assertEquals;

public class CustomConfigurationTests extends BaseProfileTest {

private static final String PROFILE = "customconfiguration";
private static final String DOUBLE_VALUE_KEY = "custom.doubleValue";

@BeforeMethod
public void before() {
System.setProperty(PROFILE_KEY, PROFILE);
CustomAqualityServices.initInjector(new TestModule(CustomAqualityServices::getApplication));
}

@Test
public void testShouldBePossibleToGetDoubleValueFromCustomConfiguration() {
checkDoubleValue(0.2);
}

@Test
public void testShouldBePossibleToGetDoubleValueFromEnvironment() {
System.setProperty(DOUBLE_VALUE_KEY, "0.3");
checkDoubleValue(0.3);
}

@Test
public void testShouldBePossibleToGetValueFromCustomConfiguration() {
String value = CustomAqualityServices.getServiceProvider().getInstance(CustomConfiguration.class).getStringValue();
assertEquals(value, "customString", "String value should be got from custom configuration");
}

@AfterMethod
public void after() {
System.clearProperty(DOUBLE_VALUE_KEY);
}

private void checkDoubleValue(double expectedValue){
double value = new CustomConfiguration(CustomAqualityServices.getServiceProvider().getInstance(ISettingsFile.class)).getDoubleValue();
assertEquals(value, expectedValue, "Double value should be got correctly");
}
}
Loading

0 comments on commit 9e066cb

Please sign in to comment.