1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/integrationArtifactTriggerIntegrationTest_test.go
Jk1484 ffc931aad1
feat(golangBuild): use 'unit' build tag to include tests during test execution (#4345)
* Added unit tag as argument. Added description to runTests command. Changed code generator to have unit build tag in generated unit test files.

* Added unit build tag to all unit test files.

* added to new unit test unit build tag

* Update verify-go.yml

* small fix

---------

Co-authored-by: Muhammadali Nazarov <Muhammadali.Nazarov@acronis.com>
Co-authored-by: Christopher Fenner <26137398+CCFenner@users.noreply.github.com>
2023-05-03 21:02:11 +05:00

218 lines
7.2 KiB
Go

//go:build unit
// +build unit
package cmd
import (
"io/ioutil"
"path/filepath"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
type integrationArtifactTriggerIntegrationTestMockUtils struct {
*mock.ExecMockRunner
*mock.FilesMock
}
func newIntegrationArtifactTriggerIntegrationTestTestsUtils() integrationArtifactTriggerIntegrationTestMockUtils {
utils := integrationArtifactTriggerIntegrationTestMockUtils{
ExecMockRunner: &mock.ExecMockRunner{},
FilesMock: &mock.FilesMock{},
}
return utils
}
func TestRunIntegrationArtifactTriggerIntegrationTest(t *testing.T) {
t.Run("MessageBodyPath good but no ContentType (ERROR) callIFlowURL", func(t *testing.T) {
//init
iFlowServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactTriggerIntegrationTestOptions{
IntegrationFlowServiceKey: iFlowServiceKey,
IntegrationFlowID: "CPI_IFlow_Call_using_Cert",
MessageBodyPath: "/file.txt",
ContentType: "",
}
utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
utils.AddFile("file.txt", []byte("dummycontent"))
httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
cpe := integrationArtifactTriggerIntegrationTestCommonPipelineEnvironment{}
//test
err := callIFlowURL(&config, utils, &httpClient, "", &cpe)
//assert
assert.Error(t, err)
})
t.Run("MessageBodyPath and ContentType good but file missing (ERROR) callIFlowURL", func(t *testing.T) {
//init
iFlowServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactTriggerIntegrationTestOptions{
IntegrationFlowServiceKey: iFlowServiceKey,
IntegrationFlowID: "CPI_IFlow_Call_using_Cert",
MessageBodyPath: "test.txt",
ContentType: "txt",
}
utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
//no file created here. error expected
httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
cpe := integrationArtifactTriggerIntegrationTestCommonPipelineEnvironment{}
//test
err := callIFlowURL(&config, utils, &httpClient, "", &cpe)
//assert
assert.Error(t, err)
})
t.Run("MessageBodyPath, ContentType, and file good (SUCCESS) callIFlowURL", func(t *testing.T) {
dir := t.TempDir()
//init
iFlowServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactTriggerIntegrationTestOptions{
IntegrationFlowServiceKey: iFlowServiceKey,
IntegrationFlowID: "CPI_IFlow_Call_using_Cert",
MessageBodyPath: filepath.Join(dir, "test.txt"),
ContentType: "txt",
}
utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
utils.AddFile(config.MessageBodyPath, []byte("dummycontent1")) //have to add a file here to see in utils
if err := ioutil.WriteFile(config.MessageBodyPath, []byte("dummycontent2"), 0755); err != nil {
t.Fail()
}
httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
cpe := integrationArtifactTriggerIntegrationTestCommonPipelineEnvironment{}
//test
err := callIFlowURL(&config, utils, &httpClient, "https://my-service.com/endpoint", &cpe)
//assert
assert.NoError(t, err)
assert.Equal(t, "POST", httpClient.Method)
assert.Equal(t, "https://my-service.com/endpoint", httpClient.URL)
})
t.Run("No MessageBodyPath still works (SUCCESS) callIFlowURL", func(t *testing.T) {
//init
iFlowServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactTriggerIntegrationTestOptions{
IntegrationFlowServiceKey: iFlowServiceKey,
IntegrationFlowID: "CPI_IFlow_Call_using_Cert",
MessageBodyPath: "",
ContentType: "txt",
}
utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
//utils.AddFile(config.MessageBodyPath, []byte("dummycontent1")) //have to add a file here to see in utils
//ioutil.WriteFile(config.MessageBodyPath, []byte("dummycontent2"), 0755)
httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
cpe := integrationArtifactTriggerIntegrationTestCommonPipelineEnvironment{}
//test
err := callIFlowURL(&config, utils, &httpClient, "https://my-service.com/endpoint", &cpe)
//assert
assert.NoError(t, err)
assert.Equal(t, "GET", httpClient.Method)
assert.Equal(t, "https://my-service.com/endpoint", httpClient.URL)
})
t.Run("nil fileBody (SUCCESS) callIFlowURL", func(t *testing.T) {
dir := t.TempDir()
//init
iFlowServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactTriggerIntegrationTestOptions{
IntegrationFlowServiceKey: iFlowServiceKey,
IntegrationFlowID: "CPI_IFlow_Call_using_Cert",
MessageBodyPath: filepath.Join(dir, "test.txt"),
ContentType: "txt",
}
utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
utils.AddFile(config.MessageBodyPath, []byte(nil)) //have to add a file here to see in utils
if err := ioutil.WriteFile(config.MessageBodyPath, []byte(nil), 0755); err != nil {
t.Fail()
}
httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
cpe := integrationArtifactTriggerIntegrationTestCommonPipelineEnvironment{}
//test
err := callIFlowURL(&config, utils, &httpClient, "", &cpe)
//assert
assert.NoError(t, err)
})
t.Run("success - check correct body and headers in cpe", func(t *testing.T) {
//init
iFlowServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactTriggerIntegrationTestOptions{
IntegrationFlowServiceKey: iFlowServiceKey,
IntegrationFlowID: "CPI_IFlow_Call_using_Cert",
ContentType: "txt",
}
utils := newIntegrationArtifactTriggerIntegrationTestTestsUtils()
httpClient := httpMockCpis{CPIFunction: "TriggerIntegrationTest", ResponseBody: ``, TestType: "Positive"}
cpe := integrationArtifactTriggerIntegrationTestCommonPipelineEnvironment{}
//test
err := callIFlowURL(&config, utils, &httpClient, "", &cpe)
//assert
assert.NoError(t, err)
bodyRegexIgnoringWhiteSpaces := "{\\s*\"code\": \"Good Request\",\\s*\"message\": {\\s*\"@lang\": \"en\",\\s*\"#text\": \"valid\"\\s*}\\s*}"
assert.Regexp(t, bodyRegexIgnoringWhiteSpaces, cpe.custom.integrationFlowTriggerIntegrationTestResponseBody)
assert.Equal(t, "{\"test\":[\"this is a test\"]}", cpe.custom.integrationFlowTriggerIntegrationTestResponseHeaders)
})
}