From b38868cbeed607361adf47663238df9c941e91da Mon Sep 17 00:00:00 2001 From: sergiomartins8 Date: Sat, 18 Apr 2020 17:35:45 +0100 Subject: [PATCH] bring harmony between custom system configurations and selenide's --- README.md | 27 +++++++---- src/test/java/base/FrameworkBootstrap.java | 27 ++++++----- src/test/java/base/MockContext.java | 48 ++++++------------- .../selectors/ExampleSelector.java | 2 +- src/test/java/tests/ExampleTest.java | 2 +- .../utils/config/CustomConfiguration.java | 12 +---- .../config/CustomConfigurationHolder.java | 14 +++--- .../utils/listeners/MockServerListener.java | 2 + src/test/java/utils/mocks/MockDefinition.java | 15 ++++-- src/test/java/utils/mocks/MockParser.java | 2 +- .../java/utils/mocks/model/MockHeader.java | 2 +- .../mocks/model/MockQueryStringParameter.java | 2 +- .../java/utils/mocks/model/MockRequest.java | 2 +- .../java/utils/mocks/model/MockResponse.java | 18 +++---- 14 files changed, 83 insertions(+), 92 deletions(-) diff --git a/README.md b/README.md index 4a182ff..e4bac9c 100644 --- a/README.md +++ b/README.md @@ -61,26 +61,33 @@ $ mvn test [-Dselenide.remote=http://localhost:4444/wd/hub] #### System properties ````shell script -$ mvn test [-Dmock.server.url= -Dmock.server.port=] [-Dparallel=] [-DthreadCount=] [-Dlistener=] +$ mvn test [-Dmock.server.url= -Dmock.server.port=] \ + [-Dparallel=] \ + [-DthreadCount=] \ + [-Dlistener=] ```` -| Property | Description | Default | -| --------------------------------- | --------------------------------- | ----------------------------------------- | -| `mock.server.url` | Mock server url. | `null` _(Mock server it not used)_ | -| `mock.server.port` | Mock server port. | `0` _(Mock server it not used)_ | -| `parallel` | Enables parallel threads. | `false` | -| `threadCount` | The default number of threads to use when running tests in parallel. | `1` | -| `listener` | A comma-separated list of java classes that can be found on your classpath. | `null` _(Listeners not being used)_ | +| Property | Description | Default | +| --------------------------------- | --------------------------------- | ----------------------------------------- | +| `mock.server.address` | Mock server address | `null` _(Mock server it not used)_ | +| `parallel` | Enables parallel threads | `false` | +| `threadCount` | The default number of threads to use when running tests in parallel | `1` | +| `listener` | A comma-separated list of java classes that can be found on your classpath | `null` _(Listeners not being used)_ | #### More system properties -Using the goods of selenide, you can also inject its system properties +Using the goods of selenide, you can also inject its system properties. ##### Example: ```shell script -mvn test -Dselenide.remote=http://localhost:4444/wd/hub -Dselenide.headless=true -Dselenide.browser=firefox -Dselenide.baseUrl=http:/google.com +mvn test -Dselenide.remote=http://localhost:4444/wd/hub \ + -Dselenide.headless=true \ + -Dselenide.browser=firefox \ + -Dselenide.baseUrl=http:/google.com ``` +More about selenide's configuration settings and documentation [here](https://selenide.org/javadoc/current/com/codeborne/selenide/Configuration.html). + ### Listeners There are a couple listeners available _(however, disabled by default)_. diff --git a/src/test/java/base/FrameworkBootstrap.java b/src/test/java/base/FrameworkBootstrap.java index 0cfee33..a0ea1ef 100644 --- a/src/test/java/base/FrameworkBootstrap.java +++ b/src/test/java/base/FrameworkBootstrap.java @@ -1,9 +1,12 @@ package base; -import org.testng.annotations.AfterMethod; -import org.testng.annotations.BeforeMethod; +import org.testng.annotations.AfterSuite; +import org.testng.annotations.BeforeSuite; +import utils.config.CustomConfiguration; import utils.logging.Loggable; +import java.util.Optional; + /** * Where it all starts. *
@@ -13,22 +16,22 @@ public abstract class FrameworkBootstrap implements Loggable { /** - * Use {@code @BeforeMethod} when running tests in {@code parallel=methods}. Edit accordingly. - * If sequential test execution use {@code @BeforeSuite} annotation. + * Tells {@link MockContext} to initialize the mock server if it is present as a system property. */ - @BeforeMethod + @BeforeSuite public void initializeMockServer() { - MockContext.initializeMockServerClient(); + if (Optional.ofNullable(CustomConfiguration.mockServerAddress).isPresent()) { + MockContext.initializeMockServerClient(); + } } /** - * Related to the {@link #initializeMockServer()} method. - *
- * Use {@code @AfterMethod} when running tests in {@code parallel=methods}. Edit accordingly. - * If sequential test execution use {@code @AfterSuite} annotation. + * Tells {@link MockContext} to reset the mock server data if it is present as a system property. */ - @AfterMethod + @AfterSuite public void teardownMockServer() { - MockContext.resetMockServerClient(); + if (Optional.ofNullable(CustomConfiguration.mockServerAddress).isPresent()) { + MockContext.resetMockServerClient(); + } } } diff --git a/src/test/java/base/MockContext.java b/src/test/java/base/MockContext.java index 2ecd006..635c59a 100644 --- a/src/test/java/base/MockContext.java +++ b/src/test/java/base/MockContext.java @@ -2,50 +2,30 @@ import org.mockserver.client.MockServerClient; import utils.config.CustomConfiguration; -import utils.listeners.MockServerListener; + +import java.net.URI; /** - * Single responsibility is to hold a mock server object so that it can be used by the {@link MockServerListener}. + * Context for objects for mocking purposes. */ -public final class MockContext { - - /** - * Thread safe implementation that holds the mock server client. - */ - private static final ThreadLocal MOCK_SERVER_CLIENT_THREAD_LOCAL = new ThreadLocal<>(); - - /** - * Verifies if both mock server url and port are set as system properties. - *
- * System properties may be set as such: - *
- * Example: {@code mvn clean test -Dmock.server.url=localhost -Dmock.server.port=3000} - *
- * However, system properties can also be configured under testng.xml configuration - */ - public static final boolean HAS_MOCK_SERVER_PROPERTY = CustomConfiguration.mockServerUrl != null - && CustomConfiguration.mockServerPort != 0; +public class MockContext { - public static MockServerClient getMockServerClient() { - return MOCK_SERVER_CLIENT_THREAD_LOCAL.get(); - } + private static MockServerClient mockServerClient; /** - * Initializes the mock server client if {@link #HAS_MOCK_SERVER_PROPERTY} is true. + * Initializes the mock server client based on {@link CustomConfiguration} settings. */ public static void initializeMockServerClient() { - if (HAS_MOCK_SERVER_PROPERTY) { - MockServerClient mockServerClient = new MockServerClient(CustomConfiguration.mockServerUrl, CustomConfiguration.mockServerPort); - MOCK_SERVER_CLIENT_THREAD_LOCAL.set(mockServerClient); - } + MockContext.mockServerClient = new MockServerClient( + URI.create(CustomConfiguration.mockServerAddress).getHost(), + URI.create(CustomConfiguration.mockServerAddress).getPort()); } - /** - * Resets the mock server client if {@link #HAS_MOCK_SERVER_PROPERTY} is true. - */ public static void resetMockServerClient() { - if (HAS_MOCK_SERVER_PROPERTY) { - MOCK_SERVER_CLIENT_THREAD_LOCAL.get().reset(); - } + mockServerClient.reset(); + } + + public static MockServerClient getMockServerClient() { + return mockServerClient; } } diff --git a/src/test/java/pageobjects/selectors/ExampleSelector.java b/src/test/java/pageobjects/selectors/ExampleSelector.java index 156ffa7..f798868 100644 --- a/src/test/java/pageobjects/selectors/ExampleSelector.java +++ b/src/test/java/pageobjects/selectors/ExampleSelector.java @@ -1,6 +1,6 @@ package pageobjects.selectors; -public final class ExampleSelector { +public class ExampleSelector { /** * Private constructor to avoid instantiation. diff --git a/src/test/java/tests/ExampleTest.java b/src/test/java/tests/ExampleTest.java index 8809f32..ecc622e 100644 --- a/src/test/java/tests/ExampleTest.java +++ b/src/test/java/tests/ExampleTest.java @@ -25,7 +25,7 @@ public void teardown() { @Test(description = "Open up a google page and search for the word 'dogs'") public void testExampleOne() { - open(""); // empty string opens the base URL defined on + open("http://google.com"); examplePage.exampleComponent() .waitPageLoaded() diff --git a/src/test/java/utils/config/CustomConfiguration.java b/src/test/java/utils/config/CustomConfiguration.java index 96b47c4..5021648 100644 --- a/src/test/java/utils/config/CustomConfiguration.java +++ b/src/test/java/utils/config/CustomConfiguration.java @@ -17,17 +17,9 @@ public class CustomConfiguration { /** * URL for the mock server. - * Can be configured either programmatically or by system property: {@code -Dmock.server.url=localhost} + * Can be configured either programmatically or by system property: {@code -Dmock.server.address=localhost:3000} *
* Default value: null (Mock server it not used) */ - public static String mockServerUrl = DEFAULTS.mockServerUrl(); - - /** - * Port for the mock server. - * Can be configured either programmatically or by system property: {@code -Dmock.server.port=3000} - *
- * Default value: 0 (Mock server it not used) - */ - public static int mockServerPort = DEFAULTS.mockServerPort(); + public static String mockServerAddress = DEFAULTS.mockServerAddress(); } diff --git a/src/test/java/utils/config/CustomConfigurationHolder.java b/src/test/java/utils/config/CustomConfigurationHolder.java index 60bad45..548128f 100644 --- a/src/test/java/utils/config/CustomConfigurationHolder.java +++ b/src/test/java/utils/config/CustomConfigurationHolder.java @@ -1,15 +1,13 @@ package utils.config; -public class CustomConfigurationHolder { +import utils.logging.Loggable; - private final String mockServerUrl = System.getProperty("mock.server.url"); - private final int mockServerPort = Integer.parseInt(System.getProperty("mock.server.port", "0")); +public class CustomConfigurationHolder implements Loggable { - public String mockServerUrl() { - return mockServerUrl; - } + private final String mockServerAddress = System.getProperty("mock.server.address"); - public int mockServerPort() { - return mockServerPort; + public String mockServerAddress() { + logger().debug("Mock server address: " + mockServerAddress); + return mockServerAddress; } } diff --git a/src/test/java/utils/listeners/MockServerListener.java b/src/test/java/utils/listeners/MockServerListener.java index 4968256..4396539 100644 --- a/src/test/java/utils/listeners/MockServerListener.java +++ b/src/test/java/utils/listeners/MockServerListener.java @@ -26,7 +26,9 @@ public class MockServerListener implements ITestListener, Loggable { public void onTestStart(ITestResult result) { extractMockAnnotation(result).ifPresent(mock -> { for (String path : mock.path()) { + logger().debug("Mocking file on path: " + path); MockDefinition mockDefinition = MockParser.toObject(path); + logger().debug("Mocking content: " + mockDefinition.prettyPrint()); MockContext.getMockServerClient() .when(request() .withMethod(requireNonNull(mockDefinition).getRequest().getMethod()) diff --git a/src/test/java/utils/mocks/MockDefinition.java b/src/test/java/utils/mocks/MockDefinition.java index 3fa3dee..ce3aadc 100644 --- a/src/test/java/utils/mocks/MockDefinition.java +++ b/src/test/java/utils/mocks/MockDefinition.java @@ -1,16 +1,16 @@ package utils.mocks; +import com.google.gson.GsonBuilder; import utils.mocks.model.MockRequest; import utils.mocks.model.MockResponse; /** * Concrete definition of a mock. - *

+ *
* A mock is represented by a {@link MockRequest} and by a {@link MockResponse} - *

*/ @SuppressWarnings("unused") -public final class MockDefinition { +public class MockDefinition { /** * Represents the mocked request. @@ -37,4 +37,13 @@ public void setResponse(MockResponse response) { public void setRequest(MockRequest request) { this.request = request; } + + /** + * Similar to a `toString()` implementation. Useful for debugging purposes. + * + * @return current object in a pretty json format. + */ + public String prettyPrint() { + return new GsonBuilder().setPrettyPrinting().create().toJson(this); + } } diff --git a/src/test/java/utils/mocks/MockParser.java b/src/test/java/utils/mocks/MockParser.java index a26490e..d8cb372 100644 --- a/src/test/java/utils/mocks/MockParser.java +++ b/src/test/java/utils/mocks/MockParser.java @@ -7,7 +7,7 @@ /** * Single responsibility of parsing json files into {@link MockDefinition} objects. */ -public final class MockParser { +public class MockParser { public static MockDefinition toObject(String jsonPath) { return new Gson().fromJson(new InputStreamReader(MockParser.class.getResourceAsStream(jsonPath)), MockDefinition.class); diff --git a/src/test/java/utils/mocks/model/MockHeader.java b/src/test/java/utils/mocks/model/MockHeader.java index 2395c27..49f999d 100644 --- a/src/test/java/utils/mocks/model/MockHeader.java +++ b/src/test/java/utils/mocks/model/MockHeader.java @@ -4,7 +4,7 @@ * Concrete definition of a mocked header. */ @SuppressWarnings("unused") -public final class MockHeader { +public class MockHeader { private String name; diff --git a/src/test/java/utils/mocks/model/MockQueryStringParameter.java b/src/test/java/utils/mocks/model/MockQueryStringParameter.java index 4c68272..73e7639 100644 --- a/src/test/java/utils/mocks/model/MockQueryStringParameter.java +++ b/src/test/java/utils/mocks/model/MockQueryStringParameter.java @@ -6,7 +6,7 @@ * Concrete definition of a mocked query parameter. */ @SuppressWarnings("unused") -public final class MockQueryStringParameter { +public class MockQueryStringParameter { private String name; diff --git a/src/test/java/utils/mocks/model/MockRequest.java b/src/test/java/utils/mocks/model/MockRequest.java index 2663f0f..1896857 100644 --- a/src/test/java/utils/mocks/model/MockRequest.java +++ b/src/test/java/utils/mocks/model/MockRequest.java @@ -24,7 +24,7 @@ * Each header is represented by {@link MockHeader} */ @SuppressWarnings("unused") -public final class MockRequest { +public class MockRequest { private String path; diff --git a/src/test/java/utils/mocks/model/MockResponse.java b/src/test/java/utils/mocks/model/MockResponse.java index 6a5d10c..aae9ac9 100644 --- a/src/test/java/utils/mocks/model/MockResponse.java +++ b/src/test/java/utils/mocks/model/MockResponse.java @@ -11,17 +11,17 @@ /** * Concrete definition of a mocked response. - * - *

A mocked response is represented by a body and a status code. - * - *

Optionally, it also may contain a list of headers and a list of cookies. - * - *

Each header is represented by {@link MockHeader} - * - *

Each cookie is represented by {@link MockCookie} + *
+ * A mocked response is represented by a body and a status code. + *
+ * Optionally, it also may contain a list of headers and a list of cookies. + *
+ * Each header is represented by {@link MockHeader} + *
+ * Each cookie is represented by {@link MockCookie} */ @SuppressWarnings("unused") -public final class MockResponse { +public class MockResponse { /** * Holds the whole response body.