github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/abapEnvironmentPushATCSystemConfig_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "bytes" 8 "os" 9 "path/filepath" 10 "testing" 11 12 "github.com/SAP/jenkins-library/pkg/abaputils" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestFetchXcsrfTokenFromHead(t *testing.T) { 17 t.Parallel() 18 t.Run("FetchXcsrfToken Test", func(t *testing.T) { 19 tokenExpected := "myToken" 20 21 client := &abaputils.ClientMock{ 22 Body: `Xcsrf Token test`, 23 Token: tokenExpected, 24 } 25 26 con := abaputils.ConnectionDetailsHTTP{ 27 User: "Test", 28 Password: "Test", 29 URL: "https://api.endpoint.com/Entity/", 30 } 31 token, error := fetchXcsrfTokenFromHead(con, client) 32 if error == nil { 33 assert.Equal(t, tokenExpected, token) 34 } 35 }) 36 t.Run("failure case: fetch token", func(t *testing.T) { 37 tokenExpected := "" 38 39 client := &abaputils.ClientMock{ 40 Body: `Xcsrf Token test`, 41 Token: "", 42 } 43 44 con := abaputils.ConnectionDetailsHTTP{ 45 User: "Test", 46 Password: "Test", 47 URL: "https://api.endpoint.com/Entity/", 48 } 49 token, error := fetchXcsrfTokenFromHead(con, client) 50 if error == nil { 51 assert.Equal(t, tokenExpected, token) 52 } 53 }) 54 } 55 56 func TestCheckATCSystemConfigurationFile(t *testing.T) { 57 t.Parallel() 58 t.Run("Check ATC Configuration File - empty", func(t *testing.T) { 59 errExpected := "pushing ATC System Configuration failed. Reason: Configured Filelocation is empty (File: atcSystemConfig.json)" 60 var parsedConfigurationJsonExpected parsedConfigJsonWithExpand 61 var atcSystemConfiguartionJsonFileExpected []byte 62 63 config := abapEnvironmentPushATCSystemConfigOptions{AtcSystemConfigFilePath: "atcSystemConfig.json"} 64 65 parsedConfigurationJson, atcSystemConfiguartionJsonFile, err := checkATCSystemConfigurationFile(&config) 66 assert.Equal(t, errExpected, err.Error()) 67 assert.Equal(t, parsedConfigurationJson, parsedConfigurationJsonExpected) 68 assert.Equal(t, atcSystemConfiguartionJsonFile, atcSystemConfiguartionJsonFileExpected) 69 }) 70 } 71 72 func TestHandleHttpResponse(t *testing.T) { 73 t.Parallel() 74 75 t.Run("failiure case: HandleHttpResponse", func(t *testing.T) { 76 bodyText := ` 77 --B772E21DAA42B9571C778276B829D6C20 78 Content-Type: multipart/mixed; boundary=B772E21DAA42B9571C778276B829D6C21 79 Content-Length: 1973 80 81 --B772E21DAA42B9571C778276B829D6C21 82 Content-Type: application/http 83 Content-Length: 646 84 content-transfer-encoding: binary 85 content-id: 1 86 87 HTTP/1.1 200 OK 88 Content-Type: application/json;odata.metadata=minimal;charset=utf-8 89 Content-Length: 465 90 odata-version: 4.0 91 cache-control: no-cache, no-store, must-revalidate 92 93 {"@odata.context":"$metadata#configuration/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","conf_name":"Z_CONFIG_VIA_PIPELINE_STEP","checkvariant":"ABAP_CLOUD_DEVELOPMENT_DEFAULT","pseudo_comment_policy":"SP","last_changed_by":"CC0000000017","last_changed_at":"2022-03-02T11:16:51.336172Z","block_findings":"0","inform_findings":"1","transport_check_policy":"C","check_tasks":true,"check_requests":false,"check_tocs":true,"is_default":false,"is_proxy_variant":false,"SAP__Messages":[]} 94 --B772E21DAA42B9571C778276B829D6C21 95 Content-Type: application/http 96 Content-Length: 428 97 content-transfer-encoding: binary 98 content-id: 2 99 100 HTTP/1.1 200 OK 101 Content-Type: application/json;odata.metadata=minimal;charset=utf-8 102 Content-Length: 247 103 odata-version: 4.0 104 cache-control: no-cache, no-store, must-revalidate 105 106 {"@odata.context":"$metadata#priority/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","test":"CL_CI_ARS_COMPATIBILITY_CHECK","message_id":"010","default_priority":1,"priority":2} 107 --B772E21DAA42B9571C778276B829D6C21 108 Content-Type: application/http 109 Content-Length: 428 110 content-transfer-encoding: binary 111 content-id: 3 112 113 HTTP/1.1 4** OK 114 Content-Type: application/json;odata.metadata=minimal;charset=utf-8 115 Content-Length: 247 116 odata-version: 4.0 117 cache-control: no-cache, no-store, must-revalidate 118 119 {"Some Error Messages possible in here!"} 120 --B772E21DAA42B9571C778276B829D6C21-- 121 122 --B772E21DAA42B9571C778276B829D6C20--` 123 124 client := &abaputils.ClientMock{ 125 Body: bodyText, 126 Token: "myToken", 127 StatusCode: 200, 128 } 129 con := abaputils.ConnectionDetailsHTTP{ 130 User: "Test", 131 Password: "Test", 132 URL: "https://api.endpoint.com/Entity/", 133 } 134 135 body := []byte(client.Body) 136 resp, err := client.SendRequest("POST", con.URL, bytes.NewBuffer(body), nil, nil) 137 if err != nil || 138 resp == nil { 139 t.Fatal("Mock should not fail") 140 } 141 resp.Header.Set("Content-type", "multipart/mixed") 142 err = HandleHttpResponse(resp, err, "Unit Test", con) 143 // inner error expected 144 errExpected := "Outer Response Code: 200 - but at least one Inner response returned StatusCode 4* or 5*. Please check Log for details." 145 assert.Equal(t, errExpected, err.Error()) 146 }) 147 148 t.Run("success case: HandleHttpResponse", func(t *testing.T) { 149 bodyText := ` 150 --B772E21DAA42B9571C778276B829D6C20 151 Content-Type: multipart/mixed; boundary=B772E21DAA42B9571C778276B829D6C21 152 Content-Length: 1973 153 154 --B772E21DAA42B9571C778276B829D6C21 155 Content-Type: application/http 156 Content-Length: 646 157 content-transfer-encoding: binary 158 content-id: 1 159 160 HTTP/1.1 200 OK 161 Content-Type: application/json;odata.metadata=minimal;charset=utf-8 162 Content-Length: 465 163 odata-version: 4.0 164 cache-control: no-cache, no-store, must-revalidate 165 166 {"@odata.context":"$metadata#configuration/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","conf_name":"Z_CONFIG_VIA_PIPELINE_STEP","checkvariant":"ABAP_CLOUD_DEVELOPMENT_DEFAULT","pseudo_comment_policy":"SP","last_changed_by":"CC0000000017","last_changed_at":"2022-03-02T11:16:51.336172Z","block_findings":"0","inform_findings":"1","transport_check_policy":"C","check_tasks":true,"check_requests":false,"check_tocs":true,"is_default":false,"is_proxy_variant":false,"SAP__Messages":[]} 167 --B772E21DAA42B9571C778276B829D6C21 168 Content-Type: application/http 169 Content-Length: 428 170 content-transfer-encoding: binary 171 content-id: 2 172 173 HTTP/1.1 200 OK 174 Content-Type: application/json;odata.metadata=minimal;charset=utf-8 175 Content-Length: 247 176 odata-version: 4.0 177 cache-control: no-cache, no-store, must-revalidate 178 179 {"@odata.context":"$metadata#priority/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","test":"CL_CI_ARS_COMPATIBILITY_CHECK","message_id":"010","default_priority":1,"priority":2} 180 --B772E21DAA42B9571C778276B829D6C21 181 Content-Type: application/http 182 Content-Length: 428 183 content-transfer-encoding: binary 184 content-id: 3 185 186 HTTP/1.1 200 OK 187 Content-Type: application/json;odata.metadata=minimal;charset=utf-8 188 Content-Length: 247 189 odata-version: 4.0 190 cache-control: no-cache, no-store, must-revalidate 191 192 {"@odata.context":"$metadata#priority/$entity","@odata.metadataEtag":"W/\"20220211135922\"","root_id":"1","conf_id":"aef8f52b-fe16-1edc-a3fe-27a1e0226c7b","test":"CL_CI_ARS_COMPATIBILITY_CHECK","message_id":"011","default_priority":2,"priority":1} 193 --B772E21DAA42B9571C778276B829D6C21-- 194 195 --B772E21DAA42B9571C778276B829D6C20--` 196 197 client := &abaputils.ClientMock{ 198 Body: bodyText, 199 Token: "myToken", 200 StatusCode: 200, 201 } 202 con := abaputils.ConnectionDetailsHTTP{ 203 User: "Test", 204 Password: "Test", 205 URL: "https://api.endpoint.com/Entity/", 206 } 207 208 body := []byte(client.Body) 209 resp, err := client.SendRequest("POST", con.URL, bytes.NewBuffer(body), nil, nil) 210 if err != nil || 211 resp == nil { 212 t.Fatal("Mock should not fail") 213 } 214 resp.Header.Set("Content-type", "multipart/mixed") 215 err = HandleHttpResponse(resp, err, "Unit Test", con) 216 assert.NoError(t, err, "No error expected") 217 }) 218 } 219 220 func TestBuildATCSystemConfigBatchRequest(t *testing.T) { 221 t.Parallel() 222 223 t.Run("success case: BuildATCSystemConfigBatch - Config Base & 1 Priority", func(t *testing.T) { 224 batchATCSystemConfigFileExpected := ` 225 --request-separator 226 Content-Type: multipart/mixed;boundary=changeset 227 228 --changeset 229 Content-Type: application/http 230 Content-Transfer-Encoding: binary 231 Content-ID: 1 232 233 PATCH configuration(root_id='1',conf_id=4711) HTTP/1.1 234 Content-Type: application/json 235 236 {"conf_id":"4711","block_findings":"0","check_requests":false,"check_tasks":true,"check_tocs":true,"checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","conf_name":"UNITTEST_PIPERSTEP","inform_findings":"1","pseudo_comment_policy":"MK","transport_check_policy":"C"} 237 238 --changeset 239 Content-Type: application/http 240 Content-Transfer-Encoding: binary 241 Content-ID: 2 242 243 PATCH priority(root_id='1',conf_id=4711,test='CL_CI_TEST_AMDP_HDB_MIGRATION',message_id='FAIL_ABAP') HTTP/1.1 244 Content-Type: application/json 245 246 {"priority":1} 247 248 --changeset-- 249 250 --request-separator--` 251 252 // no Configuration name supplied 253 atcSystemConfigFileString := `{ 254 "conf_name": "UNITTEST_PIPERSTEP", 255 "checkvariant": "SAP_CLOUD_PLATFORM_ATC_DEFAULT", 256 "pseudo_comment_policy": "MK", 257 "block_findings": "0", 258 "inform_findings": "1", 259 "transport_check_policy": "C", 260 "check_tasks": true, 261 "check_requests": false, 262 "check_tocs": true, 263 "_priorities": [ 264 { 265 "test": "CL_CI_TEST_AMDP_HDB_MIGRATION", 266 "message_id": "FAIL_ABAP", 267 "default_priority": 3, 268 "priority": 1 269 } 270 ] 271 } 272 ` 273 274 confUUID := "4711" 275 batchATCSystemConfigFile, err := buildATCSystemConfigBatchRequest(confUUID, []byte(atcSystemConfigFileString)) 276 if err != nil { 277 t.Fatal("Failed to Build ATC System Config Batch") 278 } 279 assert.Equal(t, batchATCSystemConfigFileExpected, batchATCSystemConfigFile) 280 }) 281 282 t.Run("success case: BuildATCSystemConfigBatch - Config Base & 2 Priorities", func(t *testing.T) { 283 batchATCSystemConfigFileExpected := ` 284 --request-separator 285 Content-Type: multipart/mixed;boundary=changeset 286 287 --changeset 288 Content-Type: application/http 289 Content-Transfer-Encoding: binary 290 Content-ID: 1 291 292 PATCH configuration(root_id='1',conf_id=4711) HTTP/1.1 293 Content-Type: application/json 294 295 {"conf_id":"4711","block_findings":"0","check_requests":false,"check_tasks":true,"check_tocs":true,"checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","conf_name":"UNITTEST_PIPERSTEP","inform_findings":"1","pseudo_comment_policy":"MK","transport_check_policy":"C"} 296 297 --changeset 298 Content-Type: application/http 299 Content-Transfer-Encoding: binary 300 Content-ID: 2 301 302 PATCH priority(root_id='1',conf_id=4711,test='CL_CI_TEST_AMDP_HDB_MIGRATION',message_id='FAIL_ABAP') HTTP/1.1 303 Content-Type: application/json 304 305 {"priority":1} 306 307 --changeset 308 Content-Type: application/http 309 Content-Transfer-Encoding: binary 310 Content-ID: 3 311 312 PATCH priority(root_id='1',conf_id=4711,test='CL_CI_TEST_AMDP_HDB_MIGRATION',message_id='FAIL_AMDP') HTTP/1.1 313 Content-Type: application/json 314 315 {"priority":2} 316 317 --changeset-- 318 319 --request-separator--` 320 321 // no Configuration name supplied 322 atcSystemConfigFileString := `{ 323 "conf_name": "UNITTEST_PIPERSTEP", 324 "checkvariant": "SAP_CLOUD_PLATFORM_ATC_DEFAULT", 325 "pseudo_comment_policy": "MK", 326 "block_findings": "0", 327 "inform_findings": "1", 328 "transport_check_policy": "C", 329 "check_tasks": true, 330 "check_requests": false, 331 "check_tocs": true, 332 "_priorities": [ 333 { 334 "test": "CL_CI_TEST_AMDP_HDB_MIGRATION", 335 "message_id": "FAIL_ABAP", 336 "default_priority": 3, 337 "priority": 1 338 }, 339 { 340 "test": "CL_CI_TEST_AMDP_HDB_MIGRATION", 341 "message_id": "FAIL_AMDP", 342 "priority": 2 343 } 344 ] 345 } 346 ` 347 348 confUUID := "4711" 349 batchATCSystemConfigFile, err := buildATCSystemConfigBatchRequest(confUUID, []byte(atcSystemConfigFileString)) 350 if err != nil { 351 t.Fatal("Failed to Build ATC System Config Batch Request") 352 } 353 assert.Equal(t, batchATCSystemConfigFileExpected, batchATCSystemConfigFile) 354 }) 355 356 t.Run("success case: BuildATCSystemConfigBatch - Config Base only (no existing _priorities)", func(t *testing.T) { 357 batchATCSystemConfigFileExpected := ` 358 --request-separator 359 Content-Type: multipart/mixed;boundary=changeset 360 361 --changeset 362 Content-Type: application/http 363 Content-Transfer-Encoding: binary 364 Content-ID: 1 365 366 PATCH configuration(root_id='1',conf_id=4711) HTTP/1.1 367 Content-Type: application/json 368 369 {"conf_id":"4711","block_findings":"0","check_requests":false,"check_tasks":true,"check_tocs":true,"checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","conf_name":"UNITTEST_PIPERSTEP","inform_findings":"1","pseudo_comment_policy":"MK","transport_check_policy":"C"} 370 371 --changeset-- 372 373 --request-separator--` 374 375 // no Configuration name supplied 376 atcSystemConfigFileString := `{ 377 "conf_name": "UNITTEST_PIPERSTEP", 378 "checkvariant": "SAP_CLOUD_PLATFORM_ATC_DEFAULT", 379 "pseudo_comment_policy": "MK", 380 "block_findings": "0", 381 "inform_findings": "1", 382 "transport_check_policy": "C", 383 "check_tasks": true, 384 "check_requests": false, 385 "check_tocs": true 386 } 387 ` 388 389 confUUID := "4711" 390 batchATCSystemConfigFile, err := buildATCSystemConfigBatchRequest(confUUID, []byte(atcSystemConfigFileString)) 391 if err != nil { 392 t.Fatal("Failed to Build ATC System Config Batch") 393 } 394 assert.Equal(t, batchATCSystemConfigFileExpected, batchATCSystemConfigFile) 395 }) 396 397 t.Run("success case: BuildATCSystemConfigBatch - Config Base only (empty expand _priorities)", func(t *testing.T) { 398 batchATCSystemConfigFileExpected := ` 399 --request-separator 400 Content-Type: multipart/mixed;boundary=changeset 401 402 --changeset 403 Content-Type: application/http 404 Content-Transfer-Encoding: binary 405 Content-ID: 1 406 407 PATCH configuration(root_id='1',conf_id=4711) HTTP/1.1 408 Content-Type: application/json 409 410 {"conf_id":"4711","block_findings":"0","check_requests":false,"check_tasks":true,"check_tocs":true,"checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","conf_name":"UNITTEST_PIPERSTEP","inform_findings":"1","pseudo_comment_policy":"MK","transport_check_policy":"C"} 411 412 --changeset-- 413 414 --request-separator--` 415 416 // no Configuration name supplied 417 atcSystemConfigFileString := `{ 418 "conf_name": "UNITTEST_PIPERSTEP", 419 "checkvariant": "SAP_CLOUD_PLATFORM_ATC_DEFAULT", 420 "pseudo_comment_policy": "MK", 421 "block_findings": "0", 422 "inform_findings": "1", 423 "transport_check_policy": "C", 424 "check_tasks": true, 425 "check_requests": false, 426 "check_tocs": true, 427 "_priorities": [ 428 ] 429 } 430 ` 431 432 confUUID := "4711" 433 batchATCSystemConfigFile, err := buildATCSystemConfigBatchRequest(confUUID, []byte(atcSystemConfigFileString)) 434 if err != nil { 435 t.Fatal("Failed to Build ATC System Config Batch") 436 } 437 assert.Equal(t, batchATCSystemConfigFileExpected, batchATCSystemConfigFile) 438 }) 439 440 t.Run("success case: BuildATCSystemConfigBatch - Config Base only (empty expand _priorities) - Settings Global", func(t *testing.T) { 441 batchATCSystemConfigFileExpected := ` 442 --request-separator 443 Content-Type: multipart/mixed;boundary=changeset 444 445 --changeset 446 Content-Type: application/http 447 Content-Transfer-Encoding: binary 448 Content-ID: 1 449 450 PATCH configuration(root_id='1',conf_id=4711) HTTP/1.1 451 Content-Type: application/json 452 453 {"conf_id":"4711","block_findings":"0","checkvariant":"SAP_CLOUD_PLATFORM_ATC_DEFAULT","conf_name":"UNITTEST_PIPERSTEP","inform_findings":"1","pseudo_comment_policy":"MK","transport_check_policy":"G"} 454 455 --changeset-- 456 457 --request-separator--` 458 459 // no Configuration name supplied 460 atcSystemConfigFileString := `{ 461 "conf_name": "UNITTEST_PIPERSTEP", 462 "checkvariant": "SAP_CLOUD_PLATFORM_ATC_DEFAULT", 463 "pseudo_comment_policy": "MK", 464 "block_findings": "0", 465 "inform_findings": "1", 466 "transport_check_policy": "G", 467 "_priorities": [ 468 ] 469 } 470 ` 471 472 confUUID := "4711" 473 batchATCSystemConfigFile, err := buildATCSystemConfigBatchRequest(confUUID, []byte(atcSystemConfigFileString)) 474 if err != nil { 475 t.Fatal("Failed to Build ATC System Config Batch") 476 } 477 assert.Equal(t, batchATCSystemConfigFileExpected, batchATCSystemConfigFile) 478 }) 479 480 t.Run("failure case: BuildATCSystemConfigBatch", func(t *testing.T) { 481 batchATCSystemConfigFileExpected := `` 482 483 // no Configuration name supplied 484 atcSystemConfigFileString := `{ 485 "conf_name": "UNITTEST_PIPERSTEP", 486 "checkvariant": "SAP_CLOUD_PLATFORM_ATC_DEFAULT", 487 "pseudo_comment_policy": "MK", 488 "block_findings": "0", 489 "inform_findings": "1", 490 "transport_check_policy": "C", 491 "check_tasks": true, 492 "check_requests": false, 493 "check_tocs": true, 494 "_priorities": [ 495 { 496 "test": "CL_CI_TEST_AMDP_HDB_MIGRATION", 497 "message_id": "FAIL_ABAP", 498 "default_priority": 3, 499 "priority": 1 500 } 501 ] 502 } 503 ` 504 505 confUUID := "4711" 506 batchATCSystemConfigFile, err := buildATCSystemConfigBatchRequest(confUUID, []byte(atcSystemConfigFileString)) 507 if err != nil { 508 t.Fatal("Failed to Build ATC System Config Batch") 509 } 510 assert.NotEqual(t, batchATCSystemConfigFileExpected, batchATCSystemConfigFile) 511 }) 512 } 513 514 func TestRunAbapEnvironmentPushATCSystemConfig(t *testing.T) { 515 t.Parallel() 516 517 t.Run("run Step Failure - ATC System Configuration File empty", func(t *testing.T) { 518 autils := abaputils.AUtilsMock{} 519 defer autils.Cleanup() 520 autils.ReturnedConnectionDetailsHTTP.Password = "password" 521 autils.ReturnedConnectionDetailsHTTP.User = "user" 522 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 523 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 524 525 receivedURI := "example.com/Entity" 526 tokenExpected := "myToken" 527 528 client := &abaputils.ClientMock{ 529 Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`, 530 Token: tokenExpected, 531 StatusCode: 200, 532 } 533 534 dir := t.TempDir() 535 536 config := abapEnvironmentPushATCSystemConfigOptions{AtcSystemConfigFilePath: filepath.Join(dir, "atcSystemConfig.json")} 537 538 atcSystemConfigFileString := `` 539 540 err := os.WriteFile(config.AtcSystemConfigFilePath, []byte(atcSystemConfigFileString), 0o644) 541 if err != nil { 542 t.Fatal("Failed to write File: " + config.AtcSystemConfigFilePath) 543 } 544 545 expectedErrorMessage := "pushing ATC System Configuration failed. Reason: Configured File is empty (File: " + config.AtcSystemConfigFilePath + ")" 546 547 err = runAbapEnvironmentPushATCSystemConfig(&config, nil, &autils, client) 548 assert.Equal(t, expectedErrorMessage, err.Error(), "Different error message expected") 549 }) 550 551 t.Run("run Step Failure - ATC System Configuration invalid", func(t *testing.T) { 552 autils := abaputils.AUtilsMock{} 553 defer autils.Cleanup() 554 autils.ReturnedConnectionDetailsHTTP.Password = "password" 555 autils.ReturnedConnectionDetailsHTTP.User = "user" 556 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 557 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 558 559 receivedURI := "example.com/Entity" 560 tokenExpected := "myToken" 561 562 client := &abaputils.ClientMock{ 563 Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`, 564 Token: tokenExpected, 565 StatusCode: 200, 566 } 567 568 dir := t.TempDir() 569 570 config := abapEnvironmentPushATCSystemConfigOptions{AtcSystemConfigFilePath: filepath.Join(dir, "atcSystemConfig.json")} 571 572 // no Configuration name supplied 573 atcSystemConfigFileString := `{ 574 "conf_name": "", 575 "checkvariant": "SAP_CLOUD_PLATFORM_ATC_DEFAULT", 576 "pseudo_comment_policy": "MK", 577 "block_findings": "0", 578 "inform_findings": "1", 579 "is_default": false, 580 "is_proxy_variant": false, 581 "transport_check_policy": "C", 582 "check_tasks": true, 583 "check_requests": false, 584 "check_tocs": true, 585 "_priorities": [ 586 { 587 "test": "CL_CI_TEST_AMDP_HDB_MIGRATION", 588 "message_id": "FAIL_ABAP", 589 "default_priority": 3, 590 "priority": 1 591 } 592 ] 593 } 594 ` 595 err := os.WriteFile(config.AtcSystemConfigFilePath, []byte(atcSystemConfigFileString), 0o644) 596 if err != nil { 597 t.Fatal("Failed to write File: " + config.AtcSystemConfigFilePath) 598 } 599 600 expectedErrorMessage := "pushing ATC System Configuration failed. Reason: Configured File does not contain required ATC System Configuration attributes (File: " + config.AtcSystemConfigFilePath + ")" 601 602 err = runAbapEnvironmentPushATCSystemConfig(&config, nil, &autils, client) 603 assert.Equal(t, expectedErrorMessage, err.Error(), "Different error message expected") 604 }) 605 606 t.Run("run Step Successful - Push ATC System Configuration", func(t *testing.T) { 607 autils := abaputils.AUtilsMock{} 608 defer autils.Cleanup() 609 autils.ReturnedConnectionDetailsHTTP.Password = "password" 610 autils.ReturnedConnectionDetailsHTTP.User = "user" 611 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 612 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 613 614 receivedURI := "example.com/Entity" 615 tokenExpected := "myToken" 616 617 client := &abaputils.ClientMock{ 618 Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`, 619 Token: tokenExpected, 620 StatusCode: 200, 621 } 622 623 dir := t.TempDir() 624 625 config := abapEnvironmentPushATCSystemConfigOptions{AtcSystemConfigFilePath: filepath.Join(dir, "atcSystemConfig.json")} 626 627 // valid ATC System Configuration File 628 atcSystemConfigFileString := `{ 629 "conf_name": "UNITTEST_PIPERSTEP", 630 "checkvariant": "SAP_CLOUD_PLATFORM_ATC_DEFAULT", 631 "pseudo_comment_policy": "MK", 632 "block_findings": "0", 633 "inform_findings": "1", 634 "is_default": false, 635 "is_proxy_variant": false, 636 "transport_check_policy": "C", 637 "check_tasks": true, 638 "check_requests": false, 639 "check_tocs": true, 640 "_priorities": [ 641 { 642 "test": "CL_SOMECLASS", 643 "message_id": "SOME_MESSAGE_ID", 644 "default_priority": 3, 645 "priority": 1 646 } 647 ] 648 } 649 ` 650 err := os.WriteFile(config.AtcSystemConfigFilePath, []byte(atcSystemConfigFileString), 0o644) 651 if err != nil { 652 t.Fatal("Failed to write File: " + config.AtcSystemConfigFilePath) 653 } 654 655 err = runAbapEnvironmentPushATCSystemConfig(&config, nil, &autils, client) 656 assert.NoError(t, err, "No error expected") 657 }) 658 659 t.Run("run Step Failure - ATC System Configuration File does not exist", func(t *testing.T) { 660 autils := abaputils.AUtilsMock{} 661 defer autils.Cleanup() 662 autils.ReturnedConnectionDetailsHTTP.Password = "password" 663 autils.ReturnedConnectionDetailsHTTP.User = "user" 664 autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" 665 autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" 666 667 receivedURI := "example.com/Entity" 668 tokenExpected := "myToken" 669 670 client := &abaputils.ClientMock{ 671 Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`, 672 Token: tokenExpected, 673 StatusCode: 200, 674 } 675 676 config := abapEnvironmentPushATCSystemConfigOptions{AtcSystemConfigFilePath: "test.json"} 677 678 expectedErrorMessage := "pushing ATC System Configuration failed. Reason: Configured Filelocation is empty (File: " + config.AtcSystemConfigFilePath + ")" 679 680 err := runAbapEnvironmentPushATCSystemConfig(&config, nil, &autils, client) 681 assert.Equal(t, expectedErrorMessage, err.Error(), "Different error message expected") 682 }) 683 }