1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00

Merge pull request #154 from marcusholl/pr/configFrwkChanges

general default section mixed in automatically.
This commit is contained in:
Christopher Fenner 2018-05-30 11:05:52 +02:00 committed by GitHub
commit c53d87f75f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 8 deletions

View File

@ -7,7 +7,7 @@ class ConfigurationHelper implements Serializable {
.loadDefaults() .loadDefaults()
} }
private Map config private Map config = [:]
private String name private String name
ConfigurationHelper(Script step){ ConfigurationHelper(Script step){
@ -21,10 +21,21 @@ class ConfigurationHelper implements Serializable {
} }
private final ConfigurationHelper loadDefaults(){ private final ConfigurationHelper loadDefaults(){
config = ConfigurationLoader.defaultStepConfiguration(null, name) config = ConfigurationLoader.defaultGeneralConfiguration()
mixin(ConfigurationLoader.defaultStepConfiguration(null, name))
return this return this
} }
ConfigurationHelper mixinGeneralConfig(commonPipelineEnvironment, Set filter = null){
Map stepConfiguration = ConfigurationLoader.generalConfiguration([commonPipelineEnvironment: commonPipelineEnvironment])
return mixin(stepConfiguration, filter)
}
ConfigurationHelper mixinStageConfig(commonPipelineEnvironment, stageName, Set filter = null){
Map stageConfiguration = ConfigurationLoader.stageConfiguration([commonPipelineEnvironment: commonPipelineEnvironment], stageName)
return mixin(stageConfiguration, filter)
}
ConfigurationHelper mixinStepConfig(commonPipelineEnvironment, Set filter = null){ ConfigurationHelper mixinStepConfig(commonPipelineEnvironment, Set filter = null){
if(!name) throw new IllegalArgumentException('Step has no public name property!') if(!name) throw new IllegalArgumentException('Step has no public name property!')
Map stepConfiguration = ConfigurationLoader.stepConfiguration([commonPipelineEnvironment: commonPipelineEnvironment], name) Map stepConfiguration = ConfigurationLoader.stepConfiguration([commonPipelineEnvironment: commonPipelineEnvironment], name)
@ -36,6 +47,26 @@ class ConfigurationHelper implements Serializable {
return this return this
} }
Map dependingOn(dependentKey){
return [
mixin: {key ->
def dependentValue = config[dependentKey]
if(config[key] == null && dependentValue && config[dependentValue])
config[key] = config[dependentValue][key]
return this
}
]
}
ConfigurationHelper addIfEmpty(key, value){
if (config[key] instanceof Boolean) {
return this
} else if (!config[key]){
config[key] = value
}
return this
}
Map use(){ return config } Map use(){ return config }
ConfigurationHelper(Map config = [:]){ ConfigurationHelper(Map config = [:]){
@ -76,7 +107,7 @@ class ConfigurationHelper implements Serializable {
return false return false
} }
def getMandatoryProperty(key, defaultValue) { def getMandatoryProperty(key, defaultValue = null) {
def paramValue = config[key] def paramValue = config[key]
@ -84,11 +115,12 @@ class ConfigurationHelper implements Serializable {
paramValue = defaultValue paramValue = defaultValue
if (paramValue == null) if (paramValue == null)
throw new Exception("ERROR - NO VALUE AVAILABLE FOR ${key}") throw new IllegalArgumentException("ERROR - NO VALUE AVAILABLE FOR ${key}")
return paramValue return paramValue
} }
def getMandatoryProperty(key) { def withMandatoryProperty(key){
return getMandatoryProperty(key, null) getMandatoryProperty(key)
return this
} }
} }

View File

@ -1,11 +1,12 @@
package com.sap.piper package com.sap.piper
import groovy.test.GroovyAssert import groovy.test.GroovyAssert
import org.junit.Assert
import org.junit.Test
import static org.hamcrest.Matchers.* import static org.hamcrest.Matchers.*
import org.junit.Assert
import org.junit.Test
class ConfigurationHelperTest { class ConfigurationHelperTest {
private static getConfiguration() { private static getConfiguration() {
@ -72,4 +73,45 @@ class ConfigurationHelperTest {
Assert.assertThat(config, hasEntry('property2', '28')) Assert.assertThat(config, hasEntry('property2', '28'))
Assert.assertThat(config, not(hasKey('property3'))) Assert.assertThat(config, not(hasKey('property3')))
} }
@Test
void testConfigurationLoaderWithBooleanValue() {
Map config = new ConfigurationHelper([property1: '27'])
.mixin([property1: false])
.mixin([property2: false])
.use()
// asserts
Assert.assertThat(config, hasEntry('property1', false))
Assert.assertThat(config, hasEntry('property2', false))
}
@Test
void testConfigurationLoaderWithMixinDependent() {
Map config = new ConfigurationHelper([
type: 'maven',
maven: [dockerImage: 'mavenImage', dockerWorkspace: 'mavenWorkspace'],
npm: [dockerImage: 'npmImage', dockerWorkspace: 'npmWorkspace', executeDocker: true, executeDocker3: false],
executeDocker1: true
])
.mixin([dockerImage: 'anyImage', type: 'npm', type2: 'npm', type3: '', executeDocker: false, executeDocker1: false, executeDocker2: false])
.dependingOn('type').mixin('dockerImage')
// test with empty dependent value
.dependingOn('type3').mixin('dockerWorkspace')
// test with empty dependent key
.dependingOn('type4').mixin('dockerWorkspace')
// test with empty default dependent value
.dependingOn('type2').mixin('dockerWorkspace')
// test with boolean value
.dependingOn('type').mixin('executeDocker')
.dependingOn('type').mixin('executeDocker2')
.dependingOn('type').mixin('executeDocker3')
.use()
// asserts
Assert.assertThat(config, hasEntry('dockerImage', 'anyImage'))
Assert.assertThat(config, hasEntry('dockerWorkspace', 'npmWorkspace'))
Assert.assertThat(config, hasEntry('executeDocker', false))
Assert.assertThat(config, hasEntry('executeDocker1', false))
Assert.assertThat(config, hasEntry('executeDocker2', false))
Assert.assertThat(config, hasEntry('executeDocker3', false))
}
} }