1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2025-03-05 15:15:44 +02:00

Merge pull request #204 from marcusholl/pr/configurationHelperRequiredParameterFailsLate

Report all missing mandatory parameters instead of only the first on
This commit is contained in:
Marcus Holl 2018-09-27 11:21:00 +02:00 committed by GitHub
commit ced73231fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 64 additions and 1 deletions

View File

@ -12,6 +12,8 @@ class ConfigurationHelper implements Serializable {
private Map config = [:]
private String name
private Map validationResults = null
ConfigurationHelper(Script step){
name = step.STEP_NAME
if(!name) throw new IllegalArgumentException('Step has no public name property!')
@ -28,6 +30,11 @@ class ConfigurationHelper implements Serializable {
return this
}
ConfigurationHelper collectValidationFailures() {
validationResults = validationResults ?: [:]
return this
}
ConfigurationHelper mixinGeneralConfig(commonPipelineEnvironment, Set filter = null, Script step = null, Map compatibleParameters = [:]){
Map stepConfiguration = ConfigurationLoader.generalConfiguration([commonPipelineEnvironment: commonPipelineEnvironment])
return mixin(stepConfiguration, filter, step, compatibleParameters)
@ -101,6 +108,7 @@ class ConfigurationHelper implements Serializable {
@NonCPS // required because we have a closure in the
// method body that cannot be CPS transformed
Map use(){
handleValidationFailures()
MapUtils.traverse(config, { v -> (v instanceof GString) ? v.toString() : v })
return config
}
@ -140,7 +148,12 @@ class ConfigurationHelper implements Serializable {
if (paramValue == null) {
if(! errorMessage) errorMessage = "ERROR - NO VALUE AVAILABLE FOR ${key}"
throw new IllegalArgumentException(errorMessage)
def iae = new IllegalArgumentException(errorMessage)
if(validationResults == null) {
throw iae
}
validationResults.put(key, iae)
}
}
@ -153,4 +166,16 @@ class ConfigurationHelper implements Serializable {
}
return this
}
@NonCPS
private handleValidationFailures() {
if(! validationResults) return
if(validationResults.size() == 1) throw validationResults.values().first()
String msg = 'ERROR - NO VALUE AVAILABLE FOR: ' +
(validationResults.keySet().stream().collect() as Iterable).join(', ')
IllegalArgumentException iae = new IllegalArgumentException(msg)
validationResults.each { e -> iae.addSuppressed(e.value) }
throw iae
}
}

View File

@ -269,4 +269,42 @@ class ConfigurationHelperTest {
assert config.c instanceof java.lang.String
assert config.nextLevel.b instanceof java.lang.String
}
@Test
public void testWithMandatoryParameterCollectFailuresAllParamtersArePresentResultsInNoExceptionThrown() {
new ConfigurationHelper([myKey1: 'a', myKey2: 'b'])
.collectValidationFailures()
.withMandatoryProperty('myKey1')
.withMandatoryProperty('myKey2')
.use()
}
@Test
public void testWithMandatoryParameterCollectFailuresMultipleMissingParametersDoNotResultInFailuresDuringWithMandatoryProperties() {
new ConfigurationHelper([:]).collectValidationFailures()
.withMandatoryProperty('myKey1')
.withMandatoryProperty('myKey2')
}
@Test
public void testWithMandatoryParameterCollectFailuresMultipleMissingParametersResultsInFailureDuringUse() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR: myKey2, myKey3')
new ConfigurationHelper([myKey1:'a']).collectValidationFailures()
.withMandatoryProperty('myKey1')
.withMandatoryProperty('myKey2')
.withMandatoryProperty('myKey3')
.use()
}
@Test
public void testWithMandatoryParameterCollectFailuresOneMissingParametersResultsInFailureDuringUse() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR myKey2')
new ConfigurationHelper([myKey1:'a']).collectValidationFailures()
.withMandatoryProperty('myKey1')
.withMandatoryProperty('myKey2')
.use()
}
}