forked from SAP/jenkins-library
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapiKeyValueMapUpload_test.go
98 lines (78 loc) · 2.86 KB
/
apiKeyValueMapUpload_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
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",
}
}