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

     1  package formationmapping_test
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"net/http"
     7  	"testing"
     8  
     9  	tenantpkg "github.com/kyma-incubator/compass/components/director/pkg/tenant"
    10  
    11  	"github.com/kyma-incubator/compass/components/director/pkg/persistence/txtest"
    12  	"github.com/pkg/errors"
    13  
    14  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    15  	webhookclient "github.com/kyma-incubator/compass/components/director/pkg/webhook_client"
    16  
    17  	"github.com/kyma-incubator/compass/components/director/internal/domain/tenant"
    18  	fm "github.com/kyma-incubator/compass/components/director/internal/formationmapping"
    19  	"github.com/kyma-incubator/compass/components/director/internal/formationmapping/automock"
    20  	"github.com/kyma-incubator/compass/components/director/internal/model"
    21  	"github.com/kyma-incubator/compass/components/director/pkg/consumer"
    22  	persistenceautomock "github.com/kyma-incubator/compass/components/director/pkg/persistence/automock"
    23  	"github.com/stretchr/testify/mock"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  var (
    28  	emptyCtx = context.Background()
    29  	testErr  = errors.New("test error")
    30  	txGen    = txtest.NewTransactionContextGenerator(testErr)
    31  
    32  	// Tenant IDs variables
    33  	internalTntID = "testInternalID"
    34  	externalTntID = "testExternalID"
    35  
    36  	// Formation Assignment variables
    37  	faSourceID                = "testSourceID"
    38  	faTargetID                = "testTargetID"
    39  	testFormationAssignmentID = "testFormationAssignmentID"
    40  
    41  	// Formation variables
    42  	testFormationID         = "testFormationID"
    43  	testFormationName       = "testFormationName"
    44  	testFormationTemplateID = "testFormationTemplateID"
    45  )
    46  
    47  func fixTestHandler(t *testing.T) http.HandlerFunc {
    48  	return func(writer http.ResponseWriter, request *http.Request) {
    49  		writer.WriteHeader(http.StatusOK)
    50  		_, err := writer.Write([]byte("OK"))
    51  		require.NoError(t, err)
    52  	}
    53  }
    54  
    55  func fixRequestWithContext(t *testing.T, ctx context.Context, httpMethod string) *http.Request {
    56  	reqWithContext, err := http.NewRequest(httpMethod, "/", nil)
    57  	require.NoError(t, err)
    58  	reqWithContext = reqWithContext.WithContext(ctx)
    59  	return reqWithContext
    60  }
    61  
    62  func fixGetConsumer(consumerID string, consumerType consumer.ConsumerType) consumer.Consumer {
    63  	return consumer.Consumer{
    64  		ConsumerID:   consumerID,
    65  		ConsumerType: consumerType,
    66  	}
    67  }
    68  
    69  func fixContextWithTenantAndConsumer(c consumer.Consumer, internalTntID, externalTntID string) context.Context {
    70  	tenantCtx := tenant.SaveToContext(emptyCtx, internalTntID, externalTntID)
    71  	consumerAndTenantCtx := consumer.SaveToContext(tenantCtx, c)
    72  
    73  	return consumerAndTenantCtx
    74  }
    75  
    76  func fixContextWithConsumer(c consumer.Consumer) context.Context {
    77  	return consumer.SaveToContext(emptyCtx, c)
    78  }
    79  
    80  func fixFormationWithState(state model.FormationState) *model.Formation {
    81  	return &model.Formation{
    82  		ID:                  testFormationID,
    83  		TenantID:            internalTntID,
    84  		FormationTemplateID: testFormationTemplateID,
    85  		Name:                testFormationName,
    86  		State:               state,
    87  	}
    88  }
    89  
    90  func fixFormationAssignmentModel(testFormationID, testTenantID, sourceID, targetID string, sourceFAType, targetFAType model.FormationAssignmentType) *model.FormationAssignment {
    91  	return &model.FormationAssignment{
    92  		ID:          "ID",
    93  		FormationID: testFormationID,
    94  		TenantID:    testTenantID,
    95  		Source:      sourceID,
    96  		SourceType:  sourceFAType,
    97  		Target:      targetID,
    98  		TargetType:  targetFAType,
    99  	}
   100  }
   101  
   102  func fixFormationAssignmentModelWithStateAndConfig(testFormationAssignmentID, testFormationID, testTenantID, sourceID, targetID string, sourceFAType, targetFAType model.FormationAssignmentType, state model.FormationAssignmentState, config string) *model.FormationAssignment {
   103  	return &model.FormationAssignment{
   104  		ID:          testFormationAssignmentID,
   105  		FormationID: testFormationID,
   106  		TenantID:    testTenantID,
   107  		Source:      sourceID,
   108  		SourceType:  sourceFAType,
   109  		Target:      targetID,
   110  		TargetType:  targetFAType,
   111  		State:       string(state),
   112  		Value:       json.RawMessage(config),
   113  	}
   114  }
   115  
   116  func fixBusinessTenantMapping() *model.BusinessTenantMapping {
   117  	return &model.BusinessTenantMapping{
   118  		ID:             internalTntID,
   119  		Name:           "tnt",
   120  		ExternalTenant: externalTntID,
   121  		Type:           tenantpkg.Account,
   122  	}
   123  }
   124  
   125  func fixResourceGroupBusinessTenantMapping() *model.BusinessTenantMapping {
   126  	return &model.BusinessTenantMapping{
   127  		ID:             internalTntID,
   128  		Name:           "tnt",
   129  		ExternalTenant: externalTntID,
   130  		Type:           tenantpkg.ResourceGroup,
   131  	}
   132  }
   133  
   134  func fixEmptyNotificationRequest() *webhookclient.FormationAssignmentNotificationRequest {
   135  	return &webhookclient.FormationAssignmentNotificationRequest{
   136  		Webhook:       graphql.Webhook{},
   137  		Object:        nil,
   138  		CorrelationID: "",
   139  	}
   140  }
   141  
   142  func contextThatHasTenant(expectedTenant string) interface{} {
   143  	return mock.MatchedBy(func(actual context.Context) bool {
   144  		actualTenant, err := tenant.LoadFromContext(actual)
   145  		if err != nil {
   146  			return false
   147  		}
   148  		return actualTenant == expectedTenant
   149  	})
   150  }
   151  
   152  func contextThatHasConsumer(expectedConsumerID string) interface{} {
   153  	return mock.MatchedBy(func(actual context.Context) bool {
   154  		consumer, err := consumer.LoadFromContext(actual)
   155  		if err != nil {
   156  			return false
   157  		}
   158  		return consumer.ConsumerID == expectedConsumerID
   159  	})
   160  }
   161  
   162  func fixBuildExpectedErrResponse(t *testing.T, errMsg string) string {
   163  	errorResponse := fm.ErrorResponse{Message: errMsg}
   164  	encodingErr, err := json.Marshal(errorResponse)
   165  	require.NoError(t, err)
   166  	return string(encodingErr)
   167  }
   168  
   169  func fixUnusedTransactioner() (*persistenceautomock.PersistenceTx, *persistenceautomock.Transactioner) {
   170  	return &persistenceautomock.PersistenceTx{}, &persistenceautomock.Transactioner{}
   171  }
   172  
   173  func fixUnusedFormationAssignmentSvc() *automock.FormationAssignmentService {
   174  	return &automock.FormationAssignmentService{}
   175  }
   176  
   177  func fixUnusedFormationAssignmentNotificationSvc() *automock.FormationAssignmentNotificationService {
   178  	return &automock.FormationAssignmentNotificationService{}
   179  }
   180  
   181  func fixUnusedFormationSvc() *automock.FormationService {
   182  	return &automock.FormationService{}
   183  }
   184  
   185  func fixUnusedRuntimeRepo() *automock.RuntimeRepository {
   186  	return &automock.RuntimeRepository{}
   187  }
   188  
   189  func fixUnusedRuntimeContextRepo() *automock.RuntimeContextRepository {
   190  	return &automock.RuntimeContextRepository{}
   191  }
   192  
   193  func fixUnusedAppRepo() *automock.ApplicationRepository {
   194  	return &automock.ApplicationRepository{}
   195  }
   196  
   197  func fixUnusedAppTemplateRepo() *automock.ApplicationTemplateRepository {
   198  	return &automock.ApplicationTemplateRepository{}
   199  }
   200  
   201  func fixUnusedLabelRepo() *automock.LabelRepository {
   202  	return &automock.LabelRepository{}
   203  }
   204  
   205  func fixUnusedTenantRepo() *automock.TenantRepository {
   206  	return &automock.TenantRepository{}
   207  }
   208  
   209  func fixUnusedFormationRepo() *automock.FormationRepository {
   210  	return &automock.FormationRepository{}
   211  }
   212  
   213  func fixUnusedFormationTemplateRepo() *automock.FormationTemplateRepository {
   214  	return &automock.FormationTemplateRepository{}
   215  }