Skip to content

Commit

Permalink
Added unit test - Part 1
Browse files Browse the repository at this point in the history
  • Loading branch information
andreaangiolillo committed May 29, 2024
1 parent 9eb0724 commit e5d169e
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tools/cli/internal/openapi/oasdiff.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ func removeExternalRef(ref string) string {
return ref
}

return ref[:pos]
return ref[pos:]
}

// updateExternalRefContent updates the external references of OASes to remove the reference to openapi-mms.json
Expand Down
110 changes: 110 additions & 0 deletions tools/cli/internal/openapi/oasdiff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
package openapi

import (
"github.com/stretchr/testify/assert"
"reflect"
"testing"

"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -824,3 +826,111 @@ func newExternalSpecPaths(t *testing.T) *openapi3.Paths {
})
return inputPath
}

func TestUpdateExternalRefResponses(t *testing.T) {
tests := []struct {
name string
input map[string]*openapi3.ResponseRef
expected map[string]*openapi3.ResponseRef
}{
{
name: "Nil responses",
input: nil,
expected: nil,
},
{
name: "Empty responses",
input: map[string]*openapi3.ResponseRef{},
expected: map[string]*openapi3.ResponseRef{},
},
{
name: "Responses with Ref to .json#",
input: map[string]*openapi3.ResponseRef{
"200": {
Ref: "openapi-mms.json#someRef",
},
},
expected: map[string]*openapi3.ResponseRef{
"200": {
Ref: "#someRef",
},
},
},
{
name: "Responses with internal Ref",
input: map[string]*openapi3.ResponseRef{
"200": {
Ref: "#someRef",
},
},
expected: map[string]*openapi3.ResponseRef{
"200": {
Ref: "#someRef",
},
},
},
{
name: "Responses with nested Content Ref to .json#",
input: map[string]*openapi3.ResponseRef{
"200": {
Value: &openapi3.Response{
Content: openapi3.Content{
"application/json": &openapi3.MediaType{
Schema: &openapi3.SchemaRef{
Ref: "openapi-mms.json#nestedRef",
},
},
},
},
},
},
expected: map[string]*openapi3.ResponseRef{
"200": {
Value: &openapi3.Response{
Content: openapi3.Content{
"application/json": &openapi3.MediaType{
Schema: &openapi3.SchemaRef{
Ref: "#nestedRef",
},
},
},
},
},
},
},
{
name: "Responses with no matching Ref",
input: map[string]*openapi3.ResponseRef{
"200": {
Ref: "other.json#someRef",
},
},
expected: map[string]*openapi3.ResponseRef{
"200": {
Ref: "#someRef",
},
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
input := newResponseFromMap(t, tt.input)
expected := newResponseFromMap(t, tt.expected)

updateExternalRefResponses(input)
assert.True(t, reflect.DeepEqual(expected, input))
})
}
}

func newResponseFromMap(t *testing.T, input map[string]*openapi3.ResponseRef) *openapi3.Responses {
t.Helper()
output := &openapi3.Responses{}

for k, v := range input {
output.Set(k, v)
}

return output
}

0 comments on commit e5d169e

Please sign in to comment.