package com.ribose.jenkins.plugin.awscodecommittrigger.it.feature.subscribed_branch;

import com.ribose.jenkins.plugin.awscodecommittrigger.it.AbstractJenkinsIT;
import com.ribose.jenkins.plugin.awscodecommittrigger.it.fixture.ProjectFixture;
import hudson.util.OneShotEvent;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.logging.Level;


@RunWith(Parameterized.class)
public class MultiProjectFixturesIT extends AbstractJenkinsIT {

    @Parameterized.Parameter
    public String name;

    @Parameterized.Parameter(1)
    public List<ProjectFixture> projectFixtures;

    @Parameterized.Parameters(name = "{0}")
    public static List<Object[]> fixtures() {
        return Arrays.asList(new Object[][]{
            {
                "should_not_missing_any_projects",
                Arrays.asList(
                    new ProjectFixture()
                        .setSendBranches("refs/heads/foo")
                        .setSubscribedBranches("foo")
                        .setShouldStarted(Boolean.FALSE),
                    new ProjectFixture()
                        .setSendBranches("refs/heads/bar")
                        .setSubscribedBranches("bar")
                        .setShouldStarted(Boolean.TRUE)
                )
            }
        });
    }

    @Test
    public void shouldPassProjectFixtures() throws Exception {
        logger.log(Level.INFO, "[RUN] " + this.name);
        for (final ProjectFixture projectFixture : this.projectFixtures) {
            this.mockAwsSqs.send(projectFixture.getSendBranches());
        }

        ExecutorService threadPool = Executors.newFixedThreadPool(this.projectFixtures.size());

        for (final ProjectFixture projectFixture : this.projectFixtures) {
            logger.log(Level.FINEST, "[FIXTURE] {0}", projectFixture);
            threadPool.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        MultiProjectFixturesIT.this.logger.log(Level.INFO, "[THREAD-STARTED] subscribed branches: {0}", projectFixture.getSubscribedBranches());
                        OneShotEvent buildEvent = MultiProjectFixturesIT.this.submitGitScmProject(projectFixture.getSubscribedBranches());
                        buildEvent.block(projectFixture.getTimeout());
                        Assertions.assertThat(buildEvent.isSignaled()).isEqualTo(projectFixture.getShouldStarted());
                        MultiProjectFixturesIT.this.logger.log(Level.INFO, "[THREAD-DONE] subscribed branches: {0}", projectFixture.getSubscribedBranches());
                    } catch (Exception e) {
                        throw new RuntimeException(e);
                    }
                }
            });
        }

        threadPool.shutdown();
        threadPool.awaitTermination(Integer.MAX_VALUE, TimeUnit.SECONDS);

        logger.log(Level.INFO, "[DONE] " + this.name);
    }
}

@@ -406,16 +406,72 @@
 rized;%0A%0A
+import java.io.IOException;%0Aimport java.util.ArrayList;%0A
 import j
@@ -506,24 +506,47 @@
 .util.List;%0A
+import java.util.UUID;%0A
 import java.
@@ -1010,24 +1010,25 @@
 res() %7B%0A
+%0A
         
 return A
@@ -1023,145 +1023,239 @@
     
-return Arrays.asList(new Object%5B%5D%5B%5D%7B%0A            %7B%0A                %22should_not_missing_any_projects%22,%0A                Arrays.asList(%0A    
+List%3CProjectFixture%3E withoutWildcardProjects = new ArrayList%3C%3E();%0A        for (int i = 0; i %3C 10; i++) %7B%0A            String branch = String.format(%22%25s_%25s%22, i, UUID.randomUUID().toString());%0A            withoutWildcardProjects.add(%0A
     
@@ -1299,36 +1299,32 @@
                 
-   
- 
 .setSendBranches
@@ -1336,26 +1336,28 @@
 s/heads/
-foo%22)%0A    
+%22 + branch)%0A
         
@@ -1395,17 +1395,16 @@
 hes(
-%22foo%22)%0A  
+branch)%0A
     
@@ -1411,34 +1411,32 @@
                 
-  
 .setShouldStarte
@@ -1449,256 +1449,166 @@
 ean.
-FALSE),%0A                    new ProjectFixture()%0A                        .setSendBranches(%22refs/heads/bar%22)%0A                        .setSubscribedBranches(%22bar%22)%0A                        .setShouldStarted(Boolean.TRUE)%0A                )%0A            
+TRUE)%0A            );%0A        %7D%0A%0A%0A        return Arrays.asList(new Object%5B%5D%5B%5D%7B%0A            %7B%22should_trigger_branches_without_wildcard%22, withoutWildcardProjects
 %7D%0A  
@@ -1620,24 +1620,25 @@
  %7D);%0A    %7D%0A%0A
+%0A
     @Test%0A  
@@ -1953,16 +1953,41 @@
 ecutors.
+newCachedThreadPool();// 
 newFixed
@@ -1997,16 +1997,25 @@
 eadPool(
+Math.min(
 this.pro
@@ -2033,16 +2033,20 @@
 s.size()
+, 4)
 );%0A%0A    
@@ -2071,32 +2071,25 @@
 jectFixture 
-projectF
+f
 ixture : thi
@@ -2159,32 +2159,25 @@
 TURE%5D %7B0%7D%22, 
-projectF
+f
 ixture);%0A   
@@ -2169,24 +2169,25 @@
 , fixture);%0A
+%0A
             
@@ -2426,32 +2426,25 @@
 ches: %7B0%7D%22, 
-projectF
+f
 ixture.getSu
@@ -2562,24 +2562,17 @@
 Project(
-projectF
+f
 ixture.g
@@ -2641,33 +2641,79 @@
 ock(
-projectFixture.getTimeout
+fixture.getTimeout() * MultiProjectFixturesIT.this.projectFixtures.size
 ());
@@ -2759,16 +2759,50 @@
 ertThat(
+fixture.getSubscribedBranches() + 
 buildEve
@@ -2828,24 +2828,51 @@
 EqualTo(
-projectF
+fixture.getSubscribedBranches() + f
 ixture.g
@@ -3008,24 +3008,17 @@
 : %7B0%7D%22, 
-projectF
+f
 ixture.g
@@ -3071,16 +3071,58 @@
  catch (
+AssertionError %7C IOException %7C Interrupted
 Exceptio
@@ -3166,24 +3166,22 @@
 new 
-RuntimeException
+AssertionError
 (e);

Test for mactching branch names