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

     1  package runtime_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/internal/domain/runtime/automock"
     7  	"github.com/kyma-incubator/compass/components/director/internal/repo/testdb"
     8  	"github.com/pkg/errors"
     9  	"github.com/stretchr/testify/require"
    10  
    11  	"github.com/kyma-incubator/compass/components/director/internal/domain/runtime"
    12  	"github.com/kyma-incubator/compass/components/director/internal/model"
    13  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    14  	"github.com/stretchr/testify/assert"
    15  )
    16  
    17  func TestConverter_ToGraphQL(t *testing.T) {
    18  	allDetailsInput := fixDetailedModelRuntime(t, "foo", "Foo", "Lorem ipsum", "test.ns")
    19  	allDetailsExpected := fixDetailedGQLRuntime(t, "foo", "Foo", "Lorem ipsum", "test.ns")
    20  
    21  	// GIVEN
    22  	testCases := []struct {
    23  		Name     string
    24  		Input    *model.Runtime
    25  		Expected *graphql.Runtime
    26  	}{
    27  		{
    28  			Name:     "All properties given",
    29  			Input:    allDetailsInput,
    30  			Expected: allDetailsExpected,
    31  		},
    32  		{
    33  			Name:  "Empty",
    34  			Input: &model.Runtime{},
    35  			Expected: &graphql.Runtime{
    36  				Status: &graphql.RuntimeStatus{
    37  					Condition: graphql.RuntimeStatusConditionInitial,
    38  				},
    39  				Metadata: &graphql.RuntimeMetadata{
    40  					CreationTimestamp: graphql.Timestamp{},
    41  				},
    42  			},
    43  		},
    44  		{
    45  			Name:     "Nil",
    46  			Input:    nil,
    47  			Expected: nil,
    48  		},
    49  	}
    50  
    51  	for _, testCase := range testCases {
    52  		t.Run(testCase.Name, func(t *testing.T) {
    53  			// WHEN
    54  
    55  			converter := runtime.NewConverter(nil)
    56  			res := converter.ToGraphQL(testCase.Input)
    57  
    58  			// then
    59  			assert.Equal(t, testCase.Expected, res)
    60  		})
    61  	}
    62  }
    63  
    64  func TestConverter_MultipleToGraphQL(t *testing.T) {
    65  	// GIVEN
    66  	input := []*model.Runtime{
    67  		fixModelRuntime(t, "foo", "tenant-foo", "Foo", "Lorem ipsum", "test.ns"),
    68  		fixModelRuntime(t, "bar", "tenant-bar", "Bar", "Dolor sit amet", "test.ns"),
    69  		{},
    70  		nil,
    71  	}
    72  	expected := []*graphql.Runtime{
    73  		fixGQLRuntime(t, "foo", "Foo", "Lorem ipsum", "test.ns"),
    74  		fixGQLRuntime(t, "bar", "Bar", "Dolor sit amet", "test.ns"),
    75  		{
    76  			Status: &graphql.RuntimeStatus{
    77  				Condition: graphql.RuntimeStatusConditionInitial,
    78  			},
    79  			Metadata: &graphql.RuntimeMetadata{
    80  				CreationTimestamp: graphql.Timestamp{},
    81  			},
    82  		},
    83  	}
    84  
    85  	// WHEN
    86  	converter := runtime.NewConverter(nil)
    87  	res := converter.MultipleToGraphQL(input)
    88  
    89  	// then
    90  	assert.Equal(t, expected, res)
    91  }
    92  
    93  func TestConverter_RegisterInputFromGraphQL(t *testing.T) {
    94  	// GIVEN
    95  	gqlWebhooks := []*graphql.WebhookInput{{Type: "type"}}
    96  	modelWebhooks := []*model.WebhookInput{{Type: "type"}}
    97  	var emptyGqlWebhooks []*graphql.WebhookInput
    98  	var emptyModelWebhooks []*model.WebhookInput
    99  
   100  	testErr := errors.New("test error")
   101  
   102  	testCases := []struct {
   103  		Name          string
   104  		Input         graphql.RuntimeRegisterInput
   105  		Expected      model.RuntimeRegisterInput
   106  		ConverterFn   func() *automock.WebhookConverter
   107  		ExpectedError string
   108  	}{
   109  		{
   110  			Name:     "All properties given",
   111  			Input:    fixGQLRuntimeRegisterInput("foo", "Lorem ipsum", "test.ns", gqlWebhooks),
   112  			Expected: fixModelRuntimeRegisterInput("foo", "Lorem ipsum", "test.ns", modelWebhooks),
   113  			ConverterFn: func() *automock.WebhookConverter {
   114  				converter := &automock.WebhookConverter{}
   115  				converter.Mock.On("MultipleInputFromGraphQL", gqlWebhooks).Return(modelWebhooks, nil)
   116  				return converter
   117  			},
   118  			ExpectedError: "",
   119  		},
   120  		{
   121  			Name:     "Empty",
   122  			Input:    graphql.RuntimeRegisterInput{},
   123  			Expected: model.RuntimeRegisterInput{},
   124  			ConverterFn: func() *automock.WebhookConverter {
   125  				converter := &automock.WebhookConverter{}
   126  				converter.Mock.On("MultipleInputFromGraphQL", emptyGqlWebhooks).Return(emptyModelWebhooks, nil)
   127  				return converter
   128  			},
   129  			ExpectedError: "",
   130  		},
   131  		{
   132  			Name:     "Error While converting webhooks",
   133  			Input:    fixGQLRuntimeRegisterInput("foo", "Lorem ipsum", "test.ns", gqlWebhooks),
   134  			Expected: model.RuntimeRegisterInput{},
   135  			ConverterFn: func() *automock.WebhookConverter {
   136  				converter := &automock.WebhookConverter{}
   137  				converter.Mock.On("MultipleInputFromGraphQL", gqlWebhooks).Return(nil, testErr)
   138  				return converter
   139  			},
   140  			ExpectedError: "test error",
   141  		},
   142  	}
   143  
   144  	for _, testCase := range testCases {
   145  		t.Run(testCase.Name, func(t *testing.T) {
   146  			// WHEN
   147  			converter := runtime.NewConverter(testCase.ConverterFn())
   148  			res, err := converter.RegisterInputFromGraphQL(testCase.Input)
   149  			if testCase.ExpectedError == "" {
   150  				assert.NoError(t, err)
   151  
   152  				// then
   153  				assert.Equal(t, testCase.Expected, res)
   154  			} else {
   155  				assert.Equal(t, testCase.ExpectedError, err.Error())
   156  			}
   157  		})
   158  	}
   159  }
   160  
   161  func TestConverter_RegisterInputFromGraphQL_StatusCondition(t *testing.T) {
   162  	testErr := errors.New("test error")
   163  	var emptyGqlWebhooks []*graphql.WebhookInput
   164  	var emptyModelWebhooks []*model.WebhookInput
   165  
   166  	testCases := []struct {
   167  		Name           string
   168  		CondtionGQL    graphql.RuntimeStatusCondition
   169  		ConditionModel model.RuntimeStatusCondition
   170  		ConverterFn    func() *automock.WebhookConverter
   171  		ExpectedError  string
   172  	}{
   173  		{
   174  			Name:           "When status condition is FAILED",
   175  			CondtionGQL:    graphql.RuntimeStatusConditionFailed,
   176  			ConditionModel: model.RuntimeStatusConditionFailed,
   177  			ConverterFn: func() *automock.WebhookConverter {
   178  				converter := &automock.WebhookConverter{}
   179  				converter.Mock.On("MultipleInputFromGraphQL", emptyGqlWebhooks).Return(emptyModelWebhooks, nil)
   180  				return converter
   181  			},
   182  			ExpectedError: "",
   183  		},
   184  		{
   185  			Name:           "When status condition is CONNECTED",
   186  			CondtionGQL:    graphql.RuntimeStatusConditionConnected,
   187  			ConditionModel: model.RuntimeStatusConditionConnected,
   188  			ConverterFn: func() *automock.WebhookConverter {
   189  				converter := &automock.WebhookConverter{}
   190  				converter.Mock.On("MultipleInputFromGraphQL", emptyGqlWebhooks).Return(emptyModelWebhooks, nil)
   191  				return converter
   192  			},
   193  			ExpectedError: "",
   194  		},
   195  		{
   196  			Name:           "When status condition is INITIAL",
   197  			CondtionGQL:    graphql.RuntimeStatusConditionInitial,
   198  			ConditionModel: model.RuntimeStatusConditionInitial,
   199  			ConverterFn: func() *automock.WebhookConverter {
   200  				converter := &automock.WebhookConverter{}
   201  				converter.Mock.On("MultipleInputFromGraphQL", emptyGqlWebhooks).Return(emptyModelWebhooks, nil)
   202  				return converter
   203  			},
   204  			ExpectedError: "",
   205  		},
   206  		{
   207  			Name:           "When status condition is PROVISIONING",
   208  			CondtionGQL:    graphql.RuntimeStatusConditionProvisioning,
   209  			ConditionModel: model.RuntimeStatusConditionProvisioning,
   210  			ConverterFn: func() *automock.WebhookConverter {
   211  				converter := &automock.WebhookConverter{}
   212  				converter.Mock.On("MultipleInputFromGraphQL", emptyGqlWebhooks).Return(emptyModelWebhooks, nil)
   213  				return converter
   214  			},
   215  			ExpectedError: "",
   216  		},
   217  		{
   218  			Name:           "When status condition is PROVISIONING",
   219  			CondtionGQL:    graphql.RuntimeStatusConditionProvisioning,
   220  			ConditionModel: model.RuntimeStatusConditionProvisioning,
   221  			ConverterFn: func() *automock.WebhookConverter {
   222  				converter := &automock.WebhookConverter{}
   223  				converter.Mock.On("MultipleInputFromGraphQL", emptyGqlWebhooks).Return(emptyModelWebhooks, testErr)
   224  				return converter
   225  			},
   226  			ExpectedError: "test error",
   227  		},
   228  	}
   229  
   230  	for _, testCase := range testCases {
   231  		t.Run(testCase.Name, func(t *testing.T) {
   232  			gqlApp := graphql.RuntimeRegisterInput{StatusCondition: &testCase.CondtionGQL}
   233  
   234  			converter := runtime.NewConverter(testCase.ConverterFn())
   235  			modelApp, err := converter.RegisterInputFromGraphQL(gqlApp)
   236  			if testCase.ExpectedError == "" {
   237  				assert.NoError(t, err)
   238  
   239  				// then
   240  				require.Equal(t, &testCase.ConditionModel, modelApp.StatusCondition)
   241  			} else {
   242  				assert.Equal(t, testCase.ExpectedError, err.Error())
   243  			}
   244  		})
   245  	}
   246  }
   247  
   248  func TestConverter_UpdateInputFromGraphQL(t *testing.T) {
   249  	// GIVEN
   250  	testCases := []struct {
   251  		Name     string
   252  		Input    graphql.RuntimeUpdateInput
   253  		Expected model.RuntimeUpdateInput
   254  	}{
   255  		{
   256  			Name:     "All properties given",
   257  			Input:    fixGQLRuntimeUpdateInput("foo", "Lorem ipsum"),
   258  			Expected: fixModelRuntimeUpdateInput("foo", "Lorem ipsum"),
   259  		},
   260  		{
   261  			Name:     "Empty",
   262  			Input:    graphql.RuntimeUpdateInput{},
   263  			Expected: model.RuntimeUpdateInput{},
   264  		},
   265  	}
   266  
   267  	for _, testCase := range testCases {
   268  		t.Run(testCase.Name, func(t *testing.T) {
   269  			// WHEN
   270  			converter := runtime.NewConverter(nil)
   271  			res := converter.UpdateInputFromGraphQL(testCase.Input)
   272  
   273  			// then
   274  			assert.Equal(t, testCase.Expected, res)
   275  		})
   276  	}
   277  }
   278  
   279  func TestConverter_UpdateInputFromGraphQL_StatusCondition(t *testing.T) {
   280  	testCases := []struct {
   281  		Name           string
   282  		CondtionGQL    graphql.RuntimeStatusCondition
   283  		ConditionModel model.RuntimeStatusCondition
   284  	}{
   285  		{
   286  			Name:           "When status condition is FAILED",
   287  			CondtionGQL:    graphql.RuntimeStatusConditionFailed,
   288  			ConditionModel: model.RuntimeStatusConditionFailed,
   289  		},
   290  		{
   291  			Name:           "When status condition is CONNECTED",
   292  			CondtionGQL:    graphql.RuntimeStatusConditionConnected,
   293  			ConditionModel: model.RuntimeStatusConditionConnected,
   294  		},
   295  		{
   296  			Name:           "When status condition is INITIAL",
   297  			CondtionGQL:    graphql.RuntimeStatusConditionInitial,
   298  			ConditionModel: model.RuntimeStatusConditionInitial,
   299  		},
   300  		{
   301  			Name:           "When status condition is PROVISIONING",
   302  			CondtionGQL:    graphql.RuntimeStatusConditionProvisioning,
   303  			ConditionModel: model.RuntimeStatusConditionProvisioning,
   304  		},
   305  	}
   306  
   307  	for _, testCase := range testCases {
   308  		t.Run(testCase.Name, func(t *testing.T) {
   309  			gqlApp := graphql.RuntimeUpdateInput{StatusCondition: &testCase.CondtionGQL}
   310  
   311  			converter := runtime.NewConverter(nil)
   312  			modelApp := converter.UpdateInputFromGraphQL(gqlApp)
   313  
   314  			require.Equal(t, &testCase.ConditionModel, modelApp.StatusCondition)
   315  		})
   316  	}
   317  }
   318  
   319  func TestConverter_ToEntity(t *testing.T) {
   320  	conv := runtime.NewConverter(nil)
   321  
   322  	t.Run("All properties given", func(t *testing.T) {
   323  		// GIVEN
   324  		rtModel := fixDetailedModelRuntime(t, "foo", "Foo", "Lorem ipsum", "test.ns")
   325  
   326  		// WHEN
   327  		rtEntity, err := conv.ToEntity(rtModel)
   328  
   329  		// THEN
   330  		assert.NoError(t, err)
   331  		assertRuntimeDefinition(t, rtModel, rtEntity)
   332  	})
   333  
   334  	t.Run("Nil", func(t *testing.T) {
   335  		// WHEN
   336  		rtEntity, err := conv.ToEntity(nil)
   337  
   338  		// THEN
   339  		assert.NoError(t, err)
   340  		assert.Nil(t, rtEntity)
   341  	})
   342  
   343  	t.Run("Empty", func(t *testing.T) {
   344  		// GIVEN
   345  		rtModel := &model.Runtime{}
   346  
   347  		// WHEN
   348  		rtEntity, err := conv.ToEntity(rtModel)
   349  
   350  		// THEN
   351  		if err != nil {
   352  			assert.Contains(t, err.Error(), "invalid input model")
   353  		} else {
   354  			assertRuntimeDefinition(t, rtModel, rtEntity)
   355  		}
   356  	})
   357  }
   358  
   359  func TestConverter_FromEntity(t *testing.T) {
   360  	conv := runtime.NewConverter(nil)
   361  
   362  	t.Run("All properties given", func(t *testing.T) {
   363  		// GIVEN
   364  		rtEntity := fixDetailedEntityRuntime(t, "foo", "Foo", "Lorem ipsum", "test.ns")
   365  
   366  		// WHEN
   367  		rtModel := conv.FromEntity(rtEntity)
   368  
   369  		// THEN
   370  		assertRuntimeDefinition(t, rtModel, rtEntity)
   371  	})
   372  
   373  	t.Run("Nil", func(t *testing.T) {
   374  		// WHEN
   375  		rtModel := conv.FromEntity(nil)
   376  
   377  		// THEN
   378  		assert.Nil(t, rtModel)
   379  	})
   380  
   381  	t.Run("Empty", func(t *testing.T) {
   382  		// GIVEN
   383  		rtEntity := &runtime.Runtime{}
   384  
   385  		// WHEN
   386  		rtModel := conv.FromEntity(rtEntity)
   387  
   388  		// THEN
   389  		assertRuntimeDefinition(t, rtModel, rtEntity)
   390  	})
   391  }
   392  
   393  func assertRuntimeDefinition(t *testing.T, runtimeModel *model.Runtime, entity *runtime.Runtime) {
   394  	assert.Equal(t, runtimeModel.ID, entity.ID)
   395  	assert.Equal(t, runtimeModel.Name, entity.Name)
   396  
   397  	if runtimeModel.Status != nil {
   398  		assert.Equal(t, runtimeModel.Status.Condition, model.RuntimeStatusCondition(entity.StatusCondition))
   399  		assert.Equal(t, runtimeModel.Status.Timestamp, entity.StatusTimestamp)
   400  	} else {
   401  		assert.Equal(t, string(model.RuntimeStatusConditionInitial), entity.StatusCondition)
   402  	}
   403  
   404  	testdb.AssertSQLNullStringEqualTo(t, entity.Description, runtimeModel.Description)
   405  	testdb.AssertSQLNullStringEqualTo(t, entity.ApplicationNamespace, runtimeModel.ApplicationNamespace)
   406  }