github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/apptemplate/apptmpltest/selfreg_manager.go (about) 1 package apptmpltest 2 3 import ( 4 "errors" 5 6 "github.com/kyma-incubator/compass/components/director/pkg/resource" 7 8 "github.com/kyma-incubator/compass/components/director/internal/domain/apptemplate/automock" 9 "github.com/stretchr/testify/mock" 10 ) 11 12 const ( 13 // TestDistinguishLabel is a test distinguishing label key 14 TestDistinguishLabel = "test-distinguish-label" 15 // SelfRegErrorMsg is a test error message 16 SelfRegErrorMsg = "error during self-reg prep" 17 ) 18 19 // NoopSelfRegManager is a noop mock 20 func NoopSelfRegManager() *automock.SelfRegisterManager { 21 return &automock.SelfRegisterManager{} 22 } 23 24 // SelfRegManagerThatDoesPrepWithNoErrors mock for PrepareForSelfRegistration executed once 25 func SelfRegManagerThatDoesPrepWithNoErrors(res map[string]interface{}) func() *automock.SelfRegisterManager { 26 return func() *automock.SelfRegisterManager { 27 srm := &automock.SelfRegisterManager{} 28 srm.On("PrepareForSelfRegistration", mock.Anything, resource.ApplicationTemplate, mock.Anything, mock.AnythingOfType("string"), mock.Anything).Return(res, nil).Once() 29 return srm 30 } 31 } 32 33 // SelfRegManagerThatReturnsErrorOnPrep mock for GetSelfRegDistinguishingLabelKey executed once with error 34 func SelfRegManagerThatReturnsErrorOnPrep() *automock.SelfRegisterManager { 35 srm := &automock.SelfRegisterManager{} 36 labels := make(map[string]interface{}) 37 srm.On("GetSelfRegDistinguishingLabelKey").Return(TestDistinguishLabel).Once() 38 srm.On("PrepareForSelfRegistration", mock.Anything, resource.ApplicationTemplate, mock.Anything, mock.AnythingOfType("string"), mock.Anything).Return(labels, errors.New(SelfRegErrorMsg)).Once() 39 return srm 40 } 41 42 // SelfRegManagerThatReturnsErrorOnPrepAndGetSelfRegDistinguishingLabelKey mock for PrepareForSelfRegistration executed once with error, GetSelfRegDistinguishingLabelKey once 43 func SelfRegManagerThatReturnsErrorOnPrepAndGetSelfRegDistinguishingLabelKey() *automock.SelfRegisterManager { 44 srm := &automock.SelfRegisterManager{} 45 labels := make(map[string]interface{}) 46 srm.On("PrepareForSelfRegistration", mock.Anything, resource.ApplicationTemplate, mock.Anything, mock.AnythingOfType("string"), mock.Anything).Return(labels, errors.New(SelfRegErrorMsg)).Once() 47 srm.On("GetSelfRegDistinguishingLabelKey").Return(TestDistinguishLabel).Once() 48 return srm 49 } 50 51 // SelfRegManagerThatDoesCleanupWithNoErrors mock for GetSelfRegDistinguishingLabelKey executed once, CleanupSelfRegistration once 52 func SelfRegManagerThatDoesCleanupWithNoErrors() *automock.SelfRegisterManager { 53 srm := &automock.SelfRegisterManager{} 54 srm.On("GetSelfRegDistinguishingLabelKey").Return(TestDistinguishLabel).Once() 55 srm.On("CleanupSelfRegistration", mock.Anything, mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(nil).Once() 56 return srm 57 } 58 59 // SelfRegManagerThatReturnsErrorOnCleanup mock for GetSelfRegDistinguishingLabelKey executed once, CleanupSelfRegistration once with error 60 func SelfRegManagerThatReturnsErrorOnCleanup() *automock.SelfRegisterManager { 61 srm := &automock.SelfRegisterManager{} 62 srm.On("GetSelfRegDistinguishingLabelKey").Return(TestDistinguishLabel).Once() 63 srm.On("CleanupSelfRegistration", mock.Anything, mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(errors.New(SelfRegErrorMsg)).Once() 64 return srm 65 } 66 67 // SelfRegManagerReturnsDistinguishingLabel mock for GetSelfRegDistinguishingLabelKey executed once 68 func SelfRegManagerReturnsDistinguishingLabel() *automock.SelfRegisterManager { 69 srm := &automock.SelfRegisterManager{} 70 srm.On("GetSelfRegDistinguishingLabelKey").Return(TestDistinguishLabel).Once() 71 return srm 72 } 73 74 // SelfRegManagerThatDoesCleanup mock for GetSelfRegDistinguishingLabelKey executed 3 times, PrepareForSelfRegistration once, CleanupSelfRegistration once 75 func SelfRegManagerThatDoesCleanup(res map[string]interface{}) func() *automock.SelfRegisterManager { 76 return func() *automock.SelfRegisterManager { 77 srm := SelfRegManagerThatDoesPrepWithNoErrors(res)() 78 srm.On("GetSelfRegDistinguishingLabelKey").Return(TestDistinguishLabel).Times(3) 79 srm.On("CleanupSelfRegistration", mock.Anything, mock.AnythingOfType("string"), mock.AnythingOfType("string")).Return(nil).Once() 80 return srm 81 } 82 } 83 84 // SelfRegManagerThatDoesNotCleanupFunc mock for GetSelfRegDistinguishingLabelKey executed once, PrepareForSelfRegistration once 85 func SelfRegManagerThatDoesNotCleanupFunc(res map[string]interface{}) func() *automock.SelfRegisterManager { 86 return func() *automock.SelfRegisterManager { 87 srm := SelfRegManagerThatDoesPrepWithNoErrors(res)() 88 srm.On("GetSelfRegDistinguishingLabelKey").Return(TestDistinguishLabel).Times(2) 89 return srm 90 } 91 } 92 93 // SelfRegManagerOnlyGetDistinguishedLabelKey mock for GetSelfRegDistinguishingLabelKey once 94 func SelfRegManagerOnlyGetDistinguishedLabelKey() func() *automock.SelfRegisterManager { 95 return func() *automock.SelfRegisterManager { 96 srm := NoopSelfRegManager() 97 srm.On("GetSelfRegDistinguishingLabelKey").Return(TestDistinguishLabel).Once() 98 return srm 99 } 100 } 101 102 // SelfRegManagerThatDoesPrepAndInitiatesCleanupButNotFinishIt mock for GetSelfRegDistinguishingLabelKey executed 3 times, PrepareForSelfRegistration once 103 func SelfRegManagerThatDoesPrepAndInitiatesCleanupButNotFinishIt(res map[string]interface{}) func() *automock.SelfRegisterManager { 104 return func() *automock.SelfRegisterManager { 105 srm := SelfRegManagerThatDoesPrepWithNoErrors(res)() 106 srm.On("GetSelfRegDistinguishingLabelKey").Return(TestDistinguishLabel).Times(3) 107 return srm 108 } 109 } 110 111 // SelfRegManagerThatInitiatesCleanupButNotFinishIt mock for GetSelfRegDistinguishingLabelKey executed 3 times 112 func SelfRegManagerThatInitiatesCleanupButNotFinishIt() func() *automock.SelfRegisterManager { 113 return func() *automock.SelfRegisterManager { 114 srm := NoopSelfRegManager() 115 srm.On("GetSelfRegDistinguishingLabelKey").Return(TestDistinguishLabel).Times(3) 116 return srm 117 } 118 }