github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/apptemplateversion/service_test.go (about) 1 package apptemplateversion_test 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "github.com/kyma-incubator/compass/components/director/internal/domain/apptemplateversion" 9 "github.com/kyma-incubator/compass/components/director/internal/domain/apptemplateversion/automock" 10 "github.com/kyma-incubator/compass/components/director/internal/model" 11 "github.com/pkg/errors" 12 "github.com/stretchr/testify/assert" 13 "github.com/stretchr/testify/mock" 14 "github.com/stretchr/testify/require" 15 ) 16 17 func TestService_Create(t *testing.T) { 18 // GIVEN 19 ctx := context.Background() 20 modelApplicationTemplateVersion := fixModelApplicationTemplateVersion(appTemplateVersionID) 21 22 testCases := []struct { 23 Name string 24 Input *model.ApplicationTemplateVersionInput 25 AppTemplateVersionRepoFn func() *automock.ApplicationTemplateVersionRepository 26 UIDSvcFn func() *automock.UIDService 27 TimeSvcFn func() *automock.TimeService 28 ExpectedError error 29 ExpectedOutput string 30 }{ 31 { 32 Name: "Success", 33 Input: fixModelApplicationTemplateVersionInput(), 34 AppTemplateVersionRepoFn: func() *automock.ApplicationTemplateVersionRepository { 35 appTemplateVersionRepo := &automock.ApplicationTemplateVersionRepository{} 36 appTemplateVersionRepo.On("Create", ctx, *modelApplicationTemplateVersion).Return(nil).Once() 37 return appTemplateVersionRepo 38 }, 39 UIDSvcFn: fixUIDService, 40 TimeSvcFn: fixTimeService, 41 ExpectedOutput: appTemplateVersionID, 42 }, 43 { 44 Name: "Error when input is empty", 45 Input: nil, 46 AppTemplateVersionRepoFn: fixEmptyAppTemplateVersionRepo, 47 UIDSvcFn: fixEmptyUIDService, 48 TimeSvcFn: fixEmptyTimeService, 49 ExpectedError: errors.New("Application Template Version input cannot be null"), 50 }, 51 { 52 Name: "Returns error when repo layer cannot create Application Template Version", 53 Input: fixModelApplicationTemplateVersionInput(), 54 AppTemplateVersionRepoFn: func() *automock.ApplicationTemplateVersionRepository { 55 appTemplateVersionRepo := &automock.ApplicationTemplateVersionRepository{} 56 appTemplateVersionRepo.On("Create", ctx, *modelApplicationTemplateVersion).Return(testError).Once() 57 return appTemplateVersionRepo 58 }, 59 UIDSvcFn: fixUIDService, 60 TimeSvcFn: fixTimeService, 61 ExpectedError: testError, 62 }, 63 } 64 65 for _, testCase := range testCases { 66 t.Run(testCase.Name, func(t *testing.T) { 67 appTemplateVersionRepo := testCase.AppTemplateVersionRepoFn() 68 idSvc := testCase.UIDSvcFn() 69 timeSvc := testCase.TimeSvcFn() 70 appTemplateSvc := fixEmptyAppTemplateService() 71 svc := apptemplateversion.NewService(appTemplateVersionRepo, appTemplateSvc, idSvc, timeSvc) 72 73 defer mock.AssertExpectationsForObjects(t, idSvc, appTemplateVersionRepo, timeSvc, appTemplateSvc) 74 75 // WHEN 76 result, err := svc.Create(ctx, appTemplateID, testCase.Input) 77 78 // THEN 79 if testCase.ExpectedError != nil { 80 require.Error(t, err) 81 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 82 } else { 83 assert.NoError(t, err) 84 } 85 assert.Equal(t, testCase.ExpectedOutput, result) 86 }) 87 } 88 } 89 90 func TestService_Update(t *testing.T) { 91 // GIVEN 92 ctx := context.Background() 93 modelApplicationTemplateVersion := fixModelApplicationTemplateVersion(appTemplateVersionID) 94 modelApplicationTemplateVersion.CreatedAt = time.Time{} 95 96 testCases := []struct { 97 Name string 98 Input model.ApplicationTemplateVersionInput 99 AppTemplateVersionRepoFn func() *automock.ApplicationTemplateVersionRepository 100 ExpectedError error 101 ExpectedOutput string 102 }{ 103 { 104 Name: "Success", 105 Input: *fixModelApplicationTemplateVersionInput(), 106 AppTemplateVersionRepoFn: func() *automock.ApplicationTemplateVersionRepository { 107 appTemplateVersionRepo := &automock.ApplicationTemplateVersionRepository{} 108 appTemplateVersionRepo.On("Exists", ctx, appTemplateVersionID).Return(true, nil).Once() 109 appTemplateVersionRepo.On("Update", ctx, *modelApplicationTemplateVersion).Return(nil).Once() 110 return appTemplateVersionRepo 111 }, 112 ExpectedOutput: appTemplateVersionID, 113 }, 114 { 115 Name: "Returns an error when the entity does not exist", 116 Input: *fixModelApplicationTemplateVersionInput(), 117 AppTemplateVersionRepoFn: func() *automock.ApplicationTemplateVersionRepository { 118 appTemplateVersionRepo := &automock.ApplicationTemplateVersionRepository{} 119 appTemplateVersionRepo.On("Exists", ctx, appTemplateVersionID).Return(false, nil).Once() 120 return appTemplateVersionRepo 121 }, 122 ExpectedError: errors.New("Application Template Version with ID 44444444-1111-2222-3333-51d5356e7e09 does not exist"), 123 }, 124 { 125 Name: "Returns an error when cannot determine if the entity exists", 126 Input: *fixModelApplicationTemplateVersionInput(), 127 AppTemplateVersionRepoFn: func() *automock.ApplicationTemplateVersionRepository { 128 appTemplateVersionRepo := &automock.ApplicationTemplateVersionRepository{} 129 appTemplateVersionRepo.On("Exists", ctx, appTemplateVersionID).Return(false, testError).Once() 130 return appTemplateVersionRepo 131 }, 132 ExpectedError: testError, 133 }, 134 { 135 Name: "Returns an error when repo layer cannot update Application Template Version", 136 Input: *fixModelApplicationTemplateVersionInput(), 137 AppTemplateVersionRepoFn: func() *automock.ApplicationTemplateVersionRepository { 138 appTemplateVersionRepo := &automock.ApplicationTemplateVersionRepository{} 139 appTemplateVersionRepo.On("Exists", ctx, appTemplateVersionID).Return(true, nil).Once() 140 appTemplateVersionRepo.On("Update", ctx, *modelApplicationTemplateVersion).Return(testError).Once() 141 return appTemplateVersionRepo 142 }, 143 ExpectedError: testError, 144 }, 145 } 146 147 for _, testCase := range testCases { 148 t.Run(testCase.Name, func(t *testing.T) { 149 appTemplateVersionRepo := testCase.AppTemplateVersionRepoFn() 150 idSvc := fixEmptyUIDService() 151 timeSvc := fixEmptyTimeService() 152 appTemplateSvc := fixEmptyAppTemplateService() 153 svc := apptemplateversion.NewService(appTemplateVersionRepo, appTemplateSvc, idSvc, timeSvc) 154 155 defer mock.AssertExpectationsForObjects(t, idSvc, appTemplateVersionRepo, timeSvc, appTemplateSvc) 156 157 // WHEN 158 err := svc.Update(ctx, appTemplateVersionID, appTemplateID, testCase.Input) 159 160 // THEN 161 if testCase.ExpectedError != nil { 162 require.Error(t, err) 163 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 164 } else { 165 assert.NoError(t, err) 166 } 167 }) 168 } 169 } 170 171 func TestService_GetByAppTemplateIDAndVersion(t *testing.T) { 172 // GIVEN 173 ctx := context.Background() 174 modelApplicationTemplateVersion := fixModelApplicationTemplateVersion(appTemplateVersionID) 175 176 testCases := []struct { 177 Name string 178 AppTemplateVersionRepoFn func() *automock.ApplicationTemplateVersionRepository 179 AppTemplateServiceFn func() *automock.ApplicationTemplateService 180 ExpectedError error 181 ExpectedOutput *model.ApplicationTemplateVersion 182 }{ 183 { 184 Name: "Success", 185 AppTemplateVersionRepoFn: func() *automock.ApplicationTemplateVersionRepository { 186 appTemplateVersionRepo := &automock.ApplicationTemplateVersionRepository{} 187 appTemplateVersionRepo.On("GetByAppTemplateIDAndVersion", ctx, appTemplateID, testVersion).Return(modelApplicationTemplateVersion, nil).Once() 188 return appTemplateVersionRepo 189 }, 190 AppTemplateServiceFn: func() *automock.ApplicationTemplateService { 191 appTemplateSvc := &automock.ApplicationTemplateService{} 192 appTemplateSvc.On("Exists", ctx, appTemplateID).Return(true, nil).Once() 193 return appTemplateSvc 194 }, 195 ExpectedOutput: modelApplicationTemplateVersion, 196 }, 197 { 198 Name: "Returns an error when cannot verify if App Template exists", 199 AppTemplateServiceFn: func() *automock.ApplicationTemplateService { 200 appTemplateSvc := &automock.ApplicationTemplateService{} 201 appTemplateSvc.On("Exists", ctx, appTemplateID).Return(false, testError).Once() 202 return appTemplateSvc 203 }, 204 AppTemplateVersionRepoFn: fixEmptyAppTemplateVersionRepo, 205 ExpectedError: testError, 206 }, 207 { 208 Name: "Returns an error when App Template does not exist", 209 AppTemplateServiceFn: func() *automock.ApplicationTemplateService { 210 appTemplateSvc := &automock.ApplicationTemplateService{} 211 appTemplateSvc.On("Exists", ctx, appTemplateID).Return(false, nil).Once() 212 return appTemplateSvc 213 }, 214 AppTemplateVersionRepoFn: fixEmptyAppTemplateVersionRepo, 215 ExpectedError: errors.New("Application Template with ID 58963c6f-24f6-4128-a05c-51d5356e7e09 does not exist"), 216 }, 217 { 218 Name: "Returns an error when getting the Application Template Version", 219 AppTemplateVersionRepoFn: func() *automock.ApplicationTemplateVersionRepository { 220 appTemplateVersionRepo := &automock.ApplicationTemplateVersionRepository{} 221 appTemplateVersionRepo.On("GetByAppTemplateIDAndVersion", ctx, appTemplateID, testVersion).Return(nil, testError).Once() 222 return appTemplateVersionRepo 223 }, 224 AppTemplateServiceFn: func() *automock.ApplicationTemplateService { 225 appTemplateSvc := &automock.ApplicationTemplateService{} 226 appTemplateSvc.On("Exists", ctx, appTemplateID).Return(true, nil).Once() 227 return appTemplateSvc 228 }, 229 ExpectedError: testError, 230 }, 231 } 232 233 for _, testCase := range testCases { 234 t.Run(testCase.Name, func(t *testing.T) { 235 appTemplateVersionRepo := testCase.AppTemplateVersionRepoFn() 236 appTemplateSvc := testCase.AppTemplateServiceFn() 237 idSvc := fixEmptyUIDService() 238 timeSvc := fixEmptyTimeService() 239 svc := apptemplateversion.NewService(appTemplateVersionRepo, appTemplateSvc, idSvc, timeSvc) 240 241 defer mock.AssertExpectationsForObjects(t, idSvc, appTemplateVersionRepo, timeSvc, appTemplateSvc) 242 243 // WHEN 244 result, err := svc.GetByAppTemplateIDAndVersion(ctx, appTemplateID, testVersion) 245 246 // THEN 247 if testCase.ExpectedError != nil { 248 require.Error(t, err) 249 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 250 } else { 251 assert.NoError(t, err) 252 } 253 254 assert.Equal(t, testCase.ExpectedOutput, result) 255 }) 256 } 257 } 258 259 func TestService_ListByAppTemplateID(t *testing.T) { 260 // GIVEN 261 ctx := context.Background() 262 modelApplicationTemplateVersion := fixModelApplicationTemplateVersion(appTemplateVersionID) 263 264 testCases := []struct { 265 Name string 266 AppTemplateVersionRepoFn func() *automock.ApplicationTemplateVersionRepository 267 ExpectedError error 268 ExpectedOutput []*model.ApplicationTemplateVersion 269 }{ 270 { 271 Name: "Success", 272 AppTemplateVersionRepoFn: func() *automock.ApplicationTemplateVersionRepository { 273 appTemplateVersionRepo := &automock.ApplicationTemplateVersionRepository{} 274 appTemplateVersionRepo.On("ListByAppTemplateID", ctx, appTemplateID).Return([]*model.ApplicationTemplateVersion{modelApplicationTemplateVersion}, nil).Once() 275 return appTemplateVersionRepo 276 }, 277 ExpectedOutput: []*model.ApplicationTemplateVersion{modelApplicationTemplateVersion}, 278 }, 279 { 280 Name: "Returns an error when listing the Application Template Versions", 281 AppTemplateVersionRepoFn: func() *automock.ApplicationTemplateVersionRepository { 282 appTemplateVersionRepo := &automock.ApplicationTemplateVersionRepository{} 283 appTemplateVersionRepo.On("ListByAppTemplateID", ctx, appTemplateID).Return(nil, testError).Once() 284 return appTemplateVersionRepo 285 }, 286 ExpectedError: testError, 287 }, 288 } 289 290 for _, testCase := range testCases { 291 t.Run(testCase.Name, func(t *testing.T) { 292 appTemplateVersionRepo := testCase.AppTemplateVersionRepoFn() 293 idSvc := fixEmptyUIDService() 294 timeSvc := fixEmptyTimeService() 295 appTemplateSvc := fixEmptyAppTemplateService() 296 svc := apptemplateversion.NewService(appTemplateVersionRepo, appTemplateSvc, idSvc, timeSvc) 297 298 defer mock.AssertExpectationsForObjects(t, idSvc, appTemplateVersionRepo, timeSvc, appTemplateSvc) 299 300 // WHEN 301 result, err := svc.ListByAppTemplateID(ctx, appTemplateID) 302 303 // THEN 304 if testCase.ExpectedError != nil { 305 require.Error(t, err) 306 assert.Contains(t, err.Error(), testCase.ExpectedError.Error()) 307 } else { 308 assert.NoError(t, err) 309 } 310 311 assert.Equal(t, testCase.ExpectedOutput, result) 312 }) 313 } 314 } 315 316 func fixUIDService() *automock.UIDService { 317 uidSvc := &automock.UIDService{} 318 uidSvc.On("Generate").Return(appTemplateVersionID) 319 return uidSvc 320 } 321 322 func fixTimeService() *automock.TimeService { 323 timeSvc := &automock.TimeService{} 324 timeSvc.On("Now").Return(mockedTimestamp) 325 return timeSvc 326 } 327 328 func fixEmptyAppTemplateVersionRepo() *automock.ApplicationTemplateVersionRepository { 329 return &automock.ApplicationTemplateVersionRepository{} 330 } 331 332 func fixEmptyUIDService() *automock.UIDService { 333 return &automock.UIDService{} 334 } 335 336 func fixEmptyTimeService() *automock.TimeService { 337 return &automock.TimeService{} 338 } 339 340 func fixEmptyAppTemplateService() *automock.ApplicationTemplateService { 341 return &automock.ApplicationTemplateService{} 342 }