| 1 | /* |
| 2 | Copyright 2023 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package internal |
| 18 | |
| 19 | import ( |
| 20 | "github.com/go-openapi/jsonreference" |
| 21 | "reflect" |
| 22 | "testing" |
| 23 | ) |
| 24 | |
| 25 | func TestJSONRefFromMap(t *testing.T) { |
| 26 | testcases := []struct { |
| 27 | name string |
| 28 | fromMap map[string]interface{} |
| 29 | expectNil bool |
| 30 | expectRefString string |
| 31 | }{ |
| 32 | { |
| 33 | name: "nil map", |
| 34 | expectRefString: "", |
| 35 | }, { |
| 36 | name: "map with no $ref", |
| 37 | fromMap: map[string]interface{}{"a": "b"}, |
| 38 | expectRefString: "", |
| 39 | }, { |
| 40 | name: "ref path", |
| 41 | fromMap: map[string]interface{}{"$ref": "#/path"}, |
| 42 | expectRefString: "#/path", |
| 43 | }, |
| 44 | } |
| 45 | |
| 46 | for _, tc := range testcases { |
| 47 | var tmp jsonreference.Ref |
| 48 | err := JSONRefFromMap(&tmp, tc.fromMap) |
| 49 | if err != nil { |
| 50 | t.Errorf("Expect no error from JSONRefMap %s, got error %v", tc.name, err) |
| 51 | } |
| 52 | |
| 53 | if tmp.String() != tc.expectRefString { |
| 54 | t.Errorf("Expect jsonRef to be %s, got %s for %s", tc.expectRefString, tmp.String(), tc.name) |
| 55 | } |
| 56 | |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | func TestSanitizeExtensions(t *testing.T) { |
| 61 | testcases := []struct { |
| 62 | in map[string]interface{} |
| 63 | expected map[string]interface{} |
| 64 | }{ |
| 65 | { |
| 66 | in: map[string]interface{}{"a": "b", "x-extension": "foo"}, |
| 67 | expected: map[string]interface{}{"x-extension": "foo"}, |
| 68 | }, |
| 69 | { |
| 70 | in: map[string]interface{}{"a": "b"}, |
| 71 | expected: nil, |
| 72 | }, |
| 73 | { |
| 74 | in: map[string]interface{}{}, |
| 75 | expected: nil, |
| 76 | }, |
| 77 | } |
| 78 | |
| 79 | for _, tc := range testcases { |
| 80 | e := SanitizeExtensions(tc.in) |
| 81 | if !reflect.DeepEqual(tc.expected, e) { |
| 82 | t.Errorf("Error: sanitize extensions does not match expected") |
| 83 | } |
| 84 | } |
| 85 | } |