<commit_before>/*
 * Copyright 2019 Coveros, Inc.
 *
 * This file is part of Selenified.
 *
 * Selenified is licensed under the Apache License, Version
 * 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.coveros.selenified.utilities;

import com.coveros.selenified.Browser;
import com.coveros.selenified.exceptions.InvalidBrowserOptionsException;
import com.coveros.selenified.exceptions.InvalidHTTPException;
import com.coveros.selenified.exceptions.InvalidHubException;
import com.coveros.selenified.exceptions.InvalidProxyException;
import org.testng.ITestContext;
import org.testng.log4testng.Logger;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

/**
 * Reads in properties files provided by the user in order to execute tests. These
 * files can be passed in via commandline, or set in a selenified.properties file.
 * This file should reside in src/test/resources. If the property exists in the
 * system properties, that is returned, overridding anything in the
 * selenified.properties file.
 *
 * @author <NAME>
 * @version 3.2.0
 * @lastupdate 3/22/2019
 */
public class Property {

    private Property() {
    }

    private static final Logger log = Logger.getLogger(Property.class);
    private static final String SELENIFIED = "src/test/resources/selenified.properties";

    private static final String GENERATE_PDF = "generatePDF";
    private static final String PACKAGE_RESULTS = "packageResults";
    private static final String HUB = "hub";
    private static final String PROXY = "proxy";
    public static final String APP_URL = "appURL";
    public static final String BROWSER = "browser";
    public static final String HEADLESS = "headless";
    public static final String OPTIONS = "options";

    /**
     * Retrieves the specified program property. if it exists from the system properties, that is returned, overridding
     * all other values. Otherwise, if it exists from the properties file, that is returned, otherwise, null is returned
     *
     * @param property - what property value to return
     * @return String: the property value, null if unset
     */
    private static String getProgramProperty(String property) {
        if (System.getProperty(property) != null) {
            return System.getProperty(property).trim();
        }
        Properties prop = new Properties();
        try (InputStream input = new FileInputStream(SELENIFIED)) {
            prop.load(input);
        } catch (IOException e) {
            log.info(e);
        }
        String fullProperty = prop.getProperty(property);
        if (fullProperty != null) {
            fullProperty = fullProperty.trim();
        }
        return fullProperty;
    }

    /**
     * Determines if we are supposed to generate a pdf of the results or not
     *
     * @return boolean: generate a pdf or not
     */
    public static boolean generatePDF() {
        String generatePDF = getProgramProperty(GENERATE_PDF);
        if (generatePDF == null) {
            return false;
        }
        if ("".equals(generatePDF)) {
            return true;
        }
        return "true".equalsIgnoreCase(generatePDF);
    }

    /**
     * Determines if we are supposed to zip up the results or not
     *
     * @return boolean: zip up the results or not
     */
    public static boolean packageResults() {
        String packageResults = getProgramProperty(PACKAGE_RESULTS);
        if (packageResults == null) {
            return false;
        }
        if ("".equals(packageResults)) {
            return true;
        }
        return "true".equalsIgnoreCase(packageResults);
    }

    /**
     * Determines if a hub property is set. This could be to sauce, grid, or any other cloud tool.
     * This should be provided with the protocol and address, but leaving out the /wd/hub
     *
     * @return boolean: is a hub location set
     */
    public static boolean isHubSet() {
        String hub = getProgramProperty(HUB);
        return hub != null && !"".equals(hub);
    }

    /**
     * Retrieves the hub property if it is set. This could be to sauce, grid, or any other cloud tool.
     * This should be provided with the protocol and address, but leaving out the /wd/hub
     *
     * @return String: the set hub address, null if none are set
     */
    public static String getHub() throws InvalidHubException {
        if (!isHubSet()) {
            throw new InvalidHubException("Hub isn't set");
        }
        return getProgramProperty(HUB);
    }

