github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/application/fixtures_test.go (about) 1 package application_test 2 3 import ( 4 "database/sql" 5 "encoding/json" 6 "net/url" 7 "testing" 8 "time" 9 10 "github.com/kyma-incubator/compass/components/director/internal/domain/api" 11 12 "github.com/kyma-incubator/compass/components/director/pkg/str" 13 14 "github.com/kyma-incubator/compass/components/director/internal/repo" 15 16 "github.com/kyma-incubator/compass/components/director/internal/domain/application" 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 "github.com/stretchr/testify/require" 21 ) 22 23 var ( 24 testURL = "https://foo.bar" 25 intSysID = "iiiiiiiii-iiii-iiii-iiii-iiiiiiiiiiii" 26 providerName = "provider name" 27 fixedTimestamp = time.Now() 28 legacyConnectorURL = "url.com" 29 systemNumber = "123" 30 localTenantID = "1337" 31 appName = "appName" 32 appNamespace = "appNamespace" 33 tbtID = "tbtID" 34 ) 35 36 func stringPtr(s string) *string { 37 return &s 38 } 39 40 func fixApplicationPage(applications []*model.Application) *model.ApplicationPage { 41 return &model.ApplicationPage{ 42 Data: applications, 43 PageInfo: &pagination.Page{ 44 StartCursor: "start", 45 EndCursor: "end", 46 HasNextPage: false, 47 }, 48 TotalCount: len(applications), 49 } 50 } 51 52 func fixGQLApplicationPage(applications []*graphql.Application) *graphql.ApplicationPage { 53 return &graphql.ApplicationPage{ 54 Data: applications, 55 PageInfo: &graphql.PageInfo{ 56 StartCursor: "start", 57 EndCursor: "end", 58 HasNextPage: false, 59 }, 60 TotalCount: len(applications), 61 } 62 } 63 64 func fixModelApplication(id, tenant, name, description string) *model.Application { 65 return &model.Application{ 66 Status: &model.ApplicationStatus{ 67 Condition: model.ApplicationStatusConditionInitial, 68 }, 69 Name: name, 70 Description: &description, 71 BaseEntity: &model.BaseEntity{ID: id}, 72 ApplicationNamespace: &appNamespace, 73 } 74 } 75 76 func fixModelApplicationWithAllUpdatableFields(id, name, description, url string, baseURL *string, applicationNamespace *string, conditionStatus model.ApplicationStatusCondition, conditionTimestamp time.Time) *model.Application { 77 return &model.Application{ 78 Status: &model.ApplicationStatus{ 79 Condition: conditionStatus, 80 Timestamp: conditionTimestamp, 81 }, 82 IntegrationSystemID: &intSysID, 83 Name: name, 84 LocalTenantID: &localTenantID, 85 Description: &description, 86 HealthCheckURL: &url, 87 ProviderName: &providerName, 88 BaseEntity: &model.BaseEntity{ID: id, UpdatedAt: &conditionTimestamp}, 89 BaseURL: baseURL, 90 ApplicationNamespace: applicationNamespace, 91 } 92 } 93 94 func fixGQLApplication(id, name, description string) *graphql.Application { 95 return &graphql.Application{ 96 BaseEntity: &graphql.BaseEntity{ 97 ID: id, 98 }, 99 Status: &graphql.ApplicationStatus{ 100 Condition: graphql.ApplicationStatusConditionInitial, 101 }, 102 Name: name, 103 Description: &description, 104 ApplicationNamespace: &appNamespace, 105 } 106 } 107 108 func fixGQLApplicationWithTenantBusinessType(id, name, description string) *graphql.Application { 109 return &graphql.Application{ 110 BaseEntity: &graphql.BaseEntity{ 111 ID: id, 112 }, 113 Status: &graphql.ApplicationStatus{ 114 Condition: graphql.ApplicationStatusConditionInitial, 115 }, 116 Name: name, 117 Description: &description, 118 TenantBusinessTypeID: &tbtID, 119 } 120 } 121 122 func fixGQLTenantBusinessType(id, name, code string) *graphql.TenantBusinessType { 123 return &graphql.TenantBusinessType{ 124 ID: id, 125 Name: name, 126 Code: code, 127 } 128 } 129 130 func fixDetailedModelApplication(t *testing.T, id, tenant, name, description string) *model.Application { 131 appStatusTimestamp, err := time.Parse(time.RFC3339, "2002-10-02T10:00:00-05:00") 132 require.NoError(t, err) 133 134 return &model.Application{ 135 ProviderName: &providerName, 136 Name: name, 137 Description: &description, 138 Status: &model.ApplicationStatus{ 139 Condition: model.ApplicationStatusConditionInitial, 140 Timestamp: appStatusTimestamp, 141 }, 142 HealthCheckURL: &testURL, 143 SystemNumber: &systemNumber, 144 LocalTenantID: &localTenantID, 145 IntegrationSystemID: &intSysID, 146 TenantBusinessTypeID: &tbtID, 147 BaseURL: str.Ptr("base_url"), 148 ApplicationNamespace: &appNamespace, 149 OrdLabels: json.RawMessage("[]"), 150 CorrelationIDs: json.RawMessage("[]"), 151 SystemStatus: str.Ptr("reachable"), 152 Tags: json.RawMessage("[]"), 153 DocumentationLabels: json.RawMessage("[]"), 154 BaseEntity: &model.BaseEntity{ 155 ID: id, 156 Ready: true, 157 Error: nil, 158 CreatedAt: &fixedTimestamp, 159 UpdatedAt: &time.Time{}, 160 DeletedAt: &time.Time{}, 161 }, 162 } 163 } 164 165 func fixDetailedGQLApplication(t *testing.T, id, name, description string) *graphql.Application { 166 appStatusTimestamp, err := time.Parse(time.RFC3339, "2002-10-02T10:00:00-05:00") 167 require.NoError(t, err) 168 169 return &graphql.Application{ 170 Status: &graphql.ApplicationStatus{ 171 Condition: graphql.ApplicationStatusConditionInitial, 172 Timestamp: graphql.Timestamp(appStatusTimestamp), 173 }, 174 Name: name, 175 SystemNumber: &systemNumber, 176 LocalTenantID: &localTenantID, 177 Description: &description, 178 HealthCheckURL: &testURL, 179 IntegrationSystemID: &intSysID, 180 TenantBusinessTypeID: &tbtID, 181 ProviderName: str.Ptr("provider name"), 182 BaseURL: str.Ptr("base_url"), 183 ApplicationNamespace: &appNamespace, 184 SystemStatus: str.Ptr("reachable"), 185 BaseEntity: &graphql.BaseEntity{ 186 ID: id, 187 Ready: true, 188 Error: nil, 189 CreatedAt: timeToTimestampPtr(fixedTimestamp), 190 UpdatedAt: timeToTimestampPtr(time.Time{}), 191 DeletedAt: timeToTimestampPtr(time.Time{}), 192 }, 193 } 194 } 195 196 func fixDetailedEntityApplication(t *testing.T, id, tenant, name, description string) *application.Entity { 197 ts, err := time.Parse(time.RFC3339, "2002-10-02T10:00:00-05:00") 198 require.NoError(t, err) 199 200 return &application.Entity{ 201 Name: name, 202 ProviderName: repo.NewNullableString(&providerName), 203 Description: repo.NewValidNullableString(description), 204 StatusCondition: string(model.ApplicationStatusConditionInitial), 205 StatusTimestamp: ts, 206 SystemNumber: repo.NewValidNullableString(systemNumber), 207 LocalTenantID: repo.NewValidNullableString(localTenantID), 208 HealthCheckURL: repo.NewValidNullableString(testURL), 209 IntegrationSystemID: repo.NewNullableString(&intSysID), 210 BaseURL: repo.NewValidNullableString("base_url"), 211 ApplicationNamespace: repo.NewValidNullableString(appNamespace), 212 OrdLabels: repo.NewValidNullableString("[]"), 213 CorrelationIDs: repo.NewValidNullableString("[]"), 214 SystemStatus: repo.NewValidNullableString("reachable"), 215 TenantBusinessTypeID: repo.NewValidNullableString(tbtID), 216 Tags: repo.NewValidNullableString("[]"), 217 DocumentationLabels: repo.NewValidNullableString("[]"), 218 BaseEntity: &repo.BaseEntity{ 219 ID: id, 220 Ready: true, 221 Error: sql.NullString{}, 222 CreatedAt: &fixedTimestamp, 223 UpdatedAt: &time.Time{}, 224 DeletedAt: &time.Time{}, 225 }, 226 } 227 } 228 229 func fixModelApplicationRegisterInput(name, description string) model.ApplicationRegisterInput { 230 desc := "Sample" 231 kind := "test" 232 return model.ApplicationRegisterInput{ 233 Name: name, 234 Description: &description, 235 Labels: map[string]interface{}{ 236 "test": []string{"val", "val2"}, 237 }, 238 HealthCheckURL: &testURL, 239 IntegrationSystemID: &intSysID, 240 LocalTenantID: &localTenantID, 241 ProviderName: &providerName, 242 Webhooks: []*model.WebhookInput{ 243 {URL: stringPtr("webhook1.foo.bar")}, 244 {URL: stringPtr("webhook2.foo.bar")}, 245 }, 246 Bundles: []*model.BundleCreateInput{ 247 { 248 Name: "foo", 249 APIDefinitions: []*model.APIDefinitionInput{ 250 {Name: "api1", TargetURLs: api.ConvertTargetURLToJSONArray("foo.bar")}, 251 {Name: "api2", TargetURLs: api.ConvertTargetURLToJSONArray("foo.bar2")}, 252 }, 253 EventDefinitions: []*model.EventDefinitionInput{ 254 {Name: "event1", Description: &desc}, 255 {Name: "event2", Description: &desc}, 256 }, 257 Documents: []*model.DocumentInput{ 258 {DisplayName: "doc1", Kind: &kind}, 259 {DisplayName: "doc2", Kind: &kind}, 260 }, 261 }, 262 }, 263 } 264 } 265 266 func fixModelApplicationUpdateInput(name, description, healthCheckURL, baseURL string, appNamespace string, statusCondition model.ApplicationStatusCondition) model.ApplicationUpdateInput { 267 return model.ApplicationUpdateInput{ 268 Description: &description, 269 HealthCheckURL: &healthCheckURL, 270 IntegrationSystemID: &intSysID, 271 ProviderName: &providerName, 272 StatusCondition: &statusCondition, 273 LocalTenantID: &localTenantID, 274 BaseURL: &baseURL, 275 ApplicationNamespace: &appNamespace, 276 } 277 } 278 279 func fixModelApplicationUpdateInputStatus(statusCondition model.ApplicationStatusCondition) model.ApplicationUpdateInput { 280 return model.ApplicationUpdateInput{ 281 StatusCondition: &statusCondition, 282 } 283 } 284 285 func fixGQLApplicationJSONInput(name, description string) graphql.ApplicationJSONInput { 286 labels := graphql.Labels{ 287 "test": []string{"val", "val2"}, 288 } 289 kind := "test" 290 desc := "Sample" 291 return graphql.ApplicationJSONInput{ 292 Name: name, 293 Description: &description, 294 Labels: labels, 295 HealthCheckURL: &testURL, 296 IntegrationSystemID: &intSysID, 297 LocalTenantID: &localTenantID, 298 ProviderName: &providerName, 299 Webhooks: []*graphql.WebhookInput{ 300 {URL: stringPtr("webhook1.foo.bar")}, 301 {URL: stringPtr("webhook2.foo.bar")}, 302 }, 303 Bundles: []*graphql.BundleCreateInput{ 304 { 305 Name: "foo", 306 APIDefinitions: []*graphql.APIDefinitionInput{ 307 {Name: "api1", TargetURL: "foo.bar"}, 308 {Name: "api2", TargetURL: "foo.bar2"}, 309 }, 310 EventDefinitions: []*graphql.EventDefinitionInput{ 311 {Name: "event1", Description: &desc}, 312 {Name: "event2", Description: &desc}, 313 }, 314 Documents: []*graphql.DocumentInput{ 315 {DisplayName: "doc1", Kind: &kind}, 316 {DisplayName: "doc2", Kind: &kind}, 317 }, 318 }, 319 }, 320 } 321 } 322 323 func fixGQLApplicationRegisterInput(name, description string) graphql.ApplicationRegisterInput { 324 labels := graphql.Labels{ 325 "test": []string{"val", "val2"}, 326 } 327 kind := "test" 328 desc := "Sample" 329 return graphql.ApplicationRegisterInput{ 330 Name: name, 331 Description: &description, 332 Labels: labels, 333 HealthCheckURL: &testURL, 334 IntegrationSystemID: &intSysID, 335 LocalTenantID: &localTenantID, 336 ProviderName: &providerName, 337 Webhooks: []*graphql.WebhookInput{ 338 {URL: stringPtr("webhook1.foo.bar")}, 339 {URL: stringPtr("webhook2.foo.bar")}, 340 }, 341 Bundles: []*graphql.BundleCreateInput{ 342 { 343 Name: "foo", 344 APIDefinitions: []*graphql.APIDefinitionInput{ 345 {Name: "api1", TargetURL: "foo.bar"}, 346 {Name: "api2", TargetURL: "foo.bar2"}, 347 }, 348 EventDefinitions: []*graphql.EventDefinitionInput{ 349 {Name: "event1", Description: &desc}, 350 {Name: "event2", Description: &desc}, 351 }, 352 Documents: []*graphql.DocumentInput{ 353 {DisplayName: "doc1", Kind: &kind}, 354 {DisplayName: "doc2", Kind: &kind}, 355 }, 356 }, 357 }, 358 } 359 } 360 361 func fixGQLApplicationUpdateInput(name, description, healthCheckURL, baseURL string, appNamespace string, statusCondition graphql.ApplicationStatusCondition) graphql.ApplicationUpdateInput { 362 return graphql.ApplicationUpdateInput{ 363 Description: &description, 364 HealthCheckURL: &healthCheckURL, 365 IntegrationSystemID: &intSysID, 366 LocalTenantID: &localTenantID, 367 ProviderName: &providerName, 368 StatusCondition: &statusCondition, 369 BaseURL: &baseURL, 370 ApplicationNamespace: &appNamespace, 371 } 372 } 373 374 func fixModelWebhook(appID, id string) *model.Webhook { 375 return &model.Webhook{ 376 ObjectID: appID, 377 ObjectType: model.ApplicationWebhookReference, 378 ID: id, 379 Type: model.WebhookTypeConfigurationChanged, 380 URL: stringPtr("foourl"), 381 Auth: &model.Auth{}, 382 } 383 } 384 385 func fixGQLWebhook(id string) *graphql.Webhook { 386 return &graphql.Webhook{ 387 ID: id, 388 Type: graphql.WebhookTypeConfigurationChanged, 389 URL: stringPtr("foourl"), 390 Auth: &graphql.Auth{}, 391 } 392 } 393 394 func fixLabelInput(key string, value string, objectID string, objectType model.LabelableObject) *model.LabelInput { 395 return &model.LabelInput{ 396 Key: key, 397 Value: value, 398 ObjectID: objectID, 399 ObjectType: objectType, 400 } 401 } 402 403 func fixModelApplicationEventingConfiguration(t *testing.T, rawURL string) *model.ApplicationEventingConfiguration { 404 validURL, err := url.Parse(rawURL) 405 require.NoError(t, err) 406 require.NotNil(t, validURL) 407 return &model.ApplicationEventingConfiguration{ 408 EventingConfiguration: model.EventingConfiguration{ 409 DefaultURL: *validURL, 410 }, 411 } 412 } 413 414 func fixGQLApplicationEventingConfiguration(url string) *graphql.ApplicationEventingConfiguration { 415 return &graphql.ApplicationEventingConfiguration{ 416 DefaultURL: url, 417 } 418 } 419 420 func fixModelBundle(id, appID, name, description string) *model.Bundle { 421 return &model.Bundle{ 422 ApplicationID: &appID, 423 Name: name, 424 Description: &description, 425 InstanceAuthRequestInputSchema: nil, 426 DefaultInstanceAuth: nil, 427 BaseEntity: &model.BaseEntity{ID: id}, 428 } 429 } 430 431 func fixGQLBundle(id, name, description string) *graphql.Bundle { 432 return &graphql.Bundle{ 433 BaseEntity: &graphql.BaseEntity{ 434 ID: id, 435 }, 436 Name: name, 437 Description: &description, 438 InstanceAuthRequestInputSchema: nil, 439 DefaultInstanceAuth: nil, 440 } 441 } 442 443 func fixGQLBundlePage(bundles []*graphql.Bundle) *graphql.BundlePage { 444 return &graphql.BundlePage{ 445 Data: bundles, 446 PageInfo: &graphql.PageInfo{ 447 StartCursor: "start", 448 EndCursor: "end", 449 HasNextPage: false, 450 }, 451 TotalCount: len(bundles), 452 } 453 } 454 455 func fixBundlePage(bundles []*model.Bundle) *model.BundlePage { 456 return &model.BundlePage{ 457 Data: bundles, 458 PageInfo: &pagination.Page{ 459 StartCursor: "start", 460 EndCursor: "end", 461 HasNextPage: false, 462 }, 463 TotalCount: len(bundles), 464 } 465 } 466 467 func timeToTimestampPtr(time time.Time) *graphql.Timestamp { 468 t := graphql.Timestamp(time) 469 return &t 470 } 471 472 func fixAppColumns() []string { 473 return []string{"id", "app_template_id", "system_number", "local_tenant_id", "name", "description", "status_condition", "status_timestamp", "system_status", "healthcheck_url", "integration_system_id", "provider_name", "base_url", "application_namespace", "labels", "ready", "created_at", "updated_at", "deleted_at", "error", "correlation_ids", "tags", "documentation_labels", "tenant_business_type_id"} 474 } 475 476 func fixApplicationLabels(appID, labelKey1, labelKey2 string, labelValue1 []interface{}, labelValue2 string) map[string]*model.Label { 477 tnt := "tenant" 478 479 return map[string]*model.Label{ 480 labelKey1: { 481 ID: "abc", 482 Tenant: str.Ptr(tnt), 483 Key: labelKey1, 484 Value: labelValue1, 485 ObjectID: appID, 486 ObjectType: model.ApplicationLabelableObject, 487 }, 488 labelKey2: { 489 ID: "def", 490 Tenant: str.Ptr(tnt), 491 Key: labelKey2, 492 Value: labelValue2, 493 ObjectID: appID, 494 ObjectType: model.ApplicationLabelableObject, 495 }, 496 } 497 } 498 499 func fixModelApplicationTemplate(id, name string) *model.ApplicationTemplate { 500 desc := "Description for template" 501 return &model.ApplicationTemplate{ 502 ID: id, 503 Name: name, 504 Description: &desc, 505 AccessLevel: model.GlobalApplicationTemplateAccessLevel, 506 } 507 } 508 509 func fixModelTenantBusinessType(id, code, name string) *model.TenantBusinessType { 510 return &model.TenantBusinessType{ 511 ID: id, 512 Code: code, 513 Name: name, 514 } 515 } 516 517 func fixGQLApplicationTemplate(id, name string) *graphql.ApplicationTemplate { 518 desc := "Description for template" 519 return &graphql.ApplicationTemplate{ 520 ID: id, 521 Name: name, 522 Description: &desc, 523 AccessLevel: graphql.ApplicationTemplateAccessLevel(model.GlobalApplicationTemplateAccessLevel), 524 } 525 } 526 527 func fixGQLApplicationWithAppTemplate(id, name, description, appTemplateID string) *graphql.Application { 528 return &graphql.Application{ 529 BaseEntity: &graphql.BaseEntity{ 530 ID: id, 531 }, 532 Status: &graphql.ApplicationStatus{ 533 Condition: graphql.ApplicationStatusConditionInitial, 534 }, 535 Name: name, 536 Description: &description, 537 ApplicationNamespace: &appNamespace, 538 ApplicationTemplateID: str.Ptr(appTemplateID), 539 } 540 }