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

     1  package ord_test
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  	"testing"
     8  
     9  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    10  
    11  	"github.com/kyma-incubator/compass/components/director/pkg/resource"
    12  
    13  	"github.com/kyma-incubator/compass/components/director/internal/model"
    14  	ord "github.com/kyma-incubator/compass/components/director/internal/open_resource_discovery"
    15  	"github.com/kyma-incubator/compass/components/director/internal/open_resource_discovery/automock"
    16  	persistenceautomock "github.com/kyma-incubator/compass/components/director/pkg/persistence/automock"
    17  	"github.com/kyma-incubator/compass/components/director/pkg/persistence/txtest"
    18  	"github.com/kyma-incubator/compass/components/director/pkg/str"
    19  	"github.com/pkg/errors"
    20  	"github.com/stretchr/testify/mock"
    21  	"github.com/stretchr/testify/require"
    22  )
    23  
    24  const (
    25  	testApplicationType = "testApplicationType"
    26  )
    27  
    28  func TestService_SyncORDDocuments(t *testing.T) {
    29  	testErr := errors.New("Test error")
    30  	txGen := txtest.NewTransactionContextGenerator(testErr)
    31  
    32  	sanitizedDoc := fixSanitizedORDDocument()
    33  	sanitizedStaticDoc := fixSanitizedStaticORDDocument()
    34  	var testSpecData = "{}"
    35  	var testSpec = model.Spec{}
    36  	var nilSpecInput *model.SpecInput
    37  	var nilBundleID *string
    38  
    39  	testApplication := fixApplications()[0]
    40  	testResource := ord.Resource{
    41  		Type:          resource.Application,
    42  		ID:            testApplication.ID,
    43  		Name:          testApplication.Name,
    44  		LocalTenantID: testApplication.LocalTenantID,
    45  		ParentID:      &appTemplateID,
    46  	}
    47  	testResourceForAppTemplate := ord.Resource{
    48  		Type: resource.ApplicationTemplate,
    49  		ID:   appTemplateID,
    50  		Name: appTemplateName,
    51  	}
    52  	testWebhookForApplication := fixWebhooksForApplication()[0]
    53  	testWebhookForAppTemplate := fixOrdWebhooksForAppTemplate()[0]
    54  
    55  	api1PreSanitizedHash, err := ord.HashObject(fixORDDocument().APIResources[0])
    56  	require.NoError(t, err)
    57  
    58  	api2PreSanitizedHash, err := ord.HashObject(fixORDDocument().APIResources[1])
    59  	require.NoError(t, err)
    60  
    61  	event1PreSanitizedHash, err := ord.HashObject(fixORDDocument().EventResources[0])
    62  	require.NoError(t, err)
    63  
    64  	event2PreSanitizedHash, err := ord.HashObject(fixORDDocument().EventResources[1])
    65  	require.NoError(t, err)
    66  
    67  	packagePreSanitizedHash, err := ord.HashObject(fixORDDocument().Packages[0])
    68  	require.NoError(t, err)
    69  
    70  	bundlePreSanitizedHash, err := ord.HashObject(fixORDDocument().ConsumptionBundles[0])
    71  	require.NoError(t, err)
    72  
    73  	c := fixORDStaticDocument().ConsumptionBundles[0]
    74  	bundlePreSanitizedHashStaticDoc, err := ord.HashObject(c)
    75  	require.NoError(t, err)
    76  
    77  	successfulWebhookConversion := func() *automock.WebhookConverter {
    78  		whConv := &automock.WebhookConverter{}
    79  		whConv.On("InputFromGraphQL", fixTenantMappingWebhookGraphQLInput()).Return(fixTenantMappingWebhookModelInput(), nil).Once()
    80  		return whConv
    81  	}
    82  
    83  	successfulWebhookList := func() *automock.WebhookService {
    84  		whSvc := &automock.WebhookService{}
    85  		whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixWebhooksForApplication(), nil).Once()
    86  		return whSvc
    87  	}
    88  
    89  	successfulWebhookListAppTemplate := func() *automock.WebhookService {
    90  		whSvc := &automock.WebhookService{}
    91  		whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixOrdWebhooksForAppTemplate(), nil).Once()
    92  		return whSvc
    93  	}
    94  
    95  	successfulTenantMappingOnlyCreation := func() *automock.WebhookService {
    96  		whSvc := &automock.WebhookService{}
    97  		whInputs := []*graphql.WebhookInput{fixTenantMappingWebhookGraphQLInput()}
    98  		whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixWebhooksForApplication(), nil).Once()
    99  		whSvc.On("EnrichWebhooksWithTenantMappingWebhooks", whInputs).Return(whInputs, nil).Once()
   100  		whSvc.On("ListForApplicationGlobal", txtest.CtxWithDBMatcher(), appID).Return([]*model.Webhook{}, nil).Once()
   101  		whSvc.On("Create", txtest.CtxWithDBMatcher(), appID, *fixTenantMappingWebhookModelInput(), model.ApplicationWebhookReference).Return("id", nil).Once()
   102  		return whSvc
   103  	}
   104  
   105  	successfulAppTemplateTenantMappingOnlyCreation := func() *automock.WebhookService {
   106  		whSvc := &automock.WebhookService{}
   107  		whInputs := []*graphql.WebhookInput{fixTenantMappingWebhookGraphQLInput()}
   108  		whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixOrdWebhooksForAppTemplate(), nil).Once()
   109  		whSvc.On("EnrichWebhooksWithTenantMappingWebhooks", whInputs).Return(whInputs, nil).Once()
   110  		whSvc.On("ListForApplicationGlobal", txtest.CtxWithDBMatcher(), appID).Return([]*model.Webhook{}, nil).Once()
   111  		whSvc.On("Create", txtest.CtxWithDBMatcher(), appID, *fixTenantMappingWebhookModelInput(), model.ApplicationWebhookReference).Return("id", nil).Once()
   112  		return whSvc
   113  	}
   114  
   115  	successfulTombstoneCreate := func() *automock.TombstoneService {
   116  		tombstoneSvc := &automock.TombstoneService{}
   117  		tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   118  		tombstoneSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Tombstones[0]).Return("", nil).Once()
   119  		tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
   120  		return tombstoneSvc
   121  	}
   122  
   123  	successfulTombstoneCreateForStaticDoc := func() *automock.TombstoneService {
   124  		tombstoneSvc := &automock.TombstoneService{}
   125  		tombstoneSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, nil).Once()
   126  		tombstoneSvc.On("Create", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, appTemplateVersionID, *sanitizedStaticDoc.Tombstones[0]).Return("", nil).Once()
   127  		tombstoneSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixTombstones(), nil).Once()
   128  		return tombstoneSvc
   129  	}
   130  
   131  	successfulTombstoneUpdateForStaticDoc := func() *automock.TombstoneService {
   132  		tombstoneSvc := &automock.TombstoneService{}
   133  		tombstoneSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixTombstones(), nil).Once()
   134  		tombstoneSvc.On("Update", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, tombstoneID, *sanitizedStaticDoc.Tombstones[0]).Return(nil).Once()
   135  		tombstoneSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixTombstones(), nil).Once()
   136  		return tombstoneSvc
   137  	}
   138  
   139  	successfulTombstoneUpdateForStaticDocWithApplication := func() *automock.TombstoneService {
   140  		tombstoneSvc := &automock.TombstoneService{}
   141  		tombstoneSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixTombstones(), nil).Times(2)
   142  		tombstoneSvc.On("Update", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, tombstoneID, *sanitizedStaticDoc.Tombstones[0]).Return(nil).Times(2)
   143  		tombstoneSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixTombstones(), nil).Times(2)
   144  		return tombstoneSvc
   145  	}
   146  
   147  	successfulBundleUpdateForApplication := func() *automock.BundleService {
   148  		bundlesSvc := &automock.BundleService{}
   149  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(fixBundles(), nil).Once()
   150  		bundlesSvc.On("UpdateBundle", txtest.CtxWithDBMatcher(), resource.Application, bundleID, bundleUpdateInputFromCreateInput(*sanitizedDoc.ConsumptionBundles[0]), bundlePreSanitizedHash).Return(nil).Once()
   151  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(fixBundles(), nil).Once()
   152  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(fixBundles(), nil).Once()
   153  		return bundlesSvc
   154  	}
   155  
   156  	successfulBundleUpdateForStaticDoc := func() *automock.BundleService {
   157  		bundlesSvc := &automock.BundleService{}
   158  		bundlesSvc.On("ListByApplicationTemplateVersionIDNoPaging", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixBundlesWithCredentialExchangeStrategies(), nil).Once()
   159  		bundlesSvc.On("UpdateBundle", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, bundleID, bundleUpdateInputFromCreateInput(*sanitizedStaticDoc.ConsumptionBundles[0]), bundlePreSanitizedHashStaticDoc).Return(nil).Once()
   160  		bundlesSvc.On("ListByApplicationTemplateVersionIDNoPaging", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixBundlesWithCredentialExchangeStrategies(), nil).Once()
   161  		bundlesSvc.On("ListByApplicationTemplateVersionIDNoPaging", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixBundlesWithCredentialExchangeStrategies(), nil).Once()
   162  		return bundlesSvc
   163  	}
   164  
   165  	successfulBundleUpdateForStaticDocWithApplication := func() *automock.BundleService {
   166  		bundlesSvc := &automock.BundleService{}
   167  		bundlesSvc.On("ListByApplicationTemplateVersionIDNoPaging", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixBundlesWithCredentialExchangeStrategies(), nil).Once()
   168  		bundlesSvc.On("UpdateBundle", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, bundleID, bundleUpdateInputFromCreateInput(*sanitizedStaticDoc.ConsumptionBundles[0]), bundlePreSanitizedHashStaticDoc).Return(nil).Times(2)
   169  		bundlesSvc.On("ListByApplicationTemplateVersionIDNoPaging", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixBundlesWithCredentialExchangeStrategies(), nil).Once()
   170  		bundlesSvc.On("ListByApplicationTemplateVersionIDNoPaging", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixBundlesWithCredentialExchangeStrategies(), nil).Times(4)
   171  
   172  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(fixBundles(), nil).Once()
   173  		return bundlesSvc
   174  	}
   175  
   176  	successfulBundleCreate := func() *automock.BundleService {
   177  		bundlesSvc := &automock.BundleService{}
   178  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   179  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   180  		bundlesSvc.On("CreateBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.ConsumptionBundles[0], mock.Anything).Return("", nil).Once()
   181  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(fixBundles(), nil).Once()
   182  		return bundlesSvc
   183  	}
   184  
   185  	successfulBundleCreateForStaticDoc := func() *automock.BundleService {
   186  		bundlesSvc := &automock.BundleService{}
   187  		bundlesSvc.On("ListByApplicationTemplateVersionIDNoPaging", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, nil).Once()
   188  		bundlesSvc.On("ListByApplicationTemplateVersionIDNoPaging", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, nil).Once()
   189  		bundlesSvc.On("CreateBundle", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, appTemplateVersionID, *sanitizedStaticDoc.ConsumptionBundles[0], mock.Anything).Return("", nil).Once()
   190  		bundlesSvc.On("ListByApplicationTemplateVersionIDNoPaging", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixBundles(), nil).Once()
   191  		return bundlesSvc
   192  	}
   193  
   194  	successfulBundleCreateWithGenericParam := func() *automock.BundleService {
   195  		bundlesSvc := &automock.BundleService{}
   196  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   197  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   198  		bundlesSvc.On("CreateBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, mock.Anything, mock.Anything).Return("", nil).Once()
   199  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(fixBundles(), nil).Once()
   200  		return bundlesSvc
   201  	}
   202  
   203  	successfulListTwiceAndCreateBundle := func() *automock.BundleService {
   204  		bundlesSvc := &automock.BundleService{}
   205  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   206  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   207  		bundlesSvc.On("CreateBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, mock.Anything, mock.Anything).Return("", nil).Once()
   208  		return bundlesSvc
   209  	}
   210  
   211  	successfulBundleReferenceFetchingOfAPIBundleIDs := func() *automock.BundleReferenceService {
   212  		bundleRefSvc := &automock.BundleReferenceService{}
   213  		firstAPIID := api1ID
   214  		secondAPIID := api2ID
   215  		bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleAPIReference, &firstAPIID).Return([]string{bundleID}, nil).Once()
   216  		bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleAPIReference, &secondAPIID).Return([]string{bundleID}, nil).Once()
   217  		return bundleRefSvc
   218  	}
   219  
   220  	successfulBundleReferenceFetchingOfBundleIDs := func() *automock.BundleReferenceService {
   221  		bundleRefSvc := &automock.BundleReferenceService{}
   222  		firstAPIID := api1ID
   223  		secondAPIID := api2ID
   224  		firstEventID := event1ID
   225  		secondEventID := event2ID
   226  		bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleAPIReference, &firstAPIID).Return([]string{bundleID}, nil).Once()
   227  		bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleAPIReference, &secondAPIID).Return([]string{bundleID}, nil).Once()
   228  		bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleEventReference, &firstEventID).Return([]string{bundleID}, nil).Once()
   229  		bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleEventReference, &secondEventID).Return([]string{bundleID}, nil).Once()
   230  		bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleAPIReference, &firstAPIID).Return([]string{bundleID}, nil).Once()
   231  		bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleAPIReference, &secondAPIID).Return([]string{bundleID}, nil).Once()
   232  		bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleEventReference, &firstEventID).Return([]string{bundleID}, nil).Once()
   233  		bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleEventReference, &secondEventID).Return([]string{bundleID}, nil).Once()
   234  		return bundleRefSvc
   235  	}
   236  
   237  	successfulVendorUpdateForApplication := func() *automock.VendorService {
   238  		vendorSvc := &automock.VendorService{}
   239  		vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixVendors(), nil).Once()
   240  		vendorSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, vendorID, *sanitizedDoc.Vendors[0]).Return(nil).Once()
   241  		vendorSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, vendorID2, *sanitizedDoc.Vendors[1]).Return(nil).Once()
   242  		vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixVendors(), nil).Once()
   243  		return vendorSvc
   244  	}
   245  
   246  	successfulVendorUpdateForStaticDoc := func() *automock.VendorService {
   247  		vendorSvc := &automock.VendorService{}
   248  		vendorSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixVendors(), nil).Once()
   249  		vendorSvc.On("Update", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, vendorID, *sanitizedStaticDoc.Vendors[0]).Return(nil).Once()
   250  		vendorSvc.On("Update", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, vendorID2, *sanitizedStaticDoc.Vendors[1]).Return(nil).Once()
   251  		vendorSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixVendors(), nil).Once()
   252  		return vendorSvc
   253  	}
   254  
   255  	successfulVendorUpdateForStaticDocWithApplication := func() *automock.VendorService {
   256  		vendorSvc := &automock.VendorService{}
   257  		vendorSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixVendors(), nil).Times(2)
   258  		vendorSvc.On("Update", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, vendorID, *sanitizedStaticDoc.Vendors[0]).Return(nil).Times(2)
   259  		vendorSvc.On("Update", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, vendorID2, *sanitizedStaticDoc.Vendors[1]).Return(nil).Times(2)
   260  		vendorSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixVendors(), nil).Times(2)
   261  		return vendorSvc
   262  	}
   263  
   264  	successfulVendorCreate := func() *automock.VendorService {
   265  		vendorSvc := &automock.VendorService{}
   266  		vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   267  		vendorSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Vendors[0]).Return("", nil).Once()
   268  		vendorSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Vendors[1]).Return("", nil).Once()
   269  		vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixVendors(), nil).Once()
   270  		return vendorSvc
   271  	}
   272  
   273  	successfulVendorCreateForStaticDoc := func() *automock.VendorService {
   274  		vendorSvc := &automock.VendorService{}
   275  		vendorSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, nil).Once()
   276  		vendorSvc.On("Create", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, appTemplateVersionID, *sanitizedStaticDoc.Vendors[0]).Return("", nil).Once()
   277  		vendorSvc.On("Create", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, appTemplateVersionID, *sanitizedStaticDoc.Vendors[1]).Return("", nil).Once()
   278  		vendorSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixVendors(), nil).Once()
   279  		return vendorSvc
   280  	}
   281  
   282  	successfulProductUpdateForApplication := func() *automock.ProductService {
   283  		productSvc := &automock.ProductService{}
   284  		productSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixProducts(), nil).Once()
   285  		productSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, productID, *sanitizedDoc.Products[0]).Return(nil).Once()
   286  		productSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixProducts(), nil).Once()
   287  		return productSvc
   288  	}
   289  
   290  	successfulProductUpdateForStaticDoc := func() *automock.ProductService {
   291  		productSvc := &automock.ProductService{}
   292  		productSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixProducts(), nil).Once()
   293  		productSvc.On("Update", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, productID, *sanitizedStaticDoc.Products[0]).Return(nil).Once()
   294  		productSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixProducts(), nil).Once()
   295  		return productSvc
   296  	}
   297  
   298  	successfulProductUpdateForStaticDocWithApplication := func() *automock.ProductService {
   299  		productSvc := &automock.ProductService{}
   300  		productSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixProducts(), nil).Times(2)
   301  		productSvc.On("Update", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, productID, *sanitizedStaticDoc.Products[0]).Return(nil).Times(2)
   302  		productSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixProducts(), nil).Times(2)
   303  		return productSvc
   304  	}
   305  
   306  	successfulProductCreate := func() *automock.ProductService {
   307  		productSvc := &automock.ProductService{}
   308  		productSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   309  		productSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Products[0]).Return("", nil).Once()
   310  		productSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixProducts(), nil).Once()
   311  		return productSvc
   312  	}
   313  
   314  	successfulProductCreateForStaticDoc := func() *automock.ProductService {
   315  		productSvc := &automock.ProductService{}
   316  		productSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, nil).Once()
   317  		productSvc.On("Create", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, appTemplateVersionID, *sanitizedStaticDoc.Products[0]).Return("", nil).Once()
   318  		productSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixProducts(), nil).Once()
   319  		return productSvc
   320  	}
   321  
   322  	successfulPackageUpdateForApplication := func() *automock.PackageService {
   323  		packagesSvc := &automock.PackageService{}
   324  		packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixPackagesWithHash(), nil).Once()
   325  		packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixPackages(), nil).Once()
   326  		packagesSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, packageID, *sanitizedDoc.Packages[0], packagePreSanitizedHash).Return(nil).Once()
   327  		packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixPackages(), nil).Once()
   328  		return packagesSvc
   329  	}
   330  
   331  	successfulPackageUpdateForStaticDoc := func() *automock.PackageService {
   332  		packagesSvc := &automock.PackageService{}
   333  		packagesSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixPackagesWithHash(), nil).Once()
   334  		packagesSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixPackages(), nil).Once()
   335  		packagesSvc.On("Update", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, packageID, *sanitizedStaticDoc.Packages[0], packagePreSanitizedHash).Return(nil).Once()
   336  		packagesSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixPackages(), nil).Once()
   337  		return packagesSvc
   338  	}
   339  
   340  	successfulPackageUpdateForStaticDocWithApplication := func() *automock.PackageService {
   341  		packagesSvc := &automock.PackageService{}
   342  		packagesSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixPackagesWithHash(), nil).Once()
   343  		packagesSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixPackages(), nil).Once()
   344  		packagesSvc.On("Update", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, packageID, *sanitizedStaticDoc.Packages[0], packagePreSanitizedHash).Return(nil).Times(2)
   345  		packagesSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixPackages(), nil).Times(4)
   346  
   347  		packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixPackagesWithHash(), nil).Once()
   348  		return packagesSvc
   349  	}
   350  
   351  	successfulPackageCreate := func() *automock.PackageService {
   352  		packagesSvc := &automock.PackageService{}
   353  		packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   354  		packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   355  		packagesSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Packages[0], mock.Anything).Return("", nil).Once()
   356  		packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixPackages(), nil).Once()
   357  		return packagesSvc
   358  	}
   359  
   360  	successfulPackageCreateForStaticDoc := func() *automock.PackageService {
   361  		packagesSvc := &automock.PackageService{}
   362  		packagesSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, nil).Once()
   363  		packagesSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, nil).Once()
   364  		packagesSvc.On("Create", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, appTemplateVersionID, *sanitizedStaticDoc.Packages[0], mock.Anything).Return("", nil).Once()
   365  		packagesSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixPackages(), nil).Once()
   366  		return packagesSvc
   367  	}
   368  
   369  	successfulEmptyAPIList := func() *automock.APIService {
   370  		apiSvc := &automock.APIService{}
   371  		apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   372  
   373  		return apiSvc
   374  	}
   375  
   376  	successfulEmptyEventList := func() *automock.EventService {
   377  		eventSvc := &automock.EventService{}
   378  		eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   379  
   380  		return eventSvc
   381  	}
   382  
   383  	successfulEmptyPackageList := func() *automock.PackageService {
   384  		pkgService := &automock.PackageService{}
   385  		pkgService.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   386  
   387  		return pkgService
   388  	}
   389  
   390  	successfulEmptyBundleList := func() *automock.BundleService {
   391  		bundlesSvc := &automock.BundleService{}
   392  		bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   393  
   394  		return bundlesSvc
   395  	}
   396  
   397  	successfulEmptyVendorList := func() *automock.VendorService {
   398  		vendorService := &automock.VendorService{}
   399  		vendorService.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Twice()
   400  
   401  		return vendorService
   402  	}
   403  
   404  	successfulSpecCreate := func() *automock.SpecService {
   405  		specSvc := &automock.SpecService{}
   406  
   407  		api1SpecInput1 := fixAPI1SpecInputs()[0]
   408  		api1SpecInput2 := fixAPI1SpecInputs()[1]
   409  		api1SpecInput3 := fixAPI1SpecInputs()[2]
   410  
   411  		api2SpecInput1 := fixAPI2SpecInputs()[0]
   412  		api2SpecInput2 := fixAPI2SpecInputs()[1]
   413  
   414  		event1Spec := fixEvent1SpecInputs()[0]
   415  		event2Spec := fixEvent2SpecInputs()[0]
   416  
   417  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   418  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   419  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   420  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.Application, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   421  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.Application, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   422  
   423  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event1Spec, resource.Application, model.EventSpecReference, event1ID).Return("", fixFetchRequestFromFetchRequestInput(event1Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   424  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event2Spec, resource.Application, model.EventSpecReference, event2ID).Return("", fixFetchRequestFromFetchRequestInput(event2Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   425  
   426  		return specSvc
   427  	}
   428  
   429  	successfulSpecRecreate := func() *automock.SpecService {
   430  		specSvc := &automock.SpecService{}
   431  
   432  		api1SpecInput1 := fixAPI1SpecInputs()[0]
   433  		api1SpecInput2 := fixAPI1SpecInputs()[1]
   434  		api1SpecInput3 := fixAPI1SpecInputs()[2]
   435  
   436  		api2SpecInput1 := fixAPI2SpecInputs()[0]
   437  		api2SpecInput2 := fixAPI2SpecInputs()[1]
   438  
   439  		event1Spec := fixEvent1SpecInputs()[0]
   440  		event2Spec := fixEvent2SpecInputs()[0]
   441  
   442  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(nil).Once()
   443  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   444  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   445  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   446  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api2ID).Return(nil).Once()
   447  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.Application, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   448  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.Application, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   449  
   450  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event1ID).Return(nil).Once()
   451  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event1Spec, resource.Application, model.EventSpecReference, event1ID).Return("", fixFetchRequestFromFetchRequestInput(event1Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   452  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event2ID).Return(nil).Once()
   453  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event2Spec, resource.Application, model.EventSpecReference, event2ID).Return("", fixFetchRequestFromFetchRequestInput(event2Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   454  
   455  		return specSvc
   456  	}
   457  
   458  	successfulSpecCreateAndUpdate := func() *automock.SpecService {
   459  		specSvc := &automock.SpecService{}
   460  
   461  		api1SpecInput1 := fixAPI1SpecInputs()[0]
   462  		api1SpecInput2 := fixAPI1SpecInputs()[1]
   463  		api1SpecInput3 := fixAPI1SpecInputs()[2]
   464  
   465  		api2SpecInput1 := fixAPI2SpecInputs()[0]
   466  		api2SpecInput2 := fixAPI2SpecInputs()[1]
   467  
   468  		event1Spec := fixEvent1SpecInputs()[0]
   469  		event2Spec := fixEvent2SpecInputs()[0]
   470  
   471  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   472  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   473  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   474  
   475  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   476  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   477  
   478  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event1Spec, resource.Application, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event1Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   479  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event2Spec, resource.Application, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event2Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   480  
   481  		specSvc.On("GetByID", txtest.CtxWithDBMatcher(), mock.Anything, mock.Anything).Return(&testSpec, nil).
   482  			Times(len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   483  
   484  		expectedSpecToUpdate := testSpec
   485  		expectedSpecToUpdate.Data = &testSpecData
   486  		specSvc.On("UpdateSpecOnly", txtest.CtxWithDBMatcher(), expectedSpecToUpdate).Return(nil).
   487  			Times(len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   488  
   489  		return specSvc
   490  	}
   491  
   492  	successfulSpecCreateAndUpdateForStaticDoc := func() *automock.SpecService {
   493  		specSvc := &automock.SpecService{}
   494  
   495  		api1SpecInput1 := fixAPI1SpecInputs()[0]
   496  		api1SpecInput2 := fixAPI1SpecInputs()[1]
   497  		api1SpecInput3 := fixAPI1SpecInputs()[2]
   498  
   499  		api2SpecInput1 := fixAPI2SpecInputs()[0]
   500  		api2SpecInput2 := fixAPI2SpecInputs()[1]
   501  
   502  		event1Spec := fixEvent1SpecInputs()[0]
   503  		event2Spec := fixEvent2SpecInputs()[0]
   504  
   505  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.ApplicationTemplateVersion, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   506  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.ApplicationTemplateVersion, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   507  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.ApplicationTemplateVersion, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   508  
   509  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.ApplicationTemplateVersion, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   510  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.ApplicationTemplateVersion, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   511  
   512  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event1Spec, resource.ApplicationTemplateVersion, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event1Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   513  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event2Spec, resource.ApplicationTemplateVersion, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event2Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   514  
   515  		specSvc.On("GetByIDGlobal", txtest.CtxWithDBMatcher(), mock.Anything, mock.Anything).Return(&testSpec, nil).
   516  			Times(len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   517  
   518  		expectedSpecToUpdate := testSpec
   519  		expectedSpecToUpdate.Data = &testSpecData
   520  		specSvc.On("UpdateSpecOnlyGlobal", txtest.CtxWithDBMatcher(), expectedSpecToUpdate).Return(nil).
   521  			Times(len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   522  
   523  		return specSvc
   524  	}
   525  
   526  	successfulSpecWithOneEventCreateAndUpdate := func() *automock.SpecService {
   527  		specSvc := &automock.SpecService{}
   528  
   529  		api1SpecInput1 := fixAPI1SpecInputs()[0]
   530  		api1SpecInput2 := fixAPI1SpecInputs()[1]
   531  		api1SpecInput3 := fixAPI1SpecInputs()[2]
   532  
   533  		api2SpecInput1 := fixAPI2SpecInputs()[0]
   534  		api2SpecInput2 := fixAPI2SpecInputs()[1]
   535  
   536  		event2Spec := fixEvent2SpecInputs()[0]
   537  
   538  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   539  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   540  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   541  
   542  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   543  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   544  
   545  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event2Spec, resource.Application, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event2Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   546  
   547  		specSvc.On("GetByID", txtest.CtxWithDBMatcher(), mock.Anything, mock.Anything).Return(&testSpec, nil).
   548  			Times(len(fixAPI1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   549  
   550  		expectedSpecToUpdate := testSpec
   551  		expectedSpecToUpdate.Data = &testSpecData
   552  		specSvc.On("UpdateSpecOnly", txtest.CtxWithDBMatcher(), expectedSpecToUpdate).Return(nil).
   553  			Times(len(fixAPI1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   554  
   555  		return specSvc
   556  	}
   557  
   558  	successfulSpecRecreateAndUpdate := func() *automock.SpecService {
   559  		specSvc := &automock.SpecService{}
   560  
   561  		api1SpecInput1 := fixAPI1SpecInputs()[0]
   562  		api1SpecInput2 := fixAPI1SpecInputs()[1]
   563  		api1SpecInput3 := fixAPI1SpecInputs()[2]
   564  
   565  		api2SpecInput1 := fixAPI2SpecInputs()[0]
   566  		api2SpecInput2 := fixAPI2SpecInputs()[1]
   567  
   568  		event1Spec := fixEvent1SpecInputs()[0]
   569  		event2Spec := fixEvent2SpecInputs()[0]
   570  
   571  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(nil).Once()
   572  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   573  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   574  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   575  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api2ID).Return(nil).Once()
   576  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.Application, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   577  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.Application, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   578  
   579  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event1ID).Return(nil).Once()
   580  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event1Spec, resource.Application, model.EventSpecReference, event1ID).Return("", fixFetchRequestFromFetchRequestInput(event1Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   581  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event2ID).Return(nil).Once()
   582  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event2Spec, resource.Application, model.EventSpecReference, event2ID).Return("", fixFetchRequestFromFetchRequestInput(event2Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   583  
   584  		specSvc.On("GetByID", txtest.CtxWithDBMatcher(), mock.Anything, mock.Anything).Return(&testSpec, nil).
   585  			Times(len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   586  
   587  		expectedSpecToUpdate := testSpec
   588  		expectedSpecToUpdate.Data = &testSpecData
   589  		specSvc.On("UpdateSpecOnly", txtest.CtxWithDBMatcher(), expectedSpecToUpdate).Return(nil).
   590  			Times(len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   591  
   592  		return specSvc
   593  	}
   594  
   595  	successfulSpecRecreateAndUpdateForStaticDoc := func() *automock.SpecService {
   596  		specSvc := &automock.SpecService{}
   597  
   598  		api1SpecInput1 := fixAPI1SpecInputs()[0]
   599  		api1SpecInput2 := fixAPI1SpecInputs()[1]
   600  		api1SpecInput3 := fixAPI1SpecInputs()[2]
   601  
   602  		api2SpecInput1 := fixAPI2SpecInputs()[0]
   603  		api2SpecInput2 := fixAPI2SpecInputs()[1]
   604  
   605  		event1Spec := fixEvent1SpecInputs()[0]
   606  		event2Spec := fixEvent2SpecInputs()[0]
   607  
   608  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, model.APISpecReference, api1ID).Return(nil).Once()
   609  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.ApplicationTemplateVersion, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   610  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.ApplicationTemplateVersion, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   611  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.ApplicationTemplateVersion, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   612  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, model.APISpecReference, api2ID).Return(nil).Once()
   613  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.ApplicationTemplateVersion, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   614  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.ApplicationTemplateVersion, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   615  
   616  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, model.EventSpecReference, event1ID).Return(nil).Once()
   617  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event1Spec, resource.ApplicationTemplateVersion, model.EventSpecReference, event1ID).Return("", fixFetchRequestFromFetchRequestInput(event1Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   618  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, model.EventSpecReference, event2ID).Return(nil).Once()
   619  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event2Spec, resource.ApplicationTemplateVersion, model.EventSpecReference, event2ID).Return("", fixFetchRequestFromFetchRequestInput(event2Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
   620  
   621  		specSvc.On("GetByIDGlobal", txtest.CtxWithDBMatcher(), mock.Anything, mock.Anything).Return(&testSpec, nil).
   622  			Times(len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   623  
   624  		expectedSpecToUpdate := testSpec
   625  		expectedSpecToUpdate.Data = &testSpecData
   626  		specSvc.On("UpdateSpecOnlyGlobal", txtest.CtxWithDBMatcher(), expectedSpecToUpdate).Return(nil).
   627  			Times(len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   628  
   629  		return specSvc
   630  	}
   631  
   632  	successfulSpecRecreateAndUpdateForStaticDocWithApplication := func() *automock.SpecService {
   633  		specSvc := &automock.SpecService{}
   634  
   635  		api1SpecInput1 := fixAPI1SpecInputs()[0]
   636  		api1SpecInput2 := fixAPI1SpecInputs()[1]
   637  		api1SpecInput3 := fixAPI1SpecInputs()[2]
   638  
   639  		api2SpecInput1 := fixAPI2SpecInputs()[0]
   640  		api2SpecInput2 := fixAPI2SpecInputs()[1]
   641  
   642  		event1Spec := fixEvent1SpecInputs()[0]
   643  		event2Spec := fixEvent2SpecInputs()[0]
   644  
   645  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, model.APISpecReference, api1ID).Return(nil).Times(2)
   646  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.ApplicationTemplateVersion, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Times(2)
   647  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.ApplicationTemplateVersion, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Times(2)
   648  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.ApplicationTemplateVersion, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Times(2)
   649  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, model.APISpecReference, api2ID).Return(nil).Times(2)
   650  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.ApplicationTemplateVersion, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Times(2)
   651  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.ApplicationTemplateVersion, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Times(2)
   652  
   653  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, model.EventSpecReference, event1ID).Return(nil).Times(2)
   654  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event1Spec, resource.ApplicationTemplateVersion, model.EventSpecReference, event1ID).Return("", fixFetchRequestFromFetchRequestInput(event1Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Times(2)
   655  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, model.EventSpecReference, event2ID).Return(nil).Times(2)
   656  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event2Spec, resource.ApplicationTemplateVersion, model.EventSpecReference, event2ID).Return("", fixFetchRequestFromFetchRequestInput(event2Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Times(2)
   657  
   658  		specSvc.On("GetByIDGlobal", txtest.CtxWithDBMatcher(), mock.Anything, mock.Anything).Return(&testSpec, nil).
   659  			Times((len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) * 2) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   660  
   661  		expectedSpecToUpdate := testSpec
   662  		expectedSpecToUpdate.Data = &testSpecData
   663  		specSvc.On("UpdateSpecOnlyGlobal", txtest.CtxWithDBMatcher(), expectedSpecToUpdate).Return(nil).
   664  			Times((len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) * 2) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   665  
   666  		return specSvc
   667  	}
   668  
   669  	successfulSpecRefetch := func() *automock.SpecService {
   670  		specSvc := &automock.SpecService{}
   671  		specSvc.On("ListIDByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(fixAPI1IDs(), nil).Once()
   672  		specSvc.On("ListFetchRequestsByReferenceObjectIDs", txtest.CtxWithDBMatcher(), tenantID, fixAPI1IDs(), model.APISpecReference).Return([]*model.FetchRequest{fixSuccessfulFetchRequest(), fixSuccessfulFetchRequest(), fixFailedFetchRequest()}, nil).Once()
   673  
   674  		specSvc.On("ListIDByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api2ID).Return(fixAPI2IDs(), nil).Once()
   675  		specSvc.On("ListFetchRequestsByReferenceObjectIDs", txtest.CtxWithDBMatcher(), tenantID, []string{api2spec1ID, api2spec2ID}, model.APISpecReference).Return([]*model.FetchRequest{fixSuccessfulFetchRequest(), fixFailedFetchRequest()}, nil).Once()
   676  
   677  		specSvc.On("ListIDByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event1ID).Return(fixEvent1IDs(), nil).Once()
   678  		specSvc.On("ListFetchRequestsByReferenceObjectIDs", txtest.CtxWithDBMatcher(), tenantID, fixEvent1IDs(), model.EventSpecReference).Return([]*model.FetchRequest{fixFailedFetchRequest()}, nil).Once()
   679  
   680  		specSvc.On("ListIDByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event2ID).Return(fixEvent2IDs(), nil).Once()
   681  		specSvc.On("ListFetchRequestsByReferenceObjectIDs", txtest.CtxWithDBMatcher(), tenantID, fixEvent2IDs(), model.EventSpecReference).Return([]*model.FetchRequest{fixFailedFetchRequest()}, nil).Once()
   682  
   683  		specSvc.On("GetByID", txtest.CtxWithDBMatcher(), mock.Anything, mock.Anything).Return(&testSpec, nil).Times(3)
   684  
   685  		expectedSpecToUpdate := testSpec
   686  		expectedSpecToUpdate.Data = &testSpecData
   687  		specSvc.On("UpdateSpecOnly", txtest.CtxWithDBMatcher(), expectedSpecToUpdate).Return(nil).Times(3)
   688  
   689  		return specSvc
   690  	}
   691  
   692  	successfulAPISpecUpdate := func() *automock.SpecService {
   693  		specSvc := &automock.SpecService{}
   694  
   695  		api1SpecInput1 := fixAPI1SpecInputs()[0]
   696  		api1SpecInput2 := fixAPI1SpecInputs()[1]
   697  		api1SpecInput3 := fixAPI1SpecInputs()[2]
   698  
   699  		api2SpecInput1 := fixAPI2SpecInputs()[0]
   700  		api2SpecInput2 := fixAPI2SpecInputs()[1]
   701  
   702  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(nil).Once()
   703  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   704  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   705  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   706  		specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api2ID).Return(nil).Once()
   707  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.Application, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   708  		specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.Application, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
   709  
   710  		return specSvc
   711  	}
   712  
   713  	successfulFetchRequestFetch := func() *automock.FetchRequestService {
   714  		fetchReqSvc := &automock.FetchRequestService{}
   715  		fetchReqSvc.On("FetchSpec", txtest.CtxWithDBMatcher(), mock.Anything).Return(&testSpecData, &model.FetchRequestStatus{Condition: model.FetchRequestStatusConditionSucceeded}).
   716  			Times(len(fixAPI1SpecInputs()) + len(fixAPI2SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs()))
   717  
   718  		return fetchReqSvc
   719  	}
   720  
   721  	successfulFetchRequestFetchAndUpdate := func() *automock.FetchRequestService {
   722  		fetchReqSvc := &automock.FetchRequestService{}
   723  		fetchReqSvc.On("FetchSpec", txtest.CtxWithDBMatcher(), mock.Anything).Return(&testSpecData, &model.FetchRequestStatus{Condition: model.FetchRequestStatusConditionSucceeded}).
   724  			Times(len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   725  
   726  		fetchReqSvc.On("Update", txtest.CtxWithDBMatcher(), mock.MatchedBy(func(actual *model.FetchRequest) bool {
   727  			return actual.Status.Condition == model.FetchRequestStatusConditionSucceeded
   728  		})).Return(nil).
   729  			Times(len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   730  
   731  		return fetchReqSvc
   732  	}
   733  
   734  	successfulFetchRequestFetchAndUpdateForStaticDoc := func() *automock.FetchRequestService {
   735  		fetchReqSvc := &automock.FetchRequestService{}
   736  		fetchReqSvc.On("FetchSpec", txtest.CtxWithDBMatcher(), mock.Anything).Return(&testSpecData, &model.FetchRequestStatus{Condition: model.FetchRequestStatusConditionSucceeded}).
   737  			Times(len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   738  
   739  		fetchReqSvc.On("UpdateGlobal", txtest.CtxWithDBMatcher(), mock.MatchedBy(func(actual *model.FetchRequest) bool {
   740  			return actual.Status.Condition == model.FetchRequestStatusConditionSucceeded
   741  		})).Return(nil).
   742  			Times(len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   743  
   744  		return fetchReqSvc
   745  	}
   746  
   747  	successfulFetchRequestFetchAndUpdateForStaticDocForApplication := func() *automock.FetchRequestService {
   748  		fetchReqSvc := &automock.FetchRequestService{}
   749  		fetchReqSvc.On("FetchSpec", txtest.CtxWithDBMatcher(), mock.Anything).Return(&testSpecData, &model.FetchRequestStatus{Condition: model.FetchRequestStatusConditionSucceeded}).
   750  			Times((len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) * 2) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   751  
   752  		fetchReqSvc.On("UpdateGlobal", txtest.CtxWithDBMatcher(), mock.MatchedBy(func(actual *model.FetchRequest) bool {
   753  			return actual.Status.Condition == model.FetchRequestStatusConditionSucceeded
   754  		})).Return(nil).
   755  			Times((len(fixAPI1SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs())) * 2) // len(fixAPI2SpecInputs()) is excluded because it's API is part of tombstones
   756  
   757  		return fetchReqSvc
   758  	}
   759  
   760  	successfulAPIUpdate := func() *automock.APIService {
   761  		apiSvc := &automock.APIService{}
   762  		apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
   763  		apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
   764  		apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
   765  		apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api2ID, *sanitizedDoc.APIResources[1], nilSpecInput, map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, map[string]string{}, []string{}, api2PreSanitizedHash, "").Return(nil).Once()
   766  		apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
   767  		return apiSvc
   768  	}
   769  
   770  	successfulAPICreate := func() *automock.APIService {
   771  		apiSvc := &automock.APIService{}
   772  		apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   773  		apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   774  		apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return(api1ID, nil).Once()
   775  		apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, mock.Anything, "").Return(api2ID, nil).Once()
   776  		apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
   777  		return apiSvc
   778  	}
   779  
   780  	successfulAPICreateAndDelete := func() *automock.APIService {
   781  		apiSvc := &automock.APIService{}
   782  		apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   783  		apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   784  		apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return("", nil).Once()
   785  		apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, mock.Anything, "").Return("", nil).Once()
   786  		apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
   787  		apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
   788  		return apiSvc
   789  	}
   790  
   791  	successfulEventUpdate := func() *automock.EventService {
   792  		eventSvc := &automock.EventService{}
   793  		eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
   794  		eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event1ID, *sanitizedDoc.EventResources[0], nilSpecInput, []string{bundleID}, []string{}, []string{}, event1PreSanitizedHash, "").Return(nil).Once()
   795  		eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event2ID, *sanitizedDoc.EventResources[1], nilSpecInput, []string{bundleID}, []string{}, []string{}, event2PreSanitizedHash, "").Return(nil).Once()
   796  		eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Twice()
   797  		return eventSvc
   798  	}
   799  
   800  	successfulEventUpdateForStaticDoc := func() *automock.EventService {
   801  		eventSvc := &automock.EventService{}
   802  		eventSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixEvents(), nil).Once()
   803  		eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, event1ID, *sanitizedStaticDoc.EventResources[0], nilSpecInput, []string{bundleID}, []string{}, []string{}, event1PreSanitizedHash, "").Return(nil).Once()
   804  		eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, event2ID, *sanitizedStaticDoc.EventResources[1], nilSpecInput, []string{bundleID}, []string{}, []string{}, event2PreSanitizedHash, "").Return(nil).Once()
   805  		eventSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixEvents(), nil).Twice()
   806  		return eventSvc
   807  	}
   808  
   809  	successfulEventUpdateForStaticDocWithApplication := func() *automock.EventService {
   810  		eventSvc := &automock.EventService{}
   811  		eventSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixEvents(), nil).Once()
   812  		eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, event1ID, *sanitizedStaticDoc.EventResources[0], nilSpecInput, []string{bundleID}, []string{}, []string{}, event1PreSanitizedHash, "").Return(nil).Times(2)
   813  		eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, event2ID, *sanitizedStaticDoc.EventResources[1], nilSpecInput, []string{bundleID}, []string{}, []string{}, event2PreSanitizedHash, "").Return(nil).Times(2)
   814  		eventSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixEvents(), nil).Times(5)
   815  
   816  		eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
   817  		//eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event1ID, *sanitizedDoc.EventResources[0], nilSpecInput, []string{bundleID}, []string{}, []string{}, event1PreSanitizedHash, "").Return(nil).Times(2)
   818  		//eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event2ID, *sanitizedDoc.EventResources[1], nilSpecInput, []string{bundleID}, []string{}, []string{}, event2PreSanitizedHash, "").Return(nil).Times(2)
   819  		//eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Twice()
   820  		return eventSvc
   821  	}
   822  
   823  	successfulEventCreate := func() *automock.EventService {
   824  		eventSvc := &automock.EventService{}
   825  		eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   826  		eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   827  		eventSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.EventResources[0], ([]*model.SpecInput)(nil), []string{bundleID}, mock.Anything, "").Return(event1ID, nil).Once()
   828  		eventSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.EventResources[1], ([]*model.SpecInput)(nil), []string{bundleID}, mock.Anything, "").Return(event2ID, nil).Once()
   829  		eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
   830  		return eventSvc
   831  	}
   832  
   833  	successfulEventCreateForStaticDoc := func() *automock.EventService {
   834  		eventSvc := &automock.EventService{}
   835  		eventSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, nil).Once()
   836  		eventSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, nil).Once()
   837  		eventSvc.On("Create", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, appTemplateVersionID, nilBundleID, str.Ptr(packageID), *sanitizedStaticDoc.EventResources[0], ([]*model.SpecInput)(nil), []string{bundleID}, mock.Anything, "").Return(event1ID, nil).Once()
   838  		eventSvc.On("Create", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, appTemplateVersionID, nilBundleID, str.Ptr(packageID), *sanitizedStaticDoc.EventResources[1], ([]*model.SpecInput)(nil), []string{bundleID}, mock.Anything, "").Return(event2ID, nil).Once()
   839  		eventSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixEvents(), nil).Once()
   840  		return eventSvc
   841  	}
   842  
   843  	successfulOneEventCreate := func() *automock.EventService {
   844  		eventSvc := &automock.EventService{}
   845  		eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   846  		eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
   847  		eventSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.EventResources[1], ([]*model.SpecInput)(nil), []string{bundleID}, mock.Anything, "").Return(event2ID, nil).Once()
   848  		eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
   849  		return eventSvc
   850  	}
   851  
   852  	successfulClientFetch := func() *automock.Client {
   853  		client := &automock.Client{}
   854  		client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{fixORDDocument()}, baseURL, nil)
   855  		return client
   856  	}
   857  
   858  	successfulClientFetchForStaticDoc := func() *automock.Client {
   859  		client := &automock.Client{}
   860  		client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResourceForAppTemplate, testWebhookForAppTemplate).Return(ord.Documents{fixORDStaticDocument()}, baseURL, nil)
   861  		return client
   862  	}
   863  
   864  	successfulClientFetchForStaticDocOnAppTemplateWithApplications := func() *automock.Client {
   865  		client := &automock.Client{}
   866  		client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResourceForAppTemplate, testWebhookForAppTemplate).Return(ord.Documents{fixORDStaticDocument()}, baseURL, nil).Once()
   867  		client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForAppTemplate).Return(ord.Documents{fixORDStaticDocument()}, baseURL, nil).Once()
   868  		return client
   869  	}
   870  
   871  	successfulAppTemplateVersionList := func() *automock.ApplicationTemplateVersionService {
   872  		svc := &automock.ApplicationTemplateVersionService{}
   873  		svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixAppTemplateVersions(), nil).Twice()
   874  		return svc
   875  	}
   876  
   877  	successfulAppTemplateVersionListAndUpdate := func() *automock.ApplicationTemplateVersionService {
   878  		svc := &automock.ApplicationTemplateVersionService{}
   879  		svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixAppTemplateVersions(), nil).Twice()
   880  		svc.On("Update", txtest.CtxWithDBMatcher(), appTemplateVersionID, appTemplateID, *fixAppTemplateVersionInput()).Return(nil).Once()
   881  		svc.On("GetByAppTemplateIDAndVersion", txtest.CtxWithDBMatcher(), appTemplateID, appTemplateVersionValue).Return(fixAppTemplateVersion(), nil).Twice()
   882  		return svc
   883  	}
   884  
   885  	successfulAppTemplateVersionListAndUpdateForApplication := func() *automock.ApplicationTemplateVersionService {
   886  		svc := &automock.ApplicationTemplateVersionService{}
   887  		svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixAppTemplateVersions(), nil).Times(4)
   888  		svc.On("Update", txtest.CtxWithDBMatcher(), appTemplateVersionID, appTemplateID, *fixAppTemplateVersionInput()).Return(nil).Twice()
   889  		svc.On("GetByAppTemplateIDAndVersion", txtest.CtxWithDBMatcher(), appTemplateID, appTemplateVersionValue).Return(fixAppTemplateVersion(), nil).Times(4)
   890  		return svc
   891  	}
   892  
   893  	successfulAppTemplateVersionForCreation := func() *automock.ApplicationTemplateVersionService {
   894  		svc := &automock.ApplicationTemplateVersionService{}
   895  		svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return([]*model.ApplicationTemplateVersion{}, nil).Once()
   896  		svc.On("Create", txtest.CtxWithDBMatcher(), appTemplateID, fixAppTemplateVersionInput()).Return(appTemplateVersionID, nil)
   897  		svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixAppTemplateVersions(), nil).Once()
   898  		svc.On("GetByAppTemplateIDAndVersion", txtest.CtxWithDBMatcher(), appTemplateID, appTemplateVersionValue).Return(fixAppTemplateVersion(), nil).Twice()
   899  		return svc
   900  	}
   901  
   902  	successfulAppTemplateVersionListForAppTemplateFlow := func() *automock.ApplicationTemplateVersionService {
   903  		svc := &automock.ApplicationTemplateVersionService{}
   904  		svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixAppTemplateVersions(), nil).Times(4)
   905  		return svc
   906  	}
   907  
   908  	testCases := []struct {
   909  		Name                    string
   910  		TransactionerFn         func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
   911  		appSvcFn                func() *automock.ApplicationService
   912  		webhookSvcFn            func() *automock.WebhookService
   913  		webhookConvFn           func() *automock.WebhookConverter
   914  		bundleSvcFn             func() *automock.BundleService
   915  		bundleRefSvcFn          func() *automock.BundleReferenceService
   916  		apiSvcFn                func() *automock.APIService
   917  		eventSvcFn              func() *automock.EventService
   918  		specSvcFn               func() *automock.SpecService
   919  		fetchReqFn              func() *automock.FetchRequestService
   920  		packageSvcFn            func() *automock.PackageService
   921  		productSvcFn            func() *automock.ProductService
   922  		vendorSvcFn             func() *automock.VendorService
   923  		tombstoneSvcFn          func() *automock.TombstoneService
   924  		tenantSvcFn             func() *automock.TenantService
   925  		globalRegistrySvcFn     func() *automock.GlobalRegistryService
   926  		appTemplateVersionSvcFn func() *automock.ApplicationTemplateVersionService
   927  		appTemplateSvcFn        func() *automock.ApplicationTemplateService
   928  		clientFn                func() *automock.Client
   929  		ExpectedErr             error
   930  	}{
   931  		{
   932  			Name: "Success for Application Template webhook with Static ORD data when resources are already in db and APIs/Events versions are incremented should Update them and resync API/Event specs",
   933  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
   934  				return txGen.ThatSucceedsMultipleTimes(35)
   935  			},
   936  			appSvcFn:       successfulAppTemplateNoAppsAppSvc,
   937  			webhookSvcFn:   successfulWebhookListAppTemplate,
   938  			bundleSvcFn:    successfulBundleUpdateForStaticDoc,
   939  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
   940  			apiSvcFn: func() *automock.APIService {
   941  				apiSvc := &automock.APIService{}
   942  				apiSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixAPIs(), nil).Once()
   943  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, api1ID, *sanitizedStaticDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedStaticDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
   944  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, api2ID, *sanitizedStaticDoc.APIResources[1], nilSpecInput, map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, map[string]string{}, []string{}, api2PreSanitizedHash, "").Return(nil).Once()
   945  				apiSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixAPIs(), nil).Twice()
   946  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, api2ID).Return(nil).Once()
   947  				return apiSvc
   948  			},
   949  			eventSvcFn:              successfulEventUpdateForStaticDoc,
   950  			specSvcFn:               successfulSpecRecreateAndUpdateForStaticDoc,
   951  			fetchReqFn:              successfulFetchRequestFetchAndUpdateForStaticDoc,
   952  			packageSvcFn:            successfulPackageUpdateForStaticDoc,
   953  			productSvcFn:            successfulProductUpdateForStaticDoc,
   954  			vendorSvcFn:             successfulVendorUpdateForStaticDoc,
   955  			tombstoneSvcFn:          successfulTombstoneUpdateForStaticDoc,
   956  			appTemplateVersionSvcFn: successfulAppTemplateVersionListAndUpdate,
   957  			appTemplateSvcFn:        successAppTemplateGetSvc,
   958  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
   959  			clientFn:                successfulClientFetchForStaticDoc,
   960  		},
   961  		{
   962  			Name: "Success for Application Template and Applications webhook with Static ORD data when resources are already in db and APIs/Events versions are incremented should Update them and resync API/Event specs",
   963  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
   964  				return txGen.ThatSucceedsMultipleTimes(68)
   965  			},
   966  			tenantSvcFn:    successfulTenantSvc,
   967  			appSvcFn:       successfulAppTemplateAppSvc,
   968  			webhookSvcFn:   successfulWebhookListAppTemplate,
   969  			bundleSvcFn:    successfulBundleUpdateForStaticDocWithApplication,
   970  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
   971  			apiSvcFn: func() *automock.APIService {
   972  				apiSvc := &automock.APIService{}
   973  				apiSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixAPIs(), nil).Once()
   974  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, api1ID, *sanitizedStaticDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedStaticDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Times(2)
   975  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, api2ID, *sanitizedStaticDoc.APIResources[1], nilSpecInput, map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, map[string]string{}, []string{}, api2PreSanitizedHash, "").Return(nil).Times(2)
   976  				apiSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixAPIs(), nil).Times(5)
   977  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, api2ID).Return(nil).Times(2)
   978  
   979  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
   980  				return apiSvc
   981  			},
   982  			eventSvcFn:              successfulEventUpdateForStaticDocWithApplication,
   983  			specSvcFn:               successfulSpecRecreateAndUpdateForStaticDocWithApplication,
   984  			fetchReqFn:              successfulFetchRequestFetchAndUpdateForStaticDocForApplication,
   985  			packageSvcFn:            successfulPackageUpdateForStaticDocWithApplication,
   986  			productSvcFn:            successfulProductUpdateForStaticDocWithApplication,
   987  			vendorSvcFn:             successfulVendorUpdateForStaticDocWithApplication,
   988  			tombstoneSvcFn:          successfulTombstoneUpdateForStaticDocWithApplication,
   989  			appTemplateVersionSvcFn: successfulAppTemplateVersionListAndUpdateForApplication,
   990  			appTemplateSvcFn:        successAppTemplateGetSvc,
   991  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
   992  			clientFn:                successfulClientFetchForStaticDocOnAppTemplateWithApplications,
   993  		},
   994  		{
   995  			Name: "Success when resources are already in db and APIs/Events versions are incremented should Update them and resync API/Event specs",
   996  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
   997  				return txGen.ThatSucceedsMultipleTimes(32)
   998  			},
   999  			appSvcFn:       successfulAppGet,
  1000  			tenantSvcFn:    successfulTenantSvc,
  1001  			webhookSvcFn:   successfulTenantMappingOnlyCreation,
  1002  			webhookConvFn:  successfulWebhookConversion,
  1003  			bundleSvcFn:    successfulBundleUpdateForApplication,
  1004  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  1005  			apiSvcFn: func() *automock.APIService {
  1006  				apiSvc := &automock.APIService{}
  1007  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  1008  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
  1009  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api2ID, *sanitizedDoc.APIResources[1], nilSpecInput, map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, map[string]string{}, []string{}, api2PreSanitizedHash, "").Return(nil).Once()
  1010  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Twice()
  1011  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  1012  				return apiSvc
  1013  			},
  1014  			eventSvcFn:   successfulEventUpdate,
  1015  			specSvcFn:    successfulSpecRecreateAndUpdate,
  1016  			fetchReqFn:   successfulFetchRequestFetchAndUpdate,
  1017  			packageSvcFn: successfulPackageUpdateForApplication,
  1018  			productSvcFn: successfulProductUpdateForApplication,
  1019  			vendorSvcFn:  successfulVendorUpdateForApplication,
  1020  			tombstoneSvcFn: func() *automock.TombstoneService {
  1021  				tombstoneSvc := &automock.TombstoneService{}
  1022  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  1023  				tombstoneSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, tombstoneID, *sanitizedDoc.Tombstones[0]).Return(nil).Once()
  1024  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  1025  				return tombstoneSvc
  1026  			},
  1027  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1028  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  1029  			clientFn:                successfulClientFetch,
  1030  		},
  1031  		{
  1032  			Name: "Success when resources are already in db and APIs/Events versions are NOT incremented should Update them and refetch only failed API/Event specs",
  1033  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1034  				return txGen.ThatSucceedsMultipleTimes(32)
  1035  			},
  1036  			appSvcFn:       successfulAppGet,
  1037  			tenantSvcFn:    successfulTenantSvc,
  1038  			webhookSvcFn:   successfulTenantMappingOnlyCreation,
  1039  			webhookConvFn:  successfulWebhookConversion,
  1040  			bundleSvcFn:    successfulBundleUpdateForApplication,
  1041  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  1042  			apiSvcFn: func() *automock.APIService {
  1043  				apiSvc := &automock.APIService{}
  1044  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIsNoVersionBump(), nil).Once()
  1045  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
  1046  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api2ID, *sanitizedDoc.APIResources[1], nilSpecInput, map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, map[string]string{}, []string{}, api2PreSanitizedHash, "").Return(nil).Once()
  1047  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIsNoVersionBump(), nil).Twice()
  1048  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  1049  				return apiSvc
  1050  			},
  1051  			eventSvcFn: func() *automock.EventService {
  1052  				eventSvc := &automock.EventService{}
  1053  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEventsNoVersionBump(), nil).Once()
  1054  				eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event1ID, *sanitizedDoc.EventResources[0], nilSpecInput, []string{bundleID}, []string{}, []string{}, event1PreSanitizedHash, "").Return(nil).Once()
  1055  				eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event2ID, *sanitizedDoc.EventResources[1], nilSpecInput, []string{bundleID}, []string{}, []string{}, event2PreSanitizedHash, "").Return(nil).Once()
  1056  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEventsNoVersionBump(), nil).Twice()
  1057  				return eventSvc
  1058  			},
  1059  			specSvcFn:    successfulSpecRefetch,
  1060  			fetchReqFn:   successfulFetchRequestFetchAndUpdate,
  1061  			packageSvcFn: successfulPackageUpdateForApplication,
  1062  			productSvcFn: successfulProductUpdateForApplication,
  1063  			vendorSvcFn:  successfulVendorUpdateForApplication,
  1064  			tombstoneSvcFn: func() *automock.TombstoneService {
  1065  				tombstoneSvc := &automock.TombstoneService{}
  1066  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  1067  				tombstoneSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, tombstoneID, *sanitizedDoc.Tombstones[0]).Return(nil).Once()
  1068  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  1069  				return tombstoneSvc
  1070  			},
  1071  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1072  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  1073  			clientFn:                successfulClientFetch,
  1074  		},
  1075  		{
  1076  			Name: "Success when resources are not in db should Create them",
  1077  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1078  				return txGen.ThatSucceedsMultipleTimes(32)
  1079  			},
  1080  			appSvcFn:                successfulAppGet,
  1081  			tenantSvcFn:             successfulTenantSvc,
  1082  			webhookSvcFn:            successfulTenantMappingOnlyCreation,
  1083  			webhookConvFn:           successfulWebhookConversion,
  1084  			bundleSvcFn:             successfulBundleCreate,
  1085  			apiSvcFn:                successfulAPICreateAndDelete,
  1086  			eventSvcFn:              successfulEventCreate,
  1087  			specSvcFn:               successfulSpecCreateAndUpdate,
  1088  			fetchReqFn:              successfulFetchRequestFetchAndUpdate,
  1089  			packageSvcFn:            successfulPackageCreate,
  1090  			productSvcFn:            successfulProductCreate,
  1091  			vendorSvcFn:             successfulVendorCreate,
  1092  			tombstoneSvcFn:          successfulTombstoneCreate,
  1093  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1094  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  1095  			clientFn:                successfulClientFetch,
  1096  		},
  1097  		{
  1098  			Name: "Success when resources are not in db should Create them for a Static document",
  1099  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1100  				return txGen.ThatSucceedsMultipleTimes(35)
  1101  			},
  1102  			appSvcFn:     successfulAppTemplateNoAppsAppSvc,
  1103  			webhookSvcFn: successfulWebhookListAppTemplate,
  1104  			bundleSvcFn:  successfulBundleCreateForStaticDoc,
  1105  			apiSvcFn: func() *automock.APIService {
  1106  				apiSvc := &automock.APIService{}
  1107  
  1108  				apiSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, nil).Once()
  1109  				apiSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, nil).Once()
  1110  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, appTemplateVersionID, nilBundleID, str.Ptr(packageID), *sanitizedStaticDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedStaticDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return("", nil).Once()
  1111  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, appTemplateVersionID, nilBundleID, str.Ptr(packageID), *sanitizedStaticDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, mock.Anything, "").Return("", nil).Once()
  1112  				apiSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixAPIs(), nil).Once()
  1113  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.ApplicationTemplateVersion, api2ID).Return(nil).Once()
  1114  				return apiSvc
  1115  			},
  1116  			eventSvcFn:              successfulEventCreateForStaticDoc,
  1117  			specSvcFn:               successfulSpecCreateAndUpdateForStaticDoc,
  1118  			fetchReqFn:              successfulFetchRequestFetchAndUpdateForStaticDoc,
  1119  			packageSvcFn:            successfulPackageCreateForStaticDoc,
  1120  			productSvcFn:            successfulProductCreateForStaticDoc,
  1121  			vendorSvcFn:             successfulVendorCreateForStaticDoc,
  1122  			tombstoneSvcFn:          successfulTombstoneCreateForStaticDoc,
  1123  			appTemplateVersionSvcFn: successfulAppTemplateVersionForCreation,
  1124  			appTemplateSvcFn:        successAppTemplateGetSvc,
  1125  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  1126  			clientFn:                successfulClientFetchForStaticDoc,
  1127  		},
  1128  		{
  1129  			Name: "Error when creating Application Template Version based on the doc",
  1130  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1131  				return txGen.ThatSucceedsMultipleTimesAndCommitsMultipleTimes(4, 3)
  1132  			},
  1133  			webhookSvcFn: successfulWebhookListAppTemplate,
  1134  			appTemplateVersionSvcFn: func() *automock.ApplicationTemplateVersionService {
  1135  				svc := &automock.ApplicationTemplateVersionService{}
  1136  				svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return([]*model.ApplicationTemplateVersion{}, nil).Once()
  1137  				svc.On("Create", txtest.CtxWithDBMatcher(), appTemplateID, fixAppTemplateVersionInput()).Return("", testErr).Once()
  1138  				return svc
  1139  			},
  1140  			appTemplateSvcFn:    successAppTemplateGetSvc,
  1141  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1142  			clientFn:            successfulClientFetchForStaticDoc,
  1143  		},
  1144  		{
  1145  			Name: "Error when getting Application Template from the webhook ObjectID",
  1146  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1147  				return txGen.ThatSucceedsMultipleTimesAndCommitsMultipleTimes(2, 1)
  1148  			},
  1149  			webhookSvcFn: successfulWebhookListAppTemplate,
  1150  			appTemplateSvcFn: func() *automock.ApplicationTemplateService {
  1151  				svc := &automock.ApplicationTemplateService{}
  1152  				svc.On("Get", txtest.CtxWithDBMatcher(), appTemplateID).Return(nil, testErr)
  1153  				return svc
  1154  			},
  1155  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1156  		},
  1157  		{
  1158  			Name: "Error when listing Application Template Version by app template ID",
  1159  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1160  				return txGen.ThatSucceedsMultipleTimesAndCommitsMultipleTimes(3, 2)
  1161  			},
  1162  			webhookSvcFn: successfulWebhookListAppTemplate,
  1163  			appTemplateVersionSvcFn: func() *automock.ApplicationTemplateVersionService {
  1164  				svc := &automock.ApplicationTemplateVersionService{}
  1165  				svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(nil, testErr).Once()
  1166  				return svc
  1167  			},
  1168  			appTemplateSvcFn:    successAppTemplateGetSvc,
  1169  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1170  			clientFn:            successfulClientFetchForStaticDoc,
  1171  		},
  1172  		{
  1173  			Name: "Error when fetching the Application Template for the given dynamic doc",
  1174  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1175  				persistTx := &persistenceautomock.PersistenceTx{}
  1176  				persistTx.On("Commit").Return(nil).Times(5)
  1177  
  1178  				transact := &persistenceautomock.Transactioner{}
  1179  				transact.On("Begin").Return(persistTx, nil).Times(6)
  1180  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(5)
  1181  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1182  				return persistTx, transact
  1183  			},
  1184  			webhookSvcFn: successfulWebhookListAppTemplate,
  1185  			appTemplateVersionSvcFn: func() *automock.ApplicationTemplateVersionService {
  1186  				svc := &automock.ApplicationTemplateVersionService{}
  1187  				svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return([]*model.ApplicationTemplateVersion{}, nil).Once()
  1188  				svc.On("Create", txtest.CtxWithDBMatcher(), appTemplateID, fixAppTemplateVersionInput()).Return(appTemplateVersionID, nil)
  1189  				svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixAppTemplateVersions(), nil).Once()
  1190  				svc.On("GetByAppTemplateIDAndVersion", txtest.CtxWithDBMatcher(), appTemplateID, appTemplateVersionValue).Return(nil, testErr)
  1191  				return svc
  1192  			},
  1193  			appTemplateSvcFn:    successAppTemplateGetSvc,
  1194  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1195  			clientFn:            successfulClientFetchForStaticDoc,
  1196  		},
  1197  		{
  1198  			Name: "Error when fetching the Application Template for the given dynamic doc for a second time",
  1199  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1200  				persistTx := &persistenceautomock.PersistenceTx{}
  1201  				persistTx.On("Commit").Return(nil).Times(7)
  1202  
  1203  				transact := &persistenceautomock.Transactioner{}
  1204  				transact.On("Begin").Return(persistTx, nil).Times(8)
  1205  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(7)
  1206  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1207  				return persistTx, transact
  1208  			},
  1209  			webhookSvcFn: successfulWebhookListAppTemplate,
  1210  			bundleSvcFn: func() *automock.BundleService {
  1211  				bundlesSvc := &automock.BundleService{}
  1212  				bundlesSvc.On("ListByApplicationTemplateVersionIDNoPaging", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixBundles(), nil).Once()
  1213  				return bundlesSvc
  1214  			},
  1215  			apiSvcFn: func() *automock.APIService {
  1216  				apiSvc := &automock.APIService{}
  1217  				apiSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixAPIs(), nil).Once()
  1218  				return apiSvc
  1219  			},
  1220  			eventSvcFn: func() *automock.EventService {
  1221  				eventSvc := &automock.EventService{}
  1222  				eventSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixEvents(), nil).Once()
  1223  				return eventSvc
  1224  			},
  1225  			packageSvcFn: func() *automock.PackageService {
  1226  				packagesSvc := &automock.PackageService{}
  1227  				packagesSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixPackagesWithHash(), nil).Once()
  1228  				return packagesSvc
  1229  			},
  1230  
  1231  			appTemplateVersionSvcFn: func() *automock.ApplicationTemplateVersionService {
  1232  				svc := &automock.ApplicationTemplateVersionService{}
  1233  				svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return([]*model.ApplicationTemplateVersion{}, nil).Once()
  1234  				svc.On("Create", txtest.CtxWithDBMatcher(), appTemplateID, fixAppTemplateVersionInput()).Return(appTemplateVersionID, nil)
  1235  				svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixAppTemplateVersions(), nil).Once()
  1236  				svc.On("GetByAppTemplateIDAndVersion", txtest.CtxWithDBMatcher(), appTemplateID, appTemplateVersionValue).Return(fixAppTemplateVersion(), nil).Once()
  1237  				svc.On("GetByAppTemplateIDAndVersion", txtest.CtxWithDBMatcher(), appTemplateID, appTemplateVersionValue).Return(nil, testErr).Once()
  1238  				return svc
  1239  			},
  1240  			appTemplateSvcFn:    successAppTemplateGetSvc,
  1241  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1242  			clientFn:            successfulClientFetchForStaticDoc,
  1243  		},
  1244  		{
  1245  			Name: "Error when fetching the packages from the DB",
  1246  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1247  				persistTx := &persistenceautomock.PersistenceTx{}
  1248  				persistTx.On("Commit").Return(nil).Times(6)
  1249  
  1250  				transact := &persistenceautomock.Transactioner{}
  1251  				transact.On("Begin").Return(persistTx, nil).Times(7)
  1252  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(6)
  1253  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1254  				return persistTx, transact
  1255  			},
  1256  			webhookSvcFn: successfulWebhookListAppTemplate,
  1257  			apiSvcFn: func() *automock.APIService {
  1258  				apiSvc := &automock.APIService{}
  1259  				apiSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixAPIs(), nil).Once()
  1260  				return apiSvc
  1261  			},
  1262  			eventSvcFn: func() *automock.EventService {
  1263  				eventSvc := &automock.EventService{}
  1264  				eventSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixEvents(), nil).Once()
  1265  				return eventSvc
  1266  			},
  1267  			packageSvcFn: func() *automock.PackageService {
  1268  				packagesSvc := &automock.PackageService{}
  1269  				packagesSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, testErr).Once()
  1270  				return packagesSvc
  1271  			},
  1272  
  1273  			appTemplateVersionSvcFn: func() *automock.ApplicationTemplateVersionService {
  1274  				svc := &automock.ApplicationTemplateVersionService{}
  1275  				svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return([]*model.ApplicationTemplateVersion{}, nil).Once()
  1276  				svc.On("Create", txtest.CtxWithDBMatcher(), appTemplateID, fixAppTemplateVersionInput()).Return(appTemplateVersionID, nil)
  1277  				svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixAppTemplateVersions(), nil).Once()
  1278  				svc.On("GetByAppTemplateIDAndVersion", txtest.CtxWithDBMatcher(), appTemplateID, appTemplateVersionValue).Return(fixAppTemplateVersion(), nil).Once()
  1279  				return svc
  1280  			},
  1281  			appTemplateSvcFn:    successAppTemplateGetSvc,
  1282  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1283  			clientFn:            successfulClientFetchForStaticDoc,
  1284  		},
  1285  		{
  1286  			Name: "Error when fetching the bundles from the DB",
  1287  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1288  				persistTx := &persistenceautomock.PersistenceTx{}
  1289  				persistTx.On("Commit").Return(nil).Times(6)
  1290  
  1291  				transact := &persistenceautomock.Transactioner{}
  1292  				transact.On("Begin").Return(persistTx, nil).Times(7)
  1293  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(6)
  1294  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1295  				return persistTx, transact
  1296  			},
  1297  			webhookSvcFn: successfulWebhookListAppTemplate,
  1298  			bundleSvcFn: func() *automock.BundleService {
  1299  				bundlesSvc := &automock.BundleService{}
  1300  				bundlesSvc.On("ListByApplicationTemplateVersionIDNoPaging", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(nil, testErr).Once()
  1301  				return bundlesSvc
  1302  			},
  1303  			apiSvcFn: func() *automock.APIService {
  1304  				apiSvc := &automock.APIService{}
  1305  				apiSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixAPIs(), nil).Once()
  1306  				return apiSvc
  1307  			},
  1308  			eventSvcFn: func() *automock.EventService {
  1309  				eventSvc := &automock.EventService{}
  1310  				eventSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixEvents(), nil).Once()
  1311  				return eventSvc
  1312  			},
  1313  			packageSvcFn: func() *automock.PackageService {
  1314  				packagesSvc := &automock.PackageService{}
  1315  				packagesSvc.On("ListByApplicationTemplateVersionID", txtest.CtxWithDBMatcher(), appTemplateVersionID).Return(fixPackages(), nil).Once()
  1316  				return packagesSvc
  1317  			},
  1318  			appTemplateVersionSvcFn: func() *automock.ApplicationTemplateVersionService {
  1319  				svc := &automock.ApplicationTemplateVersionService{}
  1320  				svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return([]*model.ApplicationTemplateVersion{}, nil).Once()
  1321  				svc.On("Create", txtest.CtxWithDBMatcher(), appTemplateID, fixAppTemplateVersionInput()).Return(appTemplateVersionID, nil)
  1322  				svc.On("ListByAppTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixAppTemplateVersions(), nil).Once()
  1323  				svc.On("GetByAppTemplateIDAndVersion", txtest.CtxWithDBMatcher(), appTemplateID, appTemplateVersionValue).Return(fixAppTemplateVersion(), nil).Once()
  1324  				return svc
  1325  			},
  1326  			appTemplateSvcFn:    successAppTemplateGetSvc,
  1327  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1328  			clientFn:            successfulClientFetchForStaticDoc,
  1329  		},
  1330  		{
  1331  			Name: "Success when there is ORD webhook on app template",
  1332  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1333  				return txGen.ThatSucceedsMultipleTimes(37)
  1334  			},
  1335  			appSvcFn: func() *automock.ApplicationService {
  1336  				appSvc := &automock.ApplicationService{}
  1337  				appSvc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixApplications(), nil).Once()
  1338  				appSvc.On("Get", txtest.CtxWithDBMatcher(), appID).Return(fixApplications()[0], nil).Once()
  1339  				return appSvc
  1340  			},
  1341  			tenantSvcFn:    successfulTenantSvc,
  1342  			webhookConvFn:  successfulWebhookConversion,
  1343  			webhookSvcFn:   successfulAppTemplateTenantMappingOnlyCreation,
  1344  			bundleSvcFn:    successfulBundleUpdateForApplication,
  1345  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  1346  			apiSvcFn: func() *automock.APIService {
  1347  				apiSvc := &automock.APIService{}
  1348  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  1349  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
  1350  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api2ID, *sanitizedDoc.APIResources[1], nilSpecInput, map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, map[string]string{}, []string{}, api2PreSanitizedHash, "").Return(nil).Once()
  1351  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Twice()
  1352  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  1353  				return apiSvc
  1354  			},
  1355  			eventSvcFn:   successfulEventUpdate,
  1356  			specSvcFn:    successfulSpecRecreateAndUpdate,
  1357  			fetchReqFn:   successfulFetchRequestFetchAndUpdate,
  1358  			packageSvcFn: successfulPackageUpdateForApplication,
  1359  			productSvcFn: successfulProductUpdateForApplication,
  1360  			vendorSvcFn:  successfulVendorUpdateForApplication,
  1361  			tombstoneSvcFn: func() *automock.TombstoneService {
  1362  				tombstoneSvc := &automock.TombstoneService{}
  1363  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  1364  				tombstoneSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, tombstoneID, *sanitizedDoc.Tombstones[0]).Return(nil).Once()
  1365  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  1366  				return tombstoneSvc
  1367  			},
  1368  			appTemplateSvcFn:        successAppTemplateGetSvc,
  1369  			appTemplateVersionSvcFn: successfulAppTemplateVersionListForAppTemplateFlow,
  1370  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  1371  			clientFn: func() *automock.Client {
  1372  				client := &automock.Client{}
  1373  				testResources := ord.Resource{
  1374  					Type:     resource.Application,
  1375  					ID:       testApplication.ID,
  1376  					Name:     testApplication.Name,
  1377  					ParentID: &appTemplateID,
  1378  				}
  1379  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResourceForAppTemplate, testWebhookForAppTemplate).Return(ord.Documents{fixORDDocument()}, baseURL, nil).Once()
  1380  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResources, testWebhookForAppTemplate).Return(ord.Documents{fixORDDocument()}, baseURL, nil).Once()
  1381  				return client
  1382  			},
  1383  		},
  1384  		{
  1385  			Name: "Error when synchronizing global resources from global registry should get them from DB and proceed with the rest of the sync",
  1386  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1387  				return txGen.ThatSucceedsMultipleTimes(32)
  1388  			},
  1389  			appSvcFn:                successfulAppGet,
  1390  			tenantSvcFn:             successfulTenantSvc,
  1391  			webhookSvcFn:            successfulTenantMappingOnlyCreation,
  1392  			webhookConvFn:           successfulWebhookConversion,
  1393  			bundleSvcFn:             successfulBundleCreate,
  1394  			apiSvcFn:                successfulAPICreateAndDelete,
  1395  			eventSvcFn:              successfulEventCreate,
  1396  			specSvcFn:               successfulSpecCreateAndUpdate,
  1397  			fetchReqFn:              successfulFetchRequestFetchAndUpdate,
  1398  			packageSvcFn:            successfulPackageCreate,
  1399  			productSvcFn:            successfulProductCreate,
  1400  			vendorSvcFn:             successfulVendorCreate,
  1401  			tombstoneSvcFn:          successfulTombstoneCreate,
  1402  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1403  			globalRegistrySvcFn: func() *automock.GlobalRegistryService {
  1404  				globalRegistrySvcFn := &automock.GlobalRegistryService{}
  1405  				globalRegistrySvcFn.On("SyncGlobalResources", context.TODO()).Return(nil, errors.New("error")).Once()
  1406  				globalRegistrySvcFn.On("ListGlobalResources", context.TODO()).Return(map[string]bool{ord.SapVendor: true}, nil).Once()
  1407  				return globalRegistrySvcFn
  1408  			},
  1409  			clientFn: successfulClientFetch,
  1410  		},
  1411  		{
  1412  			Name: "Error when synchronizing global resources from global registry and get them from DB should proceed with the rest of the sync",
  1413  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1414  				return txGen.ThatSucceedsMultipleTimes(32)
  1415  			},
  1416  			appSvcFn:                successfulAppGet,
  1417  			tenantSvcFn:             successfulTenantSvc,
  1418  			webhookSvcFn:            successfulTenantMappingOnlyCreation,
  1419  			webhookConvFn:           successfulWebhookConversion,
  1420  			bundleSvcFn:             successfulBundleCreate,
  1421  			apiSvcFn:                successfulAPICreateAndDelete,
  1422  			eventSvcFn:              successfulEventCreate,
  1423  			specSvcFn:               successfulSpecCreateAndUpdate,
  1424  			fetchReqFn:              successfulFetchRequestFetchAndUpdate,
  1425  			packageSvcFn:            successfulPackageCreate,
  1426  			productSvcFn:            successfulProductCreate,
  1427  			vendorSvcFn:             successfulVendorCreate,
  1428  			tombstoneSvcFn:          successfulTombstoneCreate,
  1429  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1430  			globalRegistrySvcFn: func() *automock.GlobalRegistryService {
  1431  				globalRegistrySvcFn := &automock.GlobalRegistryService{}
  1432  				globalRegistrySvcFn.On("SyncGlobalResources", context.TODO()).Return(nil, errors.New("error")).Once()
  1433  				globalRegistrySvcFn.On("ListGlobalResources", context.TODO()).Return(nil, errors.New("error")).Once()
  1434  				return globalRegistrySvcFn
  1435  			},
  1436  			clientFn: successfulClientFetch,
  1437  		},
  1438  		{
  1439  			Name:            "Returns error when list by webhook type fails",
  1440  			TransactionerFn: txGen.ThatDoesntExpectCommit,
  1441  			webhookSvcFn: func() *automock.WebhookService {
  1442  				whSvc := &automock.WebhookService{}
  1443  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(nil, testErr).Once()
  1444  				return whSvc
  1445  			},
  1446  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1447  			ExpectedErr:         testErr,
  1448  		},
  1449  		{
  1450  			Name:                "Returns error when transaction opening fails",
  1451  			TransactionerFn:     txGen.ThatFailsOnBegin,
  1452  			ExpectedErr:         testErr,
  1453  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1454  		},
  1455  		{
  1456  			Name:                "Returns error when first transaction commit fails",
  1457  			TransactionerFn:     txGen.ThatFailsOnCommit,
  1458  			webhookSvcFn:        successfulWebhookList,
  1459  			ExpectedErr:         testErr,
  1460  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1461  		},
  1462  		{
  1463  			Name: "Returns error when second transaction begin fails",
  1464  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1465  				persistTx := &persistenceautomock.PersistenceTx{}
  1466  				persistTx.On("Commit").Return(nil).Once()
  1467  
  1468  				transact := &persistenceautomock.Transactioner{}
  1469  				transact.On("Begin").Return(persistTx, nil).Once()
  1470  				transact.On("Begin").Return(persistTx, testErr).Once()
  1471  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Once()
  1472  				return persistTx, transact
  1473  			},
  1474  			webhookSvcFn:        successfulWebhookList,
  1475  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1476  		},
  1477  		{
  1478  			Name: "Returns error when second transaction commit fails",
  1479  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1480  				persistTx := &persistenceautomock.PersistenceTx{}
  1481  				persistTx.On("Commit").Return(nil).Once()
  1482  				persistTx.On("Commit").Return(testErr).Once()
  1483  
  1484  				transact := &persistenceautomock.Transactioner{}
  1485  				transact.On("Begin").Return(persistTx, nil).Twice()
  1486  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Twice()
  1487  				return persistTx, transact
  1488  			},
  1489  			appSvcFn:            successfulAppGet,
  1490  			tenantSvcFn:         successfulTenantSvc,
  1491  			webhookSvcFn:        successfulWebhookList,
  1492  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1493  		},
  1494  		{
  1495  			Name: "Returns error when second transaction begin fails when there is app template ord webhook",
  1496  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1497  				persistTx := &persistenceautomock.PersistenceTx{}
  1498  				persistTx.On("Commit").Return(nil).Once()
  1499  
  1500  				transact := &persistenceautomock.Transactioner{}
  1501  				transact.On("Begin").Return(persistTx, nil).Once()
  1502  				transact.On("Begin").Return(persistTx, testErr).Once()
  1503  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Once()
  1504  				return persistTx, transact
  1505  			},
  1506  			webhookSvcFn: func() *automock.WebhookService {
  1507  				whSvc := &automock.WebhookService{}
  1508  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixOrdWebhooksForAppTemplate(), nil).Once()
  1509  				return whSvc
  1510  			},
  1511  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1512  		},
  1513  		{
  1514  			Name: "Returns error when get internal tenant id fails",
  1515  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1516  				persistTx := &persistenceautomock.PersistenceTx{}
  1517  				persistTx.On("Commit").Return(nil).Once()
  1518  
  1519  				transact := &persistenceautomock.Transactioner{}
  1520  				transact.On("Begin").Return(persistTx, nil).Twice()
  1521  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Once()
  1522  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1523  				return persistTx, transact
  1524  			},
  1525  			tenantSvcFn: func() *automock.TenantService {
  1526  				tenantSvc := &automock.TenantService{}
  1527  				tenantSvc.On("GetLowestOwnerForResource", txtest.CtxWithDBMatcher(), resource.Application, appID).Return("", testErr).Once()
  1528  				return tenantSvc
  1529  			},
  1530  			webhookSvcFn:        successfulWebhookList,
  1531  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1532  		},
  1533  		{
  1534  			Name: "Returns error when get tenant fails",
  1535  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1536  				persistTx := &persistenceautomock.PersistenceTx{}
  1537  				persistTx.On("Commit").Return(nil).Once()
  1538  
  1539  				transact := &persistenceautomock.Transactioner{}
  1540  				transact.On("Begin").Return(persistTx, nil).Twice()
  1541  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Once()
  1542  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1543  				return persistTx, transact
  1544  			},
  1545  			tenantSvcFn: func() *automock.TenantService {
  1546  				tenantSvc := &automock.TenantService{}
  1547  				tenantSvc.On("GetLowestOwnerForResource", txtest.CtxWithDBMatcher(), resource.Application, appID).Return(tenantID, nil).Once()
  1548  				tenantSvc.On("GetTenantByID", txtest.CtxWithDBMatcher(), tenantID).Return(nil, testErr).Once()
  1549  				return tenantSvc
  1550  			},
  1551  			webhookSvcFn:        successfulWebhookList,
  1552  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1553  		},
  1554  		{
  1555  			Name: "Returns error when application locking fails",
  1556  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1557  				persistTx := &persistenceautomock.PersistenceTx{}
  1558  				persistTx.On("Commit").Return(nil).Once()
  1559  
  1560  				transact := &persistenceautomock.Transactioner{}
  1561  				transact.On("Begin").Return(persistTx, nil).Twice()
  1562  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Once()
  1563  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1564  				return persistTx, transact
  1565  			},
  1566  			tenantSvcFn: successfulTenantSvc,
  1567  			appSvcFn: func() *automock.ApplicationService {
  1568  				appSvc := &automock.ApplicationService{}
  1569  				appSvc.On("Get", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  1570  				return appSvc
  1571  			},
  1572  			webhookSvcFn:        successfulWebhookList,
  1573  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1574  		},
  1575  		{
  1576  			Name: "Does not resync resources when event list fails",
  1577  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1578  				persistTx := &persistenceautomock.PersistenceTx{}
  1579  				persistTx.On("Commit").Return(nil).Times(5)
  1580  
  1581  				transact := &persistenceautomock.Transactioner{}
  1582  				transact.On("Begin").Return(persistTx, nil).Times(5)
  1583  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(4)
  1584  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1585  				return persistTx, transact
  1586  			},
  1587  			webhookSvcFn: successfulWebhookList,
  1588  			clientFn:     successfulClientFetch,
  1589  			appSvcFn:     successfulAppGet,
  1590  			tenantSvcFn:  successfulTenantSvc,
  1591  			apiSvcFn: func() *automock.APIService {
  1592  				apiSvc := &automock.APIService{}
  1593  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  1594  				return apiSvc
  1595  			},
  1596  			eventSvcFn: func() *automock.EventService {
  1597  				eventSvc := &automock.EventService{}
  1598  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  1599  				return eventSvc
  1600  			},
  1601  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1602  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  1603  		},
  1604  		{
  1605  			Name: "Does not resync resources when api list fails",
  1606  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1607  				persistTx := &persistenceautomock.PersistenceTx{}
  1608  				persistTx.On("Commit").Return(nil).Times(5)
  1609  
  1610  				transact := &persistenceautomock.Transactioner{}
  1611  				transact.On("Begin").Return(persistTx, nil).Times(5)
  1612  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(4)
  1613  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1614  				return persistTx, transact
  1615  			},
  1616  			webhookSvcFn: successfulWebhookList,
  1617  			clientFn:     successfulClientFetch,
  1618  			appSvcFn:     successfulAppGet,
  1619  			tenantSvcFn:  successfulTenantSvc,
  1620  			apiSvcFn: func() *automock.APIService {
  1621  				apiSvc := &automock.APIService{}
  1622  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  1623  				return apiSvc
  1624  			},
  1625  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  1626  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1627  		},
  1628  		{
  1629  			Name: "Returns error when list all applications by app template id fails",
  1630  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1631  				persistTx := &persistenceautomock.PersistenceTx{}
  1632  				persistTx.On("Commit").Return(nil).Times(5)
  1633  
  1634  				transact := &persistenceautomock.Transactioner{}
  1635  				transact.On("Begin").Return(persistTx, nil).Times(6)
  1636  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(5)
  1637  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1638  				return persistTx, transact
  1639  			},
  1640  			appSvcFn: func() *automock.ApplicationService {
  1641  				appSvc := &automock.ApplicationService{}
  1642  				appSvc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(nil, testErr).Once()
  1643  				return appSvc
  1644  			},
  1645  			webhookSvcFn: func() *automock.WebhookService {
  1646  				whSvc := &automock.WebhookService{}
  1647  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixOrdWebhooksForAppTemplate(), nil).Once()
  1648  				return whSvc
  1649  			},
  1650  			appTemplateSvcFn: successAppTemplateGetSvc,
  1651  			clientFn: func() *automock.Client {
  1652  				client := &automock.Client{}
  1653  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResourceForAppTemplate, testWebhookForAppTemplate).Return(ord.Documents{fixORDDocument()}, baseURL, nil)
  1654  				return client
  1655  			},
  1656  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1657  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  1658  		},
  1659  		{
  1660  			Name: "Returns error when get internal tenant id fails for ORD webhook for app template",
  1661  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1662  				persistTx := &persistenceautomock.PersistenceTx{}
  1663  				persistTx.On("Commit").Return(nil).Times(6)
  1664  
  1665  				transact := &persistenceautomock.Transactioner{}
  1666  				transact.On("Begin").Return(persistTx, nil).Times(7)
  1667  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(6)
  1668  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1669  				return persistTx, transact
  1670  			},
  1671  			appSvcFn: func() *automock.ApplicationService {
  1672  				appSvc := &automock.ApplicationService{}
  1673  				appSvc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixApplications(), nil).Once()
  1674  				return appSvc
  1675  			},
  1676  			tenantSvcFn: func() *automock.TenantService {
  1677  				tenantSvc := &automock.TenantService{}
  1678  				tenantSvc.On("GetLowestOwnerForResource", txtest.CtxWithDBMatcher(), resource.Application, appID).Return("", testErr).Once()
  1679  				return tenantSvc
  1680  			},
  1681  			webhookSvcFn: func() *automock.WebhookService {
  1682  				whSvc := &automock.WebhookService{}
  1683  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixOrdWebhooksForAppTemplate(), nil).Once()
  1684  				return whSvc
  1685  			},
  1686  			appTemplateSvcFn: successAppTemplateGetSvc,
  1687  			clientFn: func() *automock.Client {
  1688  				client := &automock.Client{}
  1689  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResourceForAppTemplate, testWebhookForAppTemplate).Return(ord.Documents{fixORDDocument()}, baseURL, nil)
  1690  				return client
  1691  			},
  1692  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1693  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  1694  		},
  1695  		{
  1696  			Name: "Returns error when get tenant id fails for ORD webhook for app template",
  1697  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1698  				persistTx := &persistenceautomock.PersistenceTx{}
  1699  				persistTx.On("Commit").Return(nil).Times(6)
  1700  
  1701  				transact := &persistenceautomock.Transactioner{}
  1702  				transact.On("Begin").Return(persistTx, nil).Times(7)
  1703  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(6)
  1704  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1705  				return persistTx, transact
  1706  			},
  1707  			appSvcFn: func() *automock.ApplicationService {
  1708  				appSvc := &automock.ApplicationService{}
  1709  				appSvc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixApplications(), nil).Once()
  1710  				return appSvc
  1711  			},
  1712  			tenantSvcFn: func() *automock.TenantService {
  1713  				tenantSvc := &automock.TenantService{}
  1714  				tenantSvc.On("GetLowestOwnerForResource", txtest.CtxWithDBMatcher(), resource.Application, appID).Return(tenantID, nil).Once()
  1715  				tenantSvc.On("GetTenantByID", txtest.CtxWithDBMatcher(), tenantID).Return(nil, testErr).Once()
  1716  				return tenantSvc
  1717  			},
  1718  			webhookSvcFn: func() *automock.WebhookService {
  1719  				whSvc := &automock.WebhookService{}
  1720  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixOrdWebhooksForAppTemplate(), nil).Once()
  1721  				return whSvc
  1722  			},
  1723  			appTemplateSvcFn: successAppTemplateGetSvc,
  1724  			clientFn: func() *automock.Client {
  1725  				client := &automock.Client{}
  1726  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResourceForAppTemplate, testWebhookForAppTemplate).Return(ord.Documents{fixORDDocument()}, baseURL, nil)
  1727  				return client
  1728  			},
  1729  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1730  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  1731  		},
  1732  		{
  1733  			Name: "Returns error when application locking fails for ORD webhook for app template",
  1734  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1735  				persistTx := &persistenceautomock.PersistenceTx{}
  1736  				persistTx.On("Commit").Return(nil).Times(6)
  1737  
  1738  				transact := &persistenceautomock.Transactioner{}
  1739  				transact.On("Begin").Return(persistTx, nil).Times(7)
  1740  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(6)
  1741  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1742  				return persistTx, transact
  1743  			},
  1744  			appSvcFn: func() *automock.ApplicationService {
  1745  				appSvc := &automock.ApplicationService{}
  1746  				appSvc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixApplications(), nil).Once()
  1747  				appSvc.On("Get", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  1748  				return appSvc
  1749  			},
  1750  			tenantSvcFn: successfulTenantSvc,
  1751  			webhookSvcFn: func() *automock.WebhookService {
  1752  				whSvc := &automock.WebhookService{}
  1753  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixOrdWebhooksForAppTemplate(), nil).Once()
  1754  				return whSvc
  1755  			},
  1756  			appTemplateSvcFn: successAppTemplateGetSvc,
  1757  			clientFn: func() *automock.Client {
  1758  				client := &automock.Client{}
  1759  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResourceForAppTemplate, testWebhookForAppTemplate).Return(ord.Documents{fixORDDocument()}, baseURL, nil)
  1760  				return client
  1761  			},
  1762  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1763  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  1764  		},
  1765  		{
  1766  			Name: "Skips webhook when ORD documents fetch fails",
  1767  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1768  				persistTx := &persistenceautomock.PersistenceTx{}
  1769  				persistTx.On("Commit").Return(nil).Twice()
  1770  
  1771  				transact := &persistenceautomock.Transactioner{}
  1772  				transact.On("Begin").Return(persistTx, nil).Twice()
  1773  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Once()
  1774  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1775  				return persistTx, transact
  1776  			},
  1777  			appSvcFn:     successfulAppGet,
  1778  			tenantSvcFn:  successfulTenantSvc,
  1779  			webhookSvcFn: successfulWebhookList,
  1780  			clientFn: func() *automock.Client {
  1781  				client := &automock.Client{}
  1782  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(nil, "", testErr)
  1783  				return client
  1784  			},
  1785  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1786  		},
  1787  		{
  1788  			Name: "Update application local tenant id when ord local id is unique and application does not have local tenant id",
  1789  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1790  				return txGen.ThatSucceedsMultipleTimes(32)
  1791  			},
  1792  			appSvcFn: func() *automock.ApplicationService {
  1793  				appSvc := &automock.ApplicationService{}
  1794  				appSvc.On("Get", txtest.CtxWithDBMatcher(), appID).Return(fixApplications()[0], nil).Once()
  1795  				appSvc.On("Update", txtest.CtxWithDBMatcher(), appID, model.ApplicationUpdateInput{LocalTenantID: str.Ptr("ordLocalID")}).Return(nil).Once()
  1796  				return appSvc
  1797  			},
  1798  			tenantSvcFn:    successfulTenantSvc,
  1799  			webhookSvcFn:   successfulTenantMappingOnlyCreation,
  1800  			webhookConvFn:  successfulWebhookConversion,
  1801  			bundleSvcFn:    successfulBundleUpdateForApplication,
  1802  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  1803  			apiSvcFn: func() *automock.APIService {
  1804  				apiSvc := &automock.APIService{}
  1805  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  1806  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
  1807  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api2ID, *sanitizedDoc.APIResources[1], nilSpecInput, map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, map[string]string{}, []string{}, api2PreSanitizedHash, "").Return(nil).Once()
  1808  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Twice()
  1809  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  1810  				return apiSvc
  1811  			},
  1812  			eventSvcFn:   successfulEventUpdate,
  1813  			specSvcFn:    successfulSpecRecreateAndUpdate,
  1814  			fetchReqFn:   successfulFetchRequestFetchAndUpdate,
  1815  			packageSvcFn: successfulPackageUpdateForApplication,
  1816  			productSvcFn: successfulProductUpdateForApplication,
  1817  			vendorSvcFn:  successfulVendorUpdateForApplication,
  1818  			tombstoneSvcFn: func() *automock.TombstoneService {
  1819  				tombstoneSvc := &automock.TombstoneService{}
  1820  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  1821  				tombstoneSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, tombstoneID, *sanitizedDoc.Tombstones[0]).Return(nil).Once()
  1822  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  1823  				return tombstoneSvc
  1824  			},
  1825  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1826  			clientFn: func() *automock.Client {
  1827  				client := &automock.Client{}
  1828  				doc := fixORDDocument()
  1829  				doc.DescribedSystemInstance.LocalTenantID = str.Ptr("ordLocalID")
  1830  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, baseURL, nil)
  1831  				return client
  1832  			},
  1833  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1834  		},
  1835  		{
  1836  			Name: "Fails to update application local tenant id when ord local id is unique and application does not have local tenant id",
  1837  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1838  				persistTx := &persistenceautomock.PersistenceTx{}
  1839  				persistTx.On("Commit").Return(nil).Times(5)
  1840  
  1841  				transact := &persistenceautomock.Transactioner{}
  1842  				transact.On("Begin").Return(persistTx, nil).Times(5)
  1843  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(4)
  1844  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  1845  				return persistTx, transact
  1846  			},
  1847  			appSvcFn: func() *automock.ApplicationService {
  1848  				appSvc := &automock.ApplicationService{}
  1849  				appSvc.On("Get", txtest.CtxWithDBMatcher(), appID).Return(fixApplications()[0], nil).Once()
  1850  				appSvc.On("Update", txtest.CtxWithDBMatcher(), appID, model.ApplicationUpdateInput{LocalTenantID: str.Ptr("ordLocalID")}).Return(testErr).Once()
  1851  				return appSvc
  1852  			},
  1853  			tenantSvcFn:  successfulTenantSvc,
  1854  			webhookSvcFn: successfulWebhookList,
  1855  			bundleSvcFn: func() *automock.BundleService {
  1856  				bundlesSvc := &automock.BundleService{}
  1857  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(fixBundles(), nil).Once()
  1858  				return bundlesSvc
  1859  			},
  1860  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  1861  			apiSvcFn: func() *automock.APIService {
  1862  				apiSvc := &automock.APIService{}
  1863  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  1864  				return apiSvc
  1865  			},
  1866  			eventSvcFn: func() *automock.EventService {
  1867  				eventSvc := &automock.EventService{}
  1868  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  1869  				return eventSvc
  1870  			},
  1871  			packageSvcFn: func() *automock.PackageService {
  1872  				packagesSvc := &automock.PackageService{}
  1873  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixPackagesWithHash(), nil).Once()
  1874  				return packagesSvc
  1875  			},
  1876  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1877  			clientFn: func() *automock.Client {
  1878  				client := &automock.Client{}
  1879  				doc := fixORDDocument()
  1880  				doc.DescribedSystemInstance.LocalTenantID = str.Ptr("ordLocalID")
  1881  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, baseURL, nil)
  1882  				return client
  1883  			},
  1884  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1885  		},
  1886  		{
  1887  			Name: "Resync resources for invalid ORD documents when event resource name is empty",
  1888  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1889  				persistTx := &persistenceautomock.PersistenceTx{}
  1890  				persistTx.On("Commit").Return(nil).Times(32)
  1891  
  1892  				transact := &persistenceautomock.Transactioner{}
  1893  				transact.On("Begin").Return(persistTx, nil).Times(31)
  1894  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(31)
  1895  
  1896  				return persistTx, transact
  1897  			},
  1898  			appSvcFn:            successfulAppGet,
  1899  			tenantSvcFn:         successfulTenantSvc,
  1900  			webhookSvcFn:        successfulTenantMappingOnlyCreation,
  1901  			webhookConvFn:       successfulWebhookConversion,
  1902  			bundleSvcFn:         successfulBundleCreate,
  1903  			apiSvcFn:            successfulAPICreateAndDelete,
  1904  			eventSvcFn:          successfulOneEventCreate,
  1905  			specSvcFn:           successfulSpecWithOneEventCreateAndUpdate,
  1906  			fetchReqFn:          successfulFetchRequestFetchAndUpdate,
  1907  			packageSvcFn:        successfulPackageCreate,
  1908  			productSvcFn:        successfulProductCreate,
  1909  			vendorSvcFn:         successfulVendorCreate,
  1910  			tombstoneSvcFn:      successfulTombstoneCreate,
  1911  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1912  			clientFn: func() *automock.Client {
  1913  				client := &automock.Client{}
  1914  				doc := fixORDDocument()
  1915  				doc.EventResources[0].Name = "" // invalid document
  1916  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, *doc.DescribedSystemInstance.BaseURL, nil)
  1917  				return client
  1918  			},
  1919  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1920  		},
  1921  		{
  1922  			Name: "Resync resources for invalid ORD documents when bundle name is empty",
  1923  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1924  				persistTx := &persistenceautomock.PersistenceTx{}
  1925  				persistTx.On("Commit").Return(nil).Times(31)
  1926  
  1927  				transact := &persistenceautomock.Transactioner{}
  1928  				transact.On("Begin").Return(persistTx, nil).Times(30)
  1929  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(30)
  1930  
  1931  				return persistTx, transact
  1932  			},
  1933  			appSvcFn:     successfulAppGet,
  1934  			tenantSvcFn:  successfulTenantSvc,
  1935  			webhookSvcFn: successfulWebhookList,
  1936  			bundleSvcFn: func() *automock.BundleService {
  1937  				bundlesSvc := &automock.BundleService{}
  1938  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Times(3)
  1939  				return bundlesSvc
  1940  			},
  1941  			apiSvcFn: func() *automock.APIService {
  1942  				apiSvc := &automock.APIService{}
  1943  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Twice()
  1944  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{}, mock.Anything, "").Return("", nil).Once()
  1945  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{}, mock.Anything, "").Return("", nil).Once()
  1946  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  1947  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  1948  				return apiSvc
  1949  			},
  1950  			eventSvcFn: func() *automock.EventService {
  1951  				eventSvc := &automock.EventService{}
  1952  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  1953  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  1954  				eventSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.EventResources[0], ([]*model.SpecInput)(nil), []string{}, mock.Anything, "").Return(event1ID, nil).Once()
  1955  				eventSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.EventResources[1], ([]*model.SpecInput)(nil), []string{}, mock.Anything, "").Return(event2ID, nil).Once()
  1956  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  1957  				return eventSvc
  1958  			},
  1959  			specSvcFn:           successfulSpecCreateAndUpdate,
  1960  			fetchReqFn:          successfulFetchRequestFetchAndUpdate,
  1961  			packageSvcFn:        successfulPackageCreate,
  1962  			productSvcFn:        successfulProductCreate,
  1963  			vendorSvcFn:         successfulVendorCreate,
  1964  			tombstoneSvcFn:      successfulTombstoneCreate,
  1965  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  1966  			clientFn: func() *automock.Client {
  1967  				client := &automock.Client{}
  1968  				doc := fixORDDocument()
  1969  				doc.ConsumptionBundles[0].Name = ""
  1970  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, *doc.DescribedSystemInstance.BaseURL, nil)
  1971  				return client
  1972  			},
  1973  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  1974  		},
  1975  		{
  1976  			Name: "Resync resources for invalid ORD documents when vendor ordID is empty",
  1977  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  1978  				persistTx := &persistenceautomock.PersistenceTx{}
  1979  				persistTx.On("Commit").Return(nil).Times(32)
  1980  
  1981  				transact := &persistenceautomock.Transactioner{}
  1982  				transact.On("Begin").Return(persistTx, nil).Times(31)
  1983  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(31)
  1984  
  1985  				return persistTx, transact
  1986  			},
  1987  			appSvcFn:      successfulAppGet,
  1988  			tenantSvcFn:   successfulTenantSvc,
  1989  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  1990  			webhookConvFn: successfulWebhookConversion,
  1991  			bundleSvcFn:   successfulBundleCreate,
  1992  			apiSvcFn:      successfulAPICreateAndDelete,
  1993  			eventSvcFn:    successfulEventCreate,
  1994  			specSvcFn:     successfulSpecCreateAndUpdate,
  1995  			fetchReqFn:    successfulFetchRequestFetchAndUpdate,
  1996  			packageSvcFn:  successfulPackageCreate,
  1997  			productSvcFn:  successfulProductCreate,
  1998  			vendorSvcFn: func() *automock.VendorService {
  1999  				vendorSvc := &automock.VendorService{}
  2000  				vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2001  				vendorSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Vendors[1]).Return("", nil).Once()
  2002  				vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixVendors(), nil).Once()
  2003  				return vendorSvc
  2004  			},
  2005  			tombstoneSvcFn:      successfulTombstoneCreate,
  2006  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  2007  			clientFn: func() *automock.Client {
  2008  				client := &automock.Client{}
  2009  				doc := fixORDDocument()
  2010  				doc.Vendors[0].OrdID = ""
  2011  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, *doc.DescribedSystemInstance.BaseURL, nil)
  2012  				return client
  2013  			},
  2014  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2015  		},
  2016  		{
  2017  			Name: "Resync resources for invalid ORD documents when product title is empty",
  2018  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2019  				persistTx := &persistenceautomock.PersistenceTx{}
  2020  				persistTx.On("Commit").Return(nil).Times(32)
  2021  
  2022  				transact := &persistenceautomock.Transactioner{}
  2023  				transact.On("Begin").Return(persistTx, nil).Times(31)
  2024  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(31)
  2025  
  2026  				return persistTx, transact
  2027  			},
  2028  			appSvcFn:      successfulAppGet,
  2029  			tenantSvcFn:   successfulTenantSvc,
  2030  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  2031  			webhookConvFn: successfulWebhookConversion,
  2032  			bundleSvcFn:   successfulBundleCreate,
  2033  			apiSvcFn:      successfulAPICreateAndDelete,
  2034  			eventSvcFn:    successfulEventCreate,
  2035  			specSvcFn:     successfulSpecCreateAndUpdate,
  2036  			fetchReqFn:    successfulFetchRequestFetchAndUpdate,
  2037  			packageSvcFn:  successfulPackageCreate,
  2038  			productSvcFn: func() *automock.ProductService {
  2039  				productSvc := &automock.ProductService{}
  2040  				productSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Twice()
  2041  				return productSvc
  2042  			},
  2043  			vendorSvcFn:         successfulVendorCreate,
  2044  			tombstoneSvcFn:      successfulTombstoneCreate,
  2045  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  2046  			clientFn: func() *automock.Client {
  2047  				client := &automock.Client{}
  2048  				doc := fixORDDocument()
  2049  				doc.Products[0].Title = ""
  2050  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, *doc.DescribedSystemInstance.BaseURL, nil)
  2051  				return client
  2052  			},
  2053  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2054  		},
  2055  		{
  2056  			Name: "Resync resources for invalid ORD documents when package title is empty",
  2057  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2058  				persistTx := &persistenceautomock.PersistenceTx{}
  2059  				persistTx.On("Commit").Return(nil).Times(5)
  2060  
  2061  				transact := &persistenceautomock.Transactioner{}
  2062  				transact.On("Begin").Return(persistTx, nil).Times(5)
  2063  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(5)
  2064  
  2065  				return persistTx, transact
  2066  			},
  2067  			appSvcFn:      successfulAppGet,
  2068  			tenantSvcFn:   successfulTenantSvc,
  2069  			webhookSvcFn:  successfulWebhookList,
  2070  			webhookConvFn: successfulWebhookConversion,
  2071  			bundleSvcFn:   successfulEmptyBundleList,
  2072  			apiSvcFn: func() *automock.APIService {
  2073  				apiSvc := &automock.APIService{}
  2074  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2075  				return apiSvc
  2076  			},
  2077  			eventSvcFn: func() *automock.EventService {
  2078  				eventSvc := &automock.EventService{}
  2079  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2080  				return eventSvc
  2081  			},
  2082  			fetchReqFn: successfulFetchRequestFetchAndUpdate,
  2083  			packageSvcFn: func() *automock.PackageService {
  2084  				packagesSvc := &automock.PackageService{}
  2085  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2086  				return packagesSvc
  2087  			},
  2088  			productSvcFn: func() *automock.ProductService {
  2089  				productSvc := &automock.ProductService{}
  2090  				return productSvc
  2091  			},
  2092  			vendorSvcFn: func() *automock.VendorService {
  2093  				vendorSvc := &automock.VendorService{}
  2094  				return vendorSvc
  2095  			},
  2096  			tombstoneSvcFn: func() *automock.TombstoneService {
  2097  				tombstoneSvc := &automock.TombstoneService{}
  2098  				return tombstoneSvc
  2099  			},
  2100  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  2101  			clientFn: func() *automock.Client {
  2102  				client := &automock.Client{}
  2103  				doc := fixORDDocument()
  2104  				doc.Packages[0].Title = ""
  2105  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, *doc.DescribedSystemInstance.BaseURL, nil)
  2106  				return client
  2107  			},
  2108  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2109  		},
  2110  		{
  2111  			Name: "Does not resync resources if vendor list fails",
  2112  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2113  				persistTx := &persistenceautomock.PersistenceTx{}
  2114  				persistTx.On("Commit").Return(nil).Times(6)
  2115  
  2116  				transact := &persistenceautomock.Transactioner{}
  2117  				transact.On("Begin").Return(persistTx, nil).Times(6)
  2118  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(5)
  2119  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2120  				return persistTx, transact
  2121  			},
  2122  			appSvcFn:     successfulAppGet,
  2123  			tenantSvcFn:  successfulTenantSvc,
  2124  			webhookSvcFn: successfulWebhookList,
  2125  			bundleSvcFn:  successfulEmptyBundleList,
  2126  			vendorSvcFn: func() *automock.VendorService {
  2127  				vendorSvc := &automock.VendorService{}
  2128  				vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  2129  				return vendorSvc
  2130  			},
  2131  			clientFn:                successfulClientFetch,
  2132  			apiSvcFn:                successfulEmptyAPIList,
  2133  			eventSvcFn:              successfulEmptyEventList,
  2134  			packageSvcFn:            successfulEmptyPackageList,
  2135  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2136  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2137  		},
  2138  		{
  2139  			Name: "Fails to list vendors after resync",
  2140  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2141  				persistTx := &persistenceautomock.PersistenceTx{}
  2142  				persistTx.On("Commit").Return(nil).Times(9)
  2143  
  2144  				transact := &persistenceautomock.Transactioner{}
  2145  				transact.On("Begin").Return(persistTx, nil).Times(9)
  2146  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(8)
  2147  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2148  				return persistTx, transact
  2149  			},
  2150  			appSvcFn:     successfulAppGet,
  2151  			tenantSvcFn:  successfulTenantSvc,
  2152  			webhookSvcFn: successfulWebhookList,
  2153  			bundleSvcFn:  successfulEmptyBundleList,
  2154  			vendorSvcFn: func() *automock.VendorService {
  2155  				vendorSvc := &automock.VendorService{}
  2156  				vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixVendors(), nil).Once()
  2157  				vendorSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, vendorID, *sanitizedDoc.Vendors[0]).Return(nil).Once()
  2158  				vendorSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, vendorID2, *sanitizedDoc.Vendors[1]).Return(nil).Once()
  2159  				vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  2160  				return vendorSvc
  2161  			},
  2162  			clientFn:                successfulClientFetch,
  2163  			apiSvcFn:                successfulEmptyAPIList,
  2164  			eventSvcFn:              successfulEmptyEventList,
  2165  			packageSvcFn:            successfulEmptyPackageList,
  2166  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2167  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2168  		},
  2169  		{
  2170  			Name: "Does not resync resources if vendor update fails",
  2171  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2172  				persistTx := &persistenceautomock.PersistenceTx{}
  2173  				persistTx.On("Commit").Return(nil).Times(6)
  2174  
  2175  				transact := &persistenceautomock.Transactioner{}
  2176  				transact.On("Begin").Return(persistTx, nil).Times(7)
  2177  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(6)
  2178  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2179  				return persistTx, transact
  2180  			},
  2181  			appSvcFn:     successfulAppGet,
  2182  			tenantSvcFn:  successfulTenantSvc,
  2183  			webhookSvcFn: successfulWebhookList,
  2184  			bundleSvcFn:  successfulEmptyBundleList,
  2185  			vendorSvcFn: func() *automock.VendorService {
  2186  				vendorSvc := &automock.VendorService{}
  2187  				vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixVendors(), nil).Once()
  2188  				vendorSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, vendorID, *sanitizedDoc.Vendors[0]).Return(testErr).Once()
  2189  				return vendorSvc
  2190  			},
  2191  			clientFn:                successfulClientFetch,
  2192  			apiSvcFn:                successfulEmptyAPIList,
  2193  			eventSvcFn:              successfulEmptyEventList,
  2194  			packageSvcFn:            successfulEmptyPackageList,
  2195  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2196  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2197  		},
  2198  		{
  2199  			Name: "Does not resync resources if vendor create fails",
  2200  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2201  				persistTx := &persistenceautomock.PersistenceTx{}
  2202  				persistTx.On("Commit").Return(nil).Times(6)
  2203  
  2204  				transact := &persistenceautomock.Transactioner{}
  2205  				transact.On("Begin").Return(persistTx, nil).Times(7)
  2206  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(6)
  2207  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2208  				return persistTx, transact
  2209  			},
  2210  			appSvcFn:     successfulAppGet,
  2211  			tenantSvcFn:  successfulTenantSvc,
  2212  			webhookSvcFn: successfulWebhookList,
  2213  			bundleSvcFn:  successfulEmptyBundleList,
  2214  			vendorSvcFn: func() *automock.VendorService {
  2215  				vendorSvc := &automock.VendorService{}
  2216  				vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2217  				vendorSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Vendors[0]).Return("", testErr).Once()
  2218  				return vendorSvc
  2219  			},
  2220  			clientFn:                successfulClientFetch,
  2221  			apiSvcFn:                successfulEmptyAPIList,
  2222  			eventSvcFn:              successfulEmptyEventList,
  2223  			packageSvcFn:            successfulEmptyPackageList,
  2224  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2225  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2226  		},
  2227  		{
  2228  			Name: "Does not resync resources if product list fails",
  2229  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2230  				persistTx := &persistenceautomock.PersistenceTx{}
  2231  				persistTx.On("Commit").Return(nil).Times(10)
  2232  
  2233  				transact := &persistenceautomock.Transactioner{}
  2234  				transact.On("Begin").Return(persistTx, nil).Times(10)
  2235  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(9)
  2236  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2237  				return persistTx, transact
  2238  			},
  2239  			appSvcFn:     successfulAppGet,
  2240  			tenantSvcFn:  successfulTenantSvc,
  2241  			webhookSvcFn: successfulWebhookList,
  2242  			bundleSvcFn:  successfulEmptyBundleList,
  2243  			vendorSvcFn:  successfulVendorUpdateForApplication,
  2244  			productSvcFn: func() *automock.ProductService {
  2245  				productSvc := &automock.ProductService{}
  2246  				productSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  2247  				return productSvc
  2248  			},
  2249  			clientFn:                successfulClientFetch,
  2250  			apiSvcFn:                successfulEmptyAPIList,
  2251  			eventSvcFn:              successfulEmptyEventList,
  2252  			packageSvcFn:            successfulEmptyPackageList,
  2253  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2254  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2255  		},
  2256  		{
  2257  			Name: "Fails to list products after resync",
  2258  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2259  				persistTx := &persistenceautomock.PersistenceTx{}
  2260  				persistTx.On("Commit").Return(nil).Times(12)
  2261  
  2262  				transact := &persistenceautomock.Transactioner{}
  2263  				transact.On("Begin").Return(persistTx, nil).Times(12)
  2264  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(11)
  2265  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2266  				return persistTx, transact
  2267  			},
  2268  			appSvcFn:     successfulAppGet,
  2269  			tenantSvcFn:  successfulTenantSvc,
  2270  			webhookSvcFn: successfulWebhookList,
  2271  			bundleSvcFn:  successfulEmptyBundleList,
  2272  			vendorSvcFn:  successfulVendorUpdateForApplication,
  2273  			productSvcFn: func() *automock.ProductService {
  2274  				productSvc := &automock.ProductService{}
  2275  				productSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixProducts(), nil).Once()
  2276  				productSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, productID, *sanitizedDoc.Products[0]).Return(nil).Once()
  2277  				productSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  2278  
  2279  				return productSvc
  2280  			},
  2281  			clientFn:                successfulClientFetch,
  2282  			apiSvcFn:                successfulEmptyAPIList,
  2283  			eventSvcFn:              successfulEmptyEventList,
  2284  			packageSvcFn:            successfulEmptyPackageList,
  2285  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2286  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2287  		},
  2288  		{
  2289  			Name: "Does not resync resources if product update fails",
  2290  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2291  				persistTx := &persistenceautomock.PersistenceTx{}
  2292  				persistTx.On("Commit").Return(nil).Times(10)
  2293  
  2294  				transact := &persistenceautomock.Transactioner{}
  2295  				transact.On("Begin").Return(persistTx, nil).Times(11)
  2296  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(10)
  2297  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2298  				return persistTx, transact
  2299  			},
  2300  			appSvcFn:     successfulAppGet,
  2301  			tenantSvcFn:  successfulTenantSvc,
  2302  			webhookSvcFn: successfulWebhookList,
  2303  			bundleSvcFn:  successfulEmptyBundleList,
  2304  			vendorSvcFn:  successfulVendorUpdateForApplication,
  2305  			productSvcFn: func() *automock.ProductService {
  2306  				productSvc := &automock.ProductService{}
  2307  				productSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixProducts(), nil).Once()
  2308  				productSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, productID, *sanitizedDoc.Products[0]).Return(testErr).Once()
  2309  				return productSvc
  2310  			},
  2311  			clientFn:                successfulClientFetch,
  2312  			apiSvcFn:                successfulEmptyAPIList,
  2313  			eventSvcFn:              successfulEmptyEventList,
  2314  			packageSvcFn:            successfulEmptyPackageList,
  2315  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2316  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2317  		},
  2318  		{
  2319  			Name: "Does not resync resources if product create fails",
  2320  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2321  				persistTx := &persistenceautomock.PersistenceTx{}
  2322  				persistTx.On("Commit").Return(nil).Times(10)
  2323  
  2324  				transact := &persistenceautomock.Transactioner{}
  2325  				transact.On("Begin").Return(persistTx, nil).Times(11)
  2326  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(10)
  2327  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2328  				return persistTx, transact
  2329  			},
  2330  			appSvcFn:     successfulAppGet,
  2331  			tenantSvcFn:  successfulTenantSvc,
  2332  			webhookSvcFn: successfulWebhookList,
  2333  			bundleSvcFn:  successfulEmptyBundleList,
  2334  			vendorSvcFn:  successfulVendorUpdateForApplication,
  2335  			productSvcFn: func() *automock.ProductService {
  2336  				productSvc := &automock.ProductService{}
  2337  				productSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2338  				productSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Products[0]).Return("", testErr).Once()
  2339  				return productSvc
  2340  			},
  2341  			clientFn:                successfulClientFetch,
  2342  			apiSvcFn:                successfulEmptyAPIList,
  2343  			eventSvcFn:              successfulEmptyEventList,
  2344  			packageSvcFn:            successfulEmptyPackageList,
  2345  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2346  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2347  		},
  2348  		{
  2349  			Name: "Does not resync resources if package list fails",
  2350  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2351  				persistTx := &persistenceautomock.PersistenceTx{}
  2352  				persistTx.On("Commit").Return(nil).Times(13)
  2353  
  2354  				transact := &persistenceautomock.Transactioner{}
  2355  				transact.On("Begin").Return(persistTx, nil).Times(13)
  2356  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(12)
  2357  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2358  				return persistTx, transact
  2359  			},
  2360  			appSvcFn:     successfulAppGet,
  2361  			tenantSvcFn:  successfulTenantSvc,
  2362  			webhookSvcFn: successfulWebhookList,
  2363  			bundleSvcFn:  successfulEmptyBundleList,
  2364  			productSvcFn: successfulProductUpdateForApplication,
  2365  			vendorSvcFn:  successfulVendorUpdateForApplication,
  2366  			packageSvcFn: func() *automock.PackageService {
  2367  				packagesSvc := &automock.PackageService{}
  2368  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2369  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  2370  				return packagesSvc
  2371  			},
  2372  			clientFn:                successfulClientFetch,
  2373  			apiSvcFn:                successfulEmptyAPIList,
  2374  			eventSvcFn:              successfulEmptyEventList,
  2375  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2376  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2377  		},
  2378  		{
  2379  			Name: "Fails to list packages after resync",
  2380  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2381  				persistTx := &persistenceautomock.PersistenceTx{}
  2382  				persistTx.On("Commit").Return(nil).Times(15)
  2383  
  2384  				transact := &persistenceautomock.Transactioner{}
  2385  				transact.On("Begin").Return(persistTx, nil).Times(15)
  2386  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(14)
  2387  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2388  				return persistTx, transact
  2389  			},
  2390  			appSvcFn:     successfulAppGet,
  2391  			tenantSvcFn:  successfulTenantSvc,
  2392  			webhookSvcFn: successfulWebhookList,
  2393  			bundleSvcFn:  successfulEmptyBundleList,
  2394  			productSvcFn: successfulProductUpdateForApplication,
  2395  			vendorSvcFn:  successfulVendorUpdateForApplication,
  2396  			packageSvcFn: func() *automock.PackageService {
  2397  				packagesSvc := &automock.PackageService{}
  2398  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2399  				packagesSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, packageID, *sanitizedDoc.Packages[0], packagePreSanitizedHash).Return(nil).Once()
  2400  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixPackages(), nil).Once()
  2401  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  2402  				return packagesSvc
  2403  			},
  2404  			clientFn:                successfulClientFetch,
  2405  			apiSvcFn:                successfulEmptyAPIList,
  2406  			eventSvcFn:              successfulEmptyEventList,
  2407  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2408  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2409  		},
  2410  		{
  2411  			Name: "Does not resync resources if package update fails",
  2412  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2413  				persistTx := &persistenceautomock.PersistenceTx{}
  2414  				persistTx.On("Commit").Return(nil).Times(13)
  2415  
  2416  				transact := &persistenceautomock.Transactioner{}
  2417  				transact.On("Begin").Return(persistTx, nil).Times(14)
  2418  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(13)
  2419  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2420  				return persistTx, transact
  2421  			},
  2422  			appSvcFn:     successfulAppGet,
  2423  			tenantSvcFn:  successfulTenantSvc,
  2424  			webhookSvcFn: successfulWebhookList,
  2425  			bundleSvcFn:  successfulEmptyBundleList,
  2426  			productSvcFn: successfulProductUpdateForApplication,
  2427  			vendorSvcFn:  successfulVendorUpdateForApplication,
  2428  			packageSvcFn: func() *automock.PackageService {
  2429  				packagesSvc := &automock.PackageService{}
  2430  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixPackages(), nil).Once()
  2431  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixPackages(), nil).Once()
  2432  				packagesSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, packageID, *sanitizedDoc.Packages[0], packagePreSanitizedHash).Return(testErr).Once()
  2433  				return packagesSvc
  2434  			},
  2435  			clientFn:                successfulClientFetch,
  2436  			apiSvcFn:                successfulEmptyAPIList,
  2437  			eventSvcFn:              successfulEmptyEventList,
  2438  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2439  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2440  		},
  2441  		{
  2442  			Name: "Does not resync resources if package create fails",
  2443  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2444  				persistTx := &persistenceautomock.PersistenceTx{}
  2445  				persistTx.On("Commit").Return(nil).Times(13)
  2446  
  2447  				transact := &persistenceautomock.Transactioner{}
  2448  				transact.On("Begin").Return(persistTx, nil).Times(14)
  2449  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(13)
  2450  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2451  				return persistTx, transact
  2452  			},
  2453  			appSvcFn:     successfulAppGet,
  2454  			tenantSvcFn:  successfulTenantSvc,
  2455  			webhookSvcFn: successfulWebhookList,
  2456  			bundleSvcFn:  successfulEmptyBundleList,
  2457  			productSvcFn: successfulProductCreate,
  2458  			vendorSvcFn:  successfulVendorCreate,
  2459  			packageSvcFn: func() *automock.PackageService {
  2460  				packagesSvc := &automock.PackageService{}
  2461  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2462  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2463  				packagesSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Packages[0], mock.Anything).Return("", testErr).Once()
  2464  				return packagesSvc
  2465  			},
  2466  			clientFn:                successfulClientFetch,
  2467  			apiSvcFn:                successfulEmptyAPIList,
  2468  			eventSvcFn:              successfulEmptyEventList,
  2469  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2470  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2471  		},
  2472  		{
  2473  			Name: "Does not resync resources if bundle list fails",
  2474  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2475  				persistTx := &persistenceautomock.PersistenceTx{}
  2476  				persistTx.On("Commit").Return(nil).Times(15)
  2477  
  2478  				transact := &persistenceautomock.Transactioner{}
  2479  				transact.On("Begin").Return(persistTx, nil).Times(16)
  2480  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(15)
  2481  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2482  				return persistTx, transact
  2483  			},
  2484  			appSvcFn:     successfulAppGet,
  2485  			tenantSvcFn:  successfulTenantSvc,
  2486  			webhookSvcFn: successfulWebhookList,
  2487  			productSvcFn: successfulProductUpdateForApplication,
  2488  			vendorSvcFn:  successfulVendorUpdateForApplication,
  2489  			packageSvcFn: successfulPackageUpdateForApplication,
  2490  			bundleSvcFn: func() *automock.BundleService {
  2491  				bundlesSvc := &automock.BundleService{}
  2492  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2493  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  2494  				return bundlesSvc
  2495  			},
  2496  			clientFn:                successfulClientFetch,
  2497  			apiSvcFn:                successfulEmptyAPIList,
  2498  			eventSvcFn:              successfulEmptyEventList,
  2499  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2500  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2501  		},
  2502  		{
  2503  			Name: "Fails to list bundles after resync",
  2504  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2505  				persistTx := &persistenceautomock.PersistenceTx{}
  2506  				persistTx.On("Commit").Return(nil).Times(18)
  2507  
  2508  				transact := &persistenceautomock.Transactioner{}
  2509  				transact.On("Begin").Return(persistTx, nil).Times(19)
  2510  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(18)
  2511  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2512  				return persistTx, transact
  2513  			},
  2514  			appSvcFn:      successfulAppGet,
  2515  			tenantSvcFn:   successfulTenantSvc,
  2516  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  2517  			webhookConvFn: successfulWebhookConversion,
  2518  			productSvcFn:  successfulProductUpdateForApplication,
  2519  			vendorSvcFn:   successfulVendorUpdateForApplication,
  2520  			packageSvcFn:  successfulPackageUpdateForApplication,
  2521  			bundleSvcFn: func() *automock.BundleService {
  2522  				bundlesSvc := &automock.BundleService{}
  2523  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(fixBundles(), nil).Twice()
  2524  				bundlesSvc.On("UpdateBundle", txtest.CtxWithDBMatcher(), resource.Application, bundleID, bundleUpdateInputFromCreateInput(*sanitizedDoc.ConsumptionBundles[0]), bundlePreSanitizedHash).Return(nil).Once()
  2525  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  2526  				return bundlesSvc
  2527  			},
  2528  			clientFn:                successfulClientFetch,
  2529  			apiSvcFn:                successfulEmptyAPIList,
  2530  			eventSvcFn:              successfulEmptyEventList,
  2531  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2532  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2533  		},
  2534  		{
  2535  			Name: "Does not resync resources if bundle update fails",
  2536  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2537  				persistTx := &persistenceautomock.PersistenceTx{}
  2538  				persistTx.On("Commit").Return(nil).Times(16)
  2539  
  2540  				transact := &persistenceautomock.Transactioner{}
  2541  				transact.On("Begin").Return(persistTx, nil).Times(17)
  2542  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(16)
  2543  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2544  				return persistTx, transact
  2545  			},
  2546  			appSvcFn:     successfulAppGet,
  2547  			tenantSvcFn:  successfulTenantSvc,
  2548  			webhookSvcFn: successfulWebhookList,
  2549  			productSvcFn: successfulProductUpdateForApplication,
  2550  			vendorSvcFn:  successfulVendorUpdateForApplication,
  2551  			packageSvcFn: successfulPackageUpdateForApplication,
  2552  			bundleSvcFn: func() *automock.BundleService {
  2553  				bundlesSvc := &automock.BundleService{}
  2554  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2555  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(fixBundles(), nil).Once()
  2556  				bundlesSvc.On("UpdateBundle", txtest.CtxWithDBMatcher(), resource.Application, bundleID, bundleUpdateInputFromCreateInput(*sanitizedDoc.ConsumptionBundles[0]), bundlePreSanitizedHash).Return(testErr).Once()
  2557  				return bundlesSvc
  2558  			},
  2559  			clientFn:                successfulClientFetch,
  2560  			apiSvcFn:                successfulEmptyAPIList,
  2561  			eventSvcFn:              successfulEmptyEventList,
  2562  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2563  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2564  		},
  2565  		{
  2566  			Name: "Does not resync resources if bundle create fails",
  2567  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2568  				persistTx := &persistenceautomock.PersistenceTx{}
  2569  				persistTx.On("Commit").Return(nil).Times(16)
  2570  
  2571  				transact := &persistenceautomock.Transactioner{}
  2572  				transact.On("Begin").Return(persistTx, nil).Times(17)
  2573  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(16)
  2574  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  2575  				return persistTx, transact
  2576  			},
  2577  			appSvcFn:     successfulAppGet,
  2578  			tenantSvcFn:  successfulTenantSvc,
  2579  			webhookSvcFn: successfulWebhookList,
  2580  			productSvcFn: successfulProductCreate,
  2581  			vendorSvcFn:  successfulVendorCreate,
  2582  			packageSvcFn: successfulPackageCreate,
  2583  			bundleSvcFn: func() *automock.BundleService {
  2584  				bundlesSvc := &automock.BundleService{}
  2585  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2586  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2587  				bundlesSvc.On("CreateBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.ConsumptionBundles[0], mock.Anything).Return("", testErr).Once()
  2588  				return bundlesSvc
  2589  			},
  2590  			clientFn:                successfulClientFetch,
  2591  			apiSvcFn:                successfulEmptyAPIList,
  2592  			eventSvcFn:              successfulEmptyEventList,
  2593  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2594  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2595  		},
  2596  		{
  2597  			Name: "Does not resync resources if bundle have different tenant mapping configuration",
  2598  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2599  				persistTx := &persistenceautomock.PersistenceTx{}
  2600  				persistTx.On("Commit").Return(nil).Times(17)
  2601  
  2602  				transact := &persistenceautomock.Transactioner{}
  2603  				transact.On("Begin").Return(persistTx, nil).Times(17)
  2604  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false)
  2605  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true)
  2606  				return persistTx, transact
  2607  			},
  2608  			appSvcFn:     successfulAppGet,
  2609  			tenantSvcFn:  successfulTenantSvc,
  2610  			webhookSvcFn: successfulWebhookList,
  2611  			productSvcFn: successfulProductCreate,
  2612  			vendorSvcFn:  successfulVendorCreate,
  2613  			packageSvcFn: successfulPackageCreate,
  2614  			bundleSvcFn:  successfulListTwiceAndCreateBundle,
  2615  			clientFn: func() *automock.Client {
  2616  				client := &automock.Client{}
  2617  				doc := fixORDDocument()
  2618  				doc.ConsumptionBundles[0].CredentialExchangeStrategies = json.RawMessage(fmt.Sprintf(credentialExchangeStrategiesWithMultipleSameTypesFormat, credentialExchangeStrategyType, credentialExchangeStrategyType))
  2619  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, baseURL, nil)
  2620  				return client
  2621  			},
  2622  			apiSvcFn: func() *automock.APIService {
  2623  				apiSvc := &automock.APIService{}
  2624  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2625  				return apiSvc
  2626  			},
  2627  			eventSvcFn: func() *automock.EventService {
  2628  				eventSvc := &automock.EventService{}
  2629  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2630  				return eventSvc
  2631  			},
  2632  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2633  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2634  		},
  2635  		{
  2636  			Name: "Does not resync resources if webhooks could not be enriched",
  2637  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2638  				persistTx := &persistenceautomock.PersistenceTx{}
  2639  				persistTx.On("Commit").Return(nil).Times(17)
  2640  
  2641  				transact := &persistenceautomock.Transactioner{}
  2642  				transact.On("Begin").Return(persistTx, nil).Times(17)
  2643  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false)
  2644  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true)
  2645  				return persistTx, transact
  2646  			},
  2647  			appSvcFn:    successfulAppGet,
  2648  			tenantSvcFn: successfulTenantSvc,
  2649  			webhookSvcFn: func() *automock.WebhookService {
  2650  				whSvc := &automock.WebhookService{}
  2651  				whInputs := []*graphql.WebhookInput{fixTenantMappingWebhookGraphQLInput()}
  2652  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixWebhooksForApplication(), nil).Once()
  2653  				whSvc.On("EnrichWebhooksWithTenantMappingWebhooks", whInputs).Return(nil, testErr).Once()
  2654  				return whSvc
  2655  			},
  2656  			productSvcFn: successfulProductCreate,
  2657  			vendorSvcFn:  successfulVendorCreate,
  2658  			packageSvcFn: successfulPackageCreate,
  2659  			bundleSvcFn:  successfulListTwiceAndCreateBundle,
  2660  			clientFn: func() *automock.Client {
  2661  				client := &automock.Client{}
  2662  				doc := fixORDDocument()
  2663  				doc.ConsumptionBundles[0].CredentialExchangeStrategies = json.RawMessage(fmt.Sprintf(credentialExchangeStrategiesWithCustomTypeFormat, credentialExchangeStrategyType))
  2664  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, baseURL, nil)
  2665  				return client
  2666  			},
  2667  			apiSvcFn: func() *automock.APIService {
  2668  				apiSvc := &automock.APIService{}
  2669  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2670  				return apiSvc
  2671  			},
  2672  			eventSvcFn: func() *automock.EventService {
  2673  				eventSvc := &automock.EventService{}
  2674  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2675  				return eventSvc
  2676  			},
  2677  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2678  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2679  		},
  2680  		{
  2681  			Name: "Does not resync resources if webhooks cannot be listed for application",
  2682  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2683  				persistTx := &persistenceautomock.PersistenceTx{}
  2684  				persistTx.On("Commit").Return(nil).Times(17)
  2685  
  2686  				transact := &persistenceautomock.Transactioner{}
  2687  				transact.On("Begin").Return(persistTx, nil).Times(18)
  2688  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false)
  2689  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true)
  2690  				return persistTx, transact
  2691  			},
  2692  			appSvcFn:    successfulAppGet,
  2693  			tenantSvcFn: successfulTenantSvc,
  2694  			webhookSvcFn: func() *automock.WebhookService {
  2695  				whSvc := &automock.WebhookService{}
  2696  				whInputs := []*graphql.WebhookInput{fixTenantMappingWebhookGraphQLInput()}
  2697  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixWebhooksForApplication(), nil).Once()
  2698  				whSvc.On("EnrichWebhooksWithTenantMappingWebhooks", whInputs).Return(whInputs, nil).Once()
  2699  				whSvc.On("ListForApplicationGlobal", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  2700  
  2701  				return whSvc
  2702  			},
  2703  			productSvcFn: successfulProductCreate,
  2704  			vendorSvcFn:  successfulVendorCreate,
  2705  			packageSvcFn: successfulPackageCreate,
  2706  			bundleSvcFn:  successfulListTwiceAndCreateBundle,
  2707  			clientFn: func() *automock.Client {
  2708  				client := &automock.Client{}
  2709  				doc := fixORDDocument()
  2710  				doc.ConsumptionBundles[0].CredentialExchangeStrategies = json.RawMessage(fmt.Sprintf(credentialExchangeStrategiesWithCustomTypeFormat, credentialExchangeStrategyType))
  2711  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, baseURL, nil)
  2712  				return client
  2713  			},
  2714  			apiSvcFn: func() *automock.APIService {
  2715  				apiSvc := &automock.APIService{}
  2716  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2717  				return apiSvc
  2718  			},
  2719  			eventSvcFn: func() *automock.EventService {
  2720  				eventSvc := &automock.EventService{}
  2721  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2722  				return eventSvc
  2723  			},
  2724  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2725  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2726  		},
  2727  		{
  2728  			Name: "Does not resync resources if webhooks cannot be converted from graphql input to model input",
  2729  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2730  				persistTx := &persistenceautomock.PersistenceTx{}
  2731  				persistTx.On("Commit").Return(nil).Times(17)
  2732  
  2733  				transact := &persistenceautomock.Transactioner{}
  2734  				transact.On("Begin").Return(persistTx, nil).Times(18)
  2735  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false)
  2736  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true)
  2737  				return persistTx, transact
  2738  			},
  2739  			appSvcFn:    successfulAppGet,
  2740  			tenantSvcFn: successfulTenantSvc,
  2741  			webhookSvcFn: func() *automock.WebhookService {
  2742  				whSvc := &automock.WebhookService{}
  2743  				whInputs := []*graphql.WebhookInput{fixTenantMappingWebhookGraphQLInput()}
  2744  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixWebhooksForApplication(), nil).Once()
  2745  				whSvc.On("EnrichWebhooksWithTenantMappingWebhooks", whInputs).Return(whInputs, nil).Once()
  2746  				whSvc.On("ListForApplicationGlobal", txtest.CtxWithDBMatcher(), appID).Return(fixWebhooksForApplication(), nil).Once()
  2747  
  2748  				return whSvc
  2749  			},
  2750  			webhookConvFn: func() *automock.WebhookConverter {
  2751  				whConv := &automock.WebhookConverter{}
  2752  				whConv.On("InputFromGraphQL", fixTenantMappingWebhookGraphQLInput()).Return(nil, testErr).Once()
  2753  				return whConv
  2754  			},
  2755  			productSvcFn: successfulProductCreate,
  2756  			vendorSvcFn:  successfulVendorCreate,
  2757  			packageSvcFn: successfulPackageCreate,
  2758  			bundleSvcFn:  successfulListTwiceAndCreateBundle,
  2759  			clientFn: func() *automock.Client {
  2760  				client := &automock.Client{}
  2761  				doc := fixORDDocument()
  2762  				doc.ConsumptionBundles[0].CredentialExchangeStrategies = json.RawMessage(fmt.Sprintf(credentialExchangeStrategiesWithCustomTypeFormat, credentialExchangeStrategyType))
  2763  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, baseURL, nil)
  2764  				return client
  2765  			},
  2766  			apiSvcFn: func() *automock.APIService {
  2767  				apiSvc := &automock.APIService{}
  2768  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2769  				return apiSvc
  2770  			},
  2771  			eventSvcFn: func() *automock.EventService {
  2772  				eventSvc := &automock.EventService{}
  2773  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2774  				return eventSvc
  2775  			},
  2776  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2777  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2778  		},
  2779  		{
  2780  			Name: "Does not resync resources if webhooks cannot be created",
  2781  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2782  				persistTx := &persistenceautomock.PersistenceTx{}
  2783  				persistTx.On("Commit").Return(nil).Times(17)
  2784  
  2785  				transact := &persistenceautomock.Transactioner{}
  2786  				transact.On("Begin").Return(persistTx, nil).Times(18)
  2787  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false)
  2788  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true)
  2789  				return persistTx, transact
  2790  			},
  2791  			appSvcFn:    successfulAppGet,
  2792  			tenantSvcFn: successfulTenantSvc,
  2793  			webhookSvcFn: func() *automock.WebhookService {
  2794  				whSvc := &automock.WebhookService{}
  2795  				whInputs := []*graphql.WebhookInput{fixTenantMappingWebhookGraphQLInput()}
  2796  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixWebhooksForApplication(), nil).Once()
  2797  				whSvc.On("EnrichWebhooksWithTenantMappingWebhooks", whInputs).Return(whInputs, nil).Once()
  2798  				whSvc.On("ListForApplicationGlobal", txtest.CtxWithDBMatcher(), appID).Return(fixWebhooksForApplication(), nil).Once()
  2799  				whSvc.On("Create", txtest.CtxWithDBMatcher(), appID, *fixTenantMappingWebhookModelInput(), model.ApplicationWebhookReference).Return("", testErr).Once()
  2800  
  2801  				return whSvc
  2802  			},
  2803  			webhookConvFn: successfulWebhookConversion,
  2804  			productSvcFn:  successfulProductCreate,
  2805  			vendorSvcFn:   successfulVendorCreate,
  2806  			packageSvcFn:  successfulPackageCreate,
  2807  			bundleSvcFn:   successfulListTwiceAndCreateBundle,
  2808  			clientFn: func() *automock.Client {
  2809  				client := &automock.Client{}
  2810  				doc := fixORDDocument()
  2811  				doc.ConsumptionBundles[0].CredentialExchangeStrategies = json.RawMessage(fmt.Sprintf(credentialExchangeStrategiesWithCustomTypeFormat, credentialExchangeStrategyType))
  2812  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, baseURL, nil)
  2813  				return client
  2814  			},
  2815  			apiSvcFn: func() *automock.APIService {
  2816  				apiSvc := &automock.APIService{}
  2817  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2818  				return apiSvc
  2819  			},
  2820  			eventSvcFn: func() *automock.EventService {
  2821  				eventSvc := &automock.EventService{}
  2822  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2823  				return eventSvc
  2824  			},
  2825  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2826  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2827  		},
  2828  		{
  2829  			Name: "Resync resources if webhooks can be created successfully",
  2830  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2831  				persistTx := &persistenceautomock.PersistenceTx{}
  2832  				persistTx.On("Commit").Return(nil).Times(33)
  2833  
  2834  				transact := &persistenceautomock.Transactioner{}
  2835  				transact.On("Begin").Return(persistTx, nil).Times(31)
  2836  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false)
  2837  				return persistTx, transact
  2838  			},
  2839  			appSvcFn:    successfulAppGet,
  2840  			tenantSvcFn: successfulTenantSvc,
  2841  			webhookSvcFn: func() *automock.WebhookService {
  2842  				whSvc := &automock.WebhookService{}
  2843  				whInputs := []*graphql.WebhookInput{fixTenantMappingWebhookGraphQLInput()}
  2844  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixWebhooksForApplication(), nil).Once()
  2845  				whSvc.On("EnrichWebhooksWithTenantMappingWebhooks", whInputs).Return(whInputs, nil).Once()
  2846  				whSvc.On("ListForApplicationGlobal", txtest.CtxWithDBMatcher(), appID).Return(fixWebhooksForApplication(), nil).Once()
  2847  				whSvc.On("Create", txtest.CtxWithDBMatcher(), appID, *fixTenantMappingWebhookModelInput(), model.ApplicationWebhookReference).Return("", nil).Once()
  2848  
  2849  				return whSvc
  2850  			},
  2851  			webhookConvFn: successfulWebhookConversion,
  2852  			productSvcFn:  successfulProductCreate,
  2853  			vendorSvcFn:   successfulVendorCreate,
  2854  			packageSvcFn:  successfulPackageCreate,
  2855  			bundleSvcFn:   successfulBundleCreateWithGenericParam,
  2856  			clientFn: func() *automock.Client {
  2857  				client := &automock.Client{}
  2858  				doc := fixORDDocument()
  2859  				doc.ConsumptionBundles[0].CredentialExchangeStrategies = json.RawMessage(fmt.Sprintf(credentialExchangeStrategiesWithCustomTypeFormat, credentialExchangeStrategyType))
  2860  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, baseURL, nil)
  2861  				return client
  2862  			},
  2863  			apiSvcFn: func() *automock.APIService {
  2864  				apiSvc := &automock.APIService{}
  2865  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2866  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2867  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return(api1ID, nil).Once()
  2868  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, mock.Anything, "").Return(api2ID, nil).Once()
  2869  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  2870  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(testErr).Once()
  2871  				return apiSvc
  2872  			},
  2873  			tombstoneSvcFn:          successfulTombstoneCreate,
  2874  			eventSvcFn:              successfulEventCreate,
  2875  			specSvcFn:               successfulSpecCreate,
  2876  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2877  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2878  		},
  2879  		{
  2880  			Name: "Does not recreate tenant mapping webhooks if there are no differences",
  2881  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2882  				persistTx := &persistenceautomock.PersistenceTx{}
  2883  				persistTx.On("Commit").Return(nil).Times(33)
  2884  
  2885  				transact := &persistenceautomock.Transactioner{}
  2886  				transact.On("Begin").Return(persistTx, nil).Times(31)
  2887  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false)
  2888  				return persistTx, transact
  2889  			},
  2890  			appSvcFn:    successfulAppGet,
  2891  			tenantSvcFn: successfulTenantSvc,
  2892  			webhookSvcFn: func() *automock.WebhookService {
  2893  				whSvc := &automock.WebhookService{}
  2894  				whInputs := []*graphql.WebhookInput{fixTenantMappingWebhookGraphQLInput()}
  2895  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixWebhooksForApplication(), nil).Once()
  2896  				whSvc.On("EnrichWebhooksWithTenantMappingWebhooks", whInputs).Return(whInputs, nil).Once()
  2897  				whSvc.On("ListForApplicationGlobal", txtest.CtxWithDBMatcher(), appID).Return(fixTenantMappingWebhooksForApplication(), nil).Once()
  2898  
  2899  				return whSvc
  2900  			},
  2901  			webhookConvFn: successfulWebhookConversion,
  2902  			productSvcFn:  successfulProductCreate,
  2903  			vendorSvcFn:   successfulVendorCreate,
  2904  			packageSvcFn:  successfulPackageCreate,
  2905  			bundleSvcFn:   successfulBundleCreateWithGenericParam,
  2906  			clientFn: func() *automock.Client {
  2907  				client := &automock.Client{}
  2908  				doc := fixORDDocument()
  2909  				doc.ConsumptionBundles[0].CredentialExchangeStrategies = json.RawMessage(fmt.Sprintf(credentialExchangeStrategiesWithCustomTypeFormat, credentialExchangeStrategyType))
  2910  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, baseURL, nil)
  2911  				return client
  2912  			},
  2913  			apiSvcFn: func() *automock.APIService {
  2914  				apiSvc := &automock.APIService{}
  2915  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2916  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2917  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return(api1ID, nil).Once()
  2918  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, mock.Anything, "").Return(api2ID, nil).Once()
  2919  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  2920  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(testErr).Once()
  2921  				return apiSvc
  2922  			},
  2923  			tombstoneSvcFn:          successfulTombstoneCreate,
  2924  			eventSvcFn:              successfulEventCreate,
  2925  			specSvcFn:               successfulSpecCreate,
  2926  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2927  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2928  		},
  2929  		{
  2930  			Name: "Does recreate of tenant mapping webhooks when there are differences",
  2931  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2932  				persistTx := &persistenceautomock.PersistenceTx{}
  2933  				persistTx.On("Commit").Return(nil).Times(33)
  2934  
  2935  				transact := &persistenceautomock.Transactioner{}
  2936  				transact.On("Begin").Return(persistTx, nil).Times(32)
  2937  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false)
  2938  				return persistTx, transact
  2939  			},
  2940  			appSvcFn:    successfulAppGet,
  2941  			tenantSvcFn: successfulTenantSvc,
  2942  			webhookSvcFn: func() *automock.WebhookService {
  2943  				whSvc := &automock.WebhookService{}
  2944  				whInputs := []*graphql.WebhookInput{fixTenantMappingWebhookGraphQLInput()}
  2945  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixWebhooksForApplication(), nil).Once()
  2946  				whSvc.On("EnrichWebhooksWithTenantMappingWebhooks", whInputs).Return(whInputs, nil).Once()
  2947  				webhooks := fixTenantMappingWebhooksForApplication()
  2948  				webhooks[0].URL = str.Ptr("old")
  2949  				whSvc.On("ListForApplicationGlobal", txtest.CtxWithDBMatcher(), appID).Return(webhooks, nil).Once()
  2950  				whSvc.On("Delete", txtest.CtxWithDBMatcher(), webhookID, model.ApplicationWebhookReference).Return(nil).Once()
  2951  				whSvc.On("Create", txtest.CtxWithDBMatcher(), appID, *fixTenantMappingWebhookModelInput(), model.ApplicationWebhookReference).Return(webhookID, nil).Once()
  2952  
  2953  				return whSvc
  2954  			},
  2955  			webhookConvFn: successfulWebhookConversion,
  2956  			productSvcFn:  successfulProductCreate,
  2957  			vendorSvcFn:   successfulVendorCreate,
  2958  			packageSvcFn:  successfulPackageCreate,
  2959  			bundleSvcFn:   successfulBundleCreateWithGenericParam,
  2960  			clientFn: func() *automock.Client {
  2961  				client := &automock.Client{}
  2962  				doc := fixORDDocument()
  2963  				doc.ConsumptionBundles[0].CredentialExchangeStrategies = json.RawMessage(fmt.Sprintf(credentialExchangeStrategiesWithCustomTypeFormat, credentialExchangeStrategyType))
  2964  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, baseURL, nil)
  2965  				return client
  2966  			},
  2967  			apiSvcFn: func() *automock.APIService {
  2968  				apiSvc := &automock.APIService{}
  2969  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2970  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  2971  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return(api1ID, nil).Once()
  2972  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, mock.Anything, "").Return(api2ID, nil).Once()
  2973  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  2974  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  2975  				return apiSvc
  2976  			},
  2977  			tombstoneSvcFn:          successfulTombstoneCreate,
  2978  			eventSvcFn:              successfulEventCreate,
  2979  			specSvcFn:               successfulSpecCreateAndUpdate,
  2980  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  2981  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  2982  			fetchReqFn:              successfulFetchRequestFetchAndUpdate,
  2983  		},
  2984  		{
  2985  			Name: "Does not recreate of tenant mapping webhooks when there are differences but deletion fails",
  2986  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  2987  				persistTx := &persistenceautomock.PersistenceTx{}
  2988  				persistTx.On("Commit").Return(nil).Times(32)
  2989  
  2990  				transact := &persistenceautomock.Transactioner{}
  2991  				transact.On("Begin").Return(persistTx, nil)
  2992  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false)
  2993  				return persistTx, transact
  2994  			},
  2995  			appSvcFn:    successfulAppGet,
  2996  			tenantSvcFn: successfulTenantSvc,
  2997  			webhookSvcFn: func() *automock.WebhookService {
  2998  				whSvc := &automock.WebhookService{}
  2999  				whInputs := []*graphql.WebhookInput{fixTenantMappingWebhookGraphQLInput()}
  3000  				whSvc.On("ListByWebhookType", txtest.CtxWithDBMatcher(), model.WebhookTypeOpenResourceDiscovery).Return(fixWebhooksForApplication(), nil).Once()
  3001  				whSvc.On("EnrichWebhooksWithTenantMappingWebhooks", whInputs).Return(whInputs, nil).Once()
  3002  				webhooks := fixTenantMappingWebhooksForApplication()
  3003  				webhooks[0].URL = str.Ptr("old")
  3004  				whSvc.On("ListForApplicationGlobal", txtest.CtxWithDBMatcher(), appID).Return(webhooks, nil).Once()
  3005  				whSvc.On("Delete", txtest.CtxWithDBMatcher(), webhookID, model.ApplicationWebhookReference).Return(testErr).Once()
  3006  
  3007  				return whSvc
  3008  			},
  3009  			webhookConvFn: successfulWebhookConversion,
  3010  			productSvcFn:  successfulProductCreate,
  3011  			vendorSvcFn:   successfulVendorCreate,
  3012  			packageSvcFn:  successfulPackageCreate,
  3013  			bundleSvcFn: func() *automock.BundleService {
  3014  				bundlesSvc := &automock.BundleService{}
  3015  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  3016  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  3017  				bundlesSvc.On("CreateBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, mock.Anything, mock.Anything).Return("", nil).Once()
  3018  				return bundlesSvc
  3019  			},
  3020  			clientFn: func() *automock.Client {
  3021  				client := &automock.Client{}
  3022  				doc := fixORDDocument()
  3023  				doc.ConsumptionBundles[0].CredentialExchangeStrategies = json.RawMessage(fmt.Sprintf(credentialExchangeStrategiesWithCustomTypeFormat, credentialExchangeStrategyType))
  3024  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, baseURL, nil)
  3025  				return client
  3026  			},
  3027  			apiSvcFn: func() *automock.APIService {
  3028  				apiSvc := &automock.APIService{}
  3029  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  3030  				return apiSvc
  3031  			},
  3032  			tombstoneSvcFn: func() *automock.TombstoneService {
  3033  				tombstoneSvc := &automock.TombstoneService{}
  3034  				return tombstoneSvc
  3035  			},
  3036  			eventSvcFn: func() *automock.EventService {
  3037  				eventSvc := &automock.EventService{}
  3038  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  3039  				return eventSvc
  3040  			},
  3041  			specSvcFn: func() *automock.SpecService {
  3042  				return &automock.SpecService{}
  3043  			},
  3044  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3045  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3046  		},
  3047  		{
  3048  			Name: "Does not resync resources if api list fails",
  3049  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3050  				persistTx := &persistenceautomock.PersistenceTx{}
  3051  				persistTx.On("Commit").Return(nil).Times(20)
  3052  
  3053  				transact := &persistenceautomock.Transactioner{}
  3054  				transact.On("Begin").Return(persistTx, nil).Times(20)
  3055  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(19)
  3056  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3057  				return persistTx, transact
  3058  			},
  3059  			appSvcFn:      successfulAppGet,
  3060  			tenantSvcFn:   successfulTenantSvc,
  3061  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3062  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3063  			vendorSvcFn:  successfulVendorUpdateForApplication,
  3064  			packageSvcFn: successfulPackageUpdateForApplication,
  3065  			bundleSvcFn:  successfulBundleUpdateForApplication,
  3066  			apiSvcFn: func() *automock.APIService {
  3067  				apiSvc := &automock.APIService{}
  3068  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  3069  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  3070  				return apiSvc
  3071  			},
  3072  			clientFn:                successfulClientFetch,
  3073  			eventSvcFn:              successfulEmptyEventList,
  3074  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3075  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3076  		},
  3077  		{
  3078  			Name: "Fails to list apis after resync",
  3079  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3080  				persistTx := &persistenceautomock.PersistenceTx{}
  3081  				persistTx.On("Commit").Return(nil).Times(23)
  3082  
  3083  				transact := &persistenceautomock.Transactioner{}
  3084  				transact.On("Begin").Return(persistTx, nil).Times(23)
  3085  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(22)
  3086  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3087  				return persistTx, transact
  3088  			},
  3089  			appSvcFn:      successfulAppGet,
  3090  			tenantSvcFn:   successfulTenantSvc,
  3091  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3092  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3093  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3094  			packageSvcFn:   successfulPackageUpdateForApplication,
  3095  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3096  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  3097  			specSvcFn:      successfulAPISpecUpdate,
  3098  			apiSvcFn: func() *automock.APIService {
  3099  				apiSvc := &automock.APIService{}
  3100  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3101  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3102  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
  3103  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api2ID, *sanitizedDoc.APIResources[1], nilSpecInput, map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, map[string]string{}, []string{}, api2PreSanitizedHash, "").Return(nil).Once()
  3104  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  3105  				return apiSvc
  3106  			},
  3107  			clientFn:                successfulClientFetch,
  3108  			eventSvcFn:              successfulEmptyEventList,
  3109  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3110  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3111  		},
  3112  		{
  3113  			Name: "Does not resync resources if fetching bundle ids for api fails",
  3114  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3115  				persistTx := &persistenceautomock.PersistenceTx{}
  3116  				persistTx.On("Commit").Return(nil).Times(20)
  3117  
  3118  				transact := &persistenceautomock.Transactioner{}
  3119  				transact.On("Begin").Return(persistTx, nil).Times(21)
  3120  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(20)
  3121  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3122  				return persistTx, transact
  3123  			},
  3124  			appSvcFn:      successfulAppGet,
  3125  			tenantSvcFn:   successfulTenantSvc,
  3126  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3127  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3128  			vendorSvcFn:  successfulVendorUpdateForApplication,
  3129  			packageSvcFn: successfulPackageUpdateForApplication,
  3130  			bundleSvcFn:  successfulBundleUpdateForApplication,
  3131  			bundleRefSvcFn: func() *automock.BundleReferenceService {
  3132  				bundleRefSvc := &automock.BundleReferenceService{}
  3133  				firstAPIID := api1ID
  3134  				bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleAPIReference, &firstAPIID).Return(nil, testErr).Once()
  3135  				return bundleRefSvc
  3136  			},
  3137  			apiSvcFn: func() *automock.APIService {
  3138  				apiSvc := &automock.APIService{}
  3139  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3140  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3141  
  3142  				return apiSvc
  3143  			},
  3144  			eventSvcFn:              successfulEmptyEventList,
  3145  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3146  			clientFn:                successfulClientFetch,
  3147  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3148  		},
  3149  		{
  3150  			Name: "Does not resync resources if api update fails",
  3151  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3152  				persistTx := &persistenceautomock.PersistenceTx{}
  3153  				persistTx.On("Commit").Return(nil).Times(20)
  3154  
  3155  				transact := &persistenceautomock.Transactioner{}
  3156  				transact.On("Begin").Return(persistTx, nil).Times(21)
  3157  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(20)
  3158  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3159  				return persistTx, transact
  3160  			},
  3161  			appSvcFn:      successfulAppGet,
  3162  			tenantSvcFn:   successfulTenantSvc,
  3163  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3164  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3165  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3166  			packageSvcFn:   successfulPackageUpdateForApplication,
  3167  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3168  			bundleRefSvcFn: successfulBundleReferenceFetchingOfAPIBundleIDs,
  3169  			apiSvcFn: func() *automock.APIService {
  3170  				apiSvc := &automock.APIService{}
  3171  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3172  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3173  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(testErr).Once()
  3174  				return apiSvc
  3175  			},
  3176  			eventSvcFn:              successfulEmptyEventList,
  3177  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3178  			clientFn:                successfulClientFetch,
  3179  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3180  		},
  3181  		{
  3182  			Name: "Does not resync resources if api create fails",
  3183  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3184  				persistTx := &persistenceautomock.PersistenceTx{}
  3185  				persistTx.On("Commit").Return(nil).Times(20)
  3186  
  3187  				transact := &persistenceautomock.Transactioner{}
  3188  				transact.On("Begin").Return(persistTx, nil).Times(21)
  3189  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(20)
  3190  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3191  				return persistTx, transact
  3192  			},
  3193  			appSvcFn:      successfulAppGet,
  3194  			tenantSvcFn:   successfulTenantSvc,
  3195  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3196  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductCreate,
  3197  			vendorSvcFn:  successfulVendorCreate,
  3198  			packageSvcFn: successfulPackageCreate,
  3199  			bundleSvcFn:  successfulBundleCreate,
  3200  			apiSvcFn: func() *automock.APIService {
  3201  				apiSvc := &automock.APIService{}
  3202  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  3203  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  3204  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return("", testErr).Once()
  3205  				return apiSvc
  3206  			},
  3207  			eventSvcFn:              successfulEmptyEventList,
  3208  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3209  			clientFn:                successfulClientFetch,
  3210  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3211  		},
  3212  		{
  3213  			Name: "Does not resync resources if api spec delete fails",
  3214  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3215  				persistTx := &persistenceautomock.PersistenceTx{}
  3216  				persistTx.On("Commit").Return(nil).Times(20)
  3217  
  3218  				transact := &persistenceautomock.Transactioner{}
  3219  				transact.On("Begin").Return(persistTx, nil).Times(21)
  3220  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(20)
  3221  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3222  				return persistTx, transact
  3223  			},
  3224  			appSvcFn:      successfulAppGet,
  3225  			tenantSvcFn:   successfulTenantSvc,
  3226  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3227  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3228  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3229  			packageSvcFn:   successfulPackageUpdateForApplication,
  3230  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3231  			bundleRefSvcFn: successfulBundleReferenceFetchingOfAPIBundleIDs,
  3232  			apiSvcFn: func() *automock.APIService {
  3233  				apiSvc := &automock.APIService{}
  3234  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3235  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3236  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
  3237  				return apiSvc
  3238  			},
  3239  			specSvcFn: func() *automock.SpecService {
  3240  				specSvc := &automock.SpecService{}
  3241  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(testErr).Once()
  3242  				return specSvc
  3243  			},
  3244  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3245  			clientFn:                successfulClientFetch,
  3246  			eventSvcFn:              successfulEmptyEventList,
  3247  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3248  		},
  3249  		{
  3250  			Name: "Does not resync resources if api spec create fails",
  3251  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3252  				persistTx := &persistenceautomock.PersistenceTx{}
  3253  				persistTx.On("Commit").Return(nil).Times(20)
  3254  
  3255  				transact := &persistenceautomock.Transactioner{}
  3256  				transact.On("Begin").Return(persistTx, nil).Times(21)
  3257  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(20)
  3258  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3259  				return persistTx, transact
  3260  			},
  3261  			appSvcFn:      successfulAppGet,
  3262  			tenantSvcFn:   successfulTenantSvc,
  3263  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3264  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3265  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3266  			packageSvcFn:   successfulPackageUpdateForApplication,
  3267  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3268  			bundleRefSvcFn: successfulBundleReferenceFetchingOfAPIBundleIDs,
  3269  			apiSvcFn: func() *automock.APIService {
  3270  				apiSvc := &automock.APIService{}
  3271  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3272  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3273  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
  3274  				return apiSvc
  3275  			},
  3276  			specSvcFn: func() *automock.SpecService {
  3277  				specSvc := &automock.SpecService{}
  3278  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(nil).Once()
  3279  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[0], resource.Application, model.APISpecReference, api1ID).Return("", nil, testErr).Once()
  3280  				return specSvc
  3281  			},
  3282  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3283  			clientFn:                successfulClientFetch,
  3284  			eventSvcFn:              successfulEmptyEventList,
  3285  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3286  		},
  3287  		{
  3288  			Name: "Does not resync resources if api spec list fails",
  3289  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3290  				persistTx := &persistenceautomock.PersistenceTx{}
  3291  				persistTx.On("Commit").Return(nil).Times(20)
  3292  
  3293  				transact := &persistenceautomock.Transactioner{}
  3294  				transact.On("Begin").Return(persistTx, nil).Times(21)
  3295  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(20)
  3296  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3297  				return persistTx, transact
  3298  			},
  3299  			appSvcFn:      successfulAppGet,
  3300  			tenantSvcFn:   successfulTenantSvc,
  3301  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3302  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3303  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3304  			packageSvcFn:   successfulPackageUpdateForApplication,
  3305  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3306  			bundleRefSvcFn: successfulBundleReferenceFetchingOfAPIBundleIDs,
  3307  			apiSvcFn: func() *automock.APIService {
  3308  				apiSvc := &automock.APIService{}
  3309  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIsNoVersionBump(), nil).Twice()
  3310  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
  3311  				return apiSvc
  3312  			},
  3313  			specSvcFn: func() *automock.SpecService {
  3314  				specSvc := &automock.SpecService{}
  3315  				specSvc.On("ListIDByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(nil, testErr).Once()
  3316  				return specSvc
  3317  			},
  3318  			eventSvcFn:              successfulEmptyEventList,
  3319  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3320  			clientFn:                successfulClientFetch,
  3321  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3322  		},
  3323  		{
  3324  			Name: "Does not resync resources if api spec get fetch request fails",
  3325  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3326  				persistTx := &persistenceautomock.PersistenceTx{}
  3327  				persistTx.On("Commit").Return(nil).Times(20)
  3328  
  3329  				transact := &persistenceautomock.Transactioner{}
  3330  				transact.On("Begin").Return(persistTx, nil).Times(21)
  3331  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(20)
  3332  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3333  				return persistTx, transact
  3334  			},
  3335  			appSvcFn:       successfulAppGet,
  3336  			tenantSvcFn:    successfulTenantSvc,
  3337  			webhookSvcFn:   successfulTenantMappingOnlyCreation,
  3338  			webhookConvFn:  successfulWebhookConversion,
  3339  			productSvcFn:   successfulProductUpdateForApplication,
  3340  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3341  			packageSvcFn:   successfulPackageUpdateForApplication,
  3342  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3343  			bundleRefSvcFn: successfulBundleReferenceFetchingOfAPIBundleIDs,
  3344  			apiSvcFn: func() *automock.APIService {
  3345  				apiSvc := &automock.APIService{}
  3346  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIsNoVersionBump(), nil).Twice()
  3347  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
  3348  				return apiSvc
  3349  			},
  3350  			specSvcFn: func() *automock.SpecService {
  3351  				specSvc := &automock.SpecService{}
  3352  				specSvc.On("ListIDByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(fixAPI1IDs(), nil).Once()
  3353  				specSvc.On("ListFetchRequestsByReferenceObjectIDs", txtest.CtxWithDBMatcher(), tenantID, fixAPI1IDs(), model.APISpecReference).Return(nil, testErr).Once()
  3354  				return specSvc
  3355  			},
  3356  			eventSvcFn:              successfulEmptyEventList,
  3357  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3358  			clientFn:                successfulClientFetch,
  3359  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3360  		},
  3361  		{
  3362  			Name: "Resync resources returns error if api spec refetch fails",
  3363  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3364  				persistTx := &persistenceautomock.PersistenceTx{}
  3365  				persistTx.On("Commit").Return(nil).Times(32)
  3366  
  3367  				transact := &persistenceautomock.Transactioner{}
  3368  				transact.On("Begin").Return(persistTx, nil).Times(32)
  3369  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(32)
  3370  				return persistTx, transact
  3371  			},
  3372  			appSvcFn:      successfulAppGet,
  3373  			tenantSvcFn:   successfulTenantSvc,
  3374  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3375  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3376  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3377  			packageSvcFn:   successfulPackageUpdateForApplication,
  3378  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3379  			bundleRefSvcFn: successfulBundleReferenceFetchingOfAPIBundleIDs,
  3380  			apiSvcFn: func() *automock.APIService {
  3381  				apiSvc := &automock.APIService{}
  3382  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIsNoVersionBump(), nil).Times(3)
  3383  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
  3384  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api2ID, *sanitizedDoc.APIResources[1], nilSpecInput, map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, map[string]string{}, []string{}, api2PreSanitizedHash, "").Return(nil).Once()
  3385  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  3386  				return apiSvc
  3387  			},
  3388  			specSvcFn: func() *automock.SpecService {
  3389  				specSvc := &automock.SpecService{}
  3390  				specSvc.On("ListIDByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(fixAPI1IDs(), nil).Once()
  3391  				specSvc.On("ListFetchRequestsByReferenceObjectIDs", txtest.CtxWithDBMatcher(), tenantID, fixAPI1IDs(), model.APISpecReference).Return([]*model.FetchRequest{fixFailedFetchRequest(), fixFailedFetchRequest(), fixFailedFetchRequest()}, nil).Once()
  3392  
  3393  				specSvc.On("ListIDByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api2ID).Return(fixAPI2IDs(), nil).Once()
  3394  				specSvc.On("ListFetchRequestsByReferenceObjectIDs", txtest.CtxWithDBMatcher(), tenantID, []string{api2spec1ID, api2spec2ID}, model.APISpecReference).Return([]*model.FetchRequest{fixFailedFetchRequest(), fixFailedFetchRequest(), fixFailedFetchRequest()}, nil).Once()
  3395  
  3396  				event1Spec := fixEvent1SpecInputs()[0]
  3397  				event2Spec := fixEvent2SpecInputs()[0]
  3398  
  3399  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event1Spec, resource.Application, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event1Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
  3400  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event2Spec, resource.Application, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event2Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
  3401  
  3402  				specSvc.On("GetByID", txtest.CtxWithDBMatcher(), mock.Anything, mock.Anything).Return(&testSpec, nil).
  3403  					Times(len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs()))
  3404  
  3405  				expectedSpecToUpdate := testSpec
  3406  				expectedSpecToUpdate.Data = &testSpecData
  3407  				specSvc.On("UpdateSpecOnly", txtest.CtxWithDBMatcher(), expectedSpecToUpdate).Return(nil).
  3408  					Times(len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs()))
  3409  
  3410  				return specSvc
  3411  			},
  3412  			fetchReqFn: func() *automock.FetchRequestService {
  3413  				fetchReqSvc := &automock.FetchRequestService{}
  3414  				fetchReqSvc.On("FetchSpec", txtest.CtxWithDBMatcher(), mock.Anything).Return(nil, &model.FetchRequestStatus{Condition: model.FetchRequestStatusConditionFailed}).
  3415  					Times(len(fixAPI1SpecInputs()))
  3416  				fetchReqSvc.On("FetchSpec", txtest.CtxWithDBMatcher(), mock.Anything).Return(&testSpecData, &model.FetchRequestStatus{Condition: model.FetchRequestStatusConditionSucceeded}).
  3417  					Times(len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs()))
  3418  
  3419  				fetchReqSvc.On("Update", txtest.CtxWithDBMatcher(), mock.MatchedBy(func(actual *model.FetchRequest) bool {
  3420  					return actual.Status.Condition == model.FetchRequestStatusConditionFailed
  3421  				})).Return(nil).
  3422  					Times(len(fixAPI1SpecInputs()))
  3423  				fetchReqSvc.On("Update", txtest.CtxWithDBMatcher(), mock.MatchedBy(func(actual *model.FetchRequest) bool {
  3424  					return actual.Status.Condition == model.FetchRequestStatusConditionSucceeded
  3425  				})).Return(nil).
  3426  					Times(len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs()))
  3427  
  3428  				return fetchReqSvc
  3429  			},
  3430  			eventSvcFn: func() *automock.EventService {
  3431  				eventSvc := &automock.EventService{}
  3432  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Twice()
  3433  				eventSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.EventResources[0], ([]*model.SpecInput)(nil), []string{bundleID}, mock.Anything, "").Return("", nil).Once()
  3434  				eventSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.EventResources[1], ([]*model.SpecInput)(nil), []string{bundleID}, mock.Anything, "").Return("", nil).Once()
  3435  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  3436  
  3437  				return eventSvc
  3438  			},
  3439  			tombstoneSvcFn: func() *automock.TombstoneService {
  3440  				tombstoneSvc := &automock.TombstoneService{}
  3441  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  3442  				tombstoneSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, tombstoneID, *sanitizedDoc.Tombstones[0]).Return(nil).Once()
  3443  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  3444  				return tombstoneSvc
  3445  			},
  3446  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3447  			clientFn:                successfulClientFetch,
  3448  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3449  		},
  3450  		{
  3451  			Name: "Does not resync resources if event list fails",
  3452  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3453  				persistTx := &persistenceautomock.PersistenceTx{}
  3454  				persistTx.On("Commit").Return(nil).Times(24)
  3455  
  3456  				transact := &persistenceautomock.Transactioner{}
  3457  				transact.On("Begin").Return(persistTx, nil).Times(24)
  3458  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(23)
  3459  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3460  				return persistTx, transact
  3461  			},
  3462  			appSvcFn:      successfulAppGet,
  3463  			tenantSvcFn:   successfulTenantSvc,
  3464  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3465  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3466  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3467  			packageSvcFn:   successfulPackageUpdateForApplication,
  3468  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3469  			bundleRefSvcFn: successfulBundleReferenceFetchingOfAPIBundleIDs,
  3470  			apiSvcFn:       successfulAPIUpdate,
  3471  			specSvcFn:      successfulAPISpecUpdate,
  3472  			eventSvcFn: func() *automock.EventService {
  3473  				eventSvc := &automock.EventService{}
  3474  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  3475  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  3476  				return eventSvc
  3477  			},
  3478  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3479  			clientFn:                successfulClientFetch,
  3480  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3481  		},
  3482  		{
  3483  			Name: "Fails to list events after resync",
  3484  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3485  				persistTx := &persistenceautomock.PersistenceTx{}
  3486  				persistTx.On("Commit").Return(nil).Times(26)
  3487  
  3488  				transact := &persistenceautomock.Transactioner{}
  3489  				transact.On("Begin").Return(persistTx, nil).Times(27)
  3490  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(26)
  3491  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3492  				return persistTx, transact
  3493  			},
  3494  			appSvcFn:      successfulAppGet,
  3495  			tenantSvcFn:   successfulTenantSvc,
  3496  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3497  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3498  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3499  			packageSvcFn:   successfulPackageUpdateForApplication,
  3500  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3501  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  3502  			apiSvcFn:       successfulAPIUpdate,
  3503  			specSvcFn: func() *automock.SpecService {
  3504  				specSvc := &automock.SpecService{}
  3505  
  3506  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(nil).Once()
  3507  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[0], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3508  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[1], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3509  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[2], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3510  
  3511  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api2ID).Return(nil).Once()
  3512  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI2SpecInputs()[0], resource.Application, model.APISpecReference, api2ID).Return("", nil, nil).Once()
  3513  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI2SpecInputs()[1], resource.Application, model.APISpecReference, api2ID).Return("", nil, nil).Once()
  3514  
  3515  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event1ID).Return(nil).Once()
  3516  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event2ID).Return(nil).Once()
  3517  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixEvent1SpecInputs()[0], resource.Application, model.EventSpecReference, event1ID).Return("", nil, nil).Once()
  3518  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixEvent2SpecInputs()[0], resource.Application, model.EventSpecReference, event2ID).Return("", nil, nil).Once()
  3519  
  3520  				return specSvc
  3521  			},
  3522  			eventSvcFn: func() *automock.EventService {
  3523  				eventSvc := &automock.EventService{}
  3524  				eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event1ID, *sanitizedDoc.EventResources[0], nilSpecInput, []string{bundleID}, []string{}, []string{}, event1PreSanitizedHash, "").Return(nil).Once()
  3525  				eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event2ID, *sanitizedDoc.EventResources[1], nilSpecInput, []string{bundleID}, []string{}, []string{}, event2PreSanitizedHash, "").Return(nil).Once()
  3526  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Twice()
  3527  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  3528  				return eventSvc
  3529  			},
  3530  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3531  			clientFn:                successfulClientFetch,
  3532  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3533  		},
  3534  		{
  3535  			Name: "Does not resync resources if fetching bundle ids for event fails",
  3536  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3537  				persistTx := &persistenceautomock.PersistenceTx{}
  3538  				persistTx.On("Commit").Return(nil).Times(24)
  3539  
  3540  				transact := &persistenceautomock.Transactioner{}
  3541  				transact.On("Begin").Return(persistTx, nil).Times(25)
  3542  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(24)
  3543  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3544  				return persistTx, transact
  3545  			},
  3546  			appSvcFn:      successfulAppGet,
  3547  			tenantSvcFn:   successfulTenantSvc,
  3548  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3549  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3550  			vendorSvcFn:  successfulVendorUpdateForApplication,
  3551  			packageSvcFn: successfulPackageUpdateForApplication,
  3552  			bundleSvcFn:  successfulBundleUpdateForApplication,
  3553  			bundleRefSvcFn: func() *automock.BundleReferenceService {
  3554  				bundleRefSvc := &automock.BundleReferenceService{}
  3555  				firstAPIID := api1ID
  3556  				secondAPIID := api2ID
  3557  				firstEventID := event1ID
  3558  				bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleAPIReference, &firstAPIID).Return([]string{bundleID}, nil).Once()
  3559  				bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleAPIReference, &secondAPIID).Return([]string{bundleID}, nil).Once()
  3560  				bundleRefSvc.On("GetBundleIDsForObject", txtest.CtxWithDBMatcher(), model.BundleEventReference, &firstEventID).Return(nil, testErr).Once()
  3561  				return bundleRefSvc
  3562  			},
  3563  			apiSvcFn:  successfulAPIUpdate,
  3564  			specSvcFn: successfulAPISpecUpdate,
  3565  			eventSvcFn: func() *automock.EventService {
  3566  				eventSvc := &automock.EventService{}
  3567  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  3568  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  3569  				return eventSvc
  3570  			},
  3571  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3572  			clientFn:                successfulClientFetch,
  3573  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3574  		},
  3575  		{
  3576  			Name: "Does not resync resources if event update fails",
  3577  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3578  				persistTx := &persistenceautomock.PersistenceTx{}
  3579  				persistTx.On("Commit").Return(nil).Times(24)
  3580  
  3581  				transact := &persistenceautomock.Transactioner{}
  3582  				transact.On("Begin").Return(persistTx, nil).Times(25)
  3583  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(24)
  3584  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3585  				return persistTx, transact
  3586  			},
  3587  			appSvcFn:      successfulAppGet,
  3588  			tenantSvcFn:   successfulTenantSvc,
  3589  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3590  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3591  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3592  			packageSvcFn:   successfulPackageUpdateForApplication,
  3593  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3594  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  3595  			apiSvcFn:       successfulAPIUpdate,
  3596  			specSvcFn:      successfulAPISpecUpdate,
  3597  			eventSvcFn: func() *automock.EventService {
  3598  				eventSvc := &automock.EventService{}
  3599  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  3600  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  3601  				eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event1ID, *sanitizedDoc.EventResources[0], nilSpecInput, []string{bundleID}, []string{}, []string{}, event1PreSanitizedHash, "").Return(testErr).Once()
  3602  				return eventSvc
  3603  			},
  3604  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3605  			clientFn:                successfulClientFetch,
  3606  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3607  		},
  3608  		{
  3609  			Name: "Does not resync specification resources if event create fails",
  3610  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3611  				persistTx := &persistenceautomock.PersistenceTx{}
  3612  				persistTx.On("Commit").Return(nil).Times(24)
  3613  
  3614  				transact := &persistenceautomock.Transactioner{}
  3615  				transact.On("Begin").Return(persistTx, nil).Times(25)
  3616  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(24)
  3617  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3618  				return persistTx, transact
  3619  			},
  3620  			appSvcFn:      successfulAppGet,
  3621  			tenantSvcFn:   successfulTenantSvc,
  3622  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3623  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductCreate,
  3624  			vendorSvcFn:    successfulVendorCreate,
  3625  			packageSvcFn:   successfulPackageCreate,
  3626  			bundleSvcFn:    successfulBundleCreate,
  3627  			bundleRefSvcFn: successfulBundleReferenceFetchingOfAPIBundleIDs,
  3628  			apiSvcFn:       successfulAPICreate,
  3629  			eventSvcFn: func() *automock.EventService {
  3630  				eventSvc := &automock.EventService{}
  3631  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  3632  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  3633  				eventSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.EventResources[0], ([]*model.SpecInput)(nil), []string{bundleID}, mock.Anything, "").Return("", testErr).Once()
  3634  				return eventSvc
  3635  			},
  3636  			specSvcFn: func() *automock.SpecService {
  3637  				specSvc := &automock.SpecService{}
  3638  
  3639  				api1SpecInput1 := fixAPI1SpecInputs()[0]
  3640  				api1SpecInput2 := fixAPI1SpecInputs()[1]
  3641  				api1SpecInput3 := fixAPI1SpecInputs()[2]
  3642  
  3643  				api2SpecInput1 := fixAPI2SpecInputs()[0]
  3644  				api2SpecInput2 := fixAPI2SpecInputs()[1]
  3645  
  3646  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  3647  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  3648  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  3649  
  3650  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  3651  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  3652  
  3653  				return specSvc
  3654  			},
  3655  			fetchReqFn: func() *automock.FetchRequestService {
  3656  				fetchReqSvc := &automock.FetchRequestService{}
  3657  				fetchReqSvc.On("FetchSpec", txtest.CtxWithDBMatcher(), mock.Anything).Return(nil, &model.FetchRequestStatus{Condition: model.FetchRequestStatusConditionSucceeded}).
  3658  					Times(len(fixAPI1SpecInputs()) + len(fixAPI2SpecInputs()))
  3659  
  3660  				fetchReqSvc.On("Update", txtest.CtxWithDBMatcher(), mock.MatchedBy(func(actual *model.FetchRequest) bool {
  3661  					return actual.Status.Condition == model.FetchRequestStatusConditionSucceeded
  3662  				})).Return(nil).
  3663  					Times(len(fixAPI1SpecInputs()) + len(fixAPI2SpecInputs()))
  3664  
  3665  				return fetchReqSvc
  3666  			},
  3667  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3668  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3669  			clientFn:                successfulClientFetch,
  3670  		},
  3671  		{
  3672  			Name: "Does not resync resources if event spec delete fails",
  3673  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3674  				persistTx := &persistenceautomock.PersistenceTx{}
  3675  				persistTx.On("Commit").Return(nil).Times(24)
  3676  
  3677  				transact := &persistenceautomock.Transactioner{}
  3678  				transact.On("Begin").Return(persistTx, nil).Times(25)
  3679  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(24)
  3680  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3681  				return persistTx, transact
  3682  			},
  3683  			appSvcFn:      successfulAppGet,
  3684  			tenantSvcFn:   successfulTenantSvc,
  3685  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3686  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3687  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3688  			packageSvcFn:   successfulPackageUpdateForApplication,
  3689  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3690  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  3691  			apiSvcFn:       successfulAPIUpdate,
  3692  			specSvcFn: func() *automock.SpecService {
  3693  				specSvc := &automock.SpecService{}
  3694  
  3695  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(nil).Once()
  3696  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[0], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3697  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[1], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3698  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[2], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3699  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api2ID).Return(nil).Once()
  3700  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI2SpecInputs()[0], resource.Application, model.APISpecReference, api2ID).Return("", nil, nil).Once()
  3701  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI2SpecInputs()[1], resource.Application, model.APISpecReference, api2ID).Return("", nil, nil).Once()
  3702  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event1ID).Return(testErr).Once()
  3703  				return specSvc
  3704  			},
  3705  			eventSvcFn: func() *automock.EventService {
  3706  				eventSvc := &automock.EventService{}
  3707  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  3708  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  3709  				eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event1ID, *sanitizedDoc.EventResources[0], nilSpecInput, []string{bundleID}, []string{}, []string{}, event1PreSanitizedHash, "").Return(nil).Once()
  3710  				return eventSvc
  3711  			},
  3712  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3713  			clientFn:                successfulClientFetch,
  3714  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3715  		},
  3716  		{
  3717  			Name: "Does not resync resources if event spec create fails",
  3718  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3719  				persistTx := &persistenceautomock.PersistenceTx{}
  3720  				persistTx.On("Commit").Return(nil).Times(24)
  3721  
  3722  				transact := &persistenceautomock.Transactioner{}
  3723  				transact.On("Begin").Return(persistTx, nil).Times(25)
  3724  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(24)
  3725  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3726  				return persistTx, transact
  3727  			},
  3728  			appSvcFn:      successfulAppGet,
  3729  			tenantSvcFn:   successfulTenantSvc,
  3730  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3731  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3732  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3733  			packageSvcFn:   successfulPackageUpdateForApplication,
  3734  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3735  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  3736  			apiSvcFn:       successfulAPIUpdate,
  3737  			specSvcFn: func() *automock.SpecService {
  3738  				specSvc := &automock.SpecService{}
  3739  
  3740  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(nil).Once()
  3741  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[0], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3742  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[1], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3743  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[2], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3744  
  3745  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api2ID).Return(nil).Once()
  3746  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI2SpecInputs()[0], resource.Application, model.APISpecReference, api2ID).Return("", nil, nil).Once()
  3747  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI2SpecInputs()[1], resource.Application, model.APISpecReference, api2ID).Return("", nil, nil).Once()
  3748  
  3749  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event1ID).Return(nil).Once()
  3750  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixEvent1SpecInputs()[0], resource.Application, model.EventSpecReference, event1ID).Return("", nil, testErr).Once()
  3751  				return specSvc
  3752  			},
  3753  			eventSvcFn: func() *automock.EventService {
  3754  				eventSvc := &automock.EventService{}
  3755  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  3756  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  3757  				eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event1ID, *sanitizedDoc.EventResources[0], nilSpecInput, []string{bundleID}, []string{}, []string{}, event1PreSanitizedHash, "").Return(nil).Once()
  3758  				return eventSvc
  3759  			},
  3760  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3761  			clientFn:                successfulClientFetch,
  3762  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3763  		},
  3764  		{
  3765  			Name: "Does not resync resources if event spec list fails",
  3766  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3767  				persistTx := &persistenceautomock.PersistenceTx{}
  3768  				persistTx.On("Commit").Return(nil).Times(24)
  3769  
  3770  				transact := &persistenceautomock.Transactioner{}
  3771  				transact.On("Begin").Return(persistTx, nil).Times(25)
  3772  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(24)
  3773  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3774  				return persistTx, transact
  3775  			},
  3776  			appSvcFn:      successfulAppGet,
  3777  			tenantSvcFn:   successfulTenantSvc,
  3778  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3779  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3780  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3781  			packageSvcFn:   successfulPackageUpdateForApplication,
  3782  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3783  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  3784  			apiSvcFn:       successfulAPIUpdate,
  3785  			specSvcFn: func() *automock.SpecService {
  3786  				specSvc := &automock.SpecService{}
  3787  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(nil).Once()
  3788  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[0], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3789  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[1], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3790  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[2], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3791  
  3792  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api2ID).Return(nil).Once()
  3793  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI2SpecInputs()[0], resource.Application, model.APISpecReference, api2ID).Return("", nil, nil).Once()
  3794  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI2SpecInputs()[1], resource.Application, model.APISpecReference, api2ID).Return("", nil, nil).Once()
  3795  
  3796  				specSvc.On("ListIDByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event1ID).Return(nil, testErr).Once()
  3797  				return specSvc
  3798  			},
  3799  			eventSvcFn: func() *automock.EventService {
  3800  				eventSvc := &automock.EventService{}
  3801  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEventsNoVersionBump(), nil).Twice()
  3802  				eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event1ID, *sanitizedDoc.EventResources[0], nilSpecInput, []string{bundleID}, []string{}, []string{}, event1PreSanitizedHash, "").Return(nil).Once()
  3803  				return eventSvc
  3804  			},
  3805  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3806  			clientFn:                successfulClientFetch,
  3807  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3808  		},
  3809  		{
  3810  			Name: "Does not resync resources if event spec get fetch request fails",
  3811  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3812  				persistTx := &persistenceautomock.PersistenceTx{}
  3813  				persistTx.On("Commit").Return(nil).Times(24)
  3814  
  3815  				transact := &persistenceautomock.Transactioner{}
  3816  				transact.On("Begin").Return(persistTx, nil).Times(25)
  3817  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(24)
  3818  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3819  				return persistTx, transact
  3820  			},
  3821  			appSvcFn:      successfulAppGet,
  3822  			tenantSvcFn:   successfulTenantSvc,
  3823  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3824  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3825  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3826  			packageSvcFn:   successfulPackageUpdateForApplication,
  3827  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3828  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  3829  			apiSvcFn:       successfulAPIUpdate,
  3830  			specSvcFn: func() *automock.SpecService {
  3831  				specSvc := &automock.SpecService{}
  3832  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(nil).Once()
  3833  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[0], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3834  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[1], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3835  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[2], resource.Application, model.APISpecReference, api1ID).Return("", nil, nil).Once()
  3836  
  3837  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api2ID).Return(nil).Once()
  3838  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI2SpecInputs()[0], resource.Application, model.APISpecReference, api2ID).Return("", nil, nil).Once()
  3839  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI2SpecInputs()[1], resource.Application, model.APISpecReference, api2ID).Return("", nil, nil).Once()
  3840  
  3841  				specSvc.On("ListIDByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event1ID).Return(fixEvent1IDs(), nil).Once()
  3842  				specSvc.On("ListFetchRequestsByReferenceObjectIDs", txtest.CtxWithDBMatcher(), tenantID, fixEvent1IDs(), model.EventSpecReference).Return(nil, testErr).Once()
  3843  				return specSvc
  3844  			},
  3845  			eventSvcFn: func() *automock.EventService {
  3846  				eventSvc := &automock.EventService{}
  3847  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEventsNoVersionBump(), nil).Twice()
  3848  				eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event1ID, *sanitizedDoc.EventResources[0], nilSpecInput, []string{bundleID}, []string{}, []string{}, event1PreSanitizedHash, "").Return(nil).Once()
  3849  				return eventSvc
  3850  			},
  3851  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3852  			clientFn:                successfulClientFetch,
  3853  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3854  		},
  3855  		{
  3856  			Name: "Resync resources returns error if event spec refetch fails",
  3857  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3858  				persistTx := &persistenceautomock.PersistenceTx{}
  3859  				persistTx.On("Commit").Return(nil).Times(32)
  3860  
  3861  				transact := &persistenceautomock.Transactioner{}
  3862  				transact.On("Begin").Return(persistTx, nil).Times(32)
  3863  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(32)
  3864  				return persistTx, transact
  3865  			},
  3866  			appSvcFn:      successfulAppGet,
  3867  			tenantSvcFn:   successfulTenantSvc,
  3868  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3869  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3870  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3871  			packageSvcFn:   successfulPackageUpdateForApplication,
  3872  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3873  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  3874  			apiSvcFn: func() *automock.APIService {
  3875  				apiSvc := &automock.APIService{}
  3876  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3877  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3878  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
  3879  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api2ID, *sanitizedDoc.APIResources[1], nilSpecInput, map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, map[string]string{}, []string{}, api2PreSanitizedHash, "").Return(nil).Once()
  3880  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  3881  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  3882  				return apiSvc
  3883  			},
  3884  			specSvcFn: func() *automock.SpecService {
  3885  				specSvc := &automock.SpecService{}
  3886  
  3887  				api1SpecInput1 := fixAPI1SpecInputs()[0]
  3888  				api1SpecInput2 := fixAPI1SpecInputs()[1]
  3889  				api1SpecInput3 := fixAPI1SpecInputs()[2]
  3890  
  3891  				api2SpecInput1 := fixAPI2SpecInputs()[0]
  3892  				api2SpecInput2 := fixAPI2SpecInputs()[1]
  3893  
  3894  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api1ID).Return(nil).Once()
  3895  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[0], resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  3896  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[1], resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  3897  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI1SpecInputs()[2], resource.Application, model.APISpecReference, api1ID).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  3898  
  3899  				specSvc.On("DeleteByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.APISpecReference, api2ID).Return(nil).Once()
  3900  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI2SpecInputs()[0], resource.Application, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  3901  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *fixAPI2SpecInputs()[1], resource.Application, model.APISpecReference, api2ID).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  3902  
  3903  				specSvc.On("ListIDByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event1ID).Return(fixEvent1IDs(), nil).Once()
  3904  				specSvc.On("ListIDByReferenceObjectID", txtest.CtxWithDBMatcher(), resource.Application, model.EventSpecReference, event2ID).Return(fixEvent2IDs(), nil).Once()
  3905  				specSvc.On("ListFetchRequestsByReferenceObjectIDs", txtest.CtxWithDBMatcher(), tenantID, fixEvent1IDs(), model.EventSpecReference).Return([]*model.FetchRequest{fixFailedFetchRequest()}, nil).Once()
  3906  				specSvc.On("ListFetchRequestsByReferenceObjectIDs", txtest.CtxWithDBMatcher(), tenantID, fixEvent2IDs(), model.EventSpecReference).Return([]*model.FetchRequest{fixFailedFetchRequest()}, nil).Once()
  3907  
  3908  				specSvc.On("GetByID", txtest.CtxWithDBMatcher(), mock.Anything, mock.Anything).Return(&testSpec, nil).
  3909  					Times(len(fixAPI1SpecInputs()))
  3910  
  3911  				expectedSpecToUpdate := testSpec
  3912  				expectedSpecToUpdate.Data = &testSpecData
  3913  				specSvc.On("UpdateSpecOnly", txtest.CtxWithDBMatcher(), expectedSpecToUpdate).Return(nil).
  3914  					Times(len(fixAPI1SpecInputs()))
  3915  
  3916  				return specSvc
  3917  			},
  3918  			eventSvcFn: func() *automock.EventService {
  3919  				eventSvc := &automock.EventService{}
  3920  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEventsNoVersionBump(), nil).Times(3)
  3921  				eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event1ID, *sanitizedDoc.EventResources[0], nilSpecInput, []string{bundleID}, []string{}, []string{}, event1PreSanitizedHash, "").Return(nil).Once()
  3922  				eventSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, event2ID, *sanitizedDoc.EventResources[1], nilSpecInput, []string{bundleID}, []string{}, []string{}, event2PreSanitizedHash, "").Return(nil).Once()
  3923  				return eventSvc
  3924  			},
  3925  			fetchReqFn: func() *automock.FetchRequestService {
  3926  				fetchReqSvc := &automock.FetchRequestService{}
  3927  				fetchReqSvc.On("FetchSpec", txtest.CtxWithDBMatcher(), mock.Anything).Return(&testSpecData, &model.FetchRequestStatus{Condition: model.FetchRequestStatusConditionSucceeded}).
  3928  					Times(len(fixAPI1SpecInputs()))
  3929  				fetchReqSvc.On("FetchSpec", txtest.CtxWithDBMatcher(), mock.Anything).Return(&testSpecData, &model.FetchRequestStatus{Condition: model.FetchRequestStatusConditionFailed}).
  3930  					Times(len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs()))
  3931  
  3932  				fetchReqSvc.On("Update", txtest.CtxWithDBMatcher(), mock.MatchedBy(func(actual *model.FetchRequest) bool {
  3933  					return actual.Status.Condition == model.FetchRequestStatusConditionSucceeded
  3934  				})).Return(nil).
  3935  					Times(len(fixAPI1SpecInputs()))
  3936  				fetchReqSvc.On("Update", txtest.CtxWithDBMatcher(), mock.MatchedBy(func(actual *model.FetchRequest) bool {
  3937  					return actual.Status.Condition == model.FetchRequestStatusConditionFailed
  3938  				})).Return(nil).
  3939  					Times(len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs()))
  3940  
  3941  				return fetchReqSvc
  3942  			},
  3943  			tombstoneSvcFn: func() *automock.TombstoneService {
  3944  				tombstoneSvc := &automock.TombstoneService{}
  3945  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  3946  				tombstoneSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, tombstoneID, *sanitizedDoc.Tombstones[0]).Return(nil).Once()
  3947  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  3948  				return tombstoneSvc
  3949  			},
  3950  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3951  			clientFn:                successfulClientFetch,
  3952  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3953  		},
  3954  		{
  3955  			Name: "Does not resync resources if tombstone list fails",
  3956  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3957  				persistTx := &persistenceautomock.PersistenceTx{}
  3958  				persistTx.On("Commit").Return(nil).Times(28)
  3959  
  3960  				transact := &persistenceautomock.Transactioner{}
  3961  				transact.On("Begin").Return(persistTx, nil).Times(28)
  3962  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(27)
  3963  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3964  				return persistTx, transact
  3965  			},
  3966  			appSvcFn:      successfulAppGet,
  3967  			tenantSvcFn:   successfulTenantSvc,
  3968  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  3969  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  3970  			vendorSvcFn:    successfulVendorUpdateForApplication,
  3971  			packageSvcFn:   successfulPackageUpdateForApplication,
  3972  			bundleSvcFn:    successfulBundleUpdateForApplication,
  3973  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  3974  			apiSvcFn:       successfulAPIUpdate,
  3975  			eventSvcFn:     successfulEventUpdate,
  3976  			specSvcFn:      successfulSpecRecreate,
  3977  			tombstoneSvcFn: func() *automock.TombstoneService {
  3978  				tombstoneSvc := &automock.TombstoneService{}
  3979  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  3980  				return tombstoneSvc
  3981  			},
  3982  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  3983  			clientFn:                successfulClientFetch,
  3984  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  3985  		},
  3986  		{
  3987  			Name: "Fails to list tombstones after resync",
  3988  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  3989  				persistTx := &persistenceautomock.PersistenceTx{}
  3990  				persistTx.On("Commit").Return(nil).Times(30)
  3991  
  3992  				transact := &persistenceautomock.Transactioner{}
  3993  				transact.On("Begin").Return(persistTx, nil).Times(30)
  3994  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(29)
  3995  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  3996  				return persistTx, transact
  3997  			},
  3998  			appSvcFn:      successfulAppGet,
  3999  			tenantSvcFn:   successfulTenantSvc,
  4000  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4001  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  4002  			vendorSvcFn:    successfulVendorUpdateForApplication,
  4003  			packageSvcFn:   successfulPackageUpdateForApplication,
  4004  			bundleSvcFn:    successfulBundleUpdateForApplication,
  4005  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  4006  			apiSvcFn:       successfulAPIUpdate,
  4007  			eventSvcFn:     successfulEventUpdate,
  4008  			specSvcFn:      successfulSpecRecreate,
  4009  			tombstoneSvcFn: func() *automock.TombstoneService {
  4010  				tombstoneSvc := &automock.TombstoneService{}
  4011  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  4012  				tombstoneSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, tombstoneID, *sanitizedDoc.Tombstones[0]).Return(nil).Once()
  4013  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  4014  				return tombstoneSvc
  4015  			},
  4016  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  4017  			clientFn:                successfulClientFetch,
  4018  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4019  		},
  4020  		{
  4021  			Name: "Does not resync resources if tombstone update fails",
  4022  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4023  				persistTx := &persistenceautomock.PersistenceTx{}
  4024  				persistTx.On("Commit").Return(nil).Times(28)
  4025  
  4026  				transact := &persistenceautomock.Transactioner{}
  4027  				transact.On("Begin").Return(persistTx, nil).Times(29)
  4028  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(28)
  4029  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  4030  				return persistTx, transact
  4031  			},
  4032  			appSvcFn:      successfulAppGet,
  4033  			tenantSvcFn:   successfulTenantSvc,
  4034  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4035  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductUpdateForApplication,
  4036  			vendorSvcFn:    successfulVendorUpdateForApplication,
  4037  			packageSvcFn:   successfulPackageUpdateForApplication,
  4038  			bundleSvcFn:    successfulBundleUpdateForApplication,
  4039  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  4040  			apiSvcFn:       successfulAPIUpdate,
  4041  			eventSvcFn:     successfulEventUpdate,
  4042  			specSvcFn:      successfulSpecRecreate,
  4043  			tombstoneSvcFn: func() *automock.TombstoneService {
  4044  				tombstoneSvc := &automock.TombstoneService{}
  4045  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  4046  				tombstoneSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, tombstoneID, *sanitizedDoc.Tombstones[0]).Return(testErr).Once()
  4047  				return tombstoneSvc
  4048  			},
  4049  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  4050  			clientFn:                successfulClientFetch,
  4051  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4052  		},
  4053  		{
  4054  			Name: "Does not resync resources if tombstone create fails",
  4055  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4056  				persistTx := &persistenceautomock.PersistenceTx{}
  4057  				persistTx.On("Commit").Return(nil).Times(28)
  4058  
  4059  				transact := &persistenceautomock.Transactioner{}
  4060  				transact.On("Begin").Return(persistTx, nil).Times(29)
  4061  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(28)
  4062  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  4063  				return persistTx, transact
  4064  			},
  4065  			appSvcFn:      successfulAppGet,
  4066  			tenantSvcFn:   successfulTenantSvc,
  4067  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4068  			webhookConvFn: successfulWebhookConversion, productSvcFn: successfulProductCreate,
  4069  			vendorSvcFn:  successfulVendorCreate,
  4070  			packageSvcFn: successfulPackageCreate,
  4071  			bundleSvcFn:  successfulBundleCreate,
  4072  			apiSvcFn:     successfulAPICreate,
  4073  			eventSvcFn:   successfulEventCreate,
  4074  			specSvcFn:    successfulSpecCreate,
  4075  			tombstoneSvcFn: func() *automock.TombstoneService {
  4076  				tombstoneSvc := &automock.TombstoneService{}
  4077  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4078  				tombstoneSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Tombstones[0]).Return("", testErr).Once()
  4079  				return tombstoneSvc
  4080  			},
  4081  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  4082  			clientFn:                successfulClientFetch,
  4083  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4084  		},
  4085  		{
  4086  			Name: "Does not resync resources if api resource deletion due to tombstone fails",
  4087  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4088  				persistTx := &persistenceautomock.PersistenceTx{}
  4089  				persistTx.On("Commit").Return(nil).Times(30)
  4090  
  4091  				transact := &persistenceautomock.Transactioner{}
  4092  				transact.On("Begin").Return(persistTx, nil).Times(31)
  4093  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(30)
  4094  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  4095  				return persistTx, transact
  4096  			},
  4097  			appSvcFn:      successfulAppGet,
  4098  			tenantSvcFn:   successfulTenantSvc,
  4099  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4100  			webhookConvFn: successfulWebhookConversion, bundleSvcFn: successfulBundleCreate,
  4101  			apiSvcFn: func() *automock.APIService {
  4102  				apiSvc := &automock.APIService{}
  4103  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4104  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4105  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return(api1ID, nil).Once()
  4106  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, mock.Anything, "").Return(api2ID, nil).Once()
  4107  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  4108  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(testErr).Once()
  4109  				return apiSvc
  4110  			},
  4111  			eventSvcFn:              successfulEventCreate,
  4112  			specSvcFn:               successfulSpecCreate,
  4113  			packageSvcFn:            successfulPackageCreate,
  4114  			productSvcFn:            successfulProductCreate,
  4115  			vendorSvcFn:             successfulVendorCreate,
  4116  			tombstoneSvcFn:          successfulTombstoneCreate,
  4117  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  4118  			clientFn:                successfulClientFetch,
  4119  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4120  		},
  4121  		{
  4122  			Name: "Does not resync resources if package resource deletion due to tombstone fails",
  4123  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4124  				persistTx := &persistenceautomock.PersistenceTx{}
  4125  				persistTx.On("Commit").Return(nil).Times(30)
  4126  
  4127  				transact := &persistenceautomock.Transactioner{}
  4128  				transact.On("Begin").Return(persistTx, nil).Times(31)
  4129  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(30)
  4130  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  4131  				return persistTx, transact
  4132  			},
  4133  			appSvcFn:      successfulAppGet,
  4134  			tenantSvcFn:   successfulTenantSvc,
  4135  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4136  			webhookConvFn: successfulWebhookConversion, bundleSvcFn: successfulBundleCreate,
  4137  			apiSvcFn:   successfulAPICreate,
  4138  			eventSvcFn: successfulEventCreate,
  4139  			specSvcFn:  successfulSpecCreate,
  4140  			packageSvcFn: func() *automock.PackageService {
  4141  				packagesSvc := &automock.PackageService{}
  4142  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4143  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4144  				packagesSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Packages[0], mock.Anything).Return("", nil).Once()
  4145  				packagesSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixPackages(), nil).Once()
  4146  				packagesSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, packageID).Return(testErr).Once()
  4147  				return packagesSvc
  4148  			},
  4149  			productSvcFn: successfulProductCreate,
  4150  			vendorSvcFn:  successfulVendorCreate,
  4151  			tombstoneSvcFn: func() *automock.TombstoneService {
  4152  				tombstoneSvc := &automock.TombstoneService{}
  4153  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4154  				ts := fixSanitizedORDDocument().Tombstones[0]
  4155  				ts.OrdID = packageORDID
  4156  				tombstoneSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *ts).Return("", nil).Once()
  4157  				tombstones := fixTombstones()
  4158  				tombstones[0].OrdID = packageORDID
  4159  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(tombstones, nil).Once()
  4160  				return tombstoneSvc
  4161  			},
  4162  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  4163  			clientFn: func() *automock.Client {
  4164  				client := &automock.Client{}
  4165  				doc := fixORDDocument()
  4166  				doc.Tombstones[0].OrdID = packageORDID
  4167  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, *doc.DescribedSystemInstance.BaseURL, nil)
  4168  				return client
  4169  			},
  4170  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4171  		},
  4172  		{
  4173  			Name: "Does not resync resources if event resource deletion due to tombstone fails",
  4174  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4175  				persistTx := &persistenceautomock.PersistenceTx{}
  4176  				persistTx.On("Commit").Return(nil).Times(30)
  4177  
  4178  				transact := &persistenceautomock.Transactioner{}
  4179  				transact.On("Begin").Return(persistTx, nil).Times(31)
  4180  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(30)
  4181  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  4182  				return persistTx, transact
  4183  			},
  4184  			appSvcFn:      successfulAppGet,
  4185  			tenantSvcFn:   successfulTenantSvc,
  4186  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4187  			webhookConvFn: successfulWebhookConversion, bundleSvcFn: successfulBundleCreate,
  4188  			apiSvcFn: successfulAPICreate,
  4189  			eventSvcFn: func() *automock.EventService {
  4190  				eventSvc := &automock.EventService{}
  4191  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4192  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4193  				eventSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.EventResources[0], ([]*model.SpecInput)(nil), []string{bundleID}, mock.Anything, "").Return(event1ID, nil).Once()
  4194  				eventSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.EventResources[1], ([]*model.SpecInput)(nil), []string{bundleID}, mock.Anything, "").Return(event2ID, nil).Once()
  4195  				eventSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixEvents(), nil).Once()
  4196  				eventSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, event1ID).Return(testErr).Once()
  4197  				return eventSvc
  4198  			},
  4199  			specSvcFn:    successfulSpecCreate,
  4200  			packageSvcFn: successfulPackageCreate,
  4201  			productSvcFn: successfulProductCreate,
  4202  			vendorSvcFn:  successfulVendorCreate,
  4203  			tombstoneSvcFn: func() *automock.TombstoneService {
  4204  				tombstoneSvc := &automock.TombstoneService{}
  4205  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4206  				ts := fixSanitizedORDDocument().Tombstones[0]
  4207  				ts.OrdID = event1ORDID
  4208  				tombstoneSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *ts).Return("", nil).Once()
  4209  				tombstones := fixTombstones()
  4210  				tombstones[0].OrdID = event1ORDID
  4211  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(tombstones, nil).Once()
  4212  				return tombstoneSvc
  4213  			},
  4214  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  4215  			clientFn: func() *automock.Client {
  4216  				client := &automock.Client{}
  4217  				doc := fixORDDocument()
  4218  				doc.Tombstones[0].OrdID = event1ORDID
  4219  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, *doc.DescribedSystemInstance.BaseURL, nil)
  4220  				return client
  4221  			},
  4222  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4223  		},
  4224  		{
  4225  			Name: "Does not resync resources if vendor resource deletion due to tombstone fails",
  4226  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4227  				persistTx := &persistenceautomock.PersistenceTx{}
  4228  				persistTx.On("Commit").Return(nil).Times(30)
  4229  
  4230  				transact := &persistenceautomock.Transactioner{}
  4231  				transact.On("Begin").Return(persistTx, nil).Times(31)
  4232  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(30)
  4233  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  4234  				return persistTx, transact
  4235  			},
  4236  			appSvcFn:      successfulAppGet,
  4237  			tenantSvcFn:   successfulTenantSvc,
  4238  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4239  			webhookConvFn: successfulWebhookConversion, bundleSvcFn: successfulBundleCreate,
  4240  			apiSvcFn:     successfulAPICreate,
  4241  			eventSvcFn:   successfulEventCreate,
  4242  			specSvcFn:    successfulSpecCreate,
  4243  			packageSvcFn: successfulPackageCreate,
  4244  			productSvcFn: successfulProductCreate,
  4245  			vendorSvcFn: func() *automock.VendorService {
  4246  				vendorSvc := &automock.VendorService{}
  4247  				vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4248  				vendorSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Vendors[0]).Return("", nil).Once()
  4249  				vendorSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Vendors[1]).Return("", nil).Once()
  4250  				vendorSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixVendors(), nil).Once()
  4251  				vendorSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, vendorID).Return(testErr).Once()
  4252  				return vendorSvc
  4253  			},
  4254  			tombstoneSvcFn: func() *automock.TombstoneService {
  4255  				tombstoneSvc := &automock.TombstoneService{}
  4256  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4257  				ts := fixSanitizedORDDocument().Tombstones[0]
  4258  				ts.OrdID = vendorORDID
  4259  				tombstoneSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *ts).Return("", nil).Once()
  4260  				tombstones := fixTombstones()
  4261  				tombstones[0].OrdID = vendorORDID
  4262  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(tombstones, nil).Once()
  4263  				return tombstoneSvc
  4264  			},
  4265  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  4266  			clientFn: func() *automock.Client {
  4267  				client := &automock.Client{}
  4268  				doc := fixORDDocument()
  4269  				doc.Tombstones[0].OrdID = vendorORDID
  4270  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, *doc.DescribedSystemInstance.BaseURL, nil)
  4271  				return client
  4272  			},
  4273  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4274  		},
  4275  		{
  4276  			Name: "Does not resync resources if product resource deletion due to tombstone fails",
  4277  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4278  				persistTx := &persistenceautomock.PersistenceTx{}
  4279  				persistTx.On("Commit").Return(nil).Times(30)
  4280  
  4281  				transact := &persistenceautomock.Transactioner{}
  4282  				transact.On("Begin").Return(persistTx, nil).Times(31)
  4283  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(30)
  4284  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  4285  				return persistTx, transact
  4286  			},
  4287  			appSvcFn:      successfulAppGet,
  4288  			tenantSvcFn:   successfulTenantSvc,
  4289  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4290  			webhookConvFn: successfulWebhookConversion, bundleSvcFn: successfulBundleCreate,
  4291  			apiSvcFn:     successfulAPICreate,
  4292  			eventSvcFn:   successfulEventCreate,
  4293  			specSvcFn:    successfulSpecCreate,
  4294  			packageSvcFn: successfulPackageCreate,
  4295  			productSvcFn: func() *automock.ProductService {
  4296  				productSvc := &automock.ProductService{}
  4297  				productSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4298  				productSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.Products[0]).Return("", nil).Once()
  4299  				productSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixProducts(), nil).Once()
  4300  				productSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, productID).Return(testErr).Once()
  4301  				return productSvc
  4302  			},
  4303  			vendorSvcFn: successfulVendorCreate,
  4304  			tombstoneSvcFn: func() *automock.TombstoneService {
  4305  				tombstoneSvc := &automock.TombstoneService{}
  4306  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4307  				ts := fixSanitizedORDDocument().Tombstones[0]
  4308  				ts.OrdID = productORDID
  4309  				tombstoneSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *ts).Return("", nil).Once()
  4310  				tombstones := fixTombstones()
  4311  				tombstones[0].OrdID = productORDID
  4312  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(tombstones, nil).Once()
  4313  				return tombstoneSvc
  4314  			},
  4315  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  4316  			clientFn: func() *automock.Client {
  4317  				client := &automock.Client{}
  4318  				doc := fixORDDocument()
  4319  				doc.Tombstones[0].OrdID = productORDID
  4320  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, *doc.DescribedSystemInstance.BaseURL, nil)
  4321  				return client
  4322  			},
  4323  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4324  		},
  4325  		{
  4326  			Name: "Does not resync resources if bundle resource deletion due to tombstone fails",
  4327  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4328  				persistTx := &persistenceautomock.PersistenceTx{}
  4329  				persistTx.On("Commit").Return(nil).Times(30)
  4330  
  4331  				transact := &persistenceautomock.Transactioner{}
  4332  				transact.On("Begin").Return(persistTx, nil).Times(31)
  4333  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(false).Times(30)
  4334  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  4335  				return persistTx, transact
  4336  			},
  4337  			appSvcFn:      successfulAppGet,
  4338  			tenantSvcFn:   successfulTenantSvc,
  4339  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4340  			webhookConvFn: successfulWebhookConversion, bundleSvcFn: func() *automock.BundleService {
  4341  				bundlesSvc := &automock.BundleService{}
  4342  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Twice()
  4343  				bundlesSvc.On("CreateBundle", txtest.CtxWithDBMatcher(), resource.Application, appID, *sanitizedDoc.ConsumptionBundles[0], mock.Anything).Return("", nil).Once()
  4344  				bundlesSvc.On("ListByApplicationIDNoPaging", txtest.CtxWithDBMatcher(), appID).Return(fixBundles(), nil).Once()
  4345  				bundlesSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, bundleID).Return(testErr).Once()
  4346  				return bundlesSvc
  4347  			},
  4348  			apiSvcFn:     successfulAPICreate,
  4349  			eventSvcFn:   successfulEventCreate,
  4350  			specSvcFn:    successfulSpecCreate,
  4351  			packageSvcFn: successfulPackageCreate,
  4352  			productSvcFn: successfulProductCreate,
  4353  			vendorSvcFn:  successfulVendorCreate,
  4354  			tombstoneSvcFn: func() *automock.TombstoneService {
  4355  				tombstoneSvc := &automock.TombstoneService{}
  4356  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4357  				ts := fixSanitizedORDDocument().Tombstones[0]
  4358  				ts.OrdID = bundleORDID
  4359  				tombstoneSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, *ts).Return("", nil).Once()
  4360  				tombstones := fixTombstones()
  4361  				tombstones[0].OrdID = bundleORDID
  4362  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(tombstones, nil).Once()
  4363  				return tombstoneSvc
  4364  			},
  4365  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  4366  			clientFn: func() *automock.Client {
  4367  				client := &automock.Client{}
  4368  				doc := fixORDDocument()
  4369  				doc.Tombstones[0].OrdID = bundleORDID
  4370  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, *doc.DescribedSystemInstance.BaseURL, nil)
  4371  				return client
  4372  			},
  4373  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4374  		},
  4375  		{
  4376  			Name: "Returns error when failing to open final transaction to commit fetched specs",
  4377  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4378  				persistTx, transact := txGen.ThatSucceedsMultipleTimes(31)
  4379  				transact.On("Begin").Return(persistTx, testErr).Once()
  4380  				return persistTx, transact
  4381  			},
  4382  			appSvcFn:      successfulAppGet,
  4383  			tenantSvcFn:   successfulTenantSvc,
  4384  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4385  			webhookConvFn: successfulWebhookConversion, bundleSvcFn: successfulBundleCreate,
  4386  			apiSvcFn: func() *automock.APIService {
  4387  				apiSvc := &automock.APIService{}
  4388  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4389  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4390  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return(api1ID, nil).Once()
  4391  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, mock.Anything, "").Return(api2ID, nil).Once()
  4392  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  4393  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  4394  				return apiSvc
  4395  			},
  4396  			eventSvcFn:              successfulEventCreate,
  4397  			specSvcFn:               successfulSpecCreate,
  4398  			fetchReqFn:              successfulFetchRequestFetch,
  4399  			packageSvcFn:            successfulPackageCreate,
  4400  			productSvcFn:            successfulProductCreate,
  4401  			vendorSvcFn:             successfulVendorCreate,
  4402  			tombstoneSvcFn:          successfulTombstoneCreate,
  4403  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  4404  			clientFn:                successfulClientFetch,
  4405  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4406  		},
  4407  		{
  4408  			Name: "Returns error when failing to find spec in final transaction when trying to update and persist fetched specs",
  4409  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4410  				persistTx, transact := txGen.ThatSucceedsMultipleTimes(31)
  4411  				transact.On("Begin").Return(persistTx, nil).Once()
  4412  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  4413  				return persistTx, transact
  4414  			},
  4415  			appSvcFn:      successfulAppGet,
  4416  			tenantSvcFn:   successfulTenantSvc,
  4417  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4418  			webhookConvFn: successfulWebhookConversion, bundleSvcFn: successfulBundleCreate,
  4419  			apiSvcFn: func() *automock.APIService {
  4420  				apiSvc := &automock.APIService{}
  4421  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4422  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4423  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return(api1ID, nil).Once()
  4424  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, mock.Anything, "").Return(api2ID, nil).Once()
  4425  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  4426  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  4427  				return apiSvc
  4428  			},
  4429  			eventSvcFn: successfulEventCreate,
  4430  			specSvcFn: func() *automock.SpecService {
  4431  				specSvc := &automock.SpecService{}
  4432  
  4433  				api1SpecInput1 := fixAPI1SpecInputs()[0]
  4434  				api1SpecInput2 := fixAPI1SpecInputs()[1]
  4435  				api1SpecInput3 := fixAPI1SpecInputs()[2]
  4436  
  4437  				api2SpecInput1 := fixAPI2SpecInputs()[0]
  4438  				api2SpecInput2 := fixAPI2SpecInputs()[1]
  4439  
  4440  				event1Spec := fixEvent1SpecInputs()[0]
  4441  				event2Spec := fixEvent2SpecInputs()[0]
  4442  
  4443  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4444  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4445  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4446  
  4447  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4448  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4449  
  4450  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event1Spec, resource.Application, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event1Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
  4451  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event2Spec, resource.Application, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event2Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
  4452  
  4453  				specSvc.On("GetByID", txtest.CtxWithDBMatcher(), mock.Anything, mock.Anything).Return(nil, testErr).Once()
  4454  
  4455  				return specSvc
  4456  			},
  4457  			fetchReqFn:              successfulFetchRequestFetch,
  4458  			packageSvcFn:            successfulPackageCreate,
  4459  			productSvcFn:            successfulProductCreate,
  4460  			vendorSvcFn:             successfulVendorCreate,
  4461  			tombstoneSvcFn:          successfulTombstoneCreate,
  4462  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  4463  			clientFn:                successfulClientFetch,
  4464  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4465  		},
  4466  		{
  4467  			Name: "Returns error when failing to update spec in final transaction when trying to update and persist fetched specs",
  4468  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4469  				persistTx, transact := txGen.ThatSucceedsMultipleTimes(31)
  4470  				transact.On("Begin").Return(persistTx, nil).Once()
  4471  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  4472  				return persistTx, transact
  4473  			},
  4474  			appSvcFn:      successfulAppGet,
  4475  			tenantSvcFn:   successfulTenantSvc,
  4476  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4477  			webhookConvFn: successfulWebhookConversion, bundleSvcFn: successfulBundleCreate,
  4478  			apiSvcFn: func() *automock.APIService {
  4479  				apiSvc := &automock.APIService{}
  4480  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4481  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4482  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return(api1ID, nil).Once()
  4483  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, mock.Anything, "").Return(api2ID, nil).Once()
  4484  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  4485  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  4486  				return apiSvc
  4487  			},
  4488  			eventSvcFn: successfulEventCreate,
  4489  			specSvcFn: func() *automock.SpecService {
  4490  				specSvc := &automock.SpecService{}
  4491  
  4492  				api1SpecInput1 := fixAPI1SpecInputs()[0]
  4493  				api1SpecInput2 := fixAPI1SpecInputs()[1]
  4494  				api1SpecInput3 := fixAPI1SpecInputs()[2]
  4495  
  4496  				api2SpecInput1 := fixAPI2SpecInputs()[0]
  4497  				api2SpecInput2 := fixAPI2SpecInputs()[1]
  4498  
  4499  				event1Spec := fixEvent1SpecInputs()[0]
  4500  				event2Spec := fixEvent2SpecInputs()[0]
  4501  
  4502  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4503  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4504  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4505  
  4506  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4507  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4508  
  4509  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event1Spec, resource.Application, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event1Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
  4510  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event2Spec, resource.Application, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event2Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
  4511  
  4512  				specSvc.On("GetByID", txtest.CtxWithDBMatcher(), mock.Anything, mock.Anything).Return(&testSpec, nil).Once()
  4513  
  4514  				expectedSpecToUpdate := testSpec
  4515  				expectedSpecToUpdate.Data = &testSpecData
  4516  				specSvc.On("UpdateSpecOnly", txtest.CtxWithDBMatcher(), expectedSpecToUpdate).Return(testErr).Once()
  4517  
  4518  				return specSvc
  4519  			},
  4520  			fetchReqFn:              successfulFetchRequestFetch,
  4521  			packageSvcFn:            successfulPackageCreate,
  4522  			productSvcFn:            successfulProductCreate,
  4523  			vendorSvcFn:             successfulVendorCreate,
  4524  			tombstoneSvcFn:          successfulTombstoneCreate,
  4525  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  4526  			clientFn:                successfulClientFetch,
  4527  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4528  		},
  4529  		{
  4530  			Name: "Returns error when failing to update fetch request in final transaction when trying to update and persist fetched specs",
  4531  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4532  				persistTx, transact := txGen.ThatSucceedsMultipleTimes(31)
  4533  				transact.On("Begin").Return(persistTx, nil).Once()
  4534  				transact.On("RollbackUnlessCommitted", mock.Anything, persistTx).Return(true).Once()
  4535  				return persistTx, transact
  4536  			},
  4537  			appSvcFn:      successfulAppGet,
  4538  			tenantSvcFn:   successfulTenantSvc,
  4539  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4540  			webhookConvFn: successfulWebhookConversion, bundleSvcFn: successfulBundleCreate,
  4541  			apiSvcFn: func() *automock.APIService {
  4542  				apiSvc := &automock.APIService{}
  4543  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4544  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4545  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return(api1ID, nil).Once()
  4546  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, mock.Anything, "").Return(api2ID, nil).Once()
  4547  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  4548  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  4549  				return apiSvc
  4550  			},
  4551  			eventSvcFn: successfulEventCreate,
  4552  			specSvcFn: func() *automock.SpecService {
  4553  				specSvc := &automock.SpecService{}
  4554  
  4555  				api1SpecInput1 := fixAPI1SpecInputs()[0]
  4556  				api1SpecInput2 := fixAPI1SpecInputs()[1]
  4557  				api1SpecInput3 := fixAPI1SpecInputs()[2]
  4558  
  4559  				api2SpecInput1 := fixAPI2SpecInputs()[0]
  4560  				api2SpecInput2 := fixAPI2SpecInputs()[1]
  4561  
  4562  				event1Spec := fixEvent1SpecInputs()[0]
  4563  				event2Spec := fixEvent2SpecInputs()[0]
  4564  
  4565  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput1, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4566  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput2, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4567  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api1SpecInput3, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api1SpecInput3.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4568  
  4569  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput1, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput1.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4570  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *api2SpecInput2, resource.Application, model.APISpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(api2SpecInput2.FetchRequest, model.APISpecFetchRequestReference, ""), nil).Once()
  4571  
  4572  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event1Spec, resource.Application, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event1Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
  4573  				specSvc.On("CreateByReferenceObjectIDWithDelayedFetchRequest", txtest.CtxWithDBMatcher(), *event2Spec, resource.Application, model.EventSpecReference, mock.Anything).Return("", fixFetchRequestFromFetchRequestInput(event2Spec.FetchRequest, model.EventSpecFetchRequestReference, ""), nil).Once()
  4574  
  4575  				specSvc.On("GetByID", txtest.CtxWithDBMatcher(), mock.Anything, mock.Anything).Return(&testSpec, nil).Once()
  4576  
  4577  				expectedSpecToUpdate := testSpec
  4578  				expectedSpecToUpdate.Data = &testSpecData
  4579  				specSvc.On("UpdateSpecOnly", txtest.CtxWithDBMatcher(), expectedSpecToUpdate).Return(nil).Once()
  4580  
  4581  				return specSvc
  4582  			},
  4583  			fetchReqFn: func() *automock.FetchRequestService {
  4584  				fetchReqSvc := &automock.FetchRequestService{}
  4585  				fetchReqSvc.On("FetchSpec", txtest.CtxWithDBMatcher(), mock.Anything).Return(&testSpecData, &model.FetchRequestStatus{Condition: model.FetchRequestStatusConditionSucceeded}).
  4586  					Times(len(fixAPI1SpecInputs()) + len(fixAPI2SpecInputs()) + len(fixEvent1SpecInputs()) + len(fixEvent2SpecInputs()))
  4587  
  4588  				fetchReqSvc.On("Update", txtest.CtxWithDBMatcher(), mock.MatchedBy(func(actual *model.FetchRequest) bool {
  4589  					return actual.Status.Condition == model.FetchRequestStatusConditionSucceeded
  4590  				})).Return(testErr).Once()
  4591  
  4592  				return fetchReqSvc
  4593  			},
  4594  			packageSvcFn:            successfulPackageCreate,
  4595  			productSvcFn:            successfulProductCreate,
  4596  			vendorSvcFn:             successfulVendorCreate,
  4597  			tombstoneSvcFn:          successfulTombstoneCreate,
  4598  			globalRegistrySvcFn:     successfulGlobalRegistrySvc,
  4599  			clientFn:                successfulClientFetch,
  4600  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4601  		},
  4602  		{
  4603  			Name: "Success when resources are not in db and no SAP Vendor is declared in Documents should Create them as SAP Vendor is coming from the Global Registry",
  4604  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4605  				return txGen.ThatSucceedsMultipleTimes(30)
  4606  			},
  4607  			appSvcFn:      successfulAppGet,
  4608  			tenantSvcFn:   successfulTenantSvc,
  4609  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4610  			webhookConvFn: successfulWebhookConversion, bundleSvcFn: successfulBundleCreate,
  4611  			apiSvcFn: func() *automock.APIService {
  4612  				apiSvc := &automock.APIService{}
  4613  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4614  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(nil, nil).Once()
  4615  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[0], ([]*model.SpecInput)(nil), map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, mock.Anything, "").Return(api1ID, nil).Once()
  4616  				apiSvc.On("Create", txtest.CtxWithDBMatcher(), resource.Application, appID, nilBundleID, str.Ptr(packageID), *sanitizedDoc.APIResources[1], ([]*model.SpecInput)(nil), map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, mock.Anything, "").Return(api2ID, nil).Once()
  4617  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  4618  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  4619  				return apiSvc
  4620  			},
  4621  			eventSvcFn:          successfulEventCreate,
  4622  			specSvcFn:           successfulSpecCreateAndUpdate,
  4623  			fetchReqFn:          successfulFetchRequestFetchAndUpdate,
  4624  			packageSvcFn:        successfulPackageCreate,
  4625  			productSvcFn:        successfulProductCreate,
  4626  			vendorSvcFn:         successfulEmptyVendorList,
  4627  			tombstoneSvcFn:      successfulTombstoneCreate,
  4628  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  4629  			clientFn: func() *automock.Client {
  4630  				client := &automock.Client{}
  4631  				doc := fixORDDocument()
  4632  				doc.Vendors = nil
  4633  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, *doc.DescribedSystemInstance.BaseURL, nil)
  4634  				return client
  4635  			},
  4636  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4637  		},
  4638  		{
  4639  			Name: "Success when resources are already in db and no SAP Vendor is declared in Documents should Update them as SAP Vendor is coming from the Global Registry",
  4640  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4641  				return txGen.ThatSucceedsMultipleTimes(30)
  4642  			},
  4643  			appSvcFn:      successfulAppGet,
  4644  			tenantSvcFn:   successfulTenantSvc,
  4645  			webhookSvcFn:  successfulTenantMappingOnlyCreation,
  4646  			webhookConvFn: successfulWebhookConversion, bundleSvcFn: successfulBundleUpdateForApplication,
  4647  			bundleRefSvcFn: successfulBundleReferenceFetchingOfBundleIDs,
  4648  			apiSvcFn: func() *automock.APIService {
  4649  				apiSvc := &automock.APIService{}
  4650  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  4651  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  4652  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api1ID, *sanitizedDoc.APIResources[0], nilSpecInput, map[string]string{bundleID: sanitizedDoc.APIResources[0].PartOfConsumptionBundles[0].DefaultTargetURL}, map[string]string{}, []string{}, api1PreSanitizedHash, "").Return(nil).Once()
  4653  				apiSvc.On("UpdateInManyBundles", txtest.CtxWithDBMatcher(), resource.Application, api2ID, *sanitizedDoc.APIResources[1], nilSpecInput, map[string]string{bundleID: "http://localhost:8080/some-api/v1"}, map[string]string{}, []string{}, api2PreSanitizedHash, "").Return(nil).Once()
  4654  				apiSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixAPIs(), nil).Once()
  4655  				apiSvc.On("Delete", txtest.CtxWithDBMatcher(), resource.Application, api2ID).Return(nil).Once()
  4656  				return apiSvc
  4657  			},
  4658  			eventSvcFn:   successfulEventUpdate,
  4659  			specSvcFn:    successfulSpecRecreateAndUpdate,
  4660  			fetchReqFn:   successfulFetchRequestFetchAndUpdate,
  4661  			packageSvcFn: successfulPackageUpdateForApplication,
  4662  			productSvcFn: successfulProductUpdateForApplication,
  4663  			vendorSvcFn:  successfulEmptyVendorList,
  4664  			tombstoneSvcFn: func() *automock.TombstoneService {
  4665  				tombstoneSvc := &automock.TombstoneService{}
  4666  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  4667  				tombstoneSvc.On("Update", txtest.CtxWithDBMatcher(), resource.Application, tombstoneID, *sanitizedDoc.Tombstones[0]).Return(nil).Once()
  4668  				tombstoneSvc.On("ListByApplicationID", txtest.CtxWithDBMatcher(), appID).Return(fixTombstones(), nil).Once()
  4669  				return tombstoneSvc
  4670  			},
  4671  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  4672  			clientFn: func() *automock.Client {
  4673  				client := &automock.Client{}
  4674  				doc := fixORDDocument()
  4675  				doc.Vendors = nil
  4676  				client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{doc}, *doc.DescribedSystemInstance.BaseURL, nil)
  4677  				return client
  4678  			},
  4679  			appTemplateVersionSvcFn: successfulAppTemplateVersionList,
  4680  		},
  4681  	}
  4682  
  4683  	for _, test := range testCases {
  4684  		t.Run(test.Name, func(t *testing.T) {
  4685  			_, tx := test.TransactionerFn()
  4686  			appSvc := &automock.ApplicationService{}
  4687  			if test.appSvcFn != nil {
  4688  				appSvc = test.appSvcFn()
  4689  			}
  4690  			whSvc := &automock.WebhookService{}
  4691  			if test.webhookSvcFn != nil {
  4692  				whSvc = test.webhookSvcFn()
  4693  			}
  4694  			bndlSvc := &automock.BundleService{}
  4695  			if test.bundleSvcFn != nil {
  4696  				bndlSvc = test.bundleSvcFn()
  4697  			}
  4698  			bndlRefSvc := &automock.BundleReferenceService{}
  4699  			if test.bundleRefSvcFn != nil {
  4700  				bndlRefSvc = test.bundleRefSvcFn()
  4701  			}
  4702  			apiSvc := &automock.APIService{}
  4703  			if test.apiSvcFn != nil {
  4704  				apiSvc = test.apiSvcFn()
  4705  			}
  4706  			eventSvc := &automock.EventService{}
  4707  			if test.eventSvcFn != nil {
  4708  				eventSvc = test.eventSvcFn()
  4709  			}
  4710  			specSvc := &automock.SpecService{}
  4711  			if test.specSvcFn != nil {
  4712  				specSvc = test.specSvcFn()
  4713  			}
  4714  			fetchReqSvc := &automock.FetchRequestService{}
  4715  			if test.fetchReqFn != nil {
  4716  				fetchReqSvc = test.fetchReqFn()
  4717  			}
  4718  			packageSvc := &automock.PackageService{}
  4719  			if test.packageSvcFn != nil {
  4720  				packageSvc = test.packageSvcFn()
  4721  			}
  4722  			productSvc := &automock.ProductService{}
  4723  			if test.productSvcFn != nil {
  4724  				productSvc = test.productSvcFn()
  4725  			}
  4726  			vendorSvc := &automock.VendorService{}
  4727  			if test.vendorSvcFn != nil {
  4728  				vendorSvc = test.vendorSvcFn()
  4729  			}
  4730  			tombstoneSvc := &automock.TombstoneService{}
  4731  			if test.tombstoneSvcFn != nil {
  4732  				tombstoneSvc = test.tombstoneSvcFn()
  4733  			}
  4734  			tenantSvc := &automock.TenantService{}
  4735  			if test.tenantSvcFn != nil {
  4736  				tenantSvc = test.tenantSvcFn()
  4737  			}
  4738  			globalRegistrySvcFn := &automock.GlobalRegistryService{}
  4739  			if test.globalRegistrySvcFn != nil {
  4740  				globalRegistrySvcFn = test.globalRegistrySvcFn()
  4741  			}
  4742  			client := &automock.Client{}
  4743  			if test.clientFn != nil {
  4744  				client = test.clientFn()
  4745  			}
  4746  			whConverter := &automock.WebhookConverter{}
  4747  			if test.webhookConvFn != nil {
  4748  				whConverter = test.webhookConvFn()
  4749  			}
  4750  			appTemplateVersionSvc := &automock.ApplicationTemplateVersionService{}
  4751  			if test.appTemplateVersionSvcFn != nil {
  4752  				appTemplateVersionSvc = test.appTemplateVersionSvcFn()
  4753  			}
  4754  			appTemplateSvc := &automock.ApplicationTemplateService{}
  4755  			if test.appTemplateSvcFn != nil {
  4756  				appTemplateSvc = test.appTemplateSvcFn()
  4757  			}
  4758  
  4759  			ordCfg := ord.NewServiceConfig(4, 100, 0, "", false, credentialExchangeStrategyTenantMappings)
  4760  			svc := ord.NewAggregatorService(ordCfg, tx, appSvc, whSvc, bndlSvc, bndlRefSvc, apiSvc, eventSvc, specSvc, fetchReqSvc, packageSvc, productSvc, vendorSvc, tombstoneSvc, tenantSvc, globalRegistrySvcFn, client, whConverter, appTemplateVersionSvc, appTemplateSvc)
  4761  			err := svc.SyncORDDocuments(context.TODO(), ord.MetricsConfig{})
  4762  			if test.ExpectedErr != nil {
  4763  				require.Error(t, err)
  4764  				require.Contains(t, err.Error(), test.ExpectedErr.Error())
  4765  			} else {
  4766  				require.NoError(t, err)
  4767  			}
  4768  
  4769  			mock.AssertExpectationsForObjects(t, tx, appSvc, whSvc, bndlSvc, apiSvc, eventSvc, specSvc, packageSvc, productSvc, vendorSvc, tombstoneSvc, tenantSvc, globalRegistrySvcFn, client)
  4770  		})
  4771  	}
  4772  }
  4773  
  4774  func TestService_ProcessApplications(t *testing.T) {
  4775  	testErr := errors.New("Test error")
  4776  	txGen := txtest.NewTransactionContextGenerator(testErr)
  4777  
  4778  	testApplication := fixApplications()[0]
  4779  	testResource := ord.Resource{
  4780  		Type:          resource.Application,
  4781  		ID:            testApplication.ID,
  4782  		Name:          testApplication.Name,
  4783  		LocalTenantID: testApplication.LocalTenantID,
  4784  		ParentID:      &appTemplateID,
  4785  	}
  4786  	testWebhookForApplication := fixWebhooksForApplication()[0]
  4787  
  4788  	successfulClientFetch := func() *automock.Client {
  4789  		client := &automock.Client{}
  4790  		client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResource, testWebhookForApplication).Return(ord.Documents{}, baseURL, nil)
  4791  		return client
  4792  	}
  4793  
  4794  	testCases := []struct {
  4795  		Name                string
  4796  		TransactionerFn     func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
  4797  		appSvcFn            func() *automock.ApplicationService
  4798  		webhookSvcFn        func() *automock.WebhookService
  4799  		webhookConvFn       func() *automock.WebhookConverter
  4800  		tenantSvcFn         func() *automock.TenantService
  4801  		globalRegistrySvcFn func() *automock.GlobalRegistryService
  4802  		clientFn            func() *automock.Client
  4803  		appIDs              func() []string
  4804  		ExpectedErr         error
  4805  	}{
  4806  		{
  4807  			Name: "Success when empty app IDs array",
  4808  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4809  				return txGen.ThatDoesntStartTransaction()
  4810  			},
  4811  			appSvcFn: func() *automock.ApplicationService {
  4812  				return &automock.ApplicationService{}
  4813  			},
  4814  			tenantSvcFn: func() *automock.TenantService {
  4815  				return &automock.TenantService{}
  4816  			},
  4817  			webhookSvcFn: func() *automock.WebhookService {
  4818  				return &automock.WebhookService{}
  4819  			},
  4820  			globalRegistrySvcFn: func() *automock.GlobalRegistryService {
  4821  				return &automock.GlobalRegistryService{}
  4822  			},
  4823  			clientFn: func() *automock.Client {
  4824  				return &automock.Client{}
  4825  			},
  4826  			appIDs: func() []string {
  4827  				return []string{}
  4828  			},
  4829  		},
  4830  		{
  4831  			Name: "Success",
  4832  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4833  				return txGen.ThatSucceedsMultipleTimes(2)
  4834  			},
  4835  			appSvcFn:    successfulAppGet,
  4836  			tenantSvcFn: successfulTenantSvc,
  4837  			webhookSvcFn: func() *automock.WebhookService {
  4838  				whSvc := &automock.WebhookService{}
  4839  				whSvc.On("ListForApplication", txtest.CtxWithDBMatcher(), appID).Return(fixWebhooksForApplication(), nil).Once()
  4840  				return whSvc
  4841  			},
  4842  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  4843  			clientFn:            successfulClientFetch,
  4844  			appIDs: func() []string {
  4845  				return []string{appID}
  4846  			},
  4847  		},
  4848  		{
  4849  			Name: "Error while listing webhooks for application",
  4850  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4851  				return txGen.ThatSucceedsMultipleTimes(1)
  4852  			},
  4853  			appSvcFn: func() *automock.ApplicationService {
  4854  				return &automock.ApplicationService{}
  4855  			},
  4856  			tenantSvcFn: func() *automock.TenantService {
  4857  				return &automock.TenantService{}
  4858  			},
  4859  			webhookSvcFn: func() *automock.WebhookService {
  4860  				whSvc := &automock.WebhookService{}
  4861  				whSvc.On("ListForApplication", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  4862  				return whSvc
  4863  			},
  4864  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  4865  			clientFn: func() *automock.Client {
  4866  				return &automock.Client{}
  4867  			},
  4868  			appIDs: func() []string {
  4869  				return []string{appID}
  4870  			},
  4871  			ExpectedErr: testErr,
  4872  		},
  4873  		{
  4874  			Name: "Error while retrieving application",
  4875  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4876  				return txGen.ThatSucceedsMultipleTimes(2)
  4877  			},
  4878  			appSvcFn: func() *automock.ApplicationService {
  4879  				appSvc := &automock.ApplicationService{}
  4880  				appSvc.On("Get", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  4881  				return appSvc
  4882  			},
  4883  			tenantSvcFn: successfulTenantSvc,
  4884  			webhookSvcFn: func() *automock.WebhookService {
  4885  				whSvc := &automock.WebhookService{}
  4886  				whSvc.On("ListForApplication", txtest.CtxWithDBMatcher(), appID).Return(fixWebhooksForApplication(), nil).Once()
  4887  				return whSvc
  4888  			},
  4889  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  4890  			clientFn: func() *automock.Client {
  4891  				return &automock.Client{}
  4892  			},
  4893  			appIDs: func() []string {
  4894  				return []string{appID}
  4895  			},
  4896  			ExpectedErr: testErr,
  4897  		},
  4898  		{
  4899  			Name: "Error while getting lowest owner of resource",
  4900  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  4901  				return txGen.ThatSucceedsMultipleTimes(2)
  4902  			},
  4903  			appSvcFn: func() *automock.ApplicationService {
  4904  				return &automock.ApplicationService{}
  4905  			},
  4906  			tenantSvcFn: func() *automock.TenantService {
  4907  				tenantSvc := &automock.TenantService{}
  4908  				tenantSvc.On("GetLowestOwnerForResource", txtest.CtxWithDBMatcher(), resource.Application, appID).Return("", testErr).Once()
  4909  				return tenantSvc
  4910  			},
  4911  			webhookSvcFn: func() *automock.WebhookService {
  4912  				whSvc := &automock.WebhookService{}
  4913  				whSvc.On("ListForApplication", txtest.CtxWithDBMatcher(), appID).Return(fixWebhooksForApplication(), nil).Once()
  4914  				return whSvc
  4915  			},
  4916  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  4917  			clientFn: func() *automock.Client {
  4918  				return &automock.Client{}
  4919  			},
  4920  			appIDs: func() []string {
  4921  				return []string{appID}
  4922  			},
  4923  			ExpectedErr: testErr,
  4924  		},
  4925  	}
  4926  	for _, test := range testCases {
  4927  		t.Run(test.Name, func(t *testing.T) {
  4928  			_, tx := test.TransactionerFn()
  4929  			appSvc := &automock.ApplicationService{}
  4930  			if test.appSvcFn != nil {
  4931  				appSvc = test.appSvcFn()
  4932  			}
  4933  			whSvc := &automock.WebhookService{}
  4934  			if test.webhookSvcFn != nil {
  4935  				whSvc = test.webhookSvcFn()
  4936  			}
  4937  
  4938  			whConverter := &automock.WebhookConverter{}
  4939  			if test.webhookConvFn != nil {
  4940  				whConverter = test.webhookConvFn()
  4941  			}
  4942  			bndlSvc := &automock.BundleService{}
  4943  			bndlRefSvc := &automock.BundleReferenceService{}
  4944  			apiSvc := &automock.APIService{}
  4945  			eventSvc := &automock.EventService{}
  4946  			specSvc := &automock.SpecService{}
  4947  			fetchReqSvc := &automock.FetchRequestService{}
  4948  			packageSvc := &automock.PackageService{}
  4949  			productSvc := &automock.ProductService{}
  4950  			vendorSvc := &automock.VendorService{}
  4951  			tombstoneSvc := &automock.TombstoneService{}
  4952  			tenantSvc := &automock.TenantService{}
  4953  			appTemplateVersionSvc := &automock.ApplicationTemplateVersionService{}
  4954  			appTemplateSvc := &automock.ApplicationTemplateService{}
  4955  			if test.tenantSvcFn != nil {
  4956  				tenantSvc = test.tenantSvcFn()
  4957  			}
  4958  			globalRegistrySvcFn := &automock.GlobalRegistryService{}
  4959  			if test.globalRegistrySvcFn != nil {
  4960  				globalRegistrySvcFn = test.globalRegistrySvcFn()
  4961  			}
  4962  			client := &automock.Client{}
  4963  			if test.clientFn != nil {
  4964  				client = test.clientFn()
  4965  			}
  4966  
  4967  			ordCfg := ord.NewServiceConfig(4, 100, 0, "", false, credentialExchangeStrategyTenantMappings)
  4968  			svc := ord.NewAggregatorService(ordCfg, tx, appSvc, whSvc, bndlSvc, bndlRefSvc, apiSvc, eventSvc, specSvc, fetchReqSvc, packageSvc, productSvc, vendorSvc, tombstoneSvc, tenantSvc, globalRegistrySvcFn, client, whConverter, appTemplateVersionSvc, appTemplateSvc)
  4969  			err := svc.ProcessApplications(context.TODO(), ord.MetricsConfig{}, test.appIDs())
  4970  			if test.ExpectedErr != nil {
  4971  				require.Error(t, err)
  4972  				require.Contains(t, err.Error(), test.ExpectedErr.Error())
  4973  			} else {
  4974  				require.NoError(t, err)
  4975  			}
  4976  
  4977  			mock.AssertExpectationsForObjects(t, tx, appSvc, whSvc, bndlSvc, apiSvc, eventSvc, specSvc, packageSvc, productSvc, vendorSvc, tombstoneSvc, tenantSvc, globalRegistrySvcFn, client)
  4978  		})
  4979  	}
  4980  }
  4981  
  4982  func TestService_ProcessApplicationTemplates(t *testing.T) {
  4983  	testErr := errors.New("Test error")
  4984  	txGen := txtest.NewTransactionContextGenerator(testErr)
  4985  
  4986  	testApplication := fixApplications()[0]
  4987  	testResourceApp := ord.Resource{
  4988  		Type:          resource.Application,
  4989  		ID:            testApplication.ID,
  4990  		Name:          testApplication.Name,
  4991  		LocalTenantID: testApplication.LocalTenantID,
  4992  		ParentID:      &appTemplateID,
  4993  	}
  4994  	testResourceAppTemplate := ord.Resource{
  4995  		Type: resource.ApplicationTemplate,
  4996  		ID:   appTemplateID,
  4997  		Name: appTemplateName,
  4998  	}
  4999  	testWebhookForAppTemplate := fixOrdWebhooksForAppTemplate()[0]
  5000  
  5001  	successfulClientFetchForAppTemplate := func() *automock.Client {
  5002  		client := &automock.Client{}
  5003  		client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResourceAppTemplate, testWebhookForAppTemplate).Return(ord.Documents{}, baseURL, nil).Once()
  5004  		client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResourceApp, testWebhookForAppTemplate).Return(ord.Documents{}, baseURL, nil).Once()
  5005  		return client
  5006  	}
  5007  
  5008  	successfulClientFetchForOnlyAppTemplate := func() *automock.Client {
  5009  		client := &automock.Client{}
  5010  		client.On("FetchOpenResourceDiscoveryDocuments", txtest.CtxWithDBMatcher(), testResourceAppTemplate, testWebhookForAppTemplate).Return(ord.Documents{}, baseURL, nil).Once()
  5011  		return client
  5012  	}
  5013  
  5014  	testCases := []struct {
  5015  		Name                    string
  5016  		TransactionerFn         func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner)
  5017  		appSvcFn                func() *automock.ApplicationService
  5018  		webhookSvcFn            func() *automock.WebhookService
  5019  		webhookConvFn           func() *automock.WebhookConverter
  5020  		tenantSvcFn             func() *automock.TenantService
  5021  		globalRegistrySvcFn     func() *automock.GlobalRegistryService
  5022  		appTemplateSvcFn        func() *automock.ApplicationTemplateService
  5023  		appTemplateVersionSvcFn func() *automock.ApplicationTemplateVersionService
  5024  		clientFn                func() *automock.Client
  5025  		appTemplateIDs          func() []string
  5026  		ExpectedErr             error
  5027  	}{
  5028  		{
  5029  			Name: "Success when empty application template IDs array",
  5030  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  5031  				return txGen.ThatDoesntStartTransaction()
  5032  			},
  5033  			appSvcFn: func() *automock.ApplicationService {
  5034  				return &automock.ApplicationService{}
  5035  			},
  5036  			tenantSvcFn: func() *automock.TenantService {
  5037  				return &automock.TenantService{}
  5038  			},
  5039  			webhookSvcFn: func() *automock.WebhookService {
  5040  				return &automock.WebhookService{}
  5041  			},
  5042  			globalRegistrySvcFn: func() *automock.GlobalRegistryService {
  5043  				return &automock.GlobalRegistryService{}
  5044  			},
  5045  			clientFn: func() *automock.Client {
  5046  				return &automock.Client{}
  5047  			},
  5048  			appTemplateIDs: func() []string {
  5049  				return []string{}
  5050  			},
  5051  		},
  5052  		{
  5053  			Name: "Success",
  5054  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  5055  				return txGen.ThatSucceedsMultipleTimes(4)
  5056  			},
  5057  			appSvcFn:    successfulAppTemplateAppSvc,
  5058  			tenantSvcFn: successfulTenantSvc,
  5059  			webhookSvcFn: func() *automock.WebhookService {
  5060  				whSvc := &automock.WebhookService{}
  5061  				whSvc.On("ListForApplicationTemplate", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixOrdWebhooksForAppTemplate(), nil).Once()
  5062  				return whSvc
  5063  			},
  5064  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  5065  			clientFn:            successfulClientFetchForAppTemplate,
  5066  			appTemplateIDs: func() []string {
  5067  				return []string{appTemplateID}
  5068  			},
  5069  			appTemplateSvcFn: successAppTemplateGetSvc,
  5070  		},
  5071  		{
  5072  			Name: "Error while listing webhooks for application templates",
  5073  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  5074  				return txGen.ThatSucceedsMultipleTimes(1)
  5075  			},
  5076  			appSvcFn: func() *automock.ApplicationService {
  5077  				return &automock.ApplicationService{}
  5078  			},
  5079  			tenantSvcFn: func() *automock.TenantService {
  5080  				return &automock.TenantService{}
  5081  			},
  5082  			webhookSvcFn: func() *automock.WebhookService {
  5083  				whSvc := &automock.WebhookService{}
  5084  				whSvc.On("ListForApplicationTemplate", txtest.CtxWithDBMatcher(), appTemplateID).Return(nil, testErr).Once()
  5085  				return whSvc
  5086  			},
  5087  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  5088  			clientFn: func() *automock.Client {
  5089  				return &automock.Client{}
  5090  			},
  5091  			appTemplateIDs: func() []string {
  5092  				return []string{appTemplateID}
  5093  			},
  5094  			ExpectedErr: testErr,
  5095  		},
  5096  		{
  5097  			Name: "Error while listing applications by application template id",
  5098  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  5099  				return txGen.ThatSucceedsMultipleTimes(3)
  5100  			},
  5101  			appSvcFn: func() *automock.ApplicationService {
  5102  				appSvc := &automock.ApplicationService{}
  5103  				appSvc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(nil, testErr).Once()
  5104  				return appSvc
  5105  			},
  5106  			tenantSvcFn: func() *automock.TenantService {
  5107  				return &automock.TenantService{}
  5108  			},
  5109  			webhookSvcFn: func() *automock.WebhookService {
  5110  				whSvc := &automock.WebhookService{}
  5111  				whSvc.On("ListForApplicationTemplate", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixOrdWebhooksForAppTemplate(), nil).Once()
  5112  				return whSvc
  5113  			},
  5114  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  5115  			clientFn:            successfulClientFetchForOnlyAppTemplate,
  5116  			appTemplateIDs: func() []string {
  5117  				return []string{appTemplateID}
  5118  			},
  5119  			appTemplateSvcFn: successAppTemplateGetSvc,
  5120  			ExpectedErr:      testErr,
  5121  		},
  5122  		{
  5123  			Name: "Error while getting application",
  5124  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  5125  				return txGen.ThatSucceedsMultipleTimes(4)
  5126  			},
  5127  			appSvcFn: func() *automock.ApplicationService {
  5128  				appSvc := &automock.ApplicationService{}
  5129  				appSvc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixApplications(), nil).Once()
  5130  				appSvc.On("Get", txtest.CtxWithDBMatcher(), appID).Return(nil, testErr).Once()
  5131  				return appSvc
  5132  			},
  5133  			tenantSvcFn: successfulTenantSvc,
  5134  			webhookSvcFn: func() *automock.WebhookService {
  5135  				whSvc := &automock.WebhookService{}
  5136  				whSvc.On("ListForApplicationTemplate", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixOrdWebhooksForAppTemplate(), nil).Once()
  5137  				return whSvc
  5138  			},
  5139  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  5140  			clientFn:            successfulClientFetchForOnlyAppTemplate,
  5141  			appTemplateIDs: func() []string {
  5142  				return []string{appTemplateID}
  5143  			},
  5144  			appTemplateSvcFn: successAppTemplateGetSvc,
  5145  			ExpectedErr:      testErr,
  5146  		},
  5147  		{
  5148  			Name: "Error while getting lowest owner of resource",
  5149  			TransactionerFn: func() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
  5150  				return txGen.ThatSucceedsMultipleTimes(4)
  5151  			},
  5152  			appSvcFn: func() *automock.ApplicationService {
  5153  				appSvc := &automock.ApplicationService{}
  5154  				appSvc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixApplications(), nil).Once()
  5155  				return appSvc
  5156  			},
  5157  			tenantSvcFn: func() *automock.TenantService {
  5158  				tenantSvc := &automock.TenantService{}
  5159  				tenantSvc.On("GetLowestOwnerForResource", txtest.CtxWithDBMatcher(), resource.Application, appID).Return("", testErr).Once()
  5160  				return tenantSvc
  5161  			},
  5162  			webhookSvcFn: func() *automock.WebhookService {
  5163  				whSvc := &automock.WebhookService{}
  5164  				whSvc.On("ListForApplicationTemplate", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixOrdWebhooksForAppTemplate(), nil).Once()
  5165  				return whSvc
  5166  			},
  5167  			globalRegistrySvcFn: successfulGlobalRegistrySvc,
  5168  			clientFn:            successfulClientFetchForOnlyAppTemplate,
  5169  			appTemplateIDs: func() []string {
  5170  				return []string{appTemplateID}
  5171  			},
  5172  			appTemplateSvcFn: successAppTemplateGetSvc,
  5173  			ExpectedErr:      testErr,
  5174  		},
  5175  	}
  5176  	for _, test := range testCases {
  5177  		t.Run(test.Name, func(t *testing.T) {
  5178  			_, tx := test.TransactionerFn()
  5179  			appSvc := &automock.ApplicationService{}
  5180  			if test.appSvcFn != nil {
  5181  				appSvc = test.appSvcFn()
  5182  			}
  5183  			whSvc := &automock.WebhookService{}
  5184  			if test.webhookSvcFn != nil {
  5185  				whSvc = test.webhookSvcFn()
  5186  			}
  5187  			whConv := &automock.WebhookConverter{}
  5188  			if test.webhookConvFn != nil {
  5189  				whConv = test.webhookConvFn()
  5190  			}
  5191  
  5192  			bndlSvc := &automock.BundleService{}
  5193  			bndlRefSvc := &automock.BundleReferenceService{}
  5194  			apiSvc := &automock.APIService{}
  5195  			eventSvc := &automock.EventService{}
  5196  			specSvc := &automock.SpecService{}
  5197  			fetchReqSvc := &automock.FetchRequestService{}
  5198  			packageSvc := &automock.PackageService{}
  5199  			productSvc := &automock.ProductService{}
  5200  			vendorSvc := &automock.VendorService{}
  5201  			tombstoneSvc := &automock.TombstoneService{}
  5202  			tenantSvc := &automock.TenantService{}
  5203  			if test.tenantSvcFn != nil {
  5204  				tenantSvc = test.tenantSvcFn()
  5205  			}
  5206  			globalRegistrySvcFn := &automock.GlobalRegistryService{}
  5207  			if test.globalRegistrySvcFn != nil {
  5208  				globalRegistrySvcFn = test.globalRegistrySvcFn()
  5209  			}
  5210  			client := &automock.Client{}
  5211  			if test.clientFn != nil {
  5212  				client = test.clientFn()
  5213  			}
  5214  			appTemplateSvc := &automock.ApplicationTemplateService{}
  5215  			if test.appTemplateSvcFn != nil {
  5216  				appTemplateSvc = test.appTemplateSvcFn()
  5217  			}
  5218  			appTemplateVersionSvc := &automock.ApplicationTemplateVersionService{}
  5219  			if test.appTemplateVersionSvcFn != nil {
  5220  				appTemplateVersionSvc = test.appTemplateVersionSvcFn()
  5221  			}
  5222  
  5223  			ordCfg := ord.NewServiceConfig(4, 100, 0, "", false, credentialExchangeStrategyTenantMappings)
  5224  			svc := ord.NewAggregatorService(ordCfg, tx, appSvc, whSvc, bndlSvc, bndlRefSvc, apiSvc, eventSvc, specSvc, fetchReqSvc, packageSvc, productSvc, vendorSvc, tombstoneSvc, tenantSvc, globalRegistrySvcFn, client, whConv, appTemplateVersionSvc, appTemplateSvc)
  5225  			err := svc.ProcessApplicationTemplates(context.TODO(), ord.MetricsConfig{}, test.appTemplateIDs())
  5226  			if test.ExpectedErr != nil {
  5227  				require.Error(t, err)
  5228  				require.Contains(t, err.Error(), test.ExpectedErr.Error())
  5229  			} else {
  5230  				require.NoError(t, err)
  5231  			}
  5232  
  5233  			mock.AssertExpectationsForObjects(t, tx, appSvc, whSvc, bndlSvc, apiSvc, eventSvc, specSvc, packageSvc, productSvc, vendorSvc, tombstoneSvc, tenantSvc, globalRegistrySvcFn, client)
  5234  		})
  5235  	}
  5236  }
  5237  
  5238  func successfulGlobalRegistrySvc() *automock.GlobalRegistryService {
  5239  	globalRegistrySvcFn := &automock.GlobalRegistryService{}
  5240  	globalRegistrySvcFn.On("SyncGlobalResources", context.TODO()).Return(map[string]bool{ord.SapVendor: true}, nil).Once()
  5241  	return globalRegistrySvcFn
  5242  }
  5243  
  5244  func successfulTenantSvc() *automock.TenantService {
  5245  	tenantSvc := &automock.TenantService{}
  5246  	tenantSvc.On("GetLowestOwnerForResource", txtest.CtxWithDBMatcher(), resource.Application, appID).Return(tenantID, nil).Once()
  5247  	tenantSvc.On("GetTenantByID", txtest.CtxWithDBMatcher(), tenantID).Return(&model.BusinessTenantMapping{ExternalTenant: externalTenantID}, nil).Once()
  5248  	return tenantSvc
  5249  }
  5250  
  5251  func successfulAppGet() *automock.ApplicationService {
  5252  	appSvc := &automock.ApplicationService{}
  5253  	appSvc.On("Get", txtest.CtxWithDBMatcher(), appID).Return(fixApplications()[0], nil).Once()
  5254  	return appSvc
  5255  }
  5256  
  5257  func successfulAppTemplateAppSvc() *automock.ApplicationService {
  5258  	appSvc := &automock.ApplicationService{}
  5259  	appSvc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixApplications(), nil).Once()
  5260  	appSvc.On("Get", txtest.CtxWithDBMatcher(), appID).Return(fixApplications()[0], nil).Once()
  5261  
  5262  	return appSvc
  5263  }
  5264  
  5265  func successfulAppTemplateNoAppsAppSvc() *automock.ApplicationService {
  5266  	appSvc := &automock.ApplicationService{}
  5267  	appSvc.On("ListAllByApplicationTemplateID", txtest.CtxWithDBMatcher(), appTemplateID).Return(nil, nil).Once()
  5268  
  5269  	return appSvc
  5270  }
  5271  
  5272  func successAppTemplateGetSvc() *automock.ApplicationTemplateService {
  5273  	svc := &automock.ApplicationTemplateService{}
  5274  	svc.On("Get", txtest.CtxWithDBMatcher(), appTemplateID).Return(fixAppTemplate(), nil)
  5275  	return svc
  5276  }