k8s.io/kube-openapi@v0.0.0-20240228011516-70dd3763d340/pkg/spec3/operation_test.go (about)

     1  /*
     2  Copyright 2021 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 spec3_test
    18  
    19  import (
    20  	"encoding/json"
    21  	"testing"
    22  
    23  	"github.com/stretchr/testify/require"
    24  	"k8s.io/kube-openapi/pkg/spec3"
    25  	"k8s.io/kube-openapi/pkg/util/jsontesting"
    26  	"k8s.io/kube-openapi/pkg/validation/spec"
    27  )
    28  
    29  func TestOperationRoundTrip(t *testing.T) {
    30  	cases := []jsontesting.RoundTripTestCase{
    31  		{
    32  			Name: "Basic Roundtrip",
    33  			Object: &spec3.Operation{
    34  				spec3.OperationProps{
    35  					Description: "foo",
    36  				},
    37  				spec.VendorExtensible{Extensions: spec.Extensions{
    38  					"x-framework": "go-swagger",
    39  				}},
    40  			},
    41  		},
    42  	}
    43  
    44  	for _, tcase := range cases {
    45  		t.Run(tcase.Name, func(t *testing.T) {
    46  			require.NoError(t, tcase.RoundTripTest(&spec3.Operation{}))
    47  		})
    48  	}
    49  }
    50  
    51  func TestOperationJSONSerialization(t *testing.T) {
    52  	cases := []struct {
    53  		name           string
    54  		target         *spec3.Operation
    55  		expectedOutput string
    56  	}{
    57  		{
    58  			name: "basic",
    59  			target: &spec3.Operation{
    60  				OperationProps: spec3.OperationProps{
    61  					Tags:        []string{"pet"},
    62  					Summary:     "Updates a pet in the store with form data",
    63  					OperationId: "updatePetWithForm",
    64  					Parameters: []*spec3.Parameter{
    65  						{
    66  							ParameterProps: spec3.ParameterProps{
    67  								Name:        "petId",
    68  								In:          "path",
    69  								Description: "ID of pet that needs to be updated",
    70  								Required:    true,
    71  								Schema: &spec.Schema{
    72  									SchemaProps: spec.SchemaProps{
    73  										Type: []string{"string"},
    74  									},
    75  								},
    76  							},
    77  						},
    78  					},
    79  					RequestBody: &spec3.RequestBody{
    80  						RequestBodyProps: spec3.RequestBodyProps{
    81  							Content: map[string]*spec3.MediaType{
    82  								"application/x-www-form-urlencoded": {
    83  									MediaTypeProps: spec3.MediaTypeProps{
    84  										Schema: &spec.Schema{
    85  											SchemaProps: spec.SchemaProps{
    86  												Type: []string{"object"},
    87  												Properties: map[string]spec.Schema{
    88  													"name": {
    89  														SchemaProps: spec.SchemaProps{
    90  															Description: "Updated name of the pet",
    91  															Type:        []string{"string"},
    92  														},
    93  													},
    94  													"status": {
    95  														SchemaProps: spec.SchemaProps{
    96  															Description: "Updated status of the pet",
    97  															Type:        []string{"string"},
    98  														},
    99  													},
   100  												},
   101  											},
   102  										},
   103  									},
   104  								},
   105  							},
   106  						},
   107  					},
   108  					Responses: &spec3.Responses{
   109  						ResponsesProps: spec3.ResponsesProps{
   110  							StatusCodeResponses: map[int]*spec3.Response{
   111  								200: {
   112  									ResponseProps: spec3.ResponseProps{
   113  										Description: "Pet updated.",
   114  										Content: map[string]*spec3.MediaType{
   115  											"application/json": {},
   116  											"application/xml":  {},
   117  										},
   118  									},
   119  								},
   120  							},
   121  						},
   122  					},
   123  				},
   124  			},
   125  			expectedOutput: `{"tags":["pet"],"summary":"Updates a pet in the store with form data","operationId":"updatePetWithForm","parameters":[{"name":"petId","in":"path","description":"ID of pet that needs to be updated","required":true,"schema":{"type":"string"}}],"requestBody":{"content":{"application/x-www-form-urlencoded":{"schema":{"type":"object","properties":{"name":{"description":"Updated name of the pet","type":"string"},"status":{"description":"Updated status of the pet","type":"string"}}}}}},"responses":{"200":{"description":"Pet updated.","content":{"application/json":{},"application/xml":{}}}}}`,
   126  		},
   127  	}
   128  	for _, tc := range cases {
   129  		t.Run(tc.name, func(t *testing.T) {
   130  			rawTarget, err := json.Marshal(tc.target)
   131  			if err != nil {
   132  				t.Fatal(err)
   133  			}
   134  			serializedTarget := string(rawTarget)
   135  			if err := jsontesting.JsonCompare([]byte(tc.expectedOutput), []byte(serializedTarget)); err != nil {
   136  				t.Fatalf("diff %s", err)
   137  			}
   138  		})
   139  	}
   140  }