package cmd
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestDiscoverySuccess(t *testing.T) {
config := gctsExecuteABAPUnitTestsOptions{
Host: "http://testHost.com:50000",
Client: "000",
Repository: "testRepo",
Username: "testUser",
Password: "testPassword",
}
t.Run("discovery successful", func(t *testing.T) {
httpClient := httpMockGcts{
StatusCode: 200,
Header: map[string][]string{"x-csrf-token": {"ZegUEgfa50R7ZfGGxOtx2A=="}},
ResponseBody: `
`}
header, err := discoverServer(&config, &httpClient)
if assert.NoError(t, err) {
t.Run("check url", func(t *testing.T) {
assert.Equal(t, "http://testHost.com:50000/sap/bc/adt/core/discovery?sap-client=000", httpClient.URL)
})
t.Run("check method", func(t *testing.T) {
assert.Equal(t, "GET", httpClient.Method)
})
t.Run("check header", func(t *testing.T) {
assert.Equal(t, http.Header{"x-csrf-token": []string{"ZegUEgfa50R7ZfGGxOtx2A=="}}, *header)
})
}
})
}
func TestDiscoveryFailure(t *testing.T) {
config := gctsExecuteABAPUnitTestsOptions{
Host: "http://testHost.com:50000",
Client: "000",
Repository: "testRepo",
Username: "testUser",
Password: "testPassword",
}
t.Run("a http error occurred", func(t *testing.T) {
httpClient := httpMockGcts{StatusCode: 403, ResponseBody: `
Service cannot be reached
`}
header, err := discoverServer(&config, &httpClient)
t.Run("check error", func(t *testing.T) {
assert.EqualError(t, err, "discovery of the ABAP server failed: a http error occurred")
})
t.Run("check header", func(t *testing.T) {
assert.Equal(t, (*http.Header)(nil), header)
})
})
t.Run("discovery response is nil", func(t *testing.T) {
httpClient := httpMockGcts{StatusCode: 200, ResponseBody: ``}
header, err := discoverServer(&config, &httpClient)
t.Run("check error", func(t *testing.T) {
assert.EqualError(t, err, "discovery of the ABAP server failed: did not retrieve a HTTP response")
})
t.Run("check header", func(t *testing.T) {
assert.Equal(t, (*http.Header)(nil), header)
})
})
t.Run("discovery header is nil", func(t *testing.T) {
httpClient := httpMockGcts{
StatusCode: 200,
Header: nil,
ResponseBody: `
`}
header, err := discoverServer(&config, &httpClient)
t.Run("check error", func(t *testing.T) {
assert.EqualError(t, err, "discovery of the ABAP server failed: did not retrieve a HTTP response")
})
t.Run("check header", func(t *testing.T) {
assert.Equal(t, (*http.Header)(nil), header)
})
})
}
func TestGetPackageListSuccess(t *testing.T) {
config := gctsExecuteABAPUnitTestsOptions{
Host: "http://testHost.com:50000",
Client: "000",
Repository: "testRepo",
Username: "testUser",
Password: "testPassword",
}
t.Run("return multiple objects successfully", func(t *testing.T) {
httpClient := httpMockGcts{StatusCode: 200, ResponseBody: `
{
"objects": [
{
"pgmid": "R3TR",
"object": "ZCL_GCTS",
"type": "CLAS",
"description": "Class (ABAP Objects)"
},
{
"pgmid": "R3TR",
"object": "ZP_GCTS",
"type": "DEVC",
"description": "Package"
},
{
"pgmid": "R3TR",
"object": "ZP_PIPER",
"type": "DEVC",
"description": "Package"
}
]
}
`}
objects, err := getPackageList(&config, &httpClient)
if assert.NoError(t, err) {
t.Run("check url", func(t *testing.T) {
assert.Equal(t, "http://testHost.com:50000/sap/bc/cts_abapvcs/repository/testRepo/getObjects?sap-client=000", httpClient.URL)
})
t.Run("check method", func(t *testing.T) {
assert.Equal(t, "GET", httpClient.Method)
})
t.Run("check package objects", func(t *testing.T) {
assert.Equal(t, []string{"ZP_GCTS", "ZP_PIPER"}, objects)
})
}
})
t.Run("no objects returned by http call", func(t *testing.T) {
httpClient := httpMockGcts{StatusCode: 200, ResponseBody: `{}`}
objects, err := getPackageList(&config, &httpClient)
if assert.NoError(t, err) {
t.Run("check url", func(t *testing.T) {
assert.Equal(t, "http://testHost.com:50000/sap/bc/cts_abapvcs/repository/testRepo/getObjects?sap-client=000", httpClient.URL)
})
t.Run("check method", func(t *testing.T) {
assert.Equal(t, "GET", httpClient.Method)
})
t.Run("check package objects", func(t *testing.T) {
assert.Equal(t, []string{}, objects)
})
}
})
}
func TestGetPackageListFailure(t *testing.T) {
config := gctsExecuteABAPUnitTestsOptions{
Host: "http://testHost.com:50000",
Client: "000",
Repository: "testRepo",
Username: "testUser",
Password: "testPassword",
}
t.Run("http error occurred", func(t *testing.T) {
httpClient := httpMockGcts{StatusCode: 500, ResponseBody: `
{
"exception": "No relation between system and repository"
}
`}
_, err := getPackageList(&config, &httpClient)
assert.EqualError(t, err, "getting repository object/package list failed: a http error occurred")
})
}
func TestExecuteTestsForPackageSuccess(t *testing.T) {
config := gctsExecuteABAPUnitTestsOptions{
Host: "http://testHost.com:50000",
Client: "000",
Repository: "testRepo",
Username: "testUser",
Password: "testPassword",
}
header := make(http.Header)
header.Add("Accept", "application/atomsvc+xml")
header.Add("x-csrf-token", "ZegUEgfa50R7ZfGGxOtx2A==")
header.Add("saml2", "disabled")
t.Run("all unit tests were successful", func(t *testing.T) {
httpClient := httpMockGcts{StatusCode: 200, ResponseBody: `
`}
err := executeTestsForPackage(&config, &httpClient, header, "ZP_PIPER")
if assert.NoError(t, err) {
t.Run("check url", func(t *testing.T) {
assert.Equal(t, "http://testHost.com:50000/sap/bc/adt/abapunit/testruns?sap-client=000", httpClient.URL)
})
t.Run("check method", func(t *testing.T) {
assert.Equal(t, "POST", httpClient.Method)
})
}
})
t.Run("no unit tests found", func(t *testing.T) {
httpClient := httpMockGcts{StatusCode: 200, ResponseBody: `
The task definition does not refer to any test
`}
err := executeTestsForPackage(&config, &httpClient, header, "ZP_NON_EXISTANT")
if assert.NoError(t, err) {
t.Run("check url", func(t *testing.T) {
assert.Equal(t, "http://testHost.com:50000/sap/bc/adt/abapunit/testruns?sap-client=000", httpClient.URL)
})
t.Run("check method", func(t *testing.T) {
assert.Equal(t, "POST", httpClient.Method)
})
}
})
}
func TestExecuteTestsForPackageFailure(t *testing.T) {
config := gctsExecuteABAPUnitTestsOptions{
Host: "http://testHost.com:50000",
Client: "000",
Repository: "testRepo",
Username: "testUser",
Password: "testPassword",
}
t.Run("some unit tests failed", func(t *testing.T) {
httpClient := httpMockGcts{StatusCode: 200, ResponseBody: `
Critical Assertion Error: 'Check: ASSERT_EQUALS'
`}
header := make(http.Header)
header.Add("Accept", "application/atomsvc+xml")
header.Add("x-csrf-token", "ZegUEgfa50R7ZfGGxOtx2A==")
header.Add("saml2", "disabled")
err := executeTestsForPackage(&config, &httpClient, header, "ZP_PIPER")
assert.EqualError(t, err, "some unit tests failed")
})
t.Run("a http error occurred", func(t *testing.T) {
httpClient := httpMockGcts{StatusCode: 403, ResponseBody: `
CSRF token validation failed
`}
header := make(http.Header)
header.Add("Accept", "application/atomsvc+xml")
header.Add("x-csrf-token", "ZegUEgfa50R7ZfGGxOtx2A==")
header.Add("saml2", "disabled")
err := executeTestsForPackage(&config, &httpClient, header, "ZP_PIPER")
assert.EqualError(t, err, "execution of unit tests failed: a http error occurred")
})
}