github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/label/converter_test.go (about) 1 package label_test 2 3 import ( 4 "database/sql" 5 "encoding/json" 6 "testing" 7 8 "github.com/kyma-incubator/compass/components/director/internal/domain/label" 9 "github.com/kyma-incubator/compass/components/director/internal/model" 10 "github.com/stretchr/testify/assert" 11 "github.com/stretchr/testify/require" 12 ) 13 14 func TestConverter_ToEntity(t *testing.T) { 15 stringValue := "foo" 16 marshalledStringValue, err := json.Marshal(stringValue) 17 require.NoError(t, err) 18 19 arrayValue := []interface{}{"foo", "bar"} 20 marshalledArrayValue, err := json.Marshal(arrayValue) 21 require.NoError(t, err) 22 23 version := 0 24 25 // GIVEN 26 testCases := []struct { 27 Name string 28 Input *model.Label 29 Expected *label.Entity 30 ExpectedErrMessage string 31 }{ 32 { 33 Name: "All properties given", 34 Input: fixLabelModel("1", arrayValue, version), 35 Expected: fixLabelEntity("1", marshalledArrayValue, version), 36 ExpectedErrMessage: "", 37 }, 38 { 39 Name: "String value", 40 Input: fixLabelModel("1", stringValue, version), 41 Expected: fixLabelEntity("1", marshalledStringValue, version), 42 ExpectedErrMessage: "", 43 }, 44 { 45 Name: "Empty value", 46 Input: &model.Label{ 47 ID: "2", 48 Key: "foo", 49 Version: version, 50 }, 51 Expected: &label.Entity{ 52 ID: "2", 53 Key: "foo", 54 Version: version, 55 }, 56 ExpectedErrMessage: "", 57 }, 58 { 59 Name: "Error", 60 Input: &model.Label{ 61 Value: make(chan int), 62 }, 63 Expected: nil, 64 ExpectedErrMessage: "while marshalling Value: json: unsupported type: chan int", 65 }, 66 } 67 68 for _, testCase := range testCases { 69 t.Run(testCase.Name, func(t *testing.T) { 70 conv := label.NewConverter() 71 72 // WHEN 73 res, err := conv.ToEntity(testCase.Input) 74 if testCase.ExpectedErrMessage != "" { 75 require.Error(t, err) 76 assert.Equal(t, testCase.ExpectedErrMessage, err.Error()) 77 } 78 79 // then 80 assert.Equal(t, testCase.Expected, res) 81 }) 82 } 83 } 84 85 func TestConverter_FromEntity(t *testing.T) { 86 stringValue := "foo" 87 marshalledStringValue, err := json.Marshal(stringValue) 88 require.NoError(t, err) 89 90 arrayValue := []interface{}{"foo", "bar"} 91 marshalledArrayValue, err := json.Marshal(arrayValue) 92 require.NoError(t, err) 93 94 version := 0 95 96 // GIVEN 97 testCases := []struct { 98 Name string 99 Input *label.Entity 100 Expected *model.Label 101 ExpectedErrMessage string 102 }{ 103 { 104 Name: "All properties given", 105 Input: fixLabelEntity("1", marshalledArrayValue, version), 106 Expected: fixLabelModel("1", arrayValue, version), 107 ExpectedErrMessage: "", 108 }, 109 { 110 Name: "String value", 111 Input: fixLabelEntity("1", marshalledStringValue, version), 112 Expected: fixLabelModel("1", stringValue, version), 113 ExpectedErrMessage: "", 114 }, 115 { 116 Name: "Empty value", 117 Input: &label.Entity{ 118 ID: "2", 119 Key: "foo", 120 Version: version, 121 }, 122 Expected: &model.Label{ 123 ID: "2", 124 Key: "foo", 125 Version: version, 126 }, 127 ExpectedErrMessage: "", 128 }, 129 { 130 Name: "Error", 131 Input: fixLabelEntity("1", []byte("{json"), version), 132 Expected: nil, 133 ExpectedErrMessage: "while unmarshalling Value: invalid character 'j' looking for beginning of object key string", 134 }, 135 } 136 137 for _, testCase := range testCases { 138 t.Run(testCase.Name, func(t *testing.T) { 139 conv := label.NewConverter() 140 141 // WHEN 142 res, err := conv.FromEntity(testCase.Input) 143 if testCase.ExpectedErrMessage != "" { 144 require.Error(t, err) 145 assert.Equal(t, testCase.ExpectedErrMessage, err.Error()) 146 } 147 148 // then 149 assert.Equal(t, testCase.Expected, res) 150 }) 151 } 152 } 153 154 func fixLabelEntity(id string, value []byte, version int) *label.Entity { 155 return &label.Entity{ 156 ID: id, 157 AppID: sql.NullString{}, 158 RuntimeID: sql.NullString{ 159 String: "321", 160 Valid: true, 161 }, 162 TenantID: sql.NullString{}, 163 Key: "test", 164 Value: string(value), 165 Version: version, 166 } 167 } 168 func fixLabelModel(id string, value interface{}, version int) *model.Label { 169 return &model.Label{ 170 ID: id, 171 Key: "test", 172 ObjectType: model.RuntimeLabelableObject, 173 ObjectID: "321", 174 Value: value, 175 Version: version, 176 } 177 }