github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/GatlingExecuteTestsTest.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  import util.*
     7  
     8  import static org.hamcrest.Matchers.*
     9  import static org.junit.Assert.assertThat
    10  
    11  class GatlingExecuteTestsTest extends BasePiperTest {
    12      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    13      private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
    14      private ExpectedException thrown = ExpectedException.none()
    15  
    16      @Rule
    17      public RuleChain rules = Rules
    18          .getCommonRules(this)
    19          .around(new JenkinsReadYamlRule(this))
    20          .around(loggingRule)
    21          .around(stepRule)
    22          .around(thrown)
    23  
    24      List mavenParams = []
    25  
    26      @Before
    27      void init() throws Exception {
    28          helper.registerAllowedMethod("mavenExecute", [Map], { map -> mavenParams.add(map) })
    29      }
    30  
    31      @Test
    32      void pomPathDoesNotExist() throws Exception {
    33          helper.registerAllowedMethod("fileExists", [String], { path -> return false })
    34          thrown.expectMessage("The file 'does-not-exist' does not exist.")
    35  
    36          stepRule.step.gatlingExecuteTests(
    37              script: nullScript,
    38              juStabUtils: utils,
    39              pomPath: 'does-not-exist'
    40          )
    41  
    42          assertJobStatusFailure()
    43      }
    44  
    45      @Test
    46      void executionWithoutAppUrls() throws Exception {
    47          registerPerformanceTestsModule()
    48  
    49          stepRule.step.gatlingExecuteTests(
    50              script: nullScript,
    51              juStabUtils: utils,
    52              pomPath: 'performance-tests/pom.xml'
    53          )
    54  
    55          assertThat(mavenParams.size(), is(1))
    56  
    57          assertThat(mavenParams[0], is([
    58              script: nullScript,
    59              flags: ['--update-snapshots'],
    60              pomPath: 'performance-tests/pom.xml',
    61              goals: ['test']
    62          ]))
    63  
    64          assertJobStatusSuccess()
    65      }
    66  
    67      @Test
    68      void executionWithAppUrls() throws Exception {
    69          registerPerformanceTestsModule()
    70  
    71          final String url1 = 'url1'
    72          final String url2 = 'url2'
    73          final String username = 'test-username'
    74          final String password = 'test-password'
    75  
    76          helper.registerAllowedMethod("withCredentials", [List, Closure], { creds, body ->
    77              assertThat(creds.size(), is(1))
    78              binding.setVariable(creds[0].usernameVariable, username)
    79              binding.setVariable(creds[0].passwordVariable, password)
    80              body()
    81          })
    82  
    83          stepRule.step.gatlingExecuteTests(
    84              script: nullScript,
    85              juStabUtils: utils,
    86              pomPath: 'performance-tests/pom.xml',
    87              appUrls: [[url: url1, credentialsId: 'credentials1'], [url: url2, credentialsId: 'credentials2']]
    88          )
    89  
    90          assertThat(mavenParams.size(), is(2))
    91  
    92          assertThat(mavenParams[0], is([
    93              script: nullScript,
    94              flags: ['--update-snapshots'],
    95              pomPath: 'performance-tests/pom.xml',
    96              goals: ['test'],
    97              defines: [
    98                  "-DappUrl=$url1",
    99                  "-Dusername=$username",
   100                  "-Dpassword=$password"
   101              ]
   102          ]))
   103  
   104          assertThat(mavenParams[1], is([
   105              script: nullScript,
   106              flags: ['--update-snapshots'],
   107              pomPath: 'performance-tests/pom.xml',
   108              goals: ['test'],
   109              defines: [
   110                  "-DappUrl=$url2",
   111                  "-Dusername=$username",
   112                  "-Dpassword=$password"
   113              ]
   114          ]))
   115  
   116          assertJobStatusSuccess()
   117      }
   118  
   119      private void registerPerformanceTestsModule() {
   120          helper.registerAllowedMethod("fileExists", [String], { path ->
   121              switch (path) {
   122                  case "performance-tests/pom.xml":
   123                      return true
   124                  default:
   125                      return false
   126              }
   127          })
   128      }
   129  }