github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/bundle/converter_test.go (about) 1 package bundle_test 2 3 import ( 4 "database/sql" 5 "errors" 6 "testing" 7 8 "github.com/kyma-incubator/compass/components/director/internal/repo" 9 "github.com/stretchr/testify/mock" 10 11 "github.com/kyma-incubator/compass/components/director/internal/domain/auth" 12 13 "github.com/kyma-incubator/compass/components/director/internal/domain/bundle" 14 15 "github.com/stretchr/testify/require" 16 17 "github.com/kyma-incubator/compass/components/director/internal/model" 18 19 "github.com/kyma-incubator/compass/components/director/internal/domain/bundle/automock" 20 21 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 22 "github.com/stretchr/testify/assert" 23 ) 24 25 func TestEntityConverter_ToEntity(t *testing.T) { 26 t.Run("success all nullable properties filled", func(t *testing.T) { 27 // GIVEN 28 name := "foo" 29 desc := "bar" 30 bndlModel := fixBundleModel(name, desc) 31 testErrMsg := "test-err" 32 bndlModel.Error = &testErrMsg 33 require.NotNil(t, bndlModel) 34 authConv := auth.NewConverter() 35 conv := bundle.NewConverter(authConv, nil, nil, nil) 36 // WHEN 37 entity, err := conv.ToEntity(bndlModel) 38 // THEN 39 require.NoError(t, err) 40 41 expectedBndl := fixEntityBundleWithAppID(bundleID, name, desc) 42 expectedBndl.Error = sql.NullString{ 43 String: testErrMsg, 44 Valid: true, 45 } 46 assert.Equal(t, expectedBndl, entity) 47 }) 48 t.Run("success all nullable properties empty", func(t *testing.T) { 49 // GIVEN 50 name := "foo" 51 bndlModel := &model.Bundle{ 52 ApplicationID: &appID, 53 Name: name, 54 Description: nil, 55 InstanceAuthRequestInputSchema: nil, 56 DefaultInstanceAuth: nil, 57 BaseEntity: &model.BaseEntity{ID: bundleID}, 58 } 59 60 expectedEntity := &bundle.Entity{ 61 ApplicationID: repo.NewValidNullableString(appID), 62 Name: name, 63 Description: sql.NullString{}, 64 InstanceAuthRequestJSONSchema: sql.NullString{}, 65 DefaultInstanceAuth: sql.NullString{}, 66 BaseEntity: &repo.BaseEntity{ID: bundleID}, 67 } 68 69 require.NotNil(t, bndlModel) 70 authConv := auth.NewConverter() 71 conv := bundle.NewConverter(authConv, nil, nil, nil) 72 // WHEN 73 entity, err := conv.ToEntity(bndlModel) 74 // THEN 75 require.NoError(t, err) 76 assert.Equal(t, expectedEntity, entity) 77 }) 78 } 79 80 func TestEntityConverter_FromEntity(t *testing.T) { 81 t.Run("success all nullable properties filled", func(t *testing.T) { 82 // GIVEN 83 name := "foo" 84 desc := "bar" 85 testErrMsg := "test-err" 86 entity := fixEntityBundleWithAppID(bundleID, name, desc) 87 entity.Error = sql.NullString{ 88 String: testErrMsg, 89 Valid: true, 90 } 91 authConv := auth.NewConverter() 92 conv := bundle.NewConverter(authConv, nil, nil, nil) 93 // WHEN 94 bndlModel, err := conv.FromEntity(entity) 95 // THEN 96 require.NoError(t, err) 97 expectedBdnl := fixBundleModel(name, desc) 98 expectedBdnl.Error = &testErrMsg 99 assert.Equal(t, expectedBdnl, bndlModel) 100 }) 101 t.Run("success all nullable properties empty", func(t *testing.T) { 102 // GIVEN 103 name := "foo" 104 entity := &bundle.Entity{ 105 ApplicationID: repo.NewValidNullableString(appID), 106 Name: name, 107 Description: sql.NullString{}, 108 InstanceAuthRequestJSONSchema: sql.NullString{}, 109 DefaultInstanceAuth: sql.NullString{}, 110 BaseEntity: &repo.BaseEntity{ID: bundleID}, 111 } 112 expectedModel := &model.Bundle{ 113 ApplicationID: &appID, 114 Name: name, 115 Description: nil, 116 InstanceAuthRequestInputSchema: nil, 117 DefaultInstanceAuth: nil, 118 BaseEntity: &model.BaseEntity{ID: bundleID}, 119 } 120 authConv := auth.NewConverter() 121 conv := bundle.NewConverter(authConv, nil, nil, nil) 122 // WHEN 123 bndlModel, err := conv.FromEntity(entity) 124 // THEN 125 require.NoError(t, err) 126 require.NotNil(t, expectedModel) 127 assert.Equal(t, expectedModel, bndlModel) 128 }) 129 } 130 131 func TestConverter_ToGraphQL(t *testing.T) { 132 // GIVEN 133 id := bundleID 134 name := "foo" 135 desc := "bar" 136 modelBundle := fixBundleModel(name, desc) 137 gqlBundle := fixGQLBundle(id, name, desc) 138 emptyModelBundle := &model.Bundle{BaseEntity: &model.BaseEntity{}} 139 emptyGraphQLBundle := &graphql.Bundle{BaseEntity: &graphql.BaseEntity{}} 140 141 testCases := []struct { 142 Name string 143 Input *model.Bundle 144 Expected *graphql.Bundle 145 AuthConverterFn func() *automock.AuthConverter 146 ExpectedErr error 147 }{ 148 { 149 Name: "All properties given", 150 Input: modelBundle, 151 Expected: gqlBundle, 152 AuthConverterFn: func() *automock.AuthConverter { 153 conv := &automock.AuthConverter{} 154 conv.On("ToGraphQL", modelBundle.DefaultInstanceAuth).Return(gqlBundle.DefaultInstanceAuth, nil).Once() 155 return conv 156 }, 157 }, 158 { 159 Name: "Empty", 160 Input: emptyModelBundle, 161 Expected: emptyGraphQLBundle, 162 AuthConverterFn: func() *automock.AuthConverter { 163 conv := &automock.AuthConverter{} 164 conv.On("ToGraphQL", emptyModelBundle.DefaultInstanceAuth).Return(nil, nil).Once() 165 return conv 166 }, 167 }, 168 { 169 Name: "Nil", 170 Input: nil, 171 Expected: nil, 172 ExpectedErr: errors.New("the model Bundle is nil"), 173 AuthConverterFn: func() *automock.AuthConverter { 174 return &automock.AuthConverter{} 175 }, 176 }, 177 } 178 179 for _, testCase := range testCases { 180 t.Run(testCase.Name, func(t *testing.T) { 181 // GIVEN 182 authConverter := testCase.AuthConverterFn() 183 184 // WHEN 185 converter := bundle.NewConverter(authConverter, nil, nil, nil) 186 res, err := converter.ToGraphQL(testCase.Input) 187 188 // then 189 assert.EqualValues(t, testCase.Expected, res) 190 if testCase.ExpectedErr != nil { 191 require.Error(t, err) 192 assert.Contains(t, err.Error(), testCase.ExpectedErr.Error()) 193 } else { 194 require.Nil(t, err) 195 } 196 197 authConverter.AssertExpectations(t) 198 }) 199 } 200 } 201 202 func TestConverter_MultipleToGraphQL(t *testing.T) { 203 // GIVEN 204 name1 := "foo" 205 name2 := "bar" 206 desc := "1" 207 input := []*model.Bundle{ 208 fixBundleModel(name1, desc), 209 fixBundleModel(name2, desc), 210 {BaseEntity: &model.BaseEntity{}}, 211 nil, 212 } 213 214 expected := []*graphql.Bundle{ 215 fixGQLBundle(bundleID, name1, desc), 216 fixGQLBundle(bundleID, name2, desc), 217 {BaseEntity: &graphql.BaseEntity{}}, 218 } 219 220 authConverter := &automock.AuthConverter{} 221 222 for i, api := range input { 223 if api == nil { 224 continue 225 } 226 authConverter.On("ToGraphQL", api.DefaultInstanceAuth).Return(expected[i].DefaultInstanceAuth, nil).Once() 227 } 228 229 // WHEN 230 converter := bundle.NewConverter(authConverter, nil, nil, nil) 231 res, err := converter.MultipleToGraphQL(input) 232 233 // then 234 assert.Equal(t, expected, res) 235 assert.NoError(t, err) 236 237 authConverter.AssertExpectations(t) 238 } 239 240 func TestConverter_CreateInputFromGraphQL(t *testing.T) { 241 // GIVEN 242 name := "foo" 243 desc := "Lorem ipsum" 244 gqlBundleCreateInput := fixGQLBundleCreateInput(name, desc) 245 modelBundleCreateInput := fixModelBundleCreateInput(name, desc) 246 emptyGQLBundleCreateInput := &graphql.BundleCreateInput{} 247 emptyModelBundleCreateInput := &model.BundleCreateInput{} 248 testCases := []struct { 249 Name string 250 Input graphql.BundleCreateInput 251 Expected model.BundleCreateInput 252 APIConverterFn func() *automock.APIConverter 253 EventAPIConverterFn func() *automock.EventConverter 254 DocumentConverterFn func() *automock.DocumentConverter 255 AuthConverterFn func() *automock.AuthConverter 256 }{ 257 { 258 Name: "All properties given", 259 Input: gqlBundleCreateInput, 260 Expected: modelBundleCreateInput, 261 APIConverterFn: func() *automock.APIConverter { 262 conv := &automock.APIConverter{} 263 conv.On("MultipleInputFromGraphQL", gqlBundleCreateInput.APIDefinitions).Return(modelBundleCreateInput.APIDefinitions, modelBundleCreateInput.APISpecs, nil) 264 return conv 265 }, 266 EventAPIConverterFn: func() *automock.EventConverter { 267 conv := &automock.EventConverter{} 268 conv.On("MultipleInputFromGraphQL", gqlBundleCreateInput.EventDefinitions).Return(modelBundleCreateInput.EventDefinitions, modelBundleCreateInput.EventSpecs, nil) 269 return conv 270 }, 271 DocumentConverterFn: func() *automock.DocumentConverter { 272 conv := &automock.DocumentConverter{} 273 conv.On("MultipleInputFromGraphQL", gqlBundleCreateInput.Documents).Return(modelBundleCreateInput.Documents, nil) 274 return conv 275 }, 276 AuthConverterFn: func() *automock.AuthConverter { 277 conv := &automock.AuthConverter{} 278 conv.On("InputFromGraphQL", gqlBundleCreateInput.DefaultInstanceAuth).Return(modelBundleCreateInput.DefaultInstanceAuth, nil).Once() 279 return conv 280 }, 281 }, 282 { 283 Name: "Empty", 284 Input: graphql.BundleCreateInput{}, 285 Expected: model.BundleCreateInput{}, 286 APIConverterFn: func() *automock.APIConverter { 287 conv := &automock.APIConverter{} 288 conv.On("MultipleInputFromGraphQL", emptyGQLBundleCreateInput.APIDefinitions).Return(emptyModelBundleCreateInput.APIDefinitions, emptyModelBundleCreateInput.APISpecs, nil) 289 return conv 290 }, 291 EventAPIConverterFn: func() *automock.EventConverter { 292 conv := &automock.EventConverter{} 293 conv.On("MultipleInputFromGraphQL", emptyGQLBundleCreateInput.EventDefinitions).Return(emptyModelBundleCreateInput.EventDefinitions, emptyModelBundleCreateInput.EventSpecs, nil) 294 return conv 295 }, 296 DocumentConverterFn: func() *automock.DocumentConverter { 297 conv := &automock.DocumentConverter{} 298 conv.On("MultipleInputFromGraphQL", emptyGQLBundleCreateInput.Documents).Return(emptyModelBundleCreateInput.Documents, nil) 299 return conv 300 }, 301 AuthConverterFn: func() *automock.AuthConverter { 302 conv := &automock.AuthConverter{} 303 conv.On("InputFromGraphQL", emptyGQLBundleCreateInput.DefaultInstanceAuth).Return(emptyModelBundleCreateInput.DefaultInstanceAuth, nil).Once() 304 return conv 305 }, 306 }, 307 } 308 309 for _, testCase := range testCases { 310 t.Run(testCase.Name, func(t *testing.T) { 311 // GIVEN 312 apiConverter := testCase.APIConverterFn() 313 eventConverter := testCase.EventAPIConverterFn() 314 documentConverter := testCase.DocumentConverterFn() 315 authConverter := testCase.AuthConverterFn() 316 317 // WHEN 318 converter := bundle.NewConverter(authConverter, apiConverter, eventConverter, documentConverter) 319 res, err := converter.CreateInputFromGraphQL(testCase.Input) 320 321 // then 322 assert.NoError(t, err) 323 assert.Equal(t, testCase.Expected, res) 324 mock.AssertExpectationsForObjects(t, apiConverter, eventConverter, documentConverter, authConverter) 325 }) 326 } 327 } 328 329 func TestConverter_MultipleCreateInputFromGraphQL(t *testing.T) { 330 // GIVEN 331 gqlBndl1 := fixGQLBundleCreateInput("foo", "bar") 332 gqlBndl2 := fixGQLBundleCreateInput("bar", "baz") 333 input := []*graphql.BundleCreateInput{ 334 &gqlBndl1, 335 &gqlBndl2, 336 } 337 338 modBndl1 := fixModelBundleCreateInput("foo", "bar") 339 modBndl2 := fixModelBundleCreateInput("bar", "baz") 340 expected := []*model.BundleCreateInput{ 341 &modBndl1, &modBndl2, 342 } 343 344 apiConv := &automock.APIConverter{} 345 apiConv.On("MultipleInputFromGraphQL", gqlBndl1.APIDefinitions).Return(modBndl1.APIDefinitions, modBndl1.APISpecs, nil).Once() 346 apiConv.On("MultipleInputFromGraphQL", gqlBndl2.APIDefinitions).Return(modBndl2.APIDefinitions, modBndl2.APISpecs, nil).Once() 347 348 eventConv := &automock.EventConverter{} 349 eventConv.On("MultipleInputFromGraphQL", gqlBndl1.EventDefinitions).Return(modBndl1.EventDefinitions, modBndl1.EventSpecs, nil).Once() 350 eventConv.On("MultipleInputFromGraphQL", gqlBndl2.EventDefinitions).Return(modBndl2.EventDefinitions, modBndl2.EventSpecs, nil).Once() 351 352 docConv := &automock.DocumentConverter{} 353 docConv.On("MultipleInputFromGraphQL", gqlBndl1.Documents).Return(modBndl1.Documents, nil).Once() 354 docConv.On("MultipleInputFromGraphQL", gqlBndl2.Documents).Return(modBndl2.Documents, nil).Once() 355 356 authConv := &automock.AuthConverter{} 357 authConv.On("InputFromGraphQL", gqlBndl1.DefaultInstanceAuth).Return(modBndl1.DefaultInstanceAuth, nil).Once() 358 authConv.On("InputFromGraphQL", gqlBndl2.DefaultInstanceAuth).Return(modBndl2.DefaultInstanceAuth, nil).Once() 359 360 converter := bundle.NewConverter(authConv, apiConv, eventConv, docConv) 361 362 // WHEN 363 res, err := converter.MultipleCreateInputFromGraphQL(input) 364 365 // then 366 assert.NoError(t, err) 367 assert.Equal(t, expected, res) 368 mock.AssertExpectationsForObjects(t, apiConv, eventConv, docConv, authConv) 369 } 370 371 func TestConverter_UpdateInputFromGraphQL(t *testing.T) { 372 // GIVEN 373 name := "foo" 374 desc := "Lorem ipsum" 375 gqlBundleCreateInput := fixGQLBundleUpdateInput(name, desc) 376 modelBundleCreateInput := fixModelBundleUpdateInput(name, desc) 377 emptyGQLBundleCreateInput := &graphql.BundleCreateInput{} 378 emptyModelBundleCreateInput := &model.BundleCreateInput{} 379 testCases := []struct { 380 Name string 381 Input *graphql.BundleUpdateInput 382 Expected *model.BundleUpdateInput 383 AuthConverterFn func() *automock.AuthConverter 384 }{ 385 { 386 Name: "All properties given", 387 Input: &gqlBundleCreateInput, 388 Expected: &modelBundleCreateInput, 389 AuthConverterFn: func() *automock.AuthConverter { 390 conv := &automock.AuthConverter{} 391 conv.On("InputFromGraphQL", gqlBundleCreateInput.DefaultInstanceAuth).Return(modelBundleCreateInput.DefaultInstanceAuth, nil).Once() 392 return conv 393 }, 394 }, 395 { 396 Name: "Empty", 397 Input: &graphql.BundleUpdateInput{}, 398 Expected: &model.BundleUpdateInput{}, 399 AuthConverterFn: func() *automock.AuthConverter { 400 conv := &automock.AuthConverter{} 401 conv.On("InputFromGraphQL", emptyGQLBundleCreateInput.DefaultInstanceAuth).Return(emptyModelBundleCreateInput.DefaultInstanceAuth, nil).Once() 402 return conv 403 }, 404 }, 405 } 406 407 for _, testCase := range testCases { 408 t.Run(testCase.Name, func(t *testing.T) { 409 // GIVEN 410 authConverter := testCase.AuthConverterFn() 411 412 // WHEN 413 converter := bundle.NewConverter(authConverter, nil, nil, nil) 414 res, err := converter.UpdateInputFromGraphQL(*testCase.Input) 415 416 // then 417 assert.Equal(t, testCase.Expected, res) 418 assert.NoError(t, err) 419 authConverter.AssertExpectations(t) 420 }) 421 } 422 }