From 4ca3236ba4bbdc22d185235f0300454dbf5e1aba Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Wed, 30 May 2018 10:45:55 +0200 Subject: [PATCH] general default section mixed in automatically. --- src/com/sap/piper/ConfigurationHelper.groovy | 44 +++++++++++++++--- .../sap/piper/ConfigurationHelperTest.groovy | 46 ++++++++++++++++++- 2 files changed, 82 insertions(+), 8 deletions(-) diff --git a/src/com/sap/piper/ConfigurationHelper.groovy b/src/com/sap/piper/ConfigurationHelper.groovy index 59275950f..a178fd673 100644 --- a/src/com/sap/piper/ConfigurationHelper.groovy +++ b/src/com/sap/piper/ConfigurationHelper.groovy @@ -7,7 +7,7 @@ class ConfigurationHelper implements Serializable { .loadDefaults() } - private Map config + private Map config = [:] private String name ConfigurationHelper(Script step){ @@ -21,10 +21,21 @@ class ConfigurationHelper implements Serializable { } private final ConfigurationHelper loadDefaults(){ - config = ConfigurationLoader.defaultStepConfiguration(null, name) + config = ConfigurationLoader.defaultGeneralConfiguration() + mixin(ConfigurationLoader.defaultStepConfiguration(null, name)) 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){ if(!name) throw new IllegalArgumentException('Step has no public name property!') Map stepConfiguration = ConfigurationLoader.stepConfiguration([commonPipelineEnvironment: commonPipelineEnvironment], name) @@ -36,6 +47,26 @@ class ConfigurationHelper implements Serializable { 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 } ConfigurationHelper(Map config = [:]){ @@ -76,7 +107,7 @@ class ConfigurationHelper implements Serializable { return false } - def getMandatoryProperty(key, defaultValue) { + def getMandatoryProperty(key, defaultValue = null) { def paramValue = config[key] @@ -84,11 +115,12 @@ class ConfigurationHelper implements Serializable { paramValue = defaultValue if (paramValue == null) - throw new Exception("ERROR - NO VALUE AVAILABLE FOR ${key}") + throw new IllegalArgumentException("ERROR - NO VALUE AVAILABLE FOR ${key}") return paramValue } - def getMandatoryProperty(key) { - return getMandatoryProperty(key, null) + def withMandatoryProperty(key){ + getMandatoryProperty(key) + return this } } diff --git a/test/groovy/com/sap/piper/ConfigurationHelperTest.groovy b/test/groovy/com/sap/piper/ConfigurationHelperTest.groovy index 580756fee..fbbb43dd2 100644 --- a/test/groovy/com/sap/piper/ConfigurationHelperTest.groovy +++ b/test/groovy/com/sap/piper/ConfigurationHelperTest.groovy @@ -1,11 +1,12 @@ package com.sap.piper import groovy.test.GroovyAssert -import org.junit.Assert -import org.junit.Test import static org.hamcrest.Matchers.* +import org.junit.Assert +import org.junit.Test + class ConfigurationHelperTest { private static getConfiguration() { @@ -72,4 +73,45 @@ class ConfigurationHelperTest { Assert.assertThat(config, hasEntry('property2', '28')) 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)) + } }