org.jvnet.hudson.test.TestBuilder Java Examples

The following examples show how to use org.jvnet.hudson.test.TestBuilder. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MergeRequestHookTriggerHandlerImplTest.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
private boolean ciSkipTestHelper(String MRDescription, String lastCommitMsg) throws IOException, InterruptedException {
    final OneShotEvent buildTriggered = new OneShotEvent();
    FreeStyleProject project = jenkins.createFreeStyleProject();
    project.getBuildersList().add(new TestBuilder() {
        @Override
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            buildTriggered.signal();
            return true;
        }
    });
    project.setQuietPeriod(0);
    MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler = new MergeRequestHookTriggerHandlerImpl(Arrays.asList(State.opened, State.reopened), Arrays.asList(Action.approved), false, false, false);
    mergeRequestHookTriggerHandler.handle(project, mergeRequestHook()
            .withObjectAttributes(defaultMergeRequestObjectAttributes().withDescription(MRDescription).withLastCommit(commit().withMessage(lastCommitMsg).withAuthor(user().withName("test").build()).withId("testid").build()).build())
            .build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
        newMergeRequestLabelFilter(null));

    buildTriggered.block(10000);
    return buildTriggered.isSignaled();
}
 
Example #2
Source File: PipelineHookTriggerHandlerImplTest.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void pipeline_build() throws IOException, InterruptedException, GitAPIException, ExecutionException {

    final OneShotEvent buildTriggered = new OneShotEvent();
    FreeStyleProject project = jenkins.createFreeStyleProject();
    project.getBuildersList().add(new TestBuilder() {
        @Override
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            buildTriggered.signal();
            return true;
        }
    });
    project.setQuietPeriod(0);

    pipelineHookTriggerHandler.handle(project, pipelineHook, false, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
                                  newMergeRequestLabelFilter(null));

    buildTriggered.block(10000);
    assertThat(buildTriggered.isSignaled(), is(true));
}
 
Example #3
Source File: PipelineHookTriggerHandlerImplTest.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
@Test
/**
 * always triggers since pipeline events do not contain ci skip message
 */
public void pipeline_ciSkip() throws IOException, InterruptedException {
    final OneShotEvent buildTriggered = new OneShotEvent();
    FreeStyleProject project = jenkins.createFreeStyleProject();
    project.getBuildersList().add(new TestBuilder() {
        @Override
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            buildTriggered.signal();
            return true;
        }
    });
    project.setQuietPeriod(0);
    pipelineHookTriggerHandler.handle(project, pipelineHook , true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
        newMergeRequestLabelFilter(null));

    buildTriggered.block(10000);
    assertThat(buildTriggered.isSignaled(), is(true));
}
 
Example #4
Source File: PushHookTriggerHandlerImplTest.java    From gitlab-plugin with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void push_ciSkip() throws IOException, InterruptedException {
    final OneShotEvent buildTriggered = new OneShotEvent();
    FreeStyleProject project = jenkins.createFreeStyleProject();
    project.getBuildersList().add(new TestBuilder() {
        @Override
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            buildTriggered.signal();
            return true;
        }
    });
    project.setQuietPeriod(0);
    pushHookTriggerHandler.handle(project, pushHook()
            .withCommits(Arrays.asList(commit().withMessage("some message").build(),
                                       commit().withMessage("[ci-skip]").build()))
            .build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
                                  newMergeRequestLabelFilter(null));

    buildTriggered.block(10000);
    assertThat(buildTriggered.isSignaled(), is(false));
}
 
Example #5
Source File: CaseResultTest.java    From junit-plugin with MIT License 5 votes vote down vote up
private FreeStyleBuild configureTestBuild(String projectName) throws Exception {
    FreeStyleProject p = projectName == null ? rule.createFreeStyleProject() : rule.createFreeStyleProject(projectName);
    p.getBuildersList().add(new TestBuilder() {
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            build.getWorkspace().child("junit.xml").copyFrom(
                getClass().getResource("junit-report-20090516.xml"));
            return true;
        }
    });
    p.getPublishersList().add(new JUnitResultArchiver("*.xml"));
    return rule.assertBuildStatus(Result.UNSTABLE, p.scheduleBuild2(0).get());
}
 
