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

     1  package eventdef_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/kyma-incubator/compass/components/director/pkg/pagination"
     9  	"github.com/stretchr/testify/mock"
    10  
    11  	dataloader "github.com/kyma-incubator/compass/components/director/internal/dataloaders"
    12  
    13  	"github.com/kyma-incubator/compass/components/director/pkg/str"
    14  
    15  	"github.com/kyma-incubator/compass/components/director/pkg/apperrors"
    16  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
    17  
    18  	event "github.com/kyma-incubator/compass/components/director/internal/domain/eventdef"
    19  	"github.com/kyma-incubator/compass/components/director/internal/domain/eventdef/automock"
    20  	"github.com/kyma-incubator/compass/components/director/internal/model"
    21  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    22  	persistenceautomock "github.com/kyma-incubator/compass/components/director/pkg/persistence/automock"
    23  	"github.com/kyma-incubator/compass/components/director/pkg/persistence/txtest"
    24  	"github.com/pkg/errors"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  func TestResolver_AddEventToBundle(t *testing.T) {
    30  	// GIVEN
    31  	testErr := errors.New("Test error")
    32  
    33  	id := "bar"
    34  
    35  	modelEvent, spec, bundleRef := fixFullEventDefinitionModel("test")
    36  	modelBndl := &model.Bundle{
    37  		ApplicationID: &appID,
    38  		BaseEntity: &model.BaseEntity{
    39  			ID: bundleID,
    40  		},
    41  	}
    42  	gqlEvent := fixFullGQLEventDefinition("test")
    43  	gqlEventInput := fixGQLEventDefinitionInput("name", "foo", "bar")
    44  	modelEventInput, specInput := fixModelEventDefinitionInput("name", "foo", "bar")
    45  
    46  	txGen := txtest.NewTransactionContextGenerator(testErr)
    47  
    48  	testCases := []struct {
    49  		Name                   string
    50  		TransactionerFn        func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
    51  		ServiceFn              func() *automock.EventDefService
    52  		BndlServiceFn          func() *automock.BundleService
    53  		BndlReferenceServiceFn func() *automock.BundleReferenceService
    54  		SpecServiceFn          func() *automock.SpecService
    55  		ConverterFn            func() *automock.EventDefConverter
    56  		ExpectedEvent          *graphql.EventDefinition
    57  		ExpectedErr            error
    58  	}{
    59  		{
    60  			Name:            "Success",
    61  			TransactionerFn: txGen.ThatSucceeds,
    62  			ServiceFn: func() *automock.EventDefService {
    63  				svc := &automock.EventDefService{}
    64  				svc.On("CreateInBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, bundleID, *modelEventInput, specInput).Return(id, nil).Once()
    65  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEvent, nil).Once()
    66  				return svc
    67  			},
    68  			BndlServiceFn: func() *automock.BundleService {
    69  				appSvc := &automock.BundleService{}
    70  				appSvc.On("Get", txtest.CtxWithDBMatcher(), bundleID).Return(modelBndl, nil)
    71  				return appSvc
    72  			},
    73  			BndlReferenceServiceFn: func() *automock.BundleReferenceService {
    74  				svc := &automock.BundleReferenceService{}
    75  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &gqlEvent.ID, str.Ptr(bundleID)).Return(&bundleRef, nil).Once()
    76  				return svc
    77  			},
    78  			SpecServiceFn: func() *automock.SpecService {
    79  				svc := &automock.SpecService{}
    80  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(&spec, nil).Once()
    81  				return svc
    82  			},
    83  			ConverterFn: func() *automock.EventDefConverter {
    84  				conv := &automock.EventDefConverter{}
    85  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelEventInput, specInput, nil).Once()
    86  				conv.On("ToGraphQL", &modelEvent, &spec, &bundleRef).Return(gqlEvent, nil).Once()
    87  				return conv
    88  			},
    89  			ExpectedEvent: gqlEvent,
    90  			ExpectedErr:   nil,
    91  		},
    92  		{
    93  			Name:            "Returns error when starting transaction",
    94  			TransactionerFn: txGen.ThatFailsOnBegin,
    95  			ServiceFn: func() *automock.EventDefService {
    96  				return &automock.EventDefService{}
    97  			},
    98  			BndlServiceFn: func() *automock.BundleService {
    99  				return &automock.BundleService{}
   100  			},
   101  			BndlReferenceServiceFn: func() *automock.BundleReferenceService {
   102  				return &automock.BundleReferenceService{}
   103  			},
   104  			ConverterFn: func() *automock.EventDefConverter {
   105  				return &automock.EventDefConverter{}
   106  			},
   107  			SpecServiceFn: func() *automock.SpecService {
   108  				return &automock.SpecService{}
   109  			},
   110  			ExpectedEvent: nil,
   111  			ExpectedErr:   testErr,
   112  		},
   113  		{
   114  			Name:            "Returns error when bundle not exist",
   115  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   116  			ServiceFn: func() *automock.EventDefService {
   117  				return &automock.EventDefService{}
   118  			},
   119  			BndlServiceFn: func() *automock.BundleService {
   120  				appSvc := &automock.BundleService{}
   121  				appSvc.On("Get", txtest.CtxWithDBMatcher(), bundleID).Return(nil, apperrors.NewNotFoundError(resource.Bundle, bundleID))
   122  				return appSvc
   123  			},
   124  			BndlReferenceServiceFn: func() *automock.BundleReferenceService {
   125  				return &automock.BundleReferenceService{}
   126  			},
   127  			ConverterFn: func() *automock.EventDefConverter {
   128  				conv := &automock.EventDefConverter{}
   129  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelEventInput, specInput, nil).Once()
   130  				return conv
   131  			},
   132  			SpecServiceFn: func() *automock.SpecService {
   133  				return &automock.SpecService{}
   134  			},
   135  			ExpectedEvent: nil,
   136  			ExpectedErr:   errors.New("cannot add Event Definition to not existing Bundle"),
   137  		},
   138  		{
   139  			Name:            "Returns error when bundle existence check failed",
   140  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   141  			ServiceFn: func() *automock.EventDefService {
   142  				svc := &automock.EventDefService{}
   143  				return svc
   144  			},
   145  			BndlServiceFn: func() *automock.BundleService {
   146  				appSvc := &automock.BundleService{}
   147  				appSvc.On("Get", txtest.CtxWithDBMatcher(), bundleID).Return(nil, testErr)
   148  				return appSvc
   149  			},
   150  			BndlReferenceServiceFn: func() *automock.BundleReferenceService {
   151  				return &automock.BundleReferenceService{}
   152  			},
   153  			ConverterFn: func() *automock.EventDefConverter {
   154  				conv := &automock.EventDefConverter{}
   155  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelEventInput, specInput, nil).Once()
   156  				return conv
   157  			},
   158  			SpecServiceFn: func() *automock.SpecService {
   159  				return &automock.SpecService{}
   160  			},
   161  			ExpectedEvent: nil,
   162  			ExpectedErr:   testErr,
   163  		},
   164  		{
   165  			Name:            "Returns error when Event creation failed",
   166  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   167  			ServiceFn: func() *automock.EventDefService {
   168  				svc := &automock.EventDefService{}
   169  				svc.On("CreateInBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, bundleID, *modelEventInput, specInput).Return("", testErr).Once()
   170  				return svc
   171  			},
   172  			BndlServiceFn: func() *automock.BundleService {
   173  				appSvc := &automock.BundleService{}
   174  				appSvc.On("Get", txtest.CtxWithDBMatcher(), bundleID).Return(modelBndl, nil)
   175  				return appSvc
   176  			},
   177  			BndlReferenceServiceFn: func() *automock.BundleReferenceService {
   178  				return &automock.BundleReferenceService{}
   179  			},
   180  			ConverterFn: func() *automock.EventDefConverter {
   181  				conv := &automock.EventDefConverter{}
   182  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelEventInput, specInput, nil).Once()
   183  				return conv
   184  			},
   185  			SpecServiceFn: func() *automock.SpecService {
   186  				return &automock.SpecService{}
   187  			},
   188  			ExpectedEvent: nil,
   189  			ExpectedErr:   testErr,
   190  		},
   191  		{
   192  			Name:            "Returns error when Event retrieval failed",
   193  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   194  			ServiceFn: func() *automock.EventDefService {
   195  				svc := &automock.EventDefService{}
   196  				svc.On("CreateInBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, bundleID, *modelEventInput, specInput).Return(id, nil).Once()
   197  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(nil, testErr).Once()
   198  				return svc
   199  			},
   200  			BndlServiceFn: func() *automock.BundleService {
   201  				appSvc := &automock.BundleService{}
   202  				appSvc.On("Get", txtest.CtxWithDBMatcher(), bundleID).Return(modelBndl, nil)
   203  				return appSvc
   204  			},
   205  			BndlReferenceServiceFn: func() *automock.BundleReferenceService {
   206  				return &automock.BundleReferenceService{}
   207  			},
   208  			ConverterFn: func() *automock.EventDefConverter {
   209  				conv := &automock.EventDefConverter{}
   210  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelEventInput, specInput, nil).Once()
   211  				return conv
   212  			},
   213  			SpecServiceFn: func() *automock.SpecService {
   214  				return &automock.SpecService{}
   215  			},
   216  			ExpectedEvent: nil,
   217  			ExpectedErr:   testErr,
   218  		},
   219  		{
   220  			Name:            "Returns error when Spec retrieval failed",
   221  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   222  			ServiceFn: func() *automock.EventDefService {
   223  				svc := &automock.EventDefService{}
   224  				svc.On("CreateInBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, bundleID, *modelEventInput, specInput).Return(id, nil).Once()
   225  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEvent, nil).Once()
   226  				return svc
   227  			},
   228  			BndlServiceFn: func() *automock.BundleService {
   229  				appSvc := &automock.BundleService{}
   230  				appSvc.On("Get", txtest.CtxWithDBMatcher(), bundleID).Return(modelBndl, nil)
   231  				return appSvc
   232  			},
   233  			BndlReferenceServiceFn: func() *automock.BundleReferenceService {
   234  				return &automock.BundleReferenceService{}
   235  			},
   236  			ConverterFn: func() *automock.EventDefConverter {
   237  				conv := &automock.EventDefConverter{}
   238  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelEventInput, specInput, nil).Once()
   239  				return conv
   240  			},
   241  			SpecServiceFn: func() *automock.SpecService {
   242  				svc := &automock.SpecService{}
   243  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(nil, testErr).Once()
   244  				return svc
   245  			},
   246  			ExpectedEvent: nil,
   247  			ExpectedErr:   testErr,
   248  		},
   249  		{
   250  			Name:            "Returns error when BundleReference retrieval failed",
   251  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   252  			ServiceFn: func() *automock.EventDefService {
   253  				svc := &automock.EventDefService{}
   254  				svc.On("CreateInBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, bundleID, *modelEventInput, specInput).Return(id, nil).Once()
   255  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEvent, nil).Once()
   256  				return svc
   257  			},
   258  			BndlServiceFn: func() *automock.BundleService {
   259  				appSvc := &automock.BundleService{}
   260  				appSvc.On("Get", txtest.CtxWithDBMatcher(), bundleID).Return(modelBndl, nil)
   261  				return appSvc
   262  			},
   263  			BndlReferenceServiceFn: func() *automock.BundleReferenceService {
   264  				svc := &automock.BundleReferenceService{}
   265  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &gqlEvent.ID, str.Ptr(bundleID)).Return(nil, testErr).Once()
   266  				return svc
   267  			},
   268  			ConverterFn: func() *automock.EventDefConverter {
   269  				conv := &automock.EventDefConverter{}
   270  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelEventInput, specInput, nil).Once()
   271  				return conv
   272  			},
   273  			SpecServiceFn: func() *automock.SpecService {
   274  				svc := &automock.SpecService{}
   275  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(&spec, nil).Once()
   276  				return svc
   277  			},
   278  			ExpectedEvent: nil,
   279  			ExpectedErr:   testErr,
   280  		},
   281  		{
   282  			Name:            "Success",
   283  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   284  			ServiceFn: func() *automock.EventDefService {
   285  				svc := &automock.EventDefService{}
   286  				svc.On("CreateInBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, bundleID, *modelEventInput, specInput).Return(id, nil).Once()
   287  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEvent, nil).Once()
   288  				return svc
   289  			},
   290  			BndlServiceFn: func() *automock.BundleService {
   291  				appSvc := &automock.BundleService{}
   292  				appSvc.On("Get", txtest.CtxWithDBMatcher(), bundleID).Return(modelBndl, nil)
   293  				return appSvc
   294  			},
   295  			BndlReferenceServiceFn: func() *automock.BundleReferenceService {
   296  				svc := &automock.BundleReferenceService{}
   297  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &gqlEvent.ID, str.Ptr(bundleID)).Return(&bundleRef, nil).Once()
   298  				return svc
   299  			},
   300  			SpecServiceFn: func() *automock.SpecService {
   301  				svc := &automock.SpecService{}
   302  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(&spec, nil).Once()
   303  				return svc
   304  			},
   305  			ConverterFn: func() *automock.EventDefConverter {
   306  				conv := &automock.EventDefConverter{}
   307  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelEventInput, specInput, nil).Once()
   308  				conv.On("ToGraphQL", &modelEvent, &spec, &bundleRef).Return(gqlEvent, testErr).Once()
   309  				return conv
   310  			},
   311  			ExpectedEvent: nil,
   312  			ExpectedErr:   testErr,
   313  		},
   314  		{
   315  			Name:            "Returns error when commit transaction failed",
   316  			TransactionerFn: txGen.ThatFailsOnCommit,
   317  			ServiceFn: func() *automock.EventDefService {
   318  				svc := &automock.EventDefService{}
   319  				svc.On("CreateInBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, bundleID, *modelEventInput, specInput).Return(id, nil).Once()
   320  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEvent, nil).Once()
   321  				return svc
   322  			},
   323  			BndlServiceFn: func() *automock.BundleService {
   324  				appSvc := &automock.BundleService{}
   325  				appSvc.On("Get", txtest.CtxWithDBMatcher(), bundleID).Return(modelBndl, nil)
   326  				return appSvc
   327  			},
   328  			BndlReferenceServiceFn: func() *automock.BundleReferenceService {
   329  				svc := &automock.BundleReferenceService{}
   330  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &gqlEvent.ID, str.Ptr(bundleID)).Return(&bundleRef, nil).Once()
   331  				return svc
   332  			},
   333  			ConverterFn: func() *automock.EventDefConverter {
   334  				conv := &automock.EventDefConverter{}
   335  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelEventInput, specInput, nil).Once()
   336  				conv.On("ToGraphQL", &modelEvent, &spec, &bundleRef).Return(gqlEvent, nil).Once()
   337  				return conv
   338  			},
   339  			SpecServiceFn: func() *automock.SpecService {
   340  				svc := &automock.SpecService{}
   341  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(&spec, nil).Once()
   342  				return svc
   343  			},
   344  			ExpectedEvent: nil,
   345  			ExpectedErr:   testErr,
   346  		},
   347  	}
   348  
   349  	for _, testCase := range testCases {
   350  		t.Run(testCase.Name, func(t *testing.T) {
   351  			// GIVEN
   352  			persist, transact := testCase.TransactionerFn()
   353  			svc := testCase.ServiceFn()
   354  			converter := testCase.ConverterFn()
   355  			bndlSvc := testCase.BndlServiceFn()
   356  			specSvc := testCase.SpecServiceFn()
   357  			bndlRefSvc := testCase.BndlReferenceServiceFn()
   358  
   359  			resolver := event.NewResolver(transact, svc, bndlSvc, bndlRefSvc, converter, nil, specSvc, nil)
   360  
   361  			// WHEN
   362  			result, err := resolver.AddEventDefinitionToBundle(context.TODO(), bundleID, *gqlEventInput)
   363  
   364  			// then
   365  			assert.Equal(t, testCase.ExpectedEvent, result)
   366  			if testCase.ExpectedErr != nil {
   367  				require.Error(t, err)
   368  				assert.Contains(t, err.Error(), testCase.ExpectedErr.Error())
   369  			} else {
   370  				require.Nil(t, err)
   371  			}
   372  
   373  			persist.AssertExpectations(t)
   374  			transact.AssertExpectations(t)
   375  			svc.AssertExpectations(t)
   376  			bndlSvc.AssertExpectations(t)
   377  			bndlRefSvc.AssertExpectations(t)
   378  			specSvc.AssertExpectations(t)
   379  			converter.AssertExpectations(t)
   380  		})
   381  	}
   382  }
   383  
   384  func TestResolver_DeleteEvent(t *testing.T) {
   385  	// GIVEN
   386  	testErr := errors.New("Test error")
   387  
   388  	id := "bar"
   389  	modelEventDefinition, spec, bundleRef := fixFullEventDefinitionModel("test")
   390  	gqlEventDefinition := fixFullGQLEventDefinition("test")
   391  
   392  	var nilBundleID *string
   393  
   394  	txGen := txtest.NewTransactionContextGenerator(testErr)
   395  
   396  	testCases := []struct {
   397  		Name              string
   398  		TransactionerFn   func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
   399  		ServiceFn         func() *automock.EventDefService
   400  		ConverterFn       func() *automock.EventDefConverter
   401  		SpecServiceFn     func() *automock.SpecService
   402  		BundleReferenceFn func() *automock.BundleReferenceService
   403  		ExpectedEvent     *graphql.EventDefinition
   404  		ExpectedErr       error
   405  	}{
   406  		{
   407  			Name:            "Success",
   408  			TransactionerFn: txGen.ThatSucceeds,
   409  			ServiceFn: func() *automock.EventDefService {
   410  				svc := &automock.EventDefService{}
   411  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
   412  				svc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, id).Return(nil).Once()
   413  				return svc
   414  			},
   415  			ConverterFn: func() *automock.EventDefConverter {
   416  				conv := &automock.EventDefConverter{}
   417  				conv.On("ToGraphQL", &modelEventDefinition, &spec, &bundleRef).Return(gqlEventDefinition, nil).Once()
   418  				return conv
   419  			},
   420  			SpecServiceFn: func() *automock.SpecService {
   421  				svc := &automock.SpecService{}
   422  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(&spec, nil).Once()
   423  				return svc
   424  			},
   425  			BundleReferenceFn: func() *automock.BundleReferenceService {
   426  				svc := &automock.BundleReferenceService{}
   427  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &modelEventDefinition.ID, nilBundleID).Return(&bundleRef, nil).Once()
   428  				return svc
   429  			},
   430  			ExpectedEvent: gqlEventDefinition,
   431  			ExpectedErr:   nil,
   432  		},
   433  		{
   434  			Name:            "Return error when starting transaction fails",
   435  			TransactionerFn: txGen.ThatFailsOnBegin,
   436  			ServiceFn: func() *automock.EventDefService {
   437  				return &automock.EventDefService{}
   438  			},
   439  			ConverterFn: func() *automock.EventDefConverter {
   440  				return &automock.EventDefConverter{}
   441  			},
   442  			SpecServiceFn: func() *automock.SpecService {
   443  				return &automock.SpecService{}
   444  			},
   445  			BundleReferenceFn: func() *automock.BundleReferenceService {
   446  				return &automock.BundleReferenceService{}
   447  			},
   448  			ExpectedEvent: nil,
   449  			ExpectedErr:   testErr,
   450  		},
   451  		{
   452  			Name:            "Returns error when Event retrieval failed",
   453  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   454  			ServiceFn: func() *automock.EventDefService {
   455  				svc := &automock.EventDefService{}
   456  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(nil, testErr).Once()
   457  				return svc
   458  			},
   459  			ConverterFn: func() *automock.EventDefConverter {
   460  				return &automock.EventDefConverter{}
   461  			},
   462  			SpecServiceFn: func() *automock.SpecService {
   463  				return &automock.SpecService{}
   464  			},
   465  			BundleReferenceFn: func() *automock.BundleReferenceService {
   466  				return &automock.BundleReferenceService{}
   467  			},
   468  			ExpectedEvent: nil,
   469  			ExpectedErr:   testErr,
   470  		},
   471  		{
   472  			Name:            "Returns error when Spec retrieval failed",
   473  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   474  			ServiceFn: func() *automock.EventDefService {
   475  				svc := &automock.EventDefService{}
   476  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
   477  				return svc
   478  			},
   479  			ConverterFn: func() *automock.EventDefConverter {
   480  				return &automock.EventDefConverter{}
   481  			},
   482  			SpecServiceFn: func() *automock.SpecService {
   483  				svc := &automock.SpecService{}
   484  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(nil, testErr).Once()
   485  				return svc
   486  			},
   487  			BundleReferenceFn: func() *automock.BundleReferenceService {
   488  				return &automock.BundleReferenceService{}
   489  			},
   490  			ExpectedEvent: nil,
   491  			ExpectedErr:   testErr,
   492  		},
   493  		{
   494  			Name:            "Returns error when BundleReference retrieval failed",
   495  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   496  			ServiceFn: func() *automock.EventDefService {
   497  				svc := &automock.EventDefService{}
   498  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
   499  				return svc
   500  			},
   501  			ConverterFn: func() *automock.EventDefConverter {
   502  				return &automock.EventDefConverter{}
   503  			},
   504  			SpecServiceFn: func() *automock.SpecService {
   505  				svc := &automock.SpecService{}
   506  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(&spec, nil).Once()
   507  				return svc
   508  			},
   509  			BundleReferenceFn: func() *automock.BundleReferenceService {
   510  				svc := &automock.BundleReferenceService{}
   511  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &modelEventDefinition.ID, nilBundleID).Return(nil, testErr).Once()
   512  				return svc
   513  			},
   514  			ExpectedEvent: nil,
   515  			ExpectedErr:   testErr,
   516  		},
   517  		{
   518  			Name:            "Returns error when Event conversion failed",
   519  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   520  			ServiceFn: func() *automock.EventDefService {
   521  				svc := &automock.EventDefService{}
   522  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
   523  				return svc
   524  			},
   525  			ConverterFn: func() *automock.EventDefConverter {
   526  				conv := &automock.EventDefConverter{}
   527  				conv.On("ToGraphQL", &modelEventDefinition, &spec, &bundleRef).Return(nil, testErr).Once()
   528  				return conv
   529  			},
   530  			SpecServiceFn: func() *automock.SpecService {
   531  				svc := &automock.SpecService{}
   532  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(&spec, nil).Once()
   533  				return svc
   534  			},
   535  			BundleReferenceFn: func() *automock.BundleReferenceService {
   536  				svc := &automock.BundleReferenceService{}
   537  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &modelEventDefinition.ID, nilBundleID).Return(&bundleRef, nil).Once()
   538  				return svc
   539  			},
   540  			ExpectedEvent: nil,
   541  			ExpectedErr:   testErr,
   542  		},
   543  		{
   544  			Name:            "Returns error when Event deletion failed",
   545  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   546  			ServiceFn: func() *automock.EventDefService {
   547  				svc := &automock.EventDefService{}
   548  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
   549  				svc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, id).Return(testErr).Once()
   550  				return svc
   551  			},
   552  			ConverterFn: func() *automock.EventDefConverter {
   553  				conv := &automock.EventDefConverter{}
   554  				conv.On("ToGraphQL", &modelEventDefinition, &spec, &bundleRef).Return(gqlEventDefinition, nil).Once()
   555  				return conv
   556  			},
   557  			SpecServiceFn: func() *automock.SpecService {
   558  				svc := &automock.SpecService{}
   559  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(&spec, nil).Once()
   560  				return svc
   561  			},
   562  			BundleReferenceFn: func() *automock.BundleReferenceService {
   563  				svc := &automock.BundleReferenceService{}
   564  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &modelEventDefinition.ID, nilBundleID).Return(&bundleRef, nil).Once()
   565  				return svc
   566  			},
   567  			ExpectedEvent: nil,
   568  			ExpectedErr:   testErr,
   569  		},
   570  		{
   571  			Name:            "Return error when commit transaction fails",
   572  			TransactionerFn: txGen.ThatFailsOnCommit,
   573  			ServiceFn: func() *automock.EventDefService {
   574  				svc := &automock.EventDefService{}
   575  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
   576  				svc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, id).Return(nil).Once()
   577  				return svc
   578  			},
   579  			ConverterFn: func() *automock.EventDefConverter {
   580  				conv := &automock.EventDefConverter{}
   581  				conv.On("ToGraphQL", &modelEventDefinition, &spec, &bundleRef).Return(gqlEventDefinition, nil).Once()
   582  				return conv
   583  			},
   584  			SpecServiceFn: func() *automock.SpecService {
   585  				svc := &automock.SpecService{}
   586  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(&spec, nil).Once()
   587  				return svc
   588  			},
   589  			BundleReferenceFn: func() *automock.BundleReferenceService {
   590  				svc := &automock.BundleReferenceService{}
   591  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &modelEventDefinition.ID, nilBundleID).Return(&bundleRef, nil).Once()
   592  				return svc
   593  			},
   594  			ExpectedEvent: nil,
   595  			ExpectedErr:   testErr,
   596  		},
   597  	}
   598  
   599  	for _, testCase := range testCases {
   600  		t.Run(testCase.Name, func(t *testing.T) {
   601  			// GIVEN
   602  			persist, transact := testCase.TransactionerFn()
   603  			svc := testCase.ServiceFn()
   604  			specService := testCase.SpecServiceFn()
   605  			converter := testCase.ConverterFn()
   606  			bndlRefService := testCase.BundleReferenceFn()
   607  
   608  			resolver := event.NewResolver(transact, svc, nil, bndlRefService, converter, nil, specService, nil)
   609  
   610  			// WHEN
   611  			result, err := resolver.DeleteEventDefinition(context.TODO(), id)
   612  
   613  			// then
   614  			assert.Equal(t, testCase.ExpectedEvent, result)
   615  			if testCase.ExpectedErr != nil {
   616  				require.Error(t, err)
   617  				assert.Contains(t, err.Error(), testCase.ExpectedErr.Error())
   618  			} else {
   619  				require.Nil(t, err)
   620  			}
   621  
   622  			svc.AssertExpectations(t)
   623  			specService.AssertExpectations(t)
   624  			bndlRefService.AssertExpectations(t)
   625  			converter.AssertExpectations(t)
   626  			transact.AssertExpectations(t)
   627  			persist.AssertExpectations(t)
   628  		})
   629  	}
   630  }
   631  
   632  func TestResolver_UpdateEvent(t *testing.T) {
   633  	// GIVEN
   634  	testErr := errors.New("Test error")
   635  
   636  	id := "bar"
   637  	gqlEventDefinitionInput := fixGQLEventDefinitionInput(id, "foo", "bar")
   638  	modelEventDefinitionInput, modelSpecInput := fixModelEventDefinitionInput(id, "foo", "bar")
   639  	gqlEventDefinition := fixFullGQLEventDefinition("test")
   640  	modelEventDefinition, modelSpec, modelBundleReference := fixFullEventDefinitionModel("test")
   641  
   642  	var nilBundleID *string
   643  
   644  	txGen := txtest.NewTransactionContextGenerator(testErr)
   645  
   646  	testCases := []struct {
   647  		Name                     string
   648  		TransactionerFn          func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
   649  		ServiceFn                func() *automock.EventDefService
   650  		ConverterFn              func() *automock.EventDefConverter
   651  		SpecServiceFn            func() *automock.SpecService
   652  		BundleReferenceServiceFn func() *automock.BundleReferenceService
   653  		InputWebhookID           string
   654  		InputEvent               graphql.EventDefinitionInput
   655  		ExpectedEventDefinition  *graphql.EventDefinition
   656  		ExpectedErr              error
   657  	}{
   658  		{
   659  			Name:            "Success",
   660  			TransactionerFn: txGen.ThatSucceeds,
   661  			ServiceFn: func() *automock.EventDefService {
   662  				svc := &automock.EventDefService{}
   663  				svc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, id, *modelEventDefinitionInput, modelSpecInput).Return(nil).Once()
   664  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
   665  				return svc
   666  			},
   667  			ConverterFn: func() *automock.EventDefConverter {
   668  				conv := &automock.EventDefConverter{}
   669  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
   670  				conv.On("ToGraphQL", &modelEventDefinition, &modelSpec, &modelBundleReference).Return(gqlEventDefinition, nil).Once()
   671  				return conv
   672  			},
   673  			SpecServiceFn: func() *automock.SpecService {
   674  				svc := &automock.SpecService{}
   675  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(&modelSpec, nil).Once()
   676  				return svc
   677  			},
   678  			BundleReferenceServiceFn: func() *automock.BundleReferenceService {
   679  				svc := &automock.BundleReferenceService{}
   680  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &modelEventDefinition.ID, nilBundleID).Return(&modelBundleReference, nil).Once()
   681  				return svc
   682  			},
   683  			InputWebhookID:          id,
   684  			InputEvent:              *gqlEventDefinitionInput,
   685  			ExpectedEventDefinition: gqlEventDefinition,
   686  			ExpectedErr:             nil,
   687  		},
   688  		{
   689  			Name:            "Returns error when starting transaction failed",
   690  			TransactionerFn: txGen.ThatFailsOnBegin,
   691  			ServiceFn: func() *automock.EventDefService {
   692  				return &automock.EventDefService{}
   693  			},
   694  			ConverterFn: func() *automock.EventDefConverter {
   695  				return &automock.EventDefConverter{}
   696  			},
   697  			SpecServiceFn: func() *automock.SpecService {
   698  				return &automock.SpecService{}
   699  			},
   700  			BundleReferenceServiceFn: func() *automock.BundleReferenceService {
   701  				return &automock.BundleReferenceService{}
   702  			},
   703  			InputWebhookID:          id,
   704  			InputEvent:              *gqlEventDefinitionInput,
   705  			ExpectedEventDefinition: nil,
   706  			ExpectedErr:             testErr,
   707  		},
   708  		{
   709  			Name:            "Returns error when converting input to GraphQL fails",
   710  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   711  			ServiceFn: func() *automock.EventDefService {
   712  				return &automock.EventDefService{}
   713  			},
   714  			ConverterFn: func() *automock.EventDefConverter {
   715  				conv := &automock.EventDefConverter{}
   716  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(nil, nil, testErr).Once()
   717  				return conv
   718  			},
   719  			SpecServiceFn: func() *automock.SpecService {
   720  				return &automock.SpecService{}
   721  			},
   722  			BundleReferenceServiceFn: func() *automock.BundleReferenceService {
   723  				return &automock.BundleReferenceService{}
   724  			},
   725  			InputWebhookID:          id,
   726  			InputEvent:              *gqlEventDefinitionInput,
   727  			ExpectedEventDefinition: nil,
   728  			ExpectedErr:             testErr,
   729  		},
   730  		{
   731  			Name:            "Returns error when Event update failed",
   732  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   733  			ServiceFn: func() *automock.EventDefService {
   734  				svc := &automock.EventDefService{}
   735  				svc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, id, *modelEventDefinitionInput, modelSpecInput).Return(testErr).Once()
   736  				return svc
   737  			},
   738  			ConverterFn: func() *automock.EventDefConverter {
   739  				conv := &automock.EventDefConverter{}
   740  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
   741  				return conv
   742  			},
   743  			SpecServiceFn: func() *automock.SpecService {
   744  				return &automock.SpecService{}
   745  			},
   746  			BundleReferenceServiceFn: func() *automock.BundleReferenceService {
   747  				return &automock.BundleReferenceService{}
   748  			},
   749  			InputWebhookID:          id,
   750  			InputEvent:              *gqlEventDefinitionInput,
   751  			ExpectedEventDefinition: nil,
   752  			ExpectedErr:             testErr,
   753  		},
   754  		{
   755  			Name:            "Returns error when Event retrieval failed",
   756  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   757  			ServiceFn: func() *automock.EventDefService {
   758  				svc := &automock.EventDefService{}
   759  				svc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, id, *modelEventDefinitionInput, modelSpecInput).Return(nil).Once()
   760  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(nil, testErr).Once()
   761  				return svc
   762  			},
   763  			ConverterFn: func() *automock.EventDefConverter {
   764  				conv := &automock.EventDefConverter{}
   765  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
   766  				return conv
   767  			},
   768  			SpecServiceFn: func() *automock.SpecService {
   769  				return &automock.SpecService{}
   770  			},
   771  			BundleReferenceServiceFn: func() *automock.BundleReferenceService {
   772  				return &automock.BundleReferenceService{}
   773  			},
   774  			InputWebhookID:          id,
   775  			InputEvent:              *gqlEventDefinitionInput,
   776  			ExpectedEventDefinition: nil,
   777  			ExpectedErr:             testErr,
   778  		},
   779  		{
   780  			Name:            "Returns error when Spec retrieval failed",
   781  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   782  			ServiceFn: func() *automock.EventDefService {
   783  				svc := &automock.EventDefService{}
   784  				svc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, id, *modelEventDefinitionInput, modelSpecInput).Return(nil).Once()
   785  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
   786  				return svc
   787  			},
   788  			ConverterFn: func() *automock.EventDefConverter {
   789  				conv := &automock.EventDefConverter{}
   790  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
   791  				return conv
   792  			},
   793  			SpecServiceFn: func() *automock.SpecService {
   794  				svc := &automock.SpecService{}
   795  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(nil, testErr).Once()
   796  				return svc
   797  			},
   798  			BundleReferenceServiceFn: func() *automock.BundleReferenceService {
   799  				return &automock.BundleReferenceService{}
   800  			},
   801  			InputWebhookID:          id,
   802  			InputEvent:              *gqlEventDefinitionInput,
   803  			ExpectedEventDefinition: nil,
   804  			ExpectedErr:             testErr,
   805  		},
   806  		{
   807  			Name:            "Returns error when BundlerReference retrieval failed",
   808  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   809  			ServiceFn: func() *automock.EventDefService {
   810  				svc := &automock.EventDefService{}
   811  				svc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, id, *modelEventDefinitionInput, modelSpecInput).Return(nil).Once()
   812  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
   813  				return svc
   814  			},
   815  			ConverterFn: func() *automock.EventDefConverter {
   816  				conv := &automock.EventDefConverter{}
   817  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
   818  				return conv
   819  			},
   820  			SpecServiceFn: func() *automock.SpecService {
   821  				svc := &automock.SpecService{}
   822  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(&modelSpec, nil).Once()
   823  				return svc
   824  			},
   825  			BundleReferenceServiceFn: func() *automock.BundleReferenceService {
   826  				svc := &automock.BundleReferenceService{}
   827  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &modelEventDefinition.ID, nilBundleID).Return(nil, testErr).Once()
   828  				return svc
   829  			},
   830  			InputWebhookID:          id,
   831  			InputEvent:              *gqlEventDefinitionInput,
   832  			ExpectedEventDefinition: nil,
   833  			ExpectedErr:             testErr,
   834  		},
   835  		{
   836  			Name:            "Returns error when converting to GraphQL failed",
   837  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   838  			ServiceFn: func() *automock.EventDefService {
   839  				svc := &automock.EventDefService{}
   840  				svc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, id, *modelEventDefinitionInput, modelSpecInput).Return(nil).Once()
   841  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
   842  				return svc
   843  			},
   844  			ConverterFn: func() *automock.EventDefConverter {
   845  				conv := &automock.EventDefConverter{}
   846  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
   847  				conv.On("ToGraphQL", &modelEventDefinition, &modelSpec, &modelBundleReference).Return(nil, testErr).Once()
   848  				return conv
   849  			},
   850  			SpecServiceFn: func() *automock.SpecService {
   851  				svc := &automock.SpecService{}
   852  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(&modelSpec, nil).Once()
   853  				return svc
   854  			},
   855  			BundleReferenceServiceFn: func() *automock.BundleReferenceService {
   856  				svc := &automock.BundleReferenceService{}
   857  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &modelEventDefinition.ID, nilBundleID).Return(&modelBundleReference, nil).Once()
   858  				return svc
   859  			},
   860  			InputWebhookID:          id,
   861  			InputEvent:              *gqlEventDefinitionInput,
   862  			ExpectedEventDefinition: nil,
   863  			ExpectedErr:             testErr,
   864  		},
   865  		{
   866  			Name:            "Returns error when commit transaction failed",
   867  			TransactionerFn: txGen.ThatFailsOnCommit,
   868  			ServiceFn: func() *automock.EventDefService {
   869  				svc := &automock.EventDefService{}
   870  				svc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, id, *modelEventDefinitionInput, modelSpecInput).Return(nil).Once()
   871  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
   872  				return svc
   873  			},
   874  			ConverterFn: func() *automock.EventDefConverter {
   875  				conv := &automock.EventDefConverter{}
   876  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
   877  				conv.On("ToGraphQL", &modelEventDefinition, &modelSpec, &modelBundleReference).Return(gqlEventDefinition, nil).Once()
   878  				return conv
   879  			},
   880  			SpecServiceFn: func() *automock.SpecService {
   881  				svc := &automock.SpecService{}
   882  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(&modelSpec, nil).Once()
   883  				return svc
   884  			},
   885  			BundleReferenceServiceFn: func() *automock.BundleReferenceService {
   886  				svc := &automock.BundleReferenceService{}
   887  				svc.On("GetForBundle", txtest.CtxWithDBMatcher(), model.BundleEventReference, &modelEventDefinition.ID, nilBundleID).Return(&modelBundleReference, nil).Once()
   888  				return svc
   889  			},
   890  			InputWebhookID:          id,
   891  			InputEvent:              *gqlEventDefinitionInput,
   892  			ExpectedEventDefinition: nil,
   893  			ExpectedErr:             testErr,
   894  		},
   895  	}
   896  
   897  	for _, testCase := range testCases {
   898  		t.Run(testCase.Name, func(t *testing.T) {
   899  			// GIVEN
   900  			persist, transact := testCase.TransactionerFn()
   901  			svc := testCase.ServiceFn()
   902  			converter := testCase.ConverterFn()
   903  			specService := testCase.SpecServiceFn()
   904  			bndlRefService := testCase.BundleReferenceServiceFn()
   905  
   906  			resolver := event.NewResolver(transact, svc, nil, bndlRefService, converter, nil, specService, nil)
   907  
   908  			// WHEN
   909  			result, err := resolver.UpdateEventDefinition(context.TODO(), id, *gqlEventDefinitionInput)
   910  
   911  			// then
   912  			assert.Equal(t, testCase.ExpectedEventDefinition, result)
   913  			if testCase.ExpectedErr != nil {
   914  				require.Error(t, err)
   915  				assert.Contains(t, err.Error(), testCase.ExpectedErr.Error())
   916  			} else {
   917  				require.Nil(t, err)
   918  			}
   919  
   920  			persist.AssertExpectations(t)
   921  			transact.AssertExpectations(t)
   922  			svc.AssertExpectations(t)
   923  			specService.AssertExpectations(t)
   924  			bndlRefService.AssertExpectations(t)
   925  			converter.AssertExpectations(t)
   926  		})
   927  	}
   928  }
   929  
   930  func TestResolver_RefetchEventSpec(t *testing.T) {
   931  	// GIVEN
   932  	testErr := errors.New("test error")
   933  
   934  	eventID := "eventID"
   935  	specID := "specID"
   936  
   937  	dataBytes := "data"
   938  	modelSpec := &model.Spec{
   939  		ID:   specID,
   940  		Data: &dataBytes,
   941  	}
   942  
   943  	clob := graphql.CLOB(dataBytes)
   944  	gqlEventSpec := &graphql.EventSpec{
   945  		Data: &clob,
   946  	}
   947  
   948  	txGen := txtest.NewTransactionContextGenerator(testErr)
   949  
   950  	testCases := []struct {
   951  		Name              string
   952  		TransactionerFn   func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
   953  		ServiceFn         func() *automock.SpecService
   954  		ConvFn            func() *automock.SpecConverter
   955  		ExpectedEventSpec *graphql.EventSpec
   956  		ExpectedErr       error
   957  	}{
   958  		{
   959  			Name:            "Success",
   960  			TransactionerFn: txGen.ThatSucceeds,
   961  			ServiceFn: func() *automock.SpecService {
   962  				svc := &automock.SpecService{}
   963  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, eventID).Return(modelSpec, nil).Once()
   964  				svc.On("RefetchSpec", txtest.CtxWithDBMatcher(), specID, model.EventSpecReference).Return(modelSpec, nil).Once()
   965  				return svc
   966  			},
   967  			ConvFn: func() *automock.SpecConverter {
   968  				conv := &automock.SpecConverter{}
   969  				conv.On("ToGraphQLEventSpec", modelSpec).Return(gqlEventSpec, nil).Once()
   970  				return conv
   971  			},
   972  			ExpectedEventSpec: gqlEventSpec,
   973  			ExpectedErr:       nil,
   974  		},
   975  		{
   976  			Name:            "Returns error when starting transaction",
   977  			TransactionerFn: txGen.ThatFailsOnBegin,
   978  			ServiceFn: func() *automock.SpecService {
   979  				return &automock.SpecService{}
   980  			},
   981  			ConvFn: func() *automock.SpecConverter {
   982  				return &automock.SpecConverter{}
   983  			},
   984  			ExpectedEventSpec: nil,
   985  			ExpectedErr:       testErr,
   986  		},
   987  		{
   988  			Name:            "Returns error when getting spec failed",
   989  			TransactionerFn: txGen.ThatDoesntExpectCommit,
   990  			ServiceFn: func() *automock.SpecService {
   991  				svc := &automock.SpecService{}
   992  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, eventID).Return(nil, testErr).Once()
   993  				return svc
   994  			},
   995  			ConvFn: func() *automock.SpecConverter {
   996  				return &automock.SpecConverter{}
   997  			},
   998  			ExpectedEventSpec: nil,
   999  			ExpectedErr:       testErr,
  1000  		},
  1001  		{
  1002  			Name:            "Returns error when spec not found",
  1003  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1004  			ServiceFn: func() *automock.SpecService {
  1005  				svc := &automock.SpecService{}
  1006  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, eventID).Return(nil, nil).Once()
  1007  				return svc
  1008  			},
  1009  			ConvFn: func() *automock.SpecConverter {
  1010  				return &automock.SpecConverter{}
  1011  			},
  1012  			ExpectedEventSpec: nil,
  1013  			ExpectedErr:       errors.Errorf("spec for Event with id %q not found", eventID),
  1014  		},
  1015  		{
  1016  			Name:            "Returns error when refetching event spec failed",
  1017  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1018  			ServiceFn: func() *automock.SpecService {
  1019  				svc := &automock.SpecService{}
  1020  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, eventID).Return(modelSpec, nil).Once()
  1021  				svc.On("RefetchSpec", txtest.CtxWithDBMatcher(), specID, model.EventSpecReference).Return(nil, testErr).Once()
  1022  				return svc
  1023  			},
  1024  			ConvFn: func() *automock.SpecConverter {
  1025  				return &automock.SpecConverter{}
  1026  			},
  1027  			ExpectedEventSpec: nil,
  1028  			ExpectedErr:       testErr,
  1029  		},
  1030  		{
  1031  			Name:            "Returns error converting to GraphQL fails",
  1032  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1033  			ServiceFn: func() *automock.SpecService {
  1034  				svc := &automock.SpecService{}
  1035  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, eventID).Return(modelSpec, nil).Once()
  1036  				svc.On("RefetchSpec", txtest.CtxWithDBMatcher(), specID, model.EventSpecReference).Return(modelSpec, nil).Once()
  1037  				return svc
  1038  			},
  1039  			ConvFn: func() *automock.SpecConverter {
  1040  				conv := &automock.SpecConverter{}
  1041  				conv.On("ToGraphQLEventSpec", modelSpec).Return(nil, testErr).Once()
  1042  				return conv
  1043  			},
  1044  			ExpectedEventSpec: nil,
  1045  			ExpectedErr:       testErr,
  1046  		},
  1047  		{
  1048  			Name:            "Returns error when commit transaction failed",
  1049  			TransactionerFn: txGen.ThatFailsOnCommit,
  1050  			ServiceFn: func() *automock.SpecService {
  1051  				svc := &automock.SpecService{}
  1052  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, eventID).Return(modelSpec, nil).Once()
  1053  				svc.On("RefetchSpec", txtest.CtxWithDBMatcher(), specID, model.EventSpecReference).Return(modelSpec, nil).Once()
  1054  				return svc
  1055  			},
  1056  			ConvFn: func() *automock.SpecConverter {
  1057  				conv := &automock.SpecConverter{}
  1058  				conv.On("ToGraphQLEventSpec", modelSpec).Return(gqlEventSpec, nil).Once()
  1059  				return conv
  1060  			},
  1061  			ExpectedEventSpec: nil,
  1062  			ExpectedErr:       testErr,
  1063  		},
  1064  	}
  1065  
  1066  	for _, testCase := range testCases {
  1067  		t.Run(testCase.Name, func(t *testing.T) {
  1068  			// GIVEN
  1069  			svc := testCase.ServiceFn()
  1070  			conv := testCase.ConvFn()
  1071  			persist, transact := testCase.TransactionerFn()
  1072  			resolver := event.NewResolver(transact, nil, nil, nil, nil, nil, svc, conv)
  1073  
  1074  			// WHEN
  1075  			result, err := resolver.RefetchEventDefinitionSpec(context.TODO(), eventID)
  1076  
  1077  			// then
  1078  			assert.Equal(t, testCase.ExpectedEventSpec, result)
  1079  			if testCase.ExpectedErr != nil {
  1080  				require.Error(t, err)
  1081  				assert.Contains(t, err.Error(), testCase.ExpectedErr.Error())
  1082  			} else {
  1083  				require.Nil(t, err)
  1084  			}
  1085  
  1086  			persist.AssertExpectations(t)
  1087  			transact.AssertExpectations(t)
  1088  			svc.AssertExpectations(t)
  1089  			conv.AssertExpectations(t)
  1090  		})
  1091  	}
  1092  }
  1093  
  1094  func TestResolver_FetchRequest(t *testing.T) {
  1095  	// GIVEN
  1096  	testErr := errors.New("Test error")
  1097  
  1098  	firstSpecID := "specID"
  1099  	secondSpecID := "specID2"
  1100  	specIDs := []string{firstSpecID, secondSpecID}
  1101  	firstFRID := "frID"
  1102  	secondFRID := "frID2"
  1103  	frURL := "foo.bar"
  1104  	timestamp := time.Now()
  1105  
  1106  	frFirstSpec := fixModelFetchRequest(firstFRID, frURL, timestamp)
  1107  	frSecondSpec := fixModelFetchRequest(secondFRID, frURL, timestamp)
  1108  	fetchRequests := []*model.FetchRequest{frFirstSpec, frSecondSpec}
  1109  
  1110  	gqlFRFirstSpec := fixGQLFetchRequest(frURL, timestamp)
  1111  	gqlFRSecondSpec := fixGQLFetchRequest(frURL, timestamp)
  1112  	gqlFetchRequests := []*graphql.FetchRequest{gqlFRFirstSpec, gqlFRSecondSpec}
  1113  
  1114  	txGen := txtest.NewTransactionContextGenerator(testErr)
  1115  
  1116  	testCases := []struct {
  1117  		Name            string
  1118  		TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
  1119  		ServiceFn       func() *automock.EventDefService
  1120  		ConverterFn     func() *automock.FetchRequestConverter
  1121  		ExpectedResult  []*graphql.FetchRequest
  1122  		ExpectedErr     []error
  1123  	}{
  1124  		{
  1125  			Name:            "Success",
  1126  			TransactionerFn: txGen.ThatSucceeds,
  1127  			ServiceFn: func() *automock.EventDefService {
  1128  				svc := &automock.EventDefService{}
  1129  				svc.On("ListFetchRequests", txtest.CtxWithDBMatcher(), specIDs).Return(fetchRequests, nil).Once()
  1130  				return svc
  1131  			},
  1132  			ConverterFn: func() *automock.FetchRequestConverter {
  1133  				conv := &automock.FetchRequestConverter{}
  1134  				conv.On("ToGraphQL", frFirstSpec).Return(gqlFRFirstSpec, nil).Once()
  1135  				conv.On("ToGraphQL", frSecondSpec).Return(gqlFRSecondSpec, nil).Once()
  1136  				return conv
  1137  			},
  1138  			ExpectedResult: gqlFetchRequests,
  1139  			ExpectedErr:    nil,
  1140  		},
  1141  		{
  1142  			Name:            "Returns error when starting transaction failed",
  1143  			TransactionerFn: txGen.ThatFailsOnBegin,
  1144  			ServiceFn: func() *automock.EventDefService {
  1145  				svc := &automock.EventDefService{}
  1146  				return svc
  1147  			},
  1148  			ConverterFn: func() *automock.FetchRequestConverter {
  1149  				conv := &automock.FetchRequestConverter{}
  1150  				return conv
  1151  			},
  1152  			ExpectedResult: nil,
  1153  			ExpectedErr:    []error{testErr},
  1154  		},
  1155  		{
  1156  			Name:            "FetchRequest doesn't exist",
  1157  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1158  			ServiceFn: func() *automock.EventDefService {
  1159  				svc := &automock.EventDefService{}
  1160  				svc.On("ListFetchRequests", txtest.CtxWithDBMatcher(), specIDs).Return(nil, nil).Once()
  1161  				return svc
  1162  			},
  1163  			ConverterFn: func() *automock.FetchRequestConverter {
  1164  				conv := &automock.FetchRequestConverter{}
  1165  				return conv
  1166  			},
  1167  			ExpectedResult: nil,
  1168  			ExpectedErr:    nil,
  1169  		},
  1170  		{
  1171  			Name:            "Error when listing Event FetchRequests",
  1172  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1173  			ServiceFn: func() *automock.EventDefService {
  1174  				svc := &automock.EventDefService{}
  1175  				svc.On("ListFetchRequests", txtest.CtxWithDBMatcher(), specIDs).Return(nil, testErr).Once()
  1176  				return svc
  1177  			},
  1178  			ConverterFn: func() *automock.FetchRequestConverter {
  1179  				conv := &automock.FetchRequestConverter{}
  1180  				return conv
  1181  			},
  1182  			ExpectedResult: nil,
  1183  			ExpectedErr:    []error{testErr},
  1184  		},
  1185  		{
  1186  			Name:            "Error when converting FetchRequest to graphql",
  1187  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1188  			ServiceFn: func() *automock.EventDefService {
  1189  				svc := &automock.EventDefService{}
  1190  				svc.On("ListFetchRequests", txtest.CtxWithDBMatcher(), specIDs).Return(fetchRequests, nil).Once()
  1191  				return svc
  1192  			},
  1193  			ConverterFn: func() *automock.FetchRequestConverter {
  1194  				conv := &automock.FetchRequestConverter{}
  1195  				conv.On("ToGraphQL", frFirstSpec).Return(nil, testErr).Once()
  1196  				return conv
  1197  			},
  1198  			ExpectedResult: nil,
  1199  			ExpectedErr:    []error{testErr},
  1200  		},
  1201  		{
  1202  			Name:            "Returns error when commit transaction fails",
  1203  			TransactionerFn: txGen.ThatFailsOnCommit,
  1204  			ServiceFn: func() *automock.EventDefService {
  1205  				svc := &automock.EventDefService{}
  1206  				svc.On("ListFetchRequests", txtest.CtxWithDBMatcher(), specIDs).Return(fetchRequests, nil).Once()
  1207  				return svc
  1208  			},
  1209  			ConverterFn: func() *automock.FetchRequestConverter {
  1210  				conv := &automock.FetchRequestConverter{}
  1211  				conv.On("ToGraphQL", frFirstSpec).Return(gqlFRFirstSpec, nil).Once()
  1212  				conv.On("ToGraphQL", frSecondSpec).Return(gqlFRSecondSpec, nil).Once()
  1213  				return conv
  1214  			},
  1215  			ExpectedResult: nil,
  1216  			ExpectedErr:    []error{testErr},
  1217  		},
  1218  	}
  1219  
  1220  	for _, testCase := range testCases {
  1221  		t.Run(testCase.Name, func(t *testing.T) {
  1222  			persist, transact := testCase.TransactionerFn()
  1223  			svc := testCase.ServiceFn()
  1224  			converter := testCase.ConverterFn()
  1225  
  1226  			firstFRParams := dataloader.ParamFetchRequestEventDef{ID: firstSpecID, Ctx: context.TODO()}
  1227  			secondFRParams := dataloader.ParamFetchRequestEventDef{ID: secondSpecID, Ctx: context.TODO()}
  1228  			keys := []dataloader.ParamFetchRequestEventDef{firstFRParams, secondFRParams}
  1229  			resolver := event.NewResolver(transact, svc, nil, nil, nil, converter, nil, nil)
  1230  
  1231  			// WHEN
  1232  			result, err := resolver.FetchRequestEventDefDataLoader(keys)
  1233  
  1234  			// then
  1235  			assert.Equal(t, testCase.ExpectedResult, result)
  1236  			assert.Equal(t, testCase.ExpectedErr, err)
  1237  
  1238  			svc.AssertExpectations(t)
  1239  			converter.AssertExpectations(t)
  1240  			persist.AssertExpectations(t)
  1241  			transact.AssertExpectations(t)
  1242  		})
  1243  	}
  1244  	t.Run("Returns error when there are no Specs", func(t *testing.T) {
  1245  		resolver := event.NewResolver(nil, nil, nil, nil, nil, nil, nil, nil)
  1246  		// WHEN
  1247  		_, err := resolver.FetchRequestEventDefDataLoader([]dataloader.ParamFetchRequestEventDef{})
  1248  		// THEN
  1249  		require.Error(t, err[0])
  1250  		assert.EqualError(t, err[0], apperrors.NewInternalError("No EventDef specs found").Error())
  1251  	})
  1252  
  1253  	t.Run("Returns error when Specification ID is empty", func(t *testing.T) {
  1254  		params := dataloader.ParamFetchRequestEventDef{ID: "", Ctx: context.TODO()}
  1255  		keys := []dataloader.ParamFetchRequestEventDef{params}
  1256  
  1257  		resolver := event.NewResolver(nil, nil, nil, nil, nil, nil, nil, nil)
  1258  		// WHEN
  1259  		_, err := resolver.FetchRequestEventDefDataLoader(keys)
  1260  		// THEN
  1261  		require.Error(t, err[0])
  1262  		assert.EqualError(t, err[0], apperrors.NewInternalError("Cannot fetch FetchRequest. EventDefinition Spec ID is empty").Error())
  1263  	})
  1264  }
  1265  
  1266  func TestResolver_EventDefinitionsForApplication(t *testing.T) {
  1267  	// GIVEN
  1268  	testErr := errors.New("Test error")
  1269  
  1270  	pageSize := 100
  1271  	cursor := ""
  1272  	gqlCursor := graphql.PageCursor(cursor)
  1273  
  1274  	modelEvent, spec, _ := fixFullEventDefinitionModel("test")
  1275  	gqlEvent := fixFullGQLEventDefinition("test")
  1276  
  1277  	modelPage := &model.EventDefinitionPage{
  1278  		Data: []*model.EventDefinition{&modelEvent},
  1279  		PageInfo: &pagination.Page{
  1280  			StartCursor: "",
  1281  			EndCursor:   "",
  1282  			HasNextPage: false,
  1283  		},
  1284  		TotalCount: 1,
  1285  	}
  1286  	gqlPage := &graphql.EventDefinitionPage{
  1287  		Data: []*graphql.EventDefinition{gqlEvent},
  1288  		PageInfo: &graphql.PageInfo{
  1289  			StartCursor: "",
  1290  			EndCursor:   "",
  1291  			HasNextPage: false,
  1292  		},
  1293  		TotalCount: 1,
  1294  	}
  1295  	var bundleRef *model.BundleReference
  1296  	txGen := txtest.NewTransactionContextGenerator(testErr)
  1297  
  1298  	testCases := []struct {
  1299  		Name            string
  1300  		TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
  1301  		ServiceFn       func() *automock.EventDefService
  1302  		SpecServiceFn   func() *automock.SpecService
  1303  		ConverterFn     func() *automock.EventDefConverter
  1304  		PageSize        *int
  1305  		ExpectedAPI     *graphql.EventDefinitionPage
  1306  		ExpectedErr     error
  1307  	}{
  1308  		{
  1309  			Name:            "Success",
  1310  			TransactionerFn: txGen.ThatSucceeds,
  1311  			ServiceFn: func() *automock.EventDefService {
  1312  				svc := &automock.EventDefService{}
  1313  				svc.On("ListByApplicationIDPage", txtest.CtxWithDBMatcher(), appID, pageSize, cursor).Return(modelPage, nil).Once()
  1314  				return svc
  1315  			},
  1316  			SpecServiceFn: func() *automock.SpecService {
  1317  				svc := &automock.SpecService{}
  1318  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(&spec, nil).Once()
  1319  				return svc
  1320  			},
  1321  			ConverterFn: func() *automock.EventDefConverter {
  1322  				conv := &automock.EventDefConverter{}
  1323  				conv.On("ToGraphQL", &modelEvent, &spec, bundleRef).Return(gqlEvent, nil).Once()
  1324  				return conv
  1325  			},
  1326  			PageSize:    &pageSize,
  1327  			ExpectedAPI: gqlPage,
  1328  		},
  1329  		{
  1330  			Name:            "Error when page size is missing",
  1331  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1332  			ServiceFn: func() *automock.EventDefService {
  1333  				svc := &automock.EventDefService{}
  1334  				return svc
  1335  			},
  1336  			SpecServiceFn: func() *automock.SpecService {
  1337  				svc := &automock.SpecService{}
  1338  				return svc
  1339  			},
  1340  			ConverterFn: func() *automock.EventDefConverter {
  1341  				conv := &automock.EventDefConverter{}
  1342  				return conv
  1343  			},
  1344  			ExpectedErr: errors.New("missing required parameter 'first'"),
  1345  		},
  1346  		{
  1347  			Name:            "Error when starting transaction fails",
  1348  			TransactionerFn: txGen.ThatFailsOnBegin,
  1349  			ServiceFn: func() *automock.EventDefService {
  1350  				svc := &automock.EventDefService{}
  1351  				return svc
  1352  			},
  1353  			SpecServiceFn: func() *automock.SpecService {
  1354  				svc := &automock.SpecService{}
  1355  				return svc
  1356  			},
  1357  			ConverterFn: func() *automock.EventDefConverter {
  1358  				conv := &automock.EventDefConverter{}
  1359  				return conv
  1360  			},
  1361  			ExpectedErr: testErr,
  1362  		},
  1363  		{
  1364  			Name:            "Error when listing page fails",
  1365  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1366  			ServiceFn: func() *automock.EventDefService {
  1367  				svc := &automock.EventDefService{}
  1368  				svc.On("ListByApplicationIDPage", txtest.CtxWithDBMatcher(), appID, pageSize, cursor).Return(nil, testErr).Once()
  1369  				return svc
  1370  			},
  1371  			SpecServiceFn: func() *automock.SpecService {
  1372  				svc := &automock.SpecService{}
  1373  				return svc
  1374  			},
  1375  			ConverterFn: func() *automock.EventDefConverter {
  1376  				conv := &automock.EventDefConverter{}
  1377  				return conv
  1378  			},
  1379  			PageSize:    &pageSize,
  1380  			ExpectedErr: testErr,
  1381  		},
  1382  		{
  1383  			Name:            "Error when getting spec fails",
  1384  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1385  			ServiceFn: func() *automock.EventDefService {
  1386  				svc := &automock.EventDefService{}
  1387  				svc.On("ListByApplicationIDPage", txtest.CtxWithDBMatcher(), appID, pageSize, cursor).Return(modelPage, nil).Once()
  1388  				return svc
  1389  			},
  1390  			SpecServiceFn: func() *automock.SpecService {
  1391  				svc := &automock.SpecService{}
  1392  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(nil, testErr).Once()
  1393  				return svc
  1394  			},
  1395  			ConverterFn: func() *automock.EventDefConverter {
  1396  				conv := &automock.EventDefConverter{}
  1397  				return conv
  1398  			},
  1399  			PageSize:    &pageSize,
  1400  			ExpectedErr: testErr,
  1401  		},
  1402  		{
  1403  			Name:            "Error when graphql conversion fails",
  1404  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1405  			ServiceFn: func() *automock.EventDefService {
  1406  				svc := &automock.EventDefService{}
  1407  				svc.On("ListByApplicationIDPage", txtest.CtxWithDBMatcher(), appID, pageSize, cursor).Return(modelPage, nil).Once()
  1408  				return svc
  1409  			},
  1410  			SpecServiceFn: func() *automock.SpecService {
  1411  				svc := &automock.SpecService{}
  1412  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(&spec, nil).Once()
  1413  				return svc
  1414  			},
  1415  			ConverterFn: func() *automock.EventDefConverter {
  1416  				conv := &automock.EventDefConverter{}
  1417  				conv.On("ToGraphQL", &modelEvent, &spec, bundleRef).Return(nil, testErr).Once()
  1418  				return conv
  1419  			},
  1420  			PageSize:    &pageSize,
  1421  			ExpectedErr: testErr,
  1422  		},
  1423  		{
  1424  			Name:            "Error when committing transaction fails",
  1425  			TransactionerFn: txGen.ThatFailsOnCommit,
  1426  			ServiceFn: func() *automock.EventDefService {
  1427  				svc := &automock.EventDefService{}
  1428  				svc.On("ListByApplicationIDPage", txtest.CtxWithDBMatcher(), appID, pageSize, cursor).Return(modelPage, nil).Once()
  1429  				return svc
  1430  			},
  1431  			SpecServiceFn: func() *automock.SpecService {
  1432  				svc := &automock.SpecService{}
  1433  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(&spec, nil).Once()
  1434  				return svc
  1435  			},
  1436  			ConverterFn: func() *automock.EventDefConverter {
  1437  				conv := &automock.EventDefConverter{}
  1438  				conv.On("ToGraphQL", &modelEvent, &spec, bundleRef).Return(gqlEvent, nil).Once()
  1439  				return conv
  1440  			},
  1441  			PageSize:    &pageSize,
  1442  			ExpectedErr: testErr,
  1443  		},
  1444  	}
  1445  
  1446  	for _, testCase := range testCases {
  1447  		t.Run(testCase.Name, func(t *testing.T) {
  1448  			// GIVEN
  1449  			persist, transact := testCase.TransactionerFn()
  1450  			svc := testCase.ServiceFn()
  1451  			converter := testCase.ConverterFn()
  1452  			specSvc := testCase.SpecServiceFn()
  1453  			defer mock.AssertExpectationsForObjects(t, persist, transact, svc, specSvc, converter)
  1454  
  1455  			resolver := event.NewResolver(transact, svc, nil, nil, converter, nil, specSvc, nil)
  1456  
  1457  			// WHEN
  1458  			result, err := resolver.EventDefinitionsForApplication(context.TODO(), appID, testCase.PageSize, &gqlCursor)
  1459  
  1460  			// THEN
  1461  			assert.Equal(t, testCase.ExpectedAPI, result)
  1462  			if testCase.ExpectedErr != nil {
  1463  				require.Error(t, err)
  1464  				assert.Contains(t, err.Error(), testCase.ExpectedErr.Error())
  1465  			} else {
  1466  				require.Nil(t, err)
  1467  			}
  1468  		})
  1469  	}
  1470  }
  1471  
  1472  func TestResolver_AddEventDefinitionToApplication(t *testing.T) {
  1473  	// GIVEN
  1474  	testErr := errors.New("Test error")
  1475  
  1476  	id := "bar"
  1477  
  1478  	modelEvent, spec, _ := fixFullEventDefinitionModel("test")
  1479  	var bundleRef *model.BundleReference
  1480  
  1481  	gqlEvent := fixFullGQLEventDefinition("test")
  1482  	gqlEventInput := fixGQLEventDefinitionInput("name", "foo", "bar")
  1483  	modelAPIInput, specInput := fixModelEventDefinitionInput("name", "foo", "bar")
  1484  	txGen := txtest.NewTransactionContextGenerator(testErr)
  1485  
  1486  	testCases := []struct {
  1487  		Name            string
  1488  		TransactionerFn func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
  1489  		ServiceFn       func() *automock.EventDefService
  1490  		SpecServiceFn   func() *automock.SpecService
  1491  		ConverterFn     func() *automock.EventDefConverter
  1492  		ExpectedAPI     *graphql.EventDefinition
  1493  		ExpectedErr     error
  1494  	}{
  1495  		{
  1496  			Name:            "Success",
  1497  			TransactionerFn: txGen.ThatSucceeds,
  1498  			ServiceFn: func() *automock.EventDefService {
  1499  				svc := &automock.EventDefService{}
  1500  				svc.On("CreateInApplication", txtest.CtxWithDBMatcher(), appID, *modelAPIInput, specInput).Return(id, nil).Once()
  1501  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEvent, nil).Once()
  1502  				return svc
  1503  			},
  1504  			SpecServiceFn: func() *automock.SpecService {
  1505  				svc := &automock.SpecService{}
  1506  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(&spec, nil).Once()
  1507  				return svc
  1508  			},
  1509  			ConverterFn: func() *automock.EventDefConverter {
  1510  				conv := &automock.EventDefConverter{}
  1511  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelAPIInput, specInput, nil).Once()
  1512  				conv.On("ToGraphQL", &modelEvent, &spec, bundleRef).Return(gqlEvent, nil).Once()
  1513  				return conv
  1514  			},
  1515  			ExpectedAPI: gqlEvent,
  1516  			ExpectedErr: nil,
  1517  		},
  1518  		{
  1519  			Name:            "Error when starting transaction",
  1520  			TransactionerFn: txGen.ThatFailsOnBegin,
  1521  			ServiceFn: func() *automock.EventDefService {
  1522  				svc := &automock.EventDefService{}
  1523  				return svc
  1524  			},
  1525  			SpecServiceFn: emptySpecSvc,
  1526  			ConverterFn: func() *automock.EventDefConverter {
  1527  				conv := &automock.EventDefConverter{}
  1528  				return conv
  1529  			},
  1530  			ExpectedErr: testErr,
  1531  		},
  1532  		{
  1533  			Name:            "Error when converting into model fails",
  1534  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1535  			ServiceFn: func() *automock.EventDefService {
  1536  				svc := &automock.EventDefService{}
  1537  				return svc
  1538  			},
  1539  			SpecServiceFn: emptySpecSvc,
  1540  			ConverterFn: func() *automock.EventDefConverter {
  1541  				conv := &automock.EventDefConverter{}
  1542  				conv.On("InputFromGraphQL", gqlEventInput).Return(nil, nil, testErr).Once()
  1543  				return conv
  1544  			},
  1545  			ExpectedErr: testErr,
  1546  		},
  1547  		{
  1548  			Name:            "Error when creating Event fails",
  1549  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1550  			ServiceFn: func() *automock.EventDefService {
  1551  				svc := &automock.EventDefService{}
  1552  				svc.On("CreateInApplication", txtest.CtxWithDBMatcher(), appID, *modelAPIInput, specInput).Return("", testErr).Once()
  1553  				return svc
  1554  			},
  1555  			SpecServiceFn: emptySpecSvc,
  1556  			ConverterFn: func() *automock.EventDefConverter {
  1557  				conv := &automock.EventDefConverter{}
  1558  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelAPIInput, specInput, nil).Once()
  1559  				return conv
  1560  			},
  1561  			ExpectedErr: testErr,
  1562  		},
  1563  		{
  1564  			Name:            "Error when getting the Event fails",
  1565  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1566  			ServiceFn: func() *automock.EventDefService {
  1567  				svc := &automock.EventDefService{}
  1568  				svc.On("CreateInApplication", txtest.CtxWithDBMatcher(), appID, *modelAPIInput, specInput).Return(id, nil).Once()
  1569  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(nil, testErr).Once()
  1570  				return svc
  1571  			},
  1572  			SpecServiceFn: emptySpecSvc,
  1573  			ConverterFn: func() *automock.EventDefConverter {
  1574  				conv := &automock.EventDefConverter{}
  1575  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelAPIInput, specInput, nil).Once()
  1576  				return conv
  1577  			},
  1578  			ExpectedErr: testErr,
  1579  		},
  1580  		{
  1581  			Name:            "Error when getting the spec fails",
  1582  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1583  			ServiceFn: func() *automock.EventDefService {
  1584  				svc := &automock.EventDefService{}
  1585  				svc.On("CreateInApplication", txtest.CtxWithDBMatcher(), appID, *modelAPIInput, specInput).Return(id, nil).Once()
  1586  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEvent, nil).Once()
  1587  				return svc
  1588  			},
  1589  			SpecServiceFn: func() *automock.SpecService {
  1590  				svc := &automock.SpecService{}
  1591  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(nil, testErr).Once()
  1592  				return svc
  1593  			},
  1594  			ConverterFn: func() *automock.EventDefConverter {
  1595  				conv := &automock.EventDefConverter{}
  1596  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelAPIInput, specInput, nil).Once()
  1597  				return conv
  1598  			},
  1599  			ExpectedErr: testErr,
  1600  		},
  1601  		{
  1602  			Name:            "Fail when converting to graphQL fails",
  1603  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1604  			ServiceFn: func() *automock.EventDefService {
  1605  				svc := &automock.EventDefService{}
  1606  				svc.On("CreateInApplication", txtest.CtxWithDBMatcher(), appID, *modelAPIInput, specInput).Return(id, nil).Once()
  1607  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEvent, nil).Once()
  1608  				return svc
  1609  			},
  1610  			SpecServiceFn: func() *automock.SpecService {
  1611  				svc := &automock.SpecService{}
  1612  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(&spec, nil).Once()
  1613  				return svc
  1614  			},
  1615  			ConverterFn: func() *automock.EventDefConverter {
  1616  				conv := &automock.EventDefConverter{}
  1617  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelAPIInput, specInput, nil).Once()
  1618  				conv.On("ToGraphQL", &modelEvent, &spec, bundleRef).Return(nil, testErr).Once()
  1619  				return conv
  1620  			},
  1621  			ExpectedErr: testErr,
  1622  		},
  1623  		{
  1624  			Name:            "Error when committing fails",
  1625  			TransactionerFn: txGen.ThatFailsOnCommit,
  1626  			ServiceFn: func() *automock.EventDefService {
  1627  				svc := &automock.EventDefService{}
  1628  				svc.On("CreateInApplication", txtest.CtxWithDBMatcher(), appID, *modelAPIInput, specInput).Return(id, nil).Once()
  1629  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEvent, nil).Once()
  1630  				return svc
  1631  			},
  1632  			SpecServiceFn: func() *automock.SpecService {
  1633  				svc := &automock.SpecService{}
  1634  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, gqlEvent.ID).Return(&spec, nil).Once()
  1635  				return svc
  1636  			},
  1637  			ConverterFn: func() *automock.EventDefConverter {
  1638  				conv := &automock.EventDefConverter{}
  1639  				conv.On("InputFromGraphQL", gqlEventInput).Return(modelAPIInput, specInput, nil).Once()
  1640  				conv.On("ToGraphQL", &modelEvent, &spec, bundleRef).Return(gqlEvent, nil).Once()
  1641  				return conv
  1642  			},
  1643  			ExpectedErr: testErr,
  1644  		},
  1645  	}
  1646  
  1647  	for _, testCase := range testCases {
  1648  		t.Run(testCase.Name, func(t *testing.T) {
  1649  			// GIVEN
  1650  			persist, transact := testCase.TransactionerFn()
  1651  			svc := testCase.ServiceFn()
  1652  			converter := testCase.ConverterFn()
  1653  			specSvc := testCase.SpecServiceFn()
  1654  			defer mock.AssertExpectationsForObjects(t, persist, transact, svc, specSvc, converter)
  1655  
  1656  			resolver := event.NewResolver(transact, svc, nil, nil, converter, nil, specSvc, nil)
  1657  
  1658  			// WHEN
  1659  			result, err := resolver.AddEventDefinitionToApplication(context.TODO(), appID, *gqlEventInput)
  1660  
  1661  			// THEN
  1662  			assert.Equal(t, testCase.ExpectedAPI, result)
  1663  			if testCase.ExpectedErr != nil {
  1664  				require.Error(t, err)
  1665  				assert.Contains(t, err.Error(), testCase.ExpectedErr.Error())
  1666  			} else {
  1667  				require.Nil(t, err)
  1668  			}
  1669  		})
  1670  	}
  1671  }
  1672  
  1673  func TestResolver_UpdateEventDefinitionForApplication(t *testing.T) {
  1674  	// GIVEN
  1675  	testErr := errors.New("Test error")
  1676  
  1677  	id := "bar"
  1678  	gqlEventDefinitionInput := fixGQLEventDefinitionInput(id, "foo", "bar")
  1679  	modelEventDefinitionInput, modelSpecInput := fixModelEventDefinitionInput(id, "foo", "bar")
  1680  	gqlEventDefinition := fixFullGQLEventDefinition("test")
  1681  	modelEventDefinition, modelSpec, _ := fixFullEventDefinitionModel("test")
  1682  	var bundleRef *model.BundleReference
  1683  
  1684  	txGen := txtest.NewTransactionContextGenerator(testErr)
  1685  
  1686  	testCases := []struct {
  1687  		Name                  string
  1688  		TransactionerFn       func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
  1689  		ServiceFn             func() *automock.EventDefService
  1690  		ConverterFn           func() *automock.EventDefConverter
  1691  		SpecServiceFn         func() *automock.SpecService
  1692  		InputAPI              graphql.EventDefinitionInput
  1693  		ExpectedAPIDefinition *graphql.EventDefinition
  1694  		ExpectedErr           error
  1695  	}{
  1696  		{
  1697  			Name:            "Success",
  1698  			TransactionerFn: txGen.ThatSucceeds,
  1699  			ServiceFn: func() *automock.EventDefService {
  1700  				svc := &automock.EventDefService{}
  1701  				svc.On("UpdateForApplication", txtest.CtxWithDBMatcher(), id, *modelEventDefinitionInput, modelSpecInput).Return(nil).Once()
  1702  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
  1703  				return svc
  1704  			},
  1705  			ConverterFn: func() *automock.EventDefConverter {
  1706  				conv := &automock.EventDefConverter{}
  1707  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
  1708  				conv.On("ToGraphQL", &modelEventDefinition, &modelSpec, bundleRef).Return(gqlEventDefinition, nil).Once()
  1709  				return conv
  1710  			},
  1711  			SpecServiceFn: func() *automock.SpecService {
  1712  				svc := &automock.SpecService{}
  1713  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(&modelSpec, nil).Once()
  1714  				return svc
  1715  			},
  1716  			InputAPI:              *gqlEventDefinitionInput,
  1717  			ExpectedAPIDefinition: gqlEventDefinition,
  1718  			ExpectedErr:           nil,
  1719  		},
  1720  		{
  1721  			Name:            "Error when starting transaction fails",
  1722  			TransactionerFn: txGen.ThatFailsOnBegin,
  1723  			ServiceFn: func() *automock.EventDefService {
  1724  				svc := &automock.EventDefService{}
  1725  				return svc
  1726  			},
  1727  			ConverterFn: func() *automock.EventDefConverter {
  1728  				conv := &automock.EventDefConverter{}
  1729  				return conv
  1730  			},
  1731  			SpecServiceFn: emptySpecSvc,
  1732  			InputAPI:      *gqlEventDefinitionInput,
  1733  			ExpectedErr:   testErr,
  1734  		},
  1735  		{
  1736  			Name:            "Error when converting input from graphQL fails",
  1737  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1738  			ServiceFn: func() *automock.EventDefService {
  1739  				svc := &automock.EventDefService{}
  1740  				return svc
  1741  			},
  1742  			ConverterFn: func() *automock.EventDefConverter {
  1743  				conv := &automock.EventDefConverter{}
  1744  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(nil, nil, testErr).Once()
  1745  				return conv
  1746  			},
  1747  			SpecServiceFn: emptySpecSvc,
  1748  			InputAPI:      *gqlEventDefinitionInput,
  1749  			ExpectedErr:   testErr,
  1750  		},
  1751  		{
  1752  			Name:            "Error when  updating Event fails",
  1753  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1754  			ServiceFn: func() *automock.EventDefService {
  1755  				svc := &automock.EventDefService{}
  1756  				svc.On("UpdateForApplication", txtest.CtxWithDBMatcher(), id, *modelEventDefinitionInput, modelSpecInput).Return(testErr).Once()
  1757  				return svc
  1758  			},
  1759  			ConverterFn: func() *automock.EventDefConverter {
  1760  				conv := &automock.EventDefConverter{}
  1761  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
  1762  				return conv
  1763  			},
  1764  			SpecServiceFn: emptySpecSvc,
  1765  			InputAPI:      *gqlEventDefinitionInput,
  1766  			ExpectedErr:   testErr,
  1767  		},
  1768  		{
  1769  			Name:            "Error when getting Event fails",
  1770  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1771  			ServiceFn: func() *automock.EventDefService {
  1772  				svc := &automock.EventDefService{}
  1773  				svc.On("UpdateForApplication", txtest.CtxWithDBMatcher(), id, *modelEventDefinitionInput, modelSpecInput).Return(nil).Once()
  1774  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(nil, testErr).Once()
  1775  				return svc
  1776  			},
  1777  			ConverterFn: func() *automock.EventDefConverter {
  1778  				conv := &automock.EventDefConverter{}
  1779  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
  1780  				return conv
  1781  			},
  1782  			SpecServiceFn: emptySpecSvc,
  1783  			InputAPI:      *gqlEventDefinitionInput,
  1784  			ExpectedErr:   testErr,
  1785  		},
  1786  		{
  1787  			Name:            "Error when getting Spec fails",
  1788  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1789  			ServiceFn: func() *automock.EventDefService {
  1790  				svc := &automock.EventDefService{}
  1791  				svc.On("UpdateForApplication", txtest.CtxWithDBMatcher(), id, *modelEventDefinitionInput, modelSpecInput).Return(nil).Once()
  1792  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
  1793  				return svc
  1794  			},
  1795  			ConverterFn: func() *automock.EventDefConverter {
  1796  				conv := &automock.EventDefConverter{}
  1797  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
  1798  				return conv
  1799  			},
  1800  			SpecServiceFn: func() *automock.SpecService {
  1801  				svc := &automock.SpecService{}
  1802  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(nil, testErr).Once()
  1803  				return svc
  1804  			},
  1805  			InputAPI:    *gqlEventDefinitionInput,
  1806  			ExpectedErr: testErr,
  1807  		},
  1808  		{
  1809  			Name:            "Error when converting to graphQL fails",
  1810  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1811  			ServiceFn: func() *automock.EventDefService {
  1812  				svc := &automock.EventDefService{}
  1813  				svc.On("UpdateForApplication", txtest.CtxWithDBMatcher(), id, *modelEventDefinitionInput, modelSpecInput).Return(nil).Once()
  1814  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
  1815  				return svc
  1816  			},
  1817  			ConverterFn: func() *automock.EventDefConverter {
  1818  				conv := &automock.EventDefConverter{}
  1819  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
  1820  				conv.On("ToGraphQL", &modelEventDefinition, &modelSpec, bundleRef).Return(nil, testErr).Once()
  1821  				return conv
  1822  			},
  1823  			SpecServiceFn: func() *automock.SpecService {
  1824  				svc := &automock.SpecService{}
  1825  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(&modelSpec, nil).Once()
  1826  				return svc
  1827  			},
  1828  			InputAPI:    *gqlEventDefinitionInput,
  1829  			ExpectedErr: testErr,
  1830  		},
  1831  		{
  1832  			Name:            "Error when committing transaction fails",
  1833  			TransactionerFn: txGen.ThatFailsOnCommit,
  1834  			ServiceFn: func() *automock.EventDefService {
  1835  				svc := &automock.EventDefService{}
  1836  				svc.On("UpdateForApplication", txtest.CtxWithDBMatcher(), id, *modelEventDefinitionInput, modelSpecInput).Return(nil).Once()
  1837  				svc.On("Get", txtest.CtxWithDBMatcher(), id).Return(&modelEventDefinition, nil).Once()
  1838  				return svc
  1839  			},
  1840  			ConverterFn: func() *automock.EventDefConverter {
  1841  				conv := &automock.EventDefConverter{}
  1842  				conv.On("InputFromGraphQL", gqlEventDefinitionInput).Return(modelEventDefinitionInput, modelSpecInput, nil).Once()
  1843  				conv.On("ToGraphQL", &modelEventDefinition, &modelSpec, bundleRef).Return(gqlEventDefinition, nil).Once()
  1844  				return conv
  1845  			},
  1846  			SpecServiceFn: func() *automock.SpecService {
  1847  				svc := &automock.SpecService{}
  1848  				svc.On("GetByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, modelEventDefinition.ID).Return(&modelSpec, nil).Once()
  1849  				return svc
  1850  			},
  1851  			InputAPI:    *gqlEventDefinitionInput,
  1852  			ExpectedErr: testErr,
  1853  		},
  1854  	}
  1855  
  1856  	for _, testCase := range testCases {
  1857  		t.Run(testCase.Name, func(t *testing.T) {
  1858  			// GIVEN
  1859  			persist, transact := testCase.TransactionerFn()
  1860  			svc := testCase.ServiceFn()
  1861  			converter := testCase.ConverterFn()
  1862  			specService := testCase.SpecServiceFn()
  1863  			defer mock.AssertExpectationsForObjects(t, persist, transact, svc, specService, converter)
  1864  
  1865  			resolver := event.NewResolver(transact, svc, nil, nil, converter, nil, specService, nil)
  1866  
  1867  			// WHEN
  1868  			result, err := resolver.UpdateEventDefinitionForApplication(context.TODO(), id, *gqlEventDefinitionInput)
  1869  
  1870  			// THEN
  1871  			if testCase.ExpectedErr != nil {
  1872  				require.Error(t, err)
  1873  				assert.Contains(t, err.Error(), testCase.ExpectedErr.Error())
  1874  			} else {
  1875  				require.Nil(t, err)
  1876  				assert.Equal(t, testCase.ExpectedAPIDefinition, result)
  1877  			}
  1878  		})
  1879  	}
  1880  }
  1881  
  1882  func emptySpecSvc() *automock.SpecService {
  1883  	svc := &automock.SpecService{}
  1884  	return svc
  1885  }