From 5a73af4c0225c10574d47ddb635eee4b3513b975 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 29 Jun 2018 09:34:46 +0200 Subject: [PATCH] Decouple tests: transport request create --- test/groovy/TransportRequestCreateTest.groovy | 49 +++++++++++++++++-- .../sap/piper/cm/ChangeManagementTest.groovy | 26 +++++++++- vars/transportRequestCreate.groovy | 2 +- 3 files changed, 70 insertions(+), 7 deletions(-) diff --git a/test/groovy/TransportRequestCreateTest.groovy b/test/groovy/TransportRequestCreateTest.groovy index a82682835..c80861d4e 100644 --- a/test/groovy/TransportRequestCreateTest.groovy +++ b/test/groovy/TransportRequestCreateTest.groovy @@ -4,6 +4,9 @@ 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 + import util.BasePiperTest import util.JenkinsStepRule import util.JenkinsLoggingRule @@ -75,20 +78,56 @@ public class TransportRequestCreateTest extends BasePiperTest { @Test public void createTransportRequestFailureTest() { - helper.registerAllowedMethod('sh', [Map], { Map m -> throw new AbortException('Exception message.') }) + ChangeManagement cm = new ChangeManagement(nullScript) { + + String createTransportRequest(String changeId, + String developmentSystemId, + String cmEndpoint, + String username, + String password) { + + throw new ChangeManagementException('Exception message.') + } + } + thrown.expect(AbortException) - thrown.expectMessage("Cannot create a transport request for change id '001'. Exception message.") + thrown.expectMessage("Exception message.") - jsr.step.call(script: nullScript, changeDocumentId: '001', developmentSystemId: '001') + jsr.step.call(script: nullScript, changeDocumentId: '001', developmentSystemId: '001', cmUtils: cm) } @Test public void createTransportRequestSuccessTest() { - helper.registerAllowedMethod('sh', [Map], { Map m -> return '001' }) + def result = [:] - jsr.step.call(script: nullScript, changeDocumentId: '001', developmentSystemId: '001') + ChangeManagement cm = new ChangeManagement(nullScript) { + + String createTransportRequest(String changeId, + String developmentSystemId, + String cmEndpoint, + String username, + String password) { + + result.changeId = changeId + result.developmentSystemId = developmentSystemId + result.cmEndpoint = cmEndpoint + result.username = username + result.password = password + return '001' + } + } + + def transportId = jsr.step.call(script: nullScript, changeDocumentId: '001', developmentSystemId: '001', cmUtils: cm) + + assert transportId == '001' + assert result == [changeId: '001', + developmentSystemId: '001', + cmEndpoint: 'https://example.org/cm', + username: 'anonymous', + password: '********' + ] assert jlr.log.contains("[INFO] Creating transport request for change document '001' and development system '001'.") assert jlr.log.contains("[INFO] Transport Request '001' has been successfully created.") diff --git a/test/groovy/com/sap/piper/cm/ChangeManagementTest.groovy b/test/groovy/com/sap/piper/cm/ChangeManagementTest.groovy index 06efa7d2b..5de92c529 100644 --- a/test/groovy/com/sap/piper/cm/ChangeManagementTest.groovy +++ b/test/groovy/com/sap/piper/cm/ChangeManagementTest.groovy @@ -22,12 +22,14 @@ import util.JenkinsLoggingRule import util.JenkinsShellCallRule import util.Rules +import hudson.AbortException + public class ChangeManagementTest extends BasePiperTest { private ExpectedException thrown = ExpectedException.none() private JenkinsShellCallRule script = new JenkinsShellCallRule(this) - private JenkinsLoggingRule logging = new JenkinsLoggingRule(this) + private JenkinsLoggingRule logging = new JenkinsLoggingRule(this) @Rule public RuleChain rules = Rules.getCommonRules(this) @@ -176,6 +178,28 @@ public void testGetCommandLineWithCMClientOpts() { assertThat(commandLine, containsString('export CMCLIENT_OPTS="-Djavax.net.debug=all"')) } + @Test + public void testCreateTransportRequestSucceeds() { + + script.setReturnValue(JenkinsShellCallRule.Type.REGEX, ".*cmclient.*create-transport -cID 001 -dID 002.*", '004') + def transportRequestId = new ChangeManagement(nullScript).createTransportRequest('001', '002', '003', 'me', 'openSesame') + + // the check for the transportRequestID is sufficient. This checks implicit the command line since that value is + // returned only in case the shell call matches. + assert transportRequestId == '004' + + } + + @Test + public void testCreateTransportRequestFails() { + + thrown.expect(ChangeManagementException) + thrown.expectMessage('Cannot create a transport request for change id \'001\'. Exception message.') + + //suggestion: enable shell call rule for throwing exceptions and switch to shell call rule afterwards. + helper.registerAllowedMethod('sh', [Map], { throw new AbortException('Exception message') }) + new ChangeManagement(nullScript).createTransportRequest('001', '002', '003', 'me', 'openSesame') + } private GitUtils gitUtilsMock(boolean insideWorkTree, String[] changeIds) { return new GitUtils() { diff --git a/vars/transportRequestCreate.groovy b/vars/transportRequestCreate.groovy index ca698efde..77a84a423 100644 --- a/vars/transportRequestCreate.groovy +++ b/vars/transportRequestCreate.groovy @@ -28,7 +28,7 @@ def call(parameters = [:]) { def script = parameters?.script ?: [commonPipelineEnvironment: commonPipelineEnvironment] - ChangeManagement cm = new ChangeManagement(script) + ChangeManagement cm = parameters.cmUtils ?: new ChangeManagement(script) Map configuration = ConfigurationMerger.merge(parameters.script, STEP_NAME, parameters, parameterKeys,