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

     1  package formation_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"testing"
     6  
     7  	"github.com/pkg/errors"
     8  
     9  	"github.com/kyma-incubator/compass/components/director/pkg/str"
    10  
    11  	"github.com/stretchr/testify/require"
    12  
    13  	"github.com/kyma-incubator/compass/components/director/internal/domain/formation"
    14  	"github.com/kyma-incubator/compass/components/director/internal/model"
    15  	"github.com/kyma-incubator/compass/components/director/pkg/graphql"
    16  )
    17  
    18  var converter = formation.NewConverter()
    19  
    20  func TestFromGraphQL(t *testing.T) {
    21  	t.Run("Success", func(t *testing.T) {
    22  		// WHEN
    23  		actual := converter.FromGraphQL(graphql.FormationInput{Name: testFormationName})
    24  
    25  		// THEN
    26  		require.Equal(t, testFormationName, actual.Name)
    27  	})
    28  	t.Run("Success when state is provided externally", func(t *testing.T) {
    29  		// WHEN
    30  		actual := converter.FromGraphQL(graphql.FormationInput{Name: testFormationName, State: str.Ptr(string(model.InitialFormationState))})
    31  
    32  		// THEN
    33  		require.Equal(t, testFormationName, actual.Name)
    34  		require.Equal(t, model.InitialFormationState, actual.State)
    35  	})
    36  }
    37  
    38  func TestToGraphQL(t *testing.T) {
    39  	testCases := []struct {
    40  		Name              string
    41  		Input             *model.Formation
    42  		ExpectedFormation *graphql.Formation
    43  		ExpectedError     error
    44  	}{
    45  		{
    46  			Name:              "Success",
    47  			Input:             &model.Formation{Name: testFormationName},
    48  			ExpectedFormation: &graphql.Formation{Name: testFormationName},
    49  		},
    50  		{
    51  			Name:              "Success when input is empty",
    52  			Input:             nil,
    53  			ExpectedFormation: nil,
    54  		},
    55  		{
    56  			Name:              "Returns error when can't unmarshal the error",
    57  			Input:             &model.Formation{Name: testFormationName, Error: json.RawMessage(`{invalid}`)},
    58  			ExpectedFormation: nil,
    59  			ExpectedError:     errors.New("while unmarshalling formation error"),
    60  		},
    61  	}
    62  	for _, testCase := range testCases {
    63  		t.Run(testCase.Name, func(t *testing.T) {
    64  			// WHEN
    65  			result, err := converter.ToGraphQL(testCase.Input)
    66  
    67  			if testCase.ExpectedError == nil {
    68  				require.Equal(t, result, testCase.ExpectedFormation)
    69  				require.NoError(t, err)
    70  			} else {
    71  				require.Nil(t, result)
    72  				require.Contains(t, err.Error(), testCase.ExpectedError.Error())
    73  			}
    74  		})
    75  	}
    76  }
    77  
    78  func TestConverter_ToEntity(t *testing.T) {
    79  	testCases := []struct {
    80  		Name     string
    81  		Input    *model.Formation
    82  		Expected *formation.Entity
    83  	}{
    84  		{
    85  			Name:     "Success",
    86  			Input:    fixFormationModel(),
    87  			Expected: fixFormationEntity(),
    88  		}, {
    89  			Name:     "Returns nil when given empty model",
    90  			Input:    nil,
    91  			Expected: nil,
    92  		},
    93  	}
    94  	for _, testCase := range testCases {
    95  		t.Run(testCase.Name, func(t *testing.T) {
    96  			// WHEN
    97  			result := converter.ToEntity(testCase.Input)
    98  
    99  			require.Equal(t, result, testCase.Expected)
   100  		})
   101  	}
   102  }
   103  
   104  func TestConverter_FromEntity(t *testing.T) {
   105  	testCases := []struct {
   106  		Name     string
   107  		Input    *formation.Entity
   108  		Expected *model.Formation
   109  	}{
   110  		{
   111  			Name:     "Success",
   112  			Input:    fixFormationEntity(),
   113  			Expected: fixFormationModel(),
   114  		}, {
   115  			Name:     "Empty",
   116  			Input:    nil,
   117  			Expected: nil,
   118  		},
   119  	}
   120  	for _, testCase := range testCases {
   121  		t.Run(testCase.Name, func(t *testing.T) {
   122  			// WHEN
   123  			result := converter.FromEntity(testCase.Input)
   124  
   125  			require.Equal(t, result, testCase.Expected)
   126  		})
   127  	}
   128  }
   129  
   130  func Test_converter_MultipleToGraphQL(t *testing.T) {
   131  	testCases := []struct {
   132  		Name               string
   133  		InputFormations    []*model.Formation
   134  		ExpectedFormations []*graphql.Formation
   135  		ExpectedError      error
   136  	}{
   137  		{
   138  			Name:               "Success",
   139  			InputFormations:    []*model.Formation{&modelFormation},
   140  			ExpectedFormations: []*graphql.Formation{&graphqlFormation},
   141  		},
   142  		{
   143  			Name:               "Success when input is nil",
   144  			InputFormations:    nil,
   145  			ExpectedFormations: nil,
   146  		},
   147  		{
   148  			Name:               "Success when one of the input formations is nil",
   149  			InputFormations:    []*model.Formation{nil, &modelFormation},
   150  			ExpectedFormations: []*graphql.Formation{&graphqlFormation},
   151  		},
   152  		{
   153  			Name:               "Returns error when can't convert one of input formations",
   154  			InputFormations:    []*model.Formation{&modelFormation, {Error: json.RawMessage(`{invalid}`)}},
   155  			ExpectedFormations: nil,
   156  			ExpectedError:      errors.New("while unmarshalling formation error"),
   157  		},
   158  	}
   159  
   160  	for _, testCase := range testCases {
   161  		t.Run(testCase.Name, func(t *testing.T) {
   162  			result, err := converter.MultipleToGraphQL(testCase.InputFormations)
   163  
   164  			if testCase.ExpectedError == nil {
   165  				require.NoError(t, err)
   166  				require.ElementsMatch(t, testCase.ExpectedFormations, result)
   167  			} else {
   168  				require.Error(t, err)
   169  				require.Contains(t, err.Error(), testCase.ExpectedError.Error())
   170  				require.Nil(t, result)
   171  			}
   172  		})
   173  	}
   174  }