github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/SlackSendNotificationTest.groovy (about)

     1  import org.junit.After
     2  import org.junit.Before
     3  import org.junit.Rule
     4  import org.junit.Test
     5  import org.junit.rules.RuleChain
     6  import util.BasePiperTest
     7  import util.JenkinsLoggingRule
     8  import util.JenkinsReadYamlRule
     9  import util.JenkinsStepRule
    10  import util.Rules
    11  import com.sap.piper.Utils
    12  
    13  import static org.junit.Assert.*
    14  
    15  class SlackSendNotificationTest extends BasePiperTest {
    16      def slackCallMap = [:]
    17  
    18      private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
    19      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    20  
    21      @Rule
    22      public RuleChain ruleChain = Rules
    23          .getCommonRules(this)
    24          .around(new JenkinsReadYamlRule(this))
    25          .around(loggingRule)
    26          .around(stepRule)
    27  
    28      @Before
    29      void init() throws Exception {
    30          helper.registerAllowedMethod("slackSend", [Map.class], {m -> slackCallMap = m})
    31          Utils.metaClass.echo = { def m -> }
    32      }
    33  
    34      @After
    35      public void tearDown() {
    36          Utils.metaClass = null
    37      }
    38  
    39      @Test
    40      void testNotificationBuildSuccessDefaultChannel() throws Exception {
    41          stepRule.step.slackSendNotification(script: [currentBuild: [result: 'SUCCESS']])
    42          // asserts
    43          assertEquals('Message not set correctly', 'SUCCESS: Job p <http://build.url|#1>', slackCallMap.message.toString())
    44          assertNull('Channel not set correctly', slackCallMap.channel)
    45          assertEquals('Color not set correctly', '#8cc04f', slackCallMap.color)
    46          assertJobStatusSuccess()
    47      }
    48  
    49      @Test
    50      void testNotificationBuildSuccessCustomChannel() throws Exception {
    51          stepRule.step.slackSendNotification(script: [currentBuild: [result: 'SUCCCESS']], channel: 'Test')
    52          // asserts
    53          assertEquals('Channel not set correctly', 'Test', slackCallMap.channel)
    54          assertJobStatusSuccess()
    55      }
    56  
    57      @Test
    58      void testNotificationBuildFailed() throws Exception {
    59          stepRule.step.slackSendNotification(script: [currentBuild: [result: 'FAILURE']])
    60          // asserts
    61          assertEquals('Message not set correctly', 'FAILURE: Job p <http://build.url|#1>', slackCallMap.message.toString())
    62          assertEquals('Color not set correctly', '#d54c53', slackCallMap.color)
    63      }
    64  
    65      @Test
    66      void testNotificationBuildStatusNull() throws Exception {
    67          stepRule.step.slackSendNotification(script: [currentBuild: [:]])
    68          // asserts
    69          assertTrue('Missing build status not detected', loggingRule.log.contains('currentBuild.result is not set. Skipping Slack notification'))
    70          assertJobStatusSuccess()
    71      }
    72  
    73      @Test
    74      void testNotificationCustomMessageAndColor() throws Exception {
    75          stepRule.step.slackSendNotification(script: [currentBuild: [:]], message: 'Custom Message', color: '#AAAAAA')
    76          // asserts
    77          assertEquals('Custom message not set correctly', 'Custom Message', slackCallMap.message.toString())
    78          assertEquals('Custom color not set correctly', '#AAAAAA', slackCallMap.color)
    79          assertJobStatusSuccess()
    80      }
    81  
    82      @Test
    83      void testNotificationWithCustomCredentials() throws Exception {
    84          stepRule.step.slackSendNotification(
    85              script: [currentBuild: [:]],
    86              message: 'I am no Message',
    87              baseUrl: 'https://my.base.url',
    88              credentialsId: 'MY_TOKEN_ID'
    89          )
    90          // asserts
    91          assertEquals('Custom base url not set correctly', 'https://my.base.url', slackCallMap.baseUrl)
    92          assertEquals('Custom token id not set correctly', 'MY_TOKEN_ID', slackCallMap.tokenCredentialId)
    93          assertJobStatusSuccess()
    94      }
    95  }