2018-06-22 13:56:33 +02:00
|
|
|
import org.junit.After
|
|
|
|
import org.junit.Before
|
|
|
|
import org.junit.Rule
|
|
|
|
import org.junit.Test
|
|
|
|
import org.junit.rules.ExpectedException
|
|
|
|
import org.junit.rules.RuleChain
|
|
|
|
|
|
|
|
import com.sap.piper.GitUtils
|
|
|
|
import com.sap.piper.cm.ChangeManagement
|
|
|
|
import com.sap.piper.cm.ChangeManagementException
|
|
|
|
|
|
|
|
import hudson.AbortException
|
|
|
|
import util.BasePiperTest
|
|
|
|
import util.JenkinsStepRule
|
|
|
|
import util.Rules
|
|
|
|
|
|
|
|
class CheckChangeInDevelopmentTest extends BasePiperTest {
|
|
|
|
|
|
|
|
private ExpectedException thrown = ExpectedException.none()
|
|
|
|
private JenkinsStepRule jsr = new JenkinsStepRule(this)
|
|
|
|
|
|
|
|
@Rule
|
|
|
|
public RuleChain ruleChain = Rules
|
|
|
|
.getCommonRules(this)
|
|
|
|
.around(thrown)
|
|
|
|
.around(jsr)
|
|
|
|
|
|
|
|
@Before
|
|
|
|
public void setup() {
|
|
|
|
helper.registerAllowedMethod('usernamePassword', [Map], { Map m ->
|
|
|
|
binding.setProperty('username', 'defaultUser')
|
|
|
|
binding.setProperty('password', '********')
|
|
|
|
})
|
|
|
|
|
|
|
|
helper.registerAllowedMethod('withCredentials', [List, Closure], { List l, Closure c ->
|
|
|
|
c()
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
@After
|
|
|
|
public void tearDown() {
|
|
|
|
cmUtilReceivedParams.clear()
|
|
|
|
}
|
|
|
|
|
|
|
|
private Map cmUtilReceivedParams = [:]
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void changeIsInStatusDevelopmentTest() {
|
|
|
|
|
|
|
|
ChangeManagement cm = getChangeManagementUtils(true)
|
|
|
|
boolean inDevelopment = jsr.step.checkChangeInDevelopment(
|
|
|
|
cmUtils: cm,
|
|
|
|
endpoint: 'https://example.org/cm')
|
|
|
|
|
|
|
|
assert inDevelopment
|
|
|
|
|
|
|
|
assert cmUtilReceivedParams == [
|
|
|
|
changeId: '001',
|
|
|
|
endpoint: 'https://example.org/cm',
|
|
|
|
userName: 'defaultUser',
|
2018-06-25 12:37:33 +02:00
|
|
|
password: '********',
|
|
|
|
cmclientOpts: null
|
2018-06-22 13:56:33 +02:00
|
|
|
]
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void changeIsNotInStatusDevelopmentTest() {
|
|
|
|
|
|
|
|
thrown.expect(AbortException)
|
|
|
|
thrown.expectMessage("Change '001' is not in status 'in development'")
|
|
|
|
|
|
|
|
ChangeManagement cm = getChangeManagementUtils(false)
|
|
|
|
jsr.step.checkChangeInDevelopment(
|
|
|
|
cmUtils: cm,
|
|
|
|
endpoint: 'https://example.org/cm')
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void changeIsNotInStatusDevelopmentButWeWouldLikeToSkipFailureTest() {
|
|
|
|
|
|
|
|
ChangeManagement cm = getChangeManagementUtils(false)
|
|
|
|
boolean inDevelopment = jsr.step.checkChangeInDevelopment(
|
|
|
|
cmUtils: cm,
|
|
|
|
endpoint: 'https://example.org/cm',
|
|
|
|
failIfStatusIsNotInDevelopment: false)
|
|
|
|
assert !inDevelopment
|
|
|
|
}
|
|
|
|
|
2018-07-10 11:09:20 +02:00
|
|
|
@Test
|
|
|
|
public void ifChangeIdPresentAsParameterAndFromCommitsChangeIdFromParameterIsUsedTest() {
|
|
|
|
ChangeManagement cm = getChangeManagementUtils(true, '0815')
|
|
|
|
|
|
|
|
jsr.step.checkChangeInDevelopment(
|
|
|
|
changeDocumentId: '42',
|
|
|
|
cmUtils: cm,
|
|
|
|
endpoint: 'https://example.org/cm')
|
|
|
|
|
|
|
|
assert cmUtilReceivedParams.changeId == '42'
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void ifChangeIdNotPresentAsParameterButFromCommitsChangeIdFromCommitsIsUsedTest() {
|
|
|
|
ChangeManagement cm = getChangeManagementUtils(true, '0815')
|
|
|
|
|
|
|
|
jsr.step.checkChangeInDevelopment(
|
|
|
|
cmUtils: cm,
|
|
|
|
endpoint: 'https://example.org/cm')
|
|
|
|
|
|
|
|
assert cmUtilReceivedParams.changeId == '0815'
|
|
|
|
}
|
|
|
|
|
2018-06-22 13:56:33 +02:00
|
|
|
@Test
|
|
|
|
public void changeDocumentIdRetrievalFailsTest() {
|
|
|
|
|
|
|
|
thrown.expect(AbortException)
|
|
|
|
thrown.expectMessage('Something went wrong')
|
|
|
|
|
|
|
|
ChangeManagement cm = new ChangeManagement(nullScript, null) {
|
|
|
|
|
|
|
|
String getChangeDocumentId(
|
|
|
|
String filter,
|
|
|
|
String from,
|
|
|
|
String to,
|
|
|
|
String format) {
|
|
|
|
throw new ChangeManagementException('Something went wrong')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
jsr.step.checkChangeInDevelopment(
|
|
|
|
cmUtils: cm,
|
|
|
|
endpoint: 'https://example.org/cm')
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void nullChangeDocumentIdTest() {
|
|
|
|
|
2018-07-12 08:54:04 +02:00
|
|
|
thrown.expect(IllegalArgumentException)
|
|
|
|
thrown.expectMessage("No changeDocumentId provided. Neither via parameter 'changeDocumentId' " +
|
|
|
|
"nor via label 'configuration.gitChangeIdLabel' in commit range " +
|
|
|
|
"[from: origin/master, to: HEAD].")
|
2018-06-22 13:56:33 +02:00
|
|
|
|
|
|
|
ChangeManagement cm = getChangeManagementUtils(false, null)
|
|
|
|
jsr.step.checkChangeInDevelopment(
|
|
|
|
cmUtils: cm,
|
|
|
|
endpoint: 'https://example.org/cm')
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void emptyChangeDocumentIdTest() {
|
|
|
|
|
2018-07-12 08:54:04 +02:00
|
|
|
thrown.expect(IllegalArgumentException)
|
|
|
|
thrown.expectMessage("No changeDocumentId provided. Neither via parameter 'changeDocumentId' " +
|
|
|
|
"nor via label 'configuration.gitChangeIdLabel' in commit range " +
|
|
|
|
"[from: origin/master, to: HEAD].")
|
2018-06-22 13:56:33 +02:00
|
|
|
|
|
|
|
ChangeManagement cm = getChangeManagementUtils(false, '')
|
|
|
|
jsr.step.checkChangeInDevelopment(
|
|
|
|
cmUtils: cm,
|
|
|
|
endpoint: 'https://example.org/cm')
|
|
|
|
}
|
|
|
|
|
|
|
|
private ChangeManagement getChangeManagementUtils(boolean inDevelopment, String changeDocumentId = '001') {
|
|
|
|
|
|
|
|
return new ChangeManagement(nullScript, null) {
|
|
|
|
|
|
|
|
String getChangeDocumentId(
|
|
|
|
String filter,
|
|
|
|
String from,
|
|
|
|
String to,
|
|
|
|
String format) {
|
|
|
|
return changeDocumentId
|
|
|
|
}
|
|
|
|
|
2018-06-25 12:37:33 +02:00
|
|
|
boolean isChangeInDevelopment(String changeId, String endpoint, String userName, String password, String cmclientOpts) {
|
2018-06-22 13:56:33 +02:00
|
|
|
cmUtilReceivedParams.changeId = changeId
|
|
|
|
cmUtilReceivedParams.endpoint = endpoint
|
|
|
|
cmUtilReceivedParams.userName = userName
|
|
|
|
cmUtilReceivedParams.password = password
|
2018-06-25 12:37:33 +02:00
|
|
|
cmUtilReceivedParams.cmclientOpts = cmclientOpts
|
2018-06-22 13:56:33 +02:00
|
|
|
|
|
|
|
return inDevelopment
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|