<commit_before>/*-------------------------------------------------------------------------------------------------------------------*\
|  Copyright (C) 2014 eBay Software Foundation                                                                        |
|                                                                                                                     |
|  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.paypal.selion.reports.reporter.runtimereport;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.testng.ITestResult;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.paypal.selion.configuration.Config;
import com.paypal.selion.configuration.Config.ConfigProperty;
import com.paypal.selion.logger.SeLionLogger;
import com.paypal.selion.reports.reporter.html.ReporterException;
import com.paypal.selion.reports.reporter.services.ConfigSummaryData;
import com.paypal.selion.reports.reporter.services.ReporterConfigMetadata;
import com.paypal.selion.reports.reporter.services.ReporterDateFormatter;
import com.paypal.test.utilities.logging.SimpleLogger;

/**
 * JsonRuntimeReporterHelper will provide methods to create JSON file which contains information about list of test
 * methods and configuration methods executed with their corresponding status.
 * 
 */
public class JsonRuntimeReporterHelper {

    private static final int ONE_MINUTE = 60000;
    private static final String AFTER_METHOD = "AfterMethod";
    private static final String AFTER_CLASS = "AfterClass";
    private static final String AFTER_GROUP = "AfterGroup";
    private static final String AFTER_TEST = "AfterTest";
    private static final String AFTER_SUITE = "AfterSuite";
    private static final String BEFORE_METHOD = "BeforeMethod";
    private static final String BEFORE_CLASS = "BeforeClass";
    private static final String BEFORE_GROUP = "BeforeGroup";
    private static final String BEFORE_TEST = "BeforeTest";
    private static final String BEFORE_SUITE = "BeforeSuite";
    private List<TestMethodInfo> runningTest = new ArrayList<TestMethodInfo>();;
    private List<ConfigMethodInfo> runningConfig = new ArrayList<ConfigMethodInfo>();

    private File jsonCompletedTest;
    private File jsonCompletedConfig;
    private JsonArray testJsonLocalConfigSummary = new JsonArray();
    private JsonObject jsonConfigSummary;
    private long previousTime = 0;

    private static SimpleLogger logger = SeLionLogger.getLogger();

    public JsonRuntimeReporterHelper() {
        try {
            File workingDir = new File(Config.getConfigProperty(ConfigProperty.WORK_DIR));
            workingDir.mkdirs();
            jsonCompletedTest = File.createTempFile("selion-rrct", null, workingDir);
            jsonCompletedTest.deleteOnExit();
            jsonCompletedConfig = File.createTempFile("selion-rrcf", null, workingDir);
            jsonCompletedConfig.deleteOnExit();
        } catch (IOException e) {
            logger.log(Level.SEVERE, e.getMessage(), e);
            throw new ReporterException(e);
        }
    }

    /**
     * This method will generate Configuration summary by fetching the details from ReportDataGenerator
     */
    private JsonObject generateConfigSummary() throws JsonParseException {

        logger.entering();

        if (jsonConfigSummary == null) {
            jsonConfigSummary = new JsonObject();
            for (Entry<String, String> temp : ConfigSummaryData.getConfigSummary().entrySet()) {
                jsonConfigSummary.addProperty(temp.getKey(), temp.getValue());
            }
        }
        logger.exiting(jsonConfigSummary);

        return jsonConfigSummary;
    }
    
    /**
     * This method will generate local Configuration summary by fetching the details from ReportDataGenerator
     * 
     * @param suiteName
     *            - suite name of the test method.
     * @param testName
     *            - test name of the test method.
     */
    public void generateLocalConfigSummary(String suiteName, String testName) {

        logger.entering(new Object[] { suiteName, testName });

        try {
            Map<String, String> testLocalConfigValues = ConfigSummaryData.getLocalConfigSummary(testName);
            JsonObject json = new JsonObject();
            if (testLocalConfigValues == null) {
                json.addProperty(ReporterDateFormatter.CURRENTDATE,
                		ReporterDateFormatter.getISO8601String(new Date()));
            } else {
                for (Entry<String, String> temp : testLocalConfigValues.entrySet()) {
                    json.addProperty(temp.getKey(), temp.getValue());
                }
            }

            json.addProperty("suite", suiteName);
            json.addProperty("test", testName);
            this.testJsonLocalConfigSummary.add(json);
        } catch (JsonParseException e) {
            logger.log(Level.SEVERE, e.getMessage(), e);
            throw new ReporterException(e);
        }

        logger.exiting();
    }

    /**
     * This method is used to insert test method details based on the methods suite, test, groups and class name.
     * 
     * @param suite
     *            - suite name of the test method.
     * @param test
     *            - test name of the test method.
     * @param packages
     *            - group name of the test method. If the test method doesn't belong to any group then we should pass
     *            null.
     * @param classname
     *            - class name of the test method.
     * @param result
     *            - ITestResult instance of the test method.
     */

