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

     1  package util
     2  
     3  import com.lesfurets.jenkins.unit.BasePipelineTest
     4  
     5  import java.beans.Introspector
     6  
     7  import org.junit.rules.TestRule
     8  import org.junit.runner.Description
     9  import org.junit.runners.model.Statement
    10  
    11  
    12  class JenkinsMockStepRule implements TestRule {
    13  
    14      final BasePipelineTest testInstance
    15      final String stepName
    16      def callsIndex = 0
    17      def callsParameters = [:]
    18  
    19  
    20      JenkinsMockStepRule(BasePipelineTest testInstance, String stepName) {
    21          this.testInstance = testInstance
    22          this.stepName = stepName
    23      }
    24  
    25      boolean hasParameter(def key, def value){
    26          for ( def parameters : callsParameters) {
    27              for ( def parameter : parameters.value.entrySet()) {
    28                  if (parameter.key.equals(key) && parameter.value.equals(value)) return true
    29              }
    30          }
    31          return false
    32      }
    33  
    34      @Override
    35      Statement apply(Statement base, Description description) {
    36          return new Statement() {
    37              @Override
    38              void evaluate() throws Throwable {
    39  
    40                  testInstance.helper.registerAllowedMethod(this.stepName, [Map], { Map m ->
    41                      this.callsIndex += 1
    42                      this.callsParameters.put(callsIndex, m)
    43                  })
    44  
    45                  base.evaluate()
    46              }
    47          }
    48      }
    49  
    50      @Override
    51      String toString() {
    52          return callsParameters.toString()
    53      }
    54  
    55  }