github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/abapEnvironmentRunAUnitTest_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "bytes" 8 "encoding/xml" 9 "errors" 10 "fmt" 11 "os" 12 "testing" 13 14 "github.com/SAP/jenkins-library/pkg/abaputils" 15 "github.com/SAP/jenkins-library/pkg/mock" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 func TestBuildAUnitRequestBody(t *testing.T) { 20 t.Parallel() 21 22 t.Run("Test AUnit test run body with no data", func(t *testing.T) { 23 t.Parallel() 24 25 var config abapEnvironmentRunAUnitTestOptions 26 27 bodyString, err := buildAUnitRequestBody(config) 28 expectedBodyString := "" 29 30 assert.Equal(t, expectedBodyString, bodyString) 31 assert.EqualError(t, err, "No configuration provided - please provide either an AUnit configuration file or a repository configuration file") 32 }) 33 34 t.Run("Test AUnit test run body with example yaml config of not supported Object Sets", func(t *testing.T) { 35 t.Parallel() 36 37 expectedoptionsString := `<aunit:options><aunit:measurements type="none"/><aunit:scope ownTests="false" foreignTests="false"/><aunit:riskLevel harmless="false" dangerous="false" critical="false"/><aunit:duration short="false" medium="false" long="false"/></aunit:options>` 38 expectedobjectSetString := `` 39 40 config := AUnitConfig{ 41 Title: "Test Title", 42 Context: "Test Context", 43 Options: AUnitOptions{ 44 Measurements: "none", 45 Scope: Scope{ 46 OwnTests: new(bool), 47 ForeignTests: new(bool), 48 }, 49 RiskLevel: RiskLevel{ 50 Harmless: new(bool), 51 Dangerous: new(bool), 52 Critical: new(bool), 53 }, 54 Duration: Duration{ 55 Short: new(bool), 56 Medium: new(bool), 57 Long: new(bool), 58 }, 59 }, 60 ObjectSet: abaputils.ObjectSet{ 61 Type: "testSet", 62 Set: []abaputils.Set{ 63 { 64 Type: "testSet", 65 Set: []abaputils.Set{ 66 { 67 Type: "testAUnitFlatObjectSet", 68 FlatObjectSet: []abaputils.FlatObjectSet{ 69 { 70 Name: "TestCLAS", 71 Type: "CLAS", 72 }, 73 { 74 Name: "TestINTF", 75 Type: "INTF", 76 }, 77 }, 78 }, 79 { 80 Type: "testAUnitObjectTypeSet", 81 ObjectTypeSet: []abaputils.ObjectTypeSet{ 82 { 83 Name: "TestObjectType", 84 }, 85 }, 86 }, 87 }, 88 }, 89 }, 90 }, 91 } 92 93 objectSetString := abaputils.BuildOSLString(config.ObjectSet) 94 optionsString := buildAUnitOptionsString(config) 95 96 assert.Equal(t, expectedoptionsString, optionsString) 97 assert.Equal(t, expectedobjectSetString, objectSetString) 98 }) 99 100 t.Run("Test AUnit test run body with example yaml config of only Multi Property Set", func(t *testing.T) { 101 t.Parallel() 102 103 expectedoptionsString := `<aunit:options><aunit:measurements type="none"/><aunit:scope ownTests="false" foreignTests="false"/><aunit:riskLevel harmless="false" dangerous="false" critical="false"/><aunit:duration short="false" medium="false" long="false"/></aunit:options>` 104 expectedobjectSetString := `<osl:objectSet xsi:type="multiPropertySet" xmlns:osl="http://www.sap.com/api/osl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><osl:softwareComponent name="testComponent1"/><osl:softwareComponent name="testComponent2"/></osl:objectSet>` 105 106 config := AUnitConfig{ 107 Title: "Test Title", 108 Context: "Test Context", 109 Options: AUnitOptions{ 110 Measurements: "none", 111 Scope: Scope{ 112 OwnTests: new(bool), 113 ForeignTests: new(bool), 114 }, 115 RiskLevel: RiskLevel{ 116 Harmless: new(bool), 117 Dangerous: new(bool), 118 Critical: new(bool), 119 }, 120 Duration: Duration{ 121 Short: new(bool), 122 Medium: new(bool), 123 Long: new(bool), 124 }, 125 }, 126 ObjectSet: abaputils.ObjectSet{ 127 Type: "multiPropertySet", 128 MultiPropertySet: abaputils.MultiPropertySet{ 129 SoftwareComponents: []abaputils.SoftwareComponents{ 130 { 131 Name: "testComponent1", 132 }, 133 { 134 Name: "testComponent2", 135 }, 136 }, 137 }, 138 }, 139 } 140 141 objectSetString := abaputils.BuildOSLString(config.ObjectSet) 142 optionsString := buildAUnitOptionsString(config) 143 144 assert.Equal(t, expectedoptionsString, optionsString) 145 assert.Equal(t, expectedobjectSetString, objectSetString) 146 }) 147 148 t.Run("Test AUnit test run body with example yaml config of only Multi Property Set but empty type", func(t *testing.T) { 149 t.Parallel() 150 151 expectedoptionsString := `<aunit:options><aunit:measurements type="none"/><aunit:scope ownTests="false" foreignTests="false"/><aunit:riskLevel harmless="false" dangerous="false" critical="false"/><aunit:duration short="false" medium="false" long="false"/></aunit:options>` 152 expectedobjectSetString := `<osl:objectSet xsi:type="multiPropertySet" xmlns:osl="http://www.sap.com/api/osl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><osl:softwareComponent name="testComponent1"/><osl:softwareComponent name="testComponent2"/></osl:objectSet>` 153 154 config := AUnitConfig{ 155 Title: "Test Title", 156 Context: "Test Context", 157 Options: AUnitOptions{ 158 Measurements: "none", 159 Scope: Scope{ 160 OwnTests: new(bool), 161 ForeignTests: new(bool), 162 }, 163 RiskLevel: RiskLevel{ 164 Harmless: new(bool), 165 Dangerous: new(bool), 166 Critical: new(bool), 167 }, 168 Duration: Duration{ 169 Short: new(bool), 170 Medium: new(bool), 171 Long: new(bool), 172 }, 173 }, 174 ObjectSet: abaputils.ObjectSet{ 175 Type: "", 176 MultiPropertySet: abaputils.MultiPropertySet{ 177 SoftwareComponents: []abaputils.SoftwareComponents{ 178 { 179 Name: "testComponent1", 180 }, 181 { 182 Name: "testComponent2", 183 }, 184 }, 185 }, 186 }, 187 } 188 189 objectSetString := abaputils.BuildOSLString(config.ObjectSet) 190 optionsString := buildAUnitOptionsString(config) 191 192 assert.Equal(t, expectedoptionsString, optionsString) 193 assert.Equal(t, expectedobjectSetString, objectSetString) 194 }) 195 196 t.Run("Test AUnit test run body with example yaml config of only Multi Property Set with scomps & packages on top level", func(t *testing.T) { 197 t.Parallel() 198 199 expectedoptionsString := `<aunit:options><aunit:measurements type="none"/><aunit:scope ownTests="false" foreignTests="false"/><aunit:riskLevel harmless="false" dangerous="false" critical="false"/><aunit:duration short="false" medium="false" long="false"/></aunit:options>` 200 expectedobjectSetString := `<osl:objectSet xsi:type="multiPropertySet" xmlns:osl="http://www.sap.com/api/osl" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><osl:package name="testPackage1"/><osl:package name="testPackage2"/><osl:softwareComponent name="testComponent1"/><osl:softwareComponent name="testComponent2"/></osl:objectSet>` 201 202 config := AUnitConfig{ 203 Title: "Test Title", 204 Context: "Test Context", 205 Options: AUnitOptions{ 206 Measurements: "none", 207 Scope: Scope{ 208 OwnTests: new(bool), 209 ForeignTests: new(bool), 210 }, 211 RiskLevel: RiskLevel{ 212 Harmless: new(bool), 213 Dangerous: new(bool), 214 Critical: new(bool), 215 }, 216 Duration: Duration{ 217 Short: new(bool), 218 Medium: new(bool), 219 Long: new(bool), 220 }, 221 }, 222 ObjectSet: abaputils.ObjectSet{ 223 PackageNames: []abaputils.Package{{ 224 Name: "testPackage1", 225 }, { 226 Name: "testPackage2", 227 }}, 228 SoftwareComponents: []abaputils.SoftwareComponents{{ 229 Name: "testComponent1", 230 }, { 231 Name: "testComponent2", 232 }}, 233 }, 234 } 235 236 objectSetString := abaputils.BuildOSLString(config.ObjectSet) 237 optionsString := buildAUnitOptionsString(config) 238 239 assert.Equal(t, expectedoptionsString, optionsString) 240 assert.Equal(t, expectedobjectSetString, objectSetString) 241 }) 242 243 t.Run("Test AUnit test run body with example yaml config: no Options", func(t *testing.T) { 244 t.Parallel() 245 246 expectedoptionsString := `<aunit:options><aunit:measurements type="none"/><aunit:scope ownTests="true" foreignTests="true"/><aunit:riskLevel harmless="true" dangerous="true" critical="true"/><aunit:duration short="true" medium="true" long="true"/></aunit:options>` 247 config := AUnitConfig{ 248 Title: "Test", Context: "Test", 249 ObjectSet: abaputils.ObjectSet{ 250 PackageNames: []abaputils.Package{{ 251 Name: "testPackage1", 252 }}, 253 SoftwareComponents: []abaputils.SoftwareComponents{{ 254 Name: "testComponent1", 255 }}, 256 }, 257 } 258 259 optionsString := buildAUnitOptionsString(config) 260 assert.Equal(t, expectedoptionsString, optionsString) 261 }) 262 263 t.Run("Config with repository-yml", func(t *testing.T) { 264 config := abapEnvironmentRunAUnitTestOptions{ 265 AUnitResultsFileName: "aUnitResults.xml", 266 Repositories: "repositories.yml", 267 } 268 269 dir := t.TempDir() 270 oldCWD, _ := os.Getwd() 271 _ = os.Chdir(dir) 272 // clean up tmp dir 273 defer func() { 274 _ = os.Chdir(oldCWD) 275 }() 276 277 repositories := `repositories: 278 - name: /DMO/REPO 279 branch: main 280 ` 281 expectedBodyString := "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aunit:run title=\"AUnit Test Run\" context=\"ABAP Environment Pipeline\" xmlns:aunit=\"http://www.sap.com/adt/api/aunit\"><aunit:options><aunit:measurements type=\"none\"/><aunit:scope ownTests=\"true\" foreignTests=\"true\"/><aunit:riskLevel harmless=\"true\" dangerous=\"true\" critical=\"true\"/><aunit:duration short=\"true\" medium=\"true\" long=\"true\"/></aunit:options><osl:objectSet xsi:type=\"multiPropertySet\" xmlns:osl=\"http://www.sap.com/api/osl\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><osl:softwareComponent name=\"/DMO/REPO\"/></osl:objectSet></aunit:run>" 282 err := os.WriteFile(config.Repositories, []byte(repositories), 0o644) 283 if assert.Equal(t, err, nil) { 284 bodyString, err := buildAUnitRequestBody(config) 285 assert.Equal(t, nil, err) 286 assert.Equal(t, expectedBodyString, bodyString) 287 } 288 }) 289 290 t.Run("Config with aunitconfig-yml", func(t *testing.T) { 291 config := abapEnvironmentRunAUnitTestOptions{ 292 AUnitResultsFileName: "aUnitResults.xml", 293 AUnitConfig: "aunit.yml", 294 } 295 296 dir := t.TempDir() 297 oldCWD, _ := os.Getwd() 298 _ = os.Chdir(dir) 299 // clean up tmp dir 300 defer func() { 301 _ = os.Chdir(oldCWD) 302 }() 303 304 yamlBody := `title: My AUnit run 305 objectset: 306 packages: 307 - name: Z_TEST 308 softwarecomponents: 309 - name: Z_TEST 310 - name: /DMO/SWC 311 ` 312 expectedBodyString := "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aunit:run title=\"My AUnit run\" context=\"ABAP Environment Pipeline\" xmlns:aunit=\"http://www.sap.com/adt/api/aunit\"><aunit:options><aunit:measurements type=\"none\"/><aunit:scope ownTests=\"true\" foreignTests=\"true\"/><aunit:riskLevel harmless=\"true\" dangerous=\"true\" critical=\"true\"/><aunit:duration short=\"true\" medium=\"true\" long=\"true\"/></aunit:options><osl:objectSet xsi:type=\"multiPropertySet\" xmlns:osl=\"http://www.sap.com/api/osl\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><osl:package name=\"Z_TEST\"/><osl:softwareComponent name=\"Z_TEST\"/><osl:softwareComponent name=\"/DMO/SWC\"/></osl:objectSet></aunit:run>" 313 err := os.WriteFile(config.AUnitConfig, []byte(yamlBody), 0o644) 314 if assert.Equal(t, err, nil) { 315 bodyString, err := buildAUnitRequestBody(config) 316 assert.Equal(t, nil, err) 317 assert.Equal(t, expectedBodyString, bodyString) 318 } 319 }) 320 321 t.Run("Config with aunitconfig-yml mps", func(t *testing.T) { 322 config := abapEnvironmentRunAUnitTestOptions{ 323 AUnitResultsFileName: "aUnitResults.xml", 324 AUnitConfig: "aunit.yml", 325 } 326 327 dir := t.TempDir() 328 oldCWD, _ := os.Getwd() 329 _ = os.Chdir(dir) 330 // clean up tmp dir 331 defer func() { 332 _ = os.Chdir(oldCWD) 333 }() 334 335 yamlBody := `title: My AUnit run 336 objectset: 337 type: multiPropertySet 338 multipropertyset: 339 packages: 340 - name: Z_TEST 341 softwarecomponents: 342 - name: Z_TEST 343 - name: /DMO/SWC 344 ` 345 expectedBodyString := "<?xml version=\"1.0\" encoding=\"UTF-8\"?><aunit:run title=\"My AUnit run\" context=\"ABAP Environment Pipeline\" xmlns:aunit=\"http://www.sap.com/adt/api/aunit\"><aunit:options><aunit:measurements type=\"none\"/><aunit:scope ownTests=\"true\" foreignTests=\"true\"/><aunit:riskLevel harmless=\"true\" dangerous=\"true\" critical=\"true\"/><aunit:duration short=\"true\" medium=\"true\" long=\"true\"/></aunit:options><osl:objectSet xsi:type=\"multiPropertySet\" xmlns:osl=\"http://www.sap.com/api/osl\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"><osl:package name=\"Z_TEST\"/><osl:softwareComponent name=\"Z_TEST\"/><osl:softwareComponent name=\"/DMO/SWC\"/></osl:objectSet></aunit:run>" 346 err := os.WriteFile(config.AUnitConfig, []byte(yamlBody), 0o644) 347 if assert.Equal(t, err, nil) { 348 bodyString, err := buildAUnitRequestBody(config) 349 assert.Equal(t, nil, err) 350 assert.Equal(t, expectedBodyString, bodyString) 351 } 352 }) 353 354 t.Run("No AUnit config file - expect no panic", func(t *testing.T) { 355 config := abapEnvironmentRunAUnitTestOptions{ 356 AUnitConfig: "aunit.yml", 357 } 358 359 _, err := buildAUnitRequestBody(config) 360 assert.Equal(t, "Could not find aunit.yml", err.Error()) 361 }) 362 363 t.Run("No Repo config file - expect no panic", func(t *testing.T) { 364 config := abapEnvironmentRunAUnitTestOptions{ 365 Repositories: "repo.yml", 366 } 367 368 _, err := buildAUnitRequestBody(config) 369 assert.Equal(t, "Could not find repo.yml", err.Error()) 370 }) 371 } 372 373 func TestTriggerAUnitrun(t *testing.T) { 374 t.Run("succes case: test parsing example yaml config", func(t *testing.T) { 375 config := abapEnvironmentRunAUnitTestOptions{ 376 AUnitConfig: "aUnitConfig.yml", 377 AUnitResultsFileName: "aUnitResults.xml", 378 } 379 380 client := &abaputils.ClientMock{ 381 Body: `AUnit test result body`, 382 StatusCode: 200, 383 } 384 385 con := abaputils.ConnectionDetailsHTTP{ 386 User: "Test", 387 Password: "Test", 388 URL: "https://api.endpoint.com/Entity/", 389 } 390 391 dir := t.TempDir() 392 oldCWD, _ := os.Getwd() 393 _ = os.Chdir(dir) 394 // clean up tmp dir 395 defer func() { 396 _ = os.Chdir(oldCWD) 397 }() 398 399 yamlBody := `title: My AUnit run 400 context: AIE integration tests 401 options: 402 measurements: none 403 scope: 404 owntests: true 405 foreigntests: true 406 riskLevel: 407 harmless: true 408 dangerous: true 409 critical: true 410 duration: 411 short: true 412 medium: true 413 long: true 414 objectset: 415 packages: 416 - name: Z_TEST 417 softwarecomponents: 418 - name: Z_TEST 419 ` 420 421 err := os.WriteFile(config.AUnitConfig, []byte(yamlBody), 0o644) 422 if assert.Equal(t, err, nil) { 423 _, err := triggerAUnitrun(config, con, client) 424 assert.Equal(t, nil, err) 425 } 426 }) 427 428 t.Run("succes case: test parsing example yaml config", func(t *testing.T) { 429 config := abapEnvironmentRunAUnitTestOptions{ 430 AUnitConfig: "aUnitConfig.yml", 431 AUnitResultsFileName: "aUnitResults.xml", 432 } 433 434 client := &abaputils.ClientMock{ 435 Body: `AUnit test result body`, 436 } 437 438 con := abaputils.ConnectionDetailsHTTP{ 439 User: "Test", 440 Password: "Test", 441 URL: "https://api.endpoint.com/Entity/", 442 } 443 444 dir := t.TempDir() 445 oldCWD, _ := os.Getwd() 446 _ = os.Chdir(dir) 447 // clean up tmp dir 448 defer func() { 449 _ = os.Chdir(oldCWD) 450 }() 451 452 yamlBody := `title: My AUnit run 453 context: AIE integration tests 454 options: 455 measurements: none 456 scope: 457 owntests: true 458 foreigntests: true 459 riskLevel: 460 harmless: true 461 dangerous: true 462 critical: true 463 duration: 464 short: true 465 medium: true 466 long: true 467 objectset: 468 type: unionSet 469 set: 470 - type: componentSet 471 component: 472 - name: Z_TEST_SC 473 ` 474 475 err := os.WriteFile(config.AUnitConfig, []byte(yamlBody), 0o644) 476 if assert.Equal(t, err, nil) { 477 _, err := triggerAUnitrun(config, con, client) 478 assert.Equal(t, nil, err) 479 } 480 }) 481 } 482 483 func TestParseAUnitResult(t *testing.T) { 484 t.Parallel() 485 486 t.Run("succes case: test parsing example XML result", func(t *testing.T) { 487 bodyString := `<?xml version="1.0" encoding="utf-8"?><testsuites title="My AUnit run" system="TST" client="100" executedBy="TESTUSER" time="000.000" timestamp="2021-01-01T00:00:00Z" failures="2" errors="2" skipped="0" asserts="0" tests="2"><testsuite name="" tests="2" failures="2" errors="0" skipped="0" asserts="0" package="testpackage" timestamp="2021-01-01T00:00:00ZZ" time="0.000" hostname="test"><testcase classname="test" name="execute" time="0.000" asserts="2"><failure message="testMessage1" type="Assert Failure">Test1</failure><failure message="testMessage2" type="Assert Failure">Test2</failure></testcase></testsuite></testsuites>` 488 body := []byte(bodyString) 489 err := persistAUnitResult(&mock.FilesMock{}, body, "AUnitResults.xml", false) 490 assert.Equal(t, nil, err) 491 }) 492 493 t.Run("succes case: test parsing empty AUnit run XML result", func(t *testing.T) { 494 bodyString := `<?xml version="1.0" encoding="UTF-8"?>` 495 body := []byte(bodyString) 496 err := persistAUnitResult(&mock.FilesMock{}, body, "AUnitResults.xml", false) 497 assert.Equal(t, nil, err) 498 }) 499 500 t.Run("failure case: parsing empty xml", func(t *testing.T) { 501 var bodyString string 502 body := []byte(bodyString) 503 err := persistAUnitResult(&mock.FilesMock{}, body, "AUnitResults.xml", false) 504 assert.EqualError(t, err, "Parsing AUnit result failed: Body is empty, can't parse empty body") 505 }) 506 } 507 508 func TestGetResultAUnitRun(t *testing.T) { 509 t.Parallel() 510 511 t.Run("Get HTTP Response from AUnit test run Test", func(t *testing.T) { 512 t.Parallel() 513 514 client := &abaputils.ClientMock{ 515 Body: `AUnit test result body`, 516 } 517 518 con := abaputils.ConnectionDetailsHTTP{ 519 User: "Test", 520 Password: "Test", 521 URL: "https://api.endpoint.com/Entity/", 522 } 523 resp, err := getAUnitResults("GET", con, []byte(client.Body), client) 524 assert.NoError(t, err) 525 defer resp.Body.Close() 526 if assert.Equal(t, nil, err) { 527 buf := new(bytes.Buffer) 528 _, err = buf.ReadFrom(resp.Body) 529 assert.NoError(t, err) 530 newStr := buf.String() 531 assert.Equal(t, "AUnit test result body", newStr) 532 assert.Equal(t, int64(0), resp.ContentLength) 533 assert.Equal(t, []string([]string(nil)), resp.Header["X-Crsf-Token"]) 534 } 535 }) 536 537 t.Run("Get HTTP Response from AUnit test run Test Failure", func(t *testing.T) { 538 t.Parallel() 539 540 client := &abaputils.ClientMock{ 541 Body: `AUnit test result body`, 542 BodyList: []string{}, 543 StatusCode: 400, 544 Error: fmt.Errorf("%w", errors.New("Test fail")), 545 } 546 547 con := abaputils.ConnectionDetailsHTTP{ 548 User: "Test", 549 Password: "Test", 550 URL: "https://api.endpoint.com/Entity/", 551 } 552 resp, err := getAUnitResults("GET", con, []byte(client.Body), client) 553 assert.EqualError(t, err, "Getting AUnit run results failed: Test fail") 554 defer resp.Body.Close() 555 556 buf := new(bytes.Buffer) 557 _, err = buf.ReadFrom(resp.Body) 558 assert.NoError(t, err) 559 newStr := buf.String() 560 assert.Equal(t, "AUnit test result body", newStr) 561 assert.Equal(t, int64(0), resp.ContentLength) 562 assert.Equal(t, 400, resp.StatusCode) 563 assert.Equal(t, []string([]string(nil)), resp.Header["X-Crsf-Token"]) 564 }) 565 } 566 567 func TestRunAbapEnvironmentRunAUnitTest(t *testing.T) { 568 t.Parallel() 569 570 t.Run("FetchXcsrfToken Test", func(t *testing.T) { 571 t.Parallel() 572 573 tokenExpected := "myToken" 574 575 client := &abaputils.ClientMock{ 576 Body: `Xcsrf Token test`, 577 Token: tokenExpected, 578 } 579 580 con := abaputils.ConnectionDetailsHTTP{ 581 User: "Test", 582 Password: "Test", 583 URL: "https://api.endpoint.com/Entity/", 584 } 585 token, error := fetchAUnitXcsrfToken("GET", con, []byte(client.Body), client) 586 if assert.Equal(t, nil, error) { 587 assert.Equal(t, tokenExpected, token) 588 } 589 }) 590 591 t.Run("failure case: fetch token", func(t *testing.T) { 592 t.Parallel() 593 594 tokenExpected := "" 595 596 client := &abaputils.ClientMock{ 597 Body: `Xcsrf Token test`, 598 Token: "", 599 } 600 601 con := abaputils.ConnectionDetailsHTTP{ 602 User: "Test", 603 Password: "Test", 604 URL: "https://api.endpoint.com/Entity/", 605 } 606 token, error := fetchAUnitXcsrfToken("GET", con, []byte(client.Body), client) 607 if assert.Equal(t, nil, error) { 608 assert.Equal(t, tokenExpected, token) 609 } 610 }) 611 612 t.Run("AUnit test run Poll Test", func(t *testing.T) { 613 t.Parallel() 614 615 tokenExpected := "myToken" 616 617 client := &abaputils.ClientMock{ 618 Body: `<?xml version="1.0" encoding="utf-8"?><aunit:run xmlns:aunit="http://www.sap.com/adt/api/aunit"><aunit:progress status="FINISHED"/><aunit:time/><atom:link href="/sap/bc/adt/api/abapunit/results/test" rel="http://www.sap.com/adt/relations/api/abapunit/run-result" type="application/vnd.sap.adt.api.junit.run-result.v1xml" title="JUnit Run Result" xmlns:atom="http://www.w3.org/2005/Atom"/></aunit:run>`, 619 Token: tokenExpected, 620 } 621 622 con := abaputils.ConnectionDetailsHTTP{ 623 User: "Test", 624 Password: "Test", 625 URL: "https://api.endpoint.com/Entity/", 626 } 627 resp, err := pollAUnitRun(con, []byte(client.Body), client) 628 if assert.Equal(t, nil, err) { 629 assert.Equal(t, "/sap/bc/adt/api/abapunit/results/test", resp) 630 } 631 }) 632 633 t.Run("AUnit test run Poll Test Fail", func(t *testing.T) { 634 t.Parallel() 635 636 tokenExpected := "myToken" 637 638 client := &abaputils.ClientMock{ 639 Body: `<?xml version="1.0" encoding="utf-8"?><aunit:run xmlns:aunit="http://www.sap.com/adt/api/aunit"><aunit:progress status="Not Created"/><aunit:time/><atom:link href="/sap/bc/adt/api/abapunit/results/test" rel="http://www.sap.com/adt/relations/api/abapunit/run-result" type="application/vnd.sap.adt.api.junit.run-result.v1xml" title="JUnit Run Result" xmlns:atom="http://www.w3.org/2005/Atom"/></aunit:run>`, 640 Token: tokenExpected, 641 } 642 643 con := abaputils.ConnectionDetailsHTTP{ 644 User: "Test", 645 Password: "Test", 646 URL: "https://api.endpoint.com/Entity/", 647 } 648 resp, err := pollAUnitRun(con, []byte(client.Body), client) 649 if assert.Equal(t, nil, err) { 650 assert.Equal(t, "", resp) 651 } 652 }) 653 654 t.Run("Get HTTP Response from AUnit test run Test", func(t *testing.T) { 655 t.Parallel() 656 657 client := &abaputils.ClientMock{ 658 Body: `HTTP response test`, 659 } 660 661 con := abaputils.ConnectionDetailsHTTP{ 662 User: "Test", 663 Password: "Test", 664 URL: "https://api.endpoint.com/Entity/", 665 } 666 fmt.Println("Body:" + string([]byte(client.Body))) 667 resp, err := getHTTPResponseAUnitRun("GET", con, []byte(client.Body), client) 668 assert.NoError(t, err) 669 defer resp.Body.Close() 670 if assert.Equal(t, nil, err) { 671 buf := new(bytes.Buffer) 672 _, err = buf.ReadFrom(resp.Body) 673 assert.NoError(t, err) 674 newStr := buf.String() 675 assert.Equal(t, "HTTP response test", newStr) 676 assert.Equal(t, int64(0), resp.ContentLength) 677 assert.Equal(t, []string([]string(nil)), resp.Header["X-Crsf-Token"]) 678 } 679 }) 680 } 681 682 func TestGenerateHTMLDocumentAUnit(t *testing.T) { 683 t.Run("Test empty XML Result", func(t *testing.T) { 684 expectedString := `<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><title>AUnit Results</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><style>table,th,td {border-collapse:collapse;}th,td{padding: 5px;text-align:left;font-size:medium;}</style></head><body><h1 style="text-align:left;font-size:large">AUnit Results</h1><table><tr><th>Run title</th><td style="padding-right: 20px"></td><th>System</th><td style="padding-right: 20px"></td><th>Client</th><td style="padding-right: 20px"></td><th>ExecutedBy</th><td style="padding-right: 20px"></td><th>Duration</th><td style="padding-right: 20px">s</td><th>Timestamp</th><td style="padding-right: 20px"></td></tr><tr><th>Failures</th><td style="padding-right: 20px"></td><th>Errors</th><td style="padding-right: 20px"></td><th>Skipped</th><td style="padding-right: 20px"></td><th>Asserts</th><td style="padding-right: 20px"></td><th>Tests</th><td style="padding-right: 20px"></td></tr></table><br><table style="width:100%; border: 1px solid black""><tr style="border: 1px solid black"><th style="border: 1px solid black">Severity</th><th style="border: 1px solid black">File</th><th style="border: 1px solid black">Message</th><th style="border: 1px solid black">Type</th><th style="border: 1px solid black">Text</th></tr><tr><td colspan="5"><b>There are no AUnit findings to be displayed</b></td></tr></table></body></html>` 685 686 result := AUnitResult{} 687 688 resultString := generateHTMLDocumentAUnit(&result) 689 690 assert.Equal(t, expectedString, resultString) 691 }) 692 693 t.Run("Test AUnit XML Result", func(t *testing.T) { 694 expectedString := `<!DOCTYPE html><html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><title>AUnit Results</title><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><style>table,th,td {border-collapse:collapse;}th,td{padding: 5px;text-align:left;font-size:medium;}</style></head><body><h1 style="text-align:left;font-size:large">AUnit Results</h1><table><tr><th>Run title</th><td style="padding-right: 20px">Test title</td><th>System</th><td style="padding-right: 20px">Test system</td><th>Client</th><td style="padding-right: 20px">000</td><th>ExecutedBy</th><td style="padding-right: 20px">CC00000</td><th>Duration</th><td style="padding-right: 20px">0.15s</td><th>Timestamp</th><td style="padding-right: 20px">2021-00-00T00:00:00Z</td></tr><tr><th>Failures</th><td style="padding-right: 20px">4</td><th>Errors</th><td style="padding-right: 20px">4</td><th>Skipped</th><td style="padding-right: 20px">4</td><th>Asserts</th><td style="padding-right: 20px">12</td><th>Tests</th><td style="padding-right: 20px">12</td></tr></table><br><table style="width:100%; border: 1px solid black""><tr style="border: 1px solid black"><th style="border: 1px solid black">Severity</th><th style="border: 1px solid black">File</th><th style="border: 1px solid black">Message</th><th style="border: 1px solid black">Type</th><th style="border: 1px solid black">Text</th></tr><tr style="background-color: grey"><td colspan="5"><b>Testcase: my_test for class ZCL_my_test</b></td></tr><tr style="background-color: rgba(227,85,0)"><td style="border: 1px solid black">Failure</td><td style="border: 1px solid black">ZCL_my_test</td><td style="border: 1px solid black">testMessage</td><td style="border: 1px solid black">Assert Error</td><td style="border: 1px solid black">testError</td></tr><tr style="background-color: rgba(227,85,0)"><td style="border: 1px solid black">Failure</td><td style="border: 1px solid black">ZCL_my_test</td><td style="border: 1px solid black">testMessage2</td><td style="border: 1px solid black">Assert Error2</td><td style="border: 1px solid black">testError2</td></tr><tr style="background-color: rgba(227,85,0)"><td style="border: 1px solid black">Failure</td><td style="border: 1px solid black">ZCL_my_test</td><td style="border: 1px solid black">testMessage</td><td style="border: 1px solid black">Assert Failure</td><td style="border: 1px solid black">testFailure</td></tr><tr style="background-color: rgba(227,85,0)"><td style="border: 1px solid black">Failure</td><td style="border: 1px solid black">ZCL_my_test</td><td style="border: 1px solid black">testMessage2</td><td style="border: 1px solid black">Assert Failure2</td><td style="border: 1px solid black">testFailure2</td></tr><tr style="background-color: rgba(255,175,0, 0.2)"><td style="border: 1px solid black">Failure</td><td style="border: 1px solid black">ZCL_my_test</td><td style="border: 1px solid black">testSkipped</td><td style="border: 1px solid black">-</td><td style="border: 1px solid black">testSkipped</td></tr><tr style="background-color: rgba(255,175,0, 0.2)"><td style="border: 1px solid black">Failure</td><td style="border: 1px solid black">ZCL_my_test</td><td style="border: 1px solid black">testSkipped2</td><td style="border: 1px solid black">-</td><td style="border: 1px solid black">testSkipped2</td></tr><tr style="background-color: grey"><td colspan="5"><b>Testcase: my_test2 for class ZCL_my_test2</b></td></tr><tr style="background-color: rgba(227,85,0)"><td style="border: 1px solid black">Failure</td><td style="border: 1px solid black">ZCL_my_test2</td><td style="border: 1px solid black">testMessage3</td><td style="border: 1px solid black">Assert Error3</td><td style="border: 1px solid black">testError3</td></tr><tr style="background-color: rgba(227,85,0)"><td style="border: 1px solid black">Failure</td><td style="border: 1px solid black">ZCL_my_test2</td><td style="border: 1px solid black">testMessage4</td><td style="border: 1px solid black">Assert Error4</td><td style="border: 1px solid black">testError4</td></tr><tr style="background-color: rgba(227,85,0)"><td style="border: 1px solid black">Failure</td><td style="border: 1px solid black">ZCL_my_test2</td><td style="border: 1px solid black">testMessage5</td><td style="border: 1px solid black">Assert Failure5</td><td style="border: 1px solid black">testFailure5</td></tr><tr style="background-color: rgba(227,85,0)"><td style="border: 1px solid black">Failure</td><td style="border: 1px solid black">ZCL_my_test2</td><td style="border: 1px solid black">testMessage6</td><td style="border: 1px solid black">Assert Failure6</td><td style="border: 1px solid black">testFailure6</td></tr><tr style="background-color: rgba(255,175,0, 0.2)"><td style="border: 1px solid black">Failure</td><td style="border: 1px solid black">ZCL_my_test2</td><td style="border: 1px solid black">testSkipped7</td><td style="border: 1px solid black">-</td><td style="border: 1px solid black">testSkipped7</td></tr><tr style="background-color: rgba(255,175,0, 0.2)"><td style="border: 1px solid black">Failure</td><td style="border: 1px solid black">ZCL_my_test2</td><td style="border: 1px solid black">testSkipped8</td><td style="border: 1px solid black">-</td><td style="border: 1px solid black">testSkipped8</td></tr></table></body></html>` 695 696 result := AUnitResult{ 697 XMLName: xml.Name{Space: "testSpace", Local: "testLocal"}, 698 Title: "Test title", 699 System: "Test system", 700 Client: "000", 701 ExecutedBy: "CC00000", 702 Time: "0.15", 703 Timestamp: "2021-00-00T00:00:00Z", 704 Failures: "4", 705 Errors: "4", 706 Skipped: "4", 707 Asserts: "12", 708 Tests: "12", 709 Testsuite: struct { 710 Tests string `xml:"tests,attr"` 711 Asserts string `xml:"asserts,attr"` 712 Skipped string `xml:"skipped,attr"` 713 Errors string `xml:"errors,attr"` 714 Failures string `xml:"failures,attr"` 715 Timestamp string `xml:"timestamp,attr"` 716 Time string `xml:"time,attr"` 717 Hostname string `xml:"hostname,attr"` 718 Package string `xml:"package,attr"` 719 Name string `xml:"name,attr"` 720 Testcase []struct { 721 Asserts string `xml:"asserts,attr"` 722 Time string `xml:"time,attr"` 723 Name string `xml:"name,attr"` 724 Classname string `xml:"classname,attr"` 725 Error []struct { 726 Text string `xml:",chardata"` 727 Type string `xml:"type,attr"` 728 Message string `xml:"message,attr"` 729 } `xml:"error"` 730 Failure []struct { 731 Text string `xml:",chardata"` 732 Type string `xml:"type,attr"` 733 Message string `xml:"message,attr"` 734 } `xml:"failure"` 735 Skipped []struct { 736 Text string `xml:",chardata"` 737 Message string `xml:"message,attr"` 738 } `xml:"skipped"` 739 } `xml:"testcase"` 740 }{ 741 Tests: "6", 742 Asserts: "4", 743 Skipped: "2", 744 Errors: "2", 745 Failures: "2", 746 Timestamp: "2021-00-00T00:00:00Z", 747 Time: "0.15", 748 Hostname: "0xb", 749 Package: "testPackage", 750 Name: "ZCL_testPackage", 751 Testcase: []struct { 752 Asserts string "xml:\"asserts,attr\"" 753 Time string "xml:\"time,attr\"" 754 Name string "xml:\"name,attr\"" 755 Classname string "xml:\"classname,attr\"" 756 Error []struct { 757 Text string "xml:\",chardata\"" 758 Type string "xml:\"type,attr\"" 759 Message string "xml:\"message,attr\"" 760 } "xml:\"error\"" 761 Failure []struct { 762 Text string "xml:\",chardata\"" 763 Type string "xml:\"type,attr\"" 764 Message string "xml:\"message,attr\"" 765 } "xml:\"failure\"" 766 Skipped []struct { 767 Text string "xml:\",chardata\"" 768 Message string "xml:\"message,attr\"" 769 } "xml:\"skipped\"" 770 }{{ 771 Asserts: "4", 772 Time: "0.15", 773 Name: "my_test", 774 Classname: "ZCL_my_test", 775 Error: []struct { 776 Text string "xml:\",chardata\"" 777 Type string "xml:\"type,attr\"" 778 Message string "xml:\"message,attr\"" 779 }{{ 780 Text: "testError", 781 Type: "Assert Error", 782 Message: "testMessage", 783 }, { 784 Text: "testError2", 785 Type: "Assert Error2", 786 Message: "testMessage2", 787 }}, 788 Failure: []struct { 789 Text string "xml:\",chardata\"" 790 Type string "xml:\"type,attr\"" 791 Message string "xml:\"message,attr\"" 792 }{{ 793 Text: "testFailure", 794 Type: "Assert Failure", 795 Message: "testMessage", 796 }, { 797 Text: "testFailure2", 798 Type: "Assert Failure2", 799 Message: "testMessage2", 800 }}, 801 Skipped: []struct { 802 Text string "xml:\",chardata\"" 803 Message string "xml:\"message,attr\"" 804 }{{ 805 Text: "testSkipped", 806 Message: "testSkipped", 807 }, { 808 Text: "testSkipped2", 809 Message: "testSkipped2", 810 }}, 811 }, { 812 Asserts: "4", 813 Time: "0.15", 814 Name: "my_test2", 815 Classname: "ZCL_my_test2", 816 Error: []struct { 817 Text string "xml:\",chardata\"" 818 Type string "xml:\"type,attr\"" 819 Message string "xml:\"message,attr\"" 820 }{{ 821 Text: "testError3", 822 Type: "Assert Error3", 823 Message: "testMessage3", 824 }, { 825 Text: "testError4", 826 Type: "Assert Error4", 827 Message: "testMessage4", 828 }}, 829 Failure: []struct { 830 Text string "xml:\",chardata\"" 831 Type string "xml:\"type,attr\"" 832 Message string "xml:\"message,attr\"" 833 }{{ 834 Text: "testFailure5", 835 Type: "Assert Failure5", 836 Message: "testMessage5", 837 }, { 838 Text: "testFailure6", 839 Type: "Assert Failure6", 840 Message: "testMessage6", 841 }}, 842 Skipped: []struct { 843 Text string "xml:\",chardata\"" 844 Message string "xml:\"message,attr\"" 845 }{{ 846 Text: "testSkipped7", 847 Message: "testSkipped7", 848 }, { 849 Text: "testSkipped8", 850 Message: "testSkipped8", 851 }}, 852 }}, 853 }, 854 } 855 856 resultString := generateHTMLDocumentAUnit(&result) 857 fmt.Println(resultString) 858 assert.Equal(t, expectedString, resultString) 859 }) 860 }