Example #6
Source File: InteroperabilityTest.java    From lockable-resources-plugin with MIT License 5 votes vote down vote up
@Test
public void interoperability() throws Exception {
  final Semaphore semaphore = new Semaphore(1);
  LockableResourcesManager.get().createResource("resource1");
  WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p");
  p.setDefinition(
      new CpsFlowDefinition(
          "lock('resource1') {\n" + "	echo 'Locked'\n" + "}\n" + "echo 'Finish'", true));

  FreeStyleProject f = j.createFreeStyleProject("f");
  f.addProperty(new RequiredResourcesProperty("resource1", null, null, null, null));
  f.getBuildersList()
      .add(
          new TestBuilder() {

            @Override
            public boolean perform(
                AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
                throws InterruptedException, IOException {
              semaphore.acquire();
              return true;
            }
          });
  semaphore.acquire();
  FreeStyleBuild f1 = f.scheduleBuild2(0).waitForStart();

  WorkflowRun b1 = p.scheduleBuild2(0).waitForStart();
  j.waitForMessage("[resource1] is locked by " + f1.getFullDisplayName() + ", waiting...", b1);
  isPaused(b1, 1, 1);
  semaphore.release();

  // Wait for lock after the freestyle finishes
  j.waitForMessage("Lock released on resource [resource1]", b1);
  isPaused(b1, 1, 0);
}
 
Example #7
Source File: MergeRequestHookTriggerHandlerImplTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
private OneShotEvent doHandle(MergeRequestHookTriggerHandler mergeRequestHookTriggerHandler,
		MergeRequestObjectAttributesBuilder objectAttributes) throws GitAPIException, IOException,
           InterruptedException {
	Git.init().setDirectory(tmp.getRoot()).call();
       tmp.newFile("test");
       Git git = Git.open(tmp.getRoot());
       git.add().addFilepattern("test");
       RevCommit commit = git.commit().setMessage("test").call();
       ObjectId head = git.getRepository().resolve(Constants.HEAD);
       String repositoryUrl = tmp.getRoot().toURI().toString();

       final OneShotEvent buildTriggered = new OneShotEvent();
       FreeStyleProject project = jenkins.createFreeStyleProject();
       project.setScm(new GitSCM(repositoryUrl));
       project.getBuildersList().add(new TestBuilder() {
           @Override
           public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
               buildTriggered.signal();
               return true;
           }
       });
       project.setQuietPeriod(0);
	mergeRequestHookTriggerHandler.handle(project, mergeRequestHook()
               .withObjectAttributes(objectAttributes
           		    .withTargetBranch("refs/heads/" + git.nameRev().add(head).call().get(head))
           		    .withLastCommit(commit().withAuthor(user().withName("test").build()).withId(commit.getName()).build())
                   .build())
               .withProject(project()
                   .withWebUrl("https://gitlab.org/test.git")
                   .build()
               )
               .build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
           newMergeRequestLabelFilter(null));

       buildTriggered.block(10000);
       return buildTriggered;
}
 
Example #8
Source File: NoteHookTriggerHandlerImplTest.java    From gitlab-plugin with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void note_ciSkip() throws IOException, InterruptedException {
    final OneShotEvent buildTriggered = new OneShotEvent();
    FreeStyleProject project = jenkins.createFreeStyleProject();
    project.getBuildersList().add(new TestBuilder() {
        @Override
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            buildTriggered.signal();
            return true;
        }
    });
    Date currentDate = new Date();
    project.setQuietPeriod(0);
    noteHookTriggerHandler.handle(project, noteHook()
            .withObjectAttributes(noteObjectAttributes()
                .withId(1)
                .withNote("ci-run")
                .withAuthorId(1)
                .withProjectId(1)
                .withCreatedAt(currentDate)
                .withUpdatedAt(currentDate)
                .withUrl("https://gitlab.org/test/merge_requests/1#note_1")
                .build())
            .withMergeRequest(mergeRequestObjectAttributes().withDescription("[ci-skip]").build())
            .build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
                                  newMergeRequestLabelFilter(null));

    buildTriggered.block(10000);
    assertThat(buildTriggered.isSignaled(), is(false));
}
 
