1
0
mirror of https://github.com/SAP/jenkins-library.git synced 2024-12-12 10:55:20 +02:00
sap-jenkins-library/cmd/apiKeyValueMapUpload_test.go
Mayur Belur Mohan 9a3b800b9d
ApiKeyValueMapUpload Command (#3407)
* ApiKeyValueMapUpload Command

* include command in metadata

* TestCase Fixes

* CodeReview Fixes

* CodeReview Fixes

* Code Review Fixes

* CodeReview Fixes

* CodeReview Fixes

* CodeReview FIxes

* CodeReview Fixes

* Documenation change

* documentation fix

* Documentation Fix

* Documentation Fix

* documentation Fix

* CodeReview Fixes

* CodeReview Fixes

* Revert changes

* Documentation Fix

* CodeReview FIxes

* Doc Fixes

* Code Review Fixes

* Code Review Fixes

* CodeReview FIxes

* Documentation Fix

* Documentation Changes

* Documentation Fix

* codereview fix

* Documentation Fix

* CodeReview Fixes

* CodeReview Fix

* Documentation FIx

* doc fix

* Doc Fix

* Documentation Fix

* codereview fix

* revert fix

* Code Review Fix

Co-authored-by: Oliver Feldmann <oliver.feldmann@sap.com>
2022-03-07 11:03:44 +01:00

99 lines
2.9 KiB
Go

package cmd
import (
"bytes"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestRunApiKeyValueMapUpload(t *testing.T) {
t.Parallel()
t.Run("Successfull Api Key Value Map Create Test", func(t *testing.T) {
// init
config := getDefaultOptions()
httpClient := httpMockCpis{CPIFunction: "ApiKeyValueMapUpload", ResponseBody: ``, TestType: "PositiveCase"}
// test
err := runApiKeyValueMapUpload(&config, nil, &httpClient)
// assert
if assert.NoError(t, err) {
t.Run("check url", func(t *testing.T) {
assert.Equal(t, "https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries", httpClient.URL)
})
t.Run("check method", func(t *testing.T) {
assert.Equal(t, "POST", httpClient.Method)
})
}
})
t.Run("Failed case of API Key Value Map Create Test", func(t *testing.T) {
// init
config := getDefaultOptions()
httpClient := httpMockCpis{CPIFunction: "ApiKeyValueMapUpload", ResponseBody: ``, TestType: "Negative"}
// test
err := runApiKeyValueMapUpload(&config, nil, &httpClient)
// assert
assert.EqualError(t, err, "HTTP \"POST\" request to \"https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries\" failed with error: 401 Unauthorized")
})
t.Run("Test API Key Value Map payload", func(t *testing.T) {
// init
config := getDefaultOptions()
testPayload := bytes.NewBuffer([]byte("{\"encrypted\":true,\"keyMapEntryValues\":[{\"map_name\":\"demoMap\",\"name\":\"demo\",\"value\":\"name\"}],\"name\":\"demoMap\",\"scope\":\"ENV\"}"))
// test
payload, err := createJSONPayload(&config)
// assert
require.NoError(t, err)
assert.Equal(t, testPayload, payload)
})
t.Run("Http Response not accepted Test case", func(t *testing.T) {
// init
config := getDefaultOptions()
httpClient := httpMockCpis{CPIFunction: "ApiKeyValueMapUpload", ResponseBody: ``, TestType: "HttpResponseNotAccepted"}
// test
err := runApiKeyValueMapUpload(&config, nil, &httpClient)
// assert
assert.EqualError(t, err, "Failed to upload API key value map artefact, Response Status code: 202")
})
t.Run("Http Response not accepted Test case", func(t *testing.T) {
// init
config := getDefaultOptions()
httpClient := httpMockCpis{CPIFunction: "ApiKeyValueMapUpload", ResponseBody: ``, TestType: "NilHttpResponse"}
// test
err := runApiKeyValueMapUpload(&config, nil, &httpClient)
// assert
assert.EqualError(t, err, "HTTP \"POST\" request to \"https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries\" failed with error: invalid payalod")
})
}
func getDefaultOptions() apiKeyValueMapUploadOptions {
return apiKeyValueMapUploadOptions{
APIServiceKey: `{
"oauth": {
"url": "https://demo",
"clientid": "demouser",
"clientsecret": "******",
"tokenurl": "https://demo/oauth/token"
}
}`,
Key: "demo",
Value: "name",
KeyValueMapName: "demoMap",
}
}