github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/templates/PiperPipelineStagePRVotingTest.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.JenkinsLoggingRule
     9  import util.JenkinsReadYamlRule
    10  import util.JenkinsStepRule
    11  import util.Rules
    12  
    13  import static org.hamcrest.Matchers.anyOf
    14  import static org.hamcrest.Matchers.hasItem
    15  import static org.hamcrest.Matchers.hasItems
    16  import static org.hamcrest.Matchers.is
    17  import static org.hamcrest.Matchers.not
    18  import static org.hamcrest.Matchers.nullValue
    19  import static org.junit.Assert.assertThat
    20  
    21  class PiperPipelineStagePRVotingTest extends BasePiperTest {
    22      private JenkinsStepRule jsr = new JenkinsStepRule(this)
    23      private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
    24  
    25      @Rule
    26      public RuleChain rules = Rules
    27          .getCommonRules(this)
    28          .around(new JenkinsReadYamlRule(this))
    29          .around(jlr)
    30          .around(jsr)
    31  
    32      private List stepsCalled = []
    33      private Map stepParameters = [:]
    34  
    35      @Before
    36      void init()  {
    37  
    38          nullScript.env.STAGE_NAME = 'Pull-Request Voting'
    39          binding.variables.env = [
    40              STAGE_NAME: 'Pull-Request Voting',
    41              BRANCH_NAME: 'PR-1'
    42          ]
    43  
    44          helper.registerAllowedMethod('piperStageWrapper', [Map.class, Closure.class], {m, body ->
    45              assertThat(m.stageName, is('Pull-Request Voting'))
    46              return body()
    47          })
    48  
    49          helper.registerAllowedMethod('buildExecute', [Map.class], {m ->
    50              stepsCalled.add('buildExecute')
    51              stepParameters.buildExecute = m
    52          })
    53  
    54          helper.registerAllowedMethod('checksPublishResults', [Map.class], {m ->
    55              stepsCalled.add('checksPublishResults')
    56          })
    57  
    58          helper.registerAllowedMethod('testsPublishResults', [Map.class], {m ->
    59              stepsCalled.add('testsPublishResults')
    60          })
    61  
    62          helper.registerAllowedMethod('karmaExecuteTests', [Map.class], {m ->
    63              stepsCalled.add('karmaExecuteTests')
    64          })
    65  
    66          helper.registerAllowedMethod('mavenExecuteIntegration', [Map.class], {m ->
    67              stepsCalled.add('mavenExecuteIntegration')
    68          })
    69  
    70          helper.registerAllowedMethod('mavenExecuteStaticCodeChecks', [Map.class], {m ->
    71              stepsCalled.add('mavenExecuteStaticCodeChecks')
    72          })
    73  
    74          helper.registerAllowedMethod('npmExecuteLint', [Map.class], {m ->
    75              stepsCalled.add('npmExecuteLint')
    76          })
    77  
    78          helper.registerAllowedMethod('npmExecuteScripts', [Map.class], {m ->
    79              stepsCalled.add('npmExecuteScripts')
    80          })
    81  
    82          helper.registerAllowedMethod('whitesourceExecuteScan', [Map.class], {m ->
    83              stepsCalled.add('whitesourceExecuteScan')
    84              stepParameters.whitesourceExecuteScan = m
    85              m.script.commonPipelineEnvironment.setValue('whitesourceProjectNames', ['ws project - PR1'])
    86  
    87          })
    88  
    89          helper.registerAllowedMethod('writeTemporaryCredentials', [Map.class, Closure.class], {m, body ->
    90              stepsCalled.add('writeTemporaryCredentials')
    91              body()
    92          })
    93      }
    94  
    95      @Test
    96      void testPRVotingDefault() {
    97  
    98          nullScript.commonPipelineEnvironment.configuration = [general: [buildTool: 'maven']]
    99          jsr.step.piperPipelineStagePRVoting(script: nullScript, juStabUtils: utils)
   100  
   101          assertThat(stepsCalled, hasItems('buildExecute', 'checksPublishResults', 'testsPublishResults'))
   102          assertThat(stepsCalled, not(anyOf(hasItem('karmaExecuteTests'), hasItem('mavenExecuteIntegration'), hasItem('mavenExecuteStaticCodeChecks'), hasItem('npmExecuteLint'), hasItem('npmExecuteScripts'), hasItem('whitesourceExecuteScan'))))
   103          assertThat(stepParameters.buildExecute.buildTool, is('maven'))
   104          assertThat(stepParameters.buildExecute.dockerRegistryUrl, nullValue())
   105      }
   106  
   107      @Test
   108      void testPRVotingWithCustomSteps() {
   109  
   110          nullScript.commonPipelineEnvironment.configuration = [
   111              general: [buildTool: 'maven'],
   112              runStep: ['Pull-Request Voting': [karmaExecuteTests: true, whitesourceExecuteScan: true]]
   113          ]
   114  
   115          jsr.step.piperPipelineStagePRVoting(
   116              script: nullScript,
   117              juStabUtils: utils,
   118          )
   119  
   120          assertThat(stepsCalled, hasItems( 'karmaExecuteTests', 'whitesourceExecuteScan'))
   121          assertThat(stepParameters.whitesourceExecuteScan.productVersion, is('PR-1'))
   122      }
   123  
   124      @Test
   125      void testPRVotingDocker() {
   126  
   127          nullScript.commonPipelineEnvironment.configuration = [
   128              general: [buildTool: 'docker'],
   129              runStep: ['Pull-Request Voting': [karmaExecuteTests: true, whitesourceExecuteScan: true]]
   130          ]
   131  
   132          jsr.step.piperPipelineStagePRVoting(
   133              script: nullScript,
   134              juStabUtils: utils,
   135          )
   136  
   137          assertThat(stepParameters.buildExecute.dockerRegistryUrl, is(''))
   138      }
   139  
   140      @Test
   141      void testPRVotingWithMavenExecuteIntegration() {
   142  
   143          nullScript.commonPipelineEnvironment.configuration = [
   144              general: [buildTool: 'maven'],
   145              runStep: ['Pull-Request Voting': [mavenExecuteIntegration: true]]
   146          ]
   147  
   148          jsr.step.piperPipelineStagePRVoting(
   149              script: nullScript,
   150              juStabUtils: utils,
   151          )
   152  
   153          assertThat(stepsCalled, hasItems('mavenExecuteIntegration', 'testsPublishResults', 'writeTemporaryCredentials'))
   154      }
   155  
   156      @Test
   157      void testPRVotingWithLinting() {
   158  
   159          nullScript.commonPipelineEnvironment.configuration = [
   160              general: [buildTool: 'maven'],
   161              stages: ['Pull-Request Voting': [npmExecuteLint: true, mavenExecuteStaticCodeChecks: true]]
   162          ]
   163  
   164          jsr.step.piperPipelineStagePRVoting(
   165              script: nullScript,
   166              juStabUtils: utils,
   167          )
   168  
   169          assertThat(stepsCalled, hasItem('npmExecuteLint'))
   170          assertThat(stepsCalled, hasItem('mavenExecuteStaticCodeChecks'))
   171  
   172      }
   173  
   174      @Test
   175      void testPRVotingWithNpmExecuteScripts() {
   176  
   177          nullScript.commonPipelineEnvironment.configuration = [
   178              general: [buildTool: 'maven'],
   179              runStep: ['Pull-Request Voting': [npmExecuteScripts: true]]
   180          ]
   181  
   182          jsr.step.piperPipelineStagePRVoting(
   183              script: nullScript,
   184              juStabUtils: utils,
   185          )
   186  
   187          assertThat(stepsCalled, hasItems('npmExecuteScripts', 'testsPublishResults'))
   188      }
   189  }