github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/subscription/resolver_test.go (about) 1 package subscription_test 2 3 import ( 4 "context" 5 "testing" 6 7 "github.com/kyma-incubator/compass/components/director/internal/domain/subscription" 8 "github.com/kyma-incubator/compass/components/director/internal/domain/subscription/automock" 9 persistenceautomock "github.com/kyma-incubator/compass/components/director/pkg/persistence/automock" 10 "github.com/kyma-incubator/compass/components/director/pkg/persistence/txtest" 11 "github.com/kyma-incubator/compass/components/director/pkg/resource" 12 "github.com/pkg/errors" 13 "github.com/stretchr/testify/assert" 14 "github.com/stretchr/testify/mock" 15 ) 16 17 var ( 18 testErr = errors.New("new-error") 19 txGen = txtest.NewTransactionContextGenerator(testErr) 20 ) 21 22 func TestResolver_SubscribeTenant(t *testing.T) { 23 var emptyPayload = "{}" 24 25 testCases := []struct { 26 Name string 27 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 28 ServiceFn func() *automock.SubscriptionService 29 ExpectedOutput bool 30 ExpectedErr error 31 }{ 32 { 33 Name: "Success for Application flow", 34 TransactionerFn: txGen.ThatSucceeds, 35 ServiceFn: func() *automock.SubscriptionService { 36 svc := &automock.SubscriptionService{} 37 svc.On("DetermineSubscriptionFlow", txtest.CtxWithDBMatcher(), subscriptionProviderID, tenantRegion).Return(resource.ApplicationTemplate, nil).Once() 38 svc.On("SubscribeTenantToApplication", txtest.CtxWithDBMatcher(), subscriptionProviderID, subaccountTenantExtID, consumerTenantID, tenantRegion, subscriptionAppName, emptyPayload).Return(true, nil).Once() 39 svc.AssertNotCalled(t, "SubscribeTenantToRuntime") 40 return svc 41 }, 42 ExpectedOutput: true, 43 ExpectedErr: nil, 44 }, 45 { 46 Name: "Success for Runtime flow", 47 TransactionerFn: txGen.ThatSucceeds, 48 ServiceFn: func() *automock.SubscriptionService { 49 svc := &automock.SubscriptionService{} 50 svc.On("DetermineSubscriptionFlow", txtest.CtxWithDBMatcher(), subscriptionProviderID, tenantRegion).Return(resource.Runtime, nil).Once() 51 svc.AssertNotCalled(t, "SubscribeTenantToApplication") 52 svc.On("SubscribeTenantToRuntime", txtest.CtxWithDBMatcher(), subscriptionProviderID, subaccountTenantExtID, providerSubaccountID, consumerTenantID, tenantRegion, subscriptionAppName).Return(true, nil).Once() 53 return svc 54 }, 55 ExpectedOutput: true, 56 ExpectedErr: nil, 57 }, 58 { 59 Name: "Error on transaction begin", 60 TransactionerFn: txGen.ThatFailsOnBegin, 61 ServiceFn: func() *automock.SubscriptionService { 62 svc := &automock.SubscriptionService{} 63 svc.AssertNotCalled(t, "SubscribeTenantToRuntime") 64 svc.AssertNotCalled(t, "SubscribeTenantToApplication") 65 svc.AssertNotCalled(t, "SubscribeTenantToRuntime") 66 return svc 67 }, 68 ExpectedOutput: false, 69 ExpectedErr: testErr, 70 }, 71 { 72 Name: "Error on flow determination", 73 TransactionerFn: txGen.ThatDoesntExpectCommit, 74 ServiceFn: func() *automock.SubscriptionService { 75 svc := &automock.SubscriptionService{} 76 svc.On("DetermineSubscriptionFlow", txtest.CtxWithDBMatcher(), subscriptionProviderID, tenantRegion).Return(resource.ApplicationTemplate, testErr).Once() 77 svc.AssertNotCalled(t, "SubscribeTenantToApplication") 78 svc.AssertNotCalled(t, "SubscribeTenantToRuntime") 79 return svc 80 }, 81 ExpectedOutput: false, 82 ExpectedErr: errors.Wrapf(testErr, "while determining subscription flow"), 83 }, 84 { 85 Name: "Error on subscription to applications fails", 86 TransactionerFn: txGen.ThatDoesntExpectCommit, 87 ServiceFn: func() *automock.SubscriptionService { 88 svc := &automock.SubscriptionService{} 89 svc.On("DetermineSubscriptionFlow", txtest.CtxWithDBMatcher(), subscriptionProviderID, tenantRegion).Return(resource.ApplicationTemplate, nil).Once() 90 svc.On("SubscribeTenantToApplication", txtest.CtxWithDBMatcher(), subscriptionProviderID, subaccountTenantExtID, consumerTenantID, tenantRegion, subscriptionAppName, emptyPayload).Return(false, testErr).Once() 91 svc.AssertNotCalled(t, "SubscribeTenantToRuntime") 92 return svc 93 }, 94 ExpectedOutput: false, 95 ExpectedErr: testErr, 96 }, 97 { 98 Name: "Error on subscription to runtime fails", 99 TransactionerFn: txGen.ThatDoesntExpectCommit, 100 ServiceFn: func() *automock.SubscriptionService { 101 svc := &automock.SubscriptionService{} 102 svc.On("DetermineSubscriptionFlow", txtest.CtxWithDBMatcher(), subscriptionProviderID, tenantRegion).Return(resource.Runtime, nil).Once() 103 svc.On("SubscribeTenantToRuntime", txtest.CtxWithDBMatcher(), subscriptionProviderID, subaccountTenantExtID, providerSubaccountID, consumerTenantID, tenantRegion, subscriptionAppName).Return(false, testErr).Once() 104 svc.AssertNotCalled(t, "SubscribeTenantToApplication") 105 return svc 106 }, 107 ExpectedOutput: false, 108 ExpectedErr: testErr, 109 }, 110 { 111 Name: "Error on commit fail", 112 TransactionerFn: txGen.ThatFailsOnCommit, 113 ServiceFn: func() *automock.SubscriptionService { 114 svc := &automock.SubscriptionService{} 115 svc.On("DetermineSubscriptionFlow", txtest.CtxWithDBMatcher(), subscriptionProviderID, tenantRegion).Return(resource.Runtime, nil).Once() 116 svc.On("SubscribeTenantToRuntime", txtest.CtxWithDBMatcher(), subscriptionProviderID, subaccountTenantExtID, providerSubaccountID, consumerTenantID, tenantRegion, subscriptionAppName).Return(true, nil).Once() 117 svc.AssertNotCalled(t, "SubscribeTenantToApplication") 118 return svc 119 }, 120 ExpectedOutput: false, 121 ExpectedErr: testErr, 122 }, 123 } 124 125 for _, testCase := range testCases { 126 t.Run(testCase.Name, func(t *testing.T) { 127 persistTx, transact := testCase.TransactionerFn() 128 svc := testCase.ServiceFn() 129 resolver := subscription.NewResolver(transact, svc) 130 131 defer mock.AssertExpectationsForObjects(t, transact, persistTx, svc) 132 133 // WHEN 134 result, err := resolver.SubscribeTenant(context.TODO(), subscriptionProviderID, subaccountTenantExtID, providerSubaccountID, consumerTenantID, tenantRegion, subscriptionAppName, emptyPayload) 135 136 // THEN 137 assert.Equal(t, testCase.ExpectedOutput, result) 138 139 if testCase.ExpectedErr != nil { 140 assert.Equal(t, testCase.ExpectedErr.Error(), err.Error()) 141 } 142 }) 143 } 144 } 145 146 func TestResolver_UnsubscribeTenant(t *testing.T) { 147 testCases := []struct { 148 Name string 149 TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) 150 ServiceFn func() *automock.SubscriptionService 151 ExpectedOutput bool 152 ExpectedErr error 153 }{ 154 { 155 Name: "Success when flow is runtime", 156 TransactionerFn: txGen.ThatSucceeds, 157 ServiceFn: func() *automock.SubscriptionService { 158 svc := &automock.SubscriptionService{} 159 svc.On("UnsubscribeTenantFromRuntime", txtest.CtxWithDBMatcher(), subscriptionProviderID, subaccountTenantExtID, providerSubaccountID, consumerTenantID, tenantRegion).Return(true, nil).Once() 160 svc.On("DetermineSubscriptionFlow", txtest.CtxWithDBMatcher(), subscriptionProviderID, tenantRegion).Return(resource.Runtime, nil).Once() 161 svc.AssertNotCalled(t, "UnsubscribeTenantFromApplication") 162 return svc 163 }, 164 ExpectedOutput: true, 165 ExpectedErr: nil, 166 }, 167 { 168 Name: "Success when flow is application", 169 TransactionerFn: txGen.ThatSucceeds, 170 ServiceFn: func() *automock.SubscriptionService { 171 svc := &automock.SubscriptionService{} 172 svc.On("UnsubscribeTenantFromApplication", txtest.CtxWithDBMatcher(), subscriptionProviderID, subaccountTenantExtID, tenantRegion).Return(true, nil).Once() 173 svc.On("DetermineSubscriptionFlow", txtest.CtxWithDBMatcher(), subscriptionProviderID, tenantRegion).Return(resource.ApplicationTemplate, nil).Once() 174 svc.AssertNotCalled(t, "UnsubscribeTenantFromRuntime") 175 return svc 176 }, 177 ExpectedOutput: true, 178 ExpectedErr: nil, 179 }, 180 { 181 Name: "Error on transaction begin", 182 TransactionerFn: txGen.ThatFailsOnBegin, 183 ServiceFn: func() *automock.SubscriptionService { 184 svc := &automock.SubscriptionService{} 185 svc.AssertNotCalled(t, "UnsubscribeTenantFromRuntime") 186 return svc 187 }, 188 ExpectedOutput: false, 189 ExpectedErr: testErr, 190 }, 191 { 192 Name: "Error determining flow type", 193 TransactionerFn: txGen.ThatDoesntExpectCommit, 194 ServiceFn: func() *automock.SubscriptionService { 195 svc := &automock.SubscriptionService{} 196 svc.AssertNotCalled(t, "UnsubscribeTenantFromRuntime") 197 svc.AssertNotCalled(t, "UnsubscribeTenantFromApplication") 198 svc.On("DetermineSubscriptionFlow", txtest.CtxWithDBMatcher(), subscriptionProviderID, tenantRegion).Return(resource.ApplicationTemplate, testErr).Once() 199 return svc 200 }, 201 ExpectedOutput: false, 202 ExpectedErr: errors.Wrapf(testErr, "while determining subscription flow"), 203 }, 204 { 205 Name: "Error on unsubscription from runtime fail", 206 TransactionerFn: txGen.ThatDoesntExpectCommit, 207 ServiceFn: func() *automock.SubscriptionService { 208 svc := &automock.SubscriptionService{} 209 svc.On("UnsubscribeTenantFromRuntime", txtest.CtxWithDBMatcher(), subscriptionProviderID, subaccountTenantExtID, providerSubaccountID, consumerTenantID, tenantRegion).Return(false, testErr).Once() 210 svc.On("DetermineSubscriptionFlow", txtest.CtxWithDBMatcher(), subscriptionProviderID, tenantRegion).Return(resource.Runtime, nil).Once() 211 212 return svc 213 }, 214 ExpectedOutput: false, 215 ExpectedErr: testErr, 216 }, 217 { 218 Name: "Error on unsubscription from application fail", 219 TransactionerFn: txGen.ThatDoesntExpectCommit, 220 ServiceFn: func() *automock.SubscriptionService { 221 svc := &automock.SubscriptionService{} 222 svc.On("UnsubscribeTenantFromApplication", txtest.CtxWithDBMatcher(), subscriptionProviderID, subaccountTenantExtID, tenantRegion).Return(false, testErr).Once() 223 svc.On("DetermineSubscriptionFlow", txtest.CtxWithDBMatcher(), subscriptionProviderID, tenantRegion).Return(resource.ApplicationTemplate, nil).Once() 224 225 return svc 226 }, 227 ExpectedOutput: false, 228 ExpectedErr: testErr, 229 }, 230 { 231 Name: "Error on commit fail", 232 TransactionerFn: txGen.ThatFailsOnCommit, 233 ServiceFn: func() *automock.SubscriptionService { 234 svc := &automock.SubscriptionService{} 235 svc.On("DetermineSubscriptionFlow", txtest.CtxWithDBMatcher(), subscriptionProviderID, tenantRegion).Return(resource.Runtime, nil).Once() 236 svc.On("UnsubscribeTenantFromRuntime", txtest.CtxWithDBMatcher(), subscriptionProviderID, subaccountTenantExtID, providerSubaccountID, consumerTenantID, tenantRegion).Return(true, nil).Once() 237 238 return svc 239 }, 240 ExpectedOutput: false, 241 ExpectedErr: testErr, 242 }, 243 } 244 245 for _, testCase := range testCases { 246 t.Run(testCase.Name, func(t *testing.T) { 247 persistTx, transact := testCase.TransactionerFn() 248 svc := testCase.ServiceFn() 249 resolver := subscription.NewResolver(transact, svc) 250 251 defer mock.AssertExpectationsForObjects(t, transact, persistTx, svc) 252 253 // WHEN 254 result, err := resolver.UnsubscribeTenant(context.TODO(), subscriptionProviderID, subaccountTenantExtID, providerSubaccountID, consumerTenantID, tenantRegion) 255 256 // THEN 257 assert.Equal(t, testCase.ExpectedOutput, result) 258 259 if testCase.ExpectedErr != nil { 260 assert.Equal(t, testCase.ExpectedErr.Error(), err.Error()) 261 } 262 }) 263 } 264 }