github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/eventing/resolver_test.go (about)

     1  package eventing
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/internal/domain/eventing/automock"
     8  	"github.com/kyma-incubator/compass/components/director/internal/domain/tenant"
     9  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    10  	persistenceautomock "github.com/kyma-incubator/compass/components/director/pkg/persistence/automock"
    11  	"github.com/kyma-incubator/compass/components/director/pkg/persistence/txtest"
    12  	"github.com/pkg/errors"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/mock"
    15  	"github.com/stretchr/testify/require"
    16  )
    17  
    18  func TestResolver_SetDefaultEventingForApplication(t *testing.T) {
    19  	// GIVEN
    20  	ctx := context.TODO()
    21  	ctx = tenant.SaveToContext(ctx, tenantID.String(), externalTenantID.String())
    22  
    23  	testErr := errors.New("this is a test error")
    24  	txGen := txtest.NewTransactionContextGenerator(testErr)
    25  
    26  	app := fixApplicationModel("test-app")
    27  
    28  	defaultEveningURL := "https://eventing.domain.local"
    29  	modelAppEventingCfg := fixModelApplicationEventingConfiguration(t, defaultEveningURL)
    30  	gqlAppEventingCfg := fixGQLApplicationEventingConfiguration(defaultEveningURL)
    31  
    32  	testCases := []struct {
    33  		Name            string
    34  		TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
    35  		EventingSvcFn   func() *automock.EventingService
    36  		AppSvcFn        func() *automock.ApplicationService
    37  		ExpectedOutput  *graphql.ApplicationEventingConfiguration
    38  		ExpectedError   error
    39  	}{
    40  		{
    41  			Name:            "Success",
    42  			TransactionerFn: txGen.ThatSucceeds,
    43  			EventingSvcFn: func() *automock.EventingService {
    44  				eventingSvc := &automock.EventingService{}
    45  				eventingSvc.On("SetForApplication", txtest.CtxWithDBMatcher(), runtimeID, app).
    46  					Return(modelAppEventingCfg, nil).Once()
    47  				return eventingSvc
    48  			},
    49  			AppSvcFn: func() *automock.ApplicationService {
    50  				appSvc := &automock.ApplicationService{}
    51  				appSvc.On("Get", txtest.CtxWithDBMatcher(), applicationID.String()).Return(&app, nil)
    52  				return appSvc
    53  			},
    54  			ExpectedOutput: gqlAppEventingCfg,
    55  			ExpectedError:  nil,
    56  		}, {
    57  			Name:            "Error when setting the runtime for the application",
    58  			TransactionerFn: txGen.ThatDoesntExpectCommit,
    59  			EventingSvcFn: func() *automock.EventingService {
    60  				eventingSvc := &automock.EventingService{}
    61  				eventingSvc.On("SetForApplication", txtest.CtxWithDBMatcher(), runtimeID, app).
    62  					Return(nil, testErr).Once()
    63  
    64  				return eventingSvc
    65  			},
    66  			AppSvcFn: func() *automock.ApplicationService {
    67  				appSvc := &automock.ApplicationService{}
    68  				appSvc.On("Get", txtest.CtxWithDBMatcher(), applicationID.String()).Return(&app, nil)
    69  				return appSvc
    70  			},
    71  			ExpectedOutput: nil,
    72  			ExpectedError:  testErr,
    73  		}, {
    74  			Name:            "Error when getting the application",
    75  			TransactionerFn: txGen.ThatDoesntExpectCommit,
    76  			EventingSvcFn: func() *automock.EventingService {
    77  				eventingSvc := &automock.EventingService{}
    78  				return eventingSvc
    79  			},
    80  			AppSvcFn: func() *automock.ApplicationService {
    81  				appSvc := &automock.ApplicationService{}
    82  				appSvc.On("Get", txtest.CtxWithDBMatcher(), applicationID.String()).Return(nil, testErr)
    83  				return appSvc
    84  			},
    85  			ExpectedOutput: nil,
    86  			ExpectedError:  testErr,
    87  		}, {
    88  			Name:            "Error when beginning transaction",
    89  			TransactionerFn: txGen.ThatFailsOnBegin,
    90  			EventingSvcFn: func() *automock.EventingService {
    91  				eventingSvc := &automock.EventingService{}
    92  				return eventingSvc
    93  			},
    94  			AppSvcFn: func() *automock.ApplicationService {
    95  				appSvc := &automock.ApplicationService{}
    96  				return appSvc
    97  			},
    98  			ExpectedOutput: nil,
    99  			ExpectedError:  testErr,
   100  		}, {
   101  			Name:            "Error when committing transaction",
   102  			TransactionerFn: txGen.ThatFailsOnCommit,
   103  			EventingSvcFn: func() *automock.EventingService {
   104  				eventingSvc := &automock.EventingService{}
   105  				eventingSvc.On("SetForApplication", txtest.CtxWithDBMatcher(), runtimeID, app).
   106  					Return(modelAppEventingCfg, nil).Once()
   107  
   108  				return eventingSvc
   109  			},
   110  			AppSvcFn: func() *automock.ApplicationService {
   111  				appSvc := &automock.ApplicationService{}
   112  				appSvc.On("Get", txtest.CtxWithDBMatcher(), applicationID.String()).Return(&app, nil)
   113  				return appSvc
   114  			},
   115  			ExpectedOutput: nil,
   116  			ExpectedError:  testErr,
   117  		},
   118  	}
   119  
   120  	for _, testCase := range testCases {
   121  		t.Run(testCase.Name, func(t *testing.T) {
   122  			persist, transact := testCase.TransactionerFn()
   123  			eventingSvc := testCase.EventingSvcFn()
   124  			appSvc := testCase.AppSvcFn()
   125  			resolver := NewResolver(transact, eventingSvc, appSvc)
   126  
   127  			// WHEN
   128  			result, err := resolver.SetEventingForApplication(ctx, applicationID.String(), runtimeID.String())
   129  
   130  			// THEN
   131  			if testCase.ExpectedError != nil {
   132  				require.Error(t, err)
   133  				require.Contains(t, err.Error(), testCase.ExpectedError.Error())
   134  			} else {
   135  				require.NoError(t, err)
   136  			}
   137  			require.Equal(t, testCase.ExpectedOutput, result)
   138  
   139  			mock.AssertExpectationsForObjects(t, eventingSvc, appSvc, transact, persist)
   140  		})
   141  	}
   142  
   143  	t.Run("Error when runtime ID is not a valid UUID", func(t *testing.T) {
   144  		// GIVEN
   145  		resolver := NewResolver(nil, nil, nil)
   146  
   147  		// WHEN
   148  		result, err := resolver.SetEventingForApplication(ctx, applicationID.String(), "abc")
   149  
   150  		// THEN
   151  		require.Error(t, err)
   152  		assert.Contains(t, err.Error(), "while parsing runtime ID as UUID")
   153  		assert.Nil(t, result)
   154  	})
   155  
   156  	t.Run("Error when application ID is not a valid UUID", func(t *testing.T) {
   157  		// GIVEN
   158  		resolver := NewResolver(nil, nil, nil)
   159  
   160  		// WHEN
   161  		result, err := resolver.SetEventingForApplication(ctx, "abc", runtimeID.String())
   162  
   163  		// THEN
   164  		require.Error(t, err)
   165  		assert.Contains(t, err.Error(), "while parsing application ID as UUID")
   166  		assert.Nil(t, result)
   167  	})
   168  }
   169  
   170  func TestResolver_UnsetDefaultEventingForApplication(t *testing.T) {
   171  	// GIVEN
   172  	ctx := context.TODO()
   173  	ctx = tenant.SaveToContext(ctx, tenantID.String(), externalTenantID.String())
   174  
   175  	app := fixApplicationModel("test-app")
   176  
   177  	testErr := errors.New("this is a test error")
   178  	txGen := txtest.NewTransactionContextGenerator(testErr)
   179  
   180  	defaultEveningURL := "https://eventing.domain.local/test-app/events/v1"
   181  	modelAppEventingCfg := fixModelApplicationEventingConfiguration(t, defaultEveningURL)
   182  	gqlAppEventingCfg := fixGQLApplicationEventingConfiguration(defaultEveningURL)
   183  
   184  	testCases := []struct {
   185  		Name            string
   186  		TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
   187  		EventingSvcFn   func() *automock.EventingService
   188  		AppSvcFn        func() *automock.ApplicationService
   189  		ExpectedOutput  *graphql.ApplicationEventingConfiguration
   190  		ExpectedError   error
   191  	}{
   192  		{
   193  			Name:            "Success",
   194  			TransactionerFn: txGen.ThatSucceeds,
   195  			EventingSvcFn: func() *automock.EventingService {
   196  				eventingSvc := &automock.EventingService{}
   197  				eventingSvc.On("UnsetForApplication", txtest.CtxWithDBMatcher(), app).
   198  					Return(modelAppEventingCfg, nil).Once()
   199  
   200  				return eventingSvc
   201  			},
   202  			AppSvcFn: func() *automock.ApplicationService {
   203  				appSvc := &automock.ApplicationService{}
   204  				appSvc.On("Get", txtest.CtxWithDBMatcher(), applicationID.String()).Return(&app, nil)
   205  				return appSvc
   206  			},
   207  			ExpectedOutput: gqlAppEventingCfg,
   208  			ExpectedError:  nil,
   209  		}, {
   210  			Name:            "Error when deleting the default eventing runtime for the application",
   211  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   212  			EventingSvcFn: func() *automock.EventingService {
   213  				eventingSvc := &automock.EventingService{}
   214  				eventingSvc.On("UnsetForApplication", txtest.CtxWithDBMatcher(), app).
   215  					Return(nil, testErr).Once()
   216  
   217  				return eventingSvc
   218  			},
   219  			AppSvcFn: func() *automock.ApplicationService {
   220  				appSvc := &automock.ApplicationService{}
   221  				appSvc.On("Get", txtest.CtxWithDBMatcher(), applicationID.String()).Return(&app, nil)
   222  				return appSvc
   223  			},
   224  			ExpectedOutput: nil,
   225  			ExpectedError:  testErr,
   226  		}, {
   227  			Name:            "Error when getting application",
   228  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   229  			EventingSvcFn: func() *automock.EventingService {
   230  				eventingSvc := &automock.EventingService{}
   231  				return eventingSvc
   232  			},
   233  			AppSvcFn: func() *automock.ApplicationService {
   234  				appSvc := &automock.ApplicationService{}
   235  				appSvc.On("Get", txtest.CtxWithDBMatcher(), applicationID.String()).Return(nil, testErr)
   236  				return appSvc
   237  			},
   238  			ExpectedOutput: nil,
   239  			ExpectedError:  testErr,
   240  		}, {
   241  			Name:            "Error when beginning transaction",
   242  			TransactionerFn: txGen.ThatFailsOnBegin,
   243  			EventingSvcFn: func() *automock.EventingService {
   244  				eventingSvc := &automock.EventingService{}
   245  				return eventingSvc
   246  			},
   247  			AppSvcFn: func() *automock.ApplicationService {
   248  				appSvc := &automock.ApplicationService{}
   249  				return appSvc
   250  			},
   251  			ExpectedOutput: nil,
   252  			ExpectedError:  testErr,
   253  		}, {
   254  			Name:            "Error when committing transaction",
   255  			TransactionerFn: txGen.ThatFailsOnCommit,
   256  			EventingSvcFn: func() *automock.EventingService {
   257  				eventingSvc := &automock.EventingService{}
   258  				eventingSvc.On("UnsetForApplication", txtest.CtxWithDBMatcher(), app).
   259  					Return(modelAppEventingCfg, nil).Once()
   260  				return eventingSvc
   261  			},
   262  			AppSvcFn: func() *automock.ApplicationService {
   263  				appSvc := &automock.ApplicationService{}
   264  				appSvc.On("Get", txtest.CtxWithDBMatcher(), applicationID.String()).Return(&app, nil).Once()
   265  				return appSvc
   266  			},
   267  			ExpectedOutput: nil,
   268  			ExpectedError:  testErr,
   269  		},
   270  	}
   271  
   272  	for _, testCase := range testCases {
   273  		t.Run(testCase.Name, func(t *testing.T) {
   274  			persist, transact := testCase.TransactionerFn()
   275  			eventingSvc := testCase.EventingSvcFn()
   276  			appSvc := testCase.AppSvcFn()
   277  			resolver := NewResolver(transact, eventingSvc, appSvc)
   278  
   279  			// WHEN
   280  			result, err := resolver.UnsetEventingForApplication(ctx, applicationID.String())
   281  
   282  			// THEN
   283  			if testCase.ExpectedError != nil {
   284  				require.Error(t, err)
   285  				require.Contains(t, err.Error(), testCase.ExpectedError.Error())
   286  			} else {
   287  				require.NoError(t, err)
   288  			}
   289  			require.Equal(t, testCase.ExpectedOutput, result)
   290  
   291  			mock.AssertExpectationsForObjects(t, eventingSvc, appSvc, transact, persist)
   292  		})
   293  	}
   294  
   295  	t.Run("Error when application ID is not a valid UUID", func(t *testing.T) {
   296  		// GIVEN
   297  		resolver := NewResolver(nil, nil, nil)
   298  
   299  		// WHEN
   300  		result, err := resolver.UnsetEventingForApplication(ctx, "abc")
   301  
   302  		// THEN
   303  		require.Error(t, err)
   304  		assert.Contains(t, err.Error(), "while parsing application ID as UUID")
   305  		assert.Nil(t, result)
   306  	})
   307  }