    /**
     * Determines if a proxy property is set. This could be to something local, or in the cloud.
     * Provide the protocol, address, and port
     *
     * @return boolean: is a proxy set
     */
    public static boolean isProxySet() {
        String proxy = getProgramProperty(PROXY);
        return proxy != null && !"".equals(proxy);
    }

    /**
     * Retrieves the proxy property if it is set. This could be to something local, or in the cloud.
     * Provide the protocol, address, and port
     *
     * @return String: the set proxy address, null if none are set
     */
    public static String getProxy() throws InvalidProxyException {
        if (!isProxySet()) {
            throw new InvalidProxyException("Proxy isn't set");
        }
        String proxy = getProgramProperty(PROXY);
        String[] proxyParts = proxy.split(":");
        if (proxyParts.length != 2) {
            throw new InvalidProxyException("Proxy '" + proxy + "' isn't valid. Must contain address and port, without protocol");
        }
        try {
            Integer.parseInt(proxyParts[1]);
        } catch (NumberFormatException e) {
            throw new InvalidProxyException("Proxy '" + proxy + "' isn't valid. Must contain address and port, without protocol. Invalid port provided. " + e);
        }
        return getProgramProperty(PROXY);
    }

    public static String getProxyHost() throws InvalidProxyException {
        if (!isProxySet()) {
            throw new InvalidProxyException("Proxy isn't set");
        }
        String proxy = getProxy();
        return proxy.split(":")[0];
    }

    public static int getProxyPort() throws InvalidProxyException {
        if (!isProxySet()) {
            throw new InvalidProxyException("Proxy isn't set");
        }
        String proxy = getProxy();
        return Integer.parseInt(proxy.split(":")[1]);
    }


    /**
     * Obtains the application under test, as a URL. If the site was provided as<commit_msg>Cleaning up<commit_after>/*
 * Copyright 2019 Coveros, Inc.
 *
 * This file is part of Selenified.
 *
 * Selenified is licensed under the Apache License, Version
 * 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy
 * of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

package com.coveros.selenified.utilities;

import com.coveros.selenified.Browser;
import com.coveros.selenified.exceptions.InvalidBrowserOptionsException;
import com.coveros.selenified.exceptions.InvalidHTTPException;
import com.coveros.selenified.exceptions.InvalidHubException;
import com.coveros.selenified.exceptions.InvalidProxyException;
import org.testng.ITestContext;
import org.testng.log4testng.Logger;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Properties;

/**
 * Reads in properties files provided by the user in order to execute tests. These
 * files can be passed in via commandline, or set in a selenified.properties file.
 * This file should reside in src/test/resources. If the property exists in the
 * system properties, that is returned, overridding anything in the
 * selenified.properties file.
 *
 * @author <NAME>
 * @version 3.2.0
 * @lastupdate 3/22/2019
 */
public class Property {

    private static final String PROXY_ISNT_SET = "Proxy isn't set";

    private Property() {
    }

    private static final Logger log = Logger.getLogger(Property.class);
    private static final String SELENIFIED = "src/test/resources/selenified.properties";

    private static final String GENERATE_PDF = "generatePDF";
    private static final String PACKAGE_RESULTS = "packageResults";
    private static final String HUB = "hub";
    private static final String PROXY = "proxy";
    public static final String APP_URL = "appURL";
    public static final String BROWSER = "browser";
    public static final String HEADLESS = "headless";
    public static final String OPTIONS = "options";

    /**
     * Retrieves the specified program property. if it exists from the system properties, that is returned, overridding
     * all other values. Otherwise, if it exists from the properties file, that is returned, otherwise, null is returned
     *
     * @param property - what property value to return
     * @return String: the property value, null if unset
     */
    private static String getProgramProperty(String property) {
        if (System.getProperty(property) != null) {
            return System.getProperty(property).trim();
        }
        Properties prop = new Properties();
        try (InputStream input = new FileInputStream(SELENIFIED)) {
            prop.load(input);
        } catch (IOException e) {
            log.info(e);
        }
        String fullProperty = prop.getProperty(property);
        if (fullProperty != null) {
            fullProperty = fullProperty.trim();
        }
        return fullProperty;
    }

