Merge pull request #237 from SAP/CCFenner-patch-2

ConfigurationHelper: add condition to withMandatoryProperty
This commit is contained in:
Christopher Fenner
2018-08-01 10:43:11 +02:00
committed by GitHub
2 changed files with 28 additions and 2 deletions
+7 -2
View File
@@ -162,8 +162,13 @@ class ConfigurationHelper implements Serializable {
return paramValue
}
def withMandatoryProperty(key, errorMessage = null){
getMandatoryProperty(key, null, errorMessage)
def withMandatoryProperty(key, errorMessage = null, condition = null){
if(condition){
if(condition(this.config))
getMandatoryProperty(key, null, errorMessage)
}else{
getMandatoryProperty(key, null, errorMessage)
}
return this
}
}
@@ -248,4 +248,25 @@ class ConfigurationHelperTest {
public void testWithMandoryParameterDefaultCustomFailureMessageNotProvidedSucceeds() {
new ConfigurationHelper([myKey: 'myValue']).withMandatoryProperty('myKey')
}
@Test
public void testWithMandoryWithFalseCondition() {
new ConfigurationHelper([verify: false])
.withMandatoryProperty('missingKey', null, { c -> return c.get('verify') })
}
@Test
public void testWithMandoryWithTrueConditionMissingValue() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR missingKey')
new ConfigurationHelper([verify: true])
.withMandatoryProperty('missingKey', null, { c -> return c.get('verify') })
}
@Test
public void testWithMandoryWithTrueConditionExistingValue() {
new ConfigurationHelper([existingKey: 'anyValue', verify: true])
.withMandatoryProperty('existingKey', null, { c -> return c.get('verify') })
}
}