github.com/xgoffin/jenkins-library@v1.154.0/cmd/gctsCreateRepository_test.go (about)

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