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/TransportRequestUploadFileTest.groovy

224 lines
8.4 KiB
Groovy
Raw Normal View History

import java.util.Map
2018-06-20 11:46:28 +02:00
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.cm.ChangeManagement
import com.sap.piper.cm.ChangeManagementException
2018-06-20 11:46:28 +02:00
import util.BasePiperTest
import util.JenkinsStepRule
import util.JenkinsLoggingRule
import util.Rules
import hudson.AbortException
public class TransportRequestUploadFileTest extends BasePiperTest {
private ExpectedException thrown = new ExpectedException()
private JenkinsStepRule jsr = new JenkinsStepRule(this)
private JenkinsLoggingRule jlr = new JenkinsLoggingRule(this)
@Rule
public RuleChain ruleChain = Rules.getCommonRules(this)
.around(thrown)
.around(jsr)
.around(jlr)
private Map cmUtilReceivedParams = [:]
2018-06-20 11:46:28 +02:00
@Before
public void setup() {
helper.registerAllowedMethod('usernamePassword', [Map.class], {m -> return m})
helper.registerAllowedMethod('withCredentials', [List, Closure], { l, c ->
credentialsId = l[0].credentialsId
binding.setProperty('username', 'anonymous')
binding.setProperty('password', '********')
try {
c()
} finally {
binding.setProperty('username', null)
binding.setProperty('password', null)
}
})
cmUtilReceivedParams.clear()
2018-06-20 11:46:28 +02:00
nullScript.commonPipelineEnvironment.configuration = [steps:
[transportRequestUploadFile:
[
2018-06-28 16:24:14 +02:00
credentialsId: 'CM',
endpoint: 'https://example.org/cm'
2018-06-20 11:46:28 +02:00
]
]
]
}
@Test
public void changeDocumentIdNotProvidedTest() {
2018-06-20 11:46:28 +02:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId' or via commit history).")
2018-06-20 11:46:28 +02:00
ChangeManagement cm = new ChangeManagement(nullScript) {
String getChangeDocumentId(
String from,
String to,
String pattern,
String format
) {
throw new ChangeManagementException('Cannot retrieve changeId from git commits.')
}
}
jsr.step.call(script: nullScript, transportRequestId: '001', applicationId: 'app', filePath: '/path', cmUtils: cm)
2018-06-20 11:46:28 +02:00
}
@Test
public void transportRequestIdNotProvidedTest() {
ChangeManagement cm = new ChangeManagement(nullScript) {
String getTransportRequestId(
String from,
String to,
String pattern,
String format
) {
throw new ChangeManagementException('Cannot retrieve transport request id from git commits.')
}
}
thrown.expect(IllegalArgumentException)
thrown.expectMessage("Transport request id not provided (parameter: 'transportRequestId' or via commit history).")
2018-06-20 11:46:28 +02:00
jsr.step.call(script: nullScript, changeDocumentId: '001', applicationId: 'app', filePath: '/path', cmUtils: cm)
2018-06-20 11:46:28 +02:00
}
@Test
public void applicationIdNotProvidedTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR applicationId")
2018-06-20 11:46:28 +02:00
jsr.step.call(script: nullScript, changeDocumentId: '001', transportRequestId: '001', filePath: '/path')
2018-06-20 11:46:28 +02:00
}
@Test
public void filePathNotProvidedTest() {
thrown.expect(IllegalArgumentException)
thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR filePath")
2018-06-20 11:46:28 +02:00
jsr.step.call(script: nullScript, changeDocumentId: '001', transportRequestId: '001', applicationId: 'app')
2018-06-20 11:46:28 +02:00
}
@Test
public void uploadFileToTransportRequestFailureTest() {
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(String changeId,
String transportRequestId,
String applicationId,
String filePath,
String endpoint,
String username,
String password,
String cmclientOpts) {
throw new ChangeManagementException('Exception message')
}
}
2018-06-20 11:46:28 +02:00
thrown.expect(AbortException)
thrown.expectMessage("Exception message")
jsr.step.call(script: nullScript,
changeDocumentId: '001',
transportRequestId: '001',
applicationId: 'app',
filePath: '/path',
cmUtils: cm)
2018-06-20 11:46:28 +02:00
}
@Test
public void uploadFileToTransportRequestSuccessTest() {
jlr.expect("[INFO] Uploading file '/path' to transport request '002' of change document '001'.")
jlr.expect("[INFO] File '/path' has been successfully uploaded to transport request '002' of change document '001'.")
2018-06-20 11:46:28 +02:00
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(String changeId,
String transportRequestId,
String applicationId,
String filePath,
String endpoint,
String username,
String password,
String cmclientOpts) {
2018-06-20 11:46:28 +02:00
cmUtilReceivedParams.changeId = changeId
cmUtilReceivedParams.transportRequestId = transportRequestId
cmUtilReceivedParams.applicationId = applicationId
cmUtilReceivedParams.filePath = filePath
cmUtilReceivedParams.endpoint = endpoint
cmUtilReceivedParams.username = username
cmUtilReceivedParams.password = password
cmUtilReceivedParams.cmclientOpts = cmclientOpts
}
}
2018-06-20 11:46:28 +02:00
2018-07-13 14:40:29 +02:00
jsr.step.call(script: nullScript,
changeDocumentId: '001',
transportRequestId: '002',
2018-07-13 14:40:29 +02:00
applicationId: 'app',
filePath: '/path',
cmUtils: cm)
assert cmUtilReceivedParams ==
[
changeId: '001',
transportRequestId: '002',
applicationId: 'app',
filePath: '/path',
endpoint: 'https://example.org/cm',
username: 'anonymous',
password: '********',
cmclientOpts: null
]
2018-06-20 11:46:28 +02:00
}
@Test
public void uploadFileToTransportRequestUploadFailureTest() {
thrown.expect(AbortException)
thrown.expectMessage('Upload failure.')
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(String changeId,
String transportRequestId,
String applicationId,
String filePath,
String endpoint,
String username,
String password,
String cmclientOpts) {
throw new ChangeManagementException('Upload failure.')
}
}
2018-06-20 11:46:28 +02:00
jsr.step.call(script: nullScript,
changeDocumentId: '001',
transportRequestId: '001',
applicationId: 'app',
filePath: '/path',
cmUtils: cm)
2018-06-20 11:46:28 +02:00
}
2018-06-20 11:46:28 +02:00
}