github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/operations_manager/opcreator_test.go (about)

     1  package operationsmanager_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/kyma-incubator/compass/components/director/internal/model"
     8  	operationsmanager "github.com/kyma-incubator/compass/components/director/internal/operations_manager"
     9  	"github.com/kyma-incubator/compass/components/director/internal/operations_manager/automock"
    10  	persistenceautomock "github.com/kyma-incubator/compass/components/director/pkg/persistence/automock"
    11  	"github.com/kyma-incubator/compass/components/director/pkg/persistence/txtest"
    12  	"github.com/kyma-incubator/compass/components/director/pkg/str"
    13  	"github.com/pkg/errors"
    14  	"github.com/stretchr/testify/assert"
    15  	"github.com/stretchr/testify/mock"
    16  	"github.com/stretchr/testify/require"
    17  )
    18  
    19  func TestORDOperationCreator_Create(t *testing.T) {
    20  	// GIVEN
    21  	testErr := errors.New("Test error")
    22  	ctx := context.TODO()
    23  	txGen := txtest.NewTransactionContextGenerator(testErr)
    24  
    25  	webhooks := []*model.Webhook{
    26  		{
    27  			ID:         "wh-id-1",
    28  			ObjectID:   "app-id",
    29  			ObjectType: model.ApplicationWebhookReference,
    30  			Type:       model.WebhookTypeOpenResourceDiscovery,
    31  			URL:        str.Ptr("https://test.com"),
    32  			Auth:       nil,
    33  		},
    34  		{
    35  			ID:         "wh-id-2",
    36  			ObjectID:   "app-template-id",
    37  			ObjectType: model.ApplicationTemplateWebhookReference,
    38  			Type:       model.WebhookTypeOpenResourceDiscovery,
    39  			URL:        str.Ptr("https://test.com"),
    40  			Auth:       nil,
    41  		},
    42  	}
    43  	testCases := []struct {
    44  		Name            string
    45  		TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
    46  		OpSvcFn         func() *automock.OperationService
    47  		WebhookSvcFn    func() *automock.WebhookService
    48  		AppSvcFn        func() *automock.ApplicationService
    49  		ExpectedErr     error
    50  	}{
    51  		{
    52  			Name: "Success",
    53  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
    54  				return txGen.ThatSucceedsMultipleTimes(3)
    55  			},
    56  			OpSvcFn: func() *automock.OperationService {
    57  				svc := &automock.OperationService{}
    58  				svc.On("CreateMultiple", txtest.CtxWithDBMatcher(), mock.AnythingOfType("[]*model.OperationInput")).Return(nil).Once()
    59  				return svc
    60  			},
    61  			WebhookSvcFn: func() *automock.WebhookService {
    62  				svc := &automock.WebhookService{}
    63  				svc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(webhooks, nil).Once()
    64  				return svc
    65  			},
    66  			AppSvcFn: func() *automock.ApplicationService {
    67  				apps := []*model.Application{
    68  					{
    69  						BaseEntity: &model.BaseEntity{
    70  							ID: "app-id",
    71  						},
    72  					},
    73  				}
    74  				svc := &automock.ApplicationService{}
    75  				svc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), "app-template-id").Return(apps, nil).Once()
    76  				return svc
    77  			},
    78  		},
    79  		{
    80  			Name: "Error while getting webhooks with ord type",
    81  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
    82  				return txGen.ThatDoesntExpectCommit()
    83  			},
    84  			OpSvcFn: func() *automock.OperationService {
    85  				return &automock.OperationService{}
    86  			},
    87  			WebhookSvcFn: func() *automock.WebhookService {
    88  				svc := &automock.WebhookService{}
    89  				svc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(nil, testErr).Once()
    90  				return svc
    91  			},
    92  			AppSvcFn: func() *automock.ApplicationService {
    93  				return &automock.ApplicationService{}
    94  			},
    95  			ExpectedErr: testErr,
    96  		},
    97  		{
    98  			Name: "Error while beginning transaction on getting webhooks with ord type",
    99  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
   100  				return txGen.ThatFailsOnBegin()
   101  			},
   102  			OpSvcFn: func() *automock.OperationService {
   103  				return &automock.OperationService{}
   104  			},
   105  			WebhookSvcFn: func() *automock.WebhookService {
   106  				return &automock.WebhookService{}
   107  			},
   108  			AppSvcFn: func() *automock.ApplicationService {
   109  				return &automock.ApplicationService{}
   110  			},
   111  			ExpectedErr: testErr,
   112  		},
   113  		{
   114  			Name: "Error while committing transaction on getting webhooks with ord type",
   115  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
   116  				return txGen.ThatFailsOnCommit()
   117  			},
   118  			OpSvcFn: func() *automock.OperationService {
   119  				return &automock.OperationService{}
   120  			},
   121  			WebhookSvcFn: func() *automock.WebhookService {
   122  				svc := &automock.WebhookService{}
   123  				svc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(webhooks, nil).Once()
   124  				return svc
   125  			},
   126  			AppSvcFn: func() *automock.ApplicationService {
   127  				return &automock.ApplicationService{}
   128  			},
   129  			ExpectedErr: testErr,
   130  		},
   131  		{
   132  			Name: "Error while creating multiple operations",
   133  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
   134  				return txGen.ThatSucceedsMultipleTimes(3)
   135  			},
   136  			OpSvcFn: func() *automock.OperationService {
   137  				svc := &automock.OperationService{}
   138  				svc.On("CreateMultiple", txtest.CtxWithDBMatcher(), mock.AnythingOfType("[]*model.OperationInput")).Return(testErr).Once()
   139  				return svc
   140  			},
   141  			WebhookSvcFn: func() *automock.WebhookService {
   142  				svc := &automock.WebhookService{}
   143  				svc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(webhooks, nil).Once()
   144  				return svc
   145  			},
   146  			AppSvcFn: func() *automock.ApplicationService {
   147  				apps := []*model.Application{
   148  					{
   149  						BaseEntity: &model.BaseEntity{
   150  							ID: "app-id",
   151  						},
   152  					},
   153  				}
   154  				svc := &automock.ApplicationService{}
   155  				svc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), "app-template-id").Return(apps, nil).Once()
   156  				return svc
   157  			},
   158  			ExpectedErr: testErr,
   159  		},
   160  		{
   161  			Name: "Error while listing applications by app template id",
   162  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
   163  				return txGen.ThatSucceedsMultipleTimes(2)
   164  			},
   165  			OpSvcFn: func() *automock.OperationService {
   166  				return &automock.OperationService{}
   167  			},
   168  			WebhookSvcFn: func() *automock.WebhookService {
   169  				svc := &automock.WebhookService{}
   170  				svc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(webhooks, nil).Once()
   171  				return svc
   172  			},
   173  			AppSvcFn: func() *automock.ApplicationService {
   174  				svc := &automock.ApplicationService{}
   175  				svc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), "app-template-id").Return(nil, testErr).Once()
   176  				return svc
   177  			},
   178  			ExpectedErr: testErr,
   179  		},
   180  	}
   181  
   182  	for _, testCase := range testCases {
   183  		t.Run(testCase.Name, func(t *testing.T) {
   184  			// GIVEN
   185  			_, tx := testCase.TransactionerFn()
   186  			opSvc := testCase.OpSvcFn()
   187  			webhookSvc := testCase.WebhookSvcFn()
   188  			appSvc := testCase.AppSvcFn()
   189  
   190  			opCreator := operationsmanager.NewOperationCreator(operationsmanager.OrdCreatorType, tx, opSvc, webhookSvc, appSvc)
   191  
   192  			// WHEN
   193  			err := opCreator.Create(ctx)
   194  
   195  			// THEN
   196  			if testCase.ExpectedErr != nil {
   197  				require.Error(t, err)
   198  				assert.Contains(t, err.Error(), testCase.ExpectedErr.Error())
   199  			} else {
   200  				assert.Nil(t, err)
   201  			}
   202  
   203  			mock.AssertExpectationsForObjects(t, tx, opSvc, webhookSvc, appSvc)
   204  		})
   205  	}
   206  }