github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/templates/PiperPipelineStagePostTest.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.ExpectedException
     7  import org.junit.rules.RuleChain
     8  import util.BasePiperTest
     9  import util.JenkinsReadYamlRule
    10  import util.JenkinsStepRule
    11  import util.Rules
    12  
    13  import static org.hamcrest.Matchers.*
    14  import static org.junit.Assert.assertThat
    15  
    16  class PiperPipelineStagePostTest extends BasePiperTest {
    17      private JenkinsStepRule jsr = new JenkinsStepRule(this)
    18      private ExpectedException thrown = ExpectedException.none()
    19  
    20      @Rule
    21      public RuleChain rules = Rules
    22          .getCommonRules(this)
    23          .around(new JenkinsReadYamlRule(this))
    24          .around(thrown)
    25          .around(jsr)
    26  
    27      private List stepsCalled = []
    28  
    29      @Before
    30      void init()  {
    31          nullScript.env.STAGE_NAME = 'Release'
    32  
    33          helper.registerAllowedMethod('piperStageWrapper', [Map.class, Closure.class], {m, body ->
    34              assertThat(m.stageName, is('Release'))
    35              return body()
    36          })
    37          helper.registerAllowedMethod('influxWriteData', [Map.class], {m -> stepsCalled.add('influxWriteData')})
    38          helper.registerAllowedMethod('slackSendNotification', [Map.class], {m -> stepsCalled.add('slackSendNotification')})
    39          helper.registerAllowedMethod('mailSendNotification', [Map.class], {m -> stepsCalled.add('mailSendNotification')})
    40          helper.registerAllowedMethod('piperPublishWarnings', [Map.class], {m -> stepsCalled.add('piperPublishWarnings')})
    41      }
    42  
    43      @Test
    44      void testPostDefault() {
    45          jsr.step.piperPipelineStagePost(script: nullScript, juStabUtils: utils)
    46  
    47          assertThat(stepsCalled, hasItems('influxWriteData','mailSendNotification','piperPublishWarnings'))
    48          assertThat(stepsCalled, not(hasItem('slackSendNotification')))
    49      }
    50  
    51      @Test
    52      void testPostNotOnProductiveBranch() {
    53          binding.variables.env.BRANCH_NAME = 'anyOtherBranch'
    54  
    55          jsr.step.piperPipelineStagePost(script: nullScript, juStabUtils: utils)
    56  
    57          assertThat(stepsCalled, hasItems('influxWriteData','mailSendNotification','piperPublishWarnings'))
    58          assertThat(stepsCalled, not(hasItems('slackSendNotification')))
    59      }
    60  
    61      @Test
    62      void testPostWithSlackNotification() {
    63          nullScript.commonPipelineEnvironment.configuration = [runStep: ['Post Actions': [slackSendNotification: true]]]
    64  
    65          jsr.step.piperPipelineStagePost(script: nullScript, juStabUtils: utils)
    66  
    67          assertThat(stepsCalled, hasItems('influxWriteData','mailSendNotification','slackSendNotification','piperPublishWarnings'))
    68      }
    69  }