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

     1  import org.junit.Before
     2  import org.junit.Rule
     3  import org.junit.Test
     4  import org.junit.rules.ExpectedException
     5  import org.junit.rules.RuleChain
     6  
     7  import util.BasePiperTest
     8  import util.JenkinsCredentialsRule
     9  import util.JenkinsFileExistsRule
    10  import util.JenkinsMavenExecuteRule
    11  import util.JenkinsReadJsonRule
    12  import util.JenkinsReadYamlRule
    13  import util.JenkinsShellCallRule
    14  import util.JenkinsStepRule
    15  import util.JenkinsWriteFileRule
    16  import util.Rules
    17  
    18  import static org.hamcrest.Matchers.*
    19  import static org.junit.Assert.assertThat
    20  
    21  class MavenExecuteTest extends BasePiperTest {
    22      private ExpectedException exception = ExpectedException.none()
    23  
    24      private JenkinsCredentialsRule credentialsRule = new JenkinsCredentialsRule(this)
    25      private JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
    26      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    27      private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this)
    28      private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule(this, [])
    29  
    30      private List withEnvArgs = []
    31  
    32      @Rule
    33      public RuleChain rules = Rules
    34          .getCommonRules(this)
    35          .around(exception)
    36          .around(new JenkinsReadYamlRule(this))
    37          .around(credentialsRule)
    38          .around(new JenkinsReadJsonRule(this))
    39          .around(shellCallRule)
    40          .around(stepRule)
    41          .around(writeFileRule)
    42          .around(fileExistsRule)
    43  
    44      @Before
    45      void init() {
    46          helper.registerAllowedMethod("withEnv", [List, Closure], { arguments, closure ->
    47              arguments.each {arg ->
    48                  withEnvArgs.add(arg.toString())
    49              }
    50              return closure()
    51          })
    52          credentialsRule.withCredentials('idOfCxCredential', "admin", "admin123")
    53          shellCallRule.setReturnValue('[ -x ./piper ]', 1)
    54          shellCallRule.setReturnValue(
    55              './piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/mavenExecute.yaml\'',
    56              '{"credentialsId": "idOfCxCredential", "verbose": false}'
    57          )
    58  
    59          helper.registerAllowedMethod('findFiles', [Map.class], {return null})
    60          helper.registerAllowedMethod("writePipelineEnv", [Map.class], {m -> return })
    61          helper.registerAllowedMethod("readPipelineEnv", [Map.class], {m -> return })
    62      }
    63  
    64      @Test
    65      void testExecute() {
    66          stepRule.step.mavenExecute(
    67              juStabUtils: utils,
    68              jenkinsUtilsStub: jenkinsUtils,
    69              testParam: "This is test content",
    70              script: nullScript,
    71          )
    72          // asserts
    73          assertThat(writeFileRule.files['.pipeline/tmp/metadata/mavenExecute.yaml'], containsString('name: mavenExecute'))
    74          assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'),
    75              containsString('"testParam":"This is test content"')))
    76          assertThat(shellCallRule.shell[2], is('./piper mavenExecute'))
    77      }
    78  
    79      @Test
    80      void testOutputIsReturned() {
    81          // init
    82          String outputFile = '.pipeline/maven_output.txt'
    83          String expectedOutput = 'the output'
    84          fileExistsRule.registerExistingFile(outputFile)
    85          helper.registerAllowedMethod('readFile', [String], {file ->
    86              if (file == outputFile) {
    87                  return expectedOutput
    88              }
    89              return ''
    90          })
    91  
    92          // test
    93         String receivedOutput = stepRule.step.mavenExecute(
    94              juStabUtils: utils,
    95              jenkinsUtilsStub: jenkinsUtils,
    96              script: nullScript,
    97              returnStdout: true,
    98          )
    99  
   100          // asserts
   101          assertThat(receivedOutput, is(expectedOutput))
   102      }
   103  
   104      @Test
   105      void testOutputIsMissing() {
   106          // init
   107          fileExistsRule.setExistingFiles([])
   108          helper.registerAllowedMethod('readFile', [String], {file ->
   109              return ''
   110          })
   111          String errorMessage = ''
   112          helper.registerAllowedMethod('error', [String], {message ->
   113              errorMessage = message
   114          })
   115  
   116          // test
   117          stepRule.step.mavenExecute(
   118              juStabUtils: utils,
   119              jenkinsUtilsStub: jenkinsUtils,
   120              script: nullScript,
   121              returnStdout: true,
   122          )
   123  
   124          // asserts
   125          assertThat(errorMessage, containsString('Internal error. A text file with the contents of the maven output was expected'))
   126      }
   127  }