k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/validation/spec/info_test.go (about) 1 // Copyright 2015 go-swagger maintainers 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package spec 16 17 import ( 18 "encoding/json" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 "github.com/stretchr/testify/require" 23 jsontesting "k8s.io/kube-openapi/pkg/util/jsontesting" 24 ) 25 26 const infoJSON = `{ 27 "description": "A sample API that uses a petstore as an example to demonstrate features in ` + 28 `the swagger-2.0 specification", 29 "title": "Swagger Sample API", 30 "termsOfService": "http://helloreverb.com/terms/", 31 "contact": { 32 "name": "wordnik api team", 33 "url": "http://developer.wordnik.com" 34 }, 35 "license": { 36 "name": "Creative Commons 4.0 International", 37 "url": "http://creativecommons.org/licenses/by/4.0/" 38 }, 39 "version": "1.0.9-abcd", 40 "x-framework": "go-swagger" 41 }` 42 43 var info = Info{ 44 InfoProps: InfoProps{ 45 Version: "1.0.9-abcd", 46 Title: "Swagger Sample API", 47 Description: "A sample API that uses a petstore as an example to demonstrate features in " + 48 "the swagger-2.0 specification", 49 TermsOfService: "http://helloreverb.com/terms/", 50 Contact: &ContactInfo{Name: "wordnik api team", URL: "http://developer.wordnik.com"}, 51 License: &License{ 52 Name: "Creative Commons 4.0 International", 53 URL: "http://creativecommons.org/licenses/by/4.0/", 54 }, 55 }, 56 VendorExtensible: VendorExtensible{Extensions: map[string]interface{}{"x-framework": "go-swagger"}}, 57 } 58 59 func TestIntegrationInfo_Serialize(t *testing.T) { 60 b, err := json.MarshalIndent(info, "", "\t") 61 if assert.NoError(t, err) { 62 assert.Equal(t, infoJSON, string(b)) 63 } 64 } 65 66 func TestIntegrationInfo_Deserialize(t *testing.T) { 67 actual := Info{} 68 err := json.Unmarshal([]byte(infoJSON), &actual) 69 if assert.NoError(t, err) { 70 assert.EqualValues(t, info, actual) 71 } 72 } 73 74 func TestInfoRoundTrip(t *testing.T) { 75 cases := []jsontesting.RoundTripTestCase{ 76 { 77 // Show at least one field from each embededd struct sitll allows 78 // roundtrips successfully 79 Name: "UnmarshalEmbedded", 80 JSON: `{ 81 "x-framework": "swagger-go", 82 "description": "the description of this object" 83 }`, 84 Object: &Info{ 85 VendorExtensible{Extensions{ 86 "x-framework": "swagger-go", 87 }}, 88 InfoProps{ 89 Description: "the description of this object", 90 }, 91 }, 92 }, { 93 Name: "BasicCase", 94 JSON: infoJSON, 95 Object: &info, 96 }, 97 } 98 99 for _, tcase := range cases { 100 t.Run(tcase.Name, func(t *testing.T) { 101 require.NoError(t, tcase.RoundTripTest(&Info{})) 102 }) 103 } 104 }