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

     1  package cmd
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/SAP/jenkins-library/pkg/mock"
     6  	"github.com/SAP/jenkins-library/pkg/transportrequest/rfc"
     7  	"github.com/stretchr/testify/assert"
     8  	"testing"
     9  )
    10  
    11  type transportRequestUploadRFCMockUtils struct {
    12  	*mock.ExecMockRunner
    13  }
    14  
    15  func newTransportRequestUploadRFCTestsUtils() transportRequestUploadRFCMockUtils {
    16  	utils := transportRequestUploadRFCMockUtils{
    17  		ExecMockRunner: &mock.ExecMockRunner{},
    18  	}
    19  	return utils
    20  }
    21  
    22  type uploadMock struct {
    23  	received     rfc.UploadAction
    24  	uploadCalled bool
    25  	failWith     error
    26  }
    27  
    28  // WithApplicationURL The location of the deployable
    29  func (m *uploadMock) WithApplicationURL(z string) {
    30  	m.received.ApplicationURL = z
    31  }
    32  
    33  // WithTransportRequestID The transport request ID for the upload
    34  func (m *uploadMock) WithTransportRequestID(t string) {
    35  	m.received.TransportRequestID = t
    36  }
    37  
    38  // WithApplication Everything we need to know about the application
    39  func (m *uploadMock) WithApplication(a rfc.Application) {
    40  	m.received.Application = a
    41  }
    42  
    43  // WithConfiguration Everything we need to know in order to perform the upload
    44  func (m *uploadMock) WithConfiguration(c rfc.UploadConfig) {
    45  	m.received.Configuration = c
    46  }
    47  
    48  // WithConnection Everything we need to know about the connection
    49  func (m *uploadMock) WithConnection(c rfc.Connection) {
    50  	m.received.Connection = c
    51  }
    52  
    53  func (m *uploadMock) Perform(exec rfc.Exec) error {
    54  	m.uploadCalled = true
    55  	return m.failWith
    56  }
    57  
    58  type configMock struct {
    59  	config *transportRequestUploadRFCOptions
    60  }
    61  
    62  func TestTrRfcRunTransportRequestUpload(t *testing.T) {
    63  	t.Parallel()
    64  
    65  	t.Run("good", func(t *testing.T) {
    66  		t.Parallel()
    67  
    68  		utils := newTransportRequestUploadRFCTestsUtils()
    69  		configMock := newRfcConfigMock()
    70  		actionMock := uploadMock{}
    71  		cpe := &transportRequestUploadRFCCommonPipelineEnvironment{}
    72  
    73  		err := runTransportRequestUploadRFC(configMock.config, &actionMock, nil, utils, cpe)
    74  
    75  		if assert.NoError(t, err) {
    76  			t.Run("upload triggered", func(t *testing.T) {
    77  				assert.True(t, actionMock.uploadCalled)
    78  			})
    79  			t.Run("parameters has been marshalled", func(t *testing.T) {
    80  				assert.Equal(t, rfc.UploadAction{
    81  					Connection: rfc.Connection{
    82  						Endpoint: "https://my.abap.server",
    83  						Client:   "001",
    84  						Instance: "00",
    85  						User:     "me",
    86  						Password: "******",
    87  					},
    88  					Application: rfc.Application{
    89  						Name:        "MyApp",
    90  						Description: "Lorem impsum",
    91  						AbapPackage: "ABC",
    92  					},
    93  					Configuration: rfc.UploadConfig{
    94  						AcceptUnixStyleEndOfLine: true,
    95  						CodePage:                 "UTF-8",
    96  						FailUploadOnWarning:      true,
    97  						Verbose:                  false, // comes from general config
    98  					},
    99  					TransportRequestID: "K12345678",
   100  					ApplicationURL:     "http://example.org/myDeployable.zip",
   101  				}, actionMock.received)
   102  
   103  				assert.Equal(t, cpe.custom.transportRequestID, "K12345678")
   104  			})
   105  		}
   106  	})
   107  	t.Run("bad", func(t *testing.T) {
   108  		t.Parallel()
   109  
   110  		t.Run("Error during deployment", func(t *testing.T) {
   111  			utilsMock := newTransportRequestUploadSOLMANTestsUtils(0)
   112  			configMock := newRfcConfigMock()
   113  			actionMock := uploadMock{failWith: fmt.Errorf("upload failed")}
   114  			cpe := &transportRequestUploadRFCCommonPipelineEnvironment{}
   115  
   116  			err := runTransportRequestUploadRFC(configMock.config, &actionMock, nil, utilsMock, cpe)
   117  
   118  			assert.Error(t, err, "upload failed")
   119  		})
   120  	})
   121  }
   122  
   123  func newRfcConfigMock() *configMock {
   124  	return &configMock{
   125  		config: &transportRequestUploadRFCOptions{
   126  			Endpoint:                   "https://my.abap.server",
   127  			Client:                     "001",
   128  			Instance:                   "00",
   129  			Username:                   "me",
   130  			Password:                   "******",
   131  			ApplicationName:            "MyApp",
   132  			ApplicationDescription:     "Lorem impsum",
   133  			AbapPackage:                "ABC",
   134  			ApplicationURL:             "http://example.org/myDeployable.zip",
   135  			CodePage:                   "UTF-8",
   136  			AcceptUnixStyleLineEndings: true,
   137  			FailUploadOnWarning:        true,
   138  			TransportRequestID:         "K12345678",
   139  		},
   140  	}
   141  }