Example #9
Source File: TestReportUiTest.java    From junit-plugin with MIT License 5 votes vote down vote up
/**
 * Creates a freestyle project & build with UNSTABLE status
 * containing a test report from: /hudson/tasks/junit/junit-report-20090516.xml
 */
private FreeStyleBuild configureTestBuild(String projectName) throws Exception {
    FreeStyleProject p = j.createFreeStyleProject(projectName);
    p.getBuildersList().add(new TestBuilder() {
        @Override
        @SuppressWarnings("null")
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            build.getWorkspace().child("junit.xml").copyFrom(
                    getClass().getResource("/hudson/tasks/junit/junit-report-20090516.xml"));
            return true;
        }
    });
    p.getPublishersList().add(new JUnitResultArchiver("*.xml"));
    return j.assertBuildStatus(Result.UNSTABLE, p.scheduleBuild2(0).get());
}
 
Example #10
Source File: WorkSpaceZipperTest.java    From aws-lambda-jenkins-plugin with MIT License 5 votes vote down vote up
@Test
public void testGetZipFolderEmpty() throws Exception {
    final OneShotEvent buildEnded = new OneShotEvent();

    FreeStyleProject p = j.createFreeStyleProject();
    p.getBuildersList().add(new TestBuilder() {
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
                               BuildListener listener) throws InterruptedException, IOException {
            build.getWorkspace().child("echo").mkdirs();
            buildEnded.signal();
            return true;
        }
    });

    p.scheduleBuild2(0);
    buildEnded.block();

    JenkinsLogger logger = new JenkinsLogger(System.out);
    WorkSpaceZipper workSpaceZipper = new WorkSpaceZipper(p.getSomeWorkspace(), logger);
    File zip = workSpaceZipper.getZip("echo");

    assertTrue(zip.exists());
    assertTrue(zip.getAbsolutePath().contains("awslambda-"));

    ZipFile zipFile = new ZipFile(zip);
    assertNotNull(zipFile);
    assertFalse(zipFile.entries().hasMoreElements());
}
 
Example #11
Source File: WorkSpaceZipperTest.java    From aws-lambda-jenkins-plugin with MIT License 5 votes vote down vote up
@Test
public void testGetZipFolder() throws Exception {
    final OneShotEvent buildEnded = new OneShotEvent();

    FreeStyleProject p = j.createFreeStyleProject();
    p.getBuildersList().add(new TestBuilder() {
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
                               BuildListener listener) throws InterruptedException, IOException {
            build.getWorkspace().child("echo").child("index.js").copyFrom(new FileInputStream(testUtil.getResource("echo/index.js")));
            buildEnded.signal();
            return true;
        }
    });

    p.scheduleBuild2(0);
    buildEnded.block();

    JenkinsLogger logger = new JenkinsLogger(System.out);
    WorkSpaceZipper workSpaceZipper = new WorkSpaceZipper(p.getSomeWorkspace(), logger);
    File zip = workSpaceZipper.getZip("echo");

    assertTrue(zip.exists());
    assertTrue(zip.getAbsolutePath().contains("awslambda-"));

    ZipFile zipFile = new ZipFile(zip);
    assertNotNull(zipFile);
    assertNotNull(zipFile.getEntry("index.js"));
}
 
Example #12
Source File: WorkSpaceZipperTest.java    From aws-lambda-jenkins-plugin with MIT License 5 votes vote down vote up
@Test
public void testGetZipWithZip() throws Exception {
    final OneShotEvent buildEnded = new OneShotEvent();

    FreeStyleProject p = j.createFreeStyleProject();
    p.getBuildersList().add(new TestBuilder() {
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
                               BuildListener listener) throws InterruptedException, IOException {
            build.getWorkspace().child("echo.zip").copyFrom(new FileInputStream(testUtil.getResource("echo.zip")));
            buildEnded.signal();
            return true;
        }
    });

    p.scheduleBuild2(0);
    buildEnded.block();

    JenkinsLogger logger = new JenkinsLogger(System.out);
    WorkSpaceZipper workSpaceZipper = new WorkSpaceZipper(p.getSomeWorkspace(), logger);
    File zip = workSpaceZipper.getZip("echo.zip");

    assertTrue(zip.exists());
    assertTrue(zip.getAbsolutePath().contains("awslambda-"));

    ZipFile zipFile = new ZipFile(zip);
    assertNotNull(zipFile);
    assertNotNull(zipFile.getEntry("index.js"));
}
 
