github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/NexusUploadTest.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 NexusUploadTest 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 != null)
    38                  return new JsonSlurper().parseText(m.text)
    39          })
    40          helper.registerAllowedMethod("withEnv", [List, Closure], { arguments, closure ->
    41              arguments.each {arg ->
    42                  withEnvArgs.add(arg.toString())
    43              }
    44              return closure()
    45          })
    46          credentialsRule.withCredentials('idOfCxCredential', "admin", "admin123")
    47          shellCallRule.setReturnValue('[ -x ./piper ]', 1)
    48          shellCallRule.setReturnValue(
    49              './piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/nexusUpload.yaml\'',
    50              '{"credentialsId": "idOfCxCredential", "verbose": false}'
    51          )
    52  
    53          helper.registerAllowedMethod('fileExists', [String.class], {return true})
    54          helper.registerAllowedMethod("writePipelineEnv", [Map.class], {m -> return })
    55          helper.registerAllowedMethod("readPipelineEnv", [Map.class], {m -> return })
    56          helper.registerAllowedMethod('findFiles', [Map.class], {return null})
    57      }
    58  
    59      @Test
    60      void testDeployPom() {
    61          stepRule.step.nexusUpload(
    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/nexusUpload.yaml'], containsString('name: nexusUpload'))
    69          assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'),
    70              containsString('"testParam":"This is test content"')))
    71          assertThat(shellCallRule.shell[2], is('./piper nexusUpload'))
    72      }
    73  }