diff --git a/src/main/java/jenkins/plugins/logstash/LogstashConfiguration.java b/src/main/java/jenkins/plugins/logstash/LogstashConfiguration.java index 613d7f6a..a8e88ded 100644 --- a/src/main/java/jenkins/plugins/logstash/LogstashConfiguration.java +++ b/src/main/java/jenkins/plugins/logstash/LogstashConfiguration.java @@ -42,6 +42,7 @@ public class LogstashConfiguration extends GlobalConfiguration private Boolean enabled; private boolean dataMigrated = false; private boolean enableGlobally = false; + private boolean recordingPassingTests = false; private boolean milliSecondTimestamps = true; private transient LogstashIndexer activeIndexer; @@ -82,6 +83,16 @@ public void setEnableGlobally(boolean enableGlobally) this.enableGlobally = enableGlobally; } + public void setRecordingPassingTests(boolean recordingPassingTests) + { + this.recordingPassingTests = recordingPassingTests; + } + + public boolean isRecordingPassingTests() + { + return recordingPassingTests; + } + public boolean isMilliSecondTimestamps() { return milliSecondTimestamps; diff --git a/src/main/java/jenkins/plugins/logstash/persistence/BuildData.java b/src/main/java/jenkins/plugins/logstash/persistence/BuildData.java index d96cec29..bee86191 100644 --- a/src/main/java/jenkins/plugins/logstash/persistence/BuildData.java +++ b/src/main/java/jenkins/plugins/logstash/persistence/BuildData.java @@ -67,15 +67,20 @@ public class BuildData { private final static Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getCanonicalName()); public static class TestData { private int totalCount, skipCount, failCount, passCount; - private List failedTestsWithErrorDetail; + private List failedTestsWithErrorDetail; private List failedTests; - public static class FailedTest { + private List passedTestsWithErrorDetail; + private List passedTests; + + public static class ExecutedTest { private final String fullName, errorDetails; - public FailedTest(String fullName, String errorDetails) { + private final float duration; + public ExecutedTest(String fullName, String errorDetails, float duration) { super(); this.fullName = fullName; this.errorDetails = errorDetails; + this.duration = duration; } public String getFullName() @@ -103,6 +108,8 @@ public TestData(Action action) { totalCount = skipCount = failCount = 0; failedTests = Collections.emptyList(); failedTestsWithErrorDetail = Collections.emptyList(); + passedTests = Collections.emptyList(); + passedTestsWithErrorDetail = Collections.emptyList(); return; } @@ -112,10 +119,24 @@ public TestData(Action action) { passCount = totalCount - skipCount - failCount; failedTests = new ArrayList(); - failedTestsWithErrorDetail = new ArrayList(); - for (TestResult result : testResultAction.getFailedTests()) { - failedTests.add(result.getFullName()); - failedTestsWithErrorDetail.add(new FailedTest(result.getFullName(),result.getErrorDetails())); + failedTestsWithErrorDetail = new ArrayList(); + testListFill((List) testResultAction.getFailedTests(), failedTests, failedTestsWithErrorDetail); + + LogstashConfiguration configuration = LogstashConfiguration.getInstance(); + if (configuration.isrecordingPassingTests()) { + passedTests = new ArrayList(); + passedTestsWithErrorDetail = new ArrayList(); + testListFill((List) testResultAction.getPassedTests(), passedTests, passedTestsWithErrorDetail); + } else { + passedTests = Collections.emptyList(); + passedTestsWithErrorDetail = Collections.emptyList(); + } + } + + private void testListFill(List testResults, List testNames, List testDetails) { + for (TestResult result : testResults) { + testNames.add(result.getFullName()); + testDetails.add(new ExecutedTest(result.getFullName(),result.getErrorDetails(), result.getDuration())); } } @@ -139,11 +160,21 @@ public int getPassCount() return passCount; } - public List getFailedTestsWithErrorDetail() + public List getFailedTestsWithDetail() { return failedTestsWithErrorDetail; } + public List getPassedTestsWithDetail() + { + return passedTestsWithErrorDetail; + } + + public List getPassedTests() + { + return passedTests; + } + public List getFailedTests() { return failedTests; diff --git a/src/main/resources/jenkins/plugins/logstash/LogstashConfiguration/config.jelly b/src/main/resources/jenkins/plugins/logstash/LogstashConfiguration/config.jelly index 1ad826e2..5640d3f5 100644 --- a/src/main/resources/jenkins/plugins/logstash/LogstashConfiguration/config.jelly +++ b/src/main/resources/jenkins/plugins/logstash/LogstashConfiguration/config.jelly @@ -7,6 +7,9 @@ + + + diff --git a/src/test/java/jenkins/plugins/logstash/LogstashWriterTest.java b/src/test/java/jenkins/plugins/logstash/LogstashWriterTest.java index 36c54a95..c00b9ef3 100644 --- a/src/test/java/jenkins/plugins/logstash/LogstashWriterTest.java +++ b/src/test/java/jenkins/plugins/logstash/LogstashWriterTest.java @@ -128,6 +128,7 @@ public void before() throws Exception { when(mockTestResultAction.getTotalCount()).thenReturn(0); when(mockTestResultAction.getSkipCount()).thenReturn(0); when(mockTestResultAction.getFailCount()).thenReturn(0); + when(mockTestResultAction.getPassedTests()).thenReturn(Collections.emptyList()); when(mockTestResultAction.getFailedTests()).thenReturn(Collections.emptyList()); when(mockProject.getName()).thenReturn("LogstashWriterTest"); diff --git a/src/test/java/jenkins/plugins/logstash/persistence/BuildDataTest.java b/src/test/java/jenkins/plugins/logstash/persistence/BuildDataTest.java index 2a9b8e60..ccfd9d22 100644 --- a/src/test/java/jenkins/plugins/logstash/persistence/BuildDataTest.java +++ b/src/test/java/jenkins/plugins/logstash/persistence/BuildDataTest.java @@ -59,6 +59,7 @@ public class BuildDataTest { + "\"rootProjectName\":\"RootBuildDataTest\",\"rootFullProjectName\":\"parent/RootBuildDataTest\"," + "\"rootProjectDisplayName\":\"Root BuildData Test\",\"rootBuildNum\":456,\"buildVariables\":{}," + "\"sensitiveBuildVariables\":[],\"testResults\":{\"totalCount\":0,\"skipCount\":0,\"failCount\":0, \"passCount\":0," + + "\"passedTests\":[], \"passedTestsWithErrorDetail\":[]," + "\"failedTests\":[], \"failedTestsWithErrorDetail\":[]}}"; @Mock AbstractBuild mockBuild; @@ -99,6 +100,7 @@ public void before() throws Exception { when(mockTestResultAction.getTotalCount()).thenReturn(0); when(mockTestResultAction.getSkipCount()).thenReturn(0); when(mockTestResultAction.getFailCount()).thenReturn(0); + when(mockTestResultAction.getPassedTests()).thenReturn(Collections.emptyList()); when(mockTestResultAction.getFailedTests()).thenReturn(Collections.emptyList()); when(mockProject.getName()).thenReturn("BuildDataTest"); @@ -247,6 +249,7 @@ public void constructorSuccessTestFailures() throws Exception { when(mockTestResultAction.getSkipCount()).thenReturn(0); when(mockTestResultAction.getFailCount()).thenReturn(1); when(mockTestResultAction.getFailedTests()).thenReturn(Arrays.asList(mockTestResult)); + when(mockTestResultAction.getPassedTests()).thenReturn(Arrays.asList(mockTestResult)); // Unit under test BuildData buildData = new BuildData(mockBuild, mockDate, mockListener); @@ -256,9 +259,36 @@ public void constructorSuccessTestFailures() throws Exception { Assert.assertEquals("Incorrect test results", 123, testResults.getTotalCount()); Assert.assertEquals("Incorrect test results", 0, testResults.getSkipCount()); Assert.assertEquals("Incorrect test results", 1, testResults.getFailCount()); - Assert.assertEquals("Incorrect test details count", 1, testResults.getFailedTestsWithErrorDetail().size()); - Assert.assertEquals("Incorrect failed test error details", "ErrorDetails Test", testResults.getFailedTestsWithErrorDetail().get(0).getErrorDetails()); - Assert.assertEquals("Incorrect failed test fullName", "Mock Full Test", testResults.getFailedTestsWithErrorDetail().get(0).getFullName()); + Assert.assertEquals("Incorrect test details count", 1, testResults.getFailedTestsWithDetail().size()); + Assert.assertEquals("Incorrect failed test error details", "ErrorDetails Test", testResults.getFailedTestsWithDetail().get(0).getErrorDetails()); + Assert.assertEquals("Incorrect failed test fullName", "Mock Full Test", testResults.getFailedTestsWithDetail().get(0).getFullName()); + + + verifyMocks(); + verifyTestResultActions(); + } + + @Test + public void constructorSuccessTestPasses() throws Exception { + TestResult mockTestResult = Mockito.mock(hudson.tasks.test.TestResult.class); + when(mockTestResult.getFullName()).thenReturn("Mock Full Test"); + when(mockTestResult.getErrorDetails()).thenReturn(""); + + when(mockTestResultAction.getTotalCount()).thenReturn(123); + when(mockTestResultAction.getSkipCount()).thenReturn(0); + when(mockTestResultAction.getFailCount()).thenReturn(0); + when(mockTestResultAction.getFailedTests()).thenReturn(Arrays.asList(mockTestResult)); + + // Unit under test + BuildData buildData = new BuildData(mockBuild, mockDate, mockListener); + + TestData testResults = buildData.getTestResults(); + + Assert.assertEquals("Incorrect test results", 123, testResults.getTotalCount()); + Assert.assertEquals("Incorrect test results", 0, testResults.getSkipCount()); + Assert.assertEquals("Incorrect test results", 0, testResults.getFailCount()); + Assert.assertEquals("Incorrect test results", 123, testResults.getPassCount()); + Assert.assertEquals("Incorrect test details count", 1, testResults.getFailedTestsWithDetail().size()); verifyMocks(); verifyTestResultActions();