github.com/ouraigua/jenkins-library@v0.0.0-20231028010029-fbeaf2f3aa9b/cmd/apiProviderList_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/SAP/jenkins-library/pkg/apim"
    10  	apimhttp "github.com/SAP/jenkins-library/pkg/apim"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestRunApiProviderList(t *testing.T) {
    15  	t.Parallel()
    16  
    17  	t.Run("Get API providers successfull test", func(t *testing.T) {
    18  		config := getDefaultOptionsForApiProviderList()
    19  		httpClientMock := &apimhttp.HttpMockAPIM{StatusCode: 200, ResponseBody: `{"some": "test"}`}
    20  		seOut := apiProviderListCommonPipelineEnvironment{}
    21  		apim := apim.Bundle{APIServiceKey: config.APIServiceKey, Client: httpClientMock}
    22  		// test
    23  		err := getApiProviderList(&config, apim, &seOut)
    24  
    25  		// assert
    26  		if assert.NoError(t, err) {
    27  			assert.EqualValues(t, seOut.custom.APIProviderList, "{\"some\": \"test\"}")
    28  			t.Run("check url", func(t *testing.T) {
    29  				assert.Equal(t, "/apiportal/api/1.0/Management.svc/APIProviders?orderby=value&$select=name&$top=2", httpClientMock.URL)
    30  			})
    31  			t.Run("check method", func(t *testing.T) {
    32  				assert.Equal(t, "GET", httpClientMock.Method)
    33  			})
    34  		}
    35  	})
    36  
    37  	t.Run("Get API provider failed test", func(t *testing.T) {
    38  		config := getDefaultOptionsForApiProviderList()
    39  		httpClientMock := &apimhttp.HttpMockAPIM{StatusCode: 400}
    40  		seOut := apiProviderListCommonPipelineEnvironment{}
    41  		apim := apim.Bundle{APIServiceKey: config.APIServiceKey, Client: httpClientMock}
    42  		// test
    43  		err := getApiProviderList(&config, apim, &seOut)
    44  		// assert
    45  		assert.EqualError(t, err, "HTTP GET request to /apiportal/api/1.0/Management.svc/APIProviders?orderby=value&$select=name&$top=2 failed with error: : Bad Request")
    46  	})
    47  }
    48  
    49  func getDefaultOptionsForApiProviderList() apiProviderListOptions {
    50  	return apiProviderListOptions{
    51  		APIServiceKey: apimhttp.GetServiceKey(),
    52  		Top:           2,
    53  		Select:        "name",
    54  		Orderby:       "value",
    55  	}
    56  }