github.com/SAP/jenkins-library@v1.362.0/cmd/apiKeyValueMapDownload_test.go (about) 1 //go:build unit 2 // +build unit 3 4 package cmd 5 6 import ( 7 "os" 8 "testing" 9 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestRunApiKeyValueMapDownload(t *testing.T) { 14 t.Parallel() 15 16 t.Run("Successfull Download of API Key Value Map", func(t *testing.T) { 17 file, err := os.CreateTemp("", "CustKVM.json") 18 if err != nil { 19 t.FailNow() 20 } 21 defer os.RemoveAll(file.Name()) // clean up 22 apiServiceKey := `{ 23 "oauth": { 24 "url": "https://demo", 25 "clientid": "demouser", 26 "clientsecret": "******", 27 "tokenurl": "https://demo/oauth/token" 28 } 29 }` 30 config := apiKeyValueMapDownloadOptions{ 31 APIServiceKey: apiServiceKey, 32 KeyValueMapName: "CustKVM", 33 DownloadPath: file.Name(), 34 } 35 httpClient := httpMockCpis{CPIFunction: "APIKeyValueMapDownload", ResponseBody: ``, TestType: "Positive"} 36 errResp := runApiKeyValueMapDownload(&config, nil, &httpClient) 37 if assert.NoError(t, errResp) { 38 t.Run("check file", func(t *testing.T) { 39 assert.Equal(t, fileExists(file.Name()), true) 40 }) 41 42 t.Run("check url", func(t *testing.T) { 43 assert.Equal(t, "https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries('CustKVM')", httpClient.URL) 44 }) 45 46 t.Run("check method", func(t *testing.T) { 47 assert.Equal(t, "GET", httpClient.Method) 48 }) 49 } 50 }) 51 52 t.Run("Failed case of API Key Value Map Download", func(t *testing.T) { 53 apiServiceKey := `{ 54 "oauth": { 55 "url": "https://demo", 56 "clientid": "demouser", 57 "clientsecret": "******", 58 "tokenurl": "https://demo/oauth/token" 59 } 60 }` 61 config := apiKeyValueMapDownloadOptions{ 62 APIServiceKey: apiServiceKey, 63 KeyValueMapName: "CustKVM", 64 DownloadPath: "", 65 } 66 httpClient := httpMockCpis{CPIFunction: "APIKeyValueMapDownloadFailure", ResponseBody: ``, TestType: "Negative"} 67 err := runApiKeyValueMapDownload(&config, nil, &httpClient) 68 assert.EqualError(t, err, "HTTP GET request to https://demo/apiportal/api/1.0/Management.svc/KeyMapEntries('CustKVM') failed with error: Service not Found") 69 }) 70 }