github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/webhook/repository_test.go (about) 1 package webhook_test 2 3 import ( 4 "context" 5 "database/sql" 6 "database/sql/driver" 7 "fmt" 8 "regexp" 9 "testing" 10 "time" 11 12 "github.com/kyma-incubator/compass/components/director/internal/repo" 13 "github.com/kyma-incubator/compass/components/director/pkg/persistence" 14 "github.com/stretchr/testify/assert" 15 "github.com/stretchr/testify/mock" 16 "github.com/stretchr/testify/require" 17 18 "github.com/DATA-DOG/go-sqlmock" 19 "github.com/kyma-incubator/compass/components/director/internal/domain/webhook" 20 "github.com/kyma-incubator/compass/components/director/internal/domain/webhook/automock" 21 "github.com/kyma-incubator/compass/components/director/internal/model" 22 "github.com/kyma-incubator/compass/components/director/internal/repo/testdb" 23 ) 24 25 func TestRepositoryGetByID(t *testing.T) { 26 createdAt := time.Now() 27 whModel := fixApplicationModelWebhook(givenID(), givenApplicationID(), givenTenant(), "http://kyma.io", createdAt) 28 whEntity := fixApplicationWebhookEntity(t, createdAt) 29 30 suite := testdb.RepoGetTestSuite{ 31 Name: "Get Webhook By ID", 32 SQLQueryDetails: []testdb.SQLQueryDetails{ 33 { 34 Query: regexp.QuoteMeta(`SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE id = $1 AND (id IN (SELECT id FROM application_webhooks_tenants WHERE tenant_id = $2))`), 35 Args: []driver.Value{givenID(), givenTenant()}, 36 IsSelect: true, 37 ValidRowsProvider: func() []*sqlmock.Rows { 38 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns).AddRow(whModel.ID, givenApplicationID(), nil, whModel.Type, whModel.URL, fixAuthAsAString(t), nil, nil, whModel.Mode, whModel.CorrelationIDKey, whModel.RetryInterval, whModel.Timeout, whModel.URLTemplate, whModel.InputTemplate, whModel.HeaderTemplate, whModel.OutputTemplate, whModel.StatusTemplate, whModel.CreatedAt, nil)} 39 }, 40 InvalidRowsProvider: func() []*sqlmock.Rows { 41 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns)} 42 }, 43 }, 44 }, 45 ConverterMockProvider: func() testdb.Mock { 46 return &automock.EntityConverter{} 47 }, 48 RepoConstructorFunc: webhook.NewRepository, 49 ExpectedModelEntity: whModel, 50 ExpectedDBEntity: whEntity, 51 MethodArgs: []interface{}{givenTenant(), givenID(), model.ApplicationWebhookReference}, 52 } 53 54 suite.Run(t) 55 } 56 57 func TestRepositoryGetByIDGlobal(t *testing.T) { 58 whType := model.WebhookTypeConfigurationChanged 59 whModel := fixApplicationTemplateModelWebhookWithType(givenID(), givenApplicationTemplateID(), "http://kyma.io", whType) 60 whEntity := fixApplicationTemplateWebhookEntity(t) 61 62 // GIVEN 63 mockConverter := &automock.EntityConverter{} 64 defer mockConverter.AssertExpectations(t) 65 mockConverter.On("FromEntity", whEntity).Return(whModel, nil) 66 67 db, dbMock := testdb.MockDatabase(t) 68 defer dbMock.AssertExpectations(t) 69 70 rows := sqlmock.NewRows(fixColumns). 71 AddRow(whModel.ID, nil, givenApplicationTemplateID(), whModel.Type, whModel.URL, fixAuthAsAString(t), nil, nil, whModel.Mode, whModel.CorrelationIDKey, whModel.RetryInterval, whModel.Timeout, whModel.URLTemplate, whModel.InputTemplate, whModel.HeaderTemplate, whModel.OutputTemplate, whModel.StatusTemplate, nil, nil) 72 73 dbMock.ExpectQuery(regexp.QuoteMeta("SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE id = $1")). 74 WithArgs(givenID()).WillReturnRows(rows) 75 76 ctx := persistence.SaveToContext(context.TODO(), db) 77 sut := webhook.NewRepository(mockConverter) 78 // WHEN 79 wh, err := sut.GetByIDGlobal(ctx, givenID()) 80 // THEN 81 require.NoError(t, err) 82 require.Equal(t, whModel, wh) 83 mockConverter.AssertExpectations(t) 84 } 85 86 func TestRepositoryCreate(t *testing.T) { 87 var nilWebhookModel *model.Webhook 88 createdAt := time.Now() 89 90 suite := testdb.RepoCreateTestSuite{ 91 Name: "Create Application webhook", 92 SQLQueryDetails: []testdb.SQLQueryDetails{ 93 { 94 Query: regexp.QuoteMeta("SELECT 1 FROM tenant_applications WHERE tenant_id = $1 AND id = $2 AND owner = $3"), 95 Args: []driver.Value{givenTenant(), givenApplicationID(), true}, 96 IsSelect: true, 97 ValidRowsProvider: func() []*sqlmock.Rows { 98 return []*sqlmock.Rows{testdb.RowWhenObjectExist()} 99 }, 100 InvalidRowsProvider: func() []*sqlmock.Rows { 101 return []*sqlmock.Rows{testdb.RowWhenObjectDoesNotExist()} 102 }, 103 }, 104 { 105 Query: regexp.QuoteMeta("INSERT INTO public.webhooks ( id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )"), 106 Args: []driver.Value{givenID(), givenApplicationID(), sql.NullString{}, string(model.WebhookTypeConfigurationChanged), "http://kyma.io", fixAuthAsAString(t), nil, nil, model.WebhookModeSync, nil, nil, nil, "{}", "{}", "{}", "{}", nil, createdAt, nil}, 107 ValidResult: sqlmock.NewResult(-1, 1), 108 }, 109 }, 110 ConverterMockProvider: func() testdb.Mock { 111 return &automock.EntityConverter{} 112 }, 113 RepoConstructorFunc: webhook.NewRepository, 114 ModelEntity: fixApplicationModelWebhook(givenID(), givenApplicationID(), givenTenant(), "http://kyma.io", createdAt), 115 DBEntity: fixApplicationWebhookEntity(t, createdAt), 116 NilModelEntity: nilWebhookModel, 117 TenantID: givenTenant(), 118 } 119 120 suite.Run(t) 121 122 // Additional tests for application template webhook -> global create 123 t.Run("Create ApplicationTemplate webhook", func(t *testing.T) { 124 tmplWhModel := fixApplicationTemplateModelWebhook(givenID(), givenApplicationTemplateID(), "http://kyma.io") 125 tmplWhModel.CreatedAt = &createdAt 126 127 tmplWhEntity := fixApplicationTemplateWebhookEntity(t) 128 tmplWhEntity.CreatedAt = &createdAt 129 // GIVEN 130 mockConverter := &automock.EntityConverter{} 131 defer mockConverter.AssertExpectations(t) 132 mockConverter.On("ToEntity", tmplWhModel).Return(tmplWhEntity, nil) 133 134 db, dbMock := testdb.MockDatabase(t) 135 defer dbMock.AssertExpectations(t) 136 137 dbMock.ExpectExec(regexp.QuoteMeta("INSERT INTO public.webhooks ( id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )")).WithArgs( 138 givenID(), sql.NullString{}, givenApplicationTemplateID(), string(model.WebhookTypeConfigurationChanged), "http://kyma.io", fixAuthAsAString(t), nil, nil, model.WebhookModeSync, nil, nil, nil, "{}", "{}", "{}", "{}", nil, createdAt, nil).WillReturnResult(sqlmock.NewResult(-1, 1)) 139 140 ctx := persistence.SaveToContext(context.TODO(), db) 141 sut := webhook.NewRepository(mockConverter) 142 // WHEN 143 err := sut.Create(ctx, "", tmplWhModel) 144 // THEN 145 require.NoError(t, err) 146 }) 147 } 148 149 func TestRepositoryCreateMany(t *testing.T) { 150 createdAt := time.Now() 151 expectedParentAccess := regexp.QuoteMeta("SELECT 1 FROM tenant_applications WHERE tenant_id = $1 AND id = $2 AND owner = $3") 152 expectedInsert := regexp.QuoteMeta("INSERT INTO public.webhooks ( id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id ) VALUES ( ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ? )") 153 154 t.Run("success", func(t *testing.T) { 155 // GIVEN 156 mockConverter := &automock.EntityConverter{} 157 defer mockConverter.AssertExpectations(t) 158 159 given := []*model.Webhook{ 160 {ID: "one", ObjectID: givenApplicationID(), ObjectType: model.ApplicationWebhookReference, CreatedAt: &createdAt}, 161 {ID: "two", ObjectID: givenApplicationID(), ObjectType: model.ApplicationWebhookReference, CreatedAt: &createdAt}, 162 {ID: "three", ObjectID: givenApplicationID(), ObjectType: model.ApplicationWebhookReference, CreatedAt: &createdAt}, 163 } 164 mockConverter.On("ToEntity", given[0]).Return(&webhook.Entity{ID: "one", ApplicationID: repo.NewValidNullableString(givenApplicationID()), CreatedAt: &createdAt}, nil) 165 mockConverter.On("ToEntity", given[1]).Return(&webhook.Entity{ID: "two", ApplicationID: repo.NewValidNullableString(givenApplicationID()), CreatedAt: &createdAt}, nil) 166 mockConverter.On("ToEntity", given[2]).Return(&webhook.Entity{ID: "three", ApplicationID: repo.NewValidNullableString(givenApplicationID()), CreatedAt: &createdAt}, nil) 167 168 db, dbMock := testdb.MockDatabase(t) 169 defer dbMock.AssertExpectations(t) 170 171 dbMock.ExpectQuery(expectedParentAccess).WithArgs(givenTenant(), givenApplicationID(), true).WillReturnRows(testdb.RowWhenObjectExist()) 172 dbMock.ExpectExec(expectedInsert).WithArgs( 173 "one", givenApplicationID(), nil, "", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, createdAt, nil).WillReturnResult(sqlmock.NewResult(-1, 1)) 174 dbMock.ExpectQuery(expectedParentAccess).WithArgs(givenTenant(), givenApplicationID(), true).WillReturnRows(testdb.RowWhenObjectExist()) 175 dbMock.ExpectExec(expectedInsert).WithArgs( 176 "two", givenApplicationID(), nil, "", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, createdAt, nil).WillReturnResult(sqlmock.NewResult(-1, 1)) 177 dbMock.ExpectQuery(expectedParentAccess).WithArgs(givenTenant(), givenApplicationID(), true).WillReturnRows(testdb.RowWhenObjectExist()) 178 dbMock.ExpectExec(expectedInsert).WithArgs( 179 "three", givenApplicationID(), nil, "", nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, nil, createdAt, nil).WillReturnResult(sqlmock.NewResult(-1, 1)) 180 181 ctx := persistence.SaveToContext(context.TODO(), db) 182 sut := webhook.NewRepository(mockConverter) 183 // WHEN 184 err := sut.CreateMany(ctx, givenTenant(), given) 185 // THEN 186 require.NoError(t, err) 187 }) 188 189 t.Run("got error on converting object", func(t *testing.T) { 190 // GIVEN 191 mockConverter := &automock.EntityConverter{} 192 defer mockConverter.AssertExpectations(t) 193 194 given := []*model.Webhook{ 195 {ID: "one", URL: stringPtr("unlucky"), Type: model.WebhookTypeConfigurationChanged, ObjectID: givenApplicationID(), ObjectType: model.ApplicationWebhookReference}, 196 {ID: "two", ObjectID: givenApplicationID(), ObjectType: model.ApplicationWebhookReference}, 197 {ID: "three", ObjectID: givenApplicationID(), ObjectType: model.ApplicationWebhookReference}} 198 mockConverter.On("ToEntity", given[0]).Return(&webhook.Entity{}, givenError()) 199 200 ctx := persistence.SaveToContext(context.TODO(), nil) 201 sut := webhook.NewRepository(mockConverter) 202 // WHEN 203 err := sut.CreateMany(ctx, givenTenant(), given) 204 // THEN 205 expectedErr := fmt.Sprintf("while creating Webhook with type %s and id %s for %s: while converting model to entity: some error", model.WebhookTypeConfigurationChanged, "one", model.ApplicationWebhookReference) 206 require.EqualError(t, err, expectedErr) 207 }) 208 209 t.Run("got error on db communication", func(t *testing.T) { 210 // GIVEN 211 mockConverter := &automock.EntityConverter{} 212 defer mockConverter.AssertExpectations(t) 213 214 given := []*model.Webhook{ 215 {ID: "one", URL: stringPtr("unlucky"), Type: model.WebhookTypeConfigurationChanged, ObjectID: givenApplicationID(), ObjectType: model.ApplicationWebhookReference}, 216 {ID: "two", ObjectID: givenApplicationID(), ObjectType: model.ApplicationWebhookReference}, 217 {ID: "three", ObjectID: givenApplicationID(), ObjectType: model.ApplicationWebhookReference}} 218 mockConverter.On("ToEntity", given[0]).Return(&webhook.Entity{ID: "one", ApplicationID: repo.NewValidNullableString(givenApplicationID())}, nil) 219 220 db, dbMock := testdb.MockDatabase(t) 221 defer dbMock.AssertExpectations(t) 222 223 dbMock.ExpectQuery(expectedParentAccess).WithArgs(givenTenant(), givenApplicationID(), true).WillReturnRows(testdb.RowWhenObjectExist()) 224 dbMock.ExpectExec(expectedInsert).WillReturnError(givenError()) 225 226 ctx := persistence.SaveToContext(context.TODO(), db) 227 sut := webhook.NewRepository(mockConverter) 228 // WHEN 229 err := sut.CreateMany(ctx, givenTenant(), given) 230 // THEN 231 expectedErr := fmt.Sprintf("while creating Webhook with type %s and id %s for %s: Internal Server Error: Unexpected error while executing SQL query", model.WebhookTypeConfigurationChanged, "one", model.ApplicationWebhookReference) 232 require.EqualError(t, err, expectedErr) 233 }) 234 } 235 236 func TestRepositoryUpdate(t *testing.T) { 237 var nilWebhookModel *model.Webhook 238 createdAt := time.Now() 239 240 suite := testdb.RepoUpdateTestSuite{ 241 Name: "Update Application webhook", 242 SQLQueryDetails: []testdb.SQLQueryDetails{ 243 { 244 Query: regexp.QuoteMeta(`UPDATE public.webhooks SET type = ?, url = ?, auth = ?, mode = ?, retry_interval = ?, timeout = ?, url_template = ?, input_template = ?, header_template = ?, output_template = ?, status_template = ? WHERE id = ? AND (id IN (SELECT id FROM application_webhooks_tenants WHERE tenant_id = ? AND owner = true))`), 245 Args: []driver.Value{string(model.WebhookTypeConfigurationChanged), "http://kyma.io", fixAuthAsAString(t), model.WebhookModeSync, nil, nil, "{}", "{}", "{}", "{}", nil, givenID(), givenTenant()}, 246 ValidResult: sqlmock.NewResult(-1, 1), 247 InvalidResult: sqlmock.NewResult(-1, 0), 248 }, 249 }, 250 ConverterMockProvider: func() testdb.Mock { 251 return &automock.EntityConverter{} 252 }, 253 RepoConstructorFunc: webhook.NewRepository, 254 ModelEntity: fixApplicationModelWebhook(givenID(), givenApplicationID(), givenTenant(), "http://kyma.io", createdAt), 255 DBEntity: fixApplicationWebhookEntity(t, createdAt), 256 NilModelEntity: nilWebhookModel, 257 TenantID: givenTenant(), 258 } 259 260 suite.Run(t) 261 262 // Additional tests for application template webhook -> global create 263 t.Run("Update ApplicationTemplate webhook", func(t *testing.T) { 264 tmplWhModel := fixApplicationTemplateModelWebhook(givenID(), givenApplicationTemplateID(), "http://kyma.io") 265 // GIVEN 266 mockConverter := &automock.EntityConverter{} 267 defer mockConverter.AssertExpectations(t) 268 mockConverter.On("ToEntity", tmplWhModel).Return(fixApplicationTemplateWebhookEntity(t), nil) 269 270 db, dbMock := testdb.MockDatabase(t) 271 defer dbMock.AssertExpectations(t) 272 273 dbMock.ExpectExec(regexp.QuoteMeta(`UPDATE public.webhooks SET type = ?, url = ?, auth = ?, mode = ?, retry_interval = ?, timeout = ?, url_template = ?, input_template = ?, header_template = ?, output_template = ?, status_template = ? WHERE id = ? AND app_template_id = ?`)). 274 WithArgs(string(model.WebhookTypeConfigurationChanged), "http://kyma.io", fixAuthAsAString(t), model.WebhookModeSync, nil, nil, "{}", "{}", "{}", "{}", nil, givenID(), givenApplicationTemplateID()).WillReturnResult(sqlmock.NewResult(-1, 1)) 275 276 ctx := persistence.SaveToContext(context.TODO(), db) 277 sut := webhook.NewRepository(mockConverter) 278 // WHEN 279 err := sut.Update(ctx, "", tmplWhModel) 280 // THEN 281 require.NoError(t, err) 282 }) 283 t.Run("Update FormationTemplate webhook", func(t *testing.T) { 284 tmplWhModel := fixFormationTemplateModelWebhook(givenID(), givenFormationTemplateID(), "http://kyma.io") 285 // GIVEN 286 mockConverter := &automock.EntityConverter{} 287 defer mockConverter.AssertExpectations(t) 288 mockConverter.On("ToEntity", tmplWhModel).Return(fixFormationTemplateWebhookEntityWithID(t, givenID()), nil) 289 290 db, dbMock := testdb.MockDatabase(t) 291 defer dbMock.AssertExpectations(t) 292 293 dbMock.ExpectExec(regexp.QuoteMeta(`UPDATE public.webhooks SET type = ?, url = ?, auth = ?, mode = ?, retry_interval = ?, timeout = ?, url_template = ?, input_template = ?, header_template = ?, output_template = ?, status_template = ? WHERE id = ? AND formation_template_id = ?`)). 294 WithArgs(string(model.WebhookTypeFormationLifecycle), "http://kyma.io", fixAuthAsAString(t), model.WebhookModeSync, nil, nil, "{}", "{}", "{}", "{}", nil, givenID(), givenFormationTemplateID()).WillReturnResult(sqlmock.NewResult(-1, 1)) 295 296 ctx := persistence.SaveToContext(context.TODO(), db) 297 sut := webhook.NewRepository(mockConverter) 298 // WHEN 299 err := sut.Update(ctx, "", tmplWhModel) 300 // THEN 301 require.NoError(t, err) 302 }) 303 } 304 305 func TestRepositoryDelete(t *testing.T) { 306 t.Run("success", func(t *testing.T) { 307 // GIVEN 308 db, dbMock := testdb.MockDatabase(t) 309 defer dbMock.AssertExpectations(t) 310 311 dbMock.ExpectExec(regexp.QuoteMeta("DELETE FROM public.webhooks WHERE id = $1")).WithArgs( 312 givenID()).WillReturnResult(sqlmock.NewResult(-1, 1)) 313 314 ctx := persistence.SaveToContext(context.TODO(), db) 315 sut := webhook.NewRepository(nil) 316 // WHEN 317 err := sut.Delete(ctx, givenID()) 318 // THEN 319 require.NoError(t, err) 320 }) 321 322 t.Run("got error on db communication", func(t *testing.T) { 323 // GIVEN 324 db, dbMock := testdb.MockDatabase(t) 325 defer dbMock.AssertExpectations(t) 326 327 dbMock.ExpectExec("DELETE FROM .*").WithArgs( 328 givenID()).WillReturnError(givenError()) 329 330 ctx := persistence.SaveToContext(context.TODO(), db) 331 sut := webhook.NewRepository(nil) 332 // WHEN 333 err := sut.Delete(ctx, givenID()) 334 // THEN 335 require.EqualError(t, err, "Internal Server Error: Unexpected error while executing SQL query") 336 }) 337 } 338 339 func TestRepositoryDeleteAllByApplicationID(t *testing.T) { 340 suite := testdb.RepoDeleteTestSuite{ 341 Name: "Webhook Delete by ApplicationID", 342 SQLQueryDetails: []testdb.SQLQueryDetails{ 343 { 344 Query: regexp.QuoteMeta(`DELETE FROM public.webhooks WHERE app_id = $1 AND (id IN (SELECT id FROM application_webhooks_tenants WHERE tenant_id = $2 AND owner = true))`), 345 Args: []driver.Value{givenApplicationID(), givenTenant()}, 346 ValidResult: sqlmock.NewResult(-1, 1), 347 InvalidResult: sqlmock.NewResult(-1, 2), 348 }, 349 }, 350 ConverterMockProvider: func() testdb.Mock { 351 return &automock.EntityConverter{} 352 }, 353 RepoConstructorFunc: webhook.NewRepository, 354 MethodArgs: []interface{}{givenTenant(), givenApplicationID()}, 355 MethodName: "DeleteAllByApplicationID", 356 IsDeleteMany: true, 357 } 358 359 suite.Run(t) 360 } 361 362 func TestRepositoryListByApplicationID(t *testing.T) { 363 testListByObjectID(t, "ListByReferenceObjectID", repo.NoLock, []interface{}{givenTenant(), givenApplicationID(), model.ApplicationWebhookReference}) 364 } 365 366 func TestRepositoryDeleteAllByApplicatioTemplateID(t *testing.T) { 367 suite := testdb.RepoDeleteTestSuite{ 368 Name: "Webhook Delete by ApplicationTemplateID", 369 SQLQueryDetails: []testdb.SQLQueryDetails{ 370 { 371 Query: regexp.QuoteMeta(`DELETE FROM public.webhooks WHERE app_template_id = $1`), 372 Args: []driver.Value{givenApplicationID()}, 373 ValidResult: sqlmock.NewResult(-1, 1), 374 InvalidResult: sqlmock.NewResult(-1, 2), 375 }, 376 }, 377 ConverterMockProvider: func() testdb.Mock { 378 return &automock.EntityConverter{} 379 }, 380 RepoConstructorFunc: webhook.NewRepository, 381 MethodArgs: []interface{}{givenApplicationID()}, 382 MethodName: "DeleteAllByApplicationTemplateID", 383 IsDeleteMany: true, 384 } 385 386 suite.Run(t) 387 } 388 389 func TestRepositoryListByApplicationIDWithSelectForUpdate(t *testing.T) { 390 testListByObjectID(t, "ListByApplicationIDWithSelectForUpdate", " "+repo.ForUpdateLock, []interface{}{givenTenant(), givenApplicationID()}) 391 } 392 393 func TestRepositoryListByRuntimeID(t *testing.T) { 394 whID1 := "whID1" 395 whID2 := "whID2" 396 whModel1 := fixRuntimeModelWebhook(whID1, givenRuntimeID(), "http://kyma.io") 397 whEntity1 := fixRuntimeWebhookEntityWithID(t, whID1) 398 399 whModel2 := fixRuntimeModelWebhook(whID2, givenRuntimeID(), "http://kyma.io") 400 whEntity2 := fixRuntimeWebhookEntityWithID(t, whID2) 401 402 suite := testdb.RepoListTestSuite{ 403 Name: "List Webhooks by Runtime ID", 404 SQLQueryDetails: []testdb.SQLQueryDetails{ 405 { 406 Query: regexp.QuoteMeta(`SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE runtime_id = $1 AND (id IN (SELECT id FROM runtime_webhooks_tenants WHERE tenant_id = $2))`), 407 Args: []driver.Value{givenRuntimeID(), givenTenant()}, 408 IsSelect: true, 409 ValidRowsProvider: func() []*sqlmock.Rows { 410 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns). 411 AddRow(whModel1.ID, nil, nil, whModel1.Type, whModel1.URL, fixAuthAsAString(t), givenRuntimeID(), nil, whModel1.Mode, whModel1.CorrelationIDKey, whModel1.RetryInterval, whModel1.Timeout, whModel1.URLTemplate, whModel1.InputTemplate, whModel1.HeaderTemplate, whModel1.OutputTemplate, whModel1.StatusTemplate, whModel1.CreatedAt, nil). 412 AddRow(whModel2.ID, nil, nil, whModel2.Type, whModel2.URL, fixAuthAsAString(t), givenRuntimeID(), nil, whModel2.Mode, whModel2.CorrelationIDKey, whModel2.RetryInterval, whModel2.Timeout, whModel2.URLTemplate, whModel2.InputTemplate, whModel2.HeaderTemplate, whModel2.OutputTemplate, whModel2.StatusTemplate, whModel2.CreatedAt, nil), 413 } 414 }, 415 InvalidRowsProvider: func() []*sqlmock.Rows { 416 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns)} 417 }, 418 }, 419 }, 420 ConverterMockProvider: func() testdb.Mock { 421 return &automock.EntityConverter{} 422 }, 423 RepoConstructorFunc: webhook.NewRepository, 424 ExpectedModelEntities: []interface{}{whModel1, whModel2}, 425 ExpectedDBEntities: []interface{}{whEntity1, whEntity2}, 426 MethodArgs: []interface{}{givenTenant(), givenRuntimeID(), model.RuntimeWebhookReference}, 427 MethodName: "ListByReferenceObjectID", 428 } 429 430 suite.Run(t) 431 432 t.Run("Error when webhookReferenceObjectType is not supported", func(t *testing.T) { 433 repository := webhook.NewRepository(nil) 434 _, err := repository.ListByReferenceObjectID(context.TODO(), "", "", model.IntegrationSystemWebhookReference) 435 assert.Error(t, err) 436 assert.Contains(t, err.Error(), "referenced object should be one of application, application template, runtime or formation template") 437 }) 438 } 439 440 func TestRepositoryListByFormationTemplateID(t *testing.T) { 441 whID1 := "whID1" 442 whID2 := "whID2" 443 whModel1 := fixFormationTemplateModelWebhook(whID1, givenFormationTemplateID(), "http://kyma.io") 444 whEntity1 := fixFormationTemplateWebhookEntityWithID(t, whID1) 445 446 whModel2 := fixFormationTemplateModelWebhook(whID2, givenFormationTemplateID(), "http://kyma.io") 447 whEntity2 := fixFormationTemplateWebhookEntityWithID(t, whID2) 448 449 suite := testdb.RepoListTestSuite{ 450 Name: "List Webhooks by Formation Template ID", 451 SQLQueryDetails: []testdb.SQLQueryDetails{ 452 { 453 Query: regexp.QuoteMeta(`SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE formation_template_id = $1 AND (id IN (SELECT id FROM formation_templates_webhooks_tenants WHERE tenant_id = $2))`), 454 Args: []driver.Value{givenFormationTemplateID(), givenTenant()}, 455 IsSelect: true, 456 ValidRowsProvider: func() []*sqlmock.Rows { 457 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns). 458 AddRow(whModel1.ID, nil, nil, whModel1.Type, whModel1.URL, fixAuthAsAString(t), nil, nil, whModel1.Mode, whModel1.CorrelationIDKey, whModel1.RetryInterval, whModel1.Timeout, whModel1.URLTemplate, whModel1.InputTemplate, whModel1.HeaderTemplate, whModel1.OutputTemplate, whModel1.StatusTemplate, whModel1.CreatedAt, givenFormationTemplateID()). 459 AddRow(whModel2.ID, nil, nil, whModel2.Type, whModel2.URL, fixAuthAsAString(t), nil, nil, whModel2.Mode, whModel2.CorrelationIDKey, whModel2.RetryInterval, whModel2.Timeout, whModel2.URLTemplate, whModel2.InputTemplate, whModel2.HeaderTemplate, whModel2.OutputTemplate, whModel2.StatusTemplate, whModel2.CreatedAt, givenFormationTemplateID()), 460 } 461 }, 462 InvalidRowsProvider: func() []*sqlmock.Rows { 463 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns)} 464 }, 465 }, 466 }, 467 ConverterMockProvider: func() testdb.Mock { 468 return &automock.EntityConverter{} 469 }, 470 RepoConstructorFunc: webhook.NewRepository, 471 ExpectedModelEntities: []interface{}{whModel1, whModel2}, 472 ExpectedDBEntities: []interface{}{whEntity1, whEntity2}, 473 MethodArgs: []interface{}{givenTenant(), givenFormationTemplateID(), model.FormationTemplateWebhookReference}, 474 MethodName: "ListByReferenceObjectID", 475 } 476 477 suite.Run(t) 478 479 t.Run("Error when webhookReferenceObjectType is not supported", func(t *testing.T) { 480 repository := webhook.NewRepository(nil) 481 _, err := repository.ListByReferenceObjectID(context.TODO(), "", "", model.IntegrationSystemWebhookReference) 482 assert.Error(t, err) 483 assert.Contains(t, err.Error(), "referenced object should be one of application, application template, runtime or formation template") 484 }) 485 } 486 487 func TestRepositoryListByFormationTemplateIDGlobal(t *testing.T) { 488 whID1 := "whID1" 489 whID2 := "whID2" 490 whModel1 := fixFormationTemplateModelWebhook(whID1, givenFormationTemplateID(), "http://kyma.io") 491 whEntity1 := fixFormationTemplateWebhookEntityWithID(t, whID1) 492 493 whModel2 := fixFormationTemplateModelWebhook(whID2, givenFormationTemplateID(), "http://kyma.io") 494 whEntity2 := fixFormationTemplateWebhookEntityWithID(t, whID2) 495 496 suite := testdb.RepoListTestSuite{ 497 Name: "List Webhooks by Formation Template ID Global", 498 SQLQueryDetails: []testdb.SQLQueryDetails{ 499 { 500 Query: regexp.QuoteMeta(`SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE formation_template_id = $1`), 501 Args: []driver.Value{givenFormationTemplateID()}, 502 IsSelect: true, 503 ValidRowsProvider: func() []*sqlmock.Rows { 504 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns). 505 AddRow(whModel1.ID, nil, nil, whModel1.Type, whModel1.URL, fixAuthAsAString(t), nil, nil, whModel1.Mode, whModel1.CorrelationIDKey, whModel1.RetryInterval, whModel1.Timeout, whModel1.URLTemplate, whModel1.InputTemplate, whModel1.HeaderTemplate, whModel1.OutputTemplate, whModel1.StatusTemplate, whModel1.CreatedAt, givenFormationTemplateID()). 506 AddRow(whModel2.ID, nil, nil, whModel2.Type, whModel2.URL, fixAuthAsAString(t), nil, nil, whModel2.Mode, whModel2.CorrelationIDKey, whModel2.RetryInterval, whModel2.Timeout, whModel2.URLTemplate, whModel2.InputTemplate, whModel2.HeaderTemplate, whModel2.OutputTemplate, whModel2.StatusTemplate, whModel2.CreatedAt, givenFormationTemplateID()), 507 } 508 }, 509 InvalidRowsProvider: func() []*sqlmock.Rows { 510 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns)} 511 }, 512 }, 513 }, 514 ConverterMockProvider: func() testdb.Mock { 515 return &automock.EntityConverter{} 516 }, 517 RepoConstructorFunc: webhook.NewRepository, 518 ExpectedModelEntities: []interface{}{whModel1, whModel2}, 519 ExpectedDBEntities: []interface{}{whEntity1, whEntity2}, 520 MethodArgs: []interface{}{givenFormationTemplateID(), model.FormationTemplateWebhookReference}, 521 MethodName: "ListByReferenceObjectIDGlobal", 522 } 523 524 suite.Run(t) 525 526 t.Run("Error when webhookReferenceObjectType is not supported", func(t *testing.T) { 527 repository := webhook.NewRepository(nil) 528 _, err := repository.ListByReferenceObjectIDGlobal(context.TODO(), "", model.IntegrationSystemWebhookReference) 529 assert.Error(t, err) 530 assert.Contains(t, err.Error(), "referenced object should be one of application, application template, runtime or formation template") 531 }) 532 } 533 534 func testListByObjectID(t *testing.T, methodName string, lockClause string, args []interface{}) { 535 whID1 := "whID1" 536 whID2 := "whID2" 537 createdAt := time.Now() 538 539 whModel1 := fixApplicationModelWebhook(whID1, givenApplicationID(), givenTenant(), "http://kyma.io", createdAt) 540 whEntity1 := fixApplicationWebhookEntityWithID(t, whID1, createdAt) 541 542 whModel2 := fixApplicationModelWebhook(whID2, givenApplicationID(), givenTenant(), "http://kyma.io", createdAt) 543 whEntity2 := fixApplicationWebhookEntityWithID(t, whID2, createdAt) 544 545 suite := testdb.RepoListTestSuite{ 546 Name: "List Webhooks by Application ID", 547 SQLQueryDetails: []testdb.SQLQueryDetails{ 548 { 549 Query: regexp.QuoteMeta(`SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE app_id = $1 AND (id IN (SELECT id FROM application_webhooks_tenants WHERE tenant_id = $2))` + lockClause), 550 Args: []driver.Value{givenApplicationID(), givenTenant()}, 551 IsSelect: true, 552 ValidRowsProvider: func() []*sqlmock.Rows { 553 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns). 554 AddRow(whModel1.ID, givenApplicationID(), nil, whModel1.Type, whModel1.URL, fixAuthAsAString(t), nil, nil, whModel1.Mode, whModel1.CorrelationIDKey, whModel1.RetryInterval, whModel1.Timeout, whModel1.URLTemplate, whModel1.InputTemplate, whModel1.HeaderTemplate, whModel1.OutputTemplate, whModel1.StatusTemplate, whModel1.CreatedAt, nil). 555 AddRow(whModel2.ID, givenApplicationID(), nil, whModel2.Type, whModel2.URL, fixAuthAsAString(t), nil, nil, whModel2.Mode, whModel2.CorrelationIDKey, whModel2.RetryInterval, whModel2.Timeout, whModel2.URLTemplate, whModel2.InputTemplate, whModel2.HeaderTemplate, whModel2.OutputTemplate, whModel2.StatusTemplate, whModel2.CreatedAt, nil), 556 } 557 }, 558 InvalidRowsProvider: func() []*sqlmock.Rows { 559 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns)} 560 }, 561 }, 562 }, 563 ConverterMockProvider: func() testdb.Mock { 564 return &automock.EntityConverter{} 565 }, 566 RepoConstructorFunc: webhook.NewRepository, 567 ExpectedModelEntities: []interface{}{whModel1, whModel2}, 568 ExpectedDBEntities: []interface{}{whEntity1, whEntity2}, 569 MethodArgs: args, 570 MethodName: methodName, 571 } 572 573 suite.Run(t) 574 } 575 576 func TestRepository_ListByWebhookType(t *testing.T) { 577 whID := "whID1" 578 whType := model.WebhookTypeOpenResourceDiscovery 579 createdAt := time.Now() 580 581 whModel := fixApplicationModelWebhook(whID, givenApplicationID(), givenTenant(), "http://kyma.io", createdAt) 582 whModel.Type = whType 583 whEntity := fixApplicationWebhookEntityWithIDAndWebhookType(t, whID, whType, createdAt) 584 585 suite := testdb.RepoListTestSuite{ 586 Name: "List Webhooks by type", 587 SQLQueryDetails: []testdb.SQLQueryDetails{ 588 { 589 Query: regexp.QuoteMeta(`SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE type = $1`), 590 Args: []driver.Value{whType}, 591 IsSelect: true, 592 ValidRowsProvider: func() []*sqlmock.Rows { 593 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns). 594 AddRow(whModel.ID, givenApplicationID(), nil, whModel.Type, whModel.URL, fixAuthAsAString(t), nil, nil, whModel.Mode, whModel.CorrelationIDKey, whModel.RetryInterval, whModel.Timeout, whModel.URLTemplate, whModel.InputTemplate, whModel.HeaderTemplate, whModel.OutputTemplate, whModel.StatusTemplate, whModel.CreatedAt, nil), 595 } 596 }, 597 InvalidRowsProvider: func() []*sqlmock.Rows { 598 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns)} 599 }, 600 }, 601 }, 602 ConverterMockProvider: func() testdb.Mock { 603 return &automock.EntityConverter{} 604 }, 605 RepoConstructorFunc: webhook.NewRepository, 606 ExpectedModelEntities: []interface{}{whModel}, 607 ExpectedDBEntities: []interface{}{whEntity}, 608 MethodArgs: []interface{}{whType}, 609 MethodName: "ListByWebhookType", 610 } 611 612 suite.Run(t) 613 } 614 615 func TestRepositoryListByApplicationTemplateID(t *testing.T) { 616 t.Run("success", func(t *testing.T) { 617 // GIVEN 618 mockConv := &automock.EntityConverter{} 619 defer mockConv.AssertExpectations(t) 620 mockConv.On("FromEntity", 621 &webhook.Entity{ID: givenID(), 622 ApplicationTemplateID: repo.NewValidNullableString(givenApplicationTemplateID()), 623 Type: string(model.WebhookTypeConfigurationChanged), 624 URL: repo.NewValidNullableString("http://kyma.io")}). 625 Return(&model.Webhook{ 626 ID: givenID(), 627 }, nil) 628 629 mockConv.On("FromEntity", 630 &webhook.Entity{ID: anotherID(), 631 ApplicationTemplateID: repo.NewValidNullableString(givenApplicationTemplateID()), 632 Type: string(model.WebhookTypeConfigurationChanged), 633 URL: repo.NewValidNullableString("http://kyma2.io")}). 634 Return(&model.Webhook{ID: anotherID()}, nil) 635 636 sut := webhook.NewRepository(mockConv) 637 638 db, dbMock := testdb.MockDatabase(t) 639 defer dbMock.AssertExpectations(t) 640 641 rows := sqlmock.NewRows([]string{"id", "app_template_id", "type", "url", "auth"}). 642 AddRow(givenID(), givenApplicationTemplateID(), model.WebhookTypeConfigurationChanged, "http://kyma.io", nil). 643 AddRow(anotherID(), givenApplicationTemplateID(), model.WebhookTypeConfigurationChanged, "http://kyma2.io", nil) 644 645 dbMock.ExpectQuery(regexp.QuoteMeta("SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE app_template_id = $1")). 646 WithArgs(givenApplicationTemplateID()). 647 WillReturnRows(rows) 648 ctx := persistence.SaveToContext(context.TODO(), db) 649 // WHEN 650 actual, err := sut.ListByApplicationTemplateID(ctx, givenApplicationTemplateID()) 651 // THEN 652 require.NoError(t, err) 653 require.Len(t, actual, 2) 654 assert.Equal(t, givenID(), actual[0].ID) 655 assert.Equal(t, anotherID(), actual[1].ID) 656 }) 657 658 t.Run("success if no found", func(t *testing.T) { 659 // GIVE 660 sut := webhook.NewRepository(nil) 661 662 db, dbMock := testdb.MockDatabase(t) 663 defer dbMock.AssertExpectations(t) 664 665 noRows := sqlmock.NewRows([]string{"id", "app_id", "app_template_id", "type", "url", "auth"}) 666 667 dbMock.ExpectQuery("SELECT").WithArgs(givenApplicationTemplateID()).WillReturnRows(noRows) 668 ctx := persistence.SaveToContext(context.TODO(), db) 669 // WHEN 670 actual, err := sut.ListByApplicationTemplateID(ctx, givenApplicationTemplateID()) 671 // THEN 672 require.NoError(t, err) 673 require.Empty(t, actual) 674 }) 675 676 t.Run("got error on db communication", func(t *testing.T) { 677 // GIVEN 678 sut := webhook.NewRepository(nil) 679 db, dbMock := testdb.MockDatabase(t) 680 defer dbMock.AssertExpectations(t) 681 682 dbMock.ExpectQuery("SELECT").WillReturnError(givenError()) 683 ctx := persistence.SaveToContext(context.TODO(), db) 684 // WHEN 685 _, err := sut.ListByApplicationTemplateID(ctx, givenApplicationTemplateID()) 686 // THEN 687 require.EqualError(t, err, "Internal Server Error: Unexpected error while executing SQL query") 688 }) 689 690 t.Run("got error on converting object", func(t *testing.T) { 691 // GIVEN 692 mockConv := &automock.EntityConverter{} 693 defer mockConv.AssertExpectations(t) 694 mockConv.On("FromEntity", mock.Anything).Return(&model.Webhook{}, givenError()) 695 696 sut := webhook.NewRepository(mockConv) 697 698 db, dbMock := testdb.MockDatabase(t) 699 defer dbMock.AssertExpectations(t) 700 701 rows := sqlmock.NewRows([]string{"id", "app_template_id", "type", "url", "auth"}). 702 AddRow(givenID(), givenApplicationTemplateID(), model.WebhookTypeConfigurationChanged, "http://kyma.io", nil) 703 704 dbMock.ExpectQuery(regexp.QuoteMeta("SELECT")).WithArgs(givenApplicationTemplateID()).WillReturnRows(rows) 705 ctx := persistence.SaveToContext(context.TODO(), db) 706 // WHEN 707 _, err := sut.ListByApplicationTemplateID(ctx, givenApplicationTemplateID()) 708 // THEN 709 require.EqualError(t, err, "while converting Webhook to model: some error") 710 }) 711 } 712 713 func Test_ListByReferenceObjectTypeAndWebhookType(t *testing.T) { 714 whID1 := "whID1" 715 whID2 := "whID2" 716 whType := model.WebhookTypeConfigurationChanged 717 createdAt := time.Now() 718 719 whModel1 := fixApplicationModelWebhook(whID1, givenApplicationID(), givenTenant(), "http://kyma.io", createdAt) 720 whEntity1 := fixApplicationWebhookEntityWithIDAndWebhookType(t, whID1, whType, createdAt) 721 722 whModel2 := fixApplicationModelWebhook(whID2, givenApplicationID(), givenTenant(), "http://kyma.io", createdAt) 723 whEntity2 := fixApplicationWebhookEntityWithIDAndWebhookType(t, whID2, whType, createdAt) 724 725 suite := testdb.RepoListTestSuite{ 726 Name: "List Webhooks by Application ID", 727 SQLQueryDetails: []testdb.SQLQueryDetails{ 728 { 729 Query: regexp.QuoteMeta(`SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE app_id IS NOT NULL AND type = $1 AND (id IN (SELECT id FROM application_webhooks_tenants WHERE tenant_id = $2))`), 730 Args: []driver.Value{whType, givenTenant()}, 731 IsSelect: true, 732 ValidRowsProvider: func() []*sqlmock.Rows { 733 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns). 734 AddRow(whModel1.ID, givenApplicationID(), nil, whModel1.Type, whModel1.URL, fixAuthAsAString(t), nil, nil, whModel1.Mode, whModel1.CorrelationIDKey, whModel1.RetryInterval, whModel1.Timeout, whModel1.URLTemplate, whModel1.InputTemplate, whModel1.HeaderTemplate, whModel1.OutputTemplate, whModel1.StatusTemplate, whModel1.CreatedAt, nil). 735 AddRow(whModel2.ID, givenApplicationID(), nil, whModel2.Type, whModel2.URL, fixAuthAsAString(t), nil, nil, whModel2.Mode, whModel2.CorrelationIDKey, whModel2.RetryInterval, whModel2.Timeout, whModel2.URLTemplate, whModel2.InputTemplate, whModel2.HeaderTemplate, whModel2.OutputTemplate, whModel2.StatusTemplate, whModel2.CreatedAt, nil), 736 } 737 }, 738 InvalidRowsProvider: func() []*sqlmock.Rows { 739 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns)} 740 }, 741 }, 742 }, 743 ConverterMockProvider: func() testdb.Mock { 744 return &automock.EntityConverter{} 745 }, 746 RepoConstructorFunc: webhook.NewRepository, 747 ExpectedModelEntities: []interface{}{whModel1, whModel2}, 748 ExpectedDBEntities: []interface{}{whEntity1, whEntity2}, 749 MethodArgs: []interface{}{givenTenant(), whType, model.ApplicationWebhookReference}, 750 MethodName: "ListByReferenceObjectTypeAndWebhookType", 751 } 752 753 suite.Run(t) 754 } 755 756 func Test_ListByReferenceObjectTypesAndWebhookType(t *testing.T) { 757 whID1 := "whID1" 758 whID2 := "whID2" 759 whType := model.WebhookTypeConfigurationChanged 760 createdAt := time.Now() 761 762 whModel1 := fixApplicationModelWebhook(whID1, givenApplicationID(), givenTenant(), "http://kyma.io", createdAt) 763 whEntity1 := fixApplicationWebhookEntityWithIDAndWebhookType(t, whID1, whType, createdAt) 764 765 whModel2 := fixApplicationModelWebhook(whID2, givenApplicationID(), givenTenant(), "http://kyma.io", createdAt) 766 whEntity2 := fixApplicationWebhookEntityWithIDAndWebhookType(t, whID2, whType, createdAt) 767 768 suite := testdb.RepoListTestSuite{ 769 Name: "List Webhooks by Application ID", 770 SQLQueryDetails: []testdb.SQLQueryDetails{ 771 { 772 Query: regexp.QuoteMeta(`SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE ((id IN (SELECT id FROM webhooks_tenants WHERE tenant_id = $1)) AND (type = $2 AND (app_id IS NOT NULL OR app_template_id IS NOT NULL)))`), 773 Args: []driver.Value{givenTenant(), whType}, 774 IsSelect: true, 775 ValidRowsProvider: func() []*sqlmock.Rows { 776 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns). 777 AddRow(whModel1.ID, givenApplicationID(), nil, whModel1.Type, whModel1.URL, fixAuthAsAString(t), nil, nil, whModel1.Mode, whModel1.CorrelationIDKey, whModel1.RetryInterval, whModel1.Timeout, whModel1.URLTemplate, whModel1.InputTemplate, whModel1.HeaderTemplate, whModel1.OutputTemplate, whModel1.StatusTemplate, whModel1.CreatedAt, nil), 778 } 779 }, 780 InvalidRowsProvider: func() []*sqlmock.Rows { 781 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns)} 782 }, 783 }, 784 { 785 Query: regexp.QuoteMeta(`SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE app_template_id IS NOT NULL AND type = $1`), 786 Args: []driver.Value{whModel2.Type}, 787 IsSelect: true, 788 ValidRowsProvider: func() []*sqlmock.Rows { 789 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns). 790 AddRow(whModel2.ID, givenApplicationID(), nil, whModel2.Type, whModel2.URL, fixAuthAsAString(t), nil, nil, whModel2.Mode, whModel2.CorrelationIDKey, whModel2.RetryInterval, whModel2.Timeout, whModel2.URLTemplate, whModel2.InputTemplate, whModel2.HeaderTemplate, whModel2.OutputTemplate, whModel2.StatusTemplate, whModel2.CreatedAt, nil), 791 } 792 }, 793 InvalidRowsProvider: func() []*sqlmock.Rows { 794 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns)} 795 }, 796 }, 797 }, 798 ConverterMockProvider: func() testdb.Mock { 799 return &automock.EntityConverter{} 800 }, 801 RepoConstructorFunc: webhook.NewRepository, 802 ExpectedModelEntities: []interface{}{whModel1, whModel2}, 803 ExpectedDBEntities: []interface{}{whEntity1, whEntity2}, 804 MethodArgs: []interface{}{givenTenant(), whType, []model.WebhookReferenceObjectType{model.ApplicationWebhookReference, model.ApplicationTemplateWebhookReference}}, 805 MethodName: "ListByReferenceObjectTypesAndWebhookType", 806 } 807 808 suite.Run(t) 809 } 810 811 func TestRepositoryGetByIDAndWebhookType(t *testing.T) { 812 whType := model.WebhookTypeConfigurationChanged 813 createdAt := time.Now() 814 whModel := fixApplicationModelWebhookWithType(givenID(), givenApplicationID(), givenTenant(), "http://kyma.io", whType, createdAt) 815 whEntity := fixApplicationWebhookEntity(t, createdAt) 816 817 whAppTemplateModel := fixApplicationTemplateModelWebhookWithTypeAndTimestamp(givenID(), givenApplicationTemplateID(), "http://kyma.io", whType, createdAt) 818 whAppTemplateEntity := fixApplicationTemplateWebhookEntityWithTimestamp(t, createdAt) 819 820 suite := testdb.RepoGetTestSuite{ 821 Name: "Get Webhook By ID", 822 SQLQueryDetails: []testdb.SQLQueryDetails{ 823 { 824 Query: regexp.QuoteMeta(`SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE app_id = $1 AND type = $2 AND (id IN (SELECT id FROM application_webhooks_tenants WHERE tenant_id = $3))`), 825 Args: []driver.Value{givenApplicationID(), whType, givenTenant()}, 826 IsSelect: true, 827 ValidRowsProvider: func() []*sqlmock.Rows { 828 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns).AddRow(whModel.ID, givenApplicationID(), nil, whModel.Type, whModel.URL, fixAuthAsAString(t), nil, nil, whModel.Mode, whModel.CorrelationIDKey, whModel.RetryInterval, whModel.Timeout, whModel.URLTemplate, whModel.InputTemplate, whModel.HeaderTemplate, whModel.OutputTemplate, whModel.StatusTemplate, whModel.CreatedAt, nil)} 829 }, 830 InvalidRowsProvider: func() []*sqlmock.Rows { 831 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns)} 832 }, 833 }, 834 }, 835 ConverterMockProvider: func() testdb.Mock { 836 return &automock.EntityConverter{} 837 }, 838 RepoConstructorFunc: webhook.NewRepository, 839 ExpectedModelEntity: whModel, 840 ExpectedDBEntity: whEntity, 841 MethodArgs: []interface{}{givenTenant(), givenApplicationID(), model.ApplicationWebhookReference, whType}, 842 MethodName: "GetByIDAndWebhookType", 843 } 844 845 suiteAppTemplate := testdb.RepoGetTestSuite{ 846 Name: "Get Webhook By ID", 847 SQLQueryDetails: []testdb.SQLQueryDetails{ 848 { 849 Query: regexp.QuoteMeta(`SELECT id, app_id, app_template_id, type, url, auth, runtime_id, integration_system_id, mode, correlation_id_key, retry_interval, timeout, url_template, input_template, header_template, output_template, status_template, created_at, formation_template_id FROM public.webhooks WHERE app_template_id = $1 AND type = $2`), 850 Args: []driver.Value{givenApplicationTemplateID(), whType}, 851 IsSelect: true, 852 ValidRowsProvider: func() []*sqlmock.Rows { 853 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns).AddRow(whModel.ID, nil, givenApplicationTemplateID(), whModel.Type, whModel.URL, fixAuthAsAString(t), nil, nil, whModel.Mode, whModel.CorrelationIDKey, whModel.RetryInterval, whModel.Timeout, whModel.URLTemplate, whModel.InputTemplate, whModel.HeaderTemplate, whModel.OutputTemplate, whModel.StatusTemplate, whModel.CreatedAt, nil)} 854 }, 855 InvalidRowsProvider: func() []*sqlmock.Rows { 856 return []*sqlmock.Rows{sqlmock.NewRows(fixColumns)} 857 }, 858 }, 859 }, 860 ConverterMockProvider: func() testdb.Mock { 861 return &automock.EntityConverter{} 862 }, 863 RepoConstructorFunc: webhook.NewRepository, 864 ExpectedModelEntity: whAppTemplateModel, 865 ExpectedDBEntity: whAppTemplateEntity, 866 MethodArgs: []interface{}{givenTenant(), givenApplicationTemplateID(), model.ApplicationTemplateWebhookReference, whType}, 867 MethodName: "GetByIDAndWebhookType", 868 } 869 870 suite.Run(t) 871 suiteAppTemplate.Run(t) 872 }