package testingbot;

import hudson.tasks.junit.CaseResult;
import hudson.tasks.junit.TestAction;
import hudson.tasks.junit.TestObject;
import hudson.tasks.junit.TestResult;
import hudson.tasks.junit.TestResultAction.Data;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author testingbot.com
 */
public class TestingBotReportFactory extends Data {
    
    public static final TestingBotReportFactory INSTANCE = new TestingBotReportFactory();
    
    public Object readResolve() {
        return INSTANCE;
    }
    
    public static List<String> findSessionIDs(CaseResult testResult) {
        List<String> sessions = new ArrayList<String>();
        Pattern p = Pattern.compile("TestingBotSessionID=([0-9a-zA-Z]+)");
        Matcher matchOut = p.matcher(testResult.getStdout());
        Matcher matchError = p.matcher(testResult.getStderr());
        
        while (matchOut.find()) {
            String sessionId = matchOut.group(1);
            sessions.add(sessionId);
        }
        
        while (matchError.find()) {
            String sessionId = matchError.group(1);
            sessions.add(sessionId);
        }
        
        return sessions;
    }

    @Override
    public List<? extends TestAction> getTestAction(TestObject to) {
        if (to instanceof CaseResult) {
            CaseResult cr = (CaseResult) to;
            List<String> ids = findSessionIDs(cr);
            if (!ids.isEmpty()) {
                return Collections.singletonList(new TestingBotReport(cr,ids));
            }
        }
        return Collections.emptyList();
    }
    
}

@@ -823,16 +823,66 @@
 Z%5D+)%22);%0A
+        if (testResult.getStdout() != null) %7B%0A    
         
@@ -947,73 +947,573 @@
     
-Matcher matchError = p.matcher(testResult.getStderr());%0A        %0A
+    while (matchOut.find()) %7B%0A                String sessionId = matchOut.group(1);%0A                sessions.add(sessionId);%0A            %7D%0A        %7D%0A        %0A        if (testResult.getStderr() != null) %7B%0A            Matcher matchError = p.matcher(testResult.getStderr());%0A            while (matchError.find()) %7B%0A                String sessionId = matchError.group(1);%0A                sessions.add(sessionId);%0A            %7D%0A        %7D%0A        %0A        if (testResult.getClassName() != null) %7B%0A            Matcher matchName = p.matcher(testResult.getClassName());%0A    
     
@@ -1520,35 +1520,36 @@
     while (match
-Out
+Name
 .find()) %7B%0A     
@@ -1547,32 +1547,36 @@
 ) %7B%0A            
+  
+  
 String sessionId
@@ -1575,35 +1575,36 @@
 essionId = match
-Out
+Name
 .group(1);%0A     
@@ -1602,32 +1602,36 @@
 1);%0A            
+  
+  
 sessions.add(ses
@@ -1639,32 +1639,36 @@
 ionId);%0A        
+    
 %7D%0A        %0A     
@@ -1653,32 +1653,33 @@
       %7D%0A        
+%7D
 %0A        while (
@@ -1663,32 +1663,166 @@
       %7D%0A        
+%0A        if (testResult.getFullName() != null) %7B%0A            Matcher matchFullName = p.matcher(testResult.getFullName());%0A            
 while (matchErro
@@ -1809,37 +1809,40 @@
     while (match
-Error
+FullName
 .find()) %7B%0A     
@@ -1828,32 +1828,36 @@
 lName.find()) %7B%0A
+    
             Stri
@@ -1868,37 +1868,40 @@
 essionId = match
-Error
+FullName
 .group(1);%0A     
@@ -1887,32 +1887,36 @@
 lName.group(1);%0A
+    
             sess
@@ -1928,32 +1928,46 @@
 add(sessionId);%0A
+            %7D%0A
         %7D%0A      

check for null values, check name and classname of test report for TestingBotSessionID regex