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 ats_part1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ type atsTest struct {

// The conformance class URIs of Part 1.
const (
classCollection = "http://www.opengis.net/spec/ogcapi-movingfeatures-1/1.0/conf/mf-collection"
classFeatures = "http://www.opengis.net/spec/ogcapi-movingfeatures-1/1.0/conf/movingfeatures"
classCommon = "http://www.opengis.net/spec/ogcapi-movingfeatures-1/1.0/conf/common"
classCollection = confMFCollection
classFeatures = confMovingFeatures
classCommon = confMFCommon
)

// atsPart1 is Annex A. The 45 abstract tests of the mf-collection and
Expand Down
139 changes: 139 additions & 0 deletions ats_routes_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
// Every route the service serves is a requirement of a conformance class it
// declares, or a MobilityAPI extension the API definition names as one.
//
// The routing table and the API definition are both read from #routeTable, and
// this file holds the three to each other: a route marked with a class the
// service does not declare, a route the API definition does not describe, an
// operation described that no route serves, and a route an abstract test of
// Annex A issues that is marked as an extension, each fail here.
package main

import (
"encoding/json"
"net/http/httptest"
"sort"
"strings"
"testing"
)

// declaredClasses reads the conformance declaration the service writes.
func declaredClasses(t *testing.T) map[string]bool {
t.Helper()
rec := httptest.NewRecorder()
conformance(rec, httptest.NewRequest("GET", "/conformance", nil))
var doc struct {
ConformsTo []string `json:"conformsTo"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &doc); err != nil {
t.Fatal(err)
}
out := map[string]bool{}
for _, c := range doc.ConformsTo {
out[c] = true
}
return out
}

// apiOperations reads the API definition the service writes: each operation,
// keyed "METHOD path", with its marking.
func apiOperations(t *testing.T) map[string]map[string]any {
t.Helper()
rec := httptest.NewRecorder()
apiDoc(rec, httptest.NewRequest("GET", "/api", nil))
var doc struct {
Paths map[string]map[string]map[string]any `json:"paths"`
}
if err := json.Unmarshal(rec.Body.Bytes(), &doc); err != nil {
t.Fatal(err)
}
out := map[string]map[string]any{}
for p, item := range doc.Paths {
for m, op := range item {
out[strings.ToUpper(m)+" "+p] = op
}
}
return out
}

// Every route is marked with a class the service declares, or as an extension;
// the API definition describes every route with that marking and describes no
// operation that no route serves.
func TestATSEveryRouteIsAClassOrAnExtension(t *testing.T) {
declared := declaredClasses(t)
ops := apiOperations(t)
served := map[string]bool{}
byClass := map[string]int{}
for _, r := range routeTable() {
key := r.method + " " + r.path
if served[key] {
t.Errorf("%s is routed twice", key)
}
served[key] = true
if r.class != extension && !declared[r.class] {
t.Errorf("%s is marked with %s, which the conformance declaration does not list", key, r.class)
}
op, ok := ops[key]
if !ok {
t.Errorf("%s is served and the API definition does not describe it", key)
continue
}
class, _ := op[xConformanceClass].(string)
ext, _ := op[xExtension].(bool)
switch {
case r.class == extension && (!ext || class != ""):
t.Errorf("%s is an extension and the API definition marks it %s=%v %s=%q",
key, xExtension, ext, xConformanceClass, class)
case r.class != extension && (ext || class != r.class):
t.Errorf("%s is a requirement of %s and the API definition marks it %s=%v %s=%q",
key, r.class, xExtension, ext, xConformanceClass, class)
}
if r.class == extension {
byClass["extension"]++
} else {
byClass[r.class]++
}
}
for key := range ops {
if !served[key] {
t.Errorf("the API definition describes %s, which no route serves", key)
}
}
if len(served) == 0 {
t.Fatal("the route table is empty, so this test would assert nothing")
}
var lines []string
for c, n := range byClass {
lines = append(lines, c+": "+itoa(n))
}
sort.Strings(lines)
t.Logf("%d routes: %s", len(served), strings.Join(lines, "; "))
}

// A route an abstract test of Annex A issues is a requirement of the class the
// test belongs to, never an extension: the marking agrees with the standard's
// own conformance instrument.
func TestATSAnnexARoutesAreClassRoutes(t *testing.T) {
classOf := map[string]string{}
for _, r := range routeTable() {
classOf[r.method+" "+r.path] = r.class
}
var n int
for _, a := range atsPart1 {
if a.kind != atsRoute {
continue
}
n++
class, ok := classOf[a.method+" "+a.path]
if !ok {
t.Errorf("%s issues %s %s, which the route table does not hold", a.id, a.method, a.path)
continue
}
if class != a.class {
t.Errorf("%s issues %s %s, marked %q; the abstract test belongs to %s",
a.id, a.method, a.path, class, a.class)
}
}
if n == 0 {
t.Fatal("Annex A holds no route row, so this test would assert nothing")
}
}
197 changes: 130 additions & 67 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,66 +182,130 @@ func main() {
log.Println("shut down")
}

// newMux is the routing table of the tier: every path the service serves,
// with the method each one accepts. main and the conformance tests share it,
// so a test exercises the routing the service runs.
// The conformance classes the tier declares at /conformance, beside
// #coverageConformance and #edrConformance.
const (
confMFCommon = "http://www.opengis.net/spec/ogcapi-movingfeatures-1/1.0/conf/common"
confMFCollection = "http://www.opengis.net/spec/ogcapi-movingfeatures-1/1.0/conf/mf-collection"
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"
confCommonCollections = "http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/collections"
)

// 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.
const extension = ""

// route is one operation the service serves: the method and path it is routed
// by, its handler, and the declared conformance class it is a requirement of,
// or #extension.
type route struct {
method, path string
handler http.HandlerFunc
class string
}

// routeTable is every operation the service serves. newMux routes by it and
// apiDoc marks each operation of the API definition by it, so what is served,
// what is described and what each route answers to are one list.
func routeTable() []route {
rs := []route{
{"GET", "/health", health, extension},
{"GET", "/", landing, confCommonCore},
{"GET", "/api", apiDoc, confCommonCore},
{"GET", "/conformance", conformance, confCommonCore},
{"GET", "/collections", listCollections, confMFCollection},
{"POST", "/collections", postCollection, confMFCollection},
{"GET", "/collections/{cid}", getCollection, confMFCollection},
{"PUT", "/collections/{cid}", putCollection, confMFCollection},
{"DELETE", "/collections/{cid}", deleteCollection, confMFCollection},
// The animated map and the fleet dashboard read these two.
{"GET", "/collections/{cid}/trajectories", trajectories, extension},
{"GET", "/collections/{cid}/timeseries", timeseries, extension},
{"GET", "/collections/{cid}/items", streamItems, confMovingFeatures},
{"POST", "/collections/{cid}/items", postItem, confMovingFeatures},
{"GET", "/collections/{cid}/items/{fid}", getItem, confMovingFeatures},
{"PUT", "/collections/{cid}/items/{fid}", putItem, extension},
{"DELETE", "/collections/{cid}/items/{fid}", deleteItem, confMovingFeatures},
// The lakehouse bulk feed (NDJSON | Parquet) and the bulk ingest of a
// real-time fleet feed.
{"GET", "/collections/{cid}/export", export, extension},
{"POST", "/collections/{cid}/bulk", bulkIngest, extension},
// OGC API - Coverages - Part 1: a raster collection's coverage and its fields.
{"GET", "/collections/{cid}/coverage", getCoverage, coverageConfBase + "core"},
{"GET", "/collections/{cid}/schema", coverageSchema, coverageConfBase + "core"},
// The sub-resources of a moving feature.
{"GET", "/collections/{cid}/items/{fid}/tgsequence", tgSequence, confMovingFeatures},
{"POST", "/collections/{cid}/items/{fid}/tgsequence", postTgSequence, confMovingFeatures},
{"DELETE", "/collections/{cid}/items/{fid}/tgsequence/{tgid}", deleteTgSequence, confMovingFeatures},
{"GET", "/collections/{cid}/items/{fid}/tgsequence/{tgid}/{qtype}", tgSequenceQuery, confMovingFeatures},
{"GET", "/collections/{cid}/items/{fid}/tproperties", listTProperties, confMovingFeatures},
{"POST", "/collections/{cid}/items/{fid}/tproperties", postTProperties, confMovingFeatures},
{"GET", "/collections/{cid}/items/{fid}/tproperties/{pname}", getTProperty, confMovingFeatures},
{"POST", "/collections/{cid}/items/{fid}/tproperties/{pname}", postTPropertyValues, confMovingFeatures},
{"DELETE", "/collections/{cid}/items/{fid}/tproperties/{pname}", deleteTProperty, confMovingFeatures},
{"DELETE", "/collections/{cid}/items/{fid}/tproperties/{pname}/{tvid}", deleteTPropertyValue, confMovingFeatures},
// MF Part 4 (Stream Extension): continuous queries on a temporal property,
// their results delivered over Server-Sent Events.
{"POST", "/collections/{cid}/items/{fid}/tproperties/{pname}/queries", postQuery, confCQuery},
{"GET", "/collections/{cid}/items/{fid}/tproperties/{pname}/queries", listQueries, confCQuery},
{"GET", "/collections/{cid}/items/{fid}/tproperties/{pname}/queries/{qid}", getQuery, confCQuery},
{"GET", "/collections/{cid}/items/{fid}/tproperties/{pname}/queries/{qid}/stream", streamQuery, confCQuery},
{"DELETE", "/collections/{cid}/items/{fid}/tproperties/{pname}/queries/{qid}", deleteQuery, confCQuery},
// Live ingestion: a producer pushes records the live queries process.
{"POST", "/collections/{cid}/items/{fid}/tproperties/{pname}/ingest", ingestProperty, extension},
// A continuous query streaming the moving feature's position, the feed of
// an animated map, which CONFORMANCE.md states is no Part 4 requirement.
{"POST", "/collections/{cid}/items/{fid}/tgsequence/queries", postGeometryQuery, extension},
{"GET", "/collections/{cid}/items/{fid}/tgsequence/queries", listGeometryQueries, extension},
{"GET", "/collections/{cid}/items/{fid}/tgsequence/queries/{qid}", getQuery, extension},
{"GET", "/collections/{cid}/items/{fid}/tgsequence/queries/{qid}/stream", streamQuery, extension},
{"DELETE", "/collections/{cid}/items/{fid}/tgsequence/queries/{qid}", deleteQuery, extension},
}
// OGC API - EDR: the queries of a raster collection.
for _, q := range edrQueries {
rs = append(rs, route{"GET", "/collections/{cid}/" + q.name, edrQuery(q.name), edrConformance})
}
return rs
}

// The OpenAPI specification extensions by which the API definition marks an
// operation: the declared conformance class it is a requirement of, or that it
// is a MobilityAPI extension.
const (
xConformanceClass = "x-conformance-class"
xExtension = "x-mobilityapi-extension"
)

// markOperations marks every operation of the API definition's paths with the
// class #routeTable gives its route, or as an extension.
func markOperations(paths map[string]any) {
for _, r := range routeTable() {
item, _ := paths[r.path].(map[string]any)
op, _ := item[strings.ToLower(r.method)].(map[string]any)
if op == nil {
continue
}
if r.class == extension {
op[xExtension] = true
} else {
op[xConformanceClass] = r.class
}
}
}

// health answers that the service is up.
func health(w http.ResponseWriter, r *http.Request) { writeRaw(w, 200, `{"status":"ok"}`) }

// newMux is the routing table of the tier, built from #routeTable. main and the
// conformance tests share it, so a test exercises the routing the service runs.
func newMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("GET /health", func(w http.ResponseWriter, r *http.Request) { writeRaw(w, 200, `{"status":"ok"}`) })
mux.HandleFunc("GET /", landing)
mux.HandleFunc("GET /api", apiDoc)
mux.HandleFunc("GET /conformance", conformance)
mux.HandleFunc("GET /collections", listCollections)
mux.HandleFunc("POST /collections", postCollection)
mux.HandleFunc("GET /collections/{cid}", getCollection)
mux.HandleFunc("PUT /collections/{cid}", putCollection)
mux.HandleFunc("DELETE /collections/{cid}", deleteCollection)
mux.HandleFunc("GET /collections/{cid}/trajectories", trajectories)
mux.HandleFunc("GET /collections/{cid}/timeseries", timeseries)
mux.HandleFunc("GET /collections/{cid}/items", streamItems)
mux.HandleFunc("GET /collections/{cid}/items/{fid}", getItem)
mux.HandleFunc("POST /collections/{cid}/items", postItem)
mux.HandleFunc("GET /collections/{cid}/export", export) // lakehouse bulk feed (NDJSON | Parquet)
// OGC API - Coverages - Part 1: a raster collection's coverage and its fields
mux.HandleFunc("GET /collections/{cid}/coverage", getCoverage)
mux.HandleFunc("GET /collections/{cid}/schema", coverageSchema)
// OGC API - EDR: the queries of a raster collection
for _, q := range edrQueries {
mux.HandleFunc("GET /collections/{cid}/"+q.name, edrQuery(q.name))
}
// extension (not in conformsTo): bulk ingest of a real-time fleet feed
mux.HandleFunc("POST /collections/{cid}/bulk", bulkIngest)
// OGC API – Moving Features sub-resources of a moving feature:
mux.HandleFunc("GET /collections/{cid}/items/{fid}/tgsequence", tgSequence)
mux.HandleFunc("GET /collections/{cid}/items/{fid}/tgsequence/{tgid}/{qtype}", tgSequenceQuery)
mux.HandleFunc("GET /collections/{cid}/items/{fid}/tproperties", listTProperties)
mux.HandleFunc("GET /collections/{cid}/items/{fid}/tproperties/{pname}", getTProperty)
// writes: replace / delete a feature, append a temporally-disjoint sub-trajectory
mux.HandleFunc("PUT /collections/{cid}/items/{fid}", putItem)
mux.HandleFunc("DELETE /collections/{cid}/items/{fid}", deleteItem)
mux.HandleFunc("POST /collections/{cid}/items/{fid}/tgsequence", postTgSequence)
// temporal properties are user-supplied, stored, time-varying attributes
mux.HandleFunc("POST /collections/{cid}/items/{fid}/tproperties", postTProperties)
mux.HandleFunc("POST /collections/{cid}/items/{fid}/tproperties/{pname}", postTPropertyValues)
mux.HandleFunc("DELETE /collections/{cid}/items/{fid}/tproperties/{pname}", deleteTProperty)
mux.HandleFunc("DELETE /collections/{cid}/items/{fid}/tproperties/{pname}/{tvid}", deleteTPropertyValue)
mux.HandleFunc("DELETE /collections/{cid}/items/{fid}/tgsequence/{tgid}", deleteTgSequence)
// MF Part 4 (Stream Extension): continuous queries on a temporal property,
// delivered over Server-Sent Events.
mux.HandleFunc("POST /collections/{cid}/items/{fid}/tproperties/{pname}/queries", postQuery)
mux.HandleFunc("GET /collections/{cid}/items/{fid}/tproperties/{pname}/queries", listQueries)
mux.HandleFunc("GET /collections/{cid}/items/{fid}/tproperties/{pname}/queries/{qid}", getQuery)
mux.HandleFunc("GET /collections/{cid}/items/{fid}/tproperties/{pname}/queries/{qid}/stream", streamQuery)
mux.HandleFunc("DELETE /collections/{cid}/items/{fid}/tproperties/{pname}/queries/{qid}", deleteQuery)
// live ingestion: a producer pushes records that live queries process in real time
mux.HandleFunc("POST /collections/{cid}/items/{fid}/tproperties/{pname}/ingest", ingestProperty)
// MF Part 4: a continuous query that streams the moving feature's position
// (the temporal geometry), the feed for an animated map.
mux.HandleFunc("POST /collections/{cid}/items/{fid}/tgsequence/queries", postGeometryQuery)
mux.HandleFunc("GET /collections/{cid}/items/{fid}/tgsequence/queries", listGeometryQueries)
mux.HandleFunc("GET /collections/{cid}/items/{fid}/tgsequence/queries/{qid}", getQuery)
mux.HandleFunc("GET /collections/{cid}/items/{fid}/tgsequence/queries/{qid}/stream", streamQuery)
mux.HandleFunc("DELETE /collections/{cid}/items/{fid}/tgsequence/queries/{qid}", deleteQuery)
for _, r := range routeTable() {
mux.HandleFunc(r.method+" "+r.path, r.handler)
}
return mux
}

Expand Down Expand Up @@ -312,12 +376,7 @@ func landing(w http.ResponseWriter, r *http.Request) {
// #coverageConformance classes of OGC API - Coverages its raster collections do.
func conformance(w http.ResponseWriter, r *http.Request) {
writeJSON(w, 200, map[string]any{"conformsTo": append([]string{
"http://www.opengis.net/spec/ogcapi-movingfeatures-1/1.0/conf/common",
"http://www.opengis.net/spec/ogcapi-movingfeatures-1/1.0/conf/mf-collection",
"http://www.opengis.net/spec/ogcapi-movingfeatures-1/1.0/conf/movingfeatures",
"http://www.opengis.net/spec/ogcapi-movingfeatures-4/1.0/conf/cquery",
"http://www.opengis.net/spec/ogcapi-common-1/1.0/conf/core",
"http://www.opengis.net/spec/ogcapi-common-2/1.0/conf/collections",
confMFCommon, confMFCollection, confMovingFeatures, confCQuery, confCommonCore, confCommonCollections,
}, append(coverageConformance, edrConformance)...)})
}
func listCollections(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -1471,9 +1530,12 @@ func apiDoc(w http.ResponseWriter, r *http.Request) {
"openapi": "3.0.3",
"info": map[string]any{"title": "MobilityAPI-go", "version": "1.0.0", "description": "OGC API – Moving Features over MobilityDB"},
"paths": map[string]any{
"/": get("Landing page"),
"/api": get("API definition"),
"/conformance": get("Conformance declaration"),
"/health": get("Liveness: answers {\"status\":\"ok\"} while the service is up"),
"/collections/{cid}/trajectories": get("Every feature's trajectory for animated rendering, sampled to ~sample seconds (180 by default), in WGS84, as {trips, tmin, tmax}"),
"/collections/{cid}/timeseries": get("The fleet-wide average of the speed property, grouped by ship type, over step-second buckets"),
"/": get("Landing page"),
"/api": get("API definition"),
"/conformance": get("Conformance declaration"),
"/collections": map[string]any{
"get": withParams(op("Moving feature collections"), limitParam),
"post": op("Register a new collection"),
Expand Down Expand Up @@ -1549,6 +1611,7 @@ func apiDoc(w http.ResponseWriter, r *http.Request) {
for p, item := range edrPaths() {
paths[p] = item
}
markOperations(paths)
w.Header().Set("Content-Type", "application/vnd.oai.openapi+json;version=3.0")
w.WriteHeader(200)
json.NewEncoder(w).Encode(doc)
Expand Down
Loading