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

     1  import com.sap.piper.ReportAggregator
     2  import org.hamcrest.Matcher
     3  import org.junit.Before
     4  import org.junit.Rule
     5  import org.junit.Test
     6  import org.junit.rules.ExpectedException
     7  import org.junit.rules.RuleChain
     8  import util.BasePiperTest
     9  import util.JenkinsCredentialsRule
    10  import util.JenkinsFileExistsRule
    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.allOf
    19  import static org.hamcrest.Matchers.containsString
    20  import static org.hamcrest.Matchers.hasItems
    21  import static org.hamcrest.Matchers.is
    22  import static org.hamcrest.Matchers.startsWith
    23  import static org.junit.Assert.assertFalse
    24  import static org.junit.Assert.assertThat
    25  import static org.junit.Assert.assertTrue
    26  
    27  class MavenExecuteStaticCodeChecksTest extends BasePiperTest {
    28      private ExpectedException exception = ExpectedException.none()
    29      private JenkinsCredentialsRule credentialsRule = new JenkinsCredentialsRule(this)
    30      private JenkinsReadJsonRule readJsonRule = new JenkinsReadJsonRule(this)
    31      private JenkinsShellCallRule shellCallRule = new JenkinsShellCallRule(this)
    32      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    33      private JenkinsWriteFileRule writeFileRule = new JenkinsWriteFileRule(this)
    34      private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule(this, [])
    35  
    36      private List withEnvArgs = []
    37      private boolean spotBugsStepCalled = false
    38      private boolean pmdParserStepCalled = false
    39  
    40      @Rule
    41      public RuleChain rules = Rules
    42          .getCommonRules(this)
    43          .around(exception)
    44          .around(new JenkinsReadYamlRule(this))
    45          .around(credentialsRule)
    46          .around(readJsonRule)
    47          .around(shellCallRule)
    48          .around(stepRule)
    49          .around(writeFileRule)
    50          .around(fileExistsRule)
    51  
    52      @Before
    53      void init() {
    54          helper.registerAllowedMethod("withEnv", [List.class, Closure.class], { arguments, closure ->
    55              arguments.each { arg ->
    56                  withEnvArgs.add(arg.toString())
    57              }
    58              return closure()
    59          })
    60          helper.registerAllowedMethod("dockerExecute", [Map.class, Closure.class], {
    61              Map params, Closure c ->
    62                  c.call()
    63          })
    64          helper.registerAllowedMethod("recordIssues", [Map.class], { Map config
    65              ->
    66          })
    67          helper.registerAllowedMethod("spotBugs", [Map.class], { Map config
    68              -> spotBugsStepCalled = true
    69          })
    70          helper.registerAllowedMethod("pmdParser", [Map.class], { Map config
    71              -> pmdParserStepCalled = true
    72          })
    73  
    74          shellCallRule.setReturnValue('./piper getConfig --contextConfig --stepMetadata \'.pipeline/tmp/metadata/mavenExecuteStaticCodeChecks.yaml\'', '{"dockerImage": "maven:3.6-jdk-8"}')
    75  
    76          helper.registerAllowedMethod('findFiles', [Map.class], {return null})
    77          helper.registerAllowedMethod("writePipelineEnv", [Map.class], {m -> return })
    78          helper.registerAllowedMethod("readPipelineEnv", [Map.class], {m -> return })
    79      }
    80  
    81      @Test
    82      void 'MavenExecuteStaticCodeChecks should be executed, results recorded and reported in Reportaggregator'() {
    83          spotBugsStepCalled = false
    84          pmdParserStepCalled = false
    85  
    86          nullScript.commonPipelineEnvironment.configuration = [steps: [:]]
    87          stepRule.step.mavenExecuteStaticCodeChecks(
    88              juStabUtils: utils,
    89              jenkinsUtilsStub: jenkinsUtils,
    90              testParam: "This is test content",
    91              script: nullScript
    92          )
    93  
    94          assertThat(writeFileRule.files['.pipeline/tmp/metadata/mavenExecuteStaticCodeChecks.yaml'], containsString('name: mavenExecuteStaticCodeChecks'))
    95          assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'), containsString('"testParam":"This is test content"')))
    96          assertThat(shellCallRule.shell[shellCallRule.shell.size() -1], is('./piper mavenExecuteStaticCodeChecks'))
    97          assertTrue(spotBugsStepCalled)
    98          assertTrue(pmdParserStepCalled)
    99          assertThat(ReportAggregator.instance.staticCodeScans, hasItems("Findbugs Static Code Checks", "PMD Static Code Checks"))
   100      }
   101  
   102      @Test
   103      void 'MavenExecuteStaticCodeChecks should not record results and not report in Reportaggregator when turned off'() {
   104          spotBugsStepCalled = false
   105          pmdParserStepCalled = false
   106  
   107          nullScript.commonPipelineEnvironment.configuration = [steps: [mavenExecuteStaticCodeChecks: [spotBugs: false, pmd: false]]]
   108          stepRule.step.mavenExecuteStaticCodeChecks(
   109              juStabUtils: utils,
   110              jenkinsUtilsStub: jenkinsUtils,
   111              testParam: "This is test content",
   112              script: nullScript
   113          )
   114  
   115          assertThat(writeFileRule.files['.pipeline/tmp/metadata/mavenExecuteStaticCodeChecks.yaml'], containsString('name: mavenExecuteStaticCodeChecks'))
   116          assertThat(withEnvArgs[0], allOf(startsWith('PIPER_parametersJSON'), containsString('"testParam":"This is test content"')))
   117          assertThat(shellCallRule.shell[shellCallRule.shell.size() -1], is('./piper mavenExecuteStaticCodeChecks'))
   118          assertFalse(spotBugsStepCalled)
   119          assertFalse(pmdParserStepCalled)
   120          assertTrue(ReportAggregator.instance.staticCodeScans.isEmpty())
   121      }
   122  }