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

311 lines
12 KiB
Groovy
Raw Normal View History

package com.sap.piper
import groovy.test.GroovyAssert
2018-03-14 16:44:04 +02:00
import static org.hamcrest.Matchers.*
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertThat
2018-03-14 16:44:04 +02:00
import 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
}
@Test
void testGetProperty() {
def configuration = new ConfigurationHelper(getConfiguration())
Assert.assertEquals('maven:3.2-jdk-8-onbuild', configuration.getConfigProperty('dockerImage'))
2018-02-13 19:55:26 +02:00
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
void testGetPropertyNestedLeafNodeIsString() {
def configuration = new ConfigurationHelper([a:[b: 'c']])
assertThat(configuration.getConfigProperty('a/b'), is('c'))
}
@Test
void testGetPropertyNestedLeafNodeIsMap() {
def configuration = new ConfigurationHelper([a:[b: [c: 'd']]])
assertThat(configuration.getConfigProperty('a/b'), is([c: 'd']))
}
@Test
void testGetPropertyNestedPathNotFound() {
def configuration = new ConfigurationHelper([a:[b: 'c']])
assertThat(configuration.getConfigProperty('a/c'), is((nullValue())))
}
void testGetPropertyNestedPathStartsWithTokenizer() {
def configuration = new ConfigurationHelper([k:'v'])
assertThat(configuration.getConfigProperty('/k'), is(('v')))
}
@Test
void testGetPropertyNestedPathEndsWithTokenizer() {
def configuration = new ConfigurationHelper([k:'v'])
assertThat(configuration.getConfigProperty('k/'), is(('v')))
}
@Test
void testGetPropertyNestedPathManyTokenizer() {
def configuration = new ConfigurationHelper([k1:[k2 : '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'))
}
2018-02-13 19:55:26 +02:00
@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') }
}
2018-03-14 16:44:04 +02:00
@Test
void testConfigurationLoaderWithDefaults() {
2018-03-14 16:58:16 +02:00
Map config = new ConfigurationHelper([property1: '27']).use()
2018-03-14 16:44:04 +02:00
// asserts
2018-03-14 16:58:16 +02:00
Assert.assertThat(config, hasEntry('property1', '27'))
2018-03-14 16:44:04 +02:00
}
@Test
void testConfigurationLoaderWithCustomSettings() {
2018-03-14 16:58:16 +02:00
Map config = new ConfigurationHelper([property1: '27'])
.mixin([property1: '41'])
2018-03-14 16:44:04 +02:00
.use()
// asserts
2018-03-14 16:58:16 +02:00
Assert.assertThat(config, hasEntry('property1', '41'))
2018-03-14 16:44:04 +02:00
}
@Test
void testConfigurationLoaderWithFilteredCustomSettings() {
2018-03-14 16:53:22 +02:00
Set filter = ['property2']
2018-03-14 16:58:16 +02:00
Map config = new ConfigurationHelper([property1: '27'])
.mixin([property1: '41', property2: '28', property3: '29'], filter)
2018-03-14 16:44:04 +02:00
.use()
// asserts
2018-03-14 16:58:16 +02:00
Assert.assertThat(config, hasEntry('property1', '27'))
Assert.assertThat(config, hasEntry('property2', '28'))
Assert.assertThat(config, not(hasKey('property3')))
2018-03-14 16:44:04 +02:00
}
@Test
void testConfigurationLoaderWithBooleanValue() {
Map config = new ConfigurationHelper([property1: '27'])
.mixin([property1: false])
.mixin([property2: false])
.use()
// asserts
Assert.assertThat(config, hasEntry('property1', false))
Assert.assertThat(config, hasEntry('property2', false))
}
@Test
void testConfigurationLoaderWithMixinDependent() {
Map config = new ConfigurationHelper([
type: 'maven',
maven: [dockerImage: 'mavenImage', dockerWorkspace: 'mavenWorkspace'],
npm: [dockerImage: 'npmImage', dockerWorkspace: 'npmWorkspace', executeDocker: true, executeDocker3: false],
executeDocker1: true
])
.mixin([dockerImage: 'anyImage', type: 'npm', type2: 'npm', type3: '', executeDocker: false, executeDocker1: false, executeDocker2: false])
.dependingOn('type').mixin('dockerImage')
// test with empty dependent value
.dependingOn('type3').mixin('dockerWorkspace')
// test with empty dependent key
.dependingOn('type4').mixin('dockerWorkspace')
// test with empty default dependent value
.dependingOn('type2').mixin('dockerWorkspace')
// test with boolean value
.dependingOn('type').mixin('executeDocker')
.dependingOn('type').mixin('executeDocker2')
.dependingOn('type').mixin('executeDocker3')
.use()
// asserts
Assert.assertThat(config, hasEntry('dockerImage', 'anyImage'))
Assert.assertThat(config, hasEntry('dockerWorkspace', 'npmWorkspace'))
Assert.assertThat(config, hasEntry('executeDocker', false))
Assert.assertThat(config, hasEntry('executeDocker1', false))
Assert.assertThat(config, hasEntry('executeDocker2', false))
Assert.assertThat(config, hasEntry('executeDocker3', false))
}
@Test
void testHandleCompatibility() {
def configuration = new ConfigurationHelper()
.mixin([old1: 'oldValue1', old2: 'oldValue2', test: 'testValue'], null, null, [newStructure: [new1: 'old1', new2: 'old2']])
.use()
Assert.assertThat(configuration.size(), is(4))
Assert.assertThat(configuration.newStructure.new1, is('oldValue1'))
Assert.assertThat(configuration.newStructure.new2, is('oldValue2'))
}
@Test
void testHandleCompatibilityFlat() {
def configuration = new ConfigurationHelper()
.mixin([old1: 'oldValue1', old2: 'oldValue2', test: 'testValue'], null, null, [new1: 'old1', new2: 'old2'])
.use()
Assert.assertThat(configuration.size(), is(5))
Assert.assertThat(configuration.new1, is('oldValue1'))
Assert.assertThat(configuration.new2, is('oldValue2'))
}
@Test
void testHandleCompatibilityDeep() {
def configuration = new ConfigurationHelper()
.mixin([old1: 'oldValue1', old2: 'oldValue2', test: 'testValue'], null, null, [deep:[deeper:[newStructure: [new1: 'old1', new2: 'old2']]]])
.use()
Assert.assertThat(configuration.size(), is(4))
Assert.assertThat(configuration.deep.deeper.newStructure.new1, is('oldValue1'))
Assert.assertThat(configuration.deep.deeper.newStructure.new2, is('oldValue2'))
}
@Test
void testHandleCompatibilityNewAvailable() {
def configuration = new ConfigurationHelper([old1: 'oldValue1', newStructure: [new1: 'newValue1'], test: 'testValue'])
.mixin([old1: 'oldValue1', newStructure: [new1: 'newValue1'], test: 'testValue'], null, null, [newStructure: [new1: 'old1', new2: 'old2']])
.use()
Assert.assertThat(configuration.size(), is(3))
Assert.assertThat(configuration.newStructure.new1, is('newValue1'))
}
@Test
void testHandleCompatibilityOldNotSet() {
def configuration = new ConfigurationHelper([old1: null, test: 'testValue'])
.mixin([old1: null, test: 'testValue'], null, null, [newStructure: [new1: 'old1', new2: 'old2']])
.use()
Assert.assertThat(configuration.size(), is(2))
Assert.assertThat(configuration.newStructure.new1, is(null))
}
@Test
void testHandleCompatibilityNoneAvailable() {
def configuration = new ConfigurationHelper([old1: null, test: 'testValue'])
.mixin([test: 'testValue'], null, null, [newStructure: [new1: 'old1', new2: 'old2']])
.use()
Assert.assertThat(configuration.size(), is(2))
Assert.assertThat(configuration.newStructure.new1, is(null))
}
@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')
}
2018-08-01 08:30:10 +02:00
@Test
public void testWithMandoryWithFalseCondition() {
2018-08-01 08:50:21 +02:00
new ConfigurationHelper([verify: false])
.withMandatoryProperty('missingKey', null, { c -> return c.get('verify') })
2018-08-01 08:30:10 +02:00
}
@Test
public void testWithMandoryWithTrueConditionMissingValue() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage('ERROR - NO VALUE AVAILABLE FOR missingKey')
2018-08-01 08:50:21 +02:00
new ConfigurationHelper([verify: true])
.withMandatoryProperty('missingKey', null, { c -> return c.get('verify') })
2018-08-01 08:30:10 +02:00
}
@Test
public void testWithMandoryWithTrueConditionExistingValue() {
2018-08-01 08:50:21 +02:00
new ConfigurationHelper([existingKey: 'anyValue', verify: true])
.withMandatoryProperty('existingKey', null, { c -> return c.get('verify') })
2018-08-01 08:30:10 +02:00
}
@Test
public void testTelemetryConfigurationAvailable() {
Set filter = ['test']
def configuration = new ConfigurationHelper([test: 'testValue'])
.mixin([collectTelemetryData: false], filter)
.use()
Assert.assertThat(configuration, hasEntry('collectTelemetryData', false))
}
@Test
public void testGStringsAreReplacedByJavaLangStrings() {
//
// needed in order to ensure we have real GStrings.
// a GString not containing variables might be optimized to
// a java.lang.String from the very beginning.
def dummy = 'Dummy',
aGString = "a${dummy}",
bGString = "b${dummy}",
cGString = "c${dummy}"
assert aGString instanceof GString
assert bGString instanceof GString
assert cGString instanceof GString
def config = new ConfigurationHelper([a: aGString,
nextLevel: [b: bGString]])
.mixin([c : cGString])
.use()
assert config == [a: 'aDummy',
c: 'cDummy',
nextLevel: [b: 'bDummy']]
assert config.a instanceof java.lang.String
assert config.c instanceof java.lang.String
assert config.nextLevel.b instanceof java.lang.String
}
}