Example #13
Source File: RemoteFileFetcherTest.java    From phabricator-jenkins-plugin with MIT License 5 votes vote down vote up
private Builder echoBuilder(final String fileName, final String content) {
    return new TestBuilder() {
        @Override
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws
                InterruptedException, IOException {
            build.getWorkspace().child(fileName).write(content, "UTF-8");
            return true;
        }
    };
}
 
Example #14
Source File: BuildResultProcessorTest.java    From phabricator-jenkins-plugin with MIT License 5 votes vote down vote up
private Builder echoBuilder(final String fileName, final String content) {
    return new TestBuilder() {
        @Override
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws
                InterruptedException, IOException {
            build.getWorkspace().child(fileName).write(content, "UTF-8");
            return true;
        }
    };
}
 
Example #15
Source File: TestUtils.java    From phabricator-jenkins-plugin with MIT License 5 votes vote down vote up
public static void addCopyBuildStep(
        FreeStyleProject p,
        final String fileName,
        final Class resourceClass,
        final String resourceName) {
    p.getBuildersList().add(new TestBuilder() {
        @Override
        public boolean perform(AbstractBuild build, Launcher launcher, BuildListener buildListener) throws
                InterruptedException, IOException {
            build.getWorkspace().child(fileName).copyFrom(resourceClass.getResourceAsStream(resourceName));
            return true;
        }
    });
}
 
Example #16
Source File: MarathonRecorderTest.java    From marathon-plugin with Apache License 2.0 5 votes vote down vote up
private TestBuilder createMarathonFileBuilder(final String payload) {
    return new TestBuilder() {
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher,
                               BuildListener listener) throws InterruptedException, IOException {
            build.getWorkspace().child("marathon.json").write(payload, "UTF-8");
            return true;
        }
    };
}
 
Example #17
Source File: PipelineApiTest.java    From blueocean-plugin with MIT License 5 votes vote down vote up
@Test
public void testArtifactsRunApi() throws Exception {
    FreeStyleProject p = j.createFreeStyleProject("pipeline1");
    p.getBuildersList().add(new TestBuilder() {
        @Override public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            FilePath ws = build.getWorkspace();
            if (ws == null) {
                return false;
            }
            FilePath dir = ws.child("dir");
            dir.mkdirs();
            dir.child("fizz").write("contents", null);
            dir.child("lodge").symlinkTo("fizz", listener);
            return true;
        }
    });
    ArtifactArchiver aa = new ArtifactArchiver("dir/fizz");
    aa.setAllowEmptyArchive(true);
    p.getPublishersList().add(aa);
    FreeStyleBuild b = j.assertBuildStatusSuccess(p.scheduleBuild2(0));


    List artifacts = get("/organizations/jenkins/pipelines/pipeline1/runs/"+b.getId()+"/artifacts", List.class);

    assertEquals(1, artifacts.size());
    assertEquals("fizz", ((Map) artifacts.get(0)).get("name"));

    String artifactId = (String) ((Map) artifacts.get(0)).get("id");
    ArtifactContainerImpl container = new ArtifactContainerImpl(b, new Reachable() {
        @Override
        public Link getLink() {
            return new Link("/blue/rest/organizations/jenkins/pipelines/pipeline1/runs/1/artifacts/");
        }
    });
    BlueArtifact blueArtifact = container.get(artifactId);
    assertNotNull(blueArtifact);
}
 
