1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-14 11:03:09 +02:00
sap-jenkins-library/cmd/integrationArtifactResource_test.go

138 lines
3.7 KiB
Go
Raw Normal View History

package cmd
import (
"path/filepath"
"testing"
"github.com/SAP/jenkins-library/pkg/mock"
"github.com/stretchr/testify/assert"
)
func TestRunIntegrationArtifactResource(t *testing.T) {
t.Parallel()
t.Run("Create Resource Test", func(t *testing.T) {
filesMock := mock.FilesMock{}
path := filepath.Join("tempDir", "demo.xsl")
filesMock.AddFile(path, []byte("dummy content"))
exists, err := filesMock.FileExists(path)
assert.NoError(t, err)
assert.True(t, exists)
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactResourceOptions{
APIServiceKey: apiServiceKey,
IntegrationFlowID: "flow1",
Operation: "create",
ResourcePath: path,
}
httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactResourceCreate", ResponseBody: ``, TestType: "Positive"}
// test
err = runIntegrationArtifactResource(&config, nil, &filesMock, &httpClient)
// assert
assert.NoError(t, err)
})
t.Run("Update Resource Test", func(t *testing.T) {
filesMock := mock.FilesMock{}
path := filepath.Join("tempDir", "demo.xsl")
filesMock.AddFile(path, []byte("dummy content"))
exists, err := filesMock.FileExists(path)
assert.NoError(t, err)
assert.True(t, exists)
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactResourceOptions{
APIServiceKey: apiServiceKey,
IntegrationFlowID: "flow1",
Operation: "update",
ResourcePath: path,
}
httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactResourceUpdate", ResponseBody: ``, TestType: "Positive"}
// test
err = runIntegrationArtifactResource(&config, nil, &filesMock, &httpClient)
// assert
assert.NoError(t, err)
})
t.Run("Delete Resource Test", func(t *testing.T) {
filesMock := mock.FilesMock{}
path := filepath.Join("tempDir", "demo.xsl")
filesMock.AddFile(path, []byte("dummy content"))
exists, err := filesMock.FileExists(path)
assert.NoError(t, err)
assert.True(t, exists)
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactResourceOptions{
APIServiceKey: apiServiceKey,
IntegrationFlowID: "flow1",
Operation: "delete",
ResourcePath: path,
}
httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactResourceDelete", ResponseBody: ``, TestType: "Positive"}
// test
err = runIntegrationArtifactResource(&config, nil, &filesMock, &httpClient)
// assert
assert.NoError(t, err)
})
t.Run("Create Resource Negative Test", func(t *testing.T) {
filesMock := mock.FilesMock{}
path := filepath.Join("tempDir", "demo.xsl")
filesMock.AddFile(path, []byte("dummy content"))
exists, err := filesMock.FileExists(path)
assert.NoError(t, err)
assert.True(t, exists)
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := integrationArtifactResourceOptions{
APIServiceKey: apiServiceKey,
IntegrationFlowID: "flow1",
Operation: "create",
ResourcePath: path,
}
httpClient := httpMockCpis{CPIFunction: "IntegrationArtifactResourceCreate", ResponseBody: ``, TestType: "Negative"}
// test
err = runIntegrationArtifactResource(&config, nil, &filesMock, &httpClient)
// assert
assert.Error(t, err)
})
}