From a70c83ddffb56a7d1795560b4546b4d6301e24a9 Mon Sep 17 00:00:00 2001 From: Viet Nguyen Duc Date: Mon, 4 Dec 2023 16:59:52 +0530 Subject: [PATCH] Add unit test for demo Signed-off-by: Viet Nguyen Duc --- test-libraries/pom.xml | 20 +++++++ .../org/ndviet/library/math/MathHelpers.java | 4 +- .../ndviet/library/math/MathHelpersTest.java | 58 ++++++++++++++++++ .../library/webui/driver/BrowserFactory.java | 4 +- .../library/webui/driver/WebUITest.java | 59 +++++++++++++++++++ 5 files changed, 142 insertions(+), 3 deletions(-) create mode 100644 test-libraries/test-libraries-utilities/src/test/java/org/ndviet/library/math/MathHelpersTest.java create mode 100644 test-libraries/test-libraries-webui/src/test/java/org/ndviet/library/webui/driver/WebUITest.java diff --git a/test-libraries/pom.xml b/test-libraries/pom.xml index 9b472e9..7839f1a 100644 --- a/test-libraries/pom.xml +++ b/test-libraries/pom.xml @@ -31,6 +31,26 @@ org.apache.logging.log4j log4j-api + + org.junit.jupiter + junit-jupiter-api + + + org.junit.jupiter + junit-jupiter-engine + + + org.junit.jupiter + junit-jupiter-params + + + org.mockito + mockito-core + + + org.mockito + mockito-inline + \ No newline at end of file diff --git a/test-libraries/test-libraries-utilities/src/main/java/org/ndviet/library/math/MathHelpers.java b/test-libraries/test-libraries-utilities/src/main/java/org/ndviet/library/math/MathHelpers.java index 435ac88..b34608a 100644 --- a/test-libraries/test-libraries-utilities/src/main/java/org/ndviet/library/math/MathHelpers.java +++ b/test-libraries/test-libraries-utilities/src/main/java/org/ndviet/library/math/MathHelpers.java @@ -48,8 +48,8 @@ public static String numberDecimalFormat(String input, String decimal, String ro public static boolean isCreatable(String text) { try { - createNumber(text); - return true; + Double number = createNumber(text); + return number != null; } catch (Exception e) { return false; } diff --git a/test-libraries/test-libraries-utilities/src/test/java/org/ndviet/library/math/MathHelpersTest.java b/test-libraries/test-libraries-utilities/src/test/java/org/ndviet/library/math/MathHelpersTest.java new file mode 100644 index 0000000..0783c17 --- /dev/null +++ b/test-libraries/test-libraries-utilities/src/test/java/org/ndviet/library/math/MathHelpersTest.java @@ -0,0 +1,58 @@ +package org.ndviet.library.math; + +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.CsvSource; +import org.junit.jupiter.params.provider.MethodSource; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.ndviet.library.configuration.ConfigurationHelpers; + +import java.util.Locale; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class MathHelpersTest { + + @ParameterizedTest + @MethodSource("provideParametersForNumberDecimalFormat") + public void numberDecimalFormat(String input, String decimal, String roundingMode, String expected) { + String result = MathHelpers.numberDecimalFormat(input, decimal, roundingMode); + assertEquals(expected, result); + } + + private static Stream provideParametersForNumberDecimalFormat() { + return Stream.of( + Arguments.of("1234.5678", "#.##", "HALF_UP", "1234.57"), + Arguments.of("invalid", "#.##", "HALF_UP", "invalid"), + Arguments.of("1234.5678", "#.##", null, "1234.57"), + Arguments.of("1234.5678", null, "HALF_UP", "1234.5678") + ); + } + + @ParameterizedTest + @CsvSource({ + "1234.5678, true", + "0, true", + "-1234, true", + "1.23E3, true", + "invalid, false", + ", false" + }) + public void isCreatable(String input, boolean expected) { + assertEquals(expected, MathHelpers.isCreatable(input)); + } + + @ParameterizedTest + @CsvSource({ + "'1234,5678', true", + "'-0,75', true", + }) + public void isCreatable_different_locate(String input, boolean expected) { + try (MockedStatic mockHelpers = Mockito.mockStatic(ConfigurationHelpers.class)) { + mockHelpers.when(ConfigurationHelpers::getSystemLocale).thenReturn(Locale.FRANCE); + assertEquals(expected, MathHelpers.isCreatable(input)); + } + } +} \ No newline at end of file diff --git a/test-libraries/test-libraries-webui/src/main/java/org/ndviet/library/webui/driver/BrowserFactory.java b/test-libraries/test-libraries-webui/src/main/java/org/ndviet/library/webui/driver/BrowserFactory.java index 647812f..2ce74dc 100644 --- a/test-libraries/test-libraries-webui/src/main/java/org/ndviet/library/webui/driver/BrowserFactory.java +++ b/test-libraries/test-libraries-webui/src/main/java/org/ndviet/library/webui/driver/BrowserFactory.java @@ -31,7 +31,9 @@ public WebDriver createLocalDriver() { public ChromeOptions getOptions() { ChromeOptions options = new ChromeOptions(); List listArgs = ConfigurationManager.getInstance().getListValues(SELENIUM_CHROME_ARGS); - options.addArguments(listArgs.toArray(new String[0])); + if (listArgs != null) { + listArgs.forEach(arg -> options.addArguments(arg)); + } LinkedHashMap listPrefs = ConfigurationManager.getInstance().getMapValues(SELENIUM_CHROME_PREFS); if (listPrefs != null) { listPrefs.forEach((key, value) -> { diff --git a/test-libraries/test-libraries-webui/src/test/java/org/ndviet/library/webui/driver/WebUITest.java b/test-libraries/test-libraries-webui/src/test/java/org/ndviet/library/webui/driver/WebUITest.java new file mode 100644 index 0000000..b6c85f2 --- /dev/null +++ b/test-libraries/test-libraries-webui/src/test/java/org/ndviet/library/webui/driver/WebUITest.java @@ -0,0 +1,59 @@ +package org.ndviet.library.webui.driver; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockedStatic; +import org.mockito.Mockito; +import org.ndviet.library.WebUI; +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.chrome.ChromeDriver; + +import java.time.Duration; + +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.ndviet.library.configuration.Constants.SELENIUM_BROWSER_TYPE; +import static org.ndviet.library.configuration.Constants.SELENIUM_WEB_DRIVER_TARGET; + +public class WebUITest { + + @Mock + private WebDriver mockDriver; + + @BeforeAll + public static void setUpClass() { + System.setProperty(SELENIUM_WEB_DRIVER_TARGET, "local"); + System.setProperty(SELENIUM_BROWSER_TYPE, "chrome"); + } + + @BeforeEach + public void setUp() { + try (MockedStatic mockFactory = Mockito.mockStatic(TargetFactory.class)) { + mockFactory.when(TargetFactory::createInstance).thenReturn(mockDriver); + } + } + + @AfterEach + public void tearDown() { + mockDriver.close(); + } + + @Test + public void openBrowser_returnsDriver() { + mockDriver = WebUI.openBrowser(); + assertNotNull(mockDriver); + assertTrue(mockDriver instanceof ChromeDriver); + } + + @Test + public void openBrowser_withUrl_returnsDriver() { + String url = "https://demoqa.com/"; + mockDriver = WebUI.openBrowser(url); + mockDriver.manage().timeouts().implicitlyWait(Duration.ofSeconds(10)); + assertNotNull(mockDriver); + assertTrue(mockDriver.getTitle().equals("DEMOQA")); + } +} \ No newline at end of file