package schuberg.philis.jenkins;

import org.junit.Before;
import org.junit.Test;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static junit.framework.TestCase.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

public class JenkinsClientTest {

    String baseUrl = "http://localhost:8080";
    JenkinsClient client;
    List<String> expectedSeeds = new ArrayList<String>();

    @Before
    public void setUp(){
        client = new JenkinsClient("admin","admin");
        expectedSeeds = getExpectedSeededJobs();
    }

    @Test
    public void testHasJob(){
        try {
            client.parse(baseUrl);
            assertTrue(client.hasJob("job_jenkins-job-DSL-seed"));
        } catch(Exception e){
            e.printStackTrace();
            fail("Exception occured " + e.getMessage() );
        }
    }

    @Test
    public void testGetApiToken(){
        try {
            client.parse(baseUrl + "/user/admin/configure");
            String apiToken = client.getApiToken();
            Pattern pattern = Pattern.compile("\\w+");
            Matcher matcher = pattern.matcher(apiToken);
            System.out.println("apiToken {" + apiToken + "}");
            assertTrue(matcher.matches());
        } catch(Exception e){
            e.printStackTrace();
            fail("Exception occured " + e.getMessage() );
        }
    }

    @Test
    public void testBuild(){
        try {
            String jobToken = "s22dToken23";
            client.build(baseUrl, "jenkins-job-DSL-seed", jobToken);
            Thread.sleep(10000);
            client.build(baseUrl, "jenkins-job-DSL-seed", jobToken);
            boolean foundJobs = false;
            for(int i = 0; i < 5 ; i++){
                Thread.sleep(5000);
                client.parse(baseUrl);
                List<String> jobs = client.getSeededJobNames();
                if(jobs.size() > 1){
                    foundJobs = true;
                    assertEquals(expectedSeeds, jobs);
                }
            }
            if(foundJobs == false){
                fail("Seed jobs are not found after 5 retries");
            }
        } catch(Exception e){
            e.printStackTrace();
            fail("Exception occured " + e.getMessage() );
        }
    }

    private List<String> getExpectedSeededJobs(){
        List<String>names = new ArrayList<String>();
        String seeds = System.getProperty("seededJobs");
        if(seeds != null && seeds.trim().length() > 0 ){
            if (seeds.contains(",")){
                String[] jobsArr = seeds.split(",");
                for(String job : jobsArr){
                    names.add(job.trim());
                }
            } else {
                names.add(seeds.trim());
            }
        } else {
            names.add("example1");
            names.add("example2");
            names.add("jenkins-job-DSL-seed");
            names.add("Job-DSL-Plugin");
            names.add("wf-1");
        }
        return names;
    }
}

@@ -1720,32 +1720,89 @@
 ed%22, jobToken);%0A
+            List%3CString%3E jobs = new ArrayList%3CString%3E();%0A
             bool
@@ -1865,16 +1865,119 @@
 ; i++)%7B%0A
+                foundJobs = false;%0A                System.out.println(%22Try to find jobs %7B%22 + i + %22%7D%22);%0A
         
@@ -2054,37 +2054,24 @@
             
- List%3CString%3E
  jobs = clie
@@ -2131,16 +2131,81 @@
 ) %3E 1)%7B%0A
+                    if(expectedSeeds.size() == jobs.size())%7B%0A    
         
@@ -2234,16 +2234,20 @@
 = true;%0A
+    
         
@@ -2313,84 +2313,384 @@
     
-%7D%0A            %7D%0A            if(foundJobs == false)%7B%0A                fail(%22Se
+    %7D else %7B%0A                        System.out.println(%22Not all jobs loaded, try again : %22 + i);%0A                    %7D%0A                %7D%0A            %7D%0A            if(foundJobs == false)%7B%0A                StringBuilder b = new StringBuilder();%0A                for(String job : jobs)%7B%0A                    b.append(job + %22 %22);%0A                %7D%0A                fail(%22Not all seed
 ed j
@@ -2701,12 +2701,8 @@
 are 
-not 
 foun
@@ -2718,16 +2718,40 @@
  retries
+ %7B %22 + b.toString() + %22%7D
 %22);%0A    

check if all jobs are available