diff --git a/documentation/bin/createDocu.groovy b/documentation/bin/createDocu.groovy index 1c52b6696..3532c394e 100644 --- a/documentation/bin/createDocu.groovy +++ b/documentation/bin/createDocu.groovy @@ -6,6 +6,8 @@ import com.sap.piper.GenerateDocumentation import java.util.regex.Matcher import groovy.text.StreamingTemplateEngine +import com.sap.piper.MapUtils + // // Collects helper functions for rendering the documentation // @@ -375,14 +377,6 @@ class Helper { return mappings } - static getValue(Map config, List pPath) { - def p = config[pPath.head()] - if(pPath.size() == 1) return p // there is no tail - if(p in Map) getValue(p, pPath.tail()) - return null // there is a remaining path which could not be resolved. - // the value we are looking for does not exist. - } - static resolveDocuRelevantSteps(GroovyScriptEngine gse, File stepsDir) { def docuRelevantSteps = [] @@ -641,7 +635,7 @@ def handleStep(stepName, prepareDefaultValuesStep, gse, customDefaults) { it -> - def defaultValue = Helper.getValue(defaultConfig, it.tokenize('/')) + def defaultValue = MapUtils.getByPath(defaultConfig, it) def parameterProperties = [ defaultValue: defaultValue, @@ -676,7 +670,7 @@ def handleStep(stepName, prepareDefaultValuesStep, gse, customDefaults) { [ dependentParameterKey: dependentParameterKey, key: possibleValue, - value: Helper.getValue(defaultConfig.get(possibleValue), k.tokenize('/')) + value: MapUtils.getByPath(defaultConfig.get(possibleValue), k) ] } } diff --git a/src/com/sap/piper/MapUtils.groovy b/src/com/sap/piper/MapUtils.groovy index d3cd32956..657d82215 100644 --- a/src/com/sap/piper/MapUtils.groovy +++ b/src/com/sap/piper/MapUtils.groovy @@ -63,6 +63,17 @@ class MapUtils implements Serializable { m.putAll(updates) } + static private def getByPath(Map m, def key) { + List path = key in CharSequence ? key.tokenize('/') : key + + def value = m.get(path.head()) + + if (path.size() == 1) return value + if (value in Map) return getByPath(value, path.tail()) + + return null + } + /* * Provides a new map with the same content like the original map. * Nested Collections and Maps are copied. Values with are not diff --git a/test/groovy/com/sap/piper/MapUtilsTest.groovy b/test/groovy/com/sap/piper/MapUtilsTest.groovy index 6c63ccd14..a7b0415ef 100644 --- a/test/groovy/com/sap/piper/MapUtilsTest.groovy +++ b/test/groovy/com/sap/piper/MapUtilsTest.groovy @@ -3,6 +3,9 @@ package com.sap.piper import org.junit.Assert import org.junit.Test +import static org.hamcrest.Matchers.is +import static org.junit.Assert.assertThat + class MapUtilsTest { @Test @@ -51,6 +54,15 @@ class MapUtilsTest { assert m == [a: 'replaced', m: [b: 'replaced', c: 'otherString']] } + @Test + void testGetByPath() { + Map m = [trees: [oak: 5, beech :1], flowers:[rose: 23]] + + assertThat(MapUtils.getByPath(m, 'flowers'), is([rose: 23])) + assertThat(MapUtils.getByPath(m, 'trees/oak'), is(5)) + assertThat(MapUtils.getByPath(m, 'trees/palm'), is(null)) + } + @Test void testDeepCopy() { diff --git a/test/groovy/templates/PiperInitRunStageConfigurationTest.groovy b/test/groovy/templates/PiperInitRunStageConfigurationTest.groovy index c6d2263a1..b12ff1aae 100644 --- a/test/groovy/templates/PiperInitRunStageConfigurationTest.groovy +++ b/test/groovy/templates/PiperInitRunStageConfigurationTest.groovy @@ -99,7 +99,7 @@ stages: stepConditions: firstStep: config: testGeneral - testStage2: + testStage2: stepConditions: secondStep: config: testStage @@ -107,7 +107,7 @@ stages: stepConditions: thirdStep: config: testStep - + ''' } else { return ''' @@ -155,23 +155,23 @@ stages: testStage1: stepConditions: firstStep: - config: + config: testGeneral: - myValx - - myVal1 - testStage2: + - myVal1 + testStage2: stepConditions: secondStep: - config: - testStage: + config: + testStage: - maValXyz testStage3: stepConditions: thirdStep: - config: + config: testStep: - myVal3 - + ''' } else { return ''' @@ -218,18 +218,18 @@ stages: testStage1: stepConditions: firstStep: - configKeys: + configKeys: - myKey1_1 - - myKey1_2 - testStage2: + - myKey1_2 + testStage2: stepConditions: secondStep: - configKeys: + configKeys: - myKey2_1 testStage3: stepConditions: thirdStep: - configKeys: + configKeys: - myKey3_1 ''' } else { @@ -451,27 +451,4 @@ steps: {} assertThat(nullScript.commonPipelineEnvironment.configuration.runStage.Acceptance, is(true)) } - - @Test - void testGetConfigValue() { - - def config = [ - invalidKey: 'invalidValue', - stringKey: 'stringValue', - listKey: [ - 'listValue1', - 'listValue2' - ], - nested: [ - key: 'nestedValue' - ] - ] - - assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'stringKey'), is('stringValue')) - assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'listKey'), is(['listValue1','listValue2'])) - assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'nested/key'), is('nestedValue')) - assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'invalidKey/key'), is(nullValue())) - - //assertThat(jsr.step.piperInitRunStageConfiguration.getConfigValue(config, 'nested/key'), is('nestedValue')) - } } diff --git a/vars/piperInitRunStageConfiguration.groovy b/vars/piperInitRunStageConfiguration.groovy index c89197c28..6fcf51786 100644 --- a/vars/piperInitRunStageConfiguration.groovy +++ b/vars/piperInitRunStageConfiguration.groovy @@ -3,6 +3,7 @@ import com.sap.piper.ConfigurationLoader import static com.sap.piper.Prerequisites.checkScript import com.sap.piper.ConfigurationHelper +import com.sap.piper.MapUtils import groovy.transform.Field @Field String STEP_NAME = getClass().getName() @@ -65,27 +66,27 @@ void call(Map parameters = [:]) { case 'config': if (condition.getValue() instanceof Map) { condition.getValue().each {configCondition -> - if (getConfigValue(stepConfig, configCondition.getKey()) in configCondition.getValue()) { + if (MapUtils.getByPath(stepConfig, configCondition.getKey()) in configCondition.getValue()) { stepActive = true } } - } else if (getConfigValue(stepConfig, condition.getValue())) { + } else if (MapUtils.getByPath(stepConfig, condition.getValue())) { stepActive = true } break case 'configKeys': if (condition.getValue() instanceof List) { condition.getValue().each {configKey -> - if (getConfigValue(stepConfig, configKey)) { + if (MapUtils.getByPath(stepConfig, configKey)) { stepActive = true } } - } else if (getConfigValue(stepConfig, condition.getValue())) { + } else if (MapUtils.getByPath(stepConfig, condition.getValue())) { stepActive = true } break case 'filePatternFromConfig': - def conditionValue = getConfigValue(stepConfig, condition.getValue()) + def conditionValue = MapUtils.getByPath(stepConfig, condition.getValue()) if (conditionValue && findFiles(glob: conditionValue)) { stepActive = true } @@ -110,16 +111,3 @@ void call(Map parameters = [:]) { echo "[${STEP_NAME}] Debug - Run Step Configuration: ${script.commonPipelineEnvironment.configuration.runStep}" } } - -private def getConfigValue(Map stepConfig, def configKey) { - if (stepConfig == null) return null - - List configPath = configKey instanceof String ? configKey.tokenize('/') : configKey - - def configValue = stepConfig[configPath.head()] - - if (configPath.size() == 1) return configValue - if (configValue in Map) return getConfigValue(configValue, configPath.tail()) - - return null -}