diff --git a/openmaqs-appium/config.xml b/openmaqs-appium/config.xml index 14607a7d0..c9fb2a1ac 100644 --- a/openmaqs-appium/config.xml +++ b/openmaqs-appium/config.xml @@ -37,9 +37,12 @@ Android 11.0 Android GoogleAPI Emulator + Chrome + + https://openmaqs.github.io/TestingSite/Automation/ - http://ondemand.saucelabs.com:80/wd/hub + https://ondemand.saucelabs.com:80/wd/hub 122000 diff --git a/openmaqs-appium/src/main/java/io/github/openmaqs/appium/AppiumConfig.java b/openmaqs-appium/src/main/java/io/github/openmaqs/appium/AppiumConfig.java index d809c1ba9..9006884f4 100644 --- a/openmaqs-appium/src/main/java/io/github/openmaqs/appium/AppiumConfig.java +++ b/openmaqs-appium/src/main/java/io/github/openmaqs/appium/AppiumConfig.java @@ -8,10 +8,12 @@ import io.github.openmaqs.appium.exceptions.AppiumConfigException; import io.github.openmaqs.utilities.helper.Config; import io.github.openmaqs.utilities.helper.ConfigSection; +import io.github.openmaqs.utilities.helper.ConfigValidation; import io.github.openmaqs.utilities.helper.StringProcessor; import java.net.MalformedURLException; import java.net.URL; import java.time.Duration; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; @@ -31,7 +33,22 @@ public class AppiumConfig { private static final ConfigSection APPIUM_CAPS_SECTION = ConfigSection.APPIUM_CAPS_MAQS; private AppiumConfig() { + } + + /* + Loads when class is loaded. + */ + static { + checkConfig(); + } + /** + * Ensure required fields are in the config. + */ + private static void checkConfig() { + ConfigValidation validator = new ConfigValidation(); + validator.setRequiredOneOfFields(Arrays.asList("App", "BrowserName")); + Config.validate(ConfigSection.APPIUM_MAQS, validator); } /** diff --git a/openmaqs-database/src/main/java/io/github/openmaqs/database/DatabaseConfig.java b/openmaqs-database/src/main/java/io/github/openmaqs/database/DatabaseConfig.java index 2a7f3f7ea..35afaa9e8 100644 --- a/openmaqs-database/src/main/java/io/github/openmaqs/database/DatabaseConfig.java +++ b/openmaqs-database/src/main/java/io/github/openmaqs/database/DatabaseConfig.java @@ -10,17 +10,32 @@ import io.github.openmaqs.database.providers.SQLiteProvider; import io.github.openmaqs.utilities.helper.Config; import io.github.openmaqs.utilities.helper.ConfigSection; +import io.github.openmaqs.utilities.helper.ConfigValidation; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; /** - * The type Database config. + * The Database Config class. */ public class DatabaseConfig { private DatabaseConfig() { } + static { + checkConfig(); + } + + /** + * Ensure required fields are in the config. + */ + private static void checkConfig() { + var validator = new ConfigValidation(); + validator.setRequiredOneOfFields(Arrays.asList("DatabaseConnectionString", "DatabaseProviderType")); + Config.validate(ConfigSection.DATABASE_MAQS, validator); + } + /** * The Database section. */ diff --git a/openmaqs-playwright/src/main/java/io/github/openmaqs/playwright/PlaywrightConfig.java b/openmaqs-playwright/src/main/java/io/github/openmaqs/playwright/PlaywrightConfig.java index 9a4d1913e..4d9c68482 100644 --- a/openmaqs-playwright/src/main/java/io/github/openmaqs/playwright/PlaywrightConfig.java +++ b/openmaqs-playwright/src/main/java/io/github/openmaqs/playwright/PlaywrightConfig.java @@ -6,7 +6,9 @@ import io.github.openmaqs.utilities.helper.Config; import io.github.openmaqs.utilities.helper.ConfigSection; +import io.github.openmaqs.utilities.helper.ConfigValidation; import java.awt.Dimension; +import java.util.Collections; /** * The Playwright Config class. @@ -19,6 +21,19 @@ public class PlaywrightConfig { protected PlaywrightConfig() { } + static { + checkConfig(); + } + + /** + * Ensure required fields are in the config. + */ + private static void checkConfig() { + var validator = new ConfigValidation(); + validator.setRequiredOneOfFields(Collections.singletonList("Timeout")); + Config.validate(ConfigSection.PLAYWRIGHT_MAQS, validator); + } + /** * Get the website base url. * @return The website base url diff --git a/openmaqs-selenium/src/main/java/io/github/openmaqs/selenium/SeleniumConfig.java b/openmaqs-selenium/src/main/java/io/github/openmaqs/selenium/SeleniumConfig.java index baa9eafc2..ee92847d2 100644 --- a/openmaqs-selenium/src/main/java/io/github/openmaqs/selenium/SeleniumConfig.java +++ b/openmaqs-selenium/src/main/java/io/github/openmaqs/selenium/SeleniumConfig.java @@ -8,8 +8,10 @@ import io.github.openmaqs.selenium.constants.RemoteBrowserType; import io.github.openmaqs.utilities.helper.Config; import io.github.openmaqs.utilities.helper.ConfigSection; +import io.github.openmaqs.utilities.helper.ConfigValidation; import io.github.openmaqs.utilities.helper.StringProcessor; import java.time.Duration; +import java.util.Collections; import java.util.HashMap; import java.util.Map; @@ -22,7 +24,19 @@ public final class SeleniumConfig { * Private constructor. */ private SeleniumConfig() { + } + + static { + checkConfig(); + } + /** + * Ensure required fields are in the config. + */ + private static void checkConfig() { + ConfigValidation validator = new ConfigValidation(); + validator.setRequiredFields(Collections.singletonList("BrowserTimeout")); + Config.validate(ConfigSection.SELENIUM_MAQS, validator); } /** diff --git a/openmaqs-utilities/appsettings.json b/openmaqs-utilities/appsettings.json new file mode 100644 index 000000000..726195a0d --- /dev/null +++ b/openmaqs-utilities/appsettings.json @@ -0,0 +1,53 @@ +{ + "GlobalMaqs": { + "WaitTime": "100", + "Timeout": "1000", + "Log": "OnFail", + "LogLevel": "INFORMATION", + "LogType": "TXT", + "Grog": "WillFailWithoutRunSettingOverride", + "SectionOverrideCore": "WillFailWithoutRunSettingOverride", + "SimpleOverride": "simple", + "Override": "base", + "Override2": "base2", + "ConfigJsonEnvRunOverride": "JSON", + "ConfigJsonEnvRun": "JSON", + "ConfigJsonEnv": "JSON", + "ConfigJson": "JSON", + "JsonOnly": "JSON", + "low": { "JSON": "lowerJson" }, + "compound:key": "compound", + "compound:key:lower": "compoundEvenLower" + }, + "RemoteSeleniumCapsMaqs": { + "MultiLevel": { + "MoreLevels": { + "seven": "7", + "lowest": { + "nine": "9" + } + } + }, + "MuliLevel:MoreLevels": { + "four": "4", + "five": "5" + }, + "MuliLevel:MoreLevels:six": "6" + }, + "TopTest": { + "MidTest": [ + { + "LowerTest": "A", + "Lower": { + "LowestTest": "Lowest" + } + }, + { + "LowerTest": "B" + } + ], + "AnotherMid": { + "Lowerest": "AnotherLow" + } + } +} \ No newline at end of file diff --git a/openmaqs-utilities/config.ini b/openmaqs-utilities/config.ini new file mode 100644 index 000000000..b453b249d --- /dev/null +++ b/openmaqs-utilities/config.ini @@ -0,0 +1,32 @@ +; +[GlobalMaqs] +timeout=500 +waitTime=100 +log=true +level="VERBOSE" +type="TXT" +path="./target/logs" + +[SeleniumMaqs] +softAssertOnFailure=true +format="jpeg" +OnFailure=true +browser="HEADLESSCHROME" +waitTime=1000 +browser.timeout=20000 +browser.size=1920x1080 +remote="Chrome" +remote.name="Chrome" +remote.url="http://ondemand.saucelabs.com:80/wd/hub" +remote.platform="OS X 10.11" +website.url="https://cognizantopensource.github.io/maqs-dotnet-templates/Static/Automation/" + +[WebServiceMaqs] +url=http://localhost:8080/maqs/services/ + + +[AppiumCapsMaqs] +url=http://localhost:4723/wd/hub + +[DatabaseMaqs] +url=jdbc:mysql://localhost:3306/maqs \ No newline at end of file diff --git a/openmaqs-utilities/config.properties b/openmaqs-utilities/config.properties new file mode 100644 index 000000000..972ace2ce --- /dev/null +++ b/openmaqs-utilities/config.properties @@ -0,0 +1,30 @@ +GlobalMaqs.WaitTime=100 +GlobalMaqs.Timeout=1000 +GlobalMaqs.Log=OnFail +GlobalMaqs.LogLevel=INFORMATION +GlobalMaqs.LogType=TXT +GlobalMaqs.Grog=WillFailWithoutRunSettingOverride +GlobalMaqs.SectionOverrideCore=WillFailWithoutRunSettingOverride +GlobalMaqs.SimpleOverride=simple +GlobalMaqs.Override=base +GlobalMaqs.Override2=base2 +GlobalMaqs.ConfigJsonEnvRunOverride=JSON +GlobalMaqs.ConfigJsonEnvRun=JSON +GlobalMaqs.ConfigJsonEnv=JSON +GlobalMaqs.ConfigJson=JSON +GlobalMaqs.JsonOnly=JSON +GlobalMaqs.low.JSON=lowerJson +GlobalMaqs.compound:key=compound +GlobalMaqs.compound:key:lower=compoundEvenLower +SeleniumMaqs.TestKey=testValueTwo +SeleniumMaqs.Browser=Internet Explorer +SeleniumMaqs.HubAddress=132.15.12 +RemoteSeleniumCapsMaqs.MultiLevel.MoreLevels.seven=7 +RemoteSeleniumCapsMaqs.MultiLevel.MoreLevels.lowest.nine=9 +RemoteSeleniumCapsMaqs.MuliLevel.MoreLevels.four=4 +RemoteSeleniumCapsMaqs.MuliLevel.MoreLevels.five=5 +RemoteSeleniumCapsMaqs.MuliLevel.MoreLevels:six=6 +TopTest.MidTest.0.LowerTest=A +TopTest.MidTest.0.Lower.LowestTest=Lowest +TopTest.MidTest.1.LowerTest=B +TopTest.AnotherMid.Lowerest=AnotherLow \ No newline at end of file diff --git a/openmaqs-utilities/config.yml b/openmaqs-utilities/config.yml new file mode 100644 index 000000000..3aee5b6f6 --- /dev/null +++ b/openmaqs-utilities/config.yml @@ -0,0 +1,40 @@ +GlobalMaqs: + WaitTime: '100' + Timeout: '1000' + Log: OnFail + LogLevel: INFORMATION + LogType: TXT + Grog: WillFailWithoutRunSettingOverride + SectionOverrideCore: WillFailWithoutRunSettingOverride + SimpleOverride: simple + Override: base + Override2: base2 + ConfigJsonEnvRunOverride: JSON + ConfigJsonEnvRun: JSON + ConfigJsonEnv: JSON + ConfigJson: JSON + JsonOnly: JSON + low: + JSON: lowerJson + compound:key: compound + compound:key:lower: compoundEvenLower + +RemoteSeleniumCapsMaqs: + MultiLevel: + MoreLevels: + seven: '7' + lowest: + nine: '9' + MuliLevel:MoreLevels: + four: '4' + five: '5' + MuliLevel:MoreLevels:six: '6' + +TopTest: + MidTest: + - LowerTest: A + Lower: + LowestTest: Lowest + - LowerTest: B + AnotherMid: + Lowerest: AnotherLow \ No newline at end of file diff --git a/openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/Config.java b/openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/Config.java index 14112d873..34bb27e2f 100644 --- a/openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/Config.java +++ b/openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/Config.java @@ -4,13 +4,21 @@ package io.github.openmaqs.utilities.helper; -import io.github.openmaqs.utilities.helper.exceptions.FrameworkConfigurationException; +import io.github.openmaqs.utilities.helper.exceptions.MaqsConfigException; import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.nio.file.Paths; +import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; +import java.util.List; import java.util.Map; +import org.apache.commons.configuration2.Configuration; +import org.apache.commons.configuration2.JSONConfiguration; +import org.apache.commons.configuration2.PropertiesConfiguration; import org.apache.commons.configuration2.XMLConfiguration; -import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder; +import org.apache.commons.configuration2.YAMLConfiguration; import org.apache.commons.configuration2.builder.fluent.Configurations; import org.apache.commons.configuration2.ex.ConfigurationException; import org.apache.commons.configuration2.sync.ReadWriteSynchronizer; @@ -18,7 +26,7 @@ /** * The Configuration class. */ -public final class Config { +public class Config { private Config() { // Private constructor @@ -32,17 +40,17 @@ private Config() { /** * The default config.xml file name. */ - public static final String CONFIG_FILE = PropertyManager.get("maqs.config.location", "config.xml"); + public static String CONFIG_FILE = PropertyManager.get("maqs.config.location", "config.xml"); /** * The configuration containing values loaded in from the config.xml file. */ - private static XMLConfiguration configValues; + private static Configuration configValues; /** * The configuration containing values that were added to the configuration. */ - private static final XMLConfiguration overrideConfig; + private static Configuration overrideConfig; /** * The base configs object. @@ -52,17 +60,139 @@ private Config() { // initialize the config object. static { try { - if ((new File(CONFIG_FILE).exists())) { - FileBasedConfigurationBuilder builder = configs.xmlBuilder(CONFIG_FILE); - configValues = builder.getConfiguration(); - configValues.setSynchronizer(new ReadWriteSynchronizer()); + CONFIG_FILE = checkForConfigFile(); + getConfigFile(CONFIG_FILE); + } catch (ConfigurationException exception) { + throw new MaqsConfigException(StringProcessor.safeFormatter( + "Exception creating the configuration object from the file : %s", exception)); + } catch (FileNotFoundException notFoundException) { + throw new MaqsConfigException(StringProcessor.safeFormatter("The file was not found: %s", notFoundException)); + } + } + + private static String checkForConfigFile() { + if (Paths.get("config.xml").toFile().exists()) { + return "config.xml"; + } else if (Paths.get("appsettings.json").toFile().exists()) { + return "appsettings.json"; + } else if (Paths.get("config.properties").toFile().exists()) { + return "config.properties"; + } else if (Paths.get("config.yml").toFile().exists()) { + return "config.yml"; + } else if (Paths.get("config.ini").toFile().exists()) { + return "config.ini"; + } else { + throw new MaqsConfigException("The file config is not supported"); + } + } + + protected static void getConfigFile(String configName) throws ConfigurationException, FileNotFoundException { + if (configName.contains(".xml")) { + initializeXmlConfig(); + } else if (configName.contains(".json")) { + initializeJsonConfig(); + } else if (configName.contains(".properties")) { + initializePropertiesConfig(); + } else if (configName.contains(".yml")) { + initializeYmlConfig(); + } + } + + private static void initializeXmlConfig() throws ConfigurationException { + configValues = configs.xml(CONFIG_FILE); + configValues.setSynchronizer(new ReadWriteSynchronizer()); + + overrideConfig = new XMLConfiguration(); + overrideConfig.setSynchronizer(new ReadWriteSynchronizer()); + } + + private static void initializeJsonConfig() throws ConfigurationException, FileNotFoundException { + // FileBasedConfigurationBuilder builder = configs.fileBasedBuilder( + // JSONConfiguration.class, "appsettings.json"); + + JSONConfiguration configuration = new JSONConfiguration(); + configuration.read(new FileReader("appsettings.json")); + + // configValues = builder.configure().getConfiguration(); + configValues = configs.fileBased(JSONConfiguration.class, "appsettings.json"); + configValues.setSynchronizer(new ReadWriteSynchronizer()); + + overrideConfig = new JSONConfiguration(); + overrideConfig.setSynchronizer(new ReadWriteSynchronizer()); + } + + private static void initializePropertiesConfig() throws ConfigurationException { + configValues = configs.properties("config.properties"); + configValues.setSynchronizer(new ReadWriteSynchronizer()); + + overrideConfig = new PropertiesConfiguration(); + overrideConfig.setSynchronizer(new ReadWriteSynchronizer()); + } + + private static void initializeYmlConfig() throws ConfigurationException, FileNotFoundException { + YAMLConfiguration configuration = new YAMLConfiguration(); + + try { + configuration.read(new FileReader(new File("config.yml").getAbsolutePath())); + + } catch (FileNotFoundException e) { + throw new FileNotFoundException(e.getMessage()); + } + + // FileBasedConfigurationBuilder builder = + // configs.fileBasedBuilder(YAMLConfiguration.class,"config.yml"); + // YAMLConfiguration configuration = configs.fileBased(YAMLConfiguration.class, "config.yml"); + + configValues = new YAMLConfiguration(configuration); + configValues.setSynchronizer(new ReadWriteSynchronizer()); + + overrideConfig = new YAMLConfiguration(configuration); + overrideConfig.setSynchronizer(new ReadWriteSynchronizer()); + } + + /** + * Validates the app config section by ensuring required values are present. + * @param configSection The config section to be validated + * @param configValidation A list of strings containing the required field names + */ + public static void validate(ConfigSection configSection, ConfigValidation configValidation) { + // Don't run the validation if the user has decided to skip the validation + if (getGeneralValue("SkipConfigValidation").equals("Yes")) { + return; + } + + if (configValidation == null) { + throw new MaqsConfigException("The value passed in for configValidation (required fields in a config) is null"); + } + + var configSectionPassed = getSection(configSection); + List exceptions = new ArrayList<>(); + + // Check if we have any required fields + if (configValidation.getRequiredFields() != null && !configValidation.getRequiredFields().isEmpty()) { + for (var requiredField : configValidation.getRequiredFields()) { + if (!configSectionPassed.containsKey(requiredField)) { + exceptions.add("Key missing: " + requiredField); + } } + } - overrideConfig = new XMLConfiguration(); - overrideConfig.setSynchronizer(new ReadWriteSynchronizer()); - } catch (ConfigurationException exception) { - throw new FrameworkConfigurationException(StringProcessor.safeFormatter( - "Exception creating the xml configuration object from the file : %s", exception)); + // Check if we have any one of required fields + if (configValidation.getRequiredOneOfFields() != null && !configValidation.getRequiredOneOfFields().isEmpty() + && configValidation.requiredOneOfFields.stream().noneMatch(configSectionPassed::containsKey)) { + // We have one of fields and didn't find any of them + exceptions.add("Need at least one of the following keys: " + + String.join(", ", configValidation.getRequiredOneOfFields())); + } + + if (!exceptions.isEmpty()) { + StringBuilder message = new StringBuilder(); + for (var exception : exceptions) { + message.append(exception); + } + + message.append("*This check can be skipped by setting the 'SkipConfigValidation' configuration value to 'Yes'."); + throw new MaqsConfigException(message.toString()); } } @@ -135,6 +265,7 @@ public static void addTestSettingValues(Map configurations, Stri boolean overrideExisting) { for (Map.Entry entry : configurations.entrySet()) { String sectionedKey = section + "." + entry.getKey(); + if (!overrideConfig.containsKey(sectionedKey) || overrideExisting) { overrideConfig.setProperty(sectionedKey, entry.getValue()); } diff --git a/openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/ConfigValidation.java b/openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/ConfigValidation.java new file mode 100644 index 000000000..2b16fbcfc --- /dev/null +++ b/openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/ConfigValidation.java @@ -0,0 +1,55 @@ +/* + * Copyright 2023 (C) OpenMAQS, All rights Reserved + */ + +package io.github.openmaqs.utilities.helper; + +import java.util.List; + +/** + * Elements of config files which need to be validated on load. + */ +public class ConfigValidation { + + /** + * The list of required fields for a config. + */ + List requiredFields; + + /** + * The list of fields, you need at least one of for a config. + */ + List requiredOneOfFields; + + /** + * Gets the list of required fields for a config. + * @return a list of the required fields + */ + public List getRequiredFields() { + return requiredFields; + } + + /** + * Sets the list of required fields for a config. + * @param requiredFields a list of the required fields + */ + public void setRequiredFields(List requiredFields) { + this.requiredFields = requiredFields; + } + + /** + * Gets the list of fields you need at least one of for a config. + * @return a list of the required one of fields + */ + public List getRequiredOneOfFields() { + return this.requiredOneOfFields; + } + + /** + * Sets the list of fields you need at least one of for a config. + * @param requiredFields a list of the required one of fields + */ + public void setRequiredOneOfFields(List requiredFields) { + requiredOneOfFields = requiredFields; + } +} diff --git a/openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/exceptions/FrameworkConfigurationException.java b/openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/exceptions/MaqsConfigException.java similarity index 70% rename from openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/exceptions/FrameworkConfigurationException.java rename to openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/exceptions/MaqsConfigException.java index 88597b352..6132495ab 100644 --- a/openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/exceptions/FrameworkConfigurationException.java +++ b/openmaqs-utilities/src/main/java/io/github/openmaqs/utilities/helper/exceptions/MaqsConfigException.java @@ -7,14 +7,14 @@ /** * The Framework Configuration Exception class. */ -public class FrameworkConfigurationException extends RuntimeException { +public class MaqsConfigException extends RuntimeException { /** * Instantiates a new Framework configuration exception. * * @param message the message */ - public FrameworkConfigurationException(String message) { + public MaqsConfigException(String message) { super(message); } } diff --git a/openmaqs-utilities/src/test/java/io/github/openmaqs/utilities/helper/ConfigUnitTest.java b/openmaqs-utilities/src/test/java/io/github/openmaqs/utilities/helper/ConfigUnitTest.java index 957159b45..f941e0d02 100644 --- a/openmaqs-utilities/src/test/java/io/github/openmaqs/utilities/helper/ConfigUnitTest.java +++ b/openmaqs-utilities/src/test/java/io/github/openmaqs/utilities/helper/ConfigUnitTest.java @@ -4,10 +4,17 @@ package io.github.openmaqs.utilities.helper; +import java.io.FileNotFoundException; +import java.nio.file.Paths; +import java.util.Arrays; import java.util.HashMap; +import java.util.List; import java.util.Map; +import org.apache.commons.configuration2.ex.ConfigurationException; import org.testng.Assert; +import org.testng.annotations.Ignore; import org.testng.annotations.Test; +import org.testng.asserts.SoftAssert; /** * The Configuration unit test class. @@ -122,4 +129,75 @@ public void doesKeyExistTest() { Assert.assertTrue(Config.doesKeyExist("HubAddress", ConfigSection.SELENIUM_MAQS)); Assert.assertFalse(Config.doesKeyExist("HubAddress", ConfigSection.GLOBAL_MAQS)); } -} + + @Test(groups = TestCategories.UTILITIES) + public void checkForAllFiles() { + List fileTypes = Arrays.asList(".json", ".properties", ".xml", ".yml"); + SoftAssert softAssert = new SoftAssert(); + + for (String types : fileTypes) { + String fileName; + + if (types.contains("json")) { + fileName = "appsettings"; + } else { + fileName = "config"; + } + + String file = PropertyManager.get("maqs.config.location", fileName + types); + softAssert.assertTrue(Paths.get(file).toFile().exists()); + } + softAssert.assertAll(); + } + + @Test(groups = TestCategories.UTILITIES) + public void useJsonConfigFile() throws ConfigurationException, FileNotFoundException { + Config.getConfigFile("appsettings.json"); + Map testSection = Config.getSection(ConfigSection.SELENIUM_MAQS); + Assert.assertEquals(testSection.get("TestKey"), "testValueTwo"); + Assert.assertEquals(testSection.get("Browser"), "Internet Explorer"); + } + + @Test(groups = TestCategories.UTILITIES) + public void usePropertiesConfigFile() throws ConfigurationException, FileNotFoundException { + Config.getConfigFile("config.properties"); + Map testSection = Config.getSection(ConfigSection.SELENIUM_MAQS); + Assert.assertEquals(testSection.get("TestKey"), "testValueTwo"); + Assert.assertEquals(testSection.get("Browser"), "Internet Explorer"); + Assert.assertEquals(testSection.get("HubAddress"), "132.15.12"); + } + + @Test(groups = TestCategories.UTILITIES) + public void useXmlConfigFile() throws ConfigurationException, FileNotFoundException { + Config.getConfigFile("config.xml"); + Map testSection = Config.getSection(ConfigSection.SELENIUM_MAQS); + Assert.assertEquals(testSection.get("TestKey"), "testValueTwo"); + Assert.assertEquals(testSection.get("Browser"), "Internet Explorer"); + } + + @Test(groups = TestCategories.UTILITIES) + public void useYmlConfigFile() throws ConfigurationException, FileNotFoundException { + Config.getConfigFile("config.yml"); + Map testSection = Config.getSection(ConfigSection.SELENIUM_MAQS); + Assert.assertEquals(testSection.get("TestKey"), "testValueTwo"); + Assert.assertEquals(testSection.get("Browser"), "Internet Explorer"); + } + + // TODO: this test simplifies the four tests above. Replace when those tests pass + @Ignore + @Test(groups = TestCategories.UTILITIES) + public void useConfigFiles() throws ConfigurationException, FileNotFoundException { + List files = Arrays.asList("config.xml", "appsettings.json", "config.properties", "config.yml"); + SoftAssert softAssert = new SoftAssert(); + + for (String file : files) { + Config.getConfigFile(file); + Map testSection = Config.getSection(ConfigSection.SELENIUM_MAQS); + softAssert.assertEquals(testSection.get("TestKey"), "testValueTwo"); + softAssert.assertEquals(testSection.get("Browser"), "Internet Explorer"); + softAssert.assertEquals(testSection.get("HubAddress"), "132.15.12"); + } + + softAssert.assertAll(); + } +} \ No newline at end of file diff --git a/openmaqs-webservices/src/main/java/io/github/openmaqs/webservices/WebServiceConfig.java b/openmaqs-webservices/src/main/java/io/github/openmaqs/webservices/WebServiceConfig.java index b76f4e27a..555554f2f 100644 --- a/openmaqs-webservices/src/main/java/io/github/openmaqs/webservices/WebServiceConfig.java +++ b/openmaqs-webservices/src/main/java/io/github/openmaqs/webservices/WebServiceConfig.java @@ -6,6 +6,8 @@ import io.github.openmaqs.utilities.helper.Config; import io.github.openmaqs.utilities.helper.ConfigSection; +import io.github.openmaqs.utilities.helper.ConfigValidation; +import java.util.Arrays; /** * The Web Service Configuration class. @@ -15,6 +17,19 @@ public final class WebServiceConfig { private WebServiceConfig() { } + static { + checkConfig(); + } + + /** + * Ensure required fields are in the config. + */ + private static void checkConfig() { + var validator = new ConfigValidation(); + validator.setRequiredOneOfFields(Arrays.asList("WebServiceTimeout", "WebServiceUri")); + Config.validate(ConfigSection.WEB_SERVICE_MAQS, validator); + } + /** * The web service configuration section. */ diff --git a/openmaqs-webservices/src/test/java/io/github/openmaqs/webservices/WebServiceUtilitiesUnitTest.java b/openmaqs-webservices/src/test/java/io/github/openmaqs/webservices/WebServiceUtilitiesUnitTest.java index 36711dbfb..d6d46dc35 100644 --- a/openmaqs-webservices/src/test/java/io/github/openmaqs/webservices/WebServiceUtilitiesUnitTest.java +++ b/openmaqs-webservices/src/test/java/io/github/openmaqs/webservices/WebServiceUtilitiesUnitTest.java @@ -9,7 +9,6 @@ import io.github.openmaqs.utilities.helper.TestCategories; import java.math.BigDecimal; import java.net.http.HttpResponse; - import org.testng.Assert; import org.testng.annotations.Test;