github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/com/sap/piper/mta/MtaMultiplexerTest.groovy (about)

     1  package com.sap.piper.mta
     2  
     3  import static org.hamcrest.Matchers.containsString
     4  import static org.hamcrest.Matchers.hasEntry
     5  import static org.hamcrest.Matchers.hasItem
     6  import static org.hamcrest.Matchers.hasKey
     7  import static org.hamcrest.Matchers.hasSize
     8  import static org.hamcrest.Matchers.is
     9  import static org.hamcrest.Matchers.not
    10  
    11  import static org.junit.Assert.assertThat
    12  import org.junit.Rule
    13  import org.junit.Test
    14  import org.junit.rules.ExpectedException
    15  import org.junit.rules.RuleChain
    16  
    17  import util.BasePiperTest
    18  import util.JenkinsLoggingRule
    19  import util.Rules
    20  
    21  class MtaMultiplexerTest extends BasePiperTest {
    22      private ExpectedException thrown = ExpectedException.none()
    23      private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
    24  
    25      @Rule
    26      public RuleChain rules = Rules
    27          .getCommonRules(this)
    28          .around(loggingRule)
    29          .around(thrown)
    30  
    31      @Test
    32      void testFilterFiles() {
    33          // prepare test data
    34          def files = [
    35              new File("pom.xml"),
    36              new File("some-ui${File.separator}pom.xml"),
    37              new File("some-service${File.separator}pom.xml"),
    38              new File("some-other-service${File.separator}pom.xml")
    39          ].toArray()
    40          // execute test
    41          def result = MtaMultiplexer.removeExcludedFiles(nullScript, files, ['pom.xml'])
    42          // asserts
    43          assertThat(result, not(hasItem('pom.xml')))
    44          assertThat(result, hasSize(3))
    45          assertThat(loggingRule.log, containsString('Skipping pom.xml'))
    46      }
    47  
    48      @Test
    49      void testCreateJobs() {
    50          def optionsList = []
    51          // prepare test data
    52          helper.registerAllowedMethod("findFiles", [Map.class], { map ->
    53              if (map.glob == "**${File.separator}pom.xml") {
    54                  return [new File("some-service${File.separator}pom.xml"), new File("some-other-service${File.separator}pom.xml")].toArray()
    55              }
    56              if (map.glob == "**${File.separator}package.json") {
    57                  return [new File("some-ui${File.separator}package.json"), new File("somer-service-broker${File.separator}package.json")].toArray()
    58              }
    59              return [].toArray()
    60          })
    61          // execute test
    62          def result = MtaMultiplexer.createJobs(nullScript, ['myParameters':'value'], [], 'TestJobs', 'pom.xml', 'maven'){
    63              options -> optionsList.push(options)
    64          }
    65          // invoke jobs
    66          for(Closure c : result.values()) c()
    67          // asserts
    68          assertThat(result.size(), is(2))
    69          assertThat(result, hasKey('TestJobs - some-other-service'))
    70          assertThat(loggingRule.log, containsString("Found 2 maven descriptor files: [some-service${File.separator}pom.xml, some-other-service${File.separator}pom.xml]".toString()))
    71          assertThat(optionsList.get(0), hasEntry('myParameters', 'value'))
    72          assertThat(optionsList.get(0), hasEntry('scanType', 'maven'))
    73          assertThat(optionsList.get(0), hasEntry('buildDescriptorFile', "some-service${File.separator}pom.xml".toString()))
    74          assertThat(optionsList.get(1), hasEntry('myParameters', 'value'))
    75          assertThat(optionsList.get(1), hasEntry('scanType', 'maven'))
    76          assertThat(optionsList.get(1), hasEntry('buildDescriptorFile', "some-other-service${File.separator}pom.xml".toString()))
    77      }
    78  }