Skip to content

Commit

Permalink
Add integration test for DeleteBuildAction (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
damianszczepanik authored Nov 13, 2019
1 parent 3aa89d6 commit 9c92eab
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 28 deletions.
17 changes: 4 additions & 13 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<java.level>8</java.level>

<jacoco.version>0.7.9</jacoco.version>
<powermock.version>1.6.6</powermock.version>
<powermock.version>2.0.0-beta.5</powermock.version>
</properties>

<scm>
Expand Down Expand Up @@ -169,26 +169,17 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<scope>test</scope>
<!-- powermock wants objenesis 2.4 and mockito wants objenesis 2.1 -->
<!-- exclude from mockito so that powermock version is used -->
<exclusions>
<exclusion>
<groupId>org.objenesis</groupId>
<artifactId>objenesis</artifactId>
</exclusion>
</exclusions>
<version>2.18.0</version>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-module-junit4</artifactId>
<artifactId>powermock-api-mockito2</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.powermock</groupId>
<artifactId>powermock-api-mockito</artifactId>
<artifactId>powermock-module-junit4</artifactId>
<version>${powermock.version}</version>
<scope>test</scope>
</dependency>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,13 @@
*/
public class JobBuilder {

public static <JobT extends Job<JobT, RunT>, RunT extends Run<JobT, RunT>> Job buildSampleJob() {
Job<JobT, RunT> job = mock(Job.class);
public static Job buildSampleJob() {
return buildSampleJob(mock(Run.class));
}

public static Job buildSampleJob(Run lastBuild) {
Job job = mock(Job.class);

RunT lastBuild = (RunT) mock(Run.class);
when(job.getLastCompletedBuild()).thenReturn(lastBuild);

return job;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pl.damianszczepanik.jenkins.buildhistorymanager;

import static org.assertj.core.api.Assertions.assertThat;
import static org.powermock.api.mockito.PowerMockito.mock;

import java.io.IOException;
Expand All @@ -16,12 +17,12 @@ public class RunStub extends Run {

private Result result;

public int deleteArtifactsTimes;
public int deleteTimes;
private int deleteArtifactsTimes;
private int deleteTimes;

public RunStub() throws IOException {
super(mock(Job.class));
setStartTime(System.currentTimeMillis());
public RunStub(int buildNumber) throws IOException {
this();
setBuildNumber(buildNumber);
}

public RunStub(Result result) throws IOException {
Expand All @@ -34,6 +35,19 @@ public RunStub(long startTime) throws IOException {
setStartTime(startTime);
}

public RunStub() throws IOException {
super(mock(Job.class));
setStartTime(System.currentTimeMillis());
}

public void setBuildNumber(int buildNumber) {
this.number = buildNumber;
}

public void setPreviousBuild(Run previousBuild) {
this.previousBuild = previousBuild;
}

private void setStartTime(long startTime) {
Deencapsulation.setField(this, "startTime", startTime);
}
Expand All @@ -48,6 +62,22 @@ public void delete() {
deleteTimes++;
}

public void assertBuildWasDeleted() {
assertThat(deleteTimes).isOne();
}

public void assertBuildIsAvailable() {
assertThat(deleteTimes).isZero();
}

public void assertArtifactsWereDeleted() {
assertThat(deleteArtifactsTimes).isOne();
}

public void assertArtifactsAreAvailable() {
assertThat(deleteArtifactsTimes).isZero();
}

@Override
public Result getResult() {
return result;
Expand All @@ -57,4 +87,9 @@ public Result getResult() {
public long getDuration() {
return 0;
}

@Override
public boolean isBuilding() {
return false;
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package pl.damianszczepanik.jenkins.buildhistorymanager.model.actions;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;

import org.junit.Test;
Expand All @@ -23,6 +21,6 @@ public void perform_DeletesJob() throws IOException, InterruptedException {
action.perform(run);

// then
assertThat(run.deleteArtifactsTimes).isOne();
run.assertArtifactsWereDeleted();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package pl.damianszczepanik.jenkins.buildhistorymanager.model.actions;

import static org.powermock.api.mockito.PowerMockito.mock;
import static org.powermock.api.mockito.PowerMockito.mockStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import hudson.model.Job;
import jenkins.model.Jenkins;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import pl.damianszczepanik.jenkins.buildhistorymanager.BuildHistoryManager;
import pl.damianszczepanik.jenkins.buildhistorymanager.JobBuilder;
import pl.damianszczepanik.jenkins.buildhistorymanager.RunStub;
import pl.damianszczepanik.jenkins.buildhistorymanager.descriptors.DeleteBuildActionDescriptor;
import pl.damianszczepanik.jenkins.buildhistorymanager.model.Rule;

/**
* @author Damian Szczepanik (damianszczepanik@github)
* @see <a href="https://github.com/jenkinsci/build-history-manager-plugin/wiki/Delete-artifacts">documentation</a>
*/
@PrepareForTest(Jenkins.class)
@RunWith(PowerMockRunner.class)
@PowerMockIgnore({ "javax.management.*", "com.sun.org.apache.xerces.*", "javax.xml.*",
"org.xml.*", "org.w3c.dom.*", "com.sun.org.apache.xalan.*", "javax.activation.*" })
public class DeleteBuildActionIT {

@Test
public void testDeletesArtifacts() throws IOException, InterruptedException {

// given
RunStub run23 = new RunStub(23);

RunStub run24 = new RunStub(24);
run23.setPreviousBuild(run24);

RunStub run25 = new RunStub(25);
run24.setPreviousBuild(run25);

RunStub run30 = new RunStub(30);
run25.setPreviousBuild(run30);

RunStub run31 = new RunStub(31);
run30.setPreviousBuild(run31);

RunStub run32 = new RunStub(32);
run31.setPreviousBuild(run32);

RunStub run35 = new RunStub(35);
run32.setPreviousBuild(run35);


Rule rule1 = new Rule(Collections.emptyList(), Collections.emptyList());
rule1.setContinueAfterMatch(false);
rule1.setMatchAtMost(2);

Rule rule2 = new Rule(Collections.emptyList(), Collections.singletonList(new DeleteArtifactsAction()));
rule2.setContinueAfterMatch(false);
rule2.setMatchAtMost(3);

Rule rule3 = new Rule(Collections.emptyList(), Collections.singletonList(new DeleteBuildAction()));

List<Rule> rules = Arrays.asList(rule1, rule2, rule3);
BuildHistoryManager buildHistoryManager = new BuildHistoryManager(rules);
Job job = JobBuilder.buildSampleJob(run23);
mockDescriptor();

// when
buildHistoryManager.perform(job);

// then
run23.assertBuildIsAvailable();
run23.assertArtifactsAreAvailable();

run24.assertBuildIsAvailable();
run24.assertArtifactsAreAvailable();

run25.assertBuildIsAvailable();
run25.assertArtifactsWereDeleted();

run30.assertBuildIsAvailable();
run30.assertArtifactsWereDeleted();

run31.assertBuildIsAvailable();
run31.assertArtifactsWereDeleted();

run32.assertBuildWasDeleted();

run35.assertBuildWasDeleted();
}

private static void mockDescriptor() {
mockStatic(Jenkins.class);
Jenkins jenkins = mock(Jenkins.class);
when(Jenkins.getInstance()).thenReturn(jenkins);
when(jenkins.getDescriptorOrDie(Matchers.any())).thenReturn(new DeleteBuildActionDescriptor());
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
package pl.damianszczepanik.jenkins.buildhistorymanager.model.actions;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.IOException;

import org.junit.Test;
Expand All @@ -23,7 +21,6 @@ public void perform_DeletesJob() throws IOException, InterruptedException {
action.perform(run);

// then
assertThat(run.deleteTimes).isOne();
run.assertBuildWasDeleted();
}
}

0 comments on commit 9c92eab

Please sign in to comment.