...

Source file src/github.com/go-openapi/loads/fmts/fixture_test.go

Documentation: github.com/go-openapi/loads/fmts

     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 fmts
    16  
    17  import (
    18  	"encoding/json"
    19  	"os"
    20  	"path/filepath"
    21  	"strings"
    22  	"testing"
    23  
    24  	"github.com/go-openapi/spec"
    25  	"github.com/stretchr/testify/assert"
    26  	"github.com/stretchr/testify/require"
    27  )
    28  
    29  var extensions = []string{"json"}
    30  
    31  //nolint:unparam
    32  func assertSpecJSON(t testing.TB, specJSON []byte) bool {
    33  	var expected map[string]interface{}
    34  	require.NoError(t, json.Unmarshal(specJSON, &expected))
    35  
    36  	obj := spec.Swagger{}
    37  	require.NoError(t, json.Unmarshal(specJSON, &obj))
    38  
    39  	cb, err := json.MarshalIndent(obj, "", "  ")
    40  	require.NoError(t, err)
    41  
    42  	var actual map[string]interface{}
    43  	require.NoError(t, json.Unmarshal(cb, &actual))
    44  
    45  	return assertSpecMaps(t, actual, expected)
    46  }
    47  
    48  func assertSpecMaps(t testing.TB, actual, expected map[string]interface{}) bool {
    49  	res := true
    50  	if id, ok := expected["id"]; ok {
    51  		res = assert.Equal(t, id, actual["id"])
    52  	}
    53  	res = res && assert.Equal(t, expected["consumes"], actual["consumes"])
    54  	res = res && assert.Equal(t, expected["produces"], actual["produces"])
    55  	res = res && assert.Equal(t, expected["schemes"], actual["schemes"])
    56  	res = res && assert.Equal(t, expected["swagger"], actual["swagger"])
    57  	res = res && assert.Equal(t, expected["info"], actual["info"])
    58  	res = res && assert.Equal(t, expected["host"], actual["host"])
    59  	res = res && assert.Equal(t, expected["basePath"], actual["basePath"])
    60  	res = res && assert.Equal(t, expected["paths"], actual["paths"])
    61  	res = res && assert.Equal(t, expected["definitions"], actual["definitions"])
    62  	res = res && assert.Equal(t, expected["responses"], actual["responses"])
    63  	res = res && assert.Equal(t, expected["securityDefinitions"], actual["securityDefinitions"])
    64  	res = res && assert.Equal(t, expected["tags"], actual["tags"])
    65  	res = res && assert.Equal(t, expected["externalDocs"], actual["externalDocs"])
    66  	res = res && assert.Equal(t, expected["x-some-extension"], actual["x-some-extension"])
    67  	res = res && assert.Equal(t, expected["x-schemes"], actual["x-schemes"])
    68  
    69  	return res
    70  }
    71  
    72  //nolint:unparam
    73  func roundTripTest(t *testing.T, fixtureType, extension, fileName string, schema interface{}) bool {
    74  	if extension == "yaml" {
    75  		return roundTripTestYAML(t, fixtureType, fileName, schema)
    76  	}
    77  	return roundTripTestJSON(t, fixtureType, fileName, schema)
    78  }
    79  
    80  func roundTripTestJSON(t *testing.T, fixtureType, fileName string, schema interface{}) bool {
    81  	specName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
    82  	t.Logf("verifying %s JSON fixture %q", fixtureType, specName)
    83  
    84  	b, err := os.ReadFile(fileName)
    85  	require.NoError(t, err)
    86  
    87  	var expected map[string]interface{}
    88  	require.NoError(t, json.Unmarshal(b, &expected))
    89  
    90  	require.NoError(t, json.Unmarshal(b, schema))
    91  
    92  	cb, err := json.MarshalIndent(schema, "", "  ")
    93  	require.NoError(t, err)
    94  
    95  	var actual map[string]interface{}
    96  	require.NoError(t, json.Unmarshal(cb, &actual))
    97  
    98  	return assert.EqualValues(t, expected, actual)
    99  }
   100  
   101  func roundTripTestYAML(t *testing.T, fixtureType, fileName string, schema interface{}) bool {
   102  	specName := strings.TrimSuffix(fileName, filepath.Ext(fileName))
   103  	t.Logf("verifying %s YAML fixture %q", fixtureType, specName)
   104  
   105  	b, err := YAMLDoc(fileName)
   106  	require.NoError(t, err)
   107  
   108  	var expected map[string]interface{}
   109  	require.NoError(t, json.Unmarshal(b, &expected))
   110  
   111  	require.NoError(t, json.Unmarshal(b, schema))
   112  
   113  	cb, err := json.MarshalIndent(schema, "", "  ")
   114  	require.NoError(t, err)
   115  
   116  	var actual map[string]interface{}
   117  	require.NoError(t, json.Unmarshal(cb, &actual))
   118  
   119  	return assert.EqualValues(t, expected, actual)
   120  }
   121  
   122  func TestPropertyFixtures(t *testing.T) {
   123  	for _, extension := range extensions {
   124  		path := filepath.Join("..", "fixtures", extension, "models", "properties")
   125  		files, err := os.ReadDir(path)
   126  		if err != nil {
   127  			t.Fatal(err)
   128  		}
   129  
   130  		// for _, f := range files {
   131  		// 	roundTripTest(t, "property", extension, filepath.Join(path, f.Name()), &Schema{})
   132  		// }
   133  		f := files[0]
   134  		roundTripTest(t, "property", extension, filepath.Join(path, f.Name()), &spec.Schema{})
   135  	}
   136  }
   137  
   138  func TestAdditionalPropertiesWithObject(t *testing.T) {
   139  	schema := new(spec.Schema)
   140  	b, err := YAMLDoc("../fixtures/yaml/models/modelWithObjectMap.yaml")
   141  	require.NoError(t, err)
   142  	var expected map[string]interface{}
   143  	require.NoError(t, json.Unmarshal(b, &expected))
   144  	require.NoError(t, json.Unmarshal(b, schema))
   145  
   146  	cb, err := json.MarshalIndent(schema, "", "  ")
   147  	require.NoError(t, err)
   148  
   149  	var actual map[string]interface{}
   150  	require.NoError(t, json.Unmarshal(cb, &actual))
   151  	assert.Equal(t, expected, actual)
   152  }
   153  
   154  func TestModelFixtures(t *testing.T) {
   155  	path := filepath.Join("..", "fixtures", "json", "models")
   156  	files, err := os.ReadDir(path)
   157  	require.NoError(t, err)
   158  	specs := []string{"modelWithObjectMap", "models", "modelWithComposition", "modelWithExamples", "multipleModels"}
   159  FILES:
   160  	for _, f := range files {
   161  		if f.IsDir() {
   162  			continue
   163  		}
   164  		for _, sp := range specs {
   165  			if strings.HasPrefix(f.Name(), sp) {
   166  				roundTripTest(t, "model", "json", filepath.Join(path, f.Name()), &spec.Schema{})
   167  				continue FILES
   168  			}
   169  		}
   170  		roundTripTest(t, "model", "json", filepath.Join(path, f.Name()), &spec.Schema{})
   171  	}
   172  	path = filepath.Join("..", "fixtures", "yaml", "models")
   173  	files, err = os.ReadDir(path)
   174  	require.NoError(t, err)
   175  
   176  YAMLFILES:
   177  	for _, f := range files {
   178  		if f.IsDir() {
   179  			continue
   180  		}
   181  		for _, sp := range specs {
   182  			if strings.HasPrefix(f.Name(), sp) {
   183  				roundTripTest(t, "model", "yaml", filepath.Join(path, f.Name()), &spec.Schema{})
   184  				continue YAMLFILES
   185  			}
   186  		}
   187  		roundTripTest(t, "model", "yaml", filepath.Join(path, f.Name()), &spec.Schema{})
   188  	}
   189  }
   190  
   191  func TestParameterFixtures(t *testing.T) {
   192  	path := filepath.Join("..", "fixtures", "json", "resources", "parameters")
   193  	files, err := os.ReadDir(path)
   194  	require.NoError(t, err)
   195  
   196  	for _, f := range files {
   197  		roundTripTest(t, "parameter", "json", filepath.Join(path, f.Name()), &spec.Parameter{})
   198  	}
   199  }
   200  
   201  func TestOperationFixtures(t *testing.T) {
   202  	path := filepath.Join("..", "fixtures", "json", "resources", "operations")
   203  	files, err := os.ReadDir(path)
   204  	require.NoError(t, err)
   205  
   206  	for _, f := range files {
   207  		roundTripTest(t, "operation", "json", filepath.Join(path, f.Name()), &spec.Operation{})
   208  	}
   209  }
   210  
   211  func TestResponseFixtures(t *testing.T) {
   212  	path := filepath.Join("..", "fixtures", "json", "responses")
   213  	files, err := os.ReadDir(path)
   214  	require.NoError(t, err)
   215  
   216  	for _, f := range files {
   217  		if !strings.HasPrefix(f.Name(), "multiple") {
   218  			roundTripTest(t, "response", "json", filepath.Join(path, f.Name()), &spec.Response{})
   219  		} else {
   220  			roundTripTest(t, "responses", "json", filepath.Join(path, f.Name()), &spec.Responses{})
   221  		}
   222  	}
   223  }
   224  
   225  func TestResourcesFixtures(t *testing.T) {
   226  	path := filepath.Join("..", "fixtures", "json", "resources")
   227  	files, err := os.ReadDir(path)
   228  	require.NoError(t, err)
   229  
   230  	pathItems := []string{"resourceWithLinkedDefinitions_part1"}
   231  	toSkip := []string{}
   232  FILES:
   233  	for _, f := range files {
   234  		if f.IsDir() {
   235  			continue
   236  		}
   237  		for _, ts := range toSkip {
   238  			if strings.HasPrefix(f.Name(), ts) {
   239  				t.Log("verifying resource" + strings.TrimSuffix(f.Name(), filepath.Ext(f.Name())))
   240  				b, err := os.ReadFile(filepath.Join(path, f.Name()))
   241  				require.NoError(t, err)
   242  				assertSpecJSON(t, b)
   243  				continue FILES
   244  			}
   245  		}
   246  		for _, pi := range pathItems {
   247  			if strings.HasPrefix(f.Name(), pi) {
   248  				roundTripTest(t, "path items", "json", filepath.Join(path, f.Name()), &spec.PathItem{})
   249  				continue FILES
   250  			}
   251  		}
   252  
   253  		t.Logf("verifying resource %q", strings.TrimSuffix(f.Name(), filepath.Ext(f.Name())))
   254  		b2, err := os.ReadFile(filepath.Join(path, f.Name()))
   255  		require.NoError(t, err)
   256  		assertSpecJSON(t, b2)
   257  	}
   258  }
   259  

View as plain text