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

385 lines
15 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.BackendType
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.JenkinsCredentialsRule
2018-06-20 11:46:28 +02:00
import util.JenkinsStepRule
import util.JenkinsLoggingRule
import util.JenkinsReadYamlRule
2018-06-20 11:46:28 +02:00
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(new JenkinsReadYamlRule(this))
2018-06-20 11:46:28 +02:00
.around(jsr)
.around(jlr)
.around(new JenkinsCredentialsRule(this)
.withCredentials('CM', 'anonymous', '********'))
2018-06-20 11:46:28 +02:00
private Map cmUtilReceivedParams = [:]
2018-06-20 11:46:28 +02:00
@Before
public void setup() {
cmUtilReceivedParams.clear()
2018-06-20 11:46:28 +02:00
nullScript.commonPipelineEnvironment.configuration = [general:
[changeManagement:
2018-06-20 11:46:28 +02:00
[
2018-06-28 16:24:14 +02:00
credentialsId: 'CM',
type: 'SOLMAN',
2018-06-28 16:24:14 +02:00
endpoint: 'https://example.org/cm'
2018-06-20 11:46:28 +02:00
]
]
]
}
@Test
public void changeDocumentIdNotProvidedSOLMANTest() {
// we expect the failure only for SOLMAN (which is the default).
// Use case for CTS without change document id is checked by the
// straight forward test case for CTS
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.transportRequestUploadFile(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.transportRequestUploadFile(script: nullScript, changeDocumentId: '001', applicationId: 'app', filePath: '/path', cmUtils: cm)
2018-06-20 11:46:28 +02:00
}
@Test
public void applicationIdNotProvidedSOLMANTest() {
// we expect the failure only for SOLMAN (which is the default).
// Use case for CTS without applicationId is checked by the
// straight forward test case for CTS
2018-06-20 11:46:28 +02:00
thrown.expect(IllegalArgumentException)
thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR applicationId")
2018-06-20 11:46:28 +02:00
jsr.step.transportRequestUploadFile(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.transportRequestUploadFile(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(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
String filePath,
String endpoint,
String credentialsId,
String cmclientOpts) {
throw new ChangeManagementException('Exception message')
}
}
2018-06-20 11:46:28 +02:00
thrown.expect(AbortException)
thrown.expectMessage("Exception message")
jsr.step.transportRequestUploadFile(script: nullScript,
changeDocumentId: '001',
transportRequestId: '001',
applicationId: 'app',
filePath: '/path',
cmUtils: cm)
2018-06-20 11:46:28 +02:00
}
@Test
public void uploadFileToTransportRequestCTSSuccessTest() {
jlr.expect("[INFO] Uploading file '/path' to transport request '002'.")
jlr.expect("[INFO] File '/path' has been successfully uploaded to transport request '002'.")
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
String filePath,
String endpoint,
String credentialsId,
String cmclientOpts) {
cmUtilReceivedParams.type = type
cmUtilReceivedParams.changeId = changeId
cmUtilReceivedParams.transportRequestId = transportRequestId
cmUtilReceivedParams.applicationId = applicationId
cmUtilReceivedParams.filePath = filePath
cmUtilReceivedParams.endpoint = endpoint
cmUtilReceivedParams.credentialsId = credentialsId
cmUtilReceivedParams.cmclientOpts = cmclientOpts
}
}
jsr.step.transportRequestUploadFile(script: nullScript,
changeManagement: [type: 'CTS'],
transportRequestId: '002',
filePath: '/path',
cmUtils: cm)
assert cmUtilReceivedParams ==
[
type: BackendType.CTS,
changeId: null,
transportRequestId: '002',
applicationId: null,
filePath: '/path',
endpoint: 'https://example.org/cm',
credentialsId: 'CM',
cmclientOpts: ''
]
}
@Test
public void uploadFileToTransportRequestSOLMANSuccessTest() {
2018-06-20 11:46:28 +02:00
// Here we test only the case where the transportRequestId is
// provided via parameters. The other cases are tested by
// corresponding tests for StepHelpers#getTransportRequestId(./.)
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(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
String filePath,
String endpoint,
String credentialsId,
String cmclientOpts) {
2018-06-20 11:46:28 +02:00
cmUtilReceivedParams.type = type
cmUtilReceivedParams.changeId = changeId
cmUtilReceivedParams.transportRequestId = transportRequestId
cmUtilReceivedParams.applicationId = applicationId
cmUtilReceivedParams.filePath = filePath
cmUtilReceivedParams.endpoint = endpoint
cmUtilReceivedParams.credentialsId = credentialsId
cmUtilReceivedParams.cmclientOpts = cmclientOpts
}
}
2018-06-20 11:46:28 +02:00
jsr.step.transportRequestUploadFile(script: nullScript,
2018-07-13 14:40:29 +02:00
changeDocumentId: '001',
transportRequestId: '002',
2018-07-13 14:40:29 +02:00
applicationId: 'app',
filePath: '/path',
cmUtils: cm)
assert cmUtilReceivedParams ==
[
type: BackendType.SOLMAN,
changeId: '001',
transportRequestId: '002',
applicationId: 'app',
filePath: '/path',
endpoint: 'https://example.org/cm',
credentialsId: 'CM',
cmclientOpts: ''
]
2018-06-20 11:46:28 +02:00
}
@Test
public void uploadFileToTransportRequestSuccessApplicationIdFromConfigurationTest() {
nullScript.commonPipelineEnvironment.configuration.put(['steps',
[transportRequestUploadFile:
[applicationId: 'AppIdfromConfig']]])
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
String filePath,
String endpoint,
String credentialsId,
String cmclientOpts) {
cmUtilReceivedParams.applicationId = applicationId
}
}
jsr.step.transportRequestUploadFile(
script: nullScript,
changeDocumentId: '001',
transportRequestId: '002',
filePath: '/path',
cmUtils: cm)
assert cmUtilReceivedParams.applicationId == 'AppIdfromConfig'
}
@Test
public void uploadFileToTransportRequestFilePathFromParameters() {
// this one is not used when file path is provided via signature
nullScript.commonPipelineEnvironment.setMtarFilePath('/path2')
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
String filePath,
String endpoint,
String credentialsId,
String cmclientOpts) {
cmUtilReceivedParams.filePath = filePath
}
}
jsr.step.transportRequestUploadFile(script: nullScript,
changeDocumentId: '001',
transportRequestId: '002',
applicationId: 'app',
filePath: '/path',
cmUtils: cm)
assert cmUtilReceivedParams.filePath == '/path'
}
@Test
public void uploadFileToTransportRequestFilePathFromCommonPipelineEnvironment() {
// this one is used since there is nothing in the signature
nullScript.commonPipelineEnvironment.setMtarFilePath('/path2')
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
String filePath,
String endpoint,
String credentialsId,
String cmclientOpts) {
cmUtilReceivedParams.filePath = filePath
}
}
jsr.step.transportRequestUploadFile(script: nullScript,
changeDocumentId: '001',
transportRequestId: '002',
applicationId: 'app',
cmUtils: cm)
assert cmUtilReceivedParams.filePath == '/path2'
}
@Test
public void uploadFileToTransportRequestUploadFailureTest() {
thrown.expect(AbortException)
thrown.expectMessage('Upload failure.')
2018-06-20 11:46:28 +02:00
ChangeManagement cm = new ChangeManagement(nullScript) {
void uploadFileToTransportRequest(BackendType type,
String changeId,
String transportRequestId,
String applicationId,
String filePath,
String endpoint,
String credentialsId,
String cmclientOpts) {
throw new ChangeManagementException('Upload failure.')
}
}
2018-06-20 11:46:28 +02:00
jsr.step.transportRequestUploadFile(script: nullScript,
changeDocumentId: '001',
transportRequestId: '001',
applicationId: 'app',
filePath: '/path',
cmUtils: cm)
2018-06-20 11:46:28 +02:00
}
2018-09-18 14:49:06 +02:00
@Test
public void invalidBackendTypeTest() {
thrown.expect(AbortException)
thrown.expectMessage('Invalid backend type: \'DUMMY\'. Valid values: [SOLMAN, CTS, NONE]. ' +
'Configuration: \'changeManagement/type\'.')
jsr.step.transportRequestUploadFile(script: nullScript,
2018-09-18 14:49:06 +02:00
applicationId: 'app',
filePath: '/path',
changeManagement: [type: 'DUMMY'])
}
2018-09-28 13:45:26 +02:00
@Test
public void cmIntegrationSwichtedOffTest() {
jlr.expect('[INFO] Change management integration intentionally switched off.')
jsr.step.transportRequestUploadFile(script: nullScript,
2018-09-28 13:45:26 +02:00
changeManagement: [type: 'NONE'])
}
2018-06-20 11:46:28 +02:00
}