github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/integrationsystem/service_test.go (about) 1 package integrationsystem_test 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "github.com/kyma-incubator/compass/components/director/internal/model" 9 10 "github.com/kyma-incubator/compass/components/director/internal/domain/tenant" 11 12 "github.com/kyma-incubator/compass/components/director/internal/domain/integrationsystem" 13 "github.com/kyma-incubator/compass/components/director/internal/domain/integrationsystem/automock" 14 15 "github.com/stretchr/testify/assert" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func TestService_Create(t *testing.T) { 20 // GIVEN 21 ctx := tenant.SaveToContext(context.TODO(), testTenant, testExternalTenant) 22 uidSvcFn := func() *automock.UIDService { 23 uidSvc := &automock.UIDService{} 24 uidSvc.On("Generate").Return(testID).Once() 25 return uidSvc 26 } 27 modelIntSysInput := fixModelIntegrationSystemInput(testName) 28 modelIntSys := fixModelIntegrationSystem(testID, testName) 29 30 testCases := []struct { 31 Name string 32 IntSysRepoFn func() *automock.IntegrationSystemRepository 33 ExpectedError error 34 ExpectedOutput string 35 }{ 36 { 37 Name: "Success", 38 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 39 intSysRepo := &automock.IntegrationSystemRepository{} 40 intSysRepo.On("Create", ctx, *modelIntSys).Return(nil).Once() 41 return intSysRepo 42 }, 43 ExpectedOutput: testID, 44 }, 45 { 46 Name: "Error when creating Integration System", 47 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 48 intSysRepo := &automock.IntegrationSystemRepository{} 49 intSysRepo.On("Create", ctx, *modelIntSys).Return(testError).Once() 50 return intSysRepo 51 }, 52 ExpectedError: testError, 53 ExpectedOutput: "", 54 }, 55 } 56 57 for _, testCase := range testCases { 58 t.Run(testCase.Name, func(t *testing.T) { 59 intSysRepo := testCase.IntSysRepoFn() 60 idSvc := uidSvcFn() 61 svc := integrationsystem.NewService(intSysRepo, idSvc) 62 63 // WHEN 64 result, err := svc.Create(ctx, modelIntSysInput) 65 66 // THEN 67 if testCase.ExpectedError != nil { 68 require.Error(t, err) 69 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 70 } else { 71 assert.NoError(t, err) 72 } 73 assert.Equal(t, testCase.ExpectedOutput, result) 74 75 intSysRepo.AssertExpectations(t) 76 idSvc.AssertExpectations(t) 77 }) 78 } 79 } 80 81 func TestService_Get(t *testing.T) { 82 // GIVEN 83 ctx := tenant.SaveToContext(context.TODO(), testTenant, testExternalTenant) 84 modelIntSys := fixModelIntegrationSystem(testID, testName) 85 86 testCases := []struct { 87 Name string 88 IntSysRepoFn func() *automock.IntegrationSystemRepository 89 ExpectedError error 90 ExpectedOutput *model.IntegrationSystem 91 }{ 92 { 93 Name: "Success", 94 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 95 intSysRepo := &automock.IntegrationSystemRepository{} 96 intSysRepo.On("Get", ctx, testID).Return(modelIntSys, nil).Once() 97 return intSysRepo 98 }, 99 ExpectedOutput: modelIntSys, 100 }, 101 { 102 Name: "Error when getting integration system", 103 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 104 intSysRepo := &automock.IntegrationSystemRepository{} 105 intSysRepo.On("Get", ctx, testID).Return(nil, testError).Once() 106 return intSysRepo 107 }, 108 ExpectedError: testError, 109 }, 110 } 111 112 for _, testCase := range testCases { 113 t.Run(testCase.Name, func(t *testing.T) { 114 intSysRepo := testCase.IntSysRepoFn() 115 svc := integrationsystem.NewService(intSysRepo, nil) 116 117 // WHEN 118 result, err := svc.Get(ctx, testID) 119 120 // THEN 121 if testCase.ExpectedError != nil { 122 require.Error(t, err) 123 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 124 } else { 125 assert.NoError(t, err) 126 } 127 assert.Equal(t, testCase.ExpectedOutput, result) 128 129 intSysRepo.AssertExpectations(t) 130 }) 131 } 132 } 133 134 func TestService_Exists(t *testing.T) { 135 // GIVEN 136 ctx := tenant.SaveToContext(context.TODO(), testTenant, testExternalTenant) 137 138 testCases := []struct { 139 Name string 140 IntSysRepoFn func() *automock.IntegrationSystemRepository 141 ExpectedError error 142 ExpectedOutput bool 143 }{ 144 { 145 Name: "Success", 146 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 147 intSysRepo := &automock.IntegrationSystemRepository{} 148 intSysRepo.On("Exists", ctx, testID).Return(true, nil).Once() 149 return intSysRepo 150 }, 151 ExpectedOutput: true, 152 }, 153 { 154 Name: "Error when getting integration system", 155 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 156 intSysRepo := &automock.IntegrationSystemRepository{} 157 intSysRepo.On("Exists", ctx, testID).Return(false, testError).Once() 158 return intSysRepo 159 }, 160 ExpectedError: testError, 161 ExpectedOutput: false, 162 }, 163 } 164 165 for _, testCase := range testCases { 166 t.Run(testCase.Name, func(t *testing.T) { 167 intSysRepo := testCase.IntSysRepoFn() 168 svc := integrationsystem.NewService(intSysRepo, nil) 169 170 // WHEN 171 result, err := svc.Exists(ctx, testID) 172 173 // THEN 174 if testCase.ExpectedError != nil { 175 require.Error(t, err) 176 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 177 } else { 178 assert.NoError(t, err) 179 } 180 assert.Equal(t, testCase.ExpectedOutput, result) 181 182 intSysRepo.AssertExpectations(t) 183 }) 184 } 185 } 186 187 func TestService_List(t *testing.T) { 188 // GIVEN 189 ctx := tenant.SaveToContext(context.TODO(), testTenant, testExternalTenant) 190 modelIntSys := fixModelIntegrationSystemPage([]*model.IntegrationSystem{ 191 fixModelIntegrationSystem("foo1", "bar1"), 192 fixModelIntegrationSystem("foo2", "bar2"), 193 }) 194 195 testCases := []struct { 196 Name string 197 IntSysRepoFn func() *automock.IntegrationSystemRepository 198 InputPageSize int 199 ExpectedError error 200 ExpectedOutput model.IntegrationSystemPage 201 }{ 202 { 203 Name: "Success", 204 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 205 intSysRepo := &automock.IntegrationSystemRepository{} 206 intSysRepo.On("List", ctx, 50, testCursor).Return(modelIntSys, nil).Once() 207 return intSysRepo 208 }, 209 InputPageSize: 50, 210 ExpectedOutput: modelIntSys, 211 }, 212 { 213 Name: "Error when listing integration system", 214 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 215 intSysRepo := &automock.IntegrationSystemRepository{} 216 intSysRepo.On("List", ctx, 50, testCursor).Return(model.IntegrationSystemPage{}, testError).Once() 217 return intSysRepo 218 }, 219 InputPageSize: 50, 220 ExpectedError: testError, 221 ExpectedOutput: model.IntegrationSystemPage{}, 222 }, 223 { 224 Name: "Error when page size too small", 225 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 226 intSysRepo := &automock.IntegrationSystemRepository{} 227 return intSysRepo 228 }, 229 InputPageSize: 0, 230 ExpectedError: errors.New("page size must be between 1 and 200"), 231 ExpectedOutput: model.IntegrationSystemPage{}, 232 }, 233 { 234 Name: "Error when page size too big", 235 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 236 intSysRepo := &automock.IntegrationSystemRepository{} 237 return intSysRepo 238 }, 239 InputPageSize: 201, 240 ExpectedError: errors.New("page size must be between 1 and 200"), 241 ExpectedOutput: model.IntegrationSystemPage{}, 242 }, 243 } 244 245 for _, testCase := range testCases { 246 t.Run(testCase.Name, func(t *testing.T) { 247 intSysRepo := testCase.IntSysRepoFn() 248 svc := integrationsystem.NewService(intSysRepo, nil) 249 250 // WHEN 251 result, err := svc.List(ctx, testCase.InputPageSize, testCursor) 252 253 // THEN 254 if testCase.ExpectedError != nil { 255 require.Error(t, err) 256 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 257 } else { 258 assert.NoError(t, err) 259 } 260 assert.Equal(t, testCase.ExpectedOutput, result) 261 262 intSysRepo.AssertExpectations(t) 263 }) 264 } 265 } 266 267 func TestService_Update(t *testing.T) { 268 // GIVEN 269 ctx := tenant.SaveToContext(context.TODO(), testTenant, testExternalTenant) 270 modelIntSys := fixModelIntegrationSystem(testID, testName) 271 modelIntSysInput := fixModelIntegrationSystemInput(testName) 272 273 testCases := []struct { 274 Name string 275 IntSysRepoFn func() *automock.IntegrationSystemRepository 276 ExpectedError error 277 }{ 278 { 279 Name: "Success", 280 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 281 intSysRepo := &automock.IntegrationSystemRepository{} 282 intSysRepo.On("Update", ctx, *modelIntSys).Return(nil).Once() 283 return intSysRepo 284 }, 285 }, 286 { 287 Name: "Error when updating integration system", 288 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 289 intSysRepo := &automock.IntegrationSystemRepository{} 290 intSysRepo.On("Update", ctx, *modelIntSys).Return(testError).Once() 291 return intSysRepo 292 }, 293 ExpectedError: testError, 294 }, 295 } 296 297 for _, testCase := range testCases { 298 t.Run(testCase.Name, func(t *testing.T) { 299 intSysRepo := testCase.IntSysRepoFn() 300 svc := integrationsystem.NewService(intSysRepo, nil) 301 302 // WHEN 303 err := svc.Update(ctx, testID, modelIntSysInput) 304 305 // THEN 306 if testCase.ExpectedError != nil { 307 require.Error(t, err) 308 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 309 } else { 310 assert.NoError(t, err) 311 } 312 313 intSysRepo.AssertExpectations(t) 314 }) 315 } 316 } 317 318 func TestService_Delete(t *testing.T) { 319 // GIVEN 320 ctx := tenant.SaveToContext(context.TODO(), testTenant, testExternalTenant) 321 322 testCases := []struct { 323 Name string 324 IntSysRepoFn func() *automock.IntegrationSystemRepository 325 ExpectedError error 326 }{ 327 { 328 Name: "Success", 329 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 330 intSysRepo := &automock.IntegrationSystemRepository{} 331 intSysRepo.On("Delete", ctx, testID).Return(nil).Once() 332 return intSysRepo 333 }, 334 }, 335 { 336 Name: "Error when deleting integration system", 337 IntSysRepoFn: func() *automock.IntegrationSystemRepository { 338 intSysRepo := &automock.IntegrationSystemRepository{} 339 intSysRepo.On("Delete", ctx, testID).Return(testError).Once() 340 return intSysRepo 341 }, 342 ExpectedError: testError, 343 }, 344 } 345 346 for _, testCase := range testCases { 347 t.Run(testCase.Name, func(t *testing.T) { 348 intSysRepo := testCase.IntSysRepoFn() 349 svc := integrationsystem.NewService(intSysRepo, nil) 350 351 // WHEN 352 err := svc.Delete(ctx, testID) 353 354 // THEN 355 if testCase.ExpectedError != nil { 356 require.Error(t, err) 357 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 358 } else { 359 assert.NoError(t, err) 360 } 361 362 intSysRepo.AssertExpectations(t) 363 }) 364 } 365 }