|
| 1 | +// Copyright 2025 MongoDB Inc |
| 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 filter |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/getkin/kin-openapi/openapi3" |
| 21 | + "github.com/mongodb/openapi/tools/cli/internal/apiversion" |
| 22 | + "github.com/stretchr/testify/assert" |
| 23 | + "github.com/stretchr/testify/require" |
| 24 | +) |
| 25 | + |
| 26 | +func TestBumpFilter_Apply_Preview(t *testing.T) { |
| 27 | + targetVersion, err := apiversion.New(apiversion.WithVersion("preview")) |
| 28 | + require.NoError(t, err) |
| 29 | + |
| 30 | + oas := &openapi3.T{ |
| 31 | + OpenAPI: "3.0.0", |
| 32 | + Info: &openapi3.Info{ |
| 33 | + Version: "1.0", |
| 34 | + }, |
| 35 | + Paths: openapi3.NewPaths(openapi3.WithPath("test", &openapi3.PathItem{ |
| 36 | + Get: &openapi3.Operation{ |
| 37 | + OperationID: "testOperationID", |
| 38 | + Summary: "testSummary", |
| 39 | + Responses: openapi3.NewResponses(openapi3.WithName("200", &openapi3.Response{ |
| 40 | + Content: openapi3.Content{ |
| 41 | + "application/vnd.atlas.preview+json": { |
| 42 | + Schema: &openapi3.SchemaRef{ |
| 43 | + Ref: "#/components/schemas/PaginatedAppUserView", |
| 44 | + }, |
| 45 | + Extensions: map[string]any{ |
| 46 | + "x-gen-version": "preview", |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + })), |
| 51 | + }, |
| 52 | + Extensions: map[string]any{}, |
| 53 | + })), |
| 54 | + } |
| 55 | + |
| 56 | + filter := &BumpFilter{ |
| 57 | + metadata: NewMetadata(targetVersion, "test"), |
| 58 | + oas: oas, |
| 59 | + } |
| 60 | + |
| 61 | + require.NoError(t, filter.Apply()) |
| 62 | + assert.Len(t, oas.Paths.Map(), 1) |
| 63 | + assert.Contains(t, oas.Paths.Map(), "test") |
| 64 | + |
| 65 | + testPath := oas.Paths.Map()["test"] |
| 66 | + assert.NotNil(t, testPath.Get) |
| 67 | + |
| 68 | + op := testPath.Get |
| 69 | + assert.Contains(t, op.Extensions, "x-state") |
| 70 | + assert.Equal(t, "Preview", op.Extensions["x-state"]) |
| 71 | + assert.Contains(t, op.Extensions, "x-beta") |
| 72 | + assert.Equal(t, true, op.Extensions["x-beta"]) |
| 73 | +} |
| 74 | + |
| 75 | +func TestBumpFilter_Apply_Upcoming(t *testing.T) { |
| 76 | + targetVersion, err := apiversion.New(apiversion.WithVersion("2024-09-22.upcoming")) |
| 77 | + require.NoError(t, err) |
| 78 | + |
| 79 | + oas := &openapi3.T{ |
| 80 | + OpenAPI: "3.0.0", |
| 81 | + Info: &openapi3.Info{ |
| 82 | + Version: "1.0", |
| 83 | + }, |
| 84 | + Paths: openapi3.NewPaths(openapi3.WithPath("test", &openapi3.PathItem{ |
| 85 | + Get: &openapi3.Operation{ |
| 86 | + OperationID: "testOperationID", |
| 87 | + Summary: "testSummary", |
| 88 | + Responses: openapi3.NewResponses(openapi3.WithName("200", &openapi3.Response{ |
| 89 | + Content: openapi3.Content{ |
| 90 | + "application/vnd.atlas.2024-09-22.upcoming+json": { |
| 91 | + Schema: &openapi3.SchemaRef{ |
| 92 | + Ref: "#/components/schemas/PaginatedAppUserView", |
| 93 | + }, |
| 94 | + }, |
| 95 | + }, |
| 96 | + })), |
| 97 | + }, |
| 98 | + Extensions: map[string]any{}, |
| 99 | + })), |
| 100 | + } |
| 101 | + |
| 102 | + filter := &BumpFilter{ |
| 103 | + metadata: NewMetadata(targetVersion, "test"), |
| 104 | + oas: oas, |
| 105 | + } |
| 106 | + |
| 107 | + require.NoError(t, filter.Apply()) |
| 108 | + assert.Len(t, oas.Paths.Map(), 1) |
| 109 | + assert.Contains(t, oas.Paths.Map(), "test") |
| 110 | + |
| 111 | + testPath := oas.Paths.Map()["test"] |
| 112 | + assert.NotNil(t, testPath.Get) |
| 113 | + op := testPath.Get |
| 114 | + |
| 115 | + assert.Contains(t, op.Extensions, "x-state") |
| 116 | + assert.Equal(t, "Upcoming", op.Extensions["x-state"]) |
| 117 | + assert.NotContains(t, op.Extensions, "x-beta") |
| 118 | +} |
| 119 | + |
| 120 | +func TestBumpFilter_Apply_Stable(t *testing.T) { |
| 121 | + targetVersion, err := apiversion.New(apiversion.WithVersion("2024-09-22")) |
| 122 | + require.NoError(t, err) |
| 123 | + |
| 124 | + oas := &openapi3.T{ |
| 125 | + OpenAPI: "3.0.0", |
| 126 | + Info: &openapi3.Info{ |
| 127 | + Version: "1.0", |
| 128 | + }, |
| 129 | + Paths: openapi3.NewPaths(openapi3.WithPath("test", &openapi3.PathItem{ |
| 130 | + Get: &openapi3.Operation{ |
| 131 | + OperationID: "testOperationID", |
| 132 | + Summary: "testSummary", |
| 133 | + Responses: openapi3.NewResponses(openapi3.WithName("200", &openapi3.Response{ |
| 134 | + Content: openapi3.Content{ |
| 135 | + "application/vnd.atlas.2024-09-22+json": { |
| 136 | + Schema: &openapi3.SchemaRef{ |
| 137 | + Ref: "#/components/schemas/PaginatedAppUserView", |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + })), |
| 142 | + }, |
| 143 | + Extensions: map[string]any{}, |
| 144 | + })), |
| 145 | + } |
| 146 | + |
| 147 | + filter := &BumpFilter{ |
| 148 | + metadata: NewMetadata(targetVersion, "test"), |
| 149 | + oas: oas, |
| 150 | + } |
| 151 | + |
| 152 | + require.NoError(t, filter.Apply()) |
| 153 | + assert.Len(t, oas.Paths.Map(), 1) |
| 154 | + assert.Contains(t, oas.Paths.Map(), "test") |
| 155 | + |
| 156 | + testPath := oas.Paths.Map()["test"] |
| 157 | + assert.NotNil(t, testPath.Get) |
| 158 | + op := testPath.Get |
| 159 | + |
| 160 | + assert.NotContains(t, op.Extensions, "x-state") |
| 161 | + assert.NotContains(t, op.Extensions, "x-beta") |
| 162 | +} |
0 commit comments