Example #18
Source File: AbstractFreestyleTestProject.java    From aws-codecommit-trigger-plugin with Apache License 2.0 5 votes vote down vote up
protected void subscribeProject(final ProjectFixture fixture) throws Exception {
        String name = UUID.randomUUID().toString();

        final FreeStyleProject job = jenkinsRule.getInstance().createProject(FreeStyleProject.class, name);
        job.setScm(new NullSCM());
        if (fixture.getScm() != null) {
            job.setScm(fixture.getScm());
        }

        final String uuid = this.sqsQueue.getUuid();

        SQSTrigger trigger = null;

        if (fixture.isHasTrigger()) {
            trigger = new SQSTrigger(uuid, fixture.isSubscribeInternalScm(), fixture.getScmConfigs());
        }

//        final OneShotEvent event = new OneShotEvent();
        fixture.setEvent(new OneShotEvent());
        job.getBuildersList().add(new TestBuilder() {

            @Override
            public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
                fixture.setLastBuild(job.getLastBuild());
                fixture.getEvent().signal();
                return true;
            }
        });
        job.setQuietPeriod(0);

        if (trigger != null) {
            trigger.start(job, false);
            job.addTrigger(trigger);
        }

//        fixture.setEvent(event);
    }
 
Example #19
Source File: TestUtil.java    From appcenter-plugin with MIT License 4 votes vote down vote up
public static TestBuilder createFile(final @Nonnull String pathToFile) {
    return createFile(pathToFile, "all of us with wings");
}
 
Example #20
Source File: PushHookTriggerHandlerImplTest.java    From gitlab-plugin with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void push_build() throws IOException, InterruptedException, GitAPIException, ExecutionException {
    Git.init().setDirectory(tmp.getRoot()).call();
    tmp.newFile("test");
    Git git = Git.open(tmp.getRoot());
    git.add().addFilepattern("test");
    RevCommit commit = git.commit().setMessage("test").call();
    ObjectId head = git.getRepository().resolve(Constants.HEAD);
    String repositoryUrl = tmp.getRoot().toURI().toString();

    final OneShotEvent buildTriggered = new OneShotEvent();
    FreeStyleProject project = jenkins.createFreeStyleProject();
    project.setScm(new GitSCM(repositoryUrl));
    project.getBuildersList().add(new TestBuilder() {
        @Override
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            buildTriggered.signal();
            return true;
        }
    });
    project.setQuietPeriod(0);
    pushHookTriggerHandler.handle(project, pushHook()
            .withBefore("0000000000000000000000000000000000000000")
            .withProjectId(1)
            .withUserName("test")
            .withObjectKind("tag_push")
            .withRepository(repository()
                    .withName("test")
                    .withHomepage("https://gitlab.org/test")
                    .withUrl("[email protected]:test.git")
                    .withGitSshUrl("[email protected]:test.git")
                    .withGitHttpUrl("https://gitlab.org/test.git")
                    .build())
            .withProject(project()
                    .withNamespace("test-namespace")
                    .withWebUrl("https://gitlab.org/test")
                    .build())
            .withAfter(commit.name())
            .withRef("refs/heads/" + git.nameRev().add(head).call().get(head))
            .build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
                                  newMergeRequestLabelFilter(null));

    buildTriggered.block(10000);
    assertThat(buildTriggered.isSignaled(), is(true));
}
 
Example #21
Source File: PushHookTriggerHandlerImplTest.java    From gitlab-plugin with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void push_build2DifferentBranchesButSameCommit() throws IOException, InterruptedException, GitAPIException, ExecutionException {
    Git.init().setDirectory(tmp.getRoot()).call();
    tmp.newFile("test");
    Git git = Git.open(tmp.getRoot());
    git.add().addFilepattern("test");
    RevCommit commit = git.commit().setMessage("test").call();
    ObjectId head = git.getRepository().resolve(Constants.HEAD);
    String repositoryUrl = tmp.getRoot().toURI().toString();

    final AtomicInteger buildCount = new AtomicInteger(0);

    final OneShotEvent buildTriggered = new OneShotEvent();
    FreeStyleProject project = jenkins.createFreeStyleProject();
    project.setScm(new GitSCM(repositoryUrl));
    project.getBuildersList().add(new TestBuilder() {
        @Override
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            int count = buildCount.incrementAndGet();
            if (count == 2) {
                buildTriggered.signal();
            }
            return true;
        }
    });
    project.setQuietPeriod(0);
    PushHookBuilder pushHookBuilder = pushHook()
        .withBefore("0000000000000000000000000000000000000000")
        .withProjectId(1)
        .withUserName("test")
        .withObjectKind("push")
        .withRepository(repository()
                            .withName("test")
                            .withHomepage("https://gitlab.org/test")
                            .withUrl("[email protected]:test.git")
                            .withGitSshUrl("[email protected]:test.git")
                            .withGitHttpUrl("https://gitlab.org/test.git")
                            .build())
        .withProject(project()
                         .withNamespace("test-namespace")
                         .withWebUrl("https://gitlab.org/test")
                         .build())
        .withAfter(commit.name())
        .withRef("refs/heads/" + git.nameRev().add(head).call().get(head));
    pushHookTriggerHandler.handle(project, pushHookBuilder.build(), true, newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
                                  newMergeRequestLabelFilter(null));
    pushHookTriggerHandler.handle(project, pushHookBuilder
                                      .but().withRef("refs/heads/" + git.nameRev().add(head).call().get(head) + "-2").build(), true,
                                  newBranchFilter(branchFilterConfig().build(BranchFilterType.All)), newMergeRequestLabelFilter(null));
    buildTriggered.block(10000);
    assertThat(buildTriggered.isSignaled(), is(true));
    assertThat(buildCount.intValue(), is(2));
}
 
