Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

withMandatoryParameter supports a error message from the caller #177

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to make use of the SimpleTemplateEngine:

import groovy.text.SimpleTemplateEngine

def getMandatoryProperty(key, defaultValue = null, errorMessage = "ERROR - NO VALUE AVAILABLE FOR ${key}"){
  [...]
  errorMessage = SimpleTemplateEngine.newInstance()
    .createTemplate(errorMessage)
    .make([key: key]).toString()

  throw new IllegalArgumentException(errorMessage)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CCFenner mmmhhhhhh ... Sounds like shooting on sparrows with cannons. Should we do that all the time we create a string containing a parameter? We have thousands of similar statements in the code using groovy strings for that. What is bad with the "good old" groovy string approach? Would make sense to use a template engine for big strings, like html templates where we have to replace variables with values when performance is an issue. But for a very simple and short string. What would be your general recommendation for using a TemplateEngine vs. using the groovy string pattern?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, the key is handed in from the outside anyway...

}
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')
}

}