Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions api/surface.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
{
"method": "GET",
"path": "/",
"conformanceClass": "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core",
"conformanceClass": "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/landing-page",
"functions": []
},
{
"method": "GET",
"path": "/api",
"conformanceClass": "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core",
"conformanceClass": "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/landing-page",
"functions": []
},
{
Expand Down Expand Up @@ -390,7 +390,7 @@
{
"method": "GET",
"path": "/conformance",
"conformanceClass": "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core",
"conformanceClass": "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/landing-page",
"functions": []
},
{
Expand Down
148 changes: 148 additions & 0 deletions ats_common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// The abstract tests of the class landing-page of OGC API – Common – Part 1: Core
// (OGC 19-072), as rows beside those of Moving Features Part 1 (#atsDischargedBy)
// and Part 4 (#atsPart4): the landing page, the API definition and the
// conformance declaration, each requested through the routing table the service
// runs (#newMux) and read against the schema the abstract test names.
//
// The landing page and the conformance declaration are validated against the
// landingPage and confClasses schemas of the vendored Moving Features document,
// which state the constraints of Common's landingPage.yaml, confClasses.yaml and
// link.yaml: the same required members of the same types. The API definition is
// validated against the OpenAPI 3.0 schema, vendored in openapi/ beside them.
package main

import (
"bytes"
"encoding/json"
"net/http/httptest"
"os"
"path/filepath"
"testing"

"github.com/santhosh-tekuri/jsonschema/v6"
)

// oasSchemaPath is the vendored JSON Schema of OpenAPI 3.0 documents.
const oasSchemaPath = "openapi/oas-3.0-schema.json"

// atsCommonLandingPage is the class landing-page, its abstract tests in the
// order Common Part 1 gives them, each with the test that reads what it states.
var atsCommonLandingPage = []struct{ id, dischargedBy string }{
{"/conf/landing-page/root-op", "TestCommonLandingPage"},
{"/conf/landing-page/root-success", "TestCommonLandingPage"},
{"/conf/landing-page/api-definition-op", "TestCommonAPIDefinition"},
{"/conf/landing-page/api-definition-success", "TestCommonAPIDefinition"},
{"/conf/landing-page/conformance-op", "TestCommonConformanceDeclaration"},
{"/conf/landing-page/conformance-success", "TestCommonConformanceDeclaration"},
}

// commonGet requests path through the routing table the service runs.
func commonGet(path string) *httptest.ResponseRecorder {
rec := httptest.NewRecorder()
newMux().ServeHTTP(rec, httptest.NewRequest("GET", path, nil))
return rec
}

// root-op, root-success: the landing page answers 200, is a landingPage, and
// links the API definition (service-desc) and the conformance declaration under
// the relation type Common Part 1 requires, beside the one Features Part 1 does.
func TestCommonLandingPage(t *testing.T) {
rec := commonGet("/")
validate(t, ogcSchemas(t)("landingPage"), rec, "the landing page")
var page struct {
Links []struct{ Href, Rel string } `json:"links"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &page); err != nil {
t.Fatal(err)
}
links := map[string]string{}
for _, l := range page.Links {
links[l.Rel] = l.Href
}
for rel, href := range map[string]string{
"service-desc": "/api",
relConformance: "/conformance",
"conformance": "/conformance",
"data": "/collections",
} {
if links[rel] != href {
t.Errorf("the landing page links %q to %q, want %q", rel, links[rel], href)
}
}
}

// api-definition-op, api-definition-success: the API definition the landing page
// links answers 200 and is an OpenAPI 3.0 document.
func TestCommonAPIDefinition(t *testing.T) {
f, err := os.Open(oasSchemaPath)
if err != nil {
t.Fatalf("the vendored OpenAPI schema is missing: %v", err)
}
defer f.Close()
doc, err := jsonschema.UnmarshalJSON(f)
if err != nil {
t.Fatal(err)
}
c := jsonschema.NewCompiler()
if err := c.AddResource("oas.json", doc); err != nil {
t.Fatal(err)
}
schema, err := c.Compile("oas.json")
if err != nil {
t.Fatal(err)
}
validate(t, schema, commonGet("/api"), "the API definition")

// The schema refuses what is no OpenAPI 3.0 document, so the validation
// above asserts something.
for _, bad := range []string{`{"openapi":"3.0.3","paths":{}}`, `{"openapi":"2.0","info":{"title":"x","version":"1"},"paths":{}}`} {
v, err := jsonschema.UnmarshalJSON(bytes.NewReader([]byte(bad)))
if err != nil {
t.Fatal(err)
}
if schema.Validate(v) == nil {
t.Errorf("the OpenAPI schema admits %s", bad)
}
}
}

// conformance-op, conformance-success: the conformance declaration answers 200,
// is a confClasses, and lists the classes of Common Part 1 the service implements.
func TestCommonConformanceDeclaration(t *testing.T) {
validate(t, ogcSchemas(t)("confClasses"), commonGet("/conformance"), "the conformance declaration")
declared := declaredClasses(t)
for _, c := range []string{confCommonCore, confCommonLanding} {
if !declared[c] {
t.Errorf("the conformance declaration does not list %s", c)
}
}
}

// Every row names a test a test file declares, as #TestATSPart4RowsAreDischarged
// holds the rows of Part 4, and the class is the one the routes are marked with.
func TestCommonLandingPageRowsAreDischarged(t *testing.T) {
files, err := filepath.Glob("*_test.go")
if err != nil {
t.Fatal(err)
}
var src []byte
for _, f := range files {
b, err := os.ReadFile(f)
if err != nil {
t.Fatal(err)
}
src = append(src, b...)
}
for _, r := range atsCommonLandingPage {
if !bytes.Contains(src, []byte("func "+r.dischargedBy+"(")) {
t.Errorf("%s names %s, which no test file declares", r.id, r.dischargedBy)
}
}
for _, r := range routeTable() {
if r.path == "/" || r.path == "/api" || r.path == "/conformance" {
if r.class != confCommonLanding {
t.Errorf("GET %s is marked %s, want %s", r.path, r.class, confCommonLanding)
}
}
}
}
5 changes: 4 additions & 1 deletion edr.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,10 @@ func edrPaths() map[string]any {
for _, q := range edrQueries {
out["/collections/{cid}/"+q.name] = map[string]any{"get": map[string]any{"summary": fmt.Sprintf(
"OGC API - EDR: the %s query over a raster collection, coords a WKT %s, answered in CoverageJSON",
q.name, q.geometry)}}
q.name, q.geometry),
// Every operation of an OpenAPI 3.0 document states its responses, as
// the operations #apiDoc writes do.
"responses": map[string]any{"200": map[string]any{"description": "OK"}}}}
}
return out
}
21 changes: 17 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,14 @@ const (
confMovingFeatures = "http://www.opengis.net/spec/ogcapi-movingfeatures-1/1.0/conf/movingfeatures"
confCQuery = "http://www.opengis.net/spec/ogcapi-movingfeatures-4/1.0/conf/cquery"
confCommonCore = "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core"
confCommonLanding = "http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/landing-page"
confCommonCollections = "https://www.opengis.net/spec/ogcapi-common-2/1.0/conf/collections"
)

// relConformance is the relation type OGC API - Common - Part 1 gives the link
// from the landing page to the conformance declaration.
const relConformance = "http://www.opengis.net/def/rel/ogc/1.0/conformance"

// extension marks a route that is a requirement of no class the tier declares:
// a MobilityAPI extension, which the API definition names as one and which
// changes no answer a declared class defines.
Expand All @@ -213,9 +218,9 @@ type route struct {
func routeTable() []route {
rs := []route{
{"GET", "/health", health, extension},
{"GET", "/", landing, confCommonCore},
{"GET", "/api", apiDoc, confCommonCore},
{"GET", "/conformance", conformance, confCommonCore},
{"GET", "/", landing, confCommonLanding},
{"GET", "/api", apiDoc, confCommonLanding},
{"GET", "/conformance", conformance, confCommonLanding},
{"GET", "/collections", listCollections, confMFCollection},
{"POST", "/collections", postCollection, confMFCollection},
{"GET", "/collections/{cid}", getCollection, confMFCollection},
Expand Down Expand Up @@ -360,13 +365,20 @@ func ident(s string) string { return `"` + strings.ReplaceAll(s, `"`, `""`) + `"
// be parameterised): a collection id must be a plain SQL identifier.
var validID = regexp.MustCompile(`^[a-z_][a-z0-9_]*$`)

// landing writes the landing page. The conformance declaration is linked under
// both relation types the standards require of it: conformance, as OGC API -
// Features - Part 1 requires (/req/core/root-success), on which Moving Features
// builds, and relConformance, as OGC API - Common - Part 1 requires
// (/req/landing-page/root-success). A client compares a relation type as a
// string, so each finds the link it looks for.
func landing(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, map[string]any{
"title": "MobilityAPI-go", "description": "OGC API – Moving Features over MobilityDB",
"links": []map[string]string{
{"rel": "self", "href": "/", "type": "application/json"},
{"rel": "service-desc", "href": "/api", "type": "application/vnd.oai.openapi+json;version=3.0"},
{"rel": "conformance", "href": "/conformance", "type": "application/json"},
{"rel": relConformance, "href": "/conformance", "type": "application/json"},
{"rel": "data", "href": "/collections", "type": "application/json"},
}})
}
Expand All @@ -388,7 +400,8 @@ func conformance(w http.ResponseWriter, r *http.Request) {
// conformsTo answers the identifiers the conformance declaration lists.
func conformsTo() []string {
classes := append([]string{
confMFCommon, confMFCollection, confMovingFeatures, confCQuery, confCommonCore, confCommonCollections,
confMFCommon, confMFCollection, confMovingFeatures, confCQuery, confCommonCore, confCommonLanding,
confCommonCollections,
}, append(coverageConformance, edrConformance)...)
var out []string
for _, c := range classes {
Expand Down
13 changes: 13 additions & 0 deletions openapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,3 +112,16 @@ Measured against the resolved schema, under either reading of `format`:
The resolutions do narrow what the schema admits, and the test states each narrowing as a case: a
scalar `values` (2), a three-character unit that is no common code such as `deg` (3), and an
instant that is no RFC 3339 date-time (5).

# The OpenAPI 3.0 schema

`oas-3.0-schema.json` is the JSON Schema the OpenAPI Initiative publishes for OpenAPI 3.0.x
documents, vendored byte for byte:

URL https://spec.openapis.org/oas/3.0/schema/2024-10-18
bytes 35755
sha256 2385f5bbb8c37878daae73baeabe7f34b2f022a4a8c049329ee61f71796f039c

`ats_common_test.go` validates the API definition the tier writes against it, as abstract
test `/conf/landing-page/api-definition-success` of OGC API — Common — Part 1 asks. The copy
is never edited.
Loading