Example #22
Source File: NoteHookTriggerHandlerImplTest.java    From gitlab-plugin with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void note_build() throws IOException, InterruptedException, GitAPIException, ExecutionException {
    Git.init().setDirectory(tmp.getRoot()).call();
    tmp.newFile("test");
    Git git = Git.open(tmp.getRoot());
    git.add().addFilepattern("test");
    RevCommit commit = git.commit().setMessage("test").call();
    ObjectId head = git.getRepository().resolve(Constants.HEAD);
    String repositoryUrl = tmp.getRoot().toURI().toString();

    final OneShotEvent buildTriggered = new OneShotEvent();
    FreeStyleProject project = jenkins.createFreeStyleProject();
    project.setScm(new GitSCM(repositoryUrl));
    project.getBuildersList().add(new TestBuilder() {
        @Override
        public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
            buildTriggered.signal();
            return true;
        }
    });
    Date currentDate = new Date();
    project.setQuietPeriod(0);
    noteHookTriggerHandler.handle(project, noteHook()
            .withObjectAttributes(noteObjectAttributes()
                .withId(1)
                .withNote("ci-run")
                .withAuthorId(1)
                .withProjectId(1)
                .withCreatedAt(currentDate)
                .withUpdatedAt(currentDate)
                .withUrl("https://gitlab.org/test/merge_requests/1#note_1")
                .build())
            .withMergeRequest(mergeRequestObjectAttributes()
                .withTargetBranch("refs/heads/" + git.nameRev().add(head).call().get(head))
                .withState(State.opened)
                .withIid(1)
                .withTitle("test")
                .withTargetProjectId(1)
                .withSourceProjectId(1)
                .withSourceBranch("feature")
                .withTargetBranch("master")
                .withLastCommit(commit().withAuthor(user().withName("test").build()).withId(commit.getName()).build())
                .withSource(project()
                    .withName("test")
                    .withNamespace("test-namespace")
                    .withHomepage("https://gitlab.org/test")
                    .withUrl("[email protected]:test.git")
                    .withSshUrl("[email protected]:test.git")
                    .withHttpUrl("https://gitlab.org/test.git")
                    .build())
                .withTarget(project()
                    .withName("test")
                    .withNamespace("test-namespace")
                    .withHomepage("https://gitlab.org/test")
                    .withUrl("[email protected]:test.git")
                    .withSshUrl("[email protected]:test.git")
                    .withHttpUrl("https://gitlab.org/test.git")
                    .withWebUrl("https://gitlab.org/test.git")
                    .build())
                .build())
            .build(), true, BranchFilterFactory.newBranchFilter(branchFilterConfig().build(BranchFilterType.All)),
                                  newMergeRequestLabelFilter(null));

    buildTriggered.block(10000);
    assertThat(buildTriggered.isSignaled(), is(true));
}
 
Example #23
Source File: TestUtil.java    From appcenter-plugin with MIT License 4 votes vote down vote up
public static TestBuilder createFile(final @Nonnull String pathToFile, final @Nonnull String content) {
    return new TestAppWriter(pathToFile, content);
}