2020-02-04 12:43:27 +02:00
package cmd
import (
"bytes"
2020-02-27 13:11:22 +02:00
"github.com/SAP/jenkins-library/pkg/mock"
2020-02-04 12:43:27 +02:00
"io"
"io/ioutil"
"net/http"
"testing"
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 ) {
uriExpected := "example.com"
tokenExpected := "myToken"
client := & clientMock {
Body : ` { "d" : { "__metadata" : { "uri" : " ` + uriExpected + ` " } } } ` ,
Token : tokenExpected ,
}
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKey : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
}
con := connectionDetailsHTTP {
User : "MY_USER" ,
Password : "MY_PW" ,
URL : "https://api.endpoint.com/Entity/" ,
}
entityConnection , _ := triggerPull ( config , con , client )
assert . Equal ( t , uriExpected , entityConnection . URL )
assert . Equal ( t , tokenExpected , entityConnection . XCsrfToken )
} )
}
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" } } ` ,
} ,
Token : "myToken" ,
}
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKey : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
}
con := connectionDetailsHTTP {
User : "MY_USER" ,
Password : "MY_PW" ,
URL : "https://api.endpoint.com/Entity/" ,
XCsrfToken : "MY_TOKEN" ,
}
status , _ := pollEntity ( config , con , client , 0 )
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" } } ` ,
} ,
Token : "myToken" ,
}
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKey : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
}
con := connectionDetailsHTTP {
User : "MY_USER" ,
Password : "MY_PW" ,
URL : "https://api.endpoint.com/Entity/" ,
XCsrfToken : "MY_TOKEN" ,
}
status , _ := pollEntity ( config , con , client , 0 )
assert . Equal ( t , "E" , status )
} )
}
func TestGetAbapCommunicationArrangementInfo ( t * testing . T ) {
t . Run ( "Test cf cli command: success case" , func ( t * testing . T ) {
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
CfServiceKey : "testServiceKey" ,
Username : "testUser" ,
Password : "testPassword" ,
}
2020-02-27 13:11:22 +02:00
execRunner := mock . ExecMockRunner { }
2020-02-04 12:43:27 +02:00
getAbapCommunicationArrangementInfo ( config , & execRunner )
2020-02-27 13:11:22 +02:00
assert . Equal ( t , "cf" , execRunner . Calls [ 0 ] . Exec , "Wrong command" )
assert . Equal ( t , [ ] string { "login" , "-a" , "https://api.endpoint.com" , "-u" , "testUser" , "-p" , "testPassword" , "-o" , "testOrg" , "-s" , "testSpace" } , execRunner . Calls [ 0 ] . Params , "Wrong parameters" )
2020-02-04 12:43:27 +02:00
} )
t . Run ( "Test cf cli command: params missing" , func ( t * testing . T ) {
config := abapEnvironmentPullGitRepoOptions {
CfAPIEndpoint : "https://api.endpoint.com" ,
CfOrg : "testOrg" ,
CfSpace : "testSpace" ,
CfServiceInstance : "testInstance" ,
Username : "testUser" ,
Password : "testPassword" ,
}
2020-02-27 13:11:22 +02:00
execRunner := mock . ExecMockRunner { }
2020-02-04 12:43:27 +02:00
var _ , err = getAbapCommunicationArrangementInfo ( config , & execRunner )
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 ( ) , "Expected error message" )
} )
t . Run ( "Test cf cli command: params missing" , func ( t * testing . T ) {
config := abapEnvironmentPullGitRepoOptions {
Username : "testUser" ,
Password : "testPassword" ,
}
2020-02-27 13:11:22 +02:00
execRunner := mock . ExecMockRunner { }
2020-02-04 12:43:27 +02:00
var _ , err = getAbapCommunicationArrangementInfo ( config , & execRunner )
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 ( ) , "Expected error message" )
} )
}
type clientMock struct {
Token string
Body string
BodyList [ ] string
}
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 {
StatusCode : 200 ,
Header : header ,
Body : ioutil . NopCloser ( bytes . NewReader ( body ) ) ,
} , nil
}