package hudson.plugins.promoted_builds;

import hudson.model.FreeStyleProject;
import hudson.model.Result;
import hudson.model.FreeStyleBuild;
import hudson.tasks.ArtifactArchiver;
import hudson.tasks.Fingerprinter;
import hudson.tasks.Recorder;
import hudson.tasks.Shell;
import hudson.plugins.promoted_builds.conditions.DownstreamPassCondition;
import org.jvnet.hudson.test.HudsonTestCase;

import java.util.Arrays;
import java.util.List;

/**
 * @author Kohsuke Kawaguchi
 */
public class PromotionProcessTest extends HudsonTestCase {
    public void test1() throws Exception {
        FreeStyleProject up = createFreeStyleProject("up");
        FreeStyleProject down = createFreeStyleProject();

        List<Recorder> recorders = Arrays.asList(
                new ArtifactArchiver("a.jar", null, false),
                new Fingerprinter("", true));

        // upstream job
        up.getBuildersList().add(new Shell("date > a.jar"));
        up.getPublishersList().replaceBy(recorders);

        // promote if the downstream passes
        JobPropertyImpl promotion = new JobPropertyImpl(up);
        up.addProperty(promotion);
        PromotionProcess proc = promotion.addProcess("promo");
        proc.conditions.add(new DownstreamPassCondition(down.getName()));

        // this is the test job
        String baseUrl = new WebClient().getContextPath() + "job/up/lastSuccessfulBuild";
        down.getBuildersList().add(new Shell(
            "wget -N "+baseUrl+"/artifact/a.jar \\\n"+
            "  || curl "+baseUrl+"/artifact/a.jar > a.jar\n"+
            "expr $BUILD_NUMBER % 2 - 1\n"  // expr exits with non-zero status if result is zero
        ));
        down.getPublishersList().replaceBy(recorders);

        // not yet promoted while the downstream is failing
        FreeStyleBuild up1 = assertBuildStatusSuccess(up.scheduleBuild2(0).get());
        assertBuildStatus(Result.FAILURE,down.scheduleBuild2(0).get());
        Thread.sleep(1000); // give it a time to not promote
        assertEquals(0,proc.getBuilds().size());

        // a successful downstream build promotes upstream
        assertBuildStatusSuccess(down.scheduleBuild2(0).get());
        Thread.sleep(1000); // give it a time to promote
        assertEquals(1,proc.getBuilds().size());

        {// verify that it promoted the right stuff
            Promotion pb = proc.getBuilds().get(0);
            assertSame(pb.getTarget(),up1);
            PromotedBuildAction badge = (PromotedBuildAction) up1.getBadgeActions().get(0);
            assertTrue(badge.contains(proc));
        }
    }
}

@@ -170,24 +170,58 @@
 ctArchiver;%0A
+import hudson.tasks.BuildTrigger;%0A
 import hudso
@@ -2608,11 +2608,2104 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * Tests a promotion induced by the pseudo upstream/downstream cause relationship%0A     */%0A    public void testPromotionWithoutFingerprint() throws Exception %7B%0A        FreeStyleProject up = createFreeStyleProject(%22up%22);%0A        FreeStyleProject down = createFreeStyleProject();%0A%0A        // promote if the downstream passes%0A        JobPropertyImpl promotion = new JobPropertyImpl(up);%0A        up.addProperty(promotion);%0A        PromotionProcess proc = promotion.addProcess(%22promo%22);%0A        proc.conditions.add(new DownstreamPassCondition(down.getName()));%0A%0A        // trigger downstream automatically to create relationship%0A        up.getPublishersList().add(new BuildTrigger(down.getName(), Result.SUCCESS));%0A        hudson.rebuildDependencyGraph();%0A        %0A        // this is the downstream job%0A        down.getBuildersList().add(new Shell(%0A            %22expr $BUILD_NUMBER %25 2 - 1%5Cn%22  // expr exits with non-zero status if result is zero%0A        ));%0A%0A        // not yet promoted while the downstream is failing%0A        FreeStyleBuild up1 = assertBuildStatusSuccess(up.scheduleBuild2(0).get());%0A        waitForCompletion(down,1);%0A        assertEquals(0,proc.getBuilds().size());%0A%0A        // do it one more time and this time it should work%0A        FreeStyleBuild up2 = assertBuildStatusSuccess(up.scheduleBuild2(0).get());%0A        waitForCompletion(down,2);%0A        assertEquals(1,proc.getBuilds().size());%0A%0A        %7B// verify that it promoted the right stuff%0A            Promotion pb = proc.getBuilds().get(0);%0A            assertSame(pb.getTarget(),up2);%0A            PromotedBuildAction badge = (PromotedBuildAction) up2.getBadgeActions().get(0);%0A            assertTrue(badge.contains(proc));%0A        %7D%0A    %7D%0A%0A    private void waitForCompletion(FreeStyleProject down, int n) throws InterruptedException %7B%0A        // wait for the build completion%0A        while (down.getBuildByNumber(n)==null)%0A            Thread.sleep(1000);%0A        while (down.getBuildByNumber(n).isBuilding())%0A            Thread.sleep(1000);%0A        Thread.sleep(1000); // give it a time to not promote%0A    %7D
 %0A%7D%0A

test case for the fingerprintless promotion added in rev.31890