-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Bug/43 cast json value to double (#44)
* #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
Showing
18 changed files
with
389 additions
and
89 deletions.
There are no files selected for viewing
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
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
55 changes: 55 additions & 0 deletions
55
src/main/java/aquality/selenium/core/utilities/ActionRetrier.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,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()); | ||
} | ||
} |
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
26 changes: 26 additions & 0 deletions
26
src/main/java/aquality/selenium/core/utilities/IActionRetrier.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,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); | ||
} |
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
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
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
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,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(); | ||
} | ||
} |
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,8 @@ | ||
package tests.applications; | ||
|
||
public interface ICustomConfiguration { | ||
|
||
double getDoubleValue(); | ||
|
||
String getStringValue(); | ||
} |
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
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
50 changes: 50 additions & 0 deletions
50
src/test/java/tests/configurations/CustomConfigurationTests.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,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"); | ||
} | ||
} |
Oops, something went wrong.