github.com/jaylevin/jenkins-library@v1.230.4/cmd/abapEnvironmentRunAUnitTest_test.go (about)

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