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

     1  import static org.hamcrest.Matchers.*
     2  import static org.junit.Assert.assertThat
     3  
     4  import org.junit.Before
     5  import org.junit.After
     6  import org.junit.Rule
     7  import org.junit.Test
     8  import org.junit.rules.ExpectedException
     9  import org.junit.rules.RuleChain
    10  
    11  import com.sap.piper.cm.BackendType
    12  import com.sap.piper.cm.ChangeManagement
    13  import com.sap.piper.cm.ChangeManagementException
    14  import com.sap.piper.Utils
    15  
    16  import hudson.AbortException
    17  import util.BasePiperTest
    18  import util.JenkinsCredentialsRule
    19  import util.JenkinsLoggingRule
    20  import util.JenkinsReadYamlRule
    21  import util.JenkinsStepRule
    22  import util.Rules
    23  
    24  class CheckChangeInDevelopmentTest extends BasePiperTest {
    25  
    26      private ExpectedException thrown = ExpectedException.none()
    27      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    28      private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
    29  
    30      @Rule
    31      public RuleChain ruleChain = Rules
    32          .getCommonRules(this)
    33          .around(new JenkinsReadYamlRule(this))
    34          .around(thrown)
    35          .around(stepRule)
    36          .around(loggingRule)
    37          .around(new JenkinsCredentialsRule(this)
    38          .withCredentials('CM', 'anonymous', '********'))
    39  
    40      @Before
    41      public void setup() {
    42          Utils.metaClass.echo = { def m -> }
    43  
    44          nullScript.commonPipelineEnvironment.configuration = [general:
    45                                       [changeManagement:
    46                                           [
    47                                            credentialsId: 'CM',
    48                                            type: 'SOLMAN',
    49                                            endpoint: 'https://example.org/cm'
    50                                           ]
    51                                       ]
    52                                   ]
    53          helper.registerAllowedMethod('addBadge', [Map], {return})
    54          helper.registerAllowedMethod('createSummary', [Map], {return})
    55      }
    56  
    57      @After
    58      public void tearDown() {
    59          cmUtilReceivedParams.clear()
    60          Utils.metaClass = null
    61      }
    62  
    63      private Map cmUtilReceivedParams = [:]
    64  
    65      @Test
    66      public void changeIsInStatusDevelopmentTest() {
    67  
    68          def calledWithParameters,
    69              calledWithStepName
    70  
    71          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
    72              params, stepName, metaData, creds ->
    73                  if(stepName.equals("isChangeInDevelopment")) {
    74                      nullScript.commonPipelineEnvironment.setValue('isChangeInDevelopment', true)
    75                  }
    76              })
    77  
    78          stepRule.step.checkChangeInDevelopment(
    79              script: nullScript,
    80              changeDocumentId: '001',
    81              failIfStatusIsNotInDevelopment: true)
    82  
    83          assertThat(nullScript.commonPipelineEnvironment.getValue('isChangeInDevelopment'), is(true))
    84  
    85          // no exception in thrown, so the change is in status 'in development'.
    86      }
    87  
    88      @Test
    89      public void changeIsNotInStatusDevelopmentTest() {
    90  
    91          thrown.expect(AbortException)
    92          thrown.expectMessage("Change '001' is not in status 'in development'")
    93  
    94          def calledWithParameters,
    95              calledWithStepName
    96  
    97          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
    98              params, stepName, metaData, creds ->
    99                  if(stepName.equals("isChangeInDevelopment")) {
   100                      nullScript.commonPipelineEnvironment.setValue('isChangeInDevelopment', false)
   101                  }
   102              })
   103  
   104          stepRule.step.checkChangeInDevelopment(
   105              script: nullScript,
   106              changeDocumentId: '001',
   107              failIfStatusIsNotInDevelopment: true)
   108      }
   109  
   110      @Test
   111      public void changeIsNotInStatusDevelopmentButWeWouldLikeToSkipFailureTest() {
   112  
   113          def calledWithParameters,
   114              calledWithStepName
   115  
   116          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   117              params, stepName, metaData, creds ->
   118                  if(stepName.equals("isChangeInDevelopment")) {
   119                      nullScript.commonPipelineEnvironment.setValue('isChangeInDevelopment', false)
   120                  }
   121              })
   122  
   123          stepRule.step.checkChangeInDevelopment(
   124                                      script: nullScript,
   125                                      changeDocumentId: '001',
   126                                      failIfStatusIsNotInDevelopment: false)
   127  
   128          assertThat(nullScript.commonPipelineEnvironment.getValue('isChangeInDevelopment'), is(false))
   129      }
   130  
   131      @Test
   132      public void nullChangeDocumentIdTest() {
   133  
   134          thrown.expect(IllegalArgumentException)
   135          thrown.expectMessage("No changeDocumentId provided. Neither via parameter 'changeDocumentId' nor via " +
   136                               "label 'ChangeDocument\\s?:' in commit range [from: origin/master, to: HEAD].")
   137  
   138          def calledWithParameters,
   139              calledWithStepName
   140  
   141          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   142              params, stepName, metaData, creds ->
   143                  if(stepName.equals("transportRequestDocIDFromGit")) {
   144                      calledWithParameters = params
   145                  }
   146              })
   147  
   148          stepRule.step.checkChangeInDevelopment(
   149              script: nullScript)
   150  
   151          assertThat(calledWithParameters,is(not(null)))
   152      }
   153  
   154      @Test
   155      public void emptyChangeDocumentIdTest() {
   156  
   157          thrown.expect(IllegalArgumentException)
   158          thrown.expectMessage("No changeDocumentId provided. Neither via parameter 'changeDocumentId' " +
   159                               "nor via label 'ChangeDocument\\s?:' in commit range " +
   160                               "[from: origin/master, to: HEAD].")
   161  
   162          def calledWithParameters,
   163              calledWithStepName
   164  
   165          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   166              params, stepName, metaData, creds ->
   167                  if(stepName.equals("transportRequestDocIDFromGit")) {
   168                      calledWithParameters = params
   169                      nullScript.commonPipelineEnvironment.setValue('changeDocumentId', '')
   170                  }
   171              })
   172  
   173          stepRule.step.checkChangeInDevelopment(
   174              script: nullScript)
   175  
   176          assertThat(calledWithParameters,is(not(null)))
   177      }
   178  
   179      @Test
   180      public void cmIntegrationSwichtedOffTest() {
   181  
   182          loggingRule.expect('[INFO] Change management integration intentionally switched off.')
   183  
   184          stepRule.step.checkChangeInDevelopment(
   185              script: nullScript,
   186              changeManagement: [type: 'NONE'])
   187  
   188      }
   189  
   190      @Test
   191      public void stageConfigIsNotConsideredWithParamKeysTest() {
   192  
   193          nullScript.commonPipelineEnvironment.configuration.stages = [foo:[changeDocumentId:'12345']]
   194  
   195          thrown.expect(IllegalArgumentException)
   196          thrown.expectMessage('No changeDocumentId provided.')
   197  
   198          def calledWithParameters,
   199              calledWithStepName
   200  
   201          helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
   202              params, stepName, metaData, creds ->
   203                  if(stepName.equals("transportRequestDocIDFromGit")) {
   204                      calledWithParameters = params
   205                  }
   206              })
   207  
   208          stepRule.step.checkChangeInDevelopment(
   209              script: nullScript,
   210              stageName: 'foo')
   211  
   212          assertThat(calledWithParameters,is(not(null)))
   213      }
   214  }