001/**
002 * @author Mohan.Kumar
003 */
004
005package com.thed.zblast.parser;
006
007import com.thed.zblast.parser.model.TestngResults;
008import com.thed.zblast.parser.model.TestngResults.Testsuite;
009import com.thed.zblast.parser.model.TestngResults.Testsuite.Test;
010import com.thed.zblast.parser.model.TestngResults.Testsuite.Test.Class;
011import com.thed.zblast.parser.model.TestngResults.Testsuite.Test.Class.TestMethod;
012
013import javax.xml.bind.JAXBContext;
014import javax.xml.bind.JAXBException;
015import javax.xml.bind.UnmarshalException;
016import javax.xml.bind.Unmarshaller;
017import java.io.File;
018import java.io.FilenameFilter;
019import java.util.ArrayList;
020import java.util.HashMap;
021import java.util.List;
022import java.util.Map;
023import java.util.stream.Collectors;
024
025public class TestNGXMLResultParserImpl {
026        private static final String AFTER_METHOD = "afterMethod";
027        private static final String BEFORE_METHOD = "beforeMethod";
028        private static final String AFTER_CLASS = "afterClass";
029        private static final String BEFORE_CLASS = "beforeClass";
030        private static final String AFTER_SUITE = "afterSuite";
031        private static final String BEFORE_SUITE = "beforeSuite";
032
033        public static void main(String[] args) {
034
035                List<Testsuite> testSuites = parseResults("D:\\ZAutomation\\logs");
036
037                System.out.println("Total test suites : " + testSuites.size());
038
039                for (Testsuite testsuite : testSuites) {
040                        getTestsFromTestSuite(testsuite);
041                }
042
043        }
044
045        public static List<Testsuite> parseResults(String fileName) {
046                File file = new File(fileName);
047                List<Testsuite> testSuites = new ArrayList<>();
048                if (file.isDirectory()) {
049                        FilenameFilter nameFilter = new FilenameFilter() {
050                                @Override
051                                public boolean accept(File dir, String name) {
052                                        if (name.endsWith(".xml") && !new File(name).isDirectory())
053                                                return true;
054                                        return false;
055                                }
056                        };
057                        String[] list = file.list(nameFilter);
058
059                        String absolutePath = file.getAbsolutePath();
060
061                        for (int i = 0; i < list.length; i++) {
062                                testSuites.addAll(getTestSuites(new File(absolutePath + File.separator + list[i])));
063                        }
064
065                } else if (file.getName().endsWith(".xml")) {
066                        return getTestSuites(file);
067                }
068
069                return testSuites;
070        }
071
072        private static List<Testsuite> getTestSuites(File resultsfile) {
073
074                List<Testsuite> testsuiteList = new ArrayList<Testsuite>();
075
076                try {
077
078                        JAXBContext jaxbContext = JAXBContext.newInstance(TestngResults.class);
079
080                        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
081                        Object unmarshal = jaxbUnmarshaller.unmarshal(resultsfile);
082
083                        if (unmarshal instanceof TestngResults robot) {
084                                Testsuite rootSuite = robot.getSuite();
085                                testsuiteList.add(rootSuite);
086
087                        } else {
088                                System.out.println("XML is not a valid report file");
089                        }
090                } catch (UnmarshalException e) {
091                        System.out.println(e.getMessage());
092                } catch (JAXBException e) {
093                        e.printStackTrace();
094                } catch (Exception e) {
095                        e.printStackTrace();
096                }
097
098                return testsuiteList;
099        }
100
101        private static void getTestsFromTestSuite(Testsuite testsuite) {
102                List<Test> test = testsuite.getTest();
103                List<Class> testClass = new ArrayList<>();
104                test.forEach(t->{
105                        testClass.addAll(t.getClazz());
106                });
107                List<TestMethod> testMethods=new ArrayList<>();
108                List<List<TestMethod>> testMethodsList = testClass.stream().map(s -> s.getTestMethod()).collect(Collectors.toList());
109                testMethodsList.forEach(su-> {testMethods.addAll(su);});
110
111                for (TestMethod testcase : testMethods) {
112                        String name = testcase.getName();
113
114                        if (name.equals(BEFORE_CLASS) || name.equals(AFTER_CLASS) || name.equals(BEFORE_SUITE)
115                                        || name.equals(AFTER_SUITE) || name.equals(BEFORE_METHOD) || name.equals(AFTER_METHOD)) {
116                                continue;
117                        }
118
119                        String status = testcase.getStatus().toString();
120                        System.out.println(name + " " + status);
121
122                }
123        }
124
125        public static Map<String, Boolean> getTestsFromTestSuiteList(List<Testsuite> testSuites) {
126                Map<String, Boolean> testsMap = new HashMap<String, Boolean>();
127                for (Testsuite testsuite : testSuites) {
128                        List<Test> test = testsuite.getTest();
129                        List<Class> testClass =new ArrayList<>();
130                        test.stream().forEach(t->{
131                                testClass.addAll(t.getClazz());
132                        });
133                        List<TestMethod> testMethods=new ArrayList<>();
134                        List<List<TestMethod>> testMethodsList = testClass.stream().map(s -> s.getTestMethod()).collect(Collectors.toList());
135                        testMethodsList.forEach(su-> {testMethods.addAll(su);});
136
137                        for (TestMethod testcase : testMethods) {
138                                boolean value = true;
139                                String name = testcase.getName();
140                                if (name.equals(BEFORE_CLASS) || name.equals(AFTER_CLASS) || name.equals(BEFORE_SUITE)
141                                                || name.equals(AFTER_SUITE) || name.equals(BEFORE_METHOD) || name.equals(AFTER_METHOD)) {
142                                        continue;
143                                }
144
145                                String status = testcase.getStatus().toString();
146                                if (status.equalsIgnoreCase("fail")) {
147                                        value = false;
148                                }
149
150                                testsMap.put(name, value);
151                        }
152                }
153                return testsMap;
154        }
155
156}