github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/TransportRequestCreateTest.groovy (about)

     1  import static org.hamcrest.Matchers.allOf
     2  import static org.hamcrest.Matchers.containsString
     3  
     4  import java.util.Map
     5  
     6  import org.hamcrest.Matchers
     7  import org.hamcrest.core.StringContains
     8  import org.junit.After
     9  import org.junit.Before
    10  import org.junit.Rule
    11  import org.junit.Test
    12  import org.junit.rules.ExpectedException
    13  import org.junit.rules.RuleChain
    14  
    15  import com.sap.piper.cm.BackendType
    16  import com.sap.piper.cm.ChangeManagement
    17  import com.sap.piper.cm.ChangeManagementException
    18  import com.sap.piper.Utils
    19  
    20  import util.BasePiperTest
    21  import util.JenkinsCredentialsRule
    22  import util.JenkinsStepRule
    23  import util.JenkinsLoggingRule
    24  import util.JenkinsReadYamlRule
    25  import util.Rules
    26  
    27  import hudson.AbortException
    28  
    29  public class TransportRequestCreateTest extends BasePiperTest {
    30  
    31      private ExpectedException thrown = new ExpectedException()
    32      private JenkinsStepRule stepRule = new JenkinsStepRule(this)
    33      private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this)
    34  
    35      @Rule
    36      public RuleChain ruleChain = Rules.getCommonRules(this)
    37          .around(new JenkinsReadYamlRule(this))
    38          .around(thrown)
    39          .around(stepRule)
    40          .around(loggingRule)
    41          .around(new JenkinsCredentialsRule(this)
    42              .withCredentials('CM', 'anonymous', '********'))
    43  
    44      @Before
    45      public void setup() {
    46  
    47          Utils.metaClass.echo = { def m -> }
    48  
    49          nullScript.commonPipelineEnvironment.configuration = [general:
    50                                       [changeManagement:
    51  
    52                                           [
    53                                            credentialsId: 'CM',
    54                                            type: 'SOLMAN',
    55                                            endpoint: 'https://example.org/cm',
    56                                            clientOpts: '-DmyProp=myVal',
    57                                            changeDocumentLabel: 'ChangeId\\s?:',
    58                                            git: [from: 'origin/master',
    59                                                  to: 'HEAD',
    60                                                  format: '%b']
    61                                           ]
    62                                       ]
    63                                   ]
    64          helper.registerAllowedMethod('addBadge', [Map], {return})
    65          helper.registerAllowedMethod('createSummary', [Map], {return})
    66      }
    67  
    68      @After
    69      public void tearDown() {
    70          Utils.metaClass = null
    71      }
    72  
    73      @Test
    74      public void changeIdNotProvidedSOLANTest() {
    75  
    76          thrown.expect(IllegalArgumentException)
    77          thrown.expectMessage("Change document id not provided (parameter: 'changeDocumentId' provided to the step call or via commit history).")
    78          ChangeManagement cm = new ChangeManagement(nullScript) {
    79              String getChangeDocumentId(
    80                                         String from,
    81                                         String to,
    82                                         String label,
    83                                         String format
    84                                        ) {
    85                                            throw new ChangeManagementException('Cannot retrieve changeId from git commits.')
    86                                        }
    87          }
    88  
    89          stepRule.step.transportRequestCreate(script: nullScript, developmentSystemId: '001', cmUtils: cm)
    90      }
    91  
    92      @Test
    93      public void developmentSystemIdNotProvidedSOLMANTest() {
    94  
    95          thrown.expect(IllegalArgumentException)
    96          thrown.expectMessage("ERROR - NO VALUE AVAILABLE FOR developmentSystemId")
    97  
    98          stepRule.step.transportRequestCreate(script: nullScript, changeDocumentId: '001')
    99      }
   100  
   101      @Test
   102      public void createTransportRequestFailureSOLMANTest() {
   103  
   104          ChangeManagement cm = new ChangeManagement(nullScript) {
   105  
   106              String createTransportRequestSOLMAN(
   107                                            Map docker,
   108                                            String changeId,
   109                                            String developmentSystemId,
   110                                            String cmEndpoint,
   111                                            String credentialId,
   112                                            String clientOpts) {
   113  
   114                      throw new ChangeManagementException('Exception message.')
   115              }
   116          }
   117  
   118  
   119          thrown.expect(AbortException)
   120          thrown.expectMessage("Exception message.")
   121  
   122          stepRule.step.transportRequestCreate(script: nullScript, changeDocumentId: '001', developmentSystemId: '001', cmUtils: cm)
   123      }
   124  
   125      @Test
   126      public void createTransportRequestSuccessSOLMANTest() {
   127  
   128          def result = [:]
   129  
   130          ChangeManagement cm = new ChangeManagement(nullScript) {
   131  
   132              String createTransportRequestSOLMAN(
   133                                            Map docker,
   134                                            String changeId,
   135                                            String developmentSystemId,
   136                                            String cmEndpoint,
   137                                            String credentialId,
   138                                            String clientOpts) {
   139  
   140                  result.docker = docker
   141                  result.changeId = changeId
   142                  result.developmentSystemId = developmentSystemId
   143                  result.cmEndpoint = cmEndpoint
   144                  result.credentialId = credentialId
   145                  result.clientOpts = clientOpts
   146                  return '001'
   147              }
   148          }
   149  
   150          stepRule.step.transportRequestCreate(script: nullScript, changeDocumentId: '001', developmentSystemId: '001', cmUtils: cm)
   151  
   152          assert nullScript.commonPipelineEnvironment.getValue('transportRequestId') == '001'
   153          assert result == [
   154                           docker: [
   155                               image: 'ppiper/cm-client:2.0.1.0',
   156                               pullImage: true,
   157                               options: [],
   158                               envVars: [:],
   159                           ],
   160                           changeId: '001',
   161                           developmentSystemId: '001',
   162                           cmEndpoint: 'https://example.org/cm',
   163                           credentialId: 'CM',
   164                           clientOpts: '-DmyProp=myVal'
   165                           ]
   166  
   167          assert loggingRule.log.contains("[INFO] Creating transport request for change document '001' and development system '001'.")
   168          assert loggingRule.log.contains("[INFO] Transport Request '001' has been successfully created.")
   169      }
   170  
   171      @Test
   172      public void createTransportRequestSuccessCTSTest() {
   173  
   174          def result = [:]
   175  
   176          ChangeManagement cm = new ChangeManagement(nullScript) {
   177  
   178              String createTransportRequestCTS(
   179                  Map docker,
   180                  String transportType,
   181                  String targetSystemId,
   182                  String description,
   183                  String endpoint,
   184                  String credentialsId,
   185                  String clientOpts) {
   186                  result.docker = docker
   187                  result.transportType = transportType
   188                  result.targetSystemId = targetSystemId
   189                  result.description = description
   190                  result.endpoint = endpoint
   191                  result.credentialsId = credentialsId
   192                  result.clientOpts = clientOpts
   193                  return '001'
   194              }
   195          }
   196  
   197          stepRule.step.call(script: nullScript,
   198                          transportType: 'W',
   199                          targetSystem: 'XYZ',
   200                          description: 'desc',
   201                          changeManagement: [type: 'CTS'],
   202                          cmUtils: cm)
   203  
   204          assert nullScript.commonPipelineEnvironment.getValue('transportRequestId') == '001'
   205          assert result == [
   206                           docker: [
   207                               image: 'ppiper/cm-client:2.0.1.0',
   208                               pullImage: true,
   209                               envVars: [:],
   210                               options: [],
   211                           ],
   212                           transportType: 'W',
   213                           targetSystemId: 'XYZ',
   214                           description: 'desc',
   215                           endpoint: 'https://example.org/cm',
   216                           credentialsId: 'CM',
   217                           clientOpts: '-DmyProp=myVal'
   218                           ]
   219  
   220          assert loggingRule.log.contains("[INFO] Creating transport request.")
   221          assert loggingRule.log.contains("[INFO] Transport Request '001' has been successfully created.")
   222      }
   223  
   224      @Test
   225      public void createTransportRequestSuccessRFCTest() {
   226  
   227          def result = [:]
   228  
   229          ChangeManagement cm = new ChangeManagement(nullScript) {
   230  
   231              String createTransportRequestRFC(
   232                  Map docker,
   233                  String endpoint,
   234                  String developmentInstance,
   235                  String developmentClient,
   236                  String credentialsId,
   237                  String description,
   238                  boolean verbose) {
   239  
   240                  result.docker = docker
   241                  result.endpoint = endpoint
   242                  result.developmentClient = developmentClient
   243                  result.developmentInstance= developmentInstance
   244                  result.credentialsId = credentialsId
   245                  result.description = description
   246                  result.verbose = verbose
   247  
   248                  return '001'
   249              }
   250          }
   251  
   252          stepRule.step.transportRequestCreate(
   253              script: nullScript,
   254              changeManagement: [
   255                  type: 'RFC',
   256                  rfc: [
   257                      developmentInstance: '01',
   258                      developmentClient: '001',
   259                  ],
   260                  endpoint: 'https://example.org/rfc',
   261              ],
   262              developmentSystemId: '001',
   263              description: '',
   264              cmUtils: cm,
   265              verbose: true)
   266  
   267          assert nullScript.commonPipelineEnvironment.getValue('transportRequestId') == '001'
   268          assert result == [
   269              docker: [
   270                  image: 'rfc',
   271                  options: [],
   272                  envVars: [:],
   273                  pullImage: true
   274              ],
   275              endpoint: 'https://example.org/rfc',
   276              developmentClient: '001',
   277              developmentInstance: '01',
   278              credentialsId: 'CM',
   279              description: '',
   280              verbose: true
   281          ]
   282  
   283          assert loggingRule.log.contains("[INFO] Creating transport request.")
   284          assert loggingRule.log.contains("[INFO] Transport Request '001' has been successfully created.")
   285      }
   286  
   287      @Test
   288      public void createTransportRequestFailureRFCTest() {
   289  
   290          thrown.expect(AbortException)
   291          thrown.expectMessage('upload failed')
   292  
   293          ChangeManagement cm = new ChangeManagement(nullScript) {
   294  
   295              String createTransportRequestRFC(
   296                  Map docker,
   297                  String endpoint,
   298                  String developmentClient,
   299                  String developmentInstance,
   300                  String credentialsId,
   301                  String description,
   302                  boolean verbose) {
   303  
   304                  throw new ChangeManagementException('upload failed')
   305              }
   306          }
   307  
   308          stepRule.step.transportRequestCreate(
   309              script: nullScript,
   310              changeManagement: [
   311                  type: 'RFC',
   312                  rfc: [
   313                      developmentInstance: '01',
   314                      developmentClient: '001',
   315                  ],
   316                  endpoint: 'https://example.org/rfc',
   317              ],
   318              developmentSystemId: '001',
   319              description: '',
   320              cmUtils: cm)
   321      }
   322  
   323      @Test
   324      public void createTransportRequestSanityChecksRFCTest() {
   325  
   326          thrown.expect(IllegalArgumentException)
   327          thrown.expectMessage(allOf(
   328              containsString('changeManagement/rfc/developmentInstance'),
   329              containsString('changeManagement/rfc/developmentClient'),
   330              ))
   331          stepRule.step.transportRequestCreate(
   332              script: nullScript,
   333              changeManagement: [
   334                  type: 'RFC',
   335              ])
   336      }
   337  
   338      @Test
   339      public void cmIntegrationSwichtedOffTest() {
   340  
   341          loggingRule.expect('[INFO] Change management integration intentionally switched off.')
   342  
   343          stepRule.step.transportRequestCreate(script: nullScript,
   344              changeManagement: [type: 'NONE'])
   345      }
   346  }