github.com/SAP/jenkins-library@v1.362.0/cmd/apiKeyValueMapUpload_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "bytes" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestRunApiKeyValueMapUpload(t *testing.T) { 15 t.Parallel() 16 17 t.Run("Successfull Api Key Value Map Create Test", func(t *testing.T) { 18 // init 19 config := getDefaultOptions() 20 httpClient := httpMockCpis{CPIFunction: "ApiKeyValueMapUpload", ResponseBody: ``, TestType: "PositiveCase"} 21 // test 22 err := runApiKeyValueMapUpload(&config, nil, &httpClient) 23 // assert 24 if assert.NoError(t, err) { 25 t.Run("check url", func(t *testing.T) { 26 assert.Equal(t, "https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries", httpClient.URL) 27 }) 28 29 t.Run("check method", func(t *testing.T) { 30 assert.Equal(t, "POST", httpClient.Method) 31 }) 32 } 33 }) 34 35 t.Run("Failed case of API Key Value Map Create Test", func(t *testing.T) { 36 // init 37 config := getDefaultOptions() 38 39 httpClient := httpMockCpis{CPIFunction: "ApiKeyValueMapUpload", ResponseBody: ``, TestType: "Negative"} 40 41 // test 42 err := runApiKeyValueMapUpload(&config, nil, &httpClient) 43 44 // assert 45 assert.EqualError(t, err, "HTTP \"POST\" request to \"https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries\" failed with error: 401 Unauthorized") 46 }) 47 48 t.Run("Test API Key Value Map payload", func(t *testing.T) { 49 // init 50 config := getDefaultOptions() 51 testPayload := bytes.NewBuffer([]byte("{\"encrypted\":true,\"keyMapEntryValues\":[{\"map_name\":\"demoMap\",\"name\":\"demo\",\"value\":\"name\"}],\"name\":\"demoMap\",\"scope\":\"ENV\"}")) 52 // test 53 payload, err := createJSONPayload(&config) 54 // assert 55 require.NoError(t, err) 56 assert.Equal(t, testPayload, payload) 57 }) 58 59 t.Run("Http Response not accepted Test case", func(t *testing.T) { 60 // init 61 config := getDefaultOptions() 62 63 httpClient := httpMockCpis{CPIFunction: "ApiKeyValueMapUpload", ResponseBody: ``, TestType: "HttpResponseNotAccepted"} 64 65 // test 66 err := runApiKeyValueMapUpload(&config, nil, &httpClient) 67 68 // assert 69 assert.EqualError(t, err, "Failed to upload API key value map artefact, Response Status code: 202") 70 }) 71 72 t.Run("Http Response not accepted Test case", func(t *testing.T) { 73 // init 74 config := getDefaultOptions() 75 76 httpClient := httpMockCpis{CPIFunction: "ApiKeyValueMapUpload", ResponseBody: ``, TestType: "NilHttpResponse"} 77 78 // test 79 err := runApiKeyValueMapUpload(&config, nil, &httpClient) 80 81 // assert 82 assert.EqualError(t, err, "HTTP \"POST\" request to \"https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries\" failed with error: invalid payalod") 83 }) 84 85 } 86 87 func getDefaultOptions() apiKeyValueMapUploadOptions { 88 return apiKeyValueMapUploadOptions{ 89 APIServiceKey: `{ 90 "oauth": { 91 "url": "https://demo", 92 "clientid": "demouser", 93 "clientsecret": "******", 94 "tokenurl": "https://demo/oauth/token" 95 } 96 }`, 97 Key: "demo", 98 Value: "name", 99 KeyValueMapName: "demoMap", 100 } 101 }