<commit_before>package com.hp.octane.plugins.jetbrains.teamcity.tests.services;

import com.hp.octane.plugins.jetbrains.teamcity.tests.model.TestResult;
import jetbrains.buildServer.serverSide.ServerExtension;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lev on 06/01/2016.
 */
public class BuildTestsService implements ServerExtension {

    public static boolean handleTestResult(String surefirePath, File destPath, long buildStartingTime){
        File checkoutFolder = new File(surefirePath);
        if(checkoutFolder.exists()){
            List<File> fileList = new ArrayList<File>();
            searchForSureFireFiles(checkoutFolder, fileList);
            if(fileList.isEmpty()){
                return false;
            }
            for(File surefireFile:fileList){
                createTestListFromFile(surefireFile, buildStartingTime);
            }
        }else{
            return false;
        }
        return true;
    }

    private static List<TestResult> createTestListFromFile(File surefireFile, long startingTime){
        List<TestResult> testList = new ArrayList<TestResult>();

        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(surefireFile);
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("testcase");

            for (int i = 0; i < nList.getLength(); i++) {
                String moduleName = "";
                String packageName = "";
                String className = "";
                String testName = "";
                long duration = 0;
                //TestResultStatus status;

                Node nNode = nList.item(i);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;
                    duration = Long.valueOf(eElement.getAttribute("time"));
                    moduleName = surefireFile.getParentFile().getName();
                    //new TestResult(moduleName, packageName, className, testName, status, duration, buildStarted);
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return testList;
    }


    private static void searchForSureFireFiles(File root, List<File> surefireOnly) {
        if(root == null || surefireOnly == null) return;
        if(root.isDirectory()) {
            for(File file : root.listFiles()) {
                searchForSureFireFiles(file, surefireOnly);
            }
        } else if(root.isFile() && root.getPath().contains("surefire-reports") && root.getPath().endsWith(".xml")) {
            surefireOnly.add(root);
        }
    }<commit_msg>US 9001 - as a user I get test results coming from TC pipeline - JUNIT<commit_after>package com.hp.octane.plugins.jetbrains.teamcity.tests.services;

import com.hp.octane.plugins.jetbrains.teamcity.tests.model.TestResult;
import com.hp.octane.plugins.jetbrains.teamcity.tests.model.TestResultStatus;
import jetbrains.buildServer.serverSide.ServerExtension;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by lev on 06/01/2016.
 */
public class BuildTestsService implements ServerExtension {

    public static boolean handleTestResult(String surefirePath, File destPath, long buildStartingTime){
        File checkoutFolder = new File(surefirePath);
        if(checkoutFolder.exists()){
            List<File> fileList = new ArrayList<File>();
            searchForSureFireFiles(checkoutFolder, fileList);
            if(fileList.isEmpty()){
                return false;
            }
            for(File surefireFile:fileList){
                createTestListFromFile(surefireFile, buildStartingTime);
            }
        }else{
            return false;
        }
        return true;
    }

    private static List<TestResult> createTestListFromFile(File surefireFile, long startingTime){
        List<TestResult> testList = new ArrayList<TestResult>();

        try {
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(surefireFile);
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("testcase");

            for (int i = 0; i < nList.getLength(); i++) {
                String moduleName = "";
                String packageName = "";
                String className = "";
                String testName = "";
                long duration = 0;
                TestResultStatus status;

                Node nNode = nList.item(i);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element eElement = (Element) nNode;
                    duration = Long.valueOf(eElement.getAttribute("time"));
                    moduleName = surefireFile.getParentFile().getName();
                    packageName = eElement.getAttribute("classname");
                    int p = packageName.lastIndexOf(".");
                    className = packageName.substring(p + 1);
                    testName = eElement.getAttribute("name");
                    eElement.getElementsByTagName("error").item(0);
                    testList.add(new TestResult(moduleName, packageName, className, testName, null, duration, startingTime));
                }
            }
        }catch (Exception e) {
            e.printStackTrace();
        }
        return testList;
    }


    private static void searchForSureFireFiles(File root, List<File> surefireOnly) {
        if(root == null || surefireOnly == null) return;
        if(root.isDirectory()) {
            for(File file : root.listFiles()) {
                searchForSureFireFiles(file, surefireOnly);
            }
        } else if(root.isFile() && root.getPath().contains("surefire-reports") && root.getPath().endsWith(".xml")) {
            surefireOnly.add(root);
        }
    }
{'fraction_non_alphanumeric': 0.10548007937719432, 'fraction_numerical': 0.0051900473210196914, 'mean_word_length': 2.262948207171315, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, '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': '3026914', 'n_tokens_mistral': 1766, 'n_tokens_neox': 1695, 'n_words': 350}