2018-01-16 16:42:11 +02:00
|
|
|
package util
|
|
|
|
|
|
|
|
import com.lesfurets.jenkins.unit.BasePipelineTest
|
|
|
|
import com.sap.piper.DefaultValueCache
|
|
|
|
import org.junit.rules.TestRule
|
|
|
|
import org.junit.runner.Description
|
|
|
|
import org.junit.runners.model.Statement
|
|
|
|
import org.yaml.snakeyaml.Yaml
|
|
|
|
|
2018-01-26 14:35:49 +02:00
|
|
|
class JenkinsReadYamlRule implements TestRule {
|
2018-01-16 16:42:11 +02:00
|
|
|
|
|
|
|
final BasePipelineTest testInstance
|
|
|
|
|
|
|
|
|
2018-01-26 14:35:49 +02:00
|
|
|
JenkinsReadYamlRule(BasePipelineTest testInstance) {
|
2018-01-16 16:42:11 +02:00
|
|
|
this.testInstance = testInstance
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
Statement apply(Statement base, Description description) {
|
|
|
|
return statement(base)
|
|
|
|
}
|
|
|
|
|
|
|
|
private Statement statement(final Statement base) {
|
|
|
|
return new Statement() {
|
|
|
|
@Override
|
|
|
|
void evaluate() throws Throwable {
|
2018-01-26 14:35:49 +02:00
|
|
|
testInstance.helper.registerAllowedMethod("readYaml", [Map], { Map m ->
|
|
|
|
if(m.text) {
|
|
|
|
return new Yaml().load(m.text)
|
|
|
|
} else if(m.file) {
|
2018-02-08 14:18:04 +02:00
|
|
|
return new Yaml().load((m.file as File).text)
|
2018-01-26 14:35:49 +02:00
|
|
|
} else {
|
|
|
|
throw new IllegalArgumentException("Key 'text' is missing in map ${m}.")
|
|
|
|
}
|
2018-01-16 16:42:11 +02:00
|
|
|
})
|
|
|
|
|
|
|
|
base.evaluate()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|