    /**
     * Determines if we are supposed to generate a pdf of the results or not
     *
     * @return boolean: generate a pdf or not
     */
    public static boolean generatePDF() {
        String generatePDF = getProgramProperty(GENERATE_PDF);
        if (generatePDF == null) {
            return false;
        }
        if ("".equals(generatePDF)) {
            return true;
        }
        return "true".equalsIgnoreCase(generatePDF);
    }

    /**
     * Determines if we are supposed to zip up the results or not
     *
     * @return boolean: zip up the results or not
     */
    public static boolean packageResults() {
        String packageResults = getProgramProperty(PACKAGE_RESULTS);
        if (packageResults == null) {
            return false;
        }
        if ("".equals(packageResults)) {
            return true;
        }
        return "true".equalsIgnoreCase(packageResults);
    }

    /**
     * Determines if a hub property is set. This could be to sauce, grid, or any other cloud tool.
     * This should be provided with the protocol and address, but leaving out the /wd/hub
     *
     * @return boolean: is a hub location set
     */
    public static boolean isHubSet() {
        String hub = getProgramProperty(HUB);
        return hub != null && !"".equals(hub);
    }

    /**
     * Retrieves the hub property if it is set. This could be to sauce, grid, or any other cloud tool.
     * This should be provided with the protocol and address, but leaving out the /wd/hub
     *
     * @return String: the set hub address, null if none are set
     */
    public static String getHub() throws InvalidHubException {
        if (!isHubSet()) {
            throw new InvalidHubException("Hub isn't set");
        }
        return getProgramProperty(HUB);
    }

    /**
     * Determines if a proxy property is set. This could be to something local, or in the cloud.
     * Provide the protocol, address, and port
     *
     * @return boolean: is a proxy set
     */
    public static boolean isProxySet() {
        String proxy = getProgramProperty(PROXY);
        return proxy != null && !"".equals(proxy);
    }

    /**
     * Retrieves the proxy property if it is set. This could be to something local, or in the cloud.
     * Provide the protocol, address, and port
     *
     * @return String: the set proxy address, null if none are set
     */
    public static String getProxy() throws InvalidProxyException {
        if (!isProxySet()) {
            throw new InvalidProxyException(PROXY_ISNT_SET);
        }
        String proxy = getProgramProperty(PROXY);
        String[] proxyParts = proxy.split(":");
        if (proxyParts.length != 2) {
            throw new InvalidProxyException("Proxy '" + proxy + "' isn't valid. Must contain address and port, without protocol");
        }
        try {
            Integer.parseInt(proxyParts[1]);
        } catch (NumberFormatException e) {
            throw new InvalidProxyException("Proxy '" + proxy + "' isn't valid. Must contain address and port, without protocol. Invalid port provided. " + e);
        }
        return getProgramProperty(PROXY);
    }

    public static String getProxyHost() throws InvalidProxyException {
        if (!isProxySet()) {
            throw new InvalidProxyException(PROXY_ISNT_SET);
        }
        String proxy = getProxy();
        return proxy.split(":")[0];
    }

    public static int getProxyPort() throws InvalidProxyException {
        if (!isProxySet()) {
            throw new InvalidProxyException(PROXY_ISNT_SET);
        }
        String proxy = getProxy();
        return Integer.parseInt(proxy.split(":")[1]);
    }


    /**
     * Obtains the application under test, as a URL. If the site was provided as
{'fraction_non_alphanumeric': 0.08910472972972973, 'fraction_numerical': 0.0032376126126126125, 'mean_word_length': 2.7002604166666666, 'pattern_counts': {'":': 6, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3107315', 'n_tokens_mistral': 3749, 'n_tokens_neox': 3673, 'n_words': 1388}