github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/test/groovy/com/sap/piper/integration/TransportManagementServiceTest.groovy (about) 1 package com.sap.piper.integration 2 3 import hudson.AbortException 4 import org.junit.Rule 5 import org.junit.Test 6 import org.junit.Ignore 7 import org.junit.rules.ExpectedException 8 import org.junit.rules.RuleChain 9 import util.* 10 11 import static org.hamcrest.Matchers.* 12 import static org.junit.Assert.assertThat 13 import static org.junit.Assert.assertFalse 14 15 class TransportManagementServiceTest extends BasePiperTest { 16 private ExpectedException thrown = ExpectedException.none() 17 private JenkinsShellCallRule shellRule = new JenkinsShellCallRule(this) 18 private JenkinsLoggingRule loggingRule = new JenkinsLoggingRule(this) 19 private JenkinsReadFileRule readFileRule = new JenkinsReadFileRule(this, 'test/resources/TransportManagementService') 20 private JenkinsFileExistsRule fileExistsRule = new JenkinsFileExistsRule(this, ['responseFileUpload.txt', 'responseExtDescriptorUpload.txt', 'responseExtDescriptorUpdate.txt']) 21 22 @Rule 23 public RuleChain rules = Rules 24 .getCommonRules(this) 25 .around(new JenkinsErrorRule(this)) 26 .around(new JenkinsReadJsonRule(this)) 27 .around(shellRule) 28 .around(loggingRule) 29 .around(readFileRule) 30 .around(fileExistsRule) 31 .around(thrown) 32 33 @Test 34 void retrieveOAuthToken__successfully() { 35 Map requestParams 36 helper.registerAllowedMethod('httpRequest', [Map.class], { m -> 37 requestParams = m 38 return [content: '{ "access_token": "myOAuthToken" }', status: 200] 39 }) 40 41 def uaaUrl = 'http://dummy.sap.com/oauth' 42 def clientId = 'myId' 43 def clientSecret = 'mySecret' 44 45 def tms = new TransportManagementService(nullScript, [verbose: false]) 46 def token = tms.authentication(uaaUrl, clientId, clientSecret) 47 48 assertThat(loggingRule.log, containsString("[TransportManagementService] OAuth Token retrieval started.")) 49 assertThat(loggingRule.log, containsString("[TransportManagementService] OAuth Token retrieved successfully.")) 50 assertThat(loggingRule.log, not(containsString("myOAuthToken"))) 51 assertThat(token, is('myOAuthToken')) 52 assertThat(requestParams, hasEntry('url', "${uaaUrl}/oauth/token/?grant_type=client_credentials&response_type=token")) 53 assertThat(requestParams, hasEntry('requestBody', "grant_type=password&username=${clientId}&password=${clientSecret}".toString())) 54 assertThat(requestParams.customHeaders[1].value, is("Basic ${"${clientId}:${clientSecret}".bytes.encodeBase64()}")) 55 } 56 57 @Test 58 void retrieveOAuthToken__inVerboseMode__yieldsMoreEchos() { 59 helper.registerAllowedMethod('httpRequest', [Map.class], { 60 return [content: '{ "access_token": "myOAuthToken" }', status: 200] 61 }) 62 63 def uaaUrl = 'http://dummy.sap.com/oauth' 64 def clientId = 'myId' 65 def clientSecret = 'mySecret' 66 67 def tms = new TransportManagementService(nullScript, [verbose: true]) 68 def token = tms.authentication(uaaUrl, clientId, clientSecret) 69 70 assertThat(loggingRule.log, containsString("[TransportManagementService] OAuth Token retrieval started.")) 71 assertThat(loggingRule.log, containsString("[TransportManagementService] UAA-URL: '${uaaUrl}', ClientId: '${clientId}'")) 72 assertThat(loggingRule.log, containsString("[TransportManagementService] OAuth Token retrieved successfully.")) 73 assertThat(loggingRule.log, not(containsString("myOAuthToken"))) 74 assertThat(token, is('myOAuthToken')) 75 } 76 77 @Test 78 void retrieveOAuthToken__failure() { 79 def uaaUrl = 'http://dummy.sap.com/oauth' 80 def clientId = 'myId' 81 def clientSecret = 'mySecret' 82 def responseStatusCode = 400 83 def responseContent = 'Here an error message is expected (THIS PART IS HERE TO CHECK THAT ERROR MESSAGE IS EXPOSED IN NON-VERBOSE MODE)' 84 85 thrown.expect(AbortException) 86 thrown.expectMessage("[TransportManagementService] OAuth Token retrieval failed (HTTP status code '${responseStatusCode}'). Response content '${responseContent}'.") 87 loggingRule.expect("[TransportManagementService] OAuth Token retrieval started.") 88 89 helper.registerAllowedMethod('httpRequest', [Map.class], { 90 return [content: responseContent, status: responseStatusCode] 91 }) 92 93 def tms = new TransportManagementService(nullScript, [verbose: false]) 94 tms.authentication(uaaUrl, clientId, clientSecret) 95 } 96 97 @Test 98 void retrieveOAuthToken__failure__status__less__than__300() { 99 def uaaUrl = 'http://dummy.sap.com/oauth' 100 def clientId = 'myId' 101 def clientSecret = 'mySecret' 102 def responseStatusCode = 201 103 def responseContent = 'This response content should not be printed to the logs as well as be thrown in exception message, since it might contain a token, if for some reason authentication service spec changes' 104 105 thrown.expect(AbortException) 106 thrown.expectMessage("[TransportManagementService] OAuth Token retrieval failed (HTTP status code '${responseStatusCode}').") 107 thrown.expectMessage(not(containsString(responseContent))) 108 loggingRule.expect("[TransportManagementService] OAuth Token retrieval started.") 109 loggingRule.notExpect(responseContent) 110 111 helper.registerAllowedMethod('httpRequest', [Map.class], { 112 return [content: responseContent, status: responseStatusCode] 113 }) 114 115 def tms = new TransportManagementService(nullScript, [verbose: false]) 116 tms.authentication(uaaUrl, clientId, clientSecret) 117 } 118 119 @Test 120 void retrieveOAuthToken__failure__status__400__inVerboseMode() { 121 def uaaUrl = 'http://dummy.sap.com/oauth' 122 def clientId = 'myId' 123 def clientSecret = 'mySecret' 124 def responseStatusCode = 400 125 def responseContent = 'Here an error message is expected (THIS PART IS HERE TO CHECK THAT ERROR MESSAGE IS EXPOSED IN VERBOSE MODE)' 126 127 thrown.expect(AbortException) 128 thrown.expectMessage("[TransportManagementService] OAuth Token retrieval failed (HTTP status code '${responseStatusCode}'). Response content '${responseContent}'.") 129 loggingRule.expect("[TransportManagementService] OAuth Token retrieval started.") 130 loggingRule.expect("[TransportManagementService] UAA-URL: '${uaaUrl}', ClientId: '${clientId}'") 131 132 helper.registerAllowedMethod('httpRequest', [Map.class], { 133 return [content: responseContent, status: responseStatusCode] 134 }) 135 136 def tms = new TransportManagementService(nullScript, [verbose: true]) 137 tms.authentication(uaaUrl, clientId, clientSecret) 138 } 139 140 @Test 141 void retrieveOAuthToken__failure__status__less__than__300__inVerboseMode() { 142 def uaaUrl = 'http://dummy.sap.com/oauth' 143 def clientId = 'myId' 144 def clientSecret = 'mySecret' 145 def responseStatusCode = 201 146 def responseContent = 'This response content should not be printed to the logs as well as be thrown in exception message, since it might contain a token, if for some reason authentication service spec changes' 147 148 thrown.expect(AbortException) 149 thrown.expectMessage("[TransportManagementService] OAuth Token retrieval failed (HTTP status code '${responseStatusCode}').") 150 thrown.expectMessage(not(containsString(responseContent))) 151 loggingRule.expect("[TransportManagementService] OAuth Token retrieval started.") 152 loggingRule.expect("[TransportManagementService] UAA-URL: '${uaaUrl}', ClientId: '${clientId}'") 153 loggingRule.notExpect(responseContent) 154 155 helper.registerAllowedMethod('httpRequest', [Map.class], { 156 return [content: responseContent, status: responseStatusCode] 157 }) 158 159 def tms = new TransportManagementService(nullScript, [verbose: true]) 160 tms.authentication(uaaUrl, clientId, clientSecret) 161 } 162 163 @Test 164 void uploadFile__successfully() { 165 166 def url = 'http://dummy.sap.com' 167 def token = 'myToken' 168 def file = 'myFile.mtar' 169 def namedUser = 'myUser' 170 171 shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX,'.*curl.*', '201') 172 173 def tms = new TransportManagementService(nullScript, [:]) 174 def responseDetails = tms.uploadFile(url, token, file, namedUser) 175 176 // replace needed since the curl command is spread over several lines. 177 def oAuthShellCall = shellRule.shell[0].replaceAll('\\\\ ', '') 178 179 assertThat(loggingRule.log, containsString("[TransportManagementService] File upload started.")) 180 assertThat(loggingRule.log, containsString("[TransportManagementService] File upload successful.")) 181 assertThat(oAuthShellCall, startsWith("#!/bin/sh -e ")) 182 assertThat(oAuthShellCall, endsWith("curl --write-out '%{response_code}' -H 'Authorization: Bearer ${token}' -F 'file=@${file}' -F 'namedUser=${namedUser}' --output responseFileUpload.txt '${url}/v2/files/upload'")) 183 assertThat(responseDetails, hasEntry("fileId", 1234)) 184 } 185 186 @Test 187 void uploadFile__verboseMode__withHttpErrorResponse__throwsError() { 188 189 def url = 'http://dummy.sap.com' 190 def token = 'myWrongToken' 191 def file = 'myFile.mtar' 192 def namedUser = 'myUser' 193 194 shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, ".* curl .*", '400') 195 196 readFileRule.files << [ 'responseFileUpload.txt': 'Something went wrong during file upload (WE ARE IN VERBOSE MODE)'] 197 198 thrown.expect(AbortException.class) 199 thrown.expectMessage('Unexpected response code received from File upload (400). 201 expected') 200 201 loggingRule.expect('[TransportManagementService] URL: \'http://dummy.sap.com\', File: \'myFile.mtar\'') 202 loggingRule.expect('[TransportManagementService] Response body: Something went wrong during file upload (WE ARE IN VERBOSE MODE)') 203 204 // The log entries which are present in non verbose mode must be present in verbose mode also, of course 205 loggingRule.expect('[TransportManagementService] File upload started.') 206 loggingRule.expect('[TransportManagementService] Unexpected response code received from File upload (400). 201 expected. Response body: Something went wrong during file upload') 207 208 new TransportManagementService(nullScript, [verbose:true]) 209 .uploadFile(url, token, file, namedUser) 210 } 211 212 @Test 213 void uploadFile__NonVerboseMode__withHttpErrorResponse__throwsError() { 214 215 def url = 'http://dummy.sap.com' 216 def token = 'myWrongToken' 217 def file = 'myFile.mtar' 218 def namedUser = 'myUser' 219 220 // 418 (tea-pot)? Other than 400 which is used in verbose mode in order to be sure that we don't mix up 221 // with any details from the other test for the verbose mode. The log message below (Unexpected response code ...) 222 // reflects that 418 instead of 400. 223 shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, ".* curl .*", '418') 224 225 readFileRule.files << [ 'responseFileUpload.txt': 'Something went wrong during file upload. WE ARE IN NON VERBOSE MODE.'] 226 227 thrown.expect(AbortException.class) 228 thrown.expectMessage('Unexpected response code received from File upload (418). 201 expected') 229 230 loggingRule.expect('[TransportManagementService] File upload started.') 231 loggingRule.expect('[TransportManagementService] Unexpected response code received from File upload (418). 201 expected. Response body: Something went wrong during file upload. WE ARE IN NON VERBOSE MODE.') 232 233 new TransportManagementService(nullScript, [verbose:false]) 234 .uploadFile(url, token, file, namedUser) 235 } 236 237 @Test 238 void uploadFile__inVerboseMode__yieldsMoreEchos() { 239 240 def url = 'http://dummy.sap.com' 241 def token = 'myToken' 242 def file = 'myFile.mtar' 243 def namedUser = 'myUser' 244 245 shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, ".* curl .*", '201') 246 fileExistsRule.existingFiles.add('responseFileUpload.txt') 247 readFileRule.files.put('responseFileUpload.txt', '{"fileId": 1234}') 248 249 def tms = new TransportManagementService(nullScript, [verbose: true]) 250 tms.uploadFile(url, token, file, namedUser) 251 252 assertThat(loggingRule.log, containsString("[TransportManagementService] File upload started.")) 253 assertThat(loggingRule.log, containsString("[TransportManagementService] URL: '${url}', File: '${file}'")) 254 assertThat(loggingRule.log, containsString("\"fileId\": 1234")) 255 assertThat(loggingRule.log, containsString("[TransportManagementService] File upload successful.")) 256 } 257 258 @Test 259 void uploadFileToNode__successfully() { 260 Map requestParams 261 helper.registerAllowedMethod('httpRequest', [Map.class], { m -> 262 requestParams = m 263 return [content: '{ "upload": "success" }', status: 200] 264 }) 265 266 def url = 'http://dummy.sap.com' 267 def token = 'myToken' 268 def nodeName = 'myNode' 269 def fileId = 1234 270 def description = "My description." 271 def namedUser = 'myUser' 272 273 def tms = new TransportManagementService(nullScript, [:]) 274 def responseDetails = tms.uploadFileToNode(url, token, nodeName, fileId, description, namedUser) 275 276 def bodyRegEx = /^\{\s+"nodeName":\s+"myNode",\s+"contentType":\s+"MTA",\s+"description":\s+"My\s+description.",\s+"storageType":\s+"FILE",\s+"namedUser":\s+"myUser",\s+"entries":\s+\[\s+\{\s+"uri":\s+1234\s+}\s+]\s+}$/ 277 278 assertThat(loggingRule.log, containsString("[TransportManagementService] Node upload started.")) 279 assertThat(loggingRule.log, containsString("[TransportManagementService] Node upload successful.")) 280 assertThat(requestParams, hasEntry('url', "${url}/v2/nodes/upload")) 281 assert requestParams.requestBody ==~ bodyRegEx 282 assertThat(requestParams.customHeaders[0].value, is("Bearer ${token}")) 283 assertThat(responseDetails, hasEntry("upload", "success")) 284 } 285 286 @Test 287 void uploadFileToNode__inVerboseMode__yieldsMoreEchos() { 288 def url = 'http://dummy.sap.com' 289 def token = 'myToken' 290 def nodeName = 'myNode' 291 def fileId = 1234 292 def description = "My description." 293 def namedUser = 'myUser' 294 def responseContent = '{ "upload": "success" }' 295 296 helper.registerAllowedMethod('httpRequest', [Map.class], { 297 return [content: responseContent, status: 200] 298 }) 299 300 def tms = new TransportManagementService(nullScript, [verbose: true]) 301 tms.uploadFileToNode(url, token, nodeName, fileId, description, namedUser) 302 303 assertThat(loggingRule.log, containsString("[TransportManagementService] Node upload started.")) 304 assertThat(loggingRule.log, containsString("[TransportManagementService] URL: '${url}', NodeName: '${nodeName}', FileId: '${fileId}'")) 305 assertThat(loggingRule.log, containsString("\"upload\": \"success\"")) 306 assertThat(loggingRule.log, containsString("[TransportManagementService] Node upload successful. Response content '${responseContent}'.")) 307 } 308 309 @Test 310 void uploadFileToNode__failure() { 311 def url = 'http://dummy.sap.com' 312 def token = 'myToken' 313 def nodeName = 'myNode' 314 def fileId = 1234 315 def description = "My description." 316 def namedUser = 'myUser' 317 def responseStatusCode = 400 318 def responseContent = '{ "errorType": "TsInternalServerErrorException", "message": "The application has encountered an unexpected error (THIS PART IS HERE TO CHECK THAT ERROR MESSAGE IS EXPOSED IN NON-VERBOSE MODE)." }' 319 320 thrown.expect(AbortException) 321 thrown.expectMessage("[TransportManagementService] Node upload failed (HTTP status code '${responseStatusCode}'). Response content '${responseContent}'.") 322 loggingRule.expect("[TransportManagementService] Node upload started.") 323 324 helper.registerAllowedMethod('httpRequest', [Map.class], { 325 return [content: responseContent, status: responseStatusCode] 326 }) 327 328 def tms = new TransportManagementService(nullScript, [verbose: false]) 329 tms.uploadFileToNode(url, token, nodeName, fileId, description, namedUser) 330 } 331 332 @Test 333 void uploadFileToNode__failure__status__400__inVerboseMode() { 334 def url = 'http://dummy.sap.com' 335 def token = 'myToken' 336 def nodeName = 'myNode' 337 def fileId = 1234 338 def description = "My description." 339 def namedUser = 'myUser' 340 def responseStatusCode = 400 341 def responseContent = '{ "errorType": "TsInternalServerErrorException", "message": "The application has encountered an unexpected error (THIS PART IS HERE TO CHECK THAT ERROR MESSAGE IS EXPOSED IN VERBOSE MODE)." }' 342 343 thrown.expect(AbortException) 344 thrown.expectMessage("[TransportManagementService] Node upload failed (HTTP status code '${responseStatusCode}'). Response content '${responseContent}'.") 345 loggingRule.expect("[TransportManagementService] Node upload started.") 346 loggingRule.expect("[TransportManagementService] URL: '${url}', NodeName: '${nodeName}', FileId: '${fileId}'") 347 348 helper.registerAllowedMethod('httpRequest', [Map.class], { 349 return [content: responseContent, status: responseStatusCode] 350 }) 351 352 def tms = new TransportManagementService(nullScript, [verbose: true]) 353 tms.uploadFileToNode(url, token, nodeName, fileId, description, namedUser) 354 } 355 356 @Test 357 void uploadMtaExtDescriptorToNode__successfully() { 358 def url = 'http://dummy.sap.com' 359 def token = 'myToken' 360 def nodeId = 1 361 def file = 'myFile.mtaext' 362 def mtaVersion = '0.0.1' 363 def description = "My description." 364 def namedUser = 'myUser' 365 366 shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX,'.*curl.*', '201') 367 368 def tms = new TransportManagementService(nullScript, [:]) 369 def responseDetails = tms.uploadMtaExtDescriptorToNode(url, token, nodeId, file, mtaVersion, description, namedUser) 370 def oAuthShellCall = shellRule.shell[0].replaceAll('\\\\ ', '') 371 372 assertThat(loggingRule.log, containsString("[TransportManagementService] MTA Extension Descriptor upload started.")) 373 assertThat(oAuthShellCall, startsWith("#!/bin/sh -e ")) 374 assertThat(oAuthShellCall, endsWith("curl --write-out '%{response_code}' -H 'Authorization: Bearer ${token}' -H 'tms-named-user: ${namedUser}' -F 'file=@${file}' -F 'mtaVersion=${mtaVersion}' -F 'description=${description}' --output responseExtDescriptorUpload.txt '${url}/v2/nodes/${nodeId}/mtaExtDescriptors'")) 375 assertThat(responseDetails, hasEntry("id", 123)) 376 assertThat(loggingRule.log, containsString("[TransportManagementService] MTA Extension Descriptor upload successful.")) 377 } 378 379 @Test 380 void uploadMtaExtDescriptorToNode__inVerboseMode__yieldsMoreEchos() { 381 def url = 'http://dummy.sap.com' 382 def token = 'myToken' 383 def nodeId = 1 384 def file = 'myFile.mtaext' 385 def mtaVersion = '0.0.1' 386 def description = "My description." 387 def namedUser = 'myUser' 388 389 shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, ".* curl .*", '201') 390 391 def tms = new TransportManagementService(nullScript, [verbose: true]) 392 tms.uploadMtaExtDescriptorToNode(url, token, nodeId, file, mtaVersion, description, namedUser) 393 394 assertThat(loggingRule.log, containsString("[TransportManagementService] MTA Extension Descriptor upload started.")) 395 assertThat(loggingRule.log, containsString("URL: '${url}', NodeId: '${nodeId}', File: '${file}', MtaVersion: '${mtaVersion}'")) 396 assertThat(loggingRule.log, containsString("\"id\": 123")) 397 assertThat(loggingRule.log, containsString("[TransportManagementService] MTA Extension Descriptor upload successful.")) 398 } 399 400 @Test 401 void uploadMtaExtDescriptorToNode__verboseMode__withHttpErrorResponse__throwsError() { 402 403 def url = 'http://dummy.sap.com' 404 def token = 'myWrongToken' 405 def namedUser = 'myUser' 406 def nodeId = 1 407 def file = 'myFile.mtaext' 408 def mtaVersion = '0.0.1' 409 def description = "My description." 410 411 shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, ".* curl .*", '400') 412 413 readFileRule.files << [ 'responseExtDescriptorUpload.txt': 'Something went wrong during MTA Extension Descriptor upload (WE ARE IN VERBOSE MODE)'] 414 415 thrown.expect(AbortException.class) 416 thrown.expectMessage('Unexpected response code received from MTA Extension Descriptor upload (400). 201 expected') 417 418 loggingRule.expect('[TransportManagementService] URL: \'http://dummy.sap.com\', NodeId: \'1\', File: \'myFile.mtaext\', MtaVersion: \'0.0.1\'') 419 loggingRule.expect('[TransportManagementService] Response body: Something went wrong during MTA Extension Descriptor upload (WE ARE IN VERBOSE MODE)') 420 421 loggingRule.expect('[TransportManagementService] MTA Extension Descriptor upload started.') 422 loggingRule.expect('[TransportManagementService] Unexpected response code received from MTA Extension Descriptor upload (400). 201 expected. Response body: Something went wrong during MTA Extension Descriptor upload') 423 424 new TransportManagementService(nullScript, [verbose:true]) 425 .uploadMtaExtDescriptorToNode(url, token, nodeId, file, mtaVersion, description, namedUser) 426 } 427 428 @Test 429 void uploadMtaExtDescriptorToNode__NonVerboseMode__withHttpErrorResponse__throwsError() { 430 431 def url = 'http://dummy.sap.com' 432 def token = 'myWrongToken' 433 def namedUser = 'myUser' 434 def nodeId = 1 435 def file = 'myFile.mtaext' 436 def mtaVersion = '0.0.1' 437 def description = "My description." 438 439 shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, ".* curl .*", '418') 440 441 readFileRule.files << [ 'responseExtDescriptorUpload.txt': 'Something went wrong during MTA Extension Descriptor upload. WE ARE IN NON VERBOSE MODE.'] 442 443 thrown.expect(AbortException.class) 444 thrown.expectMessage('Unexpected response code received from MTA Extension Descriptor upload (418). 201 expected') 445 446 loggingRule.expect('[TransportManagementService] MTA Extension Descriptor upload started.') 447 loggingRule.expect('[TransportManagementService] Unexpected response code received from MTA Extension Descriptor upload (418). 201 expected. Response body: Something went wrong during MTA Extension Descriptor upload. WE ARE IN NON VERBOSE MODE.') 448 449 new TransportManagementService(nullScript, [verbose:false]) 450 .uploadMtaExtDescriptorToNode(url, token, nodeId, file, mtaVersion, description, namedUser) 451 } 452 453 @Test 454 void updateMtaExtDescriptor__successfully() { 455 456 def url = 'http://dummy.sap.com' 457 def token = 'myToken' 458 def namedUser = 'myUser' 459 def nodeId = 1 460 def idOfMtaDescriptor = 1 461 def file = 'myFile.mtaext' 462 def mtaVersion = '0.0.1' 463 def description = "My description." 464 465 shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX,'.*curl.*', '200') 466 467 def tms = new TransportManagementService(nullScript, [:]) 468 def responseDetails = tms.updateMtaExtDescriptor(url, token, nodeId, idOfMtaDescriptor, file, mtaVersion, description, namedUser) 469 470 def oAuthShellCall = shellRule.shell[0].replaceAll('\\\\ ', '') 471 472 assertThat(loggingRule.log, containsString("[TransportManagementService] MTA Extension Descriptor update started.")) 473 assertThat(loggingRule.log, containsString("[TransportManagementService] MTA Extension Descriptor update successful.")) 474 assertThat(oAuthShellCall, startsWith("#!/bin/sh -e ")) 475 assertThat(oAuthShellCall, endsWith("curl --write-out '%{response_code}' -H 'Authorization: Bearer ${token}' -H 'tms-named-user: ${namedUser}' -F 'file=@${file}' -F 'mtaVersion=${mtaVersion}' -F 'description=${description}' --output responseExtDescriptorUpdate.txt -X PUT '${url}/v2/nodes/${nodeId}/mtaExtDescriptors/${idOfMtaDescriptor}'")) 476 assertThat(responseDetails, hasEntry("id", 456)) 477 } 478 479 @Test 480 void updateMtaExtDescriptor__inVerboseMode__yieldsMoreEchos() { 481 482 def url = 'http://dummy.sap.com' 483 def token = 'myToken' 484 def namedUser = 'myUser' 485 def nodeId = 1 486 def idOfMtaDescriptor = 1 487 def file = 'myFile.mtaext' 488 def mtaVersion = '0.0.1' 489 def description = "My description." 490 491 shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, ".* curl .*", '200') 492 493 def tms = new TransportManagementService(nullScript, [verbose: true]) 494 tms.updateMtaExtDescriptor(url, token, nodeId, idOfMtaDescriptor, file, mtaVersion, description, namedUser) 495 496 assertThat(loggingRule.log, containsString("[TransportManagementService] MTA Extension Descriptor update started.")) 497 assertThat(loggingRule.log, containsString("[TransportManagementService] URL: '${url}', NodeId: '${nodeId}', IdOfMtaDescriptor: '${idOfMtaDescriptor}', File: '${file}', MtaVersion: '${mtaVersion}'")) 498 assertThat(loggingRule.log, containsString("\"id\": 456")) 499 assertThat(loggingRule.log, containsString("[TransportManagementService] MTA Extension Descriptor update successful.")) 500 } 501 502 @Test 503 void updateMtaExtDescriptor__verboseMode__withHttpErrorResponse__throwsError() { 504 505 def url = 'http://dummy.sap.com' 506 def token = 'myWrongToken' 507 def namedUser = 'myUser' 508 def nodeId = 1 509 def idOfMtaDescriptor = 1 510 def file = 'myFile.mtaext' 511 def mtaVersion = '0.0.1' 512 def description = "My description." 513 514 shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, ".* curl .*", '400') 515 516 readFileRule.files << [ 'responseExtDescriptorUpdate.txt': 'Something went wrong during MTA Extension Descriptor update (WE ARE IN VERBOSE MODE).'] 517 518 thrown.expect(AbortException.class) 519 thrown.expectMessage('Unexpected response code received from MTA Extension Descriptor update (400). 200 expected') 520 521 loggingRule.expect("[TransportManagementService] URL: '${url}', NodeId: '${nodeId}', IdOfMtaDescriptor: '${idOfMtaDescriptor}', File: '${file}', MtaVersion: '${mtaVersion}'") 522 loggingRule.expect('[TransportManagementService] Response body: Something went wrong during MTA Extension Descriptor update (WE ARE IN VERBOSE MODE).') 523 524 // The log entries which are present in non verbose mode must be present in verbose mode also, of course 525 loggingRule.expect('[TransportManagementService] MTA Extension Descriptor update started.') 526 loggingRule.expect('[TransportManagementService] Unexpected response code received from MTA Extension Descriptor update (400). 200 expected. Response body: Something went wrong during MTA Extension Descriptor update (WE ARE IN VERBOSE MODE).') 527 528 new TransportManagementService(nullScript, [verbose:true]) 529 .updateMtaExtDescriptor(url, token, nodeId, idOfMtaDescriptor, file, mtaVersion, description, namedUser) 530 } 531 532 @Test 533 void updateMtaExtDescriptor__NonVerboseMode__withHttpErrorResponse__throwsError() { 534 535 def url = 'http://dummy.sap.com' 536 def token = 'myWrongToken' 537 def namedUser = 'myUser' 538 def nodeId = 1 539 def idOfMtaDescriptor = 1 540 def file = 'myFile.mtaext' 541 def mtaVersion = '0.0.1' 542 def description = "My description." 543 544 shellRule.setReturnValue(JenkinsShellCallRule.Type.REGEX, ".* curl .*", '418') 545 546 readFileRule.files << [ 'responseExtDescriptorUpdate.txt': 'Something went wrong during MTA Extension Descriptor update (WE ARE IN NON VERBOSE MODE).'] 547 548 thrown.expect(AbortException.class) 549 thrown.expectMessage('Unexpected response code received from MTA Extension Descriptor update (418). 200 expected') 550 551 loggingRule.expect('[TransportManagementService] MTA Extension Descriptor update started.') 552 loggingRule.expect('[TransportManagementService] Unexpected response code received from MTA Extension Descriptor update (418). 200 expected. Response body: Something went wrong during MTA Extension Descriptor update (WE ARE IN NON VERBOSE MODE).') 553 554 new TransportManagementService(nullScript, [verbose:false]) 555 .updateMtaExtDescriptor(url, token, nodeId, idOfMtaDescriptor, file, mtaVersion, description, namedUser) 556 } 557 558 @Test 559 void getNodes__successfully() { 560 Map requestParams 561 helper.registerAllowedMethod('httpRequest', [Map.class], { m -> 562 requestParams = m 563 return [content: '{ "nodes": [{ "id": 1, "name": "testNode1" }, { "id": 2, "name": "testNode2" }] }', status: 200] 564 }) 565 566 def url = 'http://dummy.sap.com' 567 def token = 'myToken' 568 569 def tms = new TransportManagementService(nullScript, [:]) 570 def responseDetails = tms.getNodes(url, token) 571 572 assertFalse(loggingRule.log.contains("[TransportManagementService] Get nodes successful.")) 573 assertThat(requestParams, hasEntry('url', "${url}/v2/nodes")) 574 assertThat(requestParams.customHeaders[0].value, is("Bearer ${token}")) 575 assertThat(responseDetails.getAt("nodes").get(0), hasEntry("id", 1)) 576 assertThat(responseDetails.getAt("nodes").get(1), hasEntry("id", 2)) 577 } 578 579 @Test 580 void getNodes__inVerboseMode__yieldsMoreEchos() { 581 def url = 'http://dummy.sap.com' 582 def token = 'myToken' 583 def responseContent = '{ "nodes": [{ "id": 1, "name": "testNode1" }, { "id": 2, "name": "testNode2" }] }' 584 585 helper.registerAllowedMethod('httpRequest', [Map.class], { 586 return [content: responseContent, status: 200] 587 }) 588 589 def tms = new TransportManagementService(nullScript, [verbose: true]) 590 def responseDetails = tms.getNodes(url, token) 591 592 assertThat(loggingRule.log, containsString("[TransportManagementService] Get nodes started. URL: '${url}'")) 593 assertThat(loggingRule.log, containsString("[TransportManagementService] Get nodes successful. Response content '${responseContent}'.")) 594 } 595 596 @Test 597 void getNodes__failure() { 598 def url = 'http://dummy.sap.com' 599 def token = 'myToken' 600 def responseStatusCode = 500 601 def responseContent = '{ "errorType": "TsInternalServerErrorException", "message": "The application has encountered an unexpected error (THIS PART IS HERE TO CHECK THAT ERROR MESSAGE IS EXPOSED IN NON-VERBOSE MODE)." }' 602 603 thrown.expect(AbortException) 604 thrown.expectMessage("[TransportManagementService] Get nodes failed (HTTP status code '${responseStatusCode}'). Response content '${responseContent}'.") 605 606 helper.registerAllowedMethod('httpRequest', [Map.class], { 607 return [content: responseContent, status: responseStatusCode] 608 }) 609 610 def tms = new TransportManagementService(nullScript, [verbose: false]) 611 tms.getNodes(url, token) 612 } 613 614 @Test 615 void getNodes__failure__status__500__inVerboseMode() { 616 def url = 'http://dummy.sap.com' 617 def token = 'myToken' 618 def responseStatusCode = 500 619 def responseContent = '{ "errorType": "TsInternalServerErrorException", "message": "The application has encountered an unexpected error (THIS PART IS HERE TO CHECK THAT ERROR MESSAGE IS EXPOSED IN VERBOSE MODE)." }' 620 621 thrown.expect(AbortException) 622 thrown.expectMessage("[TransportManagementService] Get nodes failed (HTTP status code '${responseStatusCode}'). Response content '${responseContent}'.") 623 loggingRule.expect("[TransportManagementService] Get nodes started. URL: '${url}'") 624 625 helper.registerAllowedMethod('httpRequest', [Map.class], { 626 return [content: responseContent, status: responseStatusCode] 627 }) 628 629 def tms = new TransportManagementService(nullScript, [verbose: true]) 630 tms.getNodes(url, token) 631 } 632 633 @Test 634 void getExtDescriptor__successfully() { 635 Map requestParams 636 helper.registerAllowedMethod('httpRequest', [Map.class], { m -> 637 requestParams = m 638 return [content: '{ "mtaExtDescriptors": [{"id": 2, "mtaId": "com.sap.piper.tms.test", "mtaExtId": "com.sap.piper.tms.test.extension", "mtaVersion": "1.2.3"}]}', status: 200] 639 }) 640 641 def url = 'http://dummy.sap.com' 642 def token = 'myToken' 643 def nodeId = 1 644 def mtaId = "com.sap.piper.tms.test" 645 def mtaVersion = '1.2.3' 646 647 def tms = new TransportManagementService(nullScript, [:]) 648 def responseDetails = tms.getMtaExtDescriptor(url, token, nodeId, mtaId, mtaVersion) 649 650 assertThat(loggingRule.log, containsString("[TransportManagementService] Get MTA Extension Descriptor started.")) 651 assertThat(loggingRule.log, containsString("[TransportManagementService] Get MTA Extension Descriptor successful.")) 652 assertThat(requestParams, hasEntry('url', "${url}/v2/nodes/${nodeId}/mtaExtDescriptors?mtaId=${mtaId}&mtaVersion=${mtaVersion}")) 653 assertThat(requestParams.customHeaders[0].value, is("Bearer ${token}")) 654 assertThat(responseDetails, hasEntry("id", 2)) 655 assertThat(responseDetails, hasEntry("mtaExtId", "com.sap.piper.tms.test.extension")) 656 } 657 658 @Test 659 void getExtDescriptor__inVerboseMode__yieldsMoreEchos() { 660 def url = 'http://dummy.sap.com' 661 def token = 'myToken' 662 def nodeId = 1 663 def mtaId = "com.sap.piper.tms.test" 664 def mtaVersion = '1.2.3' 665 def responseContent = '{ "mtaExtDescriptors": [{"id": 2, "mtaId": "com.sap.piper.tms.test", "mtaExtId": "com.sap.piper.tms.test.extension", "mtaVersion": "1.2.3"}]}' 666 667 helper.registerAllowedMethod('httpRequest', [Map.class], { 668 return [content: responseContent, status: 200] 669 }) 670 671 def tms = new TransportManagementService(nullScript, [verbose: true]) 672 def responseDetails = tms.getMtaExtDescriptor(url, token, nodeId, mtaId, mtaVersion) 673 674 assertThat(loggingRule.log, containsString("[TransportManagementService] URL: '${url}', NodeId: '${nodeId}', MtaId: '${mtaId}', MtaVersion: '${mtaVersion}'")) 675 assertThat(loggingRule.log, containsString("[TransportManagementService] Response content '${responseContent}'.")) 676 } 677 678 @Test 679 void getExtDescriptor__failure() { 680 def url = 'http://dummy.sap.com' 681 def token = 'myToken' 682 def nodeId = 1 683 def mtaId = "alm.pi.test.scv_x" 684 def mtaVersion = '1.2.3' 685 def responseStatusCode = 500 686 def responseContent = '{ "errorType": "TsInternalServerErrorException", "message": "The application has encountered an unexpected error (THIS PART IS HERE TO CHECK THAT ERROR MESSAGE IS EXPOSED IN NON-VERBOSE MODE)." }' 687 688 thrown.expect(AbortException) 689 thrown.expectMessage("[TransportManagementService] Get MTA Extension Descriptor failed (HTTP status code '${responseStatusCode}'). Response content '${responseContent}'.") 690 691 helper.registerAllowedMethod('httpRequest', [Map.class], { 692 return [content: responseContent, status: responseStatusCode] 693 }) 694 695 def tms = new TransportManagementService(nullScript, [verbose: false]) 696 tms.getMtaExtDescriptor(url, token, nodeId, mtaId, mtaVersion) 697 } 698 699 @Test 700 void getAExtDescriptor__failure__status__500__inVerboseMode() { 701 def url = 'http://dummy.sap.com' 702 def token = 'myToken' 703 def nodeId = 1 704 def mtaId = "alm.pi.test.scv_x" 705 def mtaVersion = '1.2.3' 706 def responseStatusCode = 500 707 def responseContent = '{ "errorType": "TsInternalServerErrorException", "message": "The application has encountered an unexpected error (THIS PART IS HERE TO CHECK THAT ERROR MESSAGE IS EXPOSED IN VERBOSE MODE)." }' 708 709 thrown.expect(AbortException) 710 thrown.expectMessage("[TransportManagementService] Get MTA Extension Descriptor failed (HTTP status code '${responseStatusCode}'). Response content '${responseContent}'.") 711 loggingRule.expect("[TransportManagementService] Get MTA Extension Descriptor started.") 712 loggingRule.expect("[TransportManagementService] URL: '${url}', NodeId: '${nodeId}', MtaId: '${mtaId}', MtaVersion: '${mtaVersion}'") 713 714 helper.registerAllowedMethod('httpRequest', [Map.class], { 715 return [content: responseContent, status: responseStatusCode] 716 }) 717 718 def tms = new TransportManagementService(nullScript, [verbose: true]) 719 tms.getMtaExtDescriptor(url, token, nodeId, mtaId, mtaVersion) 720 } 721 }