Skip to content

Commit

Permalink
Merge pull request #177 from marcusholl/pr/customMessageForWithMandat…
Browse files Browse the repository at this point in the history
…oryParameter

withMandatoryParameter supports a error message from the caller
  • Loading branch information
marcusholl committed Jul 4, 2018
2 parents 0935d76 + aa900d7 commit 99c6012
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/com/sap/piper/ConfigurationHelper.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -107,20 +107,22 @@ class ConfigurationHelper implements Serializable {
return false
}

def getMandatoryProperty(key, defaultValue = null) {
def getMandatoryProperty(key, defaultValue = null, errorMessage = null) {

def paramValue = config[key]

if (paramValue == null)
paramValue = defaultValue

if (paramValue == null)
throw new IllegalArgumentException("ERROR - NO VALUE AVAILABLE FOR ${key}")
if (paramValue == null) {
if(! errorMessage) errorMessage = "ERROR - NO VALUE AVAILABLE FOR ${key}"
throw new IllegalArgumentException(errorMessage)
}
return paramValue
}

def withMandatoryProperty(key){
getMandatoryProperty(key)
def withMandatoryProperty(key, errorMessage = null){
getMandatoryProperty(key, null, errorMessage)
return this
}
}
34 changes: 34 additions & 0 deletions test/groovy/com/sap/piper/ConfigurationHelperTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,15 @@ import groovy.test.GroovyAssert
import static org.hamcrest.Matchers.*

import org.junit.Assert
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException

class ConfigurationHelperTest {

@Rule
public ExpectedException thrown = ExpectedException.none()

private static getConfiguration() {
Map configuration = [dockerImage: 'maven:3.2-jdk-8-onbuild']
return configuration
Expand Down Expand Up @@ -114,4 +119,33 @@ class ConfigurationHelperTest {
Assert.assertThat(config, hasEntry('executeDocker2', false))
Assert.assertThat(config, hasEntry('executeDocker3', false))
}

@Test
public void testWithMandoryParameterReturnDefaultFailureMessage() {

thrown.expect(IllegalArgumentException)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR myKey')

new ConfigurationHelper().withMandatoryProperty('myKey')
}

@Test
public void testWithMandoryParameterReturnCustomerFailureMessage() {

thrown.expect(IllegalArgumentException)
thrown.expectMessage('My error message')

new ConfigurationHelper().withMandatoryProperty('myKey', 'My error message')
}

@Test
public void testWithMandoryParameterDefaultCustomFailureMessageProvidedSucceeds() {
new ConfigurationHelper([myKey: 'myValue']).withMandatoryProperty('myKey', 'My error message')
}

@Test
public void testWithMandoryParameterDefaultCustomFailureMessageNotProvidedSucceeds() {
new ConfigurationHelper([myKey: 'myValue']).withMandatoryProperty('myKey')
}

}

0 comments on commit 99c6012

Please sign in to comment.