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

     1  package eventdef_test
     2  
     3  import (
     4  	"database/sql"
     5  	"database/sql/driver"
     6  	"encoding/json"
     7  	"time"
     8  
     9  	event "github.com/kyma-incubator/compass/components/director/internal/domain/eventdef"
    10  
    11  	"github.com/kyma-incubator/compass/components/director/internal/domain/version"
    12  
    13  	"github.com/kyma-incubator/compass/components/director/internal/model"
    14  	"github.com/kyma-incubator/compass/components/director/internal/repo"
    15  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    16  	"github.com/kyma-incubator/compass/components/director/pkg/str"
    17  )
    18  
    19  const (
    20  	eventID          = "ddddddddd-dddd-dddd-dddd-dddddddddddd"
    21  	specID           = "sssssssss-ssss-ssss-ssss-ssssssssssss"
    22  	tenantID         = "b91b59f7-2563-40b2-aba9-fef726037aa3"
    23  	externalTenantID = "eeeeeeeee-eeee-eeee-eeee-eeeeeeeeeeee"
    24  	bundleID         = "bbbbbbbbb-bbbb-bbbb-bbbb-bbbbbbbbbbbb"
    25  	packageID        = "ppppppppp-pppp-pppp-pppp-pppppppppppp"
    26  	ordID            = "com.compass.ord.v1"
    27  	localTenantID    = "localTenantID"
    28  	extensible       = `{"supported":"automatic","description":"Please find the extensibility documentation"}`
    29  	successors       = `["sap.billing.sb:eventResource:BusinessEvents_SubscriptionEvents:v1"]`
    30  	resourceHash     = "123456"
    31  	publicVisibility = "public"
    32  )
    33  
    34  var (
    35  	fixedTimestamp       = time.Now()
    36  	appID                = "aaaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa"
    37  	appTemplateVersionID = "aaaaaaaaa-bbbb-cccc-dddd-aaaaaaaaaaaa"
    38  )
    39  
    40  func fixEventDefinitionModel(id string, name string) *model.EventDefinition {
    41  	return &model.EventDefinition{
    42  		Name:       name,
    43  		BaseEntity: &model.BaseEntity{ID: id},
    44  		Visibility: str.Ptr(publicVisibility),
    45  	}
    46  }
    47  
    48  func fixFullEventDefinitionModel(placeholder string) (model.EventDefinition, model.Spec, model.BundleReference) {
    49  	return fixFullEventDefinitionModelWithID(eventID, placeholder)
    50  }
    51  
    52  func fixFullEventDefinitionModelWithID(id, placeholder string) (model.EventDefinition, model.Spec, model.BundleReference) {
    53  	eventType := model.EventSpecTypeAsyncAPI
    54  	spec := model.Spec{
    55  		ID:         specID,
    56  		Data:       str.Ptr("spec_data_" + placeholder),
    57  		Format:     model.SpecFormatYaml,
    58  		ObjectType: model.EventSpecReference,
    59  		ObjectID:   eventID,
    60  		EventType:  &eventType,
    61  	}
    62  
    63  	deprecated := false
    64  	forRemoval := false
    65  
    66  	v := &model.Version{
    67  		Value:           "v1.1",
    68  		Deprecated:      &deprecated,
    69  		DeprecatedSince: str.Ptr("v1.0"),
    70  		ForRemoval:      &forRemoval,
    71  	}
    72  
    73  	eventBundleReference := model.BundleReference{
    74  		BundleID:   str.Ptr(bundleID),
    75  		ObjectType: model.BundleEventReference,
    76  		ObjectID:   str.Ptr(eventID),
    77  	}
    78  
    79  	boolVar := false
    80  	return model.EventDefinition{
    81  		ApplicationID:       &appID,
    82  		PackageID:           str.Ptr(packageID),
    83  		Name:                placeholder,
    84  		Description:         str.Ptr("desc_" + placeholder),
    85  		Group:               str.Ptr("group_" + placeholder),
    86  		OrdID:               str.Ptr(ordID),
    87  		LocalTenantID:       str.Ptr(localTenantID),
    88  		ShortDescription:    str.Ptr("shortDescription"),
    89  		SystemInstanceAware: &boolVar,
    90  		PolicyLevel:         nil,
    91  		CustomPolicyLevel:   nil,
    92  		Tags:                json.RawMessage("[]"),
    93  		Countries:           json.RawMessage("[]"),
    94  		Links:               json.RawMessage("[]"),
    95  		ReleaseStatus:       str.Ptr("releaseStatus"),
    96  		SunsetDate:          str.Ptr("sunsetDate"),
    97  		Successors:          json.RawMessage(successors),
    98  		ChangeLogEntries:    json.RawMessage("[]"),
    99  		Labels:              json.RawMessage("[]"),
   100  		Visibility:          str.Ptr(publicVisibility),
   101  		Disabled:            &boolVar,
   102  		PartOfProducts:      json.RawMessage("[]"),
   103  		LineOfBusiness:      json.RawMessage("[]"),
   104  		Industry:            json.RawMessage("[]"),
   105  		Extensible:          json.RawMessage(extensible),
   106  		Version:             v,
   107  		Hierarchy:           json.RawMessage("[]"),
   108  		DocumentationLabels: json.RawMessage("[]"),
   109  		BaseEntity: &model.BaseEntity{
   110  			ID:        id,
   111  			Ready:     true,
   112  			CreatedAt: &fixedTimestamp,
   113  			UpdatedAt: &time.Time{},
   114  			DeletedAt: &time.Time{},
   115  			Error:     nil,
   116  		},
   117  		ResourceHash: str.Ptr(resourceHash),
   118  	}, spec, eventBundleReference
   119  }
   120  
   121  func fixFullGQLEventDefinition(placeholder string) *graphql.EventDefinition {
   122  	data := graphql.CLOB("spec_data_" + placeholder)
   123  
   124  	spec := &graphql.EventSpec{
   125  		Data:         &data,
   126  		Format:       graphql.SpecFormatYaml,
   127  		Type:         graphql.EventSpecTypeAsyncAPI,
   128  		DefinitionID: eventID,
   129  	}
   130  
   131  	deprecated := false
   132  	forRemoval := false
   133  
   134  	v := &graphql.Version{
   135  		Value:           "v1.1",
   136  		Deprecated:      &deprecated,
   137  		DeprecatedSince: str.Ptr("v1.0"),
   138  		ForRemoval:      &forRemoval,
   139  	}
   140  
   141  	return &graphql.EventDefinition{
   142  		BundleID:    bundleID,
   143  		Name:        placeholder,
   144  		Description: str.Ptr("desc_" + placeholder),
   145  		Spec:        spec,
   146  		Group:       str.Ptr("group_" + placeholder),
   147  		Version:     v,
   148  		BaseEntity: &graphql.BaseEntity{
   149  			ID:        eventID,
   150  			Ready:     true,
   151  			Error:     nil,
   152  			CreatedAt: timeToTimestampPtr(fixedTimestamp),
   153  			UpdatedAt: timeToTimestampPtr(time.Time{}),
   154  			DeletedAt: timeToTimestampPtr(time.Time{}),
   155  		},
   156  	}
   157  }
   158  
   159  func fixModelEventDefinitionInput(name, description string, group string) (*model.EventDefinitionInput, *model.SpecInput) {
   160  	data := "data"
   161  	eventType := model.EventSpecTypeAsyncAPI
   162  
   163  	spec := &model.SpecInput{
   164  		Data:         &data,
   165  		EventType:    &eventType,
   166  		Format:       model.SpecFormatYaml,
   167  		FetchRequest: &model.FetchRequestInput{},
   168  	}
   169  
   170  	deprecated := false
   171  	deprecatedSince := ""
   172  	forRemoval := false
   173  
   174  	v := &model.VersionInput{
   175  		Value:           "1.0.0",
   176  		Deprecated:      &deprecated,
   177  		DeprecatedSince: &deprecatedSince,
   178  		ForRemoval:      &forRemoval,
   179  	}
   180  
   181  	return &model.EventDefinitionInput{
   182  		Name:         name,
   183  		Description:  &description,
   184  		Group:        &group,
   185  		VersionInput: v,
   186  	}, spec
   187  }
   188  
   189  func fixGQLEventDefinitionInput(name, description string, group string) *graphql.EventDefinitionInput {
   190  	data := graphql.CLOB("data")
   191  
   192  	spec := &graphql.EventSpecInput{
   193  		Data:         &data,
   194  		Type:         graphql.EventSpecTypeAsyncAPI,
   195  		Format:       graphql.SpecFormatYaml,
   196  		FetchRequest: &graphql.FetchRequestInput{},
   197  	}
   198  
   199  	deprecated := false
   200  	deprecatedSince := ""
   201  	forRemoval := false
   202  
   203  	v := &graphql.VersionInput{
   204  		Value:           "1.0.0",
   205  		Deprecated:      &deprecated,
   206  		DeprecatedSince: &deprecatedSince,
   207  		ForRemoval:      &forRemoval,
   208  	}
   209  
   210  	return &graphql.EventDefinitionInput{
   211  		Name:        name,
   212  		Description: &description,
   213  		Group:       &group,
   214  		Spec:        spec,
   215  		Version:     v,
   216  	}
   217  }
   218  
   219  func fixEntityEventDefinition(id string, name string) *event.Entity {
   220  	return &event.Entity{
   221  		Name:       name,
   222  		BaseEntity: &repo.BaseEntity{ID: id},
   223  		Visibility: publicVisibility,
   224  	}
   225  }
   226  
   227  func fixFullEntityEventDefinition(eventID, placeholder string) *event.Entity {
   228  	return &event.Entity{
   229  		ApplicationID:       repo.NewValidNullableString(appID),
   230  		PackageID:           repo.NewValidNullableString(packageID),
   231  		Name:                placeholder,
   232  		Description:         repo.NewValidNullableString("desc_" + placeholder),
   233  		GroupName:           repo.NewValidNullableString("group_" + placeholder),
   234  		OrdID:               repo.NewValidNullableString(ordID),
   235  		LocalTenantID:       repo.NewValidNullableString(localTenantID),
   236  		ShortDescription:    repo.NewValidNullableString("shortDescription"),
   237  		SystemInstanceAware: repo.NewValidNullableBool(false),
   238  		PolicyLevel:         sql.NullString{},
   239  		CustomPolicyLevel:   sql.NullString{},
   240  		ChangeLogEntries:    repo.NewValidNullableString("[]"),
   241  		Links:               repo.NewValidNullableString("[]"),
   242  		Tags:                repo.NewValidNullableString("[]"),
   243  		Countries:           repo.NewValidNullableString("[]"),
   244  		ReleaseStatus:       repo.NewValidNullableString("releaseStatus"),
   245  		SunsetDate:          repo.NewValidNullableString("sunsetDate"),
   246  		Successors:          repo.NewValidNullableString(successors),
   247  		Labels:              repo.NewValidNullableString("[]"),
   248  		Visibility:          publicVisibility,
   249  		Disabled:            repo.NewValidNullableBool(false),
   250  		PartOfProducts:      repo.NewValidNullableString("[]"),
   251  		LineOfBusiness:      repo.NewValidNullableString("[]"),
   252  		Industry:            repo.NewValidNullableString("[]"),
   253  		Extensible:          repo.NewValidNullableString(extensible),
   254  		Version: version.Version{
   255  			Value:           repo.NewNullableString(str.Ptr("v1.1")),
   256  			Deprecated:      repo.NewValidNullableBool(false),
   257  			DeprecatedSince: repo.NewNullableString(str.Ptr("v1.0")),
   258  			ForRemoval:      repo.NewValidNullableBool(false),
   259  		},
   260  		ResourceHash:        repo.NewValidNullableString(resourceHash),
   261  		Hierarchy:           repo.NewValidNullableString("[]"),
   262  		DocumentationLabels: repo.NewValidNullableString("[]"),
   263  		BaseEntity: &repo.BaseEntity{
   264  			ID:        eventID,
   265  			Ready:     true,
   266  			CreatedAt: &fixedTimestamp,
   267  			UpdatedAt: &time.Time{},
   268  			DeletedAt: &time.Time{},
   269  			Error:     sql.NullString{},
   270  		},
   271  	}
   272  }
   273  
   274  func fixEventDefinitionColumns() []string {
   275  	return []string{"id", "app_id", "app_template_version_id", "package_id", "name", "description", "group_name", "ord_id", "local_tenant_id",
   276  		"short_description", "system_instance_aware", "policy_level", "custom_policy_level",
   277  		"changelog_entries", "links", "tags", "countries", "release_status",
   278  		"sunset_date", "labels", "visibility", "disabled", "part_of_products", "line_of_business", "industry", "version_value", "version_deprecated", "version_deprecated_since",
   279  		"version_for_removal", "ready", "created_at", "updated_at", "deleted_at", "error", "extensible", "successors", "resource_hash", "hierarchy", "documentation_labels"}
   280  }
   281  
   282  func fixEventDefinitionRow(id, placeholder string) []driver.Value {
   283  	boolVar := false
   284  	return []driver.Value{id, appID, repo.NewValidNullableString(""), packageID, placeholder, "desc_" + placeholder, "group_" + placeholder, ordID, localTenantID, "shortDescription", &boolVar, nil, nil,
   285  		repo.NewValidNullableString("[]"), repo.NewValidNullableString("[]"), repo.NewValidNullableString("[]"), repo.NewValidNullableString("[]"), "releaseStatus", "sunsetDate", repo.NewValidNullableString("[]"), publicVisibility, &boolVar,
   286  		repo.NewValidNullableString("[]"), repo.NewValidNullableString("[]"), repo.NewValidNullableString("[]"), "v1.1", false, "v1.0", false, true, fixedTimestamp, time.Time{}, time.Time{}, nil, repo.NewValidNullableString(extensible),
   287  		repo.NewValidNullableString(successors), repo.NewValidNullableString(resourceHash), repo.NewValidNullableString("[]"), repo.NewValidNullableString("[]")}
   288  }
   289  
   290  func fixEventCreateArgs(id string, event *model.EventDefinition) []driver.Value {
   291  	return []driver.Value{id, appID, repo.NewValidNullableString(""), packageID, event.Name, event.Description, event.Group, event.OrdID, event.LocalTenantID, event.ShortDescription,
   292  		event.SystemInstanceAware, event.PolicyLevel, event.CustomPolicyLevel, repo.NewNullableStringFromJSONRawMessage(event.ChangeLogEntries), repo.NewNullableStringFromJSONRawMessage(event.Links),
   293  		repo.NewNullableStringFromJSONRawMessage(event.Tags), repo.NewNullableStringFromJSONRawMessage(event.Countries), event.ReleaseStatus, event.SunsetDate,
   294  		repo.NewNullableStringFromJSONRawMessage(event.Labels), event.Visibility,
   295  		event.Disabled, repo.NewNullableStringFromJSONRawMessage(event.PartOfProducts), repo.NewNullableStringFromJSONRawMessage(event.LineOfBusiness), repo.NewNullableStringFromJSONRawMessage(event.Industry),
   296  		event.Version.Value, event.Version.Deprecated, event.Version.DeprecatedSince, event.Version.ForRemoval, event.Ready, event.CreatedAt, event.UpdatedAt, event.DeletedAt, event.Error, repo.NewNullableStringFromJSONRawMessage(event.Extensible),
   297  		repo.NewNullableStringFromJSONRawMessage(event.Successors), resourceHash, repo.NewNullableStringFromJSONRawMessage(event.Hierarchy), repo.NewNullableStringFromJSONRawMessage(event.DocumentationLabels)}
   298  }
   299  
   300  func fixModelFetchRequest(id, url string, timestamp time.Time) *model.FetchRequest {
   301  	return &model.FetchRequest{
   302  		ID:     id,
   303  		URL:    url,
   304  		Auth:   nil,
   305  		Mode:   "SINGLE",
   306  		Filter: nil,
   307  		Status: &model.FetchRequestStatus{
   308  			Condition: model.FetchRequestStatusConditionInitial,
   309  			Timestamp: timestamp,
   310  		},
   311  		ObjectType: model.EventSpecFetchRequestReference,
   312  		ObjectID:   specID,
   313  	}
   314  }
   315  
   316  func fixModelBundleReference(bundleID, eventID string) *model.BundleReference {
   317  	return &model.BundleReference{
   318  		BundleID:   str.Ptr(bundleID),
   319  		ObjectType: model.BundleEventReference,
   320  		ObjectID:   str.Ptr(eventID),
   321  	}
   322  }
   323  
   324  func fixGQLFetchRequest(url string, timestamp time.Time) *graphql.FetchRequest {
   325  	return &graphql.FetchRequest{
   326  		Filter: nil,
   327  		Mode:   graphql.FetchModeSingle,
   328  		Auth:   nil,
   329  		URL:    url,
   330  		Status: &graphql.FetchRequestStatus{
   331  			Timestamp: graphql.Timestamp(timestamp),
   332  			Condition: graphql.FetchRequestStatusConditionInitial,
   333  		},
   334  	}
   335  }
   336  
   337  func timeToTimestampPtr(time time.Time) *graphql.Timestamp {
   338  	t := graphql.Timestamp(time)
   339  	return &t
   340  }