Debian Codegraph @

pkg/validation/spec/operation_test.go

raw
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 var operation = Operation{
27 VendorExtensible: VendorExtensible{
28 Extensions: map[string]interface{}{
29 "x-framework": "go-swagger",
30 },
31 },
32 OperationProps: OperationProps{
33 Description: "operation description",
34 Consumes: []string{"application/json", "application/x-yaml"},
35 Produces: []string{"application/json", "application/x-yaml"},
36 Schemes: []string{"http", "https"},
37 Tags: []string{"dogs"},
38 Summary: "the summary of the operation",
39 ID: "sendCat",
40 Deprecated: true,
41 Security: []map[string][]string{
42 {
43 "apiKey": {},
44 },
45 },
46 Parameters: []Parameter{
47 {Refable: Refable{Ref: MustCreateRef("Cat")}},
48 },
49 Responses: &Responses{
50 ResponsesProps: ResponsesProps{
51 Default: &Response{
52 ResponseProps: ResponseProps{
53 Description: "void response",
54 },
55 },
56 },
57 },
58 },
59 }
60
61 const operationJSON = `{
62 "description": "operation description",
63 "x-framework": "go-swagger",
64 "consumes": [ "application/json", "application/x-yaml" ],
65 "produces": [ "application/json", "application/x-yaml" ],
66 "schemes": ["http", "https"],
67 "tags": ["dogs"],
68 "summary": "the summary of the operation",
69 "operationId": "sendCat",
70 "deprecated": true,
71 "security": [ { "apiKey": [] } ],
72 "parameters": [{"$ref":"Cat"}],
73 "responses": {
74 "default": {
75 "description": "void response"
76 }
77 }
78 }`
79
80 func TestIntegrationOperation(t *testing.T) {
81 var actual Operation
82 if assert.NoError(t, json.Unmarshal([]byte(operationJSON), &actual)) {
83 assert.EqualValues(t, actual, operation)
84 }
85
86 assertParsesJSON(t, operationJSON, operation)
87 }
88
89 func TestOperationRoundtrip(t *testing.T) {
90 cases := []jsontesting.RoundTripTestCase{
91 {
92 // Show at least one field from each embededd struct sitll allows
93 // roundtrips successfully
94 Name: "UnmarshalEmbedded",
95 JSON: `{
96 "description": "a cool description",
97 "x-framework": "swagger-go"
98 }`,
99 Object: &Operation{
100 VendorExtensible{Extensions{
101 "x-framework": "swagger-go",
102 }},
103 OperationProps{
104 Description: "a cool description",
105 },
106 },
107 }, {
108 Name: "BasicCase",
109 JSON: operationJSON,
110 Object: &operation,
111 },
112 }
113
114 for _, tcase := range cases {
115 t.Run(tcase.Name, func(t *testing.T) {
116 require.NoError(t, tcase.RoundTripTest(&Operation{}))
117 })
118 }
119 }