github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/CloudFoundryDeployTest.groovy (about) 1 import org.junit.Before 2 import org.junit.Rule 3 import org.junit.Test 4 import org.junit.rules.RuleChain 5 import util.BasePiperTest 6 import util.JenkinsCredentialsRule 7 import util.JenkinsStepRule 8 import util.Rules 9 import static org.junit.Assert.assertEquals 10 import static org.junit.Assert.assertTrue 11 12 class CloudFoundryDeployTest extends BasePiperTest { 13 14 private JenkinsStepRule stepRule = new JenkinsStepRule(this) 15 private JenkinsCredentialsRule credentialsRule = new JenkinsCredentialsRule(this) 16 17 @Rule 18 public RuleChain rules = Rules 19 .getCommonRules(this) 20 .around(credentialsRule) 21 .around(stepRule) // needs to be activated after dockerExecuteRule, otherwise executeDocker is not mocked 22 23 @Before 24 void init() { 25 // removing additional credentials tests might have added; adding default credentials 26 credentialsRule.reset() 27 .withCredentials('test_cfCredentialsId', 'test_cf', '********') 28 } 29 30 @Test 31 void testGoStepWithMtaExtensionCredentialsFromParams() { 32 String calledStep = '' 33 String usedMetadataFile = '' 34 List credInfo = [] 35 helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], { 36 Map parameters, String stepName, 37 String metadataFile, List credentialInfo -> 38 calledStep = stepName 39 usedMetadataFile = metadataFile 40 credInfo = credentialInfo 41 }) 42 43 stepRule.step.cloudFoundryDeploy([ 44 script : nullScript, 45 juStabUtils : utils, 46 useGoStep : true, 47 mtaExtensionCredentials: [myCred: 'Mta.ExtensionCredential~Credential_Id1'], 48 ]) 49 50 assertEquals('cloudFoundryDeploy', calledStep) 51 assertEquals('metadata/cloudFoundryDeploy.yaml', usedMetadataFile) 52 53 // contains assertion does not work apparently when comparing a list of lists agains an expected list. 54 boolean found = false 55 credInfo.each { entry -> 56 if (entry == [type: 'token', id: 'Mta.ExtensionCredential~Credential_Id1', env: ['MTA_EXTENSION_CREDENTIAL_CREDENTIAL_ID1'], resolveCredentialsId: false]) { 57 found = true 58 } 59 } 60 assertTrue(found) 61 } 62 63 @Test 64 void testGoStepWithMtaExtensionCredentialsFromConfig() { 65 String calledStep = '' 66 String usedMetadataFile = '' 67 List credInfo = [] 68 helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], { 69 Map parameters, String stepName, 70 String metadataFile, List credentialInfo -> 71 calledStep = stepName 72 usedMetadataFile = metadataFile 73 credInfo = credentialInfo 74 }) 75 76 nullScript.commonPipelineEnvironment.configuration = [steps:[cloudFoundryDeploy:[ 77 mtaExtensionCredentials: [myCred: 'Mta.ExtensionCredential~Credential_Id1'] 78 ]]] 79 80 stepRule.step.cloudFoundryDeploy([ 81 script : nullScript, 82 juStabUtils : utils, 83 useGoStep : true, 84 ]) 85 86 assertEquals('cloudFoundryDeploy', calledStep) 87 assertEquals('metadata/cloudFoundryDeploy.yaml', usedMetadataFile) 88 89 // contains assertion does not work apparently when comparing a list of lists agains an expected list. 90 boolean found = false 91 credInfo.each { entry -> 92 if (entry == [type: 'token', id: 'Mta.ExtensionCredential~Credential_Id1', env: ['MTA_EXTENSION_CREDENTIAL_CREDENTIAL_ID1'], resolveCredentialsId: false]) { 93 found = true 94 } 95 } 96 assertTrue(found) 97 } 98 }