github.com/SAP/jenkins-library@v1.362.0/cmd/integrationArtifactDeploy_test.go (about)

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"bytes"
     8  	"fmt"
     9  	"io"
    10  	"net/http"
    11  	"testing"
    12  
    13  	"github.com/SAP/jenkins-library/pkg/cpi"
    14  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    15  	"github.com/SAP/jenkins-library/pkg/mock"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  type integrationArtifactDeployMockUtils struct {
    20  	*mock.ExecMockRunner
    21  }
    22  
    23  func newIntegrationArtifactDeployTestsUtils() integrationArtifactDeployMockUtils {
    24  	utils := integrationArtifactDeployMockUtils{
    25  		ExecMockRunner: &mock.ExecMockRunner{},
    26  	}
    27  	return utils
    28  }
    29  
    30  func TestRunIntegrationArtifactDeploy(t *testing.T) {
    31  	t.Parallel()
    32  
    33  	t.Run("Successfull Integration Flow Deploy Test", func(t *testing.T) {
    34  		apiServiceKey := `{
    35  			"oauth": {
    36  				"url": "https://demo",
    37  				"clientid": "demouser",
    38  				"clientsecret": "******",
    39  				"tokenurl": "https://demo/oauth/token"
    40  			}
    41  		}`
    42  
    43  		config := integrationArtifactDeployOptions{
    44  			APIServiceKey:     apiServiceKey,
    45  			IntegrationFlowID: "flow1",
    46  		}
    47  
    48  		httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "PositiveAndDeployIntegrationDesigntimeArtifactResBody"}
    49  
    50  		err := runIntegrationArtifactDeploy(&config, nil, &httpClient)
    51  
    52  		if assert.NoError(t, err) {
    53  
    54  			t.Run("check url", func(t *testing.T) {
    55  				assert.Equal(t, "https://demo/api/v1/BuildAndDeployStatus(TaskId='')", httpClient.URL)
    56  			})
    57  
    58  			t.Run("check method", func(t *testing.T) {
    59  				assert.Equal(t, "GET", httpClient.Method)
    60  			})
    61  		}
    62  	})
    63  
    64  	t.Run("Trigger Failure for Integration Flow Deployment", func(t *testing.T) {
    65  
    66  		apiServiceKey := `{
    67  			"oauth": {
    68  				"url": "https://demo",
    69  				"clientid": "demouser",
    70  				"clientsecret": "******",
    71  				"tokenurl": "https://demo/oauth/token"
    72  			}
    73  		}`
    74  
    75  		config := integrationArtifactDeployOptions{
    76  			APIServiceKey:     apiServiceKey,
    77  			IntegrationFlowID: "flow1",
    78  		}
    79  
    80  		httpClient := httpMockCpis{CPIFunction: "FailIntegrationDesigntimeArtifactDeployment", ResponseBody: ``, TestType: "Negative"}
    81  
    82  		err := runIntegrationArtifactDeploy(&config, nil, &httpClient)
    83  
    84  		if assert.Error(t, err) {
    85  
    86  			t.Run("check url", func(t *testing.T) {
    87  				assert.Equal(t, "https://demo/api/v1/DeployIntegrationDesigntimeArtifact?Id='flow1'&Version='Active'", httpClient.URL)
    88  			})
    89  
    90  			t.Run("check method", func(t *testing.T) {
    91  				assert.Equal(t, "POST", httpClient.Method)
    92  			})
    93  		}
    94  	})
    95  
    96  	t.Run("Failed Integration Flow Deploy Test", func(t *testing.T) {
    97  
    98  		apiServiceKey := `{
    99  			"oauth": {
   100  				"url": "https://demo",
   101  				"clientid": "demouser",
   102  				"clientsecret": "******",
   103  				"tokenurl": "https://demo/oauth/token"
   104  			}
   105  		}`
   106  
   107  		config := integrationArtifactDeployOptions{
   108  			APIServiceKey:     apiServiceKey,
   109  			IntegrationFlowID: "flow1",
   110  		}
   111  
   112  		httpClient := httpMockCpis{CPIFunction: "", ResponseBody: ``, TestType: "NegativeAndDeployIntegrationDesigntimeArtifactResBody"}
   113  
   114  		err := runIntegrationArtifactDeploy(&config, nil, &httpClient)
   115  
   116  		assert.EqualError(t, err, "{\"message\": \"java.lang.IllegalStateException: No credentials for 'smtp' found\"}")
   117  	})
   118  
   119  	t.Run("Successfull GetIntegrationArtifactDeployStatus Test", func(t *testing.T) {
   120  		clientOptions := piperhttp.ClientOptions{}
   121  		clientOptions.Token = fmt.Sprintf("Bearer %s", "Demo")
   122  		apiServiceKey := `{
   123  			"oauth": {
   124  				"url": "https://demo",
   125  				"clientid": "demouser",
   126  				"clientsecret": "******",
   127  				"tokenurl": "https://demo/oauth/token"
   128  			}
   129  		}`
   130  
   131  		config := integrationArtifactDeployOptions{
   132  			APIServiceKey:     apiServiceKey,
   133  			IntegrationFlowID: "flow1",
   134  		}
   135  
   136  		httpClient := httpMockCpis{CPIFunction: "GetIntegrationArtifactDeployStatus", Options: clientOptions, ResponseBody: ``, TestType: "PositiveAndDeployIntegrationDesigntimeArtifactResBody"}
   137  
   138  		resp, err := getIntegrationArtifactDeployStatus(&config, &httpClient, "https://demo", "9094d6cd-3683-4a99-794f-834ed30fcb01")
   139  
   140  		assert.Equal(t, "STARTED", resp)
   141  
   142  		assert.NoError(t, err)
   143  	})
   144  
   145  	t.Run("Successfull GetIntegrationArtifactDeployError Test", func(t *testing.T) {
   146  		clientOptions := piperhttp.ClientOptions{}
   147  		clientOptions.Token = fmt.Sprintf("Bearer %s", "Demo")
   148  		apiServiceKey := `{
   149  			"oauth": {
   150  				"url": "https://demo",
   151  				"clientid": "demouser",
   152  				"clientsecret": "******",
   153  				"tokenurl": "https://demo/oauth/token"
   154  			}
   155  		}`
   156  
   157  		config := integrationArtifactDeployOptions{
   158  			APIServiceKey:     apiServiceKey,
   159  			IntegrationFlowID: "flow1",
   160  		}
   161  
   162  		httpClient := httpMockCpis{CPIFunction: "GetIntegrationArtifactDeployErrorDetails", Options: clientOptions, ResponseBody: ``, TestType: "PositiveAndGetDeployedIntegrationDesigntimeArtifactErrorResBody"}
   163  
   164  		resp, err := getIntegrationArtifactDeployError(&config, &httpClient, "https://demo")
   165  
   166  		assert.Equal(t, "{\"message\": \"java.lang.IllegalStateException: No credentials for 'smtp' found\"}", resp)
   167  
   168  		assert.NoError(t, err)
   169  	})
   170  }
   171  
   172  type httpMockCpis struct {
   173  	Method       string
   174  	URL          string
   175  	Header       map[string][]string
   176  	ResponseBody string
   177  	Options      piperhttp.ClientOptions
   178  	StatusCode   int
   179  	CPIFunction  string
   180  	TestType     string
   181  }
   182  
   183  func (c *httpMockCpis) SetOptions(options piperhttp.ClientOptions) {
   184  	c.Options = options
   185  }
   186  
   187  func (c *httpMockCpis) SendRequest(method string, url string, r io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error) {
   188  
   189  	c.Method = method
   190  	c.URL = url
   191  
   192  	if r != nil {
   193  		_, err := io.ReadAll(r)
   194  
   195  		if err != nil {
   196  			return nil, err
   197  		}
   198  	}
   199  
   200  	if c.Options.Token == "" {
   201  		c.ResponseBody = "{\r\n\t\t\t\"access_token\": \"demotoken\",\r\n\t\t\t\"token_type\": \"Bearer\",\r\n\t\t\t\"expires_in\": 3600,\r\n\t\t\t\"scope\": \"\"\r\n\t\t}"
   202  		c.StatusCode = 200
   203  		res := http.Response{
   204  			StatusCode: c.StatusCode,
   205  			Header:     c.Header,
   206  			Body:       io.NopCloser(bytes.NewReader([]byte(c.ResponseBody))),
   207  		}
   208  		return &res, nil
   209  	}
   210  	if c.CPIFunction == "" {
   211  		c.CPIFunction = cpi.GetCPIFunctionNameByURLCheck(url, method, c.TestType)
   212  		resp, error := cpi.GetCPIFunctionMockResponse(c.CPIFunction, c.TestType)
   213  		c.CPIFunction = ""
   214  		return resp, error
   215  	}
   216  
   217  	return cpi.GetCPIFunctionMockResponse(c.CPIFunction, c.TestType)
   218  }