Skip to content

Commit

Permalink
add missing tests for Monitor.java
Browse files Browse the repository at this point in the history
  • Loading branch information
simonsymhoven committed May 4, 2021
1 parent 9d87295 commit 0a35267
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 16 deletions.
29 changes: 28 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
<popper-api.version>1.16.1-2</popper-api.version>
<junit-plugin.version>1.49</junit-plugin.version>
<pipeline-stage-step.version>2.5</pipeline-stage-step.version>
<scm-api.version>2.0.3</scm-api.version>
<mockito-core.version>1.8.5</mockito-core.version>
<json.version>20210307</json.version>
</properties>

Expand Down Expand Up @@ -103,6 +105,10 @@
<artifactId>muuri-api</artifactId>
<version>${muuri-api.version}</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>popper-api</artifactId>
Expand Down Expand Up @@ -150,7 +156,19 @@
<version>${pipeline-stage-step.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>${scm-api.version}</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand All @@ -165,9 +183,18 @@
<excludes>
<exclude>**/*Action*</exclude>
<exclude>**/*Factory*</exclude>
<exclude>**/*Property*</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>9</source>
<target>9</target>
</configuration>
</plugin>
</plugins>
</build>

Expand Down
12 changes: 10 additions & 2 deletions src/main/java/io/jenkins/plugins/monitoring/Monitor.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TaskListener;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.mixin.ChangeRequestSCMHead;
import org.apache.commons.lang.IllegalClassException;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty;
import org.jenkinsci.plugins.workflow.steps.*;
import org.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
Expand Down Expand Up @@ -35,7 +39,7 @@ public class Monitor extends Step implements Serializable {
@DataBoundConstructor
public Monitor() {
super();
this.configuration = "{\"plugins\":{}}";
this.configuration = "{\"plugins\": {}}";
}

/**
Expand Down Expand Up @@ -113,7 +117,11 @@ public Void run() throws Exception {
}

final Run<?, ?> run = getContext().get(Run.class);
if (run.getParent().getPronoun().equals("Pull Request")) {
final Job<?, ?> job = run.getParent();
final BranchJobProperty branchJobProperty = job.getProperty(BranchJobProperty.class);
final SCMHead head = branchJobProperty.getBranch().getHead();

if (head instanceof ChangeRequestSCMHead) {
getContext().get(TaskListener.class).getLogger()
.println("Build is part of a pull request. Add monitor now.");
run.addAction(new MonitoringBuildAction(run, monitor));
Expand Down
103 changes: 90 additions & 13 deletions src/test/java/io/jenkins/plugins/monitoring/MonitorTest.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,21 @@
package io.jenkins.plugins.monitoring;

import hudson.triggers.SCMTrigger;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import hudson.model.Result;
import jenkins.branch.BranchSource;
import jenkins.scm.impl.mock.MockSCMController;
import jenkins.scm.impl.mock.MockSCMSource;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

import java.io.IOException;
import java.io.InputStream;


/**
* Unit tests for the {@link Monitor} step.
*
Expand All @@ -19,16 +27,85 @@ public class MonitorTest {
public JenkinsRule jenkinsRule = new JenkinsRule();

@Test
public void should() throws Exception {
WorkflowJob job = jenkinsRule.createProject(WorkflowJob.class, "Pull Request");
job.addTrigger(new SCMTrigger(""));
job.setDefinition(new CpsFlowDefinition("" +
"node {" +
" stage ('Pull Request Monitoring - Empty Dashboard Configuration') {" +
" monitoring ( )" +
" }" +
"}", true));
WorkflowRun run = jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0));
jenkinsRule.assertLogContains("Configuration: {\"plugins\": {}}" , run);
public void should_AddDefaultMonitor_When_BuildIsPr() throws Exception {
WorkflowMultiBranchProject project = createRepositoryWithPr("Jenkinsfile.default");

project.scheduleBuild2(0);
jenkinsRule.waitUntilNoActivity();

final WorkflowJob job = project.getItems().iterator().next();
final WorkflowRun build = job.getLastBuild();
MonitoringBuildAction action = build.getAction(MonitoringBuildAction.class);

jenkinsRule.assertBuildStatusSuccess(build);
jenkinsRule.assertLogContains("Build is part of a pull request. Add monitor now." , build);
Assert.assertNotNull(action);
Assert.assertEquals(action.getMonitor().getConfiguration(), "{\"plugins\": {}}");
}

@Test
public void should_SkipAddDefaultMonitor_When_BuildIsNotPr() throws Exception {
WorkflowMultiBranchProject project = createRepositoryWithoutPr("Jenkinsfile.default");

project.scheduleBuild2(0);
jenkinsRule.waitUntilNoActivity();

final WorkflowJob job = project.getItems().iterator().next();
final WorkflowRun build = job.getLastBuild();
MonitoringBuildAction action = build.getAction(MonitoringBuildAction.class);

jenkinsRule.assertBuildStatusSuccess(build);
jenkinsRule.assertLogContains("Build is not part of a pull request. Skip adding monitor." , build);
Assert.assertNull(action);
}

@Test
public void should_ThrowException_When_AddingNotExistingView() throws Exception {
WorkflowMultiBranchProject project = createRepositoryWithPr("Jenkinsfile.custom2");

project.scheduleBuild2(0);
jenkinsRule.waitUntilNoActivity();

final WorkflowJob job = project.getItems().iterator().next();
final WorkflowRun build = job.getLastBuild();
MonitoringBuildAction action = build.getAction(MonitoringBuildAction.class);

jenkinsRule.assertBuildStatus(Result.FAILURE, build);
jenkinsRule.assertLogContains(
"Can't find class 'io.jenkins.plugins.view' in list of available plugins!" , build);
Assert.assertNull(action);
}

private WorkflowMultiBranchProject createRepositoryWithPr(String jenkinsfile) throws IOException {
MockSCMController controller = MockSCMController.create();
controller.createRepository("scm-repo");
controller.createBranch("scm-repo", "master");
final int num = controller.openChangeRequest("scm-repo", "master");
final String crNum = "change-request/" + num;
InputStream st = getClass().getResourceAsStream(String.format("/io/jenkins/plugins/monitoring/%s", jenkinsfile));
controller.addFile("scm-repo", crNum, "Jenkinsfile", "Jenkinsfile",
st.readAllBytes());

WorkflowMultiBranchProject project = jenkinsRule.createProject(WorkflowMultiBranchProject.class);
project.getSourcesList().add(new BranchSource(new MockSCMSource("1", controller, "scm-repo",
false, false, true)));


return project;
}

private WorkflowMultiBranchProject createRepositoryWithoutPr(String jenkinsfile) throws IOException {
MockSCMController controller = MockSCMController.create();
controller.createRepository("scm-repo");
controller.createBranch("scm-repo", "master");
InputStream st = getClass().getResourceAsStream(String.format("/io/jenkins/plugins/monitoring/%s", jenkinsfile));
controller.addFile("scm-repo", "master", "Jenkinsfile", "Jenkinsfile",
st.readAllBytes());

WorkflowMultiBranchProject project = jenkinsRule.createProject(WorkflowMultiBranchProject.class);
project.getSourcesList().add(new BranchSource(new MockSCMSource("1", controller, "scm-repo",
true, false, false)));

return project;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
node {
stage ('Pull Request Monitoring - Dashboard Configuration') {
monitoring (
configuration:
'''
{
"plugins": {

}
}
'''
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
node {
stage ('Pull Request Monitoring - Dashboard Configuration') {
monitoring (
configuration:
'''
{
"plugins": {
"io.jenkins.plugins.view": {
"width": 4,
"height": 4,
"color": "black"
}
}
}
'''
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
node {
stage ('Pull Request Monitoring - Dashboard Configuration') {
monitoring ( )
}
}

0 comments on commit 0a35267

Please sign in to comment.