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

215 lines
7.2 KiB
Groovy
Raw Normal View History

import static org.hamcrest.Matchers.*
import static org.junit.Assert.assertThat
import org.junit.Before
import org.junit.After
import org.junit.Rule
import org.junit.Test
import org.junit.rules.ExpectedException
import org.junit.rules.RuleChain
2019-02-13 14:01:56 +01:00
import com.sap.piper.cm.BackendType
import com.sap.piper.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException
import com.sap.piper.Utils
import hudson.AbortException
import util.BasePiperTest
import util.JenkinsCredentialsRule
2018-09-28 13:45:26 +02:00
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
import util.JenkinsStepRule
import util.Rules
class CheckChangeInDevelopmentTest extends BasePiperTest {
private ExpectedException thrown = ExpectedException.none()
2019-01-22 09:25:42 +01:00
private JenkinsStepRule stepRule = new JenkinsStepRule(this)
private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
@Rule
public RuleChain ruleChain = Rules
.getCommonRules(this)
.around(new JenkinsReadYamlRule(this))
.around(thrown)
2019-01-22 09:25:42 +01:00
.around(stepRule)
.around(loggingRule)
.around(new JenkinsCredentialsRule(this)
.withCredentials('CM', 'anonymous', '********'))
@Before
public void setup() {
Utils.metaClass.echo = { def m -> }
nullScript.commonPipelineEnvironment.configuration = [general:
[changeManagement:
[
credentialsId: 'CM',
type: 'SOLMAN',
endpoint: 'https://example.org/cm'
]
]
]
helper.registerAllowedMethod('addBadge', [Map], {return})
helper.registerAllowedMethod('createSummary', [Map], {return})
}
@After
public void tearDown() {
cmUtilReceivedParams.clear()
Utils.metaClass = null
}
private Map cmUtilReceivedParams = [:]
@Test
public void changeIsInStatusDevelopmentTest() {
def calledWithParameters,
calledWithStepName
helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
params, stepName, metaData, creds ->
if(stepName.equals("isChangeInDevelopment")) {
nullScript.commonPipelineEnvironment.setValue('isChangeInDevelopment', true)
}
})
2019-01-22 09:25:42 +01:00
stepRule.step.checkChangeInDevelopment(
2018-10-25 08:41:50 +02:00
script: nullScript,
changeDocumentId: '001',
failIfStatusIsNotInDevelopment: true)
assertThat(nullScript.commonPipelineEnvironment.getValue('isChangeInDevelopment'), is(true))
// no exception in thrown, so the change is in status 'in development'.
}
@Test
public void changeIsNotInStatusDevelopmentTest() {
thrown.expect(AbortException)
thrown.expectMessage("Change '001' is not in status 'in development'")
def calledWithParameters,
calledWithStepName
helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
params, stepName, metaData, creds ->
if(stepName.equals("isChangeInDevelopment")) {
nullScript.commonPipelineEnvironment.setValue('isChangeInDevelopment', false)
}
})
2019-01-22 09:25:42 +01:00
stepRule.step.checkChangeInDevelopment(
script: nullScript,
changeDocumentId: '001',
failIfStatusIsNotInDevelopment: true)
}
@Test
public void changeIsNotInStatusDevelopmentButWeWouldLikeToSkipFailureTest() {
def calledWithParameters,
calledWithStepName
helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
params, stepName, metaData, creds ->
if(stepName.equals("isChangeInDevelopment")) {
nullScript.commonPipelineEnvironment.setValue('isChangeInDevelopment', false)
}
})
stepRule.step.checkChangeInDevelopment(
script: nullScript,
changeDocumentId: '001',
failIfStatusIsNotInDevelopment: false)
assertThat(nullScript.commonPipelineEnvironment.getValue('isChangeInDevelopment'), is(false))
}
@Test
public void nullChangeDocumentIdTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage("No changeDocumentId provided. Neither via parameter 'changeDocumentId' nor via " +
"label 'ChangeDocument\\s?:' in commit range [from: origin/master, to: HEAD].")
def calledWithParameters,
calledWithStepName
helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
params, stepName, metaData, creds ->
if(stepName.equals("transportRequestDocIDFromGit")) {
calledWithParameters = params
}
})
2019-01-22 09:25:42 +01:00
stepRule.step.checkChangeInDevelopment(
script: nullScript)
assertThat(calledWithParameters,is(not(null)))
}
@Test
public void emptyChangeDocumentIdTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage("No changeDocumentId provided. Neither via parameter 'changeDocumentId' " +
"nor via label 'ChangeDocument\\s?:' in commit range " +
"[from: origin/master, to: HEAD].")
def calledWithParameters,
calledWithStepName
helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
params, stepName, metaData, creds ->
if(stepName.equals("transportRequestDocIDFromGit")) {
calledWithParameters = params
nullScript.commonPipelineEnvironment.setValue('changeDocumentId', '')
}
})
2019-01-22 09:25:42 +01:00
stepRule.step.checkChangeInDevelopment(
script: nullScript)
assertThat(calledWithParameters,is(not(null)))
}
2018-09-28 13:45:26 +02:00
@Test
public void cmIntegrationSwichtedOffTest() {
loggingRule.expect('[INFO] Change management integration intentionally switched off.')
2018-09-28 13:45:26 +02:00
2019-01-22 09:25:42 +01:00
stepRule.step.checkChangeInDevelopment(
script: nullScript,
2018-09-28 13:45:26 +02:00
changeManagement: [type: 'NONE'])
}
2019-02-12 16:56:06 +01:00
@Test
2019-02-13 14:01:56 +01:00
public void stageConfigIsNotConsideredWithParamKeysTest() {
2019-02-12 16:56:06 +01:00
nullScript.commonPipelineEnvironment.configuration.stages = [foo:[changeDocumentId:'12345']]
2019-02-12 16:56:06 +01:00
2019-02-13 14:01:56 +01:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage('No changeDocumentId provided.')
def calledWithParameters,
calledWithStepName
helper.registerAllowedMethod('piperExecuteBin', [Map, String, String, List], {
params, stepName, metaData, creds ->
if(stepName.equals("transportRequestDocIDFromGit")) {
calledWithParameters = params
}
})
2019-02-12 16:56:06 +01:00
stepRule.step.checkChangeInDevelopment(
script: nullScript,
2019-02-13 14:01:56 +01:00
stageName: 'foo')
assertThat(calledWithParameters,is(not(null)))
}
}