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

     1  package formationtemplate_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/internal/domain/formationtemplate/automock"
     7  	"github.com/pkg/errors"
     8  
     9  	"github.com/kyma-incubator/compass/components/director/internal/model"
    10  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    11  
    12  	"github.com/kyma-incubator/compass/components/director/internal/domain/formationtemplate"
    13  	"github.com/stretchr/testify/assert"
    14  	"github.com/stretchr/testify/require"
    15  )
    16  
    17  var testErr = errors.New("test-error")
    18  
    19  func TestConverter_FromInputGraphQL(t *testing.T) {
    20  	modelWebhookInputs := fixModelWebhookInput()
    21  	GQLWebhooksInputs := fixGQLWebhookInput()
    22  
    23  	testCases := []struct {
    24  		Name               string
    25  		Input              *graphql.FormationTemplateInput
    26  		WebhookConverterFn func() *automock.WebhookConverter
    27  		Expected           *model.FormationTemplateInput
    28  		ExpectedErr        bool
    29  	}{
    30  		{
    31  			Name:  "Success",
    32  			Input: &formationTemplateGraphQLInput,
    33  			WebhookConverterFn: func() *automock.WebhookConverter {
    34  				conv := &automock.WebhookConverter{}
    35  				conv.On("MultipleInputFromGraphQL", GQLWebhooksInputs).Return(modelWebhookInputs, nil)
    36  				return conv
    37  			},
    38  			Expected: &formationTemplateModelInput,
    39  		},
    40  		{
    41  			Name:  "Success for app only templates",
    42  			Input: &formationTemplateGraphQLInputAppOnly,
    43  			WebhookConverterFn: func() *automock.WebhookConverter {
    44  				conv := &automock.WebhookConverter{}
    45  				conv.On("MultipleInputFromGraphQL", GQLWebhooksInputs).Return(modelWebhookInputs, nil)
    46  				return conv
    47  			},
    48  			Expected: &formationTemplateModelInputAppOnly,
    49  		},
    50  		{
    51  			Name:  "Error when converting webhooks",
    52  			Input: &formationTemplateGraphQLInput,
    53  			WebhookConverterFn: func() *automock.WebhookConverter {
    54  				conv := &automock.WebhookConverter{}
    55  				conv.On("MultipleInputFromGraphQL", GQLWebhooksInputs).Return(nil, testErr)
    56  				return conv
    57  			},
    58  			Expected:    nil,
    59  			ExpectedErr: true,
    60  		},
    61  		{
    62  			Name:  "Empty",
    63  			Input: nil,
    64  			WebhookConverterFn: func() *automock.WebhookConverter {
    65  				return &automock.WebhookConverter{}
    66  			},
    67  			Expected:    nil,
    68  			ExpectedErr: false,
    69  		},
    70  	}
    71  	for _, testCase := range testCases {
    72  		t.Run(testCase.Name, func(t *testing.T) {
    73  			// GIVEN
    74  			whConverter := testCase.WebhookConverterFn()
    75  			converter := formationtemplate.NewConverter(whConverter)
    76  			// WHEN
    77  			result, err := converter.FromInputGraphQL(testCase.Input)
    78  			if testCase.ExpectedErr {
    79  				require.Error(t, err)
    80  			} else {
    81  				require.NoError(t, err)
    82  			}
    83  
    84  			assert.Equal(t, result, testCase.Expected)
    85  			whConverter.AssertExpectations(t)
    86  		})
    87  	}
    88  }
    89  
    90  func TestConverter_FromModelInputToModel(t *testing.T) {
    91  	testCases := []struct {
    92  		Name     string
    93  		Input    *model.FormationTemplateInput
    94  		Expected *model.FormationTemplate
    95  	}{{
    96  		Name:     "Success",
    97  		Input:    &formationTemplateModelInput,
    98  		Expected: &formationTemplateModel,
    99  	}, {
   100  		Name:     "Empty",
   101  		Input:    nil,
   102  		Expected: nil,
   103  	},
   104  	}
   105  	for _, testCase := range testCases {
   106  		t.Run(testCase.Name, func(t *testing.T) {
   107  			// GIVEN
   108  			converter := formationtemplate.NewConverter(nil)
   109  			// WHEN
   110  			result := converter.FromModelInputToModel(testCase.Input, testID, testTenantID)
   111  
   112  			if testCase.Expected != nil {
   113  				testCase.Expected.Webhooks[0].ID = result.Webhooks[0].ID // id is generated ad-hoc and can't be mocked
   114  			}
   115  
   116  			assert.Equal(t, result, testCase.Expected)
   117  		})
   118  	}
   119  }
   120  
   121  func TestConverter_ToGraphQL(t *testing.T) {
   122  	GQLWebhooks := []*graphql.Webhook{fixFormationTemplateGQLWebhook()}
   123  
   124  	testCases := []struct {
   125  		Name               string
   126  		Input              *model.FormationTemplate
   127  		WebhookConverterFn func() *automock.WebhookConverter
   128  		Expected           *graphql.FormationTemplate
   129  		ExpectedErr        bool
   130  	}{
   131  		{
   132  			Name:  "Success",
   133  			Input: &formationTemplateModel,
   134  			WebhookConverterFn: func() *automock.WebhookConverter {
   135  				conv := &automock.WebhookConverter{}
   136  				conv.On("MultipleToGraphQL", formationTemplateModel.Webhooks).Return(GQLWebhooks, nil)
   137  				return conv
   138  			},
   139  			Expected: &graphQLFormationTemplate,
   140  		},
   141  		{
   142  			Name:  "Error when converting webhook",
   143  			Input: &formationTemplateModel,
   144  			WebhookConverterFn: func() *automock.WebhookConverter {
   145  				conv := &automock.WebhookConverter{}
   146  				conv.On("MultipleToGraphQL", formationTemplateModel.Webhooks).Return(nil, testErr)
   147  				return conv
   148  			},
   149  			Expected:    nil,
   150  			ExpectedErr: true,
   151  		},
   152  		{
   153  			Name:  "Empty",
   154  			Input: nil,
   155  			WebhookConverterFn: func() *automock.WebhookConverter {
   156  				return &automock.WebhookConverter{}
   157  			},
   158  			Expected: nil,
   159  		},
   160  	}
   161  	for _, testCase := range testCases {
   162  		t.Run(testCase.Name, func(t *testing.T) {
   163  			// GIVEN
   164  			whConverter := testCase.WebhookConverterFn()
   165  			converter := formationtemplate.NewConverter(whConverter)
   166  			// WHEN
   167  			result, err := converter.ToGraphQL(testCase.Input)
   168  			if testCase.ExpectedErr {
   169  				require.Error(t, err)
   170  			} else {
   171  				require.NoError(t, err)
   172  			}
   173  
   174  			assert.Equal(t, result, testCase.Expected)
   175  			whConverter.AssertExpectations(t)
   176  		})
   177  	}
   178  }
   179  
   180  func TestConverter_MultipleToGraphQL(t *testing.T) {
   181  	GQLWebhooks := []*graphql.Webhook{fixFormationTemplateGQLWebhook()}
   182  	testCases := []struct {
   183  		Name               string
   184  		Input              []*model.FormationTemplate
   185  		WebhookConverterFn func() *automock.WebhookConverter
   186  		Expected           []*graphql.FormationTemplate
   187  		ExpectedErr        bool
   188  	}{
   189  		{
   190  			Name:  "Success",
   191  			Input: formationTemplateModelPage.Data,
   192  			WebhookConverterFn: func() *automock.WebhookConverter {
   193  				conv := &automock.WebhookConverter{}
   194  				conv.On("MultipleToGraphQL", formationTemplateModel.Webhooks).Return(GQLWebhooks, nil)
   195  				return conv
   196  			},
   197  			Expected: graphQLFormationTemplatePage.Data,
   198  		},
   199  		{
   200  			Name:  "Error when converting webhook",
   201  			Input: formationTemplateModelPage.Data,
   202  			WebhookConverterFn: func() *automock.WebhookConverter {
   203  				conv := &automock.WebhookConverter{}
   204  				conv.On("MultipleToGraphQL", formationTemplateModel.Webhooks).Return(nil, testErr)
   205  				return conv
   206  			},
   207  			Expected:    nil,
   208  			ExpectedErr: true,
   209  		},
   210  		{
   211  			Name:  "Empty",
   212  			Input: nil,
   213  			WebhookConverterFn: func() *automock.WebhookConverter {
   214  				return &automock.WebhookConverter{}
   215  			},
   216  			Expected: nil,
   217  		},
   218  	}
   219  	for _, testCase := range testCases {
   220  		t.Run(testCase.Name, func(t *testing.T) {
   221  			// GIVEN
   222  			whConverter := testCase.WebhookConverterFn()
   223  			converter := formationtemplate.NewConverter(whConverter)
   224  			// WHEN
   225  			result, err := converter.MultipleToGraphQL(testCase.Input)
   226  			if testCase.ExpectedErr {
   227  				require.Error(t, err)
   228  			} else {
   229  				require.NoError(t, err)
   230  			}
   231  
   232  			assert.ElementsMatch(t, result, testCase.Expected)
   233  			whConverter.AssertExpectations(t)
   234  		})
   235  	}
   236  	//t.Run("Success", func(t *testing.T) {
   237  	//	// GIVEN
   238  	//	converter := formationtemplate.NewConverter(nil)
   239  	//	// WHEN
   240  	//	result := converter.MultipleToGraphQL(formationTemplateModelPage.Data)
   241  	//
   242  	//	// THEN
   243  	//	assert.ElementsMatch(t, result, graphQLFormationTemplatePage.Data)
   244  	//})
   245  	//t.Run("Returns nil when given empty model", func(t *testing.T) {
   246  	//	// GIVEN
   247  	//	converter := formationtemplate.NewConverter()
   248  	//	// WHEN
   249  	//	result := converter.MultipleToGraphQL(nil)
   250  	//
   251  	//	assert.Nil(t, result)
   252  	//})
   253  }
   254  
   255  func TestConverter_ToEntity(t *testing.T) {
   256  	t.Run("Success", func(t *testing.T) {
   257  		// GIVEN
   258  		converter := formationtemplate.NewConverter(nil)
   259  		// WHEN
   260  		result, err := converter.ToEntity(&formationTemplateModel)
   261  
   262  		// THEN
   263  		require.NoError(t, err)
   264  		assert.Equal(t, result, &formationTemplateEntity)
   265  	})
   266  	t.Run("Returns nil when given empty model", func(t *testing.T) {
   267  		// GIVEN
   268  		converter := formationtemplate.NewConverter(nil)
   269  		// WHEN
   270  		result, err := converter.ToEntity(nil)
   271  
   272  		assert.NoError(t, err)
   273  		assert.Nil(t, result)
   274  	})
   275  }
   276  
   277  func TestConverter_FromEntity(t *testing.T) {
   278  	formationTemplateModelWithoutWebhooks := formationTemplateModel
   279  	formationTemplateModelWithoutWebhooks.Webhooks = nil
   280  	testCases := []struct {
   281  		Name     string
   282  		Input    *formationtemplate.Entity
   283  		Expected *model.FormationTemplate
   284  	}{{
   285  		Name:     "Success",
   286  		Input:    &formationTemplateEntity,
   287  		Expected: &formationTemplateModelWithoutWebhooks,
   288  	}, {
   289  		Name:     "Empty",
   290  		Input:    nil,
   291  		Expected: nil,
   292  	},
   293  	}
   294  	for _, testCase := range testCases {
   295  		t.Run(testCase.Name, func(t *testing.T) {
   296  			// GIVEN
   297  			converter := formationtemplate.NewConverter(nil)
   298  			// WHEN
   299  			result, err := converter.FromEntity(testCase.Input)
   300  
   301  			assert.NoError(t, err)
   302  			assert.Equal(t, result, testCase.Expected)
   303  		})
   304  	}
   305  }