1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-09-16 09:26:22 +02:00

Access to nested properties

yaml configuration supports nested properties. With this change
we can read those nested properties.
This commit is contained in:
Marcus Holl
2018-07-16 13:29:00 +02:00
parent 64291cd88f
commit 4f8f7be6aa
2 changed files with 40 additions and 4 deletions

View File

@@ -74,10 +74,7 @@ class ConfigurationHelper implements Serializable {
}
def getConfigProperty(key) {
if (config[key] != null && config[key].class == String) {
return config[key].trim()
}
return config[key]
return getConfigPropertyNested(config, key)
}
def getConfigProperty(key, defaultValue) {
@@ -88,6 +85,24 @@ class ConfigurationHelper implements Serializable {
return value
}
private getConfigPropertyNested(Map config, key) {
List parts = (key in String) ? (key as CharSequence).tokenize('/') : ([key] as List)
if(config[parts.head()] != null) {
if(config[parts.head()] in Map && parts.size() > 1) {
return getConfigPropertyNested(config[parts.head()], String.join('/', parts[1..parts.size()-1]))
}
if (config[parts.head()].class == String) {
return (config[parts.head()] as String).trim()
}
}
return config[parts.head()]
}
def isPropertyDefined(key){
def value = getConfigProperty(key)

View File

@@ -3,7 +3,10 @@ package com.sap.piper
import groovy.test.GroovyAssert
import static org.hamcrest.Matchers.*
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertThat
import org.hamcrest.Matchers
import org.junit.Assert
import org.junit.Rule
import org.junit.Test
@@ -29,6 +32,24 @@ class ConfigurationHelperTest {
Assert.assertFalse(configuration.isPropertyDefined('something'))
}
@Test
void testGetPropertyNestedLeafNodeIsString() {
def configuration = new ConfigurationHelper([a:[b: 'c']])
assertThat(configuration.getConfigProperty('a/b'), is('c'))
}
@Test
void testGetPropertyNestedLeafNodeIsMap() {
def configuration = new ConfigurationHelper([a:[b: [c: 'd']]])
assertThat(configuration.getConfigProperty('a/b'), is([c: 'd']))
}
@Test
void testGetPropertyNestedPathNotFound() {
def configuration = new ConfigurationHelper([a:[b: 'c']])
assertThat(configuration.getConfigProperty('a/c'), is((nullValue())))
}
@Test
void testIsPropertyDefined() {
def configuration = new ConfigurationHelper(getConfiguration())