github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/CheckmarxExecuteScanTest.groovy (about) 1 import groovy.json.JsonSlurper 2 import hudson.AbortException 3 import org.junit.Before 4 import org.junit.Rule 5 import org.junit.Test 6 import org.junit.rules.ExpectedException 7 import org.junit.rules.RuleChain 8 import util.* 9 10 import static org.hamcrest.Matchers.* 11 import static org.junit.Assert.assertThat 12 13 class CheckmarxExecuteScanTest extends BasePiperTest { 14 private ExpectedException exception = ExpectedException.none() 15 16 private JenkinsCredentialsRule credentialsRule = new JenkinsCredentialsRule(this) 17 private JenkinsReadJsonRule readJsonRule = new JenkinsReadJsonRule(this) 18 private JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this) 19 private JenkinsStepRule stepRule = new JenkinsStepRule(this) 20 private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this) 21 private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule(this, []) 22 23 private List withEnvArgs = [] 24 25 @Rule 26 public RuleChain rules = Rules 27 .getCommonRules(this) 28 .around(exception) 29 .around(new JenkinsReadYamlRule(this)) 30 .around(credentialsRule) 31 .around(readJsonRule) 32 .around(shellCallRule) 33 .around(stepRule) 34 .around(writeFileRule) 35 .around(fileExistsRule) 36 37 @Before 38 void init() { 39 helper.registerAllowedMethod('fileExists', [Map], { 40 return true 41 }) 42 helper.registerAllowedMethod("readJSON", [Map], { m -> 43 if(m.file == 'checkmarxExecuteScan_reports.json') 44 return [[target: "1234.pdf", mandatory: true]] 45 if(m.file == 'checkmarxExecuteScan_links.json') 46 return [] 47 if(m.text != null) 48 return new JsonSlurper().parseText(m.text) 49 }) 50 helper.registerAllowedMethod("withEnv", [List.class, Closure.class], {arguments, closure -> 51 arguments.each {arg -> 52 withEnvArgs.add(arg.toString()) 53 } 54 return closure() 55 }) 56 credentialsRule.withCredentials('idOfCxCredential', "PIPER_username", "PIPER_password") 57 shellCallRule.setReturnValue('[ -x ./piper ]', 1) 58 shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/checkmarxExecuteScan.yaml\'', '{"checkmarxCredentialsId": "idOfCxCredential", "verbose": false}') 59 60 helper.registerAllowedMethod('findFiles', [Map.class], {return null}) 61 helper.registerAllowedMethod("writePipelineEnv", [Map.class], {m -> return }) 62 helper.registerAllowedMethod("readPipelineEnv", [Map.class], {m -> return }) 63 } 64 65 @Test 66 void testCheckmarxExecuteScanDefault() { 67 stepRule.step.checkmarxExecuteScan( 68 juStabUtils: utils, 69 jenkinsUtilsStub: jenkinsUtils, 70 testParam: "This is test content", 71 script: nullScript 72 ) 73 // asserts 74 assertThat(writeFileRule.files['.pipeline/tmp/metadata/checkmarxExecuteScan.yaml'], containsString('name: checkmarxExecuteScan')) 75 assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'), containsString('"testParam":"This is test content"'))) 76 assertThat(shellCallRule.shell[2], is('./piper checkmarxExecuteScan')) 77 } 78 79 @Test 80 void testCheckmarxExecuteScanNoReports() { 81 helper.registerAllowedMethod('fileExists', [Map], { 82 return false 83 }) 84 85 exception.expect(AbortException) 86 exception.expectMessage("Expected to find checkmarxExecuteScan_reports.json in workspace but it is not there") 87 88 stepRule.step.checkmarxExecuteScan( 89 juStabUtils: utils, 90 jenkinsUtilsStub: jenkinsUtils, 91 testParam: "This is test content", 92 script: nullScript 93 ) 94 } 95 }