2020-02-04 12:43:27 +02:00
package cmd
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"testing"
2020-07-23 10:26:50 +02:00
"github.com/SAP/jenkins-library/pkg/abaputils"
2020-04-08 12:43:41 +02:00
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/pkg/errors"
2020-02-04 12:43:27 +02:00
piperhttp "github.com/SAP/jenkins-library/pkg/http"
"github.com/stretchr/testify/assert"
)
func TestTriggerPull ( t * testing . T ) {
t . Run ( "Test trigger pull: success case" , func ( t * testing . T ) {
2020-04-08 12:43:41 +02:00
receivedURI := "example.com/Entity"
uriExpected := receivedURI + "?$expand=to_Execution_log,to_Transport_log"
2020-02-04 12:43:27 +02:00
tokenExpected := "myToken"
client := & clientMock {
2020-04-08 12:43:41 +02:00
Body : ` { "d" : { "__metadata" : { "uri" : " ` + receivedURI + ` " } } } ` ,
Token : tokenExpected ,
StatusCode : 200 ,
2020-02-04 12:43:27 +02:00
}
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
2020-06-30 15:46:07 +02:00
CfServiceKeyName : "testServiceKey" ,
2020-02-04 12:43:27 +02:00
Username : "testUser" ,
Password : "testPassword" ,
2020-05-07 15:51:11 +02:00
RepositoryNames : [ ] string { "testRepo1" , "testRepo2" } ,
2020-02-04 12:43:27 +02:00
}
2020-07-23 10:26:50 +02:00
con := abaputils . ConnectionDetailsHTTP {
2020-02-04 12:43:27 +02:00
User : "MY_USER" ,
Password : "MY_PW" ,
URL : "https://api.endpoint.com/Entity/" ,
}
2020-05-07 15:51:11 +02:00
entityConnection , err := triggerPull ( config . RepositoryNames [ 0 ] , con , client )
2020-04-08 12:43:41 +02:00
assert . Nil ( t , err )
2020-02-04 12:43:27 +02:00
assert . Equal ( t , uriExpected , entityConnection . URL )
assert . Equal ( t , tokenExpected , entityConnection . XCsrfToken )
} )
2020-04-08 12:43:41 +02:00
t . Run ( "Test trigger pull: ABAP Error" , func ( t * testing . T ) {
errorMessage := "ABAP Error Message"
errorCode := "ERROR/001"
HTTPErrorMessage := "HTTP Error Message"
combinedErrorMessage := "HTTP Error Message: ERROR/001 - ABAP Error Message"
client := & clientMock {
Body : ` { "error" : { "code" : " ` + errorCode + ` ", "message" : { "lang" : "en", "value" : " ` + errorMessage + ` " } } } ` ,
Token : "myToken" ,
StatusCode : 400 ,
Error : errors . New ( HTTPErrorMessage ) ,
}
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
2020-06-30 15:46:07 +02:00
CfServiceKeyName : "testServiceKey" ,
2020-04-08 12:43:41 +02:00
Username : "testUser" ,
Password : "testPassword" ,
2020-05-07 15:51:11 +02:00
RepositoryNames : [ ] string { "testRepo1" , "testRepo2" } ,
2020-04-08 12:43:41 +02:00
}
2020-07-23 10:26:50 +02:00
con := abaputils . ConnectionDetailsHTTP {
2020-04-08 12:43:41 +02:00
User : "MY_USER" ,
Password : "MY_PW" ,
URL : "https://api.endpoint.com/Entity/" ,
}
2020-05-07 15:51:11 +02:00
_ , err := triggerPull ( config . RepositoryNames [ 0 ] , con , client )
2020-04-08 12:43:41 +02:00
assert . Equal ( t , combinedErrorMessage , err . Error ( ) , "Different error message expected" )
} )
2020-02-04 12:43:27 +02:00
}
func TestPollEntity ( t * testing . T ) {
t . Run ( "Test poll entity: success case" , func ( t * testing . T ) {
client := & clientMock {
BodyList : [ ] string {
` { "d" : { "status" : "S" } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
2020-04-08 12:43:41 +02:00
Token : "myToken" ,
StatusCode : 200 ,
2020-02-04 12:43:27 +02:00
}
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
2020-06-30 15:46:07 +02:00
CfServiceKeyName : "testServiceKey" ,
2020-02-04 12:43:27 +02:00
Username : "testUser" ,
Password : "testPassword" ,
2020-05-07 15:51:11 +02:00
RepositoryNames : [ ] string { "testRepo1" , "testRepo2" } ,
2020-02-04 12:43:27 +02:00
}
2020-07-23 10:26:50 +02:00
con := abaputils . ConnectionDetailsHTTP {
2020-02-04 12:43:27 +02:00
User : "MY_USER" ,
Password : "MY_PW" ,
URL : "https://api.endpoint.com/Entity/" ,
XCsrfToken : "MY_TOKEN" ,
}
2020-05-07 15:51:11 +02:00
status , _ := pollEntity ( config . RepositoryNames [ 0 ] , con , client , 0 )
2020-02-04 12:43:27 +02:00
assert . Equal ( t , "S" , status )
} )
t . Run ( "Test poll entity: error case" , func ( t * testing . T ) {
client := & clientMock {
BodyList : [ ] string {
` { "d" : { "status" : "E" } } ` ,
` { "d" : { "status" : "R" } } ` ,
} ,
2020-04-08 12:43:41 +02:00
Token : "myToken" ,
StatusCode : 200 ,
2020-02-04 12:43:27 +02:00
}
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
2020-06-30 15:46:07 +02:00
CfServiceKeyName : "testServiceKey" ,
2020-02-04 12:43:27 +02:00
Username : "testUser" ,
Password : "testPassword" ,
2020-05-07 15:51:11 +02:00
RepositoryNames : [ ] string { "testRepo1" , "testRepo2" } ,
2020-02-04 12:43:27 +02:00
}
2020-07-23 10:26:50 +02:00
con := abaputils . ConnectionDetailsHTTP {
2020-02-04 12:43:27 +02:00
User : "MY_USER" ,
Password : "MY_PW" ,
URL : "https://api.endpoint.com/Entity/" ,
XCsrfToken : "MY_TOKEN" ,
}
2020-05-07 15:51:11 +02:00
status , _ := pollEntity ( config . RepositoryNames [ 0 ] , con , client , 0 )
2020-02-04 12:43:27 +02:00
assert . Equal ( t , "E" , status )
} )
}
func TestGetAbapCommunicationArrangementInfo ( t * testing . T ) {
t . Run ( "Test cf cli command: success case" , func ( t * testing . T ) {
2020-07-23 10:26:50 +02:00
config := abaputils . AbapEnvironmentOptions {
2020-02-04 12:43:27 +02:00
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
2020-06-30 15:46:07 +02:00
CfServiceKeyName : "testServiceKey" ,
2020-02-04 12:43:27 +02:00
Username : "testUser" ,
Password : "testPassword" ,
}
2020-07-23 10:26:50 +02:00
options := abaputils . AbapEnvironmentPullGitRepoOptions {
AbapEnvOptions : config ,
2020-07-07 16:19:57 +02:00
}
2020-07-23 10:26:50 +02:00
execRunner := & mock . ExecMockRunner { }
2020-07-07 16:19:57 +02:00
2020-07-23 10:26:50 +02:00
abaputils . GetAbapCommunicationArrangementInfo ( options . AbapEnvOptions , execRunner , "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull" )
assert . Equal ( t , "cf" , execRunner . Calls [ 0 ] . Exec , "Wrong command" )
assert . Equal ( t , [ ] string { "login" , "-a" , "https://api.endpoint.com" , "-o" , "testOrg" , "-s" , "testSpace" , "-u" , "testUser" , "-p" , "testPassword" } , execRunner . Calls [ 0 ] . Params , "Wrong parameters" )
//assert.Equal(t, []string{"api", "https://api.endpoint.com"}, execRunner.Calls[0].Params, "Wrong parameters")
2020-07-07 16:19:57 +02:00
} )
2020-02-04 12:43:27 +02:00
t . Run ( "Test cf cli command: params missing" , func ( t * testing . T ) {
2020-07-23 10:26:50 +02:00
config := abaputils . AbapEnvironmentOptions {
//CfServiceKeyName: "testServiceKey", this parameter will be missing
2020-02-04 12:43:27 +02:00
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
Username : "testUser" ,
Password : "testPassword" ,
}
2020-07-23 10:26:50 +02:00
options := abaputils . AbapEnvironmentPullGitRepoOptions {
AbapEnvOptions : config ,
}
execRunner := & mock . ExecMockRunner { }
2020-02-04 12:43:27 +02:00
2020-07-23 10:26:50 +02:00
var _ , err = abaputils . GetAbapCommunicationArrangementInfo ( options . AbapEnvOptions , execRunner , "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull" )
2020-04-08 12:43:41 +02:00
assert . Equal ( t , "Parameters missing. Please provide EITHER the Host of the ABAP server OR the Cloud Foundry ApiEndpoint, Organization, Space, Service Instance and a corresponding Service Key for the Communication Scenario SAP_COM_0510" , err . Error ( ) , "Different error message expected" )
2020-02-04 12:43:27 +02:00
} )
t . Run ( "Test cf cli command: params missing" , func ( t * testing . T ) {
2020-07-23 10:26:50 +02:00
config := abaputils . AbapEnvironmentOptions {
2020-02-04 12:43:27 +02:00
Username : "testUser" ,
Password : "testPassword" ,
}
2020-07-23 10:26:50 +02:00
options := abaputils . AbapEnvironmentPullGitRepoOptions {
AbapEnvOptions : config ,
}
execRunner := & mock . ExecMockRunner { }
2020-02-04 12:43:27 +02:00
2020-07-23 10:26:50 +02:00
var _ , err = abaputils . GetAbapCommunicationArrangementInfo ( options . AbapEnvOptions , execRunner , "/sap/opu/odata/sap/MANAGE_GIT_REPOSITORY/Pull" )
2020-04-08 12:43:41 +02:00
assert . Equal ( t , "Parameters missing. Please provide EITHER the Host of the ABAP server OR the Cloud Foundry ApiEndpoint, Organization, Space, Service Instance and a corresponding Service Key for the Communication Scenario SAP_COM_0510" , err . Error ( ) , "Different error message expected" )
2020-02-04 12:43:27 +02:00
} )
}
2020-04-08 12:43:41 +02:00
func TestTimeConverter ( t * testing . T ) {
t . Run ( "Test example time" , func ( t * testing . T ) {
inputDate := "/Date(1585576809000+0000)/"
expectedDate := "2020-03-30 14:00:09 +0000 UTC"
result := convertTime ( inputDate )
assert . Equal ( t , expectedDate , result . String ( ) , "Dates do not match after conversion" )
} )
t . Run ( "Test Unix time" , func ( t * testing . T ) {
inputDate := "/Date(0000000000000+0000)/"
expectedDate := "1970-01-01 00:00:00 +0000 UTC"
result := convertTime ( inputDate )
assert . Equal ( t , expectedDate , result . String ( ) , "Dates do not match after conversion" )
} )
t . Run ( "Test unexpected format" , func ( t * testing . T ) {
inputDate := "/Date(0012300000001+0000)/"
expectedDate := "1970-01-01 00:00:00 +0000 UTC"
result := convertTime ( inputDate )
assert . Equal ( t , expectedDate , result . String ( ) , "Dates do not match after conversion" )
} )
}
2020-02-04 12:43:27 +02:00
type clientMock struct {
2020-04-08 12:43:41 +02:00
Token string
Body string
BodyList [ ] string
StatusCode int
Error error
2020-02-04 12:43:27 +02:00
}
func ( c * clientMock ) SetOptions ( opts piperhttp . ClientOptions ) { }
func ( c * clientMock ) SendRequest ( method , url string , bdy io . Reader , hdr http . Header , cookies [ ] * http . Cookie ) ( * http . Response , error ) {
var body [ ] byte
if c . Body != "" {
body = [ ] byte ( c . Body )
} else {
bodyString := c . BodyList [ len ( c . BodyList ) - 1 ]
c . BodyList = c . BodyList [ : len ( c . BodyList ) - 1 ]
body = [ ] byte ( bodyString )
}
header := http . Header { }
header . Set ( "X-Csrf-Token" , c . Token )
return & http . Response {
2020-04-08 12:43:41 +02:00
StatusCode : c . StatusCode ,
2020-02-04 12:43:27 +02:00
Header : header ,
Body : ioutil . NopCloser ( bytes . NewReader ( body ) ) ,
2020-04-08 12:43:41 +02:00
} , c . Error
2020-02-04 12:43:27 +02:00
}