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/UtilsTest.groovy
Marcus Holl ef0b1bd9dd [refactoring] Rule handling
Starting point for that refactoring: it turned out that the tests
was not independent. The DefaultValueCache which is a singleton
keeps the status over various tests. Success of test execution depends
on the order test execution.

We have now
  * a dedicated rule for resetting the default value cache
  * JenkinsConfiguration rule (which already provided facilities for
    dealing with the configuration) has been replaced by a readYaml rule.
    From the PipelineUnit test framework we get already a handler for
    libraryResource, which is also part of the setup of the default
    values.
  * An auxiliar class which combines the
      * JenkinsSetupRule (registers the lib)
      * JenkinsReadYamlRule (provides facilities for Yaml parsing)
      * JenkinsResetDefaultValueCacheRule (cleans up the DefaultValueCache)
    into a rule chain. By using this rule chain we ensure that our
    setup OK (piper lib registered, and default config can be setup in
    a clean way).
2018-01-29 09:42:23 +01:00

76 lines
2.1 KiB
Groovy

package com.sap.piper
import com.lesfurets.jenkins.unit.BasePipelineTest
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
import util.Rules
import util.SharedLibraryCreator
import static org.hamcrest.Matchers.containsString
import static org.hamcrest.Matchers.hasSize
import static org.junit.Assert.assertEquals
import static org.junit.Assert.assertThat
class UtilsTest extends BasePipelineTest {
@Rule
public ExpectedException exception = ExpectedException.none()
@Rule
public RuleChain rules = Rules.getCommonRules(this, SharedLibraryCreator.lazyLoadedLibrary)
Utils utils
@Before
void init() throws Exception {
utils = new Utils()
prepareObjectInterceptors(utils)
}
void prepareObjectInterceptors(object) {
object.metaClass.invokeMethod = helper.getMethodInterceptor()
object.metaClass.static.invokeMethod = helper.getMethodInterceptor()
object.metaClass.methodMissing = helper.getMethodMissingInterceptor()
}
@Test
void testGetMandatoryParameterValid() {
def sourceMap = [test1: 'value1', test2: 'value2']
def defaultFallbackMap = [myDefault1: 'default1']
assertEquals('value1', utils.getMandatoryParameter(sourceMap, 'test1', null))
assertEquals('value1', utils.getMandatoryParameter(sourceMap, 'test1', ''))
assertEquals('value1', utils.getMandatoryParameter(sourceMap, 'test1', 'customValue'))
}
@Test
void testGetMandatoryParameterDefaultFallback() {
def myMap = [test1: 'value1', test2: 'value2']
assertEquals('', utils.getMandatoryParameter(myMap, 'test3', ''))
assertEquals('customValue', utils.getMandatoryParameter(myMap, 'test3', 'customValue'))
}
@Test
void testGetMandatoryParameterFail() {
def myMap = [test1: 'value1', test2: 'value2']
exception.expect(Exception.class)
exception.expectMessage("ERROR - NO VALUE AVAILABLE FOR")
utils.getMandatoryParameter(myMap, 'test3', null)
}
}