github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/runtime_context/converter_test.go (about) 1 package runtimectx_test 2 3 import ( 4 "testing" 5 6 runtimectx "github.com/kyma-incubator/compass/components/director/internal/domain/runtime_context" 7 8 "github.com/kyma-incubator/compass/components/director/internal/model" 9 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 10 "github.com/stretchr/testify/assert" 11 ) 12 13 func TestConverter_ToGraphQL(t *testing.T) { 14 id := "test_id" 15 key := "key" 16 val := "val" 17 // GIVEN 18 testCases := []struct { 19 Name string 20 Input *model.RuntimeContext 21 Expected *graphql.RuntimeContext 22 }{ 23 { 24 Name: "All properties given", 25 Input: &model.RuntimeContext{ 26 ID: id, 27 RuntimeID: runtimeID, 28 Key: key, 29 Value: val, 30 }, 31 Expected: &graphql.RuntimeContext{ 32 ID: id, 33 Key: key, 34 Value: val, 35 }, 36 }, 37 { 38 Name: "Empty", 39 Input: &model.RuntimeContext{}, 40 Expected: &graphql.RuntimeContext{}, 41 }, 42 { 43 Name: "Nil", 44 Input: nil, 45 Expected: nil, 46 }, 47 } 48 49 for _, testCase := range testCases { 50 t.Run(testCase.Name, func(t *testing.T) { 51 // WHEN 52 converter := runtimectx.NewConverter() 53 res := converter.ToGraphQL(testCase.Input) 54 55 // THEN 56 assert.Equal(t, testCase.Expected, res) 57 }) 58 } 59 } 60 61 func TestConverter_MultipleToGraphQL(t *testing.T) { 62 id := "test_id" 63 runtimeID := "test_runtime_id" 64 key := "key" 65 val := "val" 66 67 // GIVEN 68 input := []*model.RuntimeContext{ 69 { 70 ID: id, 71 RuntimeID: runtimeID, 72 Key: key, 73 Value: val, 74 }, 75 { 76 ID: id + "2", 77 RuntimeID: runtimeID + "2", 78 Key: key + "2", 79 Value: val + "2", 80 }, 81 nil, 82 } 83 expected := []*graphql.RuntimeContext{ 84 { 85 ID: id, 86 Key: key, 87 Value: val, 88 }, 89 { 90 ID: id + "2", 91 Key: key + "2", 92 Value: val + "2", 93 }, 94 } 95 96 // WHEN 97 converter := runtimectx.NewConverter() 98 res := converter.MultipleToGraphQL(input) 99 100 // THEN 101 assert.Equal(t, expected, res) 102 } 103 104 func TestConverter_InputFromGraphQL(t *testing.T) { 105 key := "key" 106 val := "val" 107 108 // GIVEN 109 testCases := []struct { 110 Name string 111 Input graphql.RuntimeContextInput 112 Expected model.RuntimeContextInput 113 }{ 114 { 115 Name: "All properties given", 116 Input: graphql.RuntimeContextInput{ 117 Key: key, 118 Value: val, 119 }, 120 Expected: model.RuntimeContextInput{ 121 Key: key, 122 Value: val, 123 }, 124 }, 125 { 126 Name: "Empty", 127 Input: graphql.RuntimeContextInput{}, 128 Expected: model.RuntimeContextInput{}, 129 }, 130 } 131 132 for _, testCase := range testCases { 133 t.Run(testCase.Name, func(t *testing.T) { 134 // WHEN 135 converter := runtimectx.NewConverter() 136 res := converter.InputFromGraphQL(testCase.Input) 137 138 // THEN 139 assert.Equal(t, testCase.Expected, res) 140 }) 141 } 142 } 143 144 func TestConverter_InputFromGraphQLWithRuntimeID(t *testing.T) { 145 key := "key" 146 val := "val" 147 runtimeID := "runtime_id" 148 149 // GIVEN 150 testCases := []struct { 151 Name string 152 Input graphql.RuntimeContextInput 153 Expected model.RuntimeContextInput 154 }{ 155 { 156 Name: "All properties given", 157 Input: graphql.RuntimeContextInput{ 158 Key: key, 159 Value: val, 160 }, 161 Expected: model.RuntimeContextInput{ 162 Key: key, 163 Value: val, 164 RuntimeID: runtimeID, 165 }, 166 }, 167 { 168 Name: "Empty", 169 Input: graphql.RuntimeContextInput{}, 170 Expected: model.RuntimeContextInput{ 171 RuntimeID: runtimeID, 172 }, 173 }, 174 } 175 176 for _, testCase := range testCases { 177 t.Run(testCase.Name, func(t *testing.T) { 178 // WHEN 179 converter := runtimectx.NewConverter() 180 res := converter.InputFromGraphQLWithRuntimeID(testCase.Input, runtimeID) 181 182 // THEN 183 assert.Equal(t, testCase.Expected, res) 184 }) 185 } 186 } 187 188 func TestConverter_EntityFromRuntimeModel(t *testing.T) { 189 // GIVEN 190 modelRuntimeCtx := model.RuntimeContext{ 191 ID: "id", 192 RuntimeID: "runtime_id", 193 Key: "key", 194 Value: "value", 195 } 196 197 conv := runtimectx.NewConverter() 198 // WHEN 199 entityRuntimeCtx := conv.ToEntity(&modelRuntimeCtx) 200 201 // THEN 202 assert.Equal(t, modelRuntimeCtx.ID, entityRuntimeCtx.ID) 203 assert.Equal(t, modelRuntimeCtx.RuntimeID, entityRuntimeCtx.RuntimeID) 204 assert.Equal(t, modelRuntimeCtx.Key, entityRuntimeCtx.Key) 205 assert.Equal(t, modelRuntimeCtx.Value, entityRuntimeCtx.Value) 206 } 207 208 func TestConverter_RuntimeContextToModel(t *testing.T) { 209 // GIVEN 210 entityRuntimeCtx := &runtimectx.RuntimeContext{ 211 ID: "id", 212 RuntimeID: "runtime_id", 213 Key: "key", 214 Value: "value", 215 } 216 217 conv := runtimectx.NewConverter() 218 // WHEN 219 modelRuntimeCtx := conv.FromEntity(entityRuntimeCtx) 220 221 // THEN 222 assert.Equal(t, entityRuntimeCtx.ID, modelRuntimeCtx.ID) 223 assert.Equal(t, entityRuntimeCtx.RuntimeID, modelRuntimeCtx.RuntimeID) 224 assert.Equal(t, entityRuntimeCtx.Key, modelRuntimeCtx.Key) 225 assert.Equal(t, entityRuntimeCtx.Value, modelRuntimeCtx.Value) 226 }