001package com.thed.zblast.parser;
002/**
003*
004* @author Mohan.Kumar
005* This parses eggPlant results
006*/
007import java.io.File;
008import java.io.FileNotFoundException;
009import java.io.FileReader;
010import java.io.IOException;
011import java.util.*;
012import java.util.logging.Level;
013import java.util.logging.Logger;
014import java.util.stream.Collectors;
015
016import com.thed.zblast.parser.model.EggPlantResult;
017import com.thed.zblast.parser.model.EggPlantScriptLine;
018
019import au.com.bytecode.opencsv.CSVReader;
020
021public final class EggplantParser {
022    private final String sut;
023    private final String url;
024
025    public EggplantParser(String sut, String url)
026   {
027        this.sut = sut;
028        this.url = url;
029    }
030
031    public ArrayList<EggPlantResult> invoke(File f)
032        {
033                List<File> suitefileList = new ArrayList<File>();
034                if (f.isDirectory()) {
035                        suitefileList = (Arrays.stream(f.listFiles()).collect(Collectors.toList())).stream()
036                                        .filter(file -> file.getName().endsWith(".csv")).collect(Collectors.toList());
037                } else {
038                        suitefileList.add(f);
039                }
040
041                Set<File> testSuiteFol = getTestSuits(suitefileList);
042
043                ArrayList<EggPlantResult> results = new ArrayList<EggPlantResult>();
044                testSuiteFol.stream().filter(file -> !file.getAbsolutePath().equals(f.getAbsolutePath())).forEach(file -> {
045                        FileReader fr = null;
046                        CSVReader reader = null;
047                        try {
048                                fr = new FileReader(file);
049                                reader = new CSVReader(fr);
050                                // skip the header row
051                                String[] line = reader.readNext();
052                                // Loop round reading each line
053                                while ((line = reader.readNext()) != null) {
054                                        EggPlantResult result = new EggPlantResult();
055                                        result.setRunDate(line[0]);
056                                        result.setDuration(line[2]);
057                                        result.setPassed(line[1].equals("Success"));
058                                        result.setErrors(line[3]);
059                                        result.setWarnings(line[4]);
060                                        result.setExceptions(line[5]);
061                                        result.setScript(f.getParent());
062                                        result.setSut(sut);
063                                        result.setXmlResultFile(file.getParent() + "/" + line[6].replaceAll(".txt", ".xml"));
064                                        getResultLines(result, file.getParent() + "/" + line[6], url);
065
066                                        results.add(result);
067                                }
068                        } catch (FileNotFoundException ex) {
069                                Logger.getLogger(EggplantParser.class.getName()).log(Level.SEVERE, null, ex);
070                        } catch (IOException ex) {
071                                Logger.getLogger(EggplantParser.class.getName()).log(Level.SEVERE, null, ex);
072                        } catch (Exception ex) {
073                                Logger.getLogger(EggplantParser.class.getName()).log(Level.SEVERE, null, ex);
074                        } finally {
075                                if (reader != null) {
076                                        try {
077                                                reader.close();
078                                        } catch (IOException ex) {
079                                                Logger.getLogger(EggplantParser.class.getName()).log(Level.SEVERE, null, ex);
080                                        }
081                                }
082                                if (fr != null) {
083                                        try {
084                                                fr.close();
085                                        } catch (IOException ex) {
086                                                Logger.getLogger(EggplantParser.class.getName()).log(Level.SEVERE, null, ex);
087                                        }
088                                }
089                        }
090                });
091                return results;
092        }
093    private Set<File> getTestSuits(List<File> suitefileList) {
094        List<File> suitList=new ArrayList<File>();
095        Set<File> csvFiles=new HashSet<File>();
096        
097        suitefileList.forEach((file)->{
098                FileReader fr;
099                CSVReader reader = null;
100                        try {
101                                fr = new FileReader(file);
102                                 reader = new CSVReader(fr);
103                                String[] line = reader.readNext();
104                                while ((line = reader.readNext()) != null) {
105                                        String suiteName=line[0];
106                                        File suiteFile=new File( file.getParent() + "/" + suiteName);
107                                        suitList.add(suiteFile);
108                                }
109                        } catch (IOException e) {
110                                e.printStackTrace();
111                        }finally {
112                                if(reader!=null) {
113                                        try {
114                                                reader.close();
115                                        } catch (IOException e) {
116                                                e.printStackTrace();
117                                        }
118                                }
119                        }
120        });
121        
122        suitList.forEach(f->{
123                File file=(Arrays.stream(f.listFiles()).collect(Collectors.toList())).stream().filter(csvFile->csvFile.getName().endsWith(".csv")).findFirst().get();
124                csvFiles.add(file);
125        });
126        
127                return csvFiles;
128        }
129
130        /**
131     * Reads the individual result lines from an eggPlant results file
132     *
133     * @param result    parent of the result line
134     * @param file      file containing the result lines
135    * @param url       url to where images will be stored TODO: Not working
136         * @throws Exception 
137     */
138    private void getResultLines(EggPlantResult result, String file, String url) throws Exception
139    {
140        FileReader fr = null;
141        CSVReader reader = null;
142        File inputFile=new File(file);
143        if(inputFile.exists()) {
144                try {
145                        // These files are tab separated
146                        fr = new FileReader(inputFile);
147                        reader = new CSVReader(fr, '\t');
148                        // skip the header row
149                        String[] line = reader.readNext(); // throw away the header line
150                        
151                        int step = 1;
152                        // Loop round reading each line
153                        while ((line = reader.readNext()) != null) {
154                                EggPlantScriptLine resultLine = new EggPlantScriptLine();
155                                resultLine.setStep(step++);
156                                resultLine.setTime(line[0]);
157                                resultLine.setMessage(line[1]);
158                                resultLine.setImage(line[2]);
159                                resultLine.setText(line[3]);
160                                resultLine.setImageURL(url + "/" + line[2]);  // TODO : How to get the image!!!!
161                                
162                                result.addScriptLine(resultLine);
163                        }
164                        
165                } catch (Exception e) {
166                        Logger.getLogger(EggplantParser.class.getName()).log(Level.SEVERE, null, e.getMessage());
167                }
168                finally{
169                        if (reader != null)
170                        {
171                                try {
172                                        reader.close();
173                                } catch (IOException ex) {
174                                        Logger.getLogger(EggplantParser.class.getName()).log(Level.SEVERE, null, ex);
175                                }
176                        }
177                        if (fr != null)
178                        {
179                                try {
180                                        fr.close();
181                                } catch (IOException ex) {
182                                        Logger.getLogger(EggplantParser.class.getName()).log(Level.SEVERE, null, ex);
183                                }
184                        }
185                }
186        }
187        }
188}