Skip to content

Commit

Permalink
Migrate tests to JUnit5
Browse files Browse the repository at this point in the history
* Migrate annotations
* Migrate assertions
* Cleanup assertions
* Remove public visibility of test classes and methods
  • Loading branch information
strangelookingnerd authored and a-st committed Jan 21, 2025
1 parent c2b85fe commit f8e8deb
Show file tree
Hide file tree
Showing 39 changed files with 894 additions and 954 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@
<artifactId>mockito-core</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-csv</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
package hudson.plugins.performance;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletOutputStream;

import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.plugins.performance.actions.PerformanceBuildAction;
import hudson.plugins.performance.parsers.JMeterTestHelper;
import hudson.plugins.performance.reports.PerformanceReport;
import hudson.util.DescribableList;
import org.jfree.data.category.CategoryDataset;
import org.junit.Before;
import org.junit.jupiter.api.BeforeEach;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import org.mockito.Mock;
import org.mockito.quality.Strictness;

import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.plugins.performance.parsers.JMeterTestHelper;
import hudson.plugins.performance.reports.PerformanceReport;
import hudson.util.DescribableList;
import javax.servlet.ServletOutputStream;
import java.util.ArrayList;
import java.util.List;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.withSettings;


public abstract class AbstractGraphGenerationTest {

@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
protected PerformanceBuildAction performanceBuildAction;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
protected AbstractProject project;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
protected Run build;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
protected StaplerRequest request;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
protected StaplerResponse response;

protected PerformanceReport report;

@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
protected PerformanceReportMap reportMap;

@Before
@BeforeEach
public void baseSetup() throws Exception {
report = JMeterTestHelper.parse("/JMeterResults.jtl");
report.setBuildAction(performanceBuildAction);
Expand All @@ -55,9 +55,9 @@ public void baseSetup() throws Exception {
}

protected void setGraphType(String graphType) {
DescribableList list = mock(DescribableList.class);
DescribableList list = mock(DescribableList.class, withSettings().strictness(Strictness.LENIENT));
when(project.getPublishersList()).thenReturn(list);
PerformancePublisher publisher = mock(PerformancePublisher.class);
PerformancePublisher publisher = mock(PerformancePublisher.class, withSettings().strictness(Strictness.LENIENT));
when(list.get(PerformancePublisher.class)).thenReturn(publisher);
when(publisher.getGraphType()).thenReturn(graphType);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,46 @@
import hudson.plugins.performance.actions.PerformanceBuildAction;
import hudson.plugins.performance.data.TaurusFinalStats;
import hudson.plugins.performance.reports.PerformanceReport;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.Silent.class)
public class BaselineComparisonTest extends AbstractGraphGenerationTest {
private final static String LABEL = "BlazeDemo";
@ExtendWith(MockitoExtension.class)
class BaselineComparisonTest extends AbstractGraphGenerationTest {
private static final String LABEL = "BlazeDemo";

@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
protected Run prevBuild;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
protected PerformanceBuildAction prevAction;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
protected PerformanceReportMap prevReportMap;


@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
protected Run buildWithNumber3;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
protected PerformanceBuildAction actionWithNumber3;
@Mock
@Mock(strictness = Mock.Strictness.LENIENT)
protected PerformanceReportMap reportMapWithNumber3;

private Map<String, PerformanceReport> reportMapForBuild = new HashMap<>();
private Map<String, PerformanceReport> reportMapForPreviousBuild = new HashMap<>();
private Map<String, PerformanceReport> reportMapForBuildNumber3 = new HashMap<>();


@Before
public void setup() {
@BeforeEach
void setup() {
when(build.getPreviousBuild()).thenReturn(prevBuild);
when(prevBuild.getPreviousBuild()).thenReturn(buildWithNumber3);
when(prevBuild.getPreviousCompletedBuild()).thenReturn(buildWithNumber3);
Expand Down Expand Up @@ -87,7 +87,7 @@ private void prepareReportForBuild(int num, int baseline, Map<String, Performanc
}

@Test
public void testBaselineBuild5() throws Exception {
void testBaselineBuild5() throws Exception {
new PerformanceReportMap(performanceBuildAction, mock(TaskListener.class)) {
@Override
protected void parseReports(Run<?, ?> build, TaskListener listener, PerformanceReportCollector collector, String filename) throws IOException {
Expand Down Expand Up @@ -119,7 +119,7 @@ protected void parseReports(Run<?, ?> build, TaskListener listener, PerformanceR
}

@Test
public void testBaselineBuild4() throws Exception {
void testBaselineBuild4() throws Exception {
new PerformanceReportMap(prevAction, mock(TaskListener.class)) {
@Override
protected void parseReports(Run<?, ?> build, TaskListener listener, PerformanceReportCollector collector, String filename) throws IOException {
Expand Down Expand Up @@ -151,7 +151,7 @@ protected void parseReports(Run<?, ?> build, TaskListener listener, PerformanceR
}

@Test
public void testBaselineBuild3() throws Exception {
void testBaselineBuild3() throws Exception {
new PerformanceReportMap(actionWithNumber3, mock(TaskListener.class)) {
@Override
protected void parseReports(Run<?, ?> build, TaskListener listener, PerformanceReportCollector collector, String filename) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,47 @@
package hudson.plugins.performance;

import java.net.URL;
import java.nio.charset.StandardCharsets;

import hudson.model.Result;
import hudson.slaves.DumbSlave;
import org.apache.commons.io.IOUtils;
import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.BuildWatcher;
import org.junit.jupiter.api.Test;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.RestartableJenkinsRule;
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;

import hudson.model.Result;
import hudson.slaves.DumbSlave;

public class PerformancePipelineTest {
@ClassRule
public static BuildWatcher buildWatcher = new BuildWatcher();
import java.net.URL;
import java.nio.charset.StandardCharsets;

@Rule
public RestartableJenkinsRule story = new RestartableJenkinsRule();
@WithJenkins
class PerformancePipelineTest {

@Test
public void bztSmokeTests() throws Exception {
void bztSmokeTests(JenkinsRule rule) throws Exception {
final String path = getClass().getResource("/performanceTest.yml").getPath();
story.then(step -> {
DumbSlave s = story.j.createOnlineSlave();
s.setLabelString("test performance test ");
WorkflowJob p = story.j.createProject(WorkflowJob.class, "demo job");
p.getRootDir().mkdirs();
String bztParams = path + ' ' + "-o modules.jmeter.plugins=[] -o services=[] " +
"-o \\'reporting.-1={module: \"junit-xml\", filename: \"report.xml\"}\\' " +
"-o \\'execution.0.scenario.requests.1={url: \"http://blazedemo.com/\": assert: [\"yo mamma\"]}\\'";
p.setDefinition(new CpsFlowDefinition(
"node('" + story.j.jenkins.getSelfLabel().getName() + "'){ bzt(params: '" + bztParams
+ "', useSystemSitePackages: false, printDebugOutput: true, bztVersion: '1.16.19') }",
true));
WorkflowRun r = p.scheduleBuild2(0).waitForStart();
story.j.assertBuildStatusSuccess(story.j.waitForCompletion(r));
story.j.assertLogContains("Writing JUnit XML report into: report.xml", r);
story.j.assertLogContains("File aggregate-results.xml reported", r);
story.j.assertLogContains("of errors [SUCCESS].", r);
if (JenkinsRule.getLog(r).contains("Performance test: Installing bzt into 'taurus-venv'")) {
story.j.assertLogContains("Taurus CLI Tool v1.16.19", r);
}
});
DumbSlave s = rule.createOnlineSlave();
s.setLabelString("test performance test ");
WorkflowJob p = rule.createProject(WorkflowJob.class, "demo job");
p.getRootDir().mkdirs();
String bztParams = path + ' ' + "-o modules.jmeter.plugins=[] -o services=[] " +
"-o \\'reporting.-1={module: \"junit-xml\", filename: \"report.xml\"}\\' " +
"-o \\'execution.0.scenario.requests.1={url: \"http://blazedemo.com/\": assert: [\"yo mamma\"]}\\'";
p.setDefinition(new CpsFlowDefinition(
"node('" + rule.jenkins.getSelfLabel().getName() + "'){ bzt(params: '" + bztParams
+ "', useSystemSitePackages: false, printDebugOutput: true, bztVersion: '1.16.19') }",
true));
WorkflowRun r = p.scheduleBuild2(0).waitForStart();
rule.assertBuildStatusSuccess(rule.waitForCompletion(r));
rule.assertLogContains("Writing JUnit XML report into: report.xml", r);
rule.assertLogContains("File aggregate-results.xml reported", r);
rule.assertLogContains("of errors [SUCCESS].", r);
if (JenkinsRule.getLog(r).contains("Performance test: Installing bzt into 'taurus-venv'")) {
rule.assertLogContains("Taurus CLI Tool v1.16.19", r);
}
}

@Test
public void perfReportSmokeTests() throws Exception {
void perfReportSmokeTests(JenkinsRule rule) throws Exception {
String fileContents = null;

URL url = getClass().getResource("/TaurusXMLReport.xml");
Expand All @@ -61,18 +51,16 @@ public void perfReportSmokeTests() throws Exception {

final String report = fileContents;

story.then(step -> {
DumbSlave s = story.j.createOnlineSlave();
s.setLabelString("test performance report DSL function");
WorkflowJob p = story.j.createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(
"node{ writeFile file: 'test.xml', text: '''" + report
+ "'''; perfReport errorFailedThreshold: 0, errorUnstableThreshold: 0, sourceDataFiles: 'test.xml' }",
true));
WorkflowRun r = p.scheduleBuild2(0).waitForStart();
story.j.assertBuildStatus(Result.FAILURE, story.j.waitForCompletion(r));
story.j.assertLogContains("File test.xml reported 1.625% of errors [FAILURE]. Build status is: FAILURE", r);
});
DumbSlave s = rule.createOnlineSlave();
s.setLabelString("test performance report DSL function");
WorkflowJob p = rule.createProject(WorkflowJob.class, "demo");
p.setDefinition(new CpsFlowDefinition(
"node{ writeFile file: 'test.xml', text: '''" + report
+ "'''; perfReport errorFailedThreshold: 0, errorUnstableThreshold: 0, sourceDataFiles: 'test.xml' }",
true));
WorkflowRun r = p.scheduleBuild2(0).waitForStart();
rule.assertBuildStatus(Result.FAILURE, rule.waitForCompletion(r));
rule.assertLogContains("File test.xml reported 1.625% of errors [FAILURE]. Build status is: FAILURE", r);
}

}
Loading

0 comments on commit f8e8deb

Please sign in to comment.