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

     1  package formationassignment_test
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/kyma-incubator/compass/components/director/internal/domain/formationassignment"
     7  	"github.com/kyma-incubator/compass/components/director/internal/model"
     8  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
     9  	"github.com/stretchr/testify/require"
    10  )
    11  
    12  var converter = formationassignment.NewConverter()
    13  
    14  func TestToGraphQL(t *testing.T) {
    15  	testCases := []struct {
    16  		Name           string
    17  		Input          *model.FormationAssignment
    18  		Expected       *graphql.FormationAssignment
    19  		ExpectedErrMsg string
    20  	}{
    21  		{
    22  			Name:     "Success",
    23  			Input:    fixFormationAssignmentModel(TestConfigValueRawJSON),
    24  			Expected: fixFormationAssignmentGQLModel(&TestConfigValueStr),
    25  		},
    26  		{
    27  			Name:     "Success when input is nil",
    28  			Input:    nil,
    29  			Expected: nil,
    30  		},
    31  		{
    32  			Name:           "Error when configuration value is invalid json",
    33  			Input:          fixFormationAssignmentModel(TestInvalidConfigValueRawJSON),
    34  			Expected:       nil,
    35  			ExpectedErrMsg: "while converting formation assignment to GraphQL",
    36  		},
    37  	}
    38  
    39  	for _, testCase := range testCases {
    40  		t.Run(testCase.Name, func(t *testing.T) {
    41  			// WHEN
    42  			r, err := converter.ToGraphQL(testCase.Input)
    43  
    44  			if testCase.ExpectedErrMsg != "" {
    45  				require.Error(t, err)
    46  				require.Contains(t, err.Error(), testCase.ExpectedErrMsg)
    47  			} else {
    48  				require.NoError(t, err)
    49  			}
    50  
    51  			require.Equal(t, r, testCase.Expected)
    52  		})
    53  	}
    54  }
    55  
    56  func Test_converter_MultipleToGraphQL(t *testing.T) {
    57  	testCases := []struct {
    58  		Name                         string
    59  		InputFormationAssignments    []*model.FormationAssignment
    60  		ExpectedFormationAssignments []*graphql.FormationAssignment
    61  		ExpectedErrorMsg             string
    62  	}{
    63  		{
    64  			Name:                         "Success",
    65  			InputFormationAssignments:    []*model.FormationAssignment{fixFormationAssignmentModel(TestConfigValueRawJSON)},
    66  			ExpectedFormationAssignments: []*graphql.FormationAssignment{fixFormationAssignmentGQLModel(&TestConfigValueStr)},
    67  		},
    68  		{
    69  			Name:                         "Success when input is nil",
    70  			InputFormationAssignments:    nil,
    71  			ExpectedFormationAssignments: nil,
    72  		},
    73  		{
    74  			Name:                         "Success when one of the input formations is nil",
    75  			InputFormationAssignments:    []*model.FormationAssignment{nil, fixFormationAssignmentModel(TestConfigValueRawJSON)},
    76  			ExpectedFormationAssignments: []*graphql.FormationAssignment{fixFormationAssignmentGQLModel(&TestConfigValueStr)},
    77  		},
    78  		{
    79  			Name:                         "Error when conversion to GraphQL failed",
    80  			InputFormationAssignments:    []*model.FormationAssignment{fixFormationAssignmentModel(TestInvalidConfigValueRawJSON)},
    81  			ExpectedFormationAssignments: []*graphql.FormationAssignment{},
    82  			ExpectedErrorMsg:             "while converting formation assignment to GraphQL",
    83  		},
    84  	}
    85  
    86  	for _, testCase := range testCases {
    87  		t.Run(testCase.Name, func(t *testing.T) {
    88  			result, err := converter.MultipleToGraphQL(testCase.InputFormationAssignments)
    89  			if testCase.ExpectedErrorMsg != "" {
    90  				require.Error(t, err)
    91  				require.Contains(t, err.Error(), testCase.ExpectedErrorMsg)
    92  			} else {
    93  				require.NoError(t, err)
    94  			}
    95  
    96  			require.ElementsMatch(t, testCase.ExpectedFormationAssignments, result)
    97  		})
    98  	}
    99  }
   100  
   101  func TestConverter_ToEntity(t *testing.T) {
   102  	testCases := []struct {
   103  		Name     string
   104  		Input    *model.FormationAssignment
   105  		Expected *formationassignment.Entity
   106  	}{
   107  		{
   108  			Name:     "Success",
   109  			Input:    fixFormationAssignmentModel(TestConfigValueRawJSON),
   110  			Expected: fixFormationAssignmentEntity(TestConfigValueStr),
   111  		},
   112  		{
   113  			Name:     "Success when input is nil",
   114  			Input:    nil,
   115  			Expected: nil,
   116  		},
   117  	}
   118  	for _, testCase := range testCases {
   119  		t.Run(testCase.Name, func(t *testing.T) {
   120  			// WHEN
   121  			result := converter.ToEntity(testCase.Input)
   122  
   123  			require.Equal(t, result, testCase.Expected)
   124  		})
   125  	}
   126  }
   127  
   128  func TestConverter_FromEntity(t *testing.T) {
   129  	testCases := []struct {
   130  		Name     string
   131  		Input    *formationassignment.Entity
   132  		Expected *model.FormationAssignment
   133  	}{
   134  		{
   135  			Name:     "Success",
   136  			Input:    fixFormationAssignmentEntity(TestConfigValueStr),
   137  			Expected: fixFormationAssignmentModel(TestConfigValueRawJSON),
   138  		}, {
   139  			Name:     "Success when input is nil",
   140  			Input:    nil,
   141  			Expected: nil,
   142  		},
   143  	}
   144  	for _, testCase := range testCases {
   145  		t.Run(testCase.Name, func(t *testing.T) {
   146  			// WHEN
   147  			result := converter.FromEntity(testCase.Input)
   148  
   149  			require.Equal(t, result, testCase.Expected)
   150  		})
   151  	}
   152  }
   153  
   154  func TestConverter_ToInput(t *testing.T) {
   155  	testCases := []struct {
   156  		Name     string
   157  		Input    *model.FormationAssignment
   158  		Expected *model.FormationAssignmentInput
   159  	}{
   160  		{
   161  			Name:     "Success",
   162  			Input:    fixFormationAssignmentModel(TestConfigValueRawJSON),
   163  			Expected: fixFormationAssignmentModelInput(TestConfigValueRawJSON),
   164  		}, {
   165  			Name:     "Success when input is nil",
   166  			Input:    nil,
   167  			Expected: nil,
   168  		},
   169  	}
   170  	for _, testCase := range testCases {
   171  		t.Run(testCase.Name, func(t *testing.T) {
   172  			// WHEN
   173  			result := converter.ToInput(testCase.Input)
   174  
   175  			require.Equal(t, result, testCase.Expected)
   176  		})
   177  	}
   178  }
   179  
   180  func TestConverter_FromInput(t *testing.T) {
   181  	testCases := []struct {
   182  		Name     string
   183  		Input    *model.FormationAssignmentInput
   184  		Expected *model.FormationAssignment
   185  	}{
   186  		{
   187  			Name: "Success",
   188  			Expected: &model.FormationAssignment{
   189  				FormationID: TestFormationID,
   190  				Source:      TestSource,
   191  				SourceType:  TestSourceType,
   192  				Target:      TestTarget,
   193  				TargetType:  TestTargetType,
   194  				State:       TestStateInitial,
   195  				Value:       TestConfigValueRawJSON,
   196  			},
   197  			Input: fixFormationAssignmentModelInput(TestConfigValueRawJSON),
   198  		}, {
   199  			Name:     "Success when input is nil",
   200  			Input:    nil,
   201  			Expected: nil,
   202  		},
   203  	}
   204  	for _, testCase := range testCases {
   205  		t.Run(testCase.Name, func(t *testing.T) {
   206  			// WHEN
   207  			result := converter.FromInput(testCase.Input)
   208  
   209  			require.Equal(t, result, testCase.Expected)
   210  		})
   211  	}
   212  }