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

     1  import groovy.json.JsonSlurper
     2  import org.junit.Before
     3  import org.junit.Rule
     4  import org.junit.Test
     5  import org.junit.rules.ExpectedException
     6  import org.junit.rules.RuleChain
     7  import util.*
     8  
     9  import static org.hamcrest.Matchers.*
    10  import static org.junit.Assert.assertThat
    11  
    12  class MavenExecuteIntegrationTest extends BasePiperTest {
    13      private ExpectedException exception = ExpectedException.none()
    14  
    15      private JenkinsCredentialsRule credentialsRule = new JenkinsCredentialsRule(this)
    16      private JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
    17      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    18      private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this)
    19  
    20      private List withEnvArgs = []
    21  
    22      @Rule
    23      public RuleChain rules = Rules
    24          .getCommonRules(this)
    25          .around(exception)
    26          .around(new JenkinsReadYamlRule(this))
    27          .around(credentialsRule)
    28          .around(new JenkinsReadJsonRule(this))
    29          .around(shellCallRule)
    30          .around(stepRule)
    31          .around(writeFileRule)
    32          .around(new JenkinsFileExistsRule(this, []))
    33  
    34      @Before
    35      void init() {
    36          helper.registerAllowedMethod("readJSON", [Map], { m ->
    37              if (m.text instanceof String)
    38                  return new JsonSlurper().parseText(m.text as String)
    39          })
    40          helper.registerAllowedMethod("withEnv", [List, Closure], { arguments, closure ->
    41              arguments.each {arg ->
    42                  withEnvArgs.add(arg.toString())
    43              }
    44              return closure()
    45          })
    46          shellCallRule.setReturnValue('[ -x ./piper ]', 1)
    47          shellCallRule.setReturnValue(
    48              './piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/mavenExecuteIntegration.yaml\'',
    49              '{"verbose": false}'
    50          )
    51  
    52          helper.registerAllowedMethod('fileExists', [String], {return true})
    53          helper.registerAllowedMethod('findFiles', [Map], {return null})
    54          helper.registerAllowedMethod('testsPublishResults', [Map], {return null})
    55          helper.registerAllowedMethod("writePipelineEnv", [Map.class], {m -> return })
    56          helper.registerAllowedMethod("readPipelineEnv", [Map.class], {m -> return })
    57      }
    58  
    59      @Test
    60      void testParameterPassing() {
    61          stepRule.step.mavenExecuteIntegration(
    62              juStabUtils: utils,
    63              jenkinsUtilsStub: jenkinsUtils,
    64              testParam: 'This is test content',
    65              script: nullScript,
    66          )
    67          // asserts
    68          assertThat(writeFileRule.files['.pipeline/tmp/metadata/mavenExecuteIntegration.yaml'] as String,
    69              containsString('name: mavenExecuteIntegration'))
    70          assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'),
    71              containsString('"testParam":"This is test content"')))
    72          assertThat(shellCallRule.shell[2] as String, is('./piper mavenExecuteIntegration'))
    73      }
    74  }