From 254a4e2017aa732aed402638ae5f9230bf858769 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 13 Jul 2018 13:23:10 +0200 Subject: [PATCH 1/3] Remove unused sh registerAllowedMethod registerAllowedMethod for sh call not used anymore since we mock the ChangeManagement utils class. The sh calls was triggered from the utils class. Since we mock that class, there is no sh call anymore. --- test/groovy/TransportRequestCreateTest.groovy | 2 -- test/groovy/TransportRequestReleaseTest.groovy | 2 -- test/groovy/TransportRequestUploadFileTest.groovy | 2 -- 3 files changed, 6 deletions(-) diff --git a/test/groovy/TransportRequestCreateTest.groovy b/test/groovy/TransportRequestCreateTest.groovy index 269b8ec98..66e65668b 100644 --- a/test/groovy/TransportRequestCreateTest.groovy +++ b/test/groovy/TransportRequestCreateTest.groovy @@ -45,8 +45,6 @@ public class TransportRequestCreateTest extends BasePiperTest { } }) - helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 }) - nullScript.commonPipelineEnvironment.configuration = [steps: [transportRequestCreate: [ diff --git a/test/groovy/TransportRequestReleaseTest.groovy b/test/groovy/TransportRequestReleaseTest.groovy index 619556c76..cf74e4fe2 100644 --- a/test/groovy/TransportRequestReleaseTest.groovy +++ b/test/groovy/TransportRequestReleaseTest.groovy @@ -42,8 +42,6 @@ public class TransportRequestReleaseTest extends BasePiperTest { } }) - helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 }) - nullScript.commonPipelineEnvironment.configuration = [steps: [transportRequestRelease: [ diff --git a/test/groovy/TransportRequestUploadFileTest.groovy b/test/groovy/TransportRequestUploadFileTest.groovy index 08ab99eee..75e96525a 100644 --- a/test/groovy/TransportRequestUploadFileTest.groovy +++ b/test/groovy/TransportRequestUploadFileTest.groovy @@ -45,8 +45,6 @@ public class TransportRequestUploadFileTest extends BasePiperTest { } }) - helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 }) - nullScript.commonPipelineEnvironment.configuration = [steps: [transportRequestUploadFile: [ From 0337b34cefaf7e214969ec6d7785c12ba3ef7af8 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 13 Jul 2018 14:40:29 +0200 Subject: [PATCH 2/3] [whitespace only] increase readability --- test/groovy/TransportRequestUploadFileTest.groovy | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test/groovy/TransportRequestUploadFileTest.groovy b/test/groovy/TransportRequestUploadFileTest.groovy index 75e96525a..9ce045ee8 100644 --- a/test/groovy/TransportRequestUploadFileTest.groovy +++ b/test/groovy/TransportRequestUploadFileTest.groovy @@ -134,7 +134,11 @@ public class TransportRequestUploadFileTest extends BasePiperTest { helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 }) - jsr.step.call(script: nullScript, changeDocumentId: '001', transportRequestId: '001', applicationId: 'app', filePath: '/path') + jsr.step.call(script: nullScript, + changeDocumentId: '001', + transportRequestId: '001', + applicationId: 'app', + filePath: '/path') assert jlr.log.contains("[INFO] Uploading file '/path' to transport request '001' of change document '001'.") assert jlr.log.contains("[INFO] File '/path' has been successfully uploaded to transport request '001' of change document '001'.") From bb0e9b92ce5122ead5d29659109e8a64f41985a8 Mon Sep 17 00:00:00 2001 From: Marcus Holl Date: Fri, 13 Jul 2018 15:13:58 +0200 Subject: [PATCH 3/3] transport request upload file: decouple tests. --- .../TransportRequestUploadFileTest.groovy | 76 +++++++++++++++++-- .../sap/piper/cm/ChangeManagementTest.groovy | 18 +++++ 2 files changed, 89 insertions(+), 5 deletions(-) diff --git a/test/groovy/TransportRequestUploadFileTest.groovy b/test/groovy/TransportRequestUploadFileTest.groovy index 9ce045ee8..a38db3459 100644 --- a/test/groovy/TransportRequestUploadFileTest.groovy +++ b/test/groovy/TransportRequestUploadFileTest.groovy @@ -1,3 +1,5 @@ +import java.util.Map + import org.junit.Before import org.junit.Rule import org.junit.Test @@ -27,6 +29,8 @@ public class TransportRequestUploadFileTest extends BasePiperTest { .around(jsr) .around(jlr) + private Map cmUtilReceivedParams = [:] + @Before public void setup() { @@ -45,6 +49,8 @@ public class TransportRequestUploadFileTest extends BasePiperTest { } }) + cmUtilReceivedParams.clear() + nullScript.commonPipelineEnvironment.configuration = [steps: [transportRequestUploadFile: [ @@ -132,15 +138,75 @@ public class TransportRequestUploadFileTest extends BasePiperTest { @Test public void uploadFileToTransportRequestSuccessTest() { - helper.registerAllowedMethod('sh', [Map], { Map m -> return 0 }) + 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'.") + + ChangeManagement cm = new ChangeManagement(nullScript) { + void uploadFileToTransportRequest(String changeId, + String transportRequestId, + String applicationId, + String filePath, + String endpoint, + String username, + String password, + String cmclientOpts) { + + cmUtilReceivedParams.changeId = changeId + cmUtilReceivedParams.transportRequestId = transportRequestId + cmUtilReceivedParams.applicationId = applicationId + cmUtilReceivedParams.filePath = filePath + cmUtilReceivedParams.endpoint = endpoint + cmUtilReceivedParams.username = username + cmUtilReceivedParams.password = password + cmUtilReceivedParams.cmclientOpts = cmclientOpts + } + } + + jsr.step.call(script: nullScript, + changeDocumentId: '001', + transportRequestId: '002', + 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 + ] + } + + @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.') + } + } jsr.step.call(script: nullScript, changeDocumentId: '001', transportRequestId: '001', applicationId: 'app', - filePath: '/path') - - assert jlr.log.contains("[INFO] Uploading file '/path' to transport request '001' of change document '001'.") - assert jlr.log.contains("[INFO] File '/path' has been successfully uploaded to transport request '001' of change document '001'.") + filePath: '/path', + cmUtils: cm) } + } diff --git a/test/groovy/com/sap/piper/cm/ChangeManagementTest.groovy b/test/groovy/com/sap/piper/cm/ChangeManagementTest.groovy index f3492aa4a..2747e0ccb 100644 --- a/test/groovy/com/sap/piper/cm/ChangeManagementTest.groovy +++ b/test/groovy/com/sap/piper/cm/ChangeManagementTest.groovy @@ -202,6 +202,24 @@ public void testGetCommandLineWithCMClientOpts() { // the command line. } + @Test + public void testUploadFileToTransportFails() { + + thrown.expect(ChangeManagementException) + thrown.expectMessage("Cannot upload file '/path' for change document '001' with transport request '002'. " + + "Return code from cmclient: 1.") + + script.setReturnValue(JenkinsShellCallRule.Type.REGEX,, 'upload-file-to-transport', 1) + + new ChangeManagement(nullScript).uploadFileToTransportRequest('001', + '002', + 'XXX', + '/path', + 'https://example.org/cm', + 'me', + 'openSesame') + } + private GitUtils gitUtilsMock(boolean insideWorkTree, String[] changeIds) { return new GitUtils() { public boolean insideWorkTree() {