1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/apiKeyValueMapDownload_test.go
Jordi van Liempt 0ba4c2206c
chore(deps): Replace io/ioutil package (#4494)
* update all deprecated ioutil usages

* forgotten changes

* add missing imports

* undo changing comment

* add missing 'os' import

* fix integration test

---------

Co-authored-by: I557621 <jordi.van.liempt@sap.com>
Co-authored-by: Gulom Alimov <gulomjon.alimov@sap.com>
2023-08-16 12:57:04 +02:00

71 lines
2.0 KiB
Go

//go:build unit
// +build unit
package cmd
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
)
func TestRunApiKeyValueMapDownload(t *testing.T) {
t.Parallel()
t.Run("Successfull Download of API Key Value Map", func(t *testing.T) {
file, err := os.CreateTemp("", "CustKVM.json")
if err != nil {
t.FailNow()
}
defer os.RemoveAll(file.Name()) // clean up
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := apiKeyValueMapDownloadOptions{
APIServiceKey: apiServiceKey,
KeyValueMapName: "CustKVM",
DownloadPath: file.Name(),
}
httpClient := httpMockCpis{CPIFunction: "APIKeyValueMapDownload", ResponseBody: ``, TestType: "Positive"}
errResp := runApiKeyValueMapDownload(&config, nil, &httpClient)
if assert.NoError(t, errResp) {
t.Run("check file", func(t *testing.T) {
assert.Equal(t, fileExists(file.Name()), true)
})
t.Run("check url", func(t *testing.T) {
assert.Equal(t, "https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries('CustKVM')", httpClient.URL)
})
t.Run("check method", func(t *testing.T) {
assert.Equal(t, "GET", httpClient.Method)
})
}
})
t.Run("Failed case of API Key Value Map Download", func(t *testing.T) {
apiServiceKey := `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`
config := apiKeyValueMapDownloadOptions{
APIServiceKey: apiServiceKey,
KeyValueMapName: "CustKVM",
DownloadPath: "",
}
httpClient := httpMockCpis{CPIFunction: "APIKeyValueMapDownloadFailure", ResponseBody: ``, TestType: "Negative"}
err := runApiKeyValueMapDownload(&config, nil, &httpClient)
assert.EqualError(t, err, "HTTP GET request to https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries('CustKVM') failed with error: Service not Found")
})
}