github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/internal/domain/spec/converter_test.go (about) 1 package spec_test 2 3 import ( 4 "testing" 5 6 "github.com/kyma-incubator/compass/components/director/internal/domain/spec" 7 "github.com/kyma-incubator/compass/components/director/internal/domain/spec/automock" 8 "github.com/kyma-incubator/compass/components/director/internal/model" 9 "github.com/kyma-incubator/compass/components/director/pkg/graphql" 10 "github.com/pkg/errors" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestConverter_ToGraphQLAPISpec(t *testing.T) { 16 // GIVEN 17 testCases := []struct { 18 Name string 19 Input *model.Spec 20 Expected *graphql.APISpec 21 ExpectErr bool 22 }{ 23 { 24 Name: "All properties given", 25 Input: fixModelAPISpec(), 26 Expected: fixGQLAPISpec(), 27 }, 28 { 29 Name: "Referenced ObjectType is not API should return error", 30 Input: fixModelEventSpec(), 31 ExpectErr: true, 32 }, 33 { 34 Name: "APIType is nil should return error", 35 Input: &model.Spec{ 36 ObjectType: model.APISpecReference, 37 }, 38 ExpectErr: true, 39 }, 40 { 41 Name: "Nil", 42 Input: nil, 43 Expected: nil, 44 }, 45 } 46 47 for _, testCase := range testCases { 48 t.Run(testCase.Name, func(t *testing.T) { 49 converter := spec.NewConverter(&automock.FetchRequestConverter{}) 50 51 // WHEN 52 res, err := converter.ToGraphQLAPISpec(testCase.Input) 53 54 // then 55 if testCase.ExpectErr { 56 assert.Error(t, err) 57 } else { 58 assert.NoError(t, err) 59 } 60 assert.Equal(t, testCase.Expected, res) 61 }) 62 } 63 } 64 65 func TestConverter_ToGraphQLEventSpec(t *testing.T) { 66 // GIVEN 67 testCases := []struct { 68 Name string 69 Input *model.Spec 70 Expected *graphql.EventSpec 71 ExpectErr bool 72 }{ 73 { 74 Name: "All properties given", 75 Input: fixModelEventSpec(), 76 Expected: fixGQLEventSpec(), 77 }, 78 { 79 Name: "Referenced ObjectType is not Event should return error", 80 Input: fixModelAPISpec(), 81 ExpectErr: true, 82 }, 83 { 84 Name: "EventType is nil should return error", 85 Input: &model.Spec{ 86 ObjectType: model.EventSpecReference, 87 }, 88 ExpectErr: true, 89 }, 90 { 91 Name: "Nil", 92 Input: nil, 93 Expected: nil, 94 }, 95 } 96 97 for _, testCase := range testCases { 98 t.Run(testCase.Name, func(t *testing.T) { 99 converter := spec.NewConverter(&automock.FetchRequestConverter{}) 100 101 // WHEN 102 res, err := converter.ToGraphQLEventSpec(testCase.Input) 103 104 // then 105 if testCase.ExpectErr { 106 assert.Error(t, err) 107 } else { 108 assert.NoError(t, err) 109 } 110 assert.Equal(t, testCase.Expected, res) 111 }) 112 } 113 } 114 115 func TestConverter_InputFromGraphQLAPISpec(t *testing.T) { 116 testErr := errors.New("test") 117 118 // GIVEN 119 testCases := []struct { 120 Name string 121 FetchRequestConvFn func() *automock.FetchRequestConverter 122 Input *graphql.APISpecInput 123 Expected *model.SpecInput 124 ExpectedErr error 125 }{ 126 { 127 Name: "All properties given", 128 Input: fixGQLAPISpecInputWithFetchRequest(), 129 FetchRequestConvFn: func() *automock.FetchRequestConverter { 130 conv := &automock.FetchRequestConverter{} 131 conv.On("InputFromGraphQL", fixGQLAPISpecInputWithFetchRequest().FetchRequest).Return(fixModelAPISpecInputWithFetchRequest().FetchRequest, nil).Once() 132 return conv 133 }, 134 Expected: fixModelAPISpecInputWithFetchRequest(), 135 }, 136 { 137 Name: "Return error when FetchRequest conversion fails", 138 Input: fixGQLAPISpecInputWithFetchRequest(), 139 FetchRequestConvFn: func() *automock.FetchRequestConverter { 140 conv := &automock.FetchRequestConverter{} 141 conv.On("InputFromGraphQL", fixGQLAPISpecInputWithFetchRequest().FetchRequest).Return(nil, testErr).Once() 142 return conv 143 }, 144 ExpectedErr: testErr, 145 }, 146 { 147 Name: "Nil", 148 Input: nil, 149 FetchRequestConvFn: func() *automock.FetchRequestConverter { 150 return &automock.FetchRequestConverter{} 151 }, 152 Expected: nil, 153 }, 154 } 155 156 for _, testCase := range testCases { 157 t.Run(testCase.Name, func(t *testing.T) { 158 frConv := testCase.FetchRequestConvFn() 159 converter := spec.NewConverter(frConv) 160 161 // WHEN 162 res, err := converter.InputFromGraphQLAPISpec(testCase.Input) 163 164 // then 165 // then 166 if testCase.ExpectedErr != nil { 167 assert.Error(t, err) 168 assert.Contains(t, err.Error(), testCase.ExpectedErr.Error()) 169 } else { 170 assert.NoError(t, err) 171 } 172 assert.Equal(t, testCase.Expected, res) 173 frConv.AssertExpectations(t) 174 }) 175 } 176 } 177 178 func TestConverter_InputFromGraphQLEventSpec(t *testing.T) { 179 testErr := errors.New("test") 180 181 // GIVEN 182 testCases := []struct { 183 Name string 184 FetchRequestConvFn func() *automock.FetchRequestConverter 185 Input *graphql.EventSpecInput 186 Expected *model.SpecInput 187 ExpectedErr error 188 }{ 189 { 190 Name: "All properties given", 191 Input: fixGQLEventSpecInputWithFetchRequest(), 192 FetchRequestConvFn: func() *automock.FetchRequestConverter { 193 conv := &automock.FetchRequestConverter{} 194 conv.On("InputFromGraphQL", fixGQLEventSpecInputWithFetchRequest().FetchRequest).Return(fixModelEventSpecInputWithFetchRequest().FetchRequest, nil).Once() 195 return conv 196 }, 197 Expected: fixModelEventSpecInputWithFetchRequest(), 198 }, 199 { 200 Name: "Return error when FetchRequest conversion fails", 201 Input: fixGQLEventSpecInputWithFetchRequest(), 202 FetchRequestConvFn: func() *automock.FetchRequestConverter { 203 conv := &automock.FetchRequestConverter{} 204 conv.On("InputFromGraphQL", fixGQLEventSpecInputWithFetchRequest().FetchRequest).Return(nil, testErr).Once() 205 return conv 206 }, 207 ExpectedErr: testErr, 208 }, 209 { 210 Name: "Nil", 211 Input: nil, 212 FetchRequestConvFn: func() *automock.FetchRequestConverter { 213 return &automock.FetchRequestConverter{} 214 }, 215 Expected: nil, 216 }, 217 } 218 219 for _, testCase := range testCases { 220 t.Run(testCase.Name, func(t *testing.T) { 221 frConv := testCase.FetchRequestConvFn() 222 converter := spec.NewConverter(frConv) 223 224 // WHEN 225 res, err := converter.InputFromGraphQLEventSpec(testCase.Input) 226 227 // then 228 // then 229 if testCase.ExpectedErr != nil { 230 assert.Error(t, err) 231 assert.Contains(t, err.Error(), testCase.ExpectedErr.Error()) 232 } else { 233 assert.NoError(t, err) 234 } 235 assert.Equal(t, testCase.Expected, res) 236 frConv.AssertExpectations(t) 237 }) 238 } 239 } 240 241 func TestConverter_FromEntity(t *testing.T) { 242 // GIVEN 243 testCases := []struct { 244 Name string 245 Input *spec.Entity 246 Expected *model.Spec 247 ExpectedErrMessage string 248 }{ 249 { 250 Name: "All properties given for API", 251 Input: fixAPISpecEntity(), 252 Expected: fixModelAPISpec(), 253 ExpectedErrMessage: "", 254 }, 255 { 256 Name: "All properties given for Event", 257 Input: fixEventSpecEntity(), 258 Expected: fixModelEventSpec(), 259 ExpectedErrMessage: "", 260 }, 261 { 262 Name: "Error when no reference entity", 263 Input: &spec.Entity{ 264 ID: "2", 265 }, 266 ExpectedErrMessage: "while determining object reference: incorrect Object Reference ID and its type for Entity with ID '2'", 267 }, 268 } 269 270 for _, testCase := range testCases { 271 t.Run(testCase.Name, func(t *testing.T) { 272 conv := spec.NewConverter(&automock.FetchRequestConverter{}) 273 274 // WHEN 275 res, err := conv.FromEntity(testCase.Input) 276 277 if testCase.ExpectedErrMessage != "" { 278 require.Error(t, err) 279 assert.Equal(t, testCase.ExpectedErrMessage, err.Error()) 280 return 281 } 282 283 // then 284 require.NoError(t, err) 285 assert.Equal(t, testCase.Expected, res) 286 }) 287 } 288 } 289 290 func TestConverter_ToEntity(t *testing.T) { 291 // GIVEN 292 testCases := []struct { 293 Name string 294 Input *model.Spec 295 Expected *spec.Entity 296 }{ 297 { 298 Name: "All properties given for API", 299 Input: fixModelAPISpec(), 300 Expected: fixAPISpecEntity(), 301 }, 302 { 303 Name: "All properties given for Event", 304 Input: fixModelEventSpec(), 305 Expected: fixEventSpecEntity(), 306 }, 307 } 308 309 for _, testCase := range testCases { 310 t.Run(testCase.Name, func(t *testing.T) { 311 conv := spec.NewConverter(&automock.FetchRequestConverter{}) 312 313 // WHEN 314 res := conv.ToEntity(testCase.Input) 315 316 // then 317 assert.Equal(t, testCase.Expected, res) 318 }) 319 } 320 }