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