From 9c92eabbd8b0c24fa7d6444bd93b1c9da4398867 Mon Sep 17 00:00:00 2001 From: Damian Szczepanik Date: Thu, 14 Nov 2019 00:03:33 +0100 Subject: [PATCH] Add integration test for DeleteBuildAction (#23) --- pom.xml | 17 +-- .../buildhistorymanager/JobBuilder.java | 9 +- .../jenkins/buildhistorymanager/RunStub.java | 45 +++++++- .../actions/DeleteArtifactsActionTest.java | 4 +- .../model/actions/DeleteBuildActionIT.java | 106 ++++++++++++++++++ .../model/actions/DeleteBuildActionTest.java | 5 +- 6 files changed, 158 insertions(+), 28 deletions(-) create mode 100644 src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/actions/DeleteBuildActionIT.java diff --git a/pom.xml b/pom.xml index d29e6030..bbd4ca3d 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ 8 0.7.9 - 1.6.6 + 2.0.0-beta.5 @@ -169,26 +169,17 @@ org.mockito mockito-core - 1.10.19 - test - - - - - org.objenesis - objenesis - - + 2.18.0 org.powermock - powermock-module-junit4 + powermock-api-mockito2 ${powermock.version} test org.powermock - powermock-api-mockito + powermock-module-junit4 ${powermock.version} test diff --git a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/JobBuilder.java b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/JobBuilder.java index 6e2ccba6..0ad3070d 100644 --- a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/JobBuilder.java +++ b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/JobBuilder.java @@ -11,10 +11,13 @@ */ public class JobBuilder { - public static , RunT extends Run> Job buildSampleJob() { - Job 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; diff --git a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/RunStub.java b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/RunStub.java index f63076cc..c8180c2c 100644 --- a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/RunStub.java +++ b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/RunStub.java @@ -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; @@ -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 { @@ -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); } @@ -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; @@ -57,4 +87,9 @@ public Result getResult() { public long getDuration() { return 0; } + + @Override + public boolean isBuilding() { + return false; + } } diff --git a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/actions/DeleteArtifactsActionTest.java b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/actions/DeleteArtifactsActionTest.java index fb30f9b4..626bd90a 100644 --- a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/actions/DeleteArtifactsActionTest.java +++ b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/actions/DeleteArtifactsActionTest.java @@ -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; @@ -23,6 +21,6 @@ public void perform_DeletesJob() throws IOException, InterruptedException { action.perform(run); // then - assertThat(run.deleteArtifactsTimes).isOne(); + run.assertArtifactsWereDeleted(); } } diff --git a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/actions/DeleteBuildActionIT.java b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/actions/DeleteBuildActionIT.java new file mode 100644 index 00000000..c640d006 --- /dev/null +++ b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/actions/DeleteBuildActionIT.java @@ -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 documentation + */ +@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 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()); + } +} diff --git a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/actions/DeleteBuildActionTest.java b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/actions/DeleteBuildActionTest.java index 0ef8875b..050890d8 100644 --- a/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/actions/DeleteBuildActionTest.java +++ b/src/test/java/pl/damianszczepanik/jenkins/buildhistorymanager/model/actions/DeleteBuildActionTest.java @@ -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; @@ -23,7 +21,6 @@ public void perform_DeletesJob() throws IOException, InterruptedException { action.perform(run); // then - assertThat(run.deleteTimes).isOne(); + run.assertBuildWasDeleted(); } } -