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

Limit usage of ConfigurationHelper to the builder pattern

it is the duty of the configuration helper to build a map
providing the configuration. Having facilites to use the
ConfigurationHelper as some kind of configuration registry
makes life more complicated.
This commit is contained in:
Marcus Holl 2018-07-13 12:35:09 +02:00
parent 657ccf7d91
commit 726fbe36ab
2 changed files with 13 additions and 79 deletions

View File

@ -102,19 +102,11 @@ class ConfigurationHelper implements Serializable {
this.config = config this.config = config
} }
def getConfigProperty(key) { /* private */ def getConfigPropertyNested(key) {
return getConfigPropertyNested(config, key) return getConfigPropertyNested(config, key)
} }
def getConfigProperty(key, defaultValue) { /* private */ static getConfigPropertyNested(Map config, key) {
def value = getConfigProperty(key)
if (value == null) {
return defaultValue
}
return value
}
private getConfigPropertyNested(Map config, key) {
def separator = '/' def separator = '/'
@ -135,42 +127,22 @@ class ConfigurationHelper implements Serializable {
return config[parts.head()] return config[parts.head()]
} }
def isPropertyDefined(key){ private void existsMandatoryProperty(key, errorMessage) {
def value = getConfigProperty(key) def paramValue = getConfigPropertyNested(config, key)
if(value == null){
return false
}
if(value.class == String){
return value?.isEmpty() == false
}
if(value){
return true
}
return false
}
def getMandatoryProperty(key, defaultValue = null, errorMessage = null) {
def paramValue = getConfigProperty(key, defaultValue)
if (paramValue == null) { if (paramValue == null) {
if(! errorMessage) errorMessage = "ERROR - NO VALUE AVAILABLE FOR ${key}" if(! errorMessage) errorMessage = "ERROR - NO VALUE AVAILABLE FOR ${key}"
throw new IllegalArgumentException(errorMessage) throw new IllegalArgumentException(errorMessage)
} }
return paramValue
} }
def withMandatoryProperty(key, errorMessage = null, condition = null){ ConfigurationHelper withMandatoryProperty(key, errorMessage = null, condition = null){
if(condition){ if(condition){
if(condition(this.config)) if(condition(this.config))
getMandatoryProperty(key, null, errorMessage) existsMandatoryProperty(key, errorMessage)
}else{ }else{
getMandatoryProperty(key, null, errorMessage) existsMandatoryProperty(key, errorMessage)
} }
return this return this
} }

View File

@ -22,71 +22,33 @@ class ConfigurationHelperTest {
return configuration return configuration
} }
@Test
void testGetProperty() {
def configuration = new ConfigurationHelper(getConfiguration())
Assert.assertEquals('maven:3.2-jdk-8-onbuild', configuration.getConfigProperty('dockerImage'))
Assert.assertEquals('maven:3.2-jdk-8-onbuild', configuration.getConfigProperty('dockerImage', 'default'))
Assert.assertEquals('default', configuration.getConfigProperty('something', 'default'))
Assert.assertTrue(configuration.isPropertyDefined('dockerImage'))
Assert.assertFalse(configuration.isPropertyDefined('something'))
}
@Test @Test
void testGetPropertyNestedLeafNodeIsString() { void testGetPropertyNestedLeafNodeIsString() {
def configuration = new ConfigurationHelper([a:[b: 'c']]) assertThat(ConfigurationHelper.getConfigPropertyNested([a:[b: 'c']], 'a/b'), is('c'))
assertThat(configuration.getConfigProperty('a/b'), is('c'))
} }
@Test @Test
void testGetPropertyNestedLeafNodeIsMap() { void testGetPropertyNestedLeafNodeIsMap() {
def configuration = new ConfigurationHelper([a:[b: [c: 'd']]]) assertThat(ConfigurationHelper.getConfigPropertyNested([a:[b: [c: 'd']]], 'a/b'), is([c: 'd']))
assertThat(configuration.getConfigProperty('a/b'), is([c: 'd']))
} }
@Test @Test
void testGetPropertyNestedPathNotFound() { void testGetPropertyNestedPathNotFound() {
def configuration = new ConfigurationHelper([a:[b: 'c']]) assertThat(ConfigurationHelper.getConfigPropertyNested([a:[b: 'c']], 'a/c'), is((nullValue())))
assertThat(configuration.getConfigProperty('a/c'), is((nullValue())))
} }
void testGetPropertyNestedPathStartsWithTokenizer() { void testGetPropertyNestedPathStartsWithTokenizer() {
def configuration = new ConfigurationHelper([k:'v']) assertThat(ConfigurationHelper.getConfigPropertyNested([k:'v'], '/k'), is(('v')))
assertThat(configuration.getConfigProperty('/k'), is(('v')))
} }
@Test @Test
void testGetPropertyNestedPathEndsWithTokenizer() { void testGetPropertyNestedPathEndsWithTokenizer() {
def configuration = new ConfigurationHelper([k:'v']) assertThat(ConfigurationHelper.getConfigPropertyNested([k:'v'], 'k/'), is(('v')))
assertThat(configuration.getConfigProperty('k/'), is(('v')))
} }
@Test @Test
void testGetPropertyNestedPathManyTokenizer() { void testGetPropertyNestedPathManyTokenizer() {
def configuration = new ConfigurationHelper([k1:[k2 : 'v']]) assertThat(ConfigurationHelper.getConfigPropertyNested([k1:[k2 : 'v']], '///k1/////k2///'), is(('v')))
assertThat(configuration.getConfigProperty('///k1/////k2///'), is(('v')))
}
@Test
void testIsPropertyDefined() {
def configuration = new ConfigurationHelper(getConfiguration())
Assert.assertTrue(configuration.isPropertyDefined('dockerImage'))
Assert.assertFalse(configuration.isPropertyDefined('something'))
}
@Test
void testIsPropertyDefinedWithInteger() {
def configuration = new ConfigurationHelper([dockerImage: 3])
Assert.assertTrue(configuration.isPropertyDefined('dockerImage'))
}
@Test
void testGetMandatoryProperty() {
def configuration = new ConfigurationHelper(getConfiguration())
Assert.assertEquals('maven:3.2-jdk-8-onbuild', configuration.getMandatoryProperty('dockerImage'))
Assert.assertEquals('default', configuration.getMandatoryProperty('something', 'default'))
GroovyAssert.shouldFail { configuration.getMandatoryProperty('something') }
} }
@Test @Test