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

     1  //go:build unit
     2  // +build unit
     3  
     4  package cmd
     5  
     6  import (
     7  	"bytes"
     8  	"errors"
     9  	"io"
    10  	"net/http"
    11  	"testing"
    12  
    13  	piperhttp "github.com/SAP/jenkins-library/pkg/http"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestGctsCreateRepositorySuccess(t *testing.T) {
    18  
    19  	config := gctsCreateRepositoryOptions{
    20  		Host:                "http://testHost.com:50000",
    21  		Client:              "000",
    22  		Repository:          "testRepo",
    23  		Username:            "testUser",
    24  		Password:            "testPassword",
    25  		RemoteRepositoryURL: "https://github.com/org/testRepo",
    26  		Role:                "SOURCE",
    27  		VSID:                "TST",
    28  	}
    29  
    30  	t.Run("creating repository on ABAP system successful", func(t *testing.T) {
    31  
    32  		httpClient := httpMockGcts{StatusCode: 200, ResponseBody: `{
    33  			"repository": {
    34  				"rid": "my-repository",
    35  				"name": "Example repository",
    36  				"role": "SOURCE",
    37  				"type": "GIT",
    38  				"vsid": "GI7",
    39  				"status": "READY",
    40  				"branch": "master",
    41  				"url": "https://github.com/git/git",
    42  				"version": "1.0.1",
    43  				"objects": 1337,
    44  				"currentCommit": "f1cdb6a032c1d8187c0990b51e94e8d8bb9898b2",
    45  				"connection": "ssl",
    46  				"config": [
    47  					{
    48  						"key": "CLIENT_VCS_URI",
    49  						"value": "git@github.com/example.git"
    50  					}
    51  				]
    52  			},
    53  			"log": [
    54  				{
    55  					"time": 20180606130524,
    56  					"user": "JENKINS",
    57  					"section": "REPOSITORY_FACTORY",
    58  					"action": "CREATE_REPOSITORY",
    59  					"severity": "INFO",
    60  					"message": "Start action CREATE_REPOSITORY review",
    61  					"code": "GCTS.API.410"
    62  				}
    63  			]
    64  		}`}
    65  
    66  		err := createRepository(&config, nil, nil, &httpClient)
    67  
    68  		if assert.NoError(t, err) {
    69  
    70  			t.Run("check url", func(t *testing.T) {
    71  				assert.Equal(t, "http://testHost.com:50000/sap/bc/cts_abapvcs/repository?sap-client=000", httpClient.URL)
    72  			})
    73  
    74  			t.Run("check method", func(t *testing.T) {
    75  				assert.Equal(t, "POST", httpClient.Method)
    76  			})
    77  
    78  			t.Run("check user", func(t *testing.T) {
    79  				assert.Equal(t, "testUser", httpClient.Options.Username)
    80  			})
    81  
    82  			t.Run("check password", func(t *testing.T) {
    83  				assert.Equal(t, "testPassword", httpClient.Options.Password)
    84  			})
    85  
    86  		}
    87  
    88  	})
    89  
    90  	t.Run("repository already exists on ABAP system", func(t *testing.T) {
    91  
    92  		httpClient := httpMockGcts{StatusCode: 500, ResponseBody: `{
    93  			"exception": "Repository already exists"
    94  		}`}
    95  
    96  		err := createRepository(&config, nil, nil, &httpClient)
    97  
    98  		assert.NoError(t, err)
    99  	})
   100  }
   101  func TestGctsCreateRepositoryFailure(t *testing.T) {
   102  
   103  	config := gctsCreateRepositoryOptions{
   104  		Host:                "http://testHost.com:50000",
   105  		Client:              "000",
   106  		Repository:          "testRepo",
   107  		Username:            "testUser",
   108  		Password:            "testPassword",
   109  		RemoteRepositoryURL: "https://github.com/org/testRepo",
   110  		Role:                "SOURCE",
   111  		VSID:                "TST",
   112  	}
   113  
   114  	t.Run("a http error occurred", func(t *testing.T) {
   115  
   116  		httpClient := httpMockGcts{StatusCode: 500, ResponseBody: `{
   117  			"log": [
   118  				{
   119  					"time": 20180606130524,
   120  					"user": "JENKINS",
   121  					"section": "REPOSITORY_FACTORY",
   122  					"action": "CREATE_REPOSITORY",
   123  					"severity": "INFO",
   124  					"message": "Start action CREATE_REPOSITORY review",
   125  					"code": "GCTS.API.410"
   126  				}
   127  			],
   128  			"errorLog": [
   129  				{
   130  					"time": 20180606130524,
   131  					"user": "JENKINS",
   132  					"section": "REPOSITORY_FACTORY",
   133  					"action": "CREATE_REPOSITORY",
   134  					"severity": "INFO",
   135  					"message": "Start action CREATE_REPOSITORY review",
   136  					"code": "GCTS.API.410"
   137  				}
   138  			],
   139  			"exception": {
   140  				"message": "repository_not_found",
   141  				"description": "Repository not found",
   142  				"code": 404
   143  			}
   144  		}`}
   145  
   146  		err := createRepository(&config, nil, nil, &httpClient)
   147  
   148  		assert.EqualError(t, err, "creating repository on the ABAP system http://testHost.com:50000 failed: a http error occurred")
   149  	})
   150  }
   151  
   152  type httpMockGcts struct {
   153  	Method       string                  // is set during test execution
   154  	URL          string                  // is set before test execution
   155  	Header       map[string][]string     // is set before test execution
   156  	ResponseBody string                  // is set before test execution
   157  	Options      piperhttp.ClientOptions // is set during test
   158  	StatusCode   int                     // is set during test
   159  }
   160  
   161  func (c *httpMockGcts) SetOptions(options piperhttp.ClientOptions) {
   162  	c.Options = options
   163  }
   164  
   165  func (c *httpMockGcts) SendRequest(method string, url string, r io.Reader, header http.Header, cookies []*http.Cookie) (*http.Response, error) {
   166  
   167  	c.Method = method
   168  	c.URL = url
   169  
   170  	if r != nil {
   171  		_, err := io.ReadAll(r)
   172  
   173  		if err != nil {
   174  			return nil, err
   175  		}
   176  	}
   177  
   178  	res := http.Response{
   179  		StatusCode: c.StatusCode,
   180  		Header:     c.Header,
   181  		Body:       io.NopCloser(bytes.NewReader([]byte(c.ResponseBody))),
   182  	}
   183  
   184  	if c.StatusCode >= 400 {
   185  		return &res, errors.New("a http error occurred")
   186  	}
   187  
   188  	return &res, nil
   189  }