github.com/PDOK/gokoala@v0.50.6/internal/engine/openapi_test.go (about)

     1  package engine
     2  
     3  import (
     4  	"net/url"
     5  	"path/filepath"
     6  	"testing"
     7  
     8  	gokoalaconfig "github.com/PDOK/gokoala/config"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func Test_newOpenAPI(t *testing.T) {
    14  	openAPIViaCliArgument, err := filepath.Abs("internal/engine/testdata/ogcapi-tiles-1.modified.json")
    15  	if err != nil {
    16  		t.Fatalf("can't locate testdata %v", err)
    17  	}
    18  
    19  	type args struct {
    20  		config      *gokoalaconfig.Config
    21  		openAPIFile string
    22  	}
    23  	tests := []struct {
    24  		name                         string
    25  		args                         args
    26  		expectedStringsInOpenAPISpec []string
    27  	}{
    28  		{
    29  			name: "Test render OpenAPI spec with MINIMAL config",
    30  			args: args{
    31  				config: &gokoalaconfig.Config{
    32  					Version:  "2.3.0",
    33  					Title:    "Test API",
    34  					Abstract: "Test API description",
    35  					BaseURL:  gokoalaconfig.URL{URL: &url.URL{Scheme: "https", Host: "api.foobar.example", Path: "/"}},
    36  					OgcAPI: gokoalaconfig.OgcAPI{
    37  						GeoVolumes: nil,
    38  						Tiles:      nil,
    39  						Styles:     nil,
    40  					},
    41  				},
    42  			},
    43  			expectedStringsInOpenAPISpec: []string{
    44  				"Landing page",
    45  				"/conformance",
    46  				"/api",
    47  			},
    48  		},
    49  		{
    50  			name: "Test render OpenAPI spec with OGC Tiles config",
    51  			args: args{
    52  				config: &gokoalaconfig.Config{
    53  					Version:  "2.3.0",
    54  					Title:    "Test API",
    55  					Abstract: "Test API description",
    56  					BaseURL:  gokoalaconfig.URL{URL: &url.URL{Scheme: "https", Host: "api.foobar.example", Path: "/"}},
    57  					OgcAPI: gokoalaconfig.OgcAPI{
    58  						Tiles: &gokoalaconfig.OgcAPITiles{
    59  							TileServer: gokoalaconfig.URL{URL: &url.URL{Scheme: "https", Host: "tiles.foobar.example", Path: "/somedataset"}},
    60  						},
    61  					},
    62  				},
    63  			},
    64  			expectedStringsInOpenAPISpec: []string{
    65  				"Landing page",
    66  				"/conformance",
    67  				"/api",
    68  				"Vector Tiles",
    69  				"/tiles/{tileMatrixSetId}",
    70  			},
    71  		},
    72  		{
    73  			name: "Test render OpenAPI spec with OGC Styles config",
    74  			args: args{
    75  				config: &gokoalaconfig.Config{
    76  					Version:  "2.3.0",
    77  					Title:    "Test API",
    78  					Abstract: "Test API description",
    79  					BaseURL:  gokoalaconfig.URL{URL: &url.URL{Scheme: "https", Host: "api.foobar.example", Path: "/"}},
    80  					OgcAPI: gokoalaconfig.OgcAPI{
    81  						Styles: &gokoalaconfig.OgcAPIStyles{},
    82  					},
    83  				},
    84  			},
    85  			expectedStringsInOpenAPISpec: []string{
    86  				"Landing page",
    87  				"/conformance",
    88  				"/api",
    89  				"/styles",
    90  				"/styles/{styleId}",
    91  				"/resources",
    92  			},
    93  		},
    94  		{
    95  			name: "Test render OpenAPI spec with OGC GeoVolumes config",
    96  			args: args{
    97  				config: &gokoalaconfig.Config{
    98  					Version:  "2.3.0",
    99  					Title:    "Test API",
   100  					Abstract: "Test API description",
   101  					BaseURL:  gokoalaconfig.URL{URL: &url.URL{Scheme: "https", Host: "api.foobar.example", Path: "/"}},
   102  					OgcAPI: gokoalaconfig.OgcAPI{
   103  						GeoVolumes: &gokoalaconfig.OgcAPI3dGeoVolumes{
   104  							TileServer: gokoalaconfig.URL{URL: &url.URL{Scheme: "https", Host: "api.foobar.example", Path: "/"}},
   105  							Collections: gokoalaconfig.GeoSpatialCollections{
   106  								gokoalaconfig.GeoSpatialCollection{ID: "feature1"},
   107  								gokoalaconfig.GeoSpatialCollection{ID: "feature2"},
   108  							},
   109  						},
   110  					},
   111  				},
   112  			},
   113  			expectedStringsInOpenAPISpec: []string{
   114  				"Landing page",
   115  				"/conformance",
   116  				"/api",
   117  				"Collections",
   118  				"/collections",
   119  			},
   120  		},
   121  		{
   122  			name: "Test render OpenAPI spec with OGC Tiles and extra spec provided through CLI for overwrite",
   123  			args: args{
   124  				config: &gokoalaconfig.Config{
   125  					Version:  "2.3.0",
   126  					Title:    "Test API",
   127  					Abstract: "Test API description",
   128  					BaseURL:  gokoalaconfig.URL{URL: &url.URL{Scheme: "https", Host: "api.foobar.example", Path: "/"}},
   129  					OgcAPI: gokoalaconfig.OgcAPI{
   130  						Tiles: &gokoalaconfig.OgcAPITiles{
   131  							TileServer: gokoalaconfig.URL{URL: &url.URL{Scheme: "https", Host: "tiles.foobar.example", Path: "/somedataset"}},
   132  						},
   133  					},
   134  				},
   135  				openAPIFile: openAPIViaCliArgument,
   136  			},
   137  			expectedStringsInOpenAPISpec: []string{
   138  				"Test API",
   139  				"/conformance",
   140  				"/api",
   141  				"Vector Tiles",
   142  				"/tiles/{tileMatrixSetId}",
   143  				"Map Tiles",                             // extra from given spec through CLI
   144  				"/collections/{collectionId}/map/tiles", // extra from given spec through CLI
   145  			},
   146  		},
   147  		{
   148  			name: "Test render OpenAPI spec with ALL OGC APIs (common, tiles, styles, features, geovolumes)",
   149  			args: args{
   150  				config: &gokoalaconfig.Config{
   151  					Version:  "2.3.0",
   152  					Title:    "Test API",
   153  					Abstract: "Test API description",
   154  					BaseURL:  gokoalaconfig.URL{URL: &url.URL{Scheme: "https", Host: "api.foobar.example", Path: "/"}},
   155  					OgcAPI: gokoalaconfig.OgcAPI{
   156  						GeoVolumes: &gokoalaconfig.OgcAPI3dGeoVolumes{
   157  							TileServer: gokoalaconfig.URL{URL: &url.URL{Scheme: "https", Host: "api.foobar.example", Path: "/"}},
   158  							Collections: gokoalaconfig.GeoSpatialCollections{
   159  								gokoalaconfig.GeoSpatialCollection{ID: "feature1"},
   160  								gokoalaconfig.GeoSpatialCollection{ID: "feature2"},
   161  							},
   162  						},
   163  						Tiles: &gokoalaconfig.OgcAPITiles{
   164  							TileServer: gokoalaconfig.URL{URL: &url.URL{Scheme: "https", Host: "tiles.foobar.example", Path: "/somedataset"}},
   165  						},
   166  						Styles: &gokoalaconfig.OgcAPIStyles{},
   167  						Features: &gokoalaconfig.OgcAPIFeatures{
   168  							Limit: gokoalaconfig.Limit{
   169  								Default: 20,
   170  								Max:     2000,
   171  							},
   172  							Collections: []gokoalaconfig.GeoSpatialCollection{
   173  								{
   174  									ID: "foobar",
   175  									Features: &gokoalaconfig.CollectionEntryFeatures{
   176  										Datasources: &gokoalaconfig.Datasources{
   177  											DefaultWGS84: gokoalaconfig.Datasource{
   178  												GeoPackage: &gokoalaconfig.GeoPackage{
   179  													Local: &gokoalaconfig.GeoPackageLocal{
   180  														File: "./examples/resources/addresses-crs84.gpkg",
   181  													},
   182  												},
   183  											},
   184  										},
   185  									},
   186  								},
   187  							},
   188  						},
   189  					},
   190  				},
   191  			},
   192  			expectedStringsInOpenAPISpec: []string{
   193  				"Landing page",
   194  				"/conformance",
   195  				"/api",
   196  				"Vector Tiles",
   197  				"Features",
   198  			},
   199  		},
   200  	}
   201  	for _, test := range tests {
   202  		t.Run(test.name, func(t *testing.T) {
   203  			openAPI := newOpenAPI(test.args.config, []string{test.args.openAPIFile}, nil)
   204  			assert.NotNil(t, openAPI)
   205  
   206  			// verify resulting OpenAPI spec contains expected strings (keywords, paths, etc)
   207  			for _, expectedStr := range test.expectedStringsInOpenAPISpec {
   208  				assert.Contains(t, string(openAPI.SpecJSON), expectedStr,
   209  					"\"%s\" not found in spec: %s", expectedStr, string(openAPI.SpecJSON))
   210  			}
   211  		})
   212  	}
   213  }