2020-01-28 00:40:53 +02:00
|
|
|
import groovy.json.JsonSlurper
|
2020-02-03 16:25:49 +02:00
|
|
|
import hudson.AbortException
|
2020-01-28 00:40:53 +02:00
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
2020-02-03 16:25:49 +02:00
|
|
|
import org.junit.rules.ExpectedException
|
2020-01-28 00:40:53 +02:00
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
import util.*
|
|
|
|
|
|
|
|
import static org.hamcrest.Matchers.*
|
|
|
|
import static org.junit.Assert.assertThat
|
|
|
|
|
|
|
|
class CheckmarxExecuteScanTest extends BasePiperTest {
|
2020-02-03 16:25:49 +02:00
|
|
|
private ExpectedException exception = ExpectedException.none()
|
2020-01-28 00:40:53 +02:00
|
|
|
|
|
|
|
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 = []
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain rules = Rules
|
|
|
|
.getCommonRules(this)
|
2020-02-03 16:25:49 +02:00
|
|
|
.around(exception)
|
2020-01-28 00:40:53 +02:00
|
|
|
.around(new JenkinsReadYamlRule(this))
|
|
|
|
.around(credentialsRule)
|
|
|
|
.around(readJsonRule)
|
|
|
|
.around(shellCallRule)
|
|
|
|
.around(stepRule)
|
|
|
|
.around(writeFileRule)
|
|
|
|
.around(fileExistsRule)
|
|
|
|
|
|
|
|
@Before
|
|
|
|
void init() {
|
2020-02-03 16:25:49 +02:00
|
|
|
helper.registerAllowedMethod('fileExists', [Map], {
|
|
|
|
return true
|
|
|
|
})
|
2020-01-28 00:40:53 +02:00
|
|
|
helper.registerAllowedMethod("readJSON", [Map], { m ->
|
2020-02-03 16:25:49 +02:00
|
|
|
if(m.file == 'checkmarxExecuteScan_reports.json')
|
2020-01-28 00:40:53 +02:00
|
|
|
return [[target: "1234.pdf", mandatory: true]]
|
2020-02-03 16:25:49 +02:00
|
|
|
if(m.file == 'checkmarxExecuteScan_links.json')
|
2020-01-28 00:40:53 +02:00
|
|
|
return []
|
|
|
|
if(m.text != null)
|
|
|
|
return new JsonSlurper().parseText(m.text)
|
|
|
|
})
|
|
|
|
helper.registerAllowedMethod("withEnv", [List.class, Closure.class], {arguments, closure ->
|
|
|
|
arguments.each {arg ->
|
|
|
|
withEnvArgs.add(arg.toString())
|
|
|
|
}
|
|
|
|
return closure()
|
|
|
|
})
|
|
|
|
credentialsRule.withCredentials('idOfCxCredential', "PIPER_username", "PIPER_password")
|
2020-03-17 10:19:09 +02:00
|
|
|
shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/checkmarx.yaml\'', '{"checkmarxCredentialsId": "idOfCxCredential", "verbose": false}')
|
2020-04-24 22:41:34 +02:00
|
|
|
|
|
|
|
helper.registerAllowedMethod('findFiles', [Map.class], {return null})
|
2020-01-28 00:40:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void testCheckmarxExecuteScanDefault() {
|
|
|
|
stepRule.step.checkmarxExecuteScan(
|
|
|
|
juStabUtils: utils,
|
|
|
|
jenkinsUtilsStub: jenkinsUtils,
|
|
|
|
testParam: "This is test content",
|
|
|
|
script: nullScript
|
|
|
|
)
|
|
|
|
// asserts
|
2020-03-17 10:19:09 +02:00
|
|
|
assertThat(writeFileRule.files['.pipeline/tmp/metadata/checkmarx.yaml'], containsString('name: checkmarxExecuteScan'))
|
2020-01-28 00:40:53 +02:00
|
|
|
assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'), containsString('"testParam":"This is test content"')))
|
|
|
|
assertThat(shellCallRule.shell[1], is('./piper checkmarxExecuteScan'))
|
|
|
|
}
|
2020-02-03 16:25:49 +02:00
|
|
|
|
|
|
|
@Test
|
|
|
|
void testCheckmarxExecuteScanNoReports() {
|
|
|
|
helper.registerAllowedMethod('fileExists', [Map], {
|
|
|
|
return false
|
|
|
|
})
|
|
|
|
|
|
|
|
exception.expect(AbortException)
|
|
|
|
exception.expectMessage("Expected to find checkmarxExecuteScan_reports.json in workspace but it is not there")
|
|
|
|
|
|
|
|
stepRule.step.checkmarxExecuteScan(
|
|
|
|
juStabUtils: utils,
|
|
|
|
jenkinsUtilsStub: jenkinsUtils,
|
|
|
|
testParam: "This is test content",
|
|
|
|
script: nullScript
|
|
|
|
)
|
|
|
|
}
|
2020-01-28 00:40:53 +02:00
|
|
|
}
|