2020-03-30 15:59:59 +02:00
|
|
|
import com.sap.piper.ReportAggregator
|
|
|
|
import org.hamcrest.Matcher
|
2020-03-12 16:45:57 +02:00
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.ExpectedException
|
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
import util.BasePiperTest
|
|
|
|
import util.JenkinsCredentialsRule
|
|
|
|
import util.JenkinsFileExistsRule
|
|
|
|
import util.JenkinsReadJsonRule
|
|
|
|
import util.JenkinsReadYamlRule
|
|
|
|
import util.JenkinsShellCallRule
|
|
|
|
import util.JenkinsStepRule
|
|
|
|
import util.JenkinsWriteFileRule
|
|
|
|
import util.Rules
|
|
|
|
|
|
|
|
import static org.hamcrest.Matchers.allOf
|
|
|
|
import static org.hamcrest.Matchers.containsString
|
2020-03-30 15:59:59 +02:00
|
|
|
import static org.hamcrest.Matchers.hasItems
|
2020-03-12 16:45:57 +02:00
|
|
|
import static org.hamcrest.Matchers.is
|
|
|
|
import static org.hamcrest.Matchers.startsWith
|
2020-03-30 15:59:59 +02:00
|
|
|
import static org.junit.Assert.assertFalse
|
2020-03-12 16:45:57 +02:00
|
|
|
import static org.junit.Assert.assertThat
|
2020-03-30 15:59:59 +02:00
|
|
|
import static org.junit.Assert.assertTrue
|
2020-03-12 16:45:57 +02:00
|
|
|
|
|
|
|
class MavenExecuteStaticCodeChecksTest extends BasePiperTest {
|
|
|
|
private ExpectedException exception = ExpectedException.none()
|
|
|
|
private JenkinsCredentialsRule credentialsRule = new JenkinsCredentialsRule(this)
|
|
|
|
private JenkinsReadJsonRule readJsonRule = new JenkinsReadJsonRule(this)
|
|
|
|
private JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
|
|
|
|
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
|
|
|
|
private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this)
|
|
|
|
private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule(this, [])
|
|
|
|
|
|
|
|
private List withEnvArgs = []
|
2020-03-30 15:59:59 +02:00
|
|
|
private boolean spotBugsStepCalled = false
|
|
|
|
private boolean pmdParserStepCalled = false
|
2020-03-12 16:45:57 +02:00
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain rules = Rules
|
|
|
|
.getCommonRules(this)
|
|
|
|
.around(exception)
|
|
|
|
.around(new JenkinsReadYamlRule(this))
|
|
|
|
.around(credentialsRule)
|
|
|
|
.around(readJsonRule)
|
|
|
|
.around(shellCallRule)
|
|
|
|
.around(stepRule)
|
|
|
|
.around(writeFileRule)
|
|
|
|
.around(fileExistsRule)
|
2020-03-30 15:59:59 +02:00
|
|
|
|
2020-03-12 16:45:57 +02:00
|
|
|
@Before
|
|
|
|
void init() {
|
|
|
|
helper.registerAllowedMethod("withEnv", [List.class, Closure.class], { arguments, closure ->
|
|
|
|
arguments.each { arg ->
|
|
|
|
withEnvArgs.add(arg.toString())
|
|
|
|
}
|
|
|
|
return closure()
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod("dockerExecute", [Map.class, Closure.class], {
|
|
|
|
Map params, Closure c ->
|
2020-03-30 15:59:59 +02:00
|
|
|
c.call()
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod("recordIssues", [Map.class], { Map config
|
|
|
|
->
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod("spotBugs", [Map.class], { Map config
|
|
|
|
-> spotBugsStepCalled = true
|
2020-03-12 16:45:57 +02:00
|
|
|
})
|
2020-03-30 15:59:59 +02:00
|
|
|
helper.registerAllowedMethod("pmdParser", [Map.class], { Map config
|
|
|
|
-> pmdParserStepCalled = true
|
|
|
|
})
|
|
|
|
|
2020-03-23 10:58:54 +02:00
|
|
|
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/mavenStaticCodeChecks.yaml\'', '{"dockerImage": "maven:3.6-jdk-8"}')
|
2020-04-24 22:41:34 +02:00
|
|
|
|
|
|
|
helper.registerAllowedMethod('findFiles', [Map.class], {return null})
|
2021-06-15 14:34:56 +02:00
|
|
|
helper.registerAllowedMethod("writePipelineEnv", [Map.class], {m -> return })
|
|
|
|
helper.registerAllowedMethod("readPipelineEnv", [Map.class], {m -> return })
|
2020-03-12 16:45:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
2020-03-30 15:59:59 +02:00
|
|
|
void 'MavenExecuteStaticCodeChecks should be executed, results recorded and reported in Reportaggregator'() {
|
|
|
|
spotBugsStepCalled = false
|
|
|
|
pmdParserStepCalled = false
|
|
|
|
|
|
|
|
nullScript.commonPipelineEnvironment.configuration = [steps: [:]]
|
2020-03-12 16:45:57 +02:00
|
|
|
stepRule.step.mavenExecuteStaticCodeChecks(
|
|
|
|
juStabUtils: utils,
|
|
|
|
jenkinsUtilsStub: jenkinsUtils,
|
|
|
|
testParam: "This is test content",
|
|
|
|
script: nullScript
|
|
|
|
)
|
2020-03-30 15:59:59 +02:00
|
|
|
|
|
|
|
assertThat(writeFileRule.files['.pipeline/tmp/metadata/mavenStaticCodeChecks.yaml'], containsString('name: mavenExecuteStaticCodeChecks'))
|
|
|
|
assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'), containsString('"testParam":"This is test content"')))
|
2020-03-31 12:19:05 +02:00
|
|
|
assertThat(shellCallRule.shell[shellCallRule.shell.size() -1], is('./piper mavenExecuteStaticCodeChecks'))
|
2020-03-30 15:59:59 +02:00
|
|
|
assertTrue(spotBugsStepCalled)
|
|
|
|
assertTrue(pmdParserStepCalled)
|
|
|
|
assertThat(ReportAggregator.instance.staticCodeScans, hasItems("Findbugs Static Code Checks", "PMD Static Code Checks"))
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void 'MavenExecuteStaticCodeChecks should not record results and not report in Reportaggregator when turned off'() {
|
|
|
|
spotBugsStepCalled = false
|
|
|
|
pmdParserStepCalled = false
|
|
|
|
|
|
|
|
nullScript.commonPipelineEnvironment.configuration = [steps: [mavenExecuteStaticCodeChecks: [spotBugs: false, pmd: false]]]
|
|
|
|
stepRule.step.mavenExecuteStaticCodeChecks(
|
|
|
|
juStabUtils: utils,
|
|
|
|
jenkinsUtilsStub: jenkinsUtils,
|
|
|
|
testParam: "This is test content",
|
|
|
|
script: nullScript
|
|
|
|
)
|
|
|
|
|
2020-03-23 10:58:54 +02:00
|
|
|
assertThat(writeFileRule.files['.pipeline/tmp/metadata/mavenStaticCodeChecks.yaml'], containsString('name: mavenExecuteStaticCodeChecks'))
|
2020-03-12 16:45:57 +02:00
|
|
|
assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'), containsString('"testParam":"This is test content"')))
|
2020-03-31 12:19:05 +02:00
|
|
|
assertThat(shellCallRule.shell[shellCallRule.shell.size() -1], is('./piper mavenExecuteStaticCodeChecks'))
|
2020-03-30 15:59:59 +02:00
|
|
|
assertFalse(spotBugsStepCalled)
|
|
|
|
assertFalse(pmdParserStepCalled)
|
|
|
|
assertTrue(ReportAggregator.instance.staticCodeScans.isEmpty())
|
2020-03-12 16:45:57 +02:00
|
|
|
}
|
|
|
|
}
|