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

     1  package eventing
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"net/url"
     7  	"testing"
     8  
     9  	"github.com/kyma-incubator/compass/components/director/internal/labelfilter"
    10  
    11  	"github.com/kyma-incubator/compass/components/director/pkg/normalizer"
    12  
    13  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
    14  
    15  	"github.com/google/uuid"
    16  	"github.com/kyma-incubator/compass/components/director/internal/domain/eventing/automock"
    17  	"github.com/kyma-incubator/compass/components/director/internal/model"
    18  	"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
    19  	"github.com/pkg/errors"
    20  	"github.com/stretchr/testify/mock"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  var (
    25  	appNameNormalizer = &normalizer.DefaultNormalizator{}
    26  	app               = fixApplicationModel("test-app")
    27  )
    28  
    29  func Test_CleanupAfterUnregisteringApplication(t *testing.T) {
    30  	t.Run("Success when cleanup does not return errors", func(t *testing.T) {
    31  		// GIVEN
    32  		ctx := fixCtxWithTenant()
    33  		labelRepo := &automock.LabelRepository{}
    34  		labelRepo.On("DeleteByKey", ctx, tenantID.String(), getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
    35  
    36  		svc := NewService(nil, nil, labelRepo)
    37  
    38  		// WHEN
    39  		eventingCfg, err := svc.CleanupAfterUnregisteringApplication(ctx, applicationID)
    40  
    41  		// THEN
    42  
    43  		require.NoError(t, err)
    44  		require.NotNil(t, eventingCfg)
    45  		require.Equal(t, "", eventingCfg.DefaultURL.String())
    46  		mock.AssertExpectationsForObjects(t, labelRepo)
    47  	})
    48  
    49  	t.Run("Error when tenant not in context", func(t *testing.T) {
    50  		// GIVEN
    51  		svc := NewService(nil, nil, nil)
    52  
    53  		// WHEN
    54  		_, err := svc.CleanupAfterUnregisteringApplication(context.TODO(), uuid.Nil)
    55  
    56  		// THEN
    57  		require.Error(t, err)
    58  		require.EqualError(t, err, "while loading tenant from context: cannot read tenant from context")
    59  	})
    60  
    61  	t.Run("Error when cleanup returns errors", func(t *testing.T) {
    62  		// GIVEN
    63  		expectedError := fmt.Sprintf(`while deleting Labels for Application with id %s: some-error`, applicationID)
    64  		ctx := fixCtxWithTenant()
    65  		labelRepo := &automock.LabelRepository{}
    66  		labelRepo.On("DeleteByKey", ctx, tenantID.String(), getDefaultEventingForAppLabelKey(applicationID)).Return(errors.New("some-error"))
    67  
    68  		svc := NewService(nil, nil, labelRepo)
    69  
    70  		// WHEN
    71  		_, err := svc.CleanupAfterUnregisteringApplication(ctx, applicationID)
    72  
    73  		// THEN
    74  
    75  		require.Error(t, err)
    76  		require.EqualError(t, err, expectedError)
    77  		mock.AssertExpectationsForObjects(t, labelRepo)
    78  	})
    79  }
    80  
    81  func Test_SetForApplication(t *testing.T) {
    82  	appEventURL := fixAppEventURL(t, app.Name)
    83  	appNormalizedEventURL := fixAppEventURL(t, appNameNormalizer.Normalize(app.Name))
    84  
    85  	t.Run("Success when assigning new default runtime, when there was no previous one", func(t *testing.T) {
    86  		// GIVEN
    87  		ctx := fixCtxWithTenant()
    88  		app := fixApplicationModel("test-app")
    89  		runtimeRepo := &automock.RuntimeRepository{}
    90  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
    91  			1, mock.Anything).Return(fixEmptyRuntimePage(), nil)
    92  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
    93  			fixLabelFilterForRuntimeScenarios()).Return(fixRuntimes()[0], nil)
    94  		labelRepo := &automock.LabelRepository{}
    95  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
    96  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
    97  		labelRepo.On("Upsert", ctx, tenantID.String(), mock.MatchedBy(fixMatcherDefaultEventingForAppLabel())).Return(nil)
    98  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
    99  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   100  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   101  			runtimeID.String(), isNormalizedLabel).Return(nil, apperrors.NewNotFoundError(resource.Runtime, runtimeID.String()))
   102  
   103  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   104  
   105  		// WHEN
   106  		eventingCfg, err := svc.SetForApplication(ctx, runtimeID, app)
   107  
   108  		// THEN
   109  		require.NoError(t, err)
   110  		require.NotNil(t, eventingCfg)
   111  		require.Equal(t, appNormalizedEventURL, eventingCfg.DefaultURL)
   112  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   113  	})
   114  
   115  	t.Run("Success when assigning new default runtime, when there is already one assigned", func(t *testing.T) {
   116  		// GIVEN
   117  		ctx := fixCtxWithTenant()
   118  		runtimeRepo := &automock.RuntimeRepository{}
   119  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   120  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   121  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
   122  			fixLabelFilterForRuntimeScenarios()).Return(fixRuntimes()[0], nil)
   123  		labelRepo := &automock.LabelRepository{}
   124  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   125  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   126  		labelRepo.On("Upsert", ctx, tenantID.String(), mock.MatchedBy(fixMatcherDefaultEventingForAppLabel())).Return(nil)
   127  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   128  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   129  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   130  			getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   131  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   132  			runtimeID.String(), isNormalizedLabel).Return(nil, apperrors.NewNotFoundError(resource.Runtime, runtimeID.String()))
   133  
   134  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   135  
   136  		// WHEN
   137  		eventingCfg, err := svc.SetForApplication(ctx, runtimeID, app)
   138  
   139  		// THEN
   140  		require.NoError(t, err)
   141  		require.NotNil(t, eventingCfg)
   142  		require.Equal(t, appNormalizedEventURL, eventingCfg.DefaultURL)
   143  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   144  	})
   145  
   146  	t.Run("Success when there is runtime labeled for application eventing and is labeled for normalization", func(t *testing.T) {
   147  		// GIVEN
   148  		ctx := fixCtxWithTenant()
   149  		app := fixApplicationModel("test-app")
   150  		runtimeRepo := &automock.RuntimeRepository{}
   151  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   152  			1, mock.Anything).Return(fixEmptyRuntimePage(), nil)
   153  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
   154  			fixLabelFilterForRuntimeScenarios()).Return(fixRuntimes()[0], nil)
   155  		labelRepo := &automock.LabelRepository{}
   156  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   157  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   158  		labelRepo.On("Upsert", ctx, tenantID.String(), mock.MatchedBy(fixMatcherDefaultEventingForAppLabel())).Return(nil)
   159  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   160  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   161  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   162  			runtimeID.String(), isNormalizedLabel).Return(&model.Label{Value: "true"}, nil)
   163  
   164  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   165  
   166  		// WHEN
   167  		eventingCfg, err := svc.SetForApplication(ctx, runtimeID, app)
   168  
   169  		// THEN
   170  		require.NoError(t, err)
   171  		require.NotNil(t, eventingCfg)
   172  		require.Equal(t, appNormalizedEventURL, eventingCfg.DefaultURL)
   173  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   174  	})
   175  
   176  	t.Run("Success when there is runtime labeled for application eventing and is labeled not for normalization", func(t *testing.T) {
   177  		// GIVEN
   178  		ctx := fixCtxWithTenant()
   179  		app := fixApplicationModel("test-app")
   180  		runtimeRepo := &automock.RuntimeRepository{}
   181  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   182  			1, mock.Anything).Return(fixEmptyRuntimePage(), nil)
   183  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
   184  			fixLabelFilterForRuntimeScenarios()).Return(fixRuntimes()[0], nil)
   185  		labelRepo := &automock.LabelRepository{}
   186  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   187  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   188  		labelRepo.On("Upsert", ctx, tenantID.String(), mock.MatchedBy(fixMatcherDefaultEventingForAppLabel())).Return(nil)
   189  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   190  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   191  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   192  			runtimeID.String(), isNormalizedLabel).Return(&model.Label{Value: "false"}, nil)
   193  
   194  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   195  
   196  		// WHEN
   197  		eventingCfg, err := svc.SetForApplication(ctx, runtimeID, app)
   198  
   199  		// THEN
   200  		require.NoError(t, err)
   201  		require.NotNil(t, eventingCfg)
   202  		require.Equal(t, appEventURL, eventingCfg.DefaultURL)
   203  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   204  	})
   205  
   206  	t.Run("Error when tenant not in context", func(t *testing.T) {
   207  		// GIVEN
   208  		svc := NewService(nil, nil, nil)
   209  
   210  		// WHEN
   211  		_, err := svc.SetForApplication(context.TODO(), uuid.Nil, model.Application{})
   212  
   213  		// THEN
   214  		require.Error(t, err)
   215  		require.EqualError(t, err, "while loading tenant from context: cannot read tenant from context")
   216  	})
   217  
   218  	t.Run("Error when deleting existing default runtime, when getting current default runtime", func(t *testing.T) {
   219  		// GIVEN
   220  		expectedError := fmt.Sprintf(`while deleting default eventing for application: while getting default runtime for app eventing: while fetching runtimes with label [key=%s]: some-error`, getDefaultEventingForAppLabelKey(applicationID))
   221  		ctx := fixCtxWithTenant()
   222  		runtimeRepo := &automock.RuntimeRepository{}
   223  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   224  			1, mock.Anything).Return(nil, errors.New("some-error"))
   225  		labelRepo := &automock.LabelRepository{}
   226  
   227  		svc := NewService(nil, runtimeRepo, labelRepo)
   228  
   229  		// WHEN
   230  		_, err := svc.SetForApplication(ctx, runtimeID, app)
   231  
   232  		// THEN
   233  		require.Error(t, err)
   234  		require.EqualError(t, err, expectedError)
   235  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   236  	})
   237  
   238  	t.Run("Error when deleting existing default runtime, when getting current default runtime repository returns more than one runtime", func(t *testing.T) {
   239  		// GIVEN
   240  		expectedError := fmt.Sprintf(`while deleting default eventing for application: while getting default runtime for app eventing: got multpile runtimes labeled [key=%s] as default for eventing`, getDefaultEventingForAppLabelKey(applicationID))
   241  		ctx := fixCtxWithTenant()
   242  		runtimeRepo := &automock.RuntimeRepository{}
   243  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   244  			1, mock.Anything).Return(fixRuntimePage(), nil)
   245  		labelRepo := &automock.LabelRepository{}
   246  
   247  		svc := NewService(nil, runtimeRepo, labelRepo)
   248  
   249  		// WHEN
   250  		_, err := svc.SetForApplication(ctx, runtimeID, app)
   251  
   252  		// THEN
   253  		require.Error(t, err)
   254  		require.EqualError(t, err, expectedError)
   255  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   256  	})
   257  
   258  	t.Run("Error when deleting existing default runtime", func(t *testing.T) {
   259  		// GIVEN
   260  		expectedError := fmt.Sprintf(`while deleting default eventing for application: while deleting label: while deleting label [key=%s, runtimeID=%s]: some-error`, getDefaultEventingForAppLabelKey(applicationID), runtimeID)
   261  		ctx := fixCtxWithTenant()
   262  		runtimeRepo := &automock.RuntimeRepository{}
   263  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   264  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   265  		labelRepo := &automock.LabelRepository{}
   266  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   267  			getDefaultEventingForAppLabelKey(applicationID)).Return(errors.New("some-error"))
   268  
   269  		svc := NewService(nil, runtimeRepo, labelRepo)
   270  
   271  		// WHEN
   272  		_, err := svc.SetForApplication(ctx, runtimeID, app)
   273  
   274  		// THEN
   275  		require.Error(t, err)
   276  		require.EqualError(t, err, expectedError)
   277  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   278  	})
   279  
   280  	t.Run("Error when getting scenarios returns error", func(t *testing.T) {
   281  		// GIVEN
   282  		expectedError := fmt.Sprintf(`while getting the runtime: while getting application scenarios: while getting the label [key=%s] for application [ID=%s]: some error`, model.ScenariosKey, applicationID)
   283  		ctx := fixCtxWithTenant()
   284  		runtimeRepo := &automock.RuntimeRepository{}
   285  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   286  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   287  		labelRepo := &automock.LabelRepository{}
   288  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   289  			getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   290  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   291  			applicationID.String(), model.ScenariosKey).Return(nil, errors.New("some error"))
   292  
   293  		svc := NewService(nil, runtimeRepo, labelRepo)
   294  
   295  		// WHEN
   296  		_, err := svc.SetForApplication(ctx, runtimeID, app)
   297  
   298  		// THEN
   299  		require.Error(t, err)
   300  		require.EqualError(t, err, expectedError)
   301  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   302  	})
   303  
   304  	t.Run("Error when given runtime does not belong to the application scenarios", func(t *testing.T) {
   305  		// GIVEN
   306  		expectedError := fmt.Sprintf(`does not find the given runtime [ID=%s] assigned to the application scenarios`, runtimeID)
   307  		ctx := fixCtxWithTenant()
   308  		runtimeRepo := &automock.RuntimeRepository{}
   309  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   310  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   311  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
   312  			fixLabelFilterForRuntimeScenarios()).Return(nil, apperrors.NewNotFoundError(resource.Runtime, ""))
   313  		labelRepo := &automock.LabelRepository{}
   314  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   315  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   316  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   317  			getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   318  
   319  		svc := NewService(nil, runtimeRepo, labelRepo)
   320  
   321  		// WHEN
   322  		_, err := svc.SetForApplication(ctx, runtimeID, app)
   323  
   324  		// THEN
   325  		require.Error(t, err)
   326  		require.EqualError(t, err, expectedError)
   327  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   328  	})
   329  
   330  	t.Run("Error when getting new runtime to assign as default", func(t *testing.T) {
   331  		// GIVEN
   332  		expectedError := fmt.Sprintf(`while getting the runtime: while getting the runtime [ID=%s] with scenarios with filter: some-error`, runtimeID)
   333  		ctx := fixCtxWithTenant()
   334  		runtimeRepo := &automock.RuntimeRepository{}
   335  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   336  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   337  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
   338  			fixLabelFilterForRuntimeScenarios()).Return(nil, errors.New("some-error"))
   339  		labelRepo := &automock.LabelRepository{}
   340  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   341  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   342  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   343  			getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   344  
   345  		svc := NewService(nil, runtimeRepo, labelRepo)
   346  
   347  		// WHEN
   348  		_, err := svc.SetForApplication(ctx, runtimeID, app)
   349  
   350  		// THEN
   351  		require.Error(t, err)
   352  		require.EqualError(t, err, expectedError)
   353  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   354  	})
   355  
   356  	t.Run("Error when assigning new default runtime", func(t *testing.T) {
   357  		// GIVEN
   358  		expectedError := fmt.Sprintf(`while setting the runtime as default for eventing for application: while labeling the runtime [ID=%s] as default for eventing for application [ID=%s]: some-error`, runtimeID, applicationID)
   359  		ctx := fixCtxWithTenant()
   360  		runtimeRepo := &automock.RuntimeRepository{}
   361  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   362  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   363  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
   364  			fixLabelFilterForRuntimeScenarios()).Return(fixRuntimes()[0], nil)
   365  		labelRepo := &automock.LabelRepository{}
   366  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   367  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   368  		labelRepo.On("Upsert", ctx, tenantID.String(), mock.MatchedBy(fixMatcherDefaultEventingForAppLabel())).Return(errors.New("some-error"))
   369  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   370  			getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   371  
   372  		svc := NewService(nil, runtimeRepo, labelRepo)
   373  
   374  		// WHEN
   375  		_, err := svc.SetForApplication(ctx, runtimeID, app)
   376  
   377  		// THEN
   378  		require.Error(t, err)
   379  		require.EqualError(t, err, expectedError)
   380  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   381  	})
   382  
   383  	t.Run("Error when getting eventing configuration for a given runtime", func(t *testing.T) {
   384  		// GIVEN
   385  		expectedError := fmt.Sprintf(`while fetching eventing configuration for runtime: while getting the label [key=%s] for runtime [ID=%s]: some-error`, RuntimeEventingURLLabel, runtimeID)
   386  		ctx := fixCtxWithTenant()
   387  		runtimeRepo := &automock.RuntimeRepository{}
   388  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   389  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   390  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
   391  			fixLabelFilterForRuntimeScenarios()).Return(fixRuntimes()[0], nil)
   392  		labelRepo := &automock.LabelRepository{}
   393  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   394  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   395  		labelRepo.On("Upsert", ctx, tenantID.String(), mock.MatchedBy(fixMatcherDefaultEventingForAppLabel())).Return(nil)
   396  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   397  			runtimeID.String(), RuntimeEventingURLLabel).Return(nil, errors.New("some-error"))
   398  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   399  			getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   400  
   401  		svc := NewService(nil, runtimeRepo, labelRepo)
   402  
   403  		// WHEN
   404  		_, err := svc.SetForApplication(ctx, runtimeID, app)
   405  
   406  		// THEN
   407  		require.Error(t, err)
   408  		require.EqualError(t, err, expectedError)
   409  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   410  	})
   411  
   412  	t.Run("Error when getting runtime normalization label", func(t *testing.T) {
   413  		// GIVEN
   414  		expectedError := fmt.Sprintf(`while determining whether application name should be normalized in runtime eventing URL: while getting the label [key=%s] for runtime [ID=%s]: some error`, isNormalizedLabel, runtimeID)
   415  		ctx := fixCtxWithTenant()
   416  		app := fixApplicationModel("test-app")
   417  		runtimeRepo := &automock.RuntimeRepository{}
   418  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   419  			1, mock.Anything).Return(fixEmptyRuntimePage(), nil)
   420  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
   421  			fixLabelFilterForRuntimeScenarios()).Return(fixRuntimes()[0], nil)
   422  		labelRepo := &automock.LabelRepository{}
   423  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   424  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   425  		labelRepo.On("Upsert", ctx, tenantID.String(), mock.MatchedBy(fixMatcherDefaultEventingForAppLabel())).Return(nil)
   426  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   427  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   428  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   429  			runtimeID.String(), isNormalizedLabel).Return(nil, errors.New("some error"))
   430  
   431  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   432  
   433  		// WHEN
   434  		_, err := svc.SetForApplication(ctx, runtimeID, app)
   435  
   436  		// THEN
   437  		require.Error(t, err)
   438  		require.EqualError(t, err, expectedError)
   439  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   440  	})
   441  }
   442  
   443  func Test_UnsetForApplication(t *testing.T) {
   444  	appEventURL := fixAppEventURL(t, app.Name)
   445  	appNormalizedEventURL := fixAppEventURL(t, appNameNormalizer.Normalize(app.Name))
   446  
   447  	t.Run("Success when there is no default runtime assigned for eventing", func(t *testing.T) {
   448  		// GIVEN
   449  		ctx := fixCtxWithTenant()
   450  		runtimeRepo := &automock.RuntimeRepository{}
   451  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   452  			1, mock.Anything).Return(fixEmptyRuntimePage(), nil)
   453  
   454  		svc := NewService(nil, runtimeRepo, nil)
   455  
   456  		// WHEN
   457  		eventingCfg, err := svc.UnsetForApplication(ctx, app)
   458  
   459  		// THEN
   460  		require.NoError(t, err)
   461  		require.NotNil(t, eventingCfg)
   462  		require.Equal(t, EmptyEventingURL, eventingCfg.DefaultURL.String())
   463  		mock.AssertExpectationsForObjects(t, runtimeRepo)
   464  	})
   465  
   466  	t.Run("Success when there is default runtime assigned for eventing", func(t *testing.T) {
   467  		// GIVEN
   468  		ctx := fixCtxWithTenant()
   469  		runtimeRepo := &automock.RuntimeRepository{}
   470  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   471  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   472  		labelRepo := &automock.LabelRepository{}
   473  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   474  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   475  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   476  			getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   477  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   478  			runtimeID.String(), isNormalizedLabel).Return(nil, apperrors.NewNotFoundError(resource.Runtime, runtimeID.String()))
   479  
   480  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   481  
   482  		// WHEN
   483  		eventingCfg, err := svc.UnsetForApplication(ctx, app)
   484  
   485  		// THEN
   486  		require.NoError(t, err)
   487  		require.NotNil(t, eventingCfg)
   488  		require.Equal(t, appNormalizedEventURL, eventingCfg.DefaultURL)
   489  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   490  	})
   491  
   492  	t.Run("Success when there is runtime labeled for application eventing and is labeled for normalization", func(t *testing.T) {
   493  		// GIVEN
   494  		ctx := fixCtxWithTenant()
   495  		runtimeRepo := &automock.RuntimeRepository{}
   496  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   497  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   498  		labelRepo := &automock.LabelRepository{}
   499  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   500  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   501  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   502  			getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   503  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   504  			runtimeID.String(), isNormalizedLabel).Return(&model.Label{Value: "true"}, nil)
   505  
   506  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   507  
   508  		// WHEN
   509  		eventingCfg, err := svc.UnsetForApplication(ctx, app)
   510  
   511  		// THEN
   512  		require.NoError(t, err)
   513  		require.NotNil(t, eventingCfg)
   514  		require.Equal(t, appNormalizedEventURL, eventingCfg.DefaultURL)
   515  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   516  	})
   517  
   518  	t.Run("Success when there is runtime labeled for application eventing and is labeled not for normalization", func(t *testing.T) {
   519  		// GIVEN
   520  		ctx := fixCtxWithTenant()
   521  		runtimeRepo := &automock.RuntimeRepository{}
   522  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   523  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   524  		labelRepo := &automock.LabelRepository{}
   525  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   526  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   527  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   528  			getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   529  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   530  			runtimeID.String(), isNormalizedLabel).Return(&model.Label{Value: "false"}, nil)
   531  
   532  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   533  
   534  		// WHEN
   535  		eventingCfg, err := svc.UnsetForApplication(ctx, app)
   536  
   537  		// THEN
   538  		require.NoError(t, err)
   539  		require.NotNil(t, eventingCfg)
   540  		require.Equal(t, appEventURL, eventingCfg.DefaultURL)
   541  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   542  	})
   543  
   544  	t.Run("Error when tenant not in context", func(t *testing.T) {
   545  		// GIVEN
   546  		svc := NewService(nil, nil, nil)
   547  
   548  		// WHEN
   549  		_, err := svc.UnsetForApplication(context.TODO(), app)
   550  
   551  		// THEN
   552  		require.Error(t, err)
   553  		require.EqualError(t, err, "while loading tenant from context: cannot read tenant from context")
   554  	})
   555  
   556  	t.Run("Error when getting default runtime assigned for eventing returns error", func(t *testing.T) {
   557  		// GIVEN
   558  		expectedError := fmt.Sprintf(`while deleting default eventing for application: while getting default runtime for app eventing: while fetching runtimes with label [key=%s]: some-error`, getDefaultEventingForAppLabelKey(applicationID))
   559  		ctx := fixCtxWithTenant()
   560  		runtimeRepo := &automock.RuntimeRepository{}
   561  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   562  			1, mock.Anything).Return(nil, errors.New("some-error"))
   563  
   564  		svc := NewService(nil, runtimeRepo, nil)
   565  
   566  		// WHEN
   567  		_, err := svc.UnsetForApplication(ctx, app)
   568  
   569  		// THEN
   570  		require.Error(t, err)
   571  		require.EqualError(t, err, expectedError)
   572  		mock.AssertExpectationsForObjects(t, runtimeRepo)
   573  	})
   574  
   575  	t.Run("Error when getting default runtime assigned for eventing returns more than one element", func(t *testing.T) {
   576  		// GIVEN
   577  		expectedError := fmt.Sprintf(`while deleting default eventing for application: while getting default runtime for app eventing: got multpile runtimes labeled [key=%s] as default for eventing`, getDefaultEventingForAppLabelKey(applicationID))
   578  		ctx := fixCtxWithTenant()
   579  		runtimeRepo := &automock.RuntimeRepository{}
   580  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   581  			1, mock.Anything).Return(fixRuntimePage(), nil)
   582  
   583  		svc := NewService(nil, runtimeRepo, nil)
   584  
   585  		// WHEN
   586  		_, err := svc.UnsetForApplication(ctx, app)
   587  
   588  		// THEN
   589  		require.Error(t, err)
   590  		require.EqualError(t, err, expectedError)
   591  		mock.AssertExpectationsForObjects(t, runtimeRepo)
   592  	})
   593  
   594  	t.Run("Error when deleting default app eventing label", func(t *testing.T) {
   595  		// GIVEN
   596  		expectedError := fmt.Sprintf(`while deleting default eventing for application: while deleting label: while deleting label [key=%s, runtimeID=%s]: some-error`, getDefaultEventingForAppLabelKey(applicationID), runtimeID)
   597  		ctx := fixCtxWithTenant()
   598  		runtimeRepo := &automock.RuntimeRepository{}
   599  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   600  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   601  		labelRepo := &automock.LabelRepository{}
   602  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   603  			getDefaultEventingForAppLabelKey(applicationID)).Return(errors.New("some-error"))
   604  
   605  		svc := NewService(nil, runtimeRepo, labelRepo)
   606  
   607  		// WHEN
   608  		_, err := svc.UnsetForApplication(ctx, app)
   609  
   610  		// THEN
   611  		require.Error(t, err)
   612  		require.EqualError(t, err, expectedError)
   613  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   614  	})
   615  
   616  	t.Run("Error when getting eventing configuration for a deleted default runtime", func(t *testing.T) {
   617  		// GIVEN
   618  		expectedError := fmt.Sprintf(`while fetching eventing configuration for runtime: while getting the label [key=%s] for runtime [ID=%s]: some error`, RuntimeEventingURLLabel, runtimeID)
   619  		ctx := fixCtxWithTenant()
   620  		runtimeRepo := &automock.RuntimeRepository{}
   621  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   622  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   623  		labelRepo := &automock.LabelRepository{}
   624  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   625  			runtimeID.String(), RuntimeEventingURLLabel).Return(nil, errors.New("some error"))
   626  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   627  			getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   628  
   629  		svc := NewService(nil, runtimeRepo, labelRepo)
   630  
   631  		// WHEN
   632  		_, err := svc.UnsetForApplication(ctx, app)
   633  
   634  		// THEN
   635  		require.Error(t, err)
   636  		require.EqualError(t, err, expectedError)
   637  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   638  	})
   639  
   640  	t.Run("Success when there is runtime labeled for application eventing and is labeled not for normalization", func(t *testing.T) {
   641  		// GIVEN
   642  		expectedError := fmt.Sprintf(`while determining whether application name should be normalized in runtime eventing URL: while getting the label [key=%s] for runtime [ID=%s]: some error`, isNormalizedLabel, runtimeID)
   643  		ctx := fixCtxWithTenant()
   644  		runtimeRepo := &automock.RuntimeRepository{}
   645  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   646  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   647  		labelRepo := &automock.LabelRepository{}
   648  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   649  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   650  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   651  			getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   652  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   653  			runtimeID.String(), isNormalizedLabel).Return(nil, errors.New("some error"))
   654  
   655  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   656  
   657  		// WHEN
   658  		_, err := svc.UnsetForApplication(ctx, app)
   659  
   660  		// THEN
   661  		require.Error(t, err)
   662  		require.EqualError(t, err, expectedError)
   663  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   664  	})
   665  }
   666  
   667  func Test_GetForApplication(t *testing.T) {
   668  	appEventURL := fixAppEventURL(t, app.Name)
   669  	appNormalizedEventURL := fixAppEventURL(t, appNameNormalizer.Normalize(app.Name))
   670  
   671  	t.Run("Success when there is default runtime labeled for application eventing", func(t *testing.T) {
   672  		// GIVEN
   673  		ctx := fixCtxWithTenant()
   674  		runtimeRepo := &automock.RuntimeRepository{}
   675  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   676  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   677  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
   678  			fixLabelFilterForRuntimeScenarios()).Return(fixRuntimes()[0], nil)
   679  		labelRepo := &automock.LabelRepository{}
   680  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   681  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   682  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   683  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   684  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   685  			runtimeID.String(), isNormalizedLabel).Return(nil, apperrors.NewNotFoundError(resource.Runtime, runtimeID.String()))
   686  
   687  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   688  
   689  		// WHEN
   690  		eventingCfg, err := svc.GetForApplication(ctx, app)
   691  
   692  		// THEN
   693  		require.NoError(t, err)
   694  		require.NotNil(t, eventingCfg)
   695  		require.Equal(t, appNormalizedEventURL, eventingCfg.DefaultURL)
   696  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   697  	})
   698  
   699  	t.Run("Success when labeling oldest runtime for application eventing", func(t *testing.T) {
   700  		// GIVEN
   701  		ctx := fixCtxWithTenant()
   702  		runtimeRepo := &automock.RuntimeRepository{}
   703  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   704  			1, mock.Anything).Return(fixEmptyRuntimePage(), nil)
   705  		runtimeRepo.On("GetOldestForFilters", ctx, tenantID.String(), fixLabelFilterForRuntimeScenarios()).
   706  			Return(fixRuntimes()[0], nil)
   707  		labelRepo := &automock.LabelRepository{}
   708  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   709  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   710  		labelRepo.On("Upsert", ctx, tenantID.String(), mock.MatchedBy(fixMatcherDefaultEventingForAppLabel())).Return(nil)
   711  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   712  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   713  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   714  			runtimeID.String(), isNormalizedLabel).Return(nil, apperrors.NewNotFoundError(resource.Runtime, runtimeID.String()))
   715  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   716  
   717  		// WHEN
   718  		eventingCfg, err := svc.GetForApplication(ctx, app)
   719  
   720  		// THEN
   721  		require.NoError(t, err)
   722  		require.NotNil(t, eventingCfg)
   723  		require.Equal(t, appNormalizedEventURL, eventingCfg.DefaultURL)
   724  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   725  	})
   726  
   727  	t.Run("Success when there is runtime labeled for application eventing and is labeled for normalization", func(t *testing.T) {
   728  		// GIVEN
   729  		ctx := fixCtxWithTenant()
   730  		runtimeRepo := &automock.RuntimeRepository{}
   731  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   732  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   733  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
   734  			fixLabelFilterForRuntimeScenarios()).Return(fixRuntimes()[0], nil)
   735  		labelRepo := &automock.LabelRepository{}
   736  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   737  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   738  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   739  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   740  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   741  			runtimeID.String(), isNormalizedLabel).Return(&model.Label{Value: "true"}, nil)
   742  
   743  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   744  
   745  		// WHEN
   746  		eventingCfg, err := svc.GetForApplication(ctx, app)
   747  
   748  		// THEN
   749  		require.NoError(t, err)
   750  		require.NotNil(t, eventingCfg)
   751  		require.Equal(t, appNormalizedEventURL, eventingCfg.DefaultURL)
   752  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   753  	})
   754  
   755  	t.Run("Success when there is runtime labeled for application eventing and is labeled not for normalization", func(t *testing.T) {
   756  		// GIVEN
   757  		ctx := fixCtxWithTenant()
   758  		runtimeRepo := &automock.RuntimeRepository{}
   759  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   760  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   761  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
   762  			fixLabelFilterForRuntimeScenarios()).Return(fixRuntimes()[0], nil)
   763  		labelRepo := &automock.LabelRepository{}
   764  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   765  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   766  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   767  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
   768  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   769  			runtimeID.String(), isNormalizedLabel).Return(&model.Label{Value: "false"}, nil)
   770  
   771  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
   772  
   773  		// WHEN
   774  		eventingCfg, err := svc.GetForApplication(ctx, app)
   775  
   776  		// THEN
   777  		require.NoError(t, err)
   778  		require.NotNil(t, eventingCfg)
   779  		require.Equal(t, appEventURL, eventingCfg.DefaultURL)
   780  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   781  	})
   782  
   783  	t.Run("Success when there is no oldest runtime for application eventing", func(t *testing.T) {
   784  		// GIVEN
   785  		ctx := fixCtxWithTenant()
   786  		runtimeRepo := &automock.RuntimeRepository{}
   787  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   788  			1, mock.Anything).Return(fixEmptyRuntimePage(), nil)
   789  		runtimeRepo.On("GetOldestForFilters", ctx, tenantID.String(), fixLabelFilterForRuntimeScenarios()).
   790  			Return(nil, apperrors.NewNotFoundError(resource.Runtime, ""))
   791  		labelRepo := &automock.LabelRepository{}
   792  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   793  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   794  
   795  		svc := NewService(nil, runtimeRepo, labelRepo)
   796  
   797  		// WHEN
   798  		eventingCfg, err := svc.GetForApplication(ctx, app)
   799  
   800  		// THEN
   801  		require.NoError(t, err)
   802  		require.NotNil(t, eventingCfg)
   803  		require.Equal(t, "", eventingCfg.DefaultURL.String())
   804  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   805  	})
   806  
   807  	t.Run("Success when there is no oldest runtime for application eventing (scenarios label does not exist)", func(t *testing.T) {
   808  		// GIVEN
   809  		ctx := fixCtxWithTenant()
   810  		runtimeRepo := &automock.RuntimeRepository{}
   811  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   812  			1, mock.Anything).Return(fixEmptyRuntimePage(), nil)
   813  		labelRepo := &automock.LabelRepository{}
   814  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   815  			applicationID.String(), model.ScenariosKey).Return(nil, apperrors.NewNotFoundError(resource.Label, ""))
   816  		svc := NewService(nil, runtimeRepo, labelRepo)
   817  
   818  		// WHEN
   819  		eventingCfg, err := svc.GetForApplication(ctx, app)
   820  
   821  		// THEN
   822  		require.NoError(t, err)
   823  		require.NotNil(t, eventingCfg)
   824  		require.Equal(t, "", eventingCfg.DefaultURL.String())
   825  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   826  	})
   827  
   828  	t.Run("Empty configuration when there are no scenarios assigned to application", func(t *testing.T) {
   829  		// GIVEN
   830  		ctx := fixCtxWithTenant()
   831  		emptyConfiguration := &model.ApplicationEventingConfiguration{
   832  			EventingConfiguration: model.EventingConfiguration{
   833  				DefaultURL: url.URL{},
   834  			},
   835  		}
   836  		runtimeRepo := &automock.RuntimeRepository{}
   837  		runtimePage := fixRuntimePageWithOne()
   838  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   839  			1, mock.Anything).Return(runtimePage, nil)
   840  		labelRepo := &automock.LabelRepository{}
   841  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   842  			applicationID.String(), model.ScenariosKey).Return(nil, apperrors.NewNotFoundError(resource.Label, ""))
   843  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimePage.Data[0].ID, getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   844  
   845  		svc := NewService(nil, runtimeRepo, labelRepo)
   846  
   847  		// WHEN
   848  		conf, err := svc.GetForApplication(ctx, app)
   849  
   850  		// THEN
   851  		require.NoError(t, err)
   852  		require.Equal(t, emptyConfiguration, conf)
   853  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   854  	})
   855  
   856  	t.Run("Success when current default runtime no longer belongs to the application scenarios", func(t *testing.T) {
   857  		// GIVEN
   858  		ctx := fixCtxWithTenant()
   859  		runtimeRepo := &automock.RuntimeRepository{}
   860  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   861  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
   862  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
   863  			fixLabelFilterForRuntimeScenarios()).Return(nil, apperrors.NewNotFoundError(resource.Runtime, ""))
   864  		runtimeRepo.On("GetOldestForFilters", ctx, tenantID.String(), fixLabelFilterForRuntimeScenarios()).
   865  			Return(nil, apperrors.NewNotFoundError(resource.Runtime, ""))
   866  		labelRepo := &automock.LabelRepository{}
   867  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   868  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   869  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
   870  			getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   871  
   872  		svc := NewService(nil, runtimeRepo, labelRepo)
   873  
   874  		// WHEN
   875  		eventingCfg, err := svc.GetForApplication(ctx, app)
   876  
   877  		// THEN
   878  		require.NoError(t, err)
   879  		require.NotNil(t, eventingCfg)
   880  		require.Equal(t, "", eventingCfg.DefaultURL.String())
   881  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   882  	})
   883  
   884  	t.Run("Success when new default runtime is elected", func(t *testing.T) {
   885  		// GIVEN
   886  		defaultURL := "https://default.URL"
   887  		ctx := fixCtxWithTenant()
   888  		runtimeRepo := &automock.RuntimeRepository{}
   889  		newRuntime := fixRuntimes()[0]
   890  		runtimePage := fixRuntimePageWithOne()
   891  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(), 1, mock.Anything).Return(runtimePage, nil)
   892  		labelRepo := &automock.LabelRepository{}
   893  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   894  			applicationID.String(), model.ScenariosKey).Return(nil, apperrors.NewNotFoundError(resource.Label, "")).Once()
   895  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   896  			applicationID.String(), model.ScenariosKey).Return(&model.Label{Key: model.ScenariosKey, Value: []interface{}{"scenario"}}, nil).Once()
   897  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   898  			newRuntime.ID, RuntimeEventingURLLabel).Return(&model.Label{Key: RuntimeEventingURLLabel, Value: defaultURL}, nil).Once()
   899  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
   900  			newRuntime.ID, isNormalizedLabel).Return(&model.Label{Value: "false"}, nil).Once()
   901  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimePage.Data[0].ID, getDefaultEventingForAppLabelKey(applicationID)).Return(nil)
   902  		labelRepo.On("Upsert", ctx, tenantID.String(), mock.Anything).Return(nil)
   903  		labelFilter := []*labelfilter.LabelFilter{
   904  			labelfilter.NewForKeyWithQuery(model.ScenariosKey, `$[*] ? ( @ == "scenario" )`),
   905  		}
   906  		runtimeRepo.On("GetOldestForFilters", ctx, tenantID.String(), labelFilter).Return(newRuntime, nil)
   907  
   908  		svc := NewService(nil, runtimeRepo, labelRepo)
   909  
   910  		// WHEN
   911  		conf, err := svc.GetForApplication(ctx, app)
   912  
   913  		// THEN
   914  		require.NoError(t, err)
   915  		require.Equal(t, fmt.Sprintf("%s/%s/v1/events", defaultURL, app.Name), conf.DefaultURL.String())
   916  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   917  	})
   918  
   919  	t.Run("Error when tenant not in context", func(t *testing.T) {
   920  		// GIVEN
   921  		svc := NewService(nil, nil, nil)
   922  
   923  		// WHEN
   924  		_, err := svc.GetForApplication(context.TODO(), app)
   925  
   926  		// THEN
   927  		require.Error(t, err)
   928  		require.EqualError(t, err, "while loading tenant from context: cannot read tenant from context")
   929  	})
   930  
   931  	t.Run("Error when labeling oldest runtime for application eventing returns error", func(t *testing.T) {
   932  		// GIVEN
   933  		expectedError := fmt.Sprintf(`while setting the runtime as default for eventing for application: while labeling the runtime [ID=%s] as default for eventing for application [ID=%s]: some error`, runtimeID, applicationID)
   934  		ctx := fixCtxWithTenant()
   935  		runtimeRepo := &automock.RuntimeRepository{}
   936  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   937  			1, mock.Anything).Return(fixEmptyRuntimePage(), nil)
   938  		runtimeRepo.On("GetOldestForFilters", ctx, tenantID.String(), fixLabelFilterForRuntimeScenarios()).
   939  			Return(fixRuntimes()[0], nil)
   940  		labelRepo := &automock.LabelRepository{}
   941  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   942  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   943  		labelRepo.On("Upsert", ctx, tenantID.String(),
   944  			mock.MatchedBy(fixMatcherDefaultEventingForAppLabel())).
   945  			Return(errors.New("some error"))
   946  		svc := NewService(nil, runtimeRepo, labelRepo)
   947  
   948  		// WHEN
   949  		_, err := svc.GetForApplication(ctx, app)
   950  
   951  		// THEN
   952  		require.Error(t, err)
   953  		require.EqualError(t, err, expectedError)
   954  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   955  	})
   956  
   957  	t.Run("Error when getting the oldest runtime for application eventing returns error", func(t *testing.T) {
   958  		// GIVEN
   959  		expectedError := fmt.Sprintf(`while getting the oldest runtime for scenarios: while getting the oldest runtime for application [ID=%s] scenarios with filter: some error`, applicationID)
   960  		ctx := fixCtxWithTenant()
   961  		runtimeRepo := &automock.RuntimeRepository{}
   962  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   963  			1, mock.Anything).Return(fixEmptyRuntimePage(), nil)
   964  		runtimeRepo.On("GetOldestForFilters", ctx, tenantID.String(), fixLabelFilterForRuntimeScenarios()).
   965  			Return(nil, errors.New("some error"))
   966  		labelRepo := &automock.LabelRepository{}
   967  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   968  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
   969  		svc := NewService(nil, runtimeRepo, labelRepo)
   970  
   971  		// WHEN
   972  		_, err := svc.GetForApplication(ctx, app)
   973  
   974  		// THEN
   975  		require.Error(t, err)
   976  		require.EqualError(t, err, expectedError)
   977  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
   978  	})
   979  
   980  	t.Run("Error when getting the oldest runtime for application eventing returns error on converting scenarios label to slice of strings", func(t *testing.T) {
   981  		// GIVEN
   982  		expectedError := fmt.Sprintf(`while getting the oldest runtime for scenarios: while getting application scenarios: while converting label [key=%s] value to a slice of strings: Internal Server Error: cannot convert label value to slice of strings`, model.ScenariosKey)
   983  		ctx := fixCtxWithTenant()
   984  		runtimeRepo := &automock.RuntimeRepository{}
   985  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
   986  			1, mock.Anything).Return(fixEmptyRuntimePage(), nil)
   987  		labelRepo := &automock.LabelRepository{}
   988  		scenariosLabel := fixApplicationScenariosLabel()
   989  		scenariosLabel.Value = "abc"
   990  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
   991  			applicationID.String(), model.ScenariosKey).Return(scenariosLabel, nil)
   992  		svc := NewService(nil, runtimeRepo, labelRepo)
   993  
   994  		// WHEN
   995  		_, err := svc.GetForApplication(ctx, app)
   996  
   997  		// THEN
   998  		require.Error(t, err)
   999  		require.EqualError(t, err, expectedError)
  1000  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
  1001  	})
  1002  
  1003  	t.Run("Error when getting the oldest runtime for application eventing returns error when getting scenarios label", func(t *testing.T) {
  1004  		// GIVEN
  1005  		expectedError := fmt.Sprintf(`while getting the oldest runtime for scenarios: while getting application scenarios: while getting the label [key=%s] for application [ID=%s]: some error`, model.ScenariosKey, applicationID)
  1006  		ctx := fixCtxWithTenant()
  1007  		runtimeRepo := &automock.RuntimeRepository{}
  1008  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
  1009  			1, mock.Anything).Return(fixEmptyRuntimePage(), nil)
  1010  		labelRepo := &automock.LabelRepository{}
  1011  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
  1012  			applicationID.String(), model.ScenariosKey).Return(nil, errors.New("some error"))
  1013  		svc := NewService(nil, runtimeRepo, labelRepo)
  1014  
  1015  		// WHEN
  1016  		_, err := svc.GetForApplication(ctx, app)
  1017  
  1018  		// THEN
  1019  		require.Error(t, err)
  1020  		require.EqualError(t, err, expectedError)
  1021  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
  1022  	})
  1023  
  1024  	t.Run("Error when getting default runtime labeled for application eventing returns error", func(t *testing.T) {
  1025  		// GIVEN
  1026  		expectedError := fmt.Sprintf(`while getting default runtime for app eventing: while fetching runtimes with label [key=%s]: some error`, getDefaultEventingForAppLabelKey(applicationID))
  1027  		ctx := fixCtxWithTenant()
  1028  		runtimeRepo := &automock.RuntimeRepository{}
  1029  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
  1030  			1, mock.Anything).Return(nil, errors.New("some error"))
  1031  		svc := NewService(nil, runtimeRepo, nil)
  1032  
  1033  		// WHEN
  1034  		_, err := svc.GetForApplication(ctx, app)
  1035  
  1036  		// THEN
  1037  		require.Error(t, err)
  1038  		require.EqualError(t, err, expectedError)
  1039  		mock.AssertExpectationsForObjects(t, runtimeRepo)
  1040  	})
  1041  
  1042  	t.Run("Error when getting default runtime labeled for application eventing returns error with multiple runtimes labeled", func(t *testing.T) {
  1043  		// GIVEN
  1044  		expectedError := fmt.Sprintf(`while getting default runtime for app eventing: got multpile runtimes labeled [key=%s] as default for eventing`, getDefaultEventingForAppLabelKey(applicationID))
  1045  		ctx := fixCtxWithTenant()
  1046  		runtimeRepo := &automock.RuntimeRepository{}
  1047  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
  1048  			1, mock.Anything).Return(fixRuntimePage(), nil)
  1049  		labelRepo := &automock.LabelRepository{}
  1050  
  1051  		svc := NewService(nil, runtimeRepo, labelRepo)
  1052  
  1053  		// WHEN
  1054  		_, err := svc.GetForApplication(ctx, app)
  1055  
  1056  		// THEN
  1057  		require.Error(t, err)
  1058  		require.EqualError(t, err, expectedError)
  1059  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
  1060  	})
  1061  
  1062  	t.Run("Error when ensuring the scenarios, getting scenarios returns error", func(t *testing.T) {
  1063  		// GIVEN
  1064  		expectedError := fmt.Sprintf(`while ensuring the scenarios assigned to the runtime and application: while verifing whether runtime [ID=%s] belongs to the application scenarios: while getting application scenarios: while getting the label [key=%s] for application [ID=%s]: some error`, runtimeID, model.ScenariosKey, applicationID)
  1065  		ctx := fixCtxWithTenant()
  1066  		runtimeRepo := &automock.RuntimeRepository{}
  1067  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
  1068  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
  1069  		labelRepo := &automock.LabelRepository{}
  1070  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
  1071  			applicationID.String(), model.ScenariosKey).Return(nil, errors.New("some error"))
  1072  
  1073  		svc := NewService(nil, runtimeRepo, labelRepo)
  1074  
  1075  		// WHEN
  1076  		_, err := svc.GetForApplication(ctx, app)
  1077  
  1078  		// THEN
  1079  		require.Error(t, err)
  1080  		require.EqualError(t, err, expectedError)
  1081  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
  1082  	})
  1083  
  1084  	t.Run("Error when verifing whether given runtime belongs to the application scenarios - repository returns error", func(t *testing.T) {
  1085  		// GIVEN
  1086  		expectedError := fmt.Sprintf(`while ensuring the scenarios assigned to the runtime and application: while verifing whether runtime [ID=%s] belongs to the application scenarios: while getting the runtime [ID=%s] with scenarios with filter: some-error`, runtimeID, runtimeID)
  1087  		ctx := fixCtxWithTenant()
  1088  		runtimeRepo := &automock.RuntimeRepository{}
  1089  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
  1090  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
  1091  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
  1092  			fixLabelFilterForRuntimeScenarios()).Return(nil, errors.New("some-error"))
  1093  		labelRepo := &automock.LabelRepository{}
  1094  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
  1095  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
  1096  
  1097  		svc := NewService(nil, runtimeRepo, labelRepo)
  1098  
  1099  		// WHEN
  1100  		_, err := svc.GetForApplication(ctx, app)
  1101  
  1102  		// THEN
  1103  		require.Error(t, err)
  1104  		require.EqualError(t, err, expectedError)
  1105  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
  1106  	})
  1107  
  1108  	t.Run("Error when deleting label from the given runtime because it does not belong to application scenarios - repository returns error", func(t *testing.T) {
  1109  		// GIVEN
  1110  		expectedError := fmt.Sprintf(`while ensuring the scenarios assigned to the runtime and application: when deleting current default runtime for the application because of scenarios mismatch: while deleting label [key=%s, runtimeID=%s]: some-error`, getDefaultEventingForAppLabelKey(applicationID), runtimeID)
  1111  		ctx := fixCtxWithTenant()
  1112  		runtimeRepo := &automock.RuntimeRepository{}
  1113  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
  1114  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
  1115  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
  1116  			fixLabelFilterForRuntimeScenarios()).Return(nil, apperrors.NewNotFoundError(resource.Runtime, ""))
  1117  		labelRepo := &automock.LabelRepository{}
  1118  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
  1119  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
  1120  		labelRepo.On("Delete", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(),
  1121  			getDefaultEventingForAppLabelKey(applicationID)).Return(errors.New("some-error"))
  1122  
  1123  		svc := NewService(nil, runtimeRepo, labelRepo)
  1124  
  1125  		// WHEN
  1126  		_, err := svc.GetForApplication(ctx, app)
  1127  
  1128  		// THEN
  1129  		require.Error(t, err)
  1130  		require.EqualError(t, err, expectedError)
  1131  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
  1132  	})
  1133  
  1134  	t.Run("Error when getting eventing configuration for a given runtime", func(t *testing.T) {
  1135  		// GIVEN
  1136  		expectedError := fmt.Sprintf(`while fetching eventing configuration for runtime: while getting the label [key=%s] for runtime [ID=%s]: some error`, RuntimeEventingURLLabel, runtimeID)
  1137  		ctx := fixCtxWithTenant()
  1138  		runtimeRepo := &automock.RuntimeRepository{}
  1139  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
  1140  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
  1141  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
  1142  			fixLabelFilterForRuntimeScenarios()).Return(fixRuntimes()[0], nil)
  1143  		labelRepo := &automock.LabelRepository{}
  1144  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
  1145  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
  1146  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
  1147  			runtimeID.String(), RuntimeEventingURLLabel).Return(nil, errors.New("some error"))
  1148  
  1149  		svc := NewService(nil, runtimeRepo, labelRepo)
  1150  
  1151  		// WHEN
  1152  		_, err := svc.GetForApplication(ctx, app)
  1153  
  1154  		// THEN
  1155  		require.Error(t, err)
  1156  		require.EqualError(t, err, expectedError)
  1157  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
  1158  	})
  1159  
  1160  	t.Run("Error when getting runtime normalization label", func(t *testing.T) {
  1161  		// GIVEN
  1162  		expectedError := fmt.Sprintf(`while determining whether application name should be normalized in runtime eventing URL: while getting the label [key=%s] for runtime [ID=%s]: some error`, isNormalizedLabel, runtimeID)
  1163  		ctx := fixCtxWithTenant()
  1164  		runtimeRepo := &automock.RuntimeRepository{}
  1165  		runtimeRepo.On("List", ctx, tenantID.String(), fixLabelFilterForRuntimeDefaultEventingForApp(),
  1166  			1, mock.Anything).Return(fixRuntimePageWithOne(), nil)
  1167  		runtimeRepo.On("GetByFiltersAndID", ctx, tenantID.String(), runtimeID.String(),
  1168  			fixLabelFilterForRuntimeScenarios()).Return(fixRuntimes()[0], nil)
  1169  		labelRepo := &automock.LabelRepository{}
  1170  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.ApplicationLabelableObject,
  1171  			applicationID.String(), model.ScenariosKey).Return(fixApplicationScenariosLabel(), nil)
  1172  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
  1173  			runtimeID.String(), RuntimeEventingURLLabel).Return(fixRuntimeEventingURLLabel(), nil)
  1174  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject,
  1175  			runtimeID.String(), isNormalizedLabel).Return(nil, errors.New("some error"))
  1176  
  1177  		svc := NewService(appNameNormalizer, runtimeRepo, labelRepo)
  1178  
  1179  		// WHEN
  1180  		_, err := svc.GetForApplication(ctx, app)
  1181  
  1182  		// THEN
  1183  		require.Error(t, err)
  1184  		require.EqualError(t, err, expectedError)
  1185  		mock.AssertExpectationsForObjects(t, runtimeRepo, labelRepo)
  1186  	})
  1187  }
  1188  
  1189  func Test_GetForRuntime(t *testing.T) {
  1190  	t.Run("Success when label repository returns NotFoundError", func(t *testing.T) {
  1191  		// GIVEN
  1192  		ctx := fixCtxWithTenant()
  1193  		labelRepo := &automock.LabelRepository{}
  1194  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(), RuntimeEventingURLLabel).
  1195  			Return(nil, apperrors.NewNotFoundError(resource.Label, ""))
  1196  		expectedEventingCfg := fixRuntimeEventngCfgWithEmptyURL(t)
  1197  		eventingSvc := NewService(nil, nil, labelRepo)
  1198  
  1199  		// WHEN
  1200  		eventingCfg, err := eventingSvc.GetForRuntime(ctx, runtimeID)
  1201  
  1202  		// THEN
  1203  		require.NoError(t, err)
  1204  		require.Equal(t, expectedEventingCfg, eventingCfg)
  1205  		mock.AssertExpectationsForObjects(t, labelRepo)
  1206  	})
  1207  
  1208  	t.Run("Success when label repository returns lable instance with URL string value", func(t *testing.T) {
  1209  		// GIVEN
  1210  		ctx := fixCtxWithTenant()
  1211  		labelRepo := &automock.LabelRepository{}
  1212  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(), RuntimeEventingURLLabel).
  1213  			Return(fixRuntimeEventingURLLabel(), nil)
  1214  		expectedEventingCfg := fixRuntimeEventngCfgWithURL(t, runtimeEventURL)
  1215  		eventingSvc := NewService(nil, nil, labelRepo)
  1216  
  1217  		// WHEN
  1218  		eventingCfg, err := eventingSvc.GetForRuntime(ctx, runtimeID)
  1219  
  1220  		// THEN
  1221  		require.NoError(t, err)
  1222  		require.Equal(t, expectedEventingCfg, eventingCfg)
  1223  		mock.AssertExpectationsForObjects(t, labelRepo)
  1224  	})
  1225  
  1226  	t.Run("Error when tenant not in context", func(t *testing.T) {
  1227  		// GIVEN
  1228  		svc := NewService(nil, nil, nil)
  1229  
  1230  		// WHEN
  1231  		_, err := svc.GetForRuntime(context.TODO(), uuid.Nil)
  1232  
  1233  		// THEN
  1234  		require.Error(t, err)
  1235  		require.EqualError(t, err, "while loading tenant from context: cannot read tenant from context")
  1236  	})
  1237  
  1238  	t.Run("Error when label repository returns error", func(t *testing.T) {
  1239  		// GIVEN
  1240  		ctx := fixCtxWithTenant()
  1241  		labelRepo := &automock.LabelRepository{}
  1242  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(), RuntimeEventingURLLabel).
  1243  			Return(nil, errors.New("some error"))
  1244  		eventingSvc := NewService(nil, nil, labelRepo)
  1245  
  1246  		// WHEN
  1247  		_, err := eventingSvc.GetForRuntime(ctx, runtimeID)
  1248  
  1249  		// THEN
  1250  		require.Error(t, err)
  1251  		require.EqualError(t, err, `while getting the label [key=`+RuntimeEventingURLLabel+`] for runtime [ID=`+runtimeID.String()+`]: some error`)
  1252  		mock.AssertExpectationsForObjects(t, labelRepo)
  1253  	})
  1254  
  1255  	t.Run("Error when label value cannot be converted to a string", func(t *testing.T) {
  1256  		// GIVEN
  1257  		ctx := fixCtxWithTenant()
  1258  
  1259  		label := fixRuntimeEventingURLLabel()
  1260  		label.Value = byte(1)
  1261  
  1262  		labelRepo := &automock.LabelRepository{}
  1263  		labelRepo.On("GetByKey", ctx, tenantID.String(), model.RuntimeLabelableObject, runtimeID.String(), RuntimeEventingURLLabel).
  1264  			Return(label, nil)
  1265  		eventingSvc := NewService(nil, nil, labelRepo)
  1266  
  1267  		// WHEN
  1268  		_, err := eventingSvc.GetForRuntime(ctx, runtimeID)
  1269  
  1270  		// THEN
  1271  		require.Error(t, err)
  1272  		require.EqualError(t, err, `unable to cast label [key=`+RuntimeEventingURLLabel+`, runtimeID=`+runtimeID.String()+`] value as a string`)
  1273  		mock.AssertExpectationsForObjects(t, labelRepo)
  1274  	})
  1275  }