github.com/xgoffin/jenkins-library@v1.154.0/cmd/apiProviderDownload_test.go (about) 1 package cmd 2 3 import ( 4 "testing" 5 6 "github.com/SAP/jenkins-library/pkg/mock" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 type apiProviderDownloadTestUtilsBundle struct { 11 *mock.ExecMockRunner 12 *mock.FilesMock 13 } 14 15 func apiProviderDownloadMockUtilsBundle() *apiProviderDownloadTestUtilsBundle { 16 utilsBundle := apiProviderDownloadTestUtilsBundle{ 17 ExecMockRunner: &mock.ExecMockRunner{}, 18 FilesMock: &mock.FilesMock{}, 19 } 20 return &utilsBundle 21 } 22 23 //Successful API Provider download cases 24 func TestApiProviderDownloadSuccess(t *testing.T) { 25 t.Parallel() 26 t.Run("Successful Download of API Provider", func(t *testing.T) { 27 apiServiceKey := `{ 28 "oauth": { 29 "url": "https://demo", 30 "clientid": "demouser", 31 "clientsecret": "******", 32 "tokenurl": "https://demo/oauth/token" 33 } 34 }` 35 36 config := apiProviderDownloadOptions{ 37 APIServiceKey: apiServiceKey, 38 APIProviderName: "provider1", 39 DownloadPath: "APIProvider.json", 40 } 41 httpClient := httpMockCpis{CPIFunction: "APIProviderDownload", ResponseBody: ``, TestType: "Positive"} 42 utilsMock := apiProviderDownloadMockUtilsBundle() 43 err := runApiProviderDownload(&config, nil, &httpClient, utilsMock) 44 45 if assert.NoError(t, err) { 46 47 t.Run("Assert file download & content", func(t *testing.T) { 48 fileExist := assert.True(t, utilsMock.HasWrittenFile(config.DownloadPath)) 49 if fileExist { 50 providerbyteContent, _ := utilsMock.FileRead(config.DownloadPath) 51 providerContent := string(providerbyteContent) 52 assert.Equal(t, providerContent, "{\n\t\t\t\t\"d\": {\n\t\t\t\t\t\"results\": [\n\t\t\t\t\t\t{\n\t\t\t\t\t\t\t\"__metadata\": {\n\t\t\t\t\t\t\t\t\"id\": \"https://roverpoc.it-accd002.cfapps.sap.hana.ondemand.com:443/api/v1/MessageProcessingLogs('AGAS1GcWkfBv-ZtpS6j7TKjReO7t')\",\n\t\t\t\t\t\t\t\t\"uri\": \"https://roverpoc.it-accd002.cfapps.sap.hana.ondemand.com:443/api/v1/MessageProcessingLogs('AGAS1GcWkfBv-ZtpS6j7TKjReO7t')\",\n\t\t\t\t\t\t\t\t\"type\": \"com.sap.hci.api.MessageProcessingLog\"\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t\t\"MessageGuid\": \"AGAS1GcWkfBv-ZtpS6j7TKjReO7t\",\n\t\t\t\t\t\t\t\"CorrelationId\": \"AGAS1GevYrPodxieoYf4YSY4jd-8\",\n\t\t\t\t\t\t\t\"ApplicationMessageId\": null,\n\t\t\t\t\t\t\t\"ApplicationMessageType\": null,\n\t\t\t\t\t\t\t\"LogStart\": \"/Date(1611846759005)/\",\n\t\t\t\t\t\t\t\"LogEnd\": \"/Date(1611846759032)/\",\n\t\t\t\t\t\t\t\"Sender\": null,\n\t\t\t\t\t\t\t\"Receiver\": null,\n\t\t\t\t\t\t\t\"IntegrationFlowName\": \"flow1\",\n\t\t\t\t\t\t\t\"Status\": \"COMPLETED\",\n\t\t\t\t\t\t\t\"LogLevel\": \"INFO\",\n\t\t\t\t\t\t\t\"CustomStatus\": \"COMPLETED\",\n\t\t\t\t\t\t\t\"TransactionId\": \"aa220151116748eeae69db3e88f2bbc8\"\n\t\t\t\t\t\t}\n\t\t\t\t\t]\n\t\t\t\t}\n\t\t\t}") 53 } 54 }) 55 56 t.Run("Assert API Provider url", func(t *testing.T) { 57 assert.Equal(t, "https://demo/apiportal/api/1.0/Management.svc/APIProviders('provider1')", httpClient.URL) 58 }) 59 60 t.Run("Assert method as GET", func(t *testing.T) { 61 assert.Equal(t, "GET", httpClient.Method) 62 }) 63 } 64 }) 65 } 66 67 //API Provider download failure cases 68 func TestApiProviderDownloadFailure(t *testing.T) { 69 t.Parallel() 70 71 t.Run("Failed case of API Provider Download", func(t *testing.T) { 72 apiServiceKey := `{ 73 "oauth": { 74 "url": "https://demo", 75 "clientid": "demouser", 76 "clientsecret": "******", 77 "tokenurl": "https://demo/oauth/token" 78 } 79 }` 80 config := apiProviderDownloadOptions{ 81 APIServiceKey: apiServiceKey, 82 APIProviderName: "provider1", 83 DownloadPath: "APIProvider.json", 84 } 85 httpClient := httpMockCpis{CPIFunction: "APIProviderDownloadFailure", ResponseBody: ``, TestType: "Negative"} 86 utilsMock := apiProviderDownloadMockUtilsBundle() 87 err := runApiProviderDownload(&config, nil, &httpClient, utilsMock) 88 89 assert.False(t, utilsMock.HasWrittenFile(config.DownloadPath)) 90 91 assert.EqualError(t, err, "HTTP GET request to https://demo/apiportal/api/1.0/Management.svc/APIProviders('provider1') failed with error: Service not Found") 92 }) 93 }