package cmd import ( "testing" "github.com/SAP/jenkins-library/pkg/abaputils" "github.com/pkg/errors" "github.com/stretchr/testify/assert" ) func TestCheckoutBranchStep(t *testing.T) { t.Run("Run Step Successful", func(t *testing.T) { var autils = abaputils.AUtilsMock{} defer autils.Cleanup() autils.ReturnedConnectionDetailsHTTP.Password = "password" autils.ReturnedConnectionDetailsHTTP.User = "user" autils.ReturnedConnectionDetailsHTTP.URL = "https://example.com" autils.ReturnedConnectionDetailsHTTP.XCsrfToken = "xcsrftoken" config := abapEnvironmentCheckoutBranchOptions{ CfAPIEndpoint: "https://api.endpoint.com", CfOrg: "testOrg", CfSpace: "testSpace", CfServiceInstance: "testInstance", CfServiceKeyName: "testServiceKey", Username: "testUser", Password: "testPassword", RepositoryName: "testRepo1", BranchName: "testBranch", } client := &abaputils.ClientMock{ BodyList: []string{ `{"d" : { "status" : "S" } }`, `{"d" : { "status" : "S" } }`, `{"d" : { "status" : "S" } }`, }, Token: "myToken", StatusCode: 200, } err := runAbapEnvironmentCheckoutBranch(&config, nil, &autils, client) assert.NoError(t, err, "Did not expect error") }) } func TestTriggerCheckout(t *testing.T) { t.Run("Test trigger checkout: success case", func(t *testing.T) { // given receivedURI := "example.com/Branches" uriExpected := receivedURI + "?$expand=to_Execution_log,to_Transport_log" tokenExpected := "myToken" client := &abaputils.ClientMock{ Body: `{"d" : { "__metadata" : { "uri" : "` + receivedURI + `" } } }`, Token: tokenExpected, StatusCode: 200, } config := abapEnvironmentCheckoutBranchOptions{ CfAPIEndpoint: "https://api.endpoint.com", CfOrg: "testOrg", CfSpace: "testSpace", CfServiceInstance: "testInstance", CfServiceKeyName: "testServiceKey", Username: "testUser", Password: "testPassword", RepositoryName: "testRepo1", BranchName: "feature-unit-test", } con := abaputils.ConnectionDetailsHTTP{ User: "MY_USER", Password: "MY_PW", URL: "https://api.endpoint.com/Branches", } // when entityConnection, err := triggerCheckout(config.RepositoryName, config.BranchName, con, client) // then assert.NoError(t, err) assert.Equal(t, uriExpected, entityConnection.URL) assert.Equal(t, tokenExpected, entityConnection.XCsrfToken) }) t.Run("Test trigger checkout: ABAP Error case", func(t *testing.T) { // given errorMessage := "ABAP Error Message" errorCode := "ERROR/001" HTTPErrorMessage := "HTTP Error Message" combinedErrorMessage := "HTTP Error Message: ERROR/001 - ABAP Error Message" client := &abaputils.ClientMock{ Body: `{"error" : { "code" : "` + errorCode + `", "message" : { "lang" : "en", "value" : "` + errorMessage + `" } } }`, Token: "myToken", StatusCode: 400, Error: errors.New(HTTPErrorMessage), } config := abapEnvironmentCheckoutBranchOptions{ CfAPIEndpoint: "https://api.endpoint.com", CfOrg: "testOrg", CfSpace: "testSpace", CfServiceInstance: "testInstance", CfServiceKeyName: "testServiceKey", Username: "testUser", Password: "testPassword", RepositoryName: "testRepo1", BranchName: "feature-unit-test", } con := abaputils.ConnectionDetailsHTTP{ User: "MY_USER", Password: "MY_PW", URL: "https://api.endpoint.com/Branches", } // when _, err := triggerCheckout(config.RepositoryName, config.BranchName, con, client) // then assert.Equal(t, combinedErrorMessage, err.Error(), "Different error message expected") }) }