    public synchronized void insertTestMethod(String suite, String test, String packages, String classname,
            ITestResult result) {
        logger.entering(new Object[] { suite, test, packages, classname, result });

        TestMethodInfo test1 = new TestMethodInfo(suite, test, packages, classname, result);

        if (result.getStatus() == ITestResult.STARTED) {
            runningTest.add(test1);
            return;
        }

        if (result.getStatus() == ITestResult.SKIP) {
            appendFile(jsonCompletedTest, test1.toJson().concat(",\n"));
            return;
        }

        for (TestMethodInfo temp : runningTest) {
            if (temp.getResult().equals(result)) {
                runningTest.remove(temp);
                appendFile(jsonCompletedTest, test1.toJson().concat(",\n"));<commit_msg>Skipped tests need to be removed from running tests<commit_after>/*-------------------------------------------------------------------------------------------------------------------*\
|  Copyright (C) 2014 eBay Software Foundation                                                                        |
|                                                                                                                     |
|  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.paypal.selion.reports.reporter.runtimereport;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.logging.Level;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.json.JSONException;
import org.testng.ITestResult;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParseException;
import com.google.gson.JsonParser;
import com.paypal.selion.configuration.Config;
import com.paypal.selion.configuration.Config.ConfigProperty;
import com.paypal.selion.logger.SeLionLogger;
import com.paypal.selion.reports.reporter.html.ReporterException;
import com.paypal.selion.reports.reporter.services.ConfigSummaryData;
import com.paypal.selion.reports.reporter.services.ReporterConfigMetadata;
import com.paypal.selion.reports.reporter.services.ReporterDateFormatter;
import com.paypal.test.utilities.logging.SimpleLogger;

/**
 * JsonRuntimeReporterHelper will provide methods to create JSON file which contains information about list of test
 * methods and configuration methods executed with their corresponding status.
 * 
 */
public class JsonRuntimeReporterHelper {

    private static final int ONE_MINUTE = 60000;
    private static final String AFTER_METHOD = "AfterMethod";
    private static final String AFTER_CLASS = "AfterClass";
    private static final String AFTER_GROUP = "AfterGroup";
    private static final String AFTER_TEST = "AfterTest";
    private static final String AFTER_SUITE = "AfterSuite";
    private static final String BEFORE_METHOD = "BeforeMethod";
    private static final String BEFORE_CLASS = "BeforeClass";
    private static final String BEFORE_GROUP = "BeforeGroup";
    private static final String BEFORE_TEST = "BeforeTest";
    private static final String BEFORE_SUITE = "BeforeSuite";
    private List<TestMethodInfo> runningTest = new ArrayList<TestMethodInfo>();;
    private List<ConfigMethodInfo> runningConfig = new ArrayList<ConfigMethodInfo>();

    private File jsonCompletedTest;
    private File jsonCompletedConfig;
    private JsonArray testJsonLocalConfigSummary = new JsonArray();
    private JsonObject jsonConfigSummary;
    private long previousTime = 0;

    private static SimpleLogger logger = SeLionLogger.getLogger();

    public JsonRuntimeReporterHelper() {
        try {
            File workingDir = new File(Config.getConfigProperty(ConfigProperty.WORK_DIR));
            workingDir.mkdirs();
            jsonCompletedTest = File.createTempFile("selion-rrct", null, workingDir);
            jsonCompletedTest.deleteOnExit();
            jsonCompletedConfig = File.createTempFile("selion-rrcf", null, workingDir);
            jsonCompletedConfig.deleteOnExit();
        } catch (IOException e) {
            logger.log(Level.SEVERE, e.getMessage(), e);
            throw new ReporterException(e);
        }
    }

    /**
     * This method will generate Configuration summary by fetching the details from ReportDataGenerator
     */
    private JsonObject generateConfigSummary() throws JsonParseException {

        logger.entering();

        if (jsonConfigSummary == null) {
            jsonConfigSummary = new JsonObject();
            for (Entry<String, String> temp : ConfigSummaryData.getConfigSummary().entrySet()) {
                jsonConfigSummary.addProperty(temp.getKey(), temp.getValue());
            }
        }
        logger.exiting(jsonConfigSummary);

        return jsonConfigSummary;
    }
    
    /**
     * This method will generate local Configuration summary by fetching the details from ReportDataGenerator
     * 
     * @param suiteName
     *            - suite name of the test method.
     * @param testName
     *            - test name of the test method.
     */
    public void generateLocalConfigSummary(String suiteName, String testName) {

        logger.entering(new Object[] { suiteName, testName });

        try {
            Map<String, String> testLocalConfigValues = ConfigSummaryData.getLocalConfigSummary(testName);
            JsonObject json = new JsonObject();
            if (testLocalConfigValues == null) {
                json.addProperty(ReporterDateFormatter.CURRENTDATE,
                		ReporterDateFormatter.getISO8601String(new Date()));
            } else {
                for (Entry<String, String> temp : testLocalConfigValues.entrySet()) {
                    json.addProperty(temp.getKey(), temp.getValue());
                }
            }

            json.addProperty("suite", suiteName);
            json.addProperty("test", testName);
            this.testJsonLocalConfigSummary.add(json);
        } catch (JsonParseException e) {
            logger.log(Level.SEVERE, e.getMessage(), e);
            throw new ReporterException(e);
        }

        logger.exiting();
    }

    /**
     * This method is used to insert test method details based on the methods suite, test, groups and class name.
     * 
     * @param suite
     *            - suite name of the test method.
     * @param test
     *            - test name of the test method.
     * @param packages
     *            - group name of the test method. If the test method doesn't belong to any group then we should pass
     *            null.
     * @param classname
     *            - class name of the test method.
     * @param result
     *            - ITestResult instance of the test method.
     */

    public synchronized void insertTestMethod(String suite, String test, String packages, String classname,
            ITestResult result) {
        logger.entering(new Object[] { suite, test, packages, classname, result });

        TestMethodInfo test1 = new TestMethodInfo(suite, test, packages, classname, result);

        if (result.getStatus() == ITestResult.STARTED) {
            runningTest.add(test1);
            return;
        }

        for (TestMethodInfo temp : runningTest) {
            if (temp.getResult().equals(result)) {
                runningTest.remove(temp);
                appendFile(jsonCompletedTest, test1.toJson().concat(",\n"));
{'fraction_non_alphanumeric': 0.10647194279481274, 'fraction_numerical': 0.002726942188825597, 'mean_word_length': 1.8750871080139373, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 66, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6653055', 'n_tokens_mistral': 3826, 'n_tokens_neox': 3722, 'n_words': 1069}