github.com/kyma-incubator/compass/components/director@v0.0.0-20230623144113-d764f56ff805/pkg/graphql/json_schema_test.go (about) 1 package graphql 2 3 import ( 4 "bytes" 5 "errors" 6 "testing" 7 8 "github.com/stretchr/testify/assert" 9 "github.com/stretchr/testify/require" 10 ) 11 12 func TestUnmarshalGQLJSON(t *testing.T) { 13 for name, tc := range map[string]struct { 14 input interface{} 15 err bool 16 errmsg string 17 expected JSONSchema 18 }{ 19 //given 20 "correct input": { 21 input: `{"schema":"schema"}`, 22 err: false, 23 expected: JSONSchema(`{"schema":"schema"}`), 24 }, 25 "error: input is nil": { 26 input: nil, 27 err: true, 28 errmsg: "Invalid data [reason=input should not be nil]", 29 }, 30 "error: invalid input": { 31 input: 123, 32 err: true, 33 errmsg: "unexpected input type: int, should be string", 34 }, 35 } { 36 t.Run(name, func(t *testing.T) { 37 // WHEN 38 var j JSONSchema 39 err := j.UnmarshalGQL(tc.input) 40 41 // THEN 42 if tc.err { 43 assert.Error(t, err) 44 assert.EqualError(t, err, tc.errmsg) 45 assert.Empty(t, j) 46 } else { 47 assert.NoError(t, err) 48 assert.Equal(t, tc.expected, j) 49 } 50 }) 51 } 52 } 53 54 func TestMarshalGQLJSON(t *testing.T) { 55 //given 56 fixJSON := JSONSchema("schema") 57 expectedJSON := `"schema"` 58 buf := bytes.Buffer{} 59 60 // WHEN 61 fixJSON.MarshalGQL(&buf) 62 63 // THEN 64 assert.NotNil(t, buf) 65 assert.Equal(t, expectedJSON, buf.String()) 66 } 67 68 func TestJSON_MarshalSchema(t *testing.T) { 69 testCases := []struct { 70 Name string 71 InputSchema *interface{} 72 Expected *JSONSchema 73 ExpectedErr error 74 }{ 75 { 76 Name: "Success", 77 InputSchema: interfacePtr(map[string]interface{}{"annotation": []string{"val1", "val2"}}), 78 Expected: jsonPtr(JSONSchema(`{"annotation":["val1","val2"]}`)), 79 ExpectedErr: nil, 80 }, 81 { 82 Name: "Success nil input", 83 InputSchema: nil, 84 Expected: nil, 85 ExpectedErr: nil, 86 }, 87 } 88 89 for _, testCase := range testCases { 90 t.Run(testCase.Name, func(t *testing.T) { 91 // WHEN 92 json, err := MarshalSchema(testCase.InputSchema) 93 94 // THEN 95 if testCase.ExpectedErr != nil { 96 require.Error(t, err) 97 assert.EqualError(t, err, testCase.ExpectedErr.Error()) 98 } else { 99 require.NoError(t, err) 100 } 101 assert.Equal(t, testCase.Expected, json) 102 }) 103 } 104 } 105 106 func TestJSON_UnmarshalSchema(t *testing.T) { 107 t.Run("Success nil JSON", func(t *testing.T) { 108 //GIVEN 109 var json *JSONSchema 110 var expected *interface{} 111 // WHEN 112 output, err := json.Unmarshal() 113 // THEN 114 require.NoError(t, err) 115 assert.Equal(t, expected, output) 116 }) 117 118 t.Run("Success correct schema", func(t *testing.T) { 119 //GIVEN 120 input := jsonPtr(`{"annotation":["val1","val2"]}`) 121 expected := map[string]interface{}{"annotation": []interface{}{"val1", "val2"}} 122 // WHEN 123 output, err := input.Unmarshal() 124 // THEN 125 require.NoError(t, err) 126 assert.Equal(t, expected, *output) 127 }) 128 129 t.Run("Error - not correct schema", func(t *testing.T) { 130 //GIVEN 131 expectedErr := errors.New("invalid character 'b' looking for beginning of value") 132 133 input := jsonPtr(`blblbl"`) 134 // WHEN 135 output, err := input.Unmarshal() 136 // THEN 137 require.Error(t, err) 138 assert.EqualError(t, err, expectedErr.Error()) 139 assert.Nil(t, output) 140 }) 141 } 142 143 func interfacePtr(input interface{}) *interface{} { 144 var tmp = input 145 return &tmp 146 } 147 148 func jsonPtr(json JSONSchema) *JSONSchema { 149 return &json 150 }