github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/templates/AbapEnvironmentPipelineStageIntegrationTestsTest.groovy (about) 1 package templates 2 3 import org.junit.Before 4 import org.junit.Rule 5 import org.junit.Test 6 import org.junit.rules.RuleChain 7 import util.BasePiperTest 8 import util.JenkinsReadYamlRule 9 import util.JenkinsStepRule 10 import util.PipelineWhenException 11 import util.Rules 12 13 import static org.hamcrest.Matchers.* 14 import static org.junit.Assert.assertThat 15 import static org.hamcrest.Matchers.equalTo; 16 import static org.hamcrest.Matchers.is; 17 import static org.junit.Assert.fail 18 19 class abapEnvironmentPipelineStageIntegrationTestsTest extends BasePiperTest { 20 private JenkinsStepRule jsr = new JenkinsStepRule(this) 21 22 @Rule 23 public RuleChain rules = Rules 24 .getCommonRules(this) 25 .around(new JenkinsReadYamlRule(this)) 26 .around(jsr) 27 28 private stepsCalled = [] 29 30 @Before 31 void init() { 32 binding.variables.env.STAGE_NAME = 'Integration Tests' 33 34 helper.registerAllowedMethod('piperStageWrapper', [Map.class, Closure.class], {m, body -> 35 assertThat(m.stageName, is('Integration Tests')) 36 return body() 37 }) 38 helper.registerAllowedMethod('input', [Map], {m -> 39 stepsCalled.add('input') 40 return null 41 }) 42 helper.registerAllowedMethod('abapEnvironmentCreateSystem', [Map.class], {m -> stepsCalled.add('abapEnvironmentCreateSystem')}) 43 helper.registerAllowedMethod('cloudFoundryDeleteService', [Map.class], {m -> stepsCalled.add('cloudFoundryDeleteService')}) 44 helper.registerAllowedMethod('abapEnvironmentBuild', [Map.class], {m -> stepsCalled.add('abapEnvironmentBuild')}) 45 helper.registerAllowedMethod('cloudFoundryCreateServiceKey', [Map.class], {m -> stepsCalled.add('cloudFoundryCreateServiceKey')}) 46 } 47 48 @Test 49 void testCloudFoundryDeleteServiceExecutedConfirm() { 50 51 nullScript.commonPipelineEnvironment.configuration.runStage = [ 52 'Integration Tests': true 53 ] 54 jsr.step.abapEnvironmentPipelineStageIntegrationTests(script: nullScript, confirmDeletion: true) 55 56 assertThat(stepsCalled, hasItems('input')) 57 assertThat(stepsCalled, hasItems('abapEnvironmentCreateSystem')) 58 assertThat(stepsCalled, hasItems('cloudFoundryDeleteService')) 59 assertThat(stepsCalled, hasItems('abapEnvironmentBuild')) 60 assertThat(stepsCalled, hasItems('cloudFoundryCreateServiceKey')) 61 } 62 63 @Test 64 void testCloudFoundryDeleteServiceExecutedNoConfirm() { 65 66 nullScript.commonPipelineEnvironment.configuration.runStage = [ 67 'Integration Tests': true 68 ] 69 jsr.step.abapEnvironmentPipelineStageIntegrationTests(script: nullScript, confirmDeletion: false) 70 71 72 assertThat(stepsCalled, not(hasItem('input'))) 73 assertThat(stepsCalled, hasItems('abapEnvironmentCreateSystem')) 74 assertThat(stepsCalled, hasItems('cloudFoundryDeleteService')) 75 assertThat(stepsCalled, hasItems('abapEnvironmentBuild')) 76 assertThat(stepsCalled, hasItems('cloudFoundryCreateServiceKey')) 77 } 78 79 @Test 80 void testCreateSystemFails() { 81 82 helper.registerAllowedMethod('abapEnvironmentCreateSystem', [Map.class], {m -> stepsCalled.add('abapEnvironmentCreateSystem'); error("Failed")}) 83 84 nullScript.commonPipelineEnvironment.configuration.runStage = [ 85 'Integration Tests': true 86 ] 87 88 try { 89 jsr.step.abapEnvironmentPipelineStageIntegrationTests(script: nullScript, confirmDeletion: false) 90 fail("Expected exception") 91 } catch (Exception e) { 92 // failure expected 93 } 94 95 assertThat(stepsCalled, not(hasItem('input'))) 96 assertThat(stepsCalled, hasItems('abapEnvironmentCreateSystem')) 97 assertThat(stepsCalled, hasItems('cloudFoundryDeleteService')) 98 } 99 100 @Test 101 void testIntegrationTestsTageSkipped4testBuild() { 102 103 nullScript.commonPipelineEnvironment.configuration.runStage = [ 104 'Integration Tests': true 105 ] 106 jsr.step.abapEnvironmentPipelineStageIntegrationTests(script: nullScript, testBuild: true) 107 108 assertThat(stepsCalled, not(hasItems('input', 109 'abapEnvironmentCreateSystem', 110 'cloudFoundryDeleteService', 111 'abapEnvironmentBuild', 112 'cloudFoundryCreateServiceKey'))) 113 } 114 115 }