github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/eventing/fixtures_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/pkg/str"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/kyma-incubator/compass/components/director/internal/labelfilter"
    14  
    15  	"github.com/google/uuid"
    16  	"github.com/kyma-incubator/compass/components/director/internal/domain/tenant"
    17  	"github.com/kyma-incubator/compass/components/director/internal/model"
    18  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    19  	"github.com/kyma-incubator/compass/components/director/pkg/pagination"
    20  )
    21  
    22  var tenantID = uuid.New()
    23  var externalTenantID = uuid.New()
    24  var runtimeID = uuid.New()
    25  var applicationID = uuid.New()
    26  
    27  const (
    28  	eventURLSchema  = "https://eventing.domain.local/%s/v1/events"
    29  	runtimeEventURL = "https://eventing.domain.local"
    30  )
    31  
    32  func fixCtxWithTenant() context.Context {
    33  	ctx := context.TODO()
    34  	ctx = tenant.SaveToContext(ctx, tenantID.String(), externalTenantID.String())
    35  
    36  	return ctx
    37  }
    38  
    39  func fixRuntimeEventingURLLabel() *model.Label {
    40  	return &model.Label{
    41  		ID:         uuid.New().String(),
    42  		Key:        RuntimeEventingURLLabel,
    43  		ObjectID:   runtimeID.String(),
    44  		ObjectType: model.RuntimeLabelableObject,
    45  		Tenant:     str.Ptr(tenantID.String()),
    46  		Value:      runtimeEventURL,
    47  	}
    48  }
    49  
    50  func fixRuntimeEventngCfgWithURL(t *testing.T, rawURL string) *model.RuntimeEventingConfiguration {
    51  	validURL := fixValidURL(t, rawURL)
    52  
    53  	return &model.RuntimeEventingConfiguration{
    54  		EventingConfiguration: model.EventingConfiguration{
    55  			DefaultURL: validURL,
    56  		},
    57  	}
    58  }
    59  
    60  func fixRuntimeEventngCfgWithEmptyURL(t *testing.T) *model.RuntimeEventingConfiguration {
    61  	return fixRuntimeEventngCfgWithURL(t, EmptyEventingURL)
    62  }
    63  
    64  func fixRuntimes() []*model.Runtime {
    65  	return []*model.Runtime{
    66  		&model.Runtime{
    67  			ID:   runtimeID.String(),
    68  			Name: "runtime-1",
    69  		},
    70  		&model.Runtime{
    71  			ID:   uuid.New().String(),
    72  			Name: "runtime-2",
    73  		},
    74  	}
    75  }
    76  
    77  func fixRuntimePage() *model.RuntimePage {
    78  	modelRuntimes := fixRuntimes()
    79  	return &model.RuntimePage{
    80  		Data:       modelRuntimes,
    81  		TotalCount: len(modelRuntimes),
    82  		PageInfo: &pagination.Page{
    83  			HasNextPage: false,
    84  			EndCursor:   "end",
    85  			StartCursor: "start",
    86  		},
    87  	}
    88  }
    89  
    90  func fixRuntimePageWithOne() *model.RuntimePage {
    91  	modelRuntimes := []*model.Runtime{
    92  		fixRuntimes()[0],
    93  	}
    94  	return &model.RuntimePage{
    95  		Data:       modelRuntimes,
    96  		TotalCount: len(modelRuntimes),
    97  		PageInfo: &pagination.Page{
    98  			HasNextPage: false,
    99  			EndCursor:   "end",
   100  			StartCursor: "start",
   101  		},
   102  	}
   103  }
   104  
   105  func fixEmptyRuntimePage() *model.RuntimePage {
   106  	return &model.RuntimePage{
   107  		Data:       nil,
   108  		TotalCount: 0,
   109  		PageInfo: &pagination.Page{
   110  			HasNextPage: false,
   111  			EndCursor:   "end",
   112  			StartCursor: "start",
   113  		},
   114  	}
   115  }
   116  
   117  func fixLabelFilterForRuntimeDefaultEventingForApp() []*labelfilter.LabelFilter {
   118  	return []*labelfilter.LabelFilter{
   119  		labelfilter.NewForKey(getDefaultEventingForAppLabelKey(applicationID)),
   120  	}
   121  }
   122  
   123  func fixLabelFilterForRuntimeScenarios() []*labelfilter.LabelFilter {
   124  	return []*labelfilter.LabelFilter{
   125  		labelfilter.NewForKeyWithQuery(model.ScenariosKey, `$[*] ? ( @ == "DEFAULT" || @ == "CUSTOM" )`),
   126  	}
   127  }
   128  
   129  func fixApplicationScenariosLabel() *model.Label {
   130  	return &model.Label{
   131  		ID:         uuid.New().String(),
   132  		Key:        model.ScenariosKey,
   133  		ObjectID:   applicationID.String(),
   134  		ObjectType: model.ApplicationLabelableObject,
   135  		Tenant:     str.Ptr(tenantID.String()),
   136  		Value:      []interface{}{"DEFAULT", "CUSTOM"},
   137  	}
   138  }
   139  
   140  func fixMatcherDefaultEventingForAppLabel() func(l *model.Label) bool {
   141  	return func(l *model.Label) bool {
   142  		return l.Key == getDefaultEventingForAppLabelKey(applicationID)
   143  	}
   144  }
   145  
   146  func fixModelApplicationEventingConfiguration(t *testing.T, rawURL string) *model.ApplicationEventingConfiguration {
   147  	validURL := fixValidURL(t, rawURL)
   148  	return &model.ApplicationEventingConfiguration{
   149  		EventingConfiguration: model.EventingConfiguration{
   150  			DefaultURL: validURL,
   151  		},
   152  	}
   153  }
   154  
   155  func fixGQLApplicationEventingConfiguration(url string) *graphql.ApplicationEventingConfiguration {
   156  	return &graphql.ApplicationEventingConfiguration{
   157  		DefaultURL: url,
   158  	}
   159  }
   160  
   161  func fixValidURL(t *testing.T, rawURL string) url.URL {
   162  	eventingURL, err := url.Parse(rawURL)
   163  	require.NoError(t, err)
   164  	require.NotNil(t, eventingURL)
   165  	return *eventingURL
   166  }
   167  
   168  func fixApplicationModel(name string) model.Application {
   169  	return model.Application{
   170  		Name:       name,
   171  		BaseEntity: &model.BaseEntity{ID: applicationID.String()},
   172  	}
   173  }
   174  
   175  func fixAppEventURL(t *testing.T, appName string) url.URL {
   176  	eventURL := fmt.Sprintf(eventURLSchema, appName)
   177  	return fixValidURL(t, eventURL)
   178  }