Merged in jb/openapi (pull request #22)

add openapi infra

* add openapi infra

Includes generated stubs and all deps

* generatetolocation

* basesetupoffunctionsandclient

* round1controllertests

* passintegrationtests

* cleanupfromfullsuite

* storedjson

* fixjsonyaml

* fixtests


Approved-by: Michael McGuinness
This commit is contained in:
Jay Brown
2025-01-15 19:45:51 +00:00
committed by Michael McGuinness
parent 5ca36b0502
commit 174644b63c
443 changed files with 78766 additions and 2119 deletions
+457
View File
@@ -0,0 +1,457 @@
// Package queryservice provides primitives to interact with the openapi HTTP API.
//
// Code generated by github.com/oapi-codegen/oapi-codegen/v2 version 2.4.1 DO NOT EDIT.
package queryservice
import (
"bytes"
"compress/gzip"
"encoding/base64"
"fmt"
"net/http"
"net/url"
"path"
"strings"
"github.com/getkin/kin-openapi/openapi3"
"github.com/labstack/echo/v4"
"github.com/oapi-codegen/runtime"
)
// Defines values for QueryType.
const (
CONTEXTFULL QueryType = "CONTEXT_FULL"
JSONEXTRACTOR QueryType = "JSON_EXTRACTOR"
)
// ExportTrigger Payload for triggering an export.
type ExportTrigger struct {
// ExampleProperty Example property for ExportTrigger.
ExampleProperty *string `json:"example_property,omitempty"`
}
// IdMessage defines model for IdMessage.
type IdMessage struct {
// Id Unique identifier.
Id string `json:"id"`
}
// JobCollector JobCollector model.
type JobCollector struct {
// ExampleProperty Example property for JobCollector.
ExampleProperty *string `json:"example_property,omitempty"`
}
// JobCollectorCreate Payload for creating a JobCollector.
type JobCollectorCreate struct {
// ExampleProperty Example property for JobCollectorCreate.
ExampleProperty *string `json:"example_property,omitempty"`
}
// JobCollectorUpdate Payload for updating a JobCollector.
type JobCollectorUpdate struct {
// ExampleProperty Example property for JobCollectorUpdate.
ExampleProperty *string `json:"example_property,omitempty"`
}
// ListQueries defines model for ListQueries.
type ListQueries struct {
// Queries List of queries.
Queries []Query `json:"queries"`
}
// Query defines model for Query.
type Query struct {
// ActiveVersion The active version of the query.
ActiveVersion int32 `json:"active_version"`
// Config Configuration for the query.
Config *string `json:"config,omitempty"`
// Id Unique identifier for the query.
Id string `json:"id"`
// LatestVersion The latest version of the query.
LatestVersion int32 `json:"latest_version"`
// RequiredQueries List of required query IDs.
RequiredQueries []string `json:"required_queries"`
// Type Specifies the type of the query.
Type QueryType `json:"type"`
}
// QueryCreate defines model for QueryCreate.
type QueryCreate struct {
// Config Configuration for the new query.
Config *string `json:"config,omitempty"`
// RequiredQueries List of required query IDs.
RequiredQueries *[]string `json:"required_queries,omitempty"`
// Type Specifies the type of the query.
Type QueryType `json:"type"`
}
// QueryTestRequest defines model for QueryTestRequest.
type QueryTestRequest struct {
// DocumentId ID of the document to test against.
DocumentId string `json:"document_id"`
// QueryVersion Version of the query to use for testing.
QueryVersion int32 `json:"query_version"`
}
// QueryTestResponse defines model for QueryTestResponse.
type QueryTestResponse struct {
// Value Result of the query test.
Value string `json:"value"`
}
// QueryType Specifies the type of the query.
type QueryType string
// QueryUpdate defines model for QueryUpdate.
type QueryUpdate struct {
// ActiveVersion Updated active version.
ActiveVersion *int32 `json:"active_version,omitempty"`
// Config Updated configuration for the query.
Config *string `json:"config,omitempty"`
// RequiredQueries Updated list of required query IDs.
RequiredQueries *[]string `json:"required_queries,omitempty"`
}
// TriggerExportJSONRequestBody defines body for TriggerExport for application/json ContentType.
type TriggerExportJSONRequestBody = ExportTrigger
// CreateJobCollectorJSONRequestBody defines body for CreateJobCollector for application/json ContentType.
type CreateJobCollectorJSONRequestBody = JobCollectorCreate
// UpdateJobCollectorJSONRequestBody defines body for UpdateJobCollector for application/json ContentType.
type UpdateJobCollectorJSONRequestBody = JobCollectorUpdate
// CreateQueryJSONRequestBody defines body for CreateQuery for application/json ContentType.
type CreateQueryJSONRequestBody = QueryCreate
// UpdateQueryJSONRequestBody defines body for UpdateQuery for application/json ContentType.
type UpdateQueryJSONRequestBody = QueryUpdate
// TestQueryJSONRequestBody defines body for TestQuery for application/json ContentType.
type TestQueryJSONRequestBody = QueryTestRequest
// ServerInterface represents all server handlers.
type ServerInterface interface {
// Trigger an export
// (POST /exports/trigger)
TriggerExport(ctx echo.Context) error
// Create a job collector
// (POST /job-collectors)
CreateJobCollector(ctx echo.Context) error
// Get a job collector by ID
// (GET /job-collectors/{id})
GetJobCollectorById(ctx echo.Context, id string) error
// Update a job collector
// (PUT /job-collectors/{id})
UpdateJobCollector(ctx echo.Context, id string) error
// List queries
// (GET /queries)
ListQueries(ctx echo.Context) error
// Create a new query
// (POST /queries)
CreateQuery(ctx echo.Context) error
// Deprecate a query
// (DELETE /queries/{id})
DeprecateQuery(ctx echo.Context, id string) error
// Get a query by ID
// (GET /queries/{id})
GetQueryById(ctx echo.Context, id string) error
// Update a query
// (PATCH /queries/{id})
UpdateQuery(ctx echo.Context, id string) error
// Test a query
// (POST /queries/{id}/test)
TestQuery(ctx echo.Context, id string) error
}
// ServerInterfaceWrapper converts echo contexts to parameters.
type ServerInterfaceWrapper struct {
Handler ServerInterface
}
// TriggerExport converts echo context to params.
func (w *ServerInterfaceWrapper) TriggerExport(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.TriggerExport(ctx)
return err
}
// CreateJobCollector converts echo context to params.
func (w *ServerInterfaceWrapper) CreateJobCollector(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.CreateJobCollector(ctx)
return err
}
// GetJobCollectorById converts echo context to params.
func (w *ServerInterfaceWrapper) GetJobCollectorById(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter id: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetJobCollectorById(ctx, id)
return err
}
// UpdateJobCollector converts echo context to params.
func (w *ServerInterfaceWrapper) UpdateJobCollector(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter id: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.UpdateJobCollector(ctx, id)
return err
}
// ListQueries converts echo context to params.
func (w *ServerInterfaceWrapper) ListQueries(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.ListQueries(ctx)
return err
}
// CreateQuery converts echo context to params.
func (w *ServerInterfaceWrapper) CreateQuery(ctx echo.Context) error {
var err error
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.CreateQuery(ctx)
return err
}
// DeprecateQuery converts echo context to params.
func (w *ServerInterfaceWrapper) DeprecateQuery(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter id: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.DeprecateQuery(ctx, id)
return err
}
// GetQueryById converts echo context to params.
func (w *ServerInterfaceWrapper) GetQueryById(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter id: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.GetQueryById(ctx, id)
return err
}
// UpdateQuery converts echo context to params.
func (w *ServerInterfaceWrapper) UpdateQuery(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter id: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.UpdateQuery(ctx, id)
return err
}
// TestQuery converts echo context to params.
func (w *ServerInterfaceWrapper) TestQuery(ctx echo.Context) error {
var err error
// ------------- Path parameter "id" -------------
var id string
err = runtime.BindStyledParameterWithOptions("simple", "id", ctx.Param("id"), &id, runtime.BindStyledParameterOptions{ParamLocation: runtime.ParamLocationPath, Explode: false, Required: true})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Invalid format for parameter id: %s", err))
}
// Invoke the callback with all the unmarshaled arguments
err = w.Handler.TestQuery(ctx, id)
return err
}
// This is a simple interface which specifies echo.Route addition functions which
// are present on both echo.Echo and echo.Group, since we want to allow using
// either of them for path registration
type EchoRouter interface {
CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
}
// RegisterHandlers adds each server route to the EchoRouter.
func RegisterHandlers(router EchoRouter, si ServerInterface) {
RegisterHandlersWithBaseURL(router, si, "")
}
// Registers handlers, and prepends BaseURL to the paths, so that the paths
// can be served under a prefix.
func RegisterHandlersWithBaseURL(router EchoRouter, si ServerInterface, baseURL string) {
wrapper := ServerInterfaceWrapper{
Handler: si,
}
router.POST(baseURL+"/exports/trigger", wrapper.TriggerExport)
router.POST(baseURL+"/job-collectors", wrapper.CreateJobCollector)
router.GET(baseURL+"/job-collectors/:id", wrapper.GetJobCollectorById)
router.PUT(baseURL+"/job-collectors/:id", wrapper.UpdateJobCollector)
router.GET(baseURL+"/queries", wrapper.ListQueries)
router.POST(baseURL+"/queries", wrapper.CreateQuery)
router.DELETE(baseURL+"/queries/:id", wrapper.DeprecateQuery)
router.GET(baseURL+"/queries/:id", wrapper.GetQueryById)
router.PATCH(baseURL+"/queries/:id", wrapper.UpdateQuery)
router.POST(baseURL+"/queries/:id/test", wrapper.TestQuery)
}
// Base64 encoded, gzipped, json marshaled Swagger object
var swaggerSpec = []string{
"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",
"wy/PGCEAAA==",
}
// GetSwagger returns the content of the embedded swagger specification file
// or error if failed to decode
func decodeSpec() ([]byte, error) {
zipped, err := base64.StdEncoding.DecodeString(strings.Join(swaggerSpec, ""))
if err != nil {
return nil, fmt.Errorf("error base64 decoding spec: %w", err)
}
zr, err := gzip.NewReader(bytes.NewReader(zipped))
if err != nil {
return nil, fmt.Errorf("error decompressing spec: %w", err)
}
var buf bytes.Buffer
_, err = buf.ReadFrom(zr)
if err != nil {
return nil, fmt.Errorf("error decompressing spec: %w", err)
}
return buf.Bytes(), nil
}
var rawSpec = decodeSpecCached()
// a naive cached of a decoded swagger spec
func decodeSpecCached() func() ([]byte, error) {
data, err := decodeSpec()
return func() ([]byte, error) {
return data, err
}
}
// Constructs a synthetic filesystem for resolving external references when loading openapi specifications.
func PathToRawSpec(pathToFile string) map[string]func() ([]byte, error) {
res := make(map[string]func() ([]byte, error))
if len(pathToFile) > 0 {
res[pathToFile] = rawSpec
}
return res
}
// GetSwagger returns the Swagger specification corresponding to the generated code
// in this file. The external references of Swagger specification are resolved.
// The logic of resolving external references is tightly connected to "import-mapping" feature.
// Externally referenced files must be embedded in the corresponding golang packages.
// Urls can be supported but this task was out of the scope.
func GetSwagger() (swagger *openapi3.T, err error) {
resolvePath := PathToRawSpec("")
loader := openapi3.NewLoader()
loader.IsExternalRefsAllowed = true
loader.ReadFromURIFunc = func(loader *openapi3.Loader, url *url.URL) ([]byte, error) {
pathToFile := url.String()
pathToFile = path.Clean(pathToFile)
getSpec, ok := resolvePath[pathToFile]
if !ok {
err1 := fmt.Errorf("path not found: %s", pathToFile)
return nil, err1
}
return getSpec()
}
var specData []byte
specData, err = rawSpec()
if err != nil {
return
}
swagger, err = loader.LoadFromData(specData)
if err != nil {
return
}
return
}
+27
View File
@@ -0,0 +1,27 @@
package queryservice
import (
"queryorchestration/internal/collector"
"queryorchestration/internal/export"
"queryorchestration/internal/query"
"github.com/go-playground/validator/v10"
)
type Services struct {
Export *export.Service
JobCollector *collector.Service
Query *query.Service
}
type Controllers struct {
validator *validator.Validate
svc *Services
}
func NewControllers(validator *validator.Validate, services *Services) *Controllers {
return &Controllers{
validator: validator,
svc: services,
}
}
+14
View File
@@ -0,0 +1,14 @@
package queryservice_test
import (
queryservice "queryorchestration/api/queryService"
"testing"
"github.com/go-playground/validator/v10"
"github.com/stretchr/testify/assert"
)
func TestNewControllers(t *testing.T) {
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{})
assert.NotNil(t, cons)
}
+11
View File
@@ -0,0 +1,11 @@
package queryservice
import (
"net/http"
"github.com/labstack/echo/v4"
)
func (s *Controllers) TriggerExport(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, map[string]string{"status": "export triggered"})
}
+27
View File
@@ -0,0 +1,27 @@
package queryservice_test
import (
"net/http"
"net/http/httptest"
queryservice "queryorchestration/api/queryService"
"strings"
"testing"
"github.com/go-playground/validator/v10"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestTriggerExport(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{})
err := cons.TriggerExport(ctx)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.NotEmpty(t, rec.Body.String())
}
+19
View File
@@ -0,0 +1,19 @@
package queryservice
import (
"net/http"
"github.com/labstack/echo/v4"
)
func (s *Controllers) GetJobCollectorById(ctx echo.Context, id string) error {
return ctx.JSON(http.StatusOK, map[string]string{"status": "export triggered"})
}
func (s *Controllers) CreateJobCollector(ctx echo.Context) error {
return ctx.JSON(http.StatusOK, map[string]string{"status": "export triggered"})
}
func (s *Controllers) UpdateJobCollector(ctx echo.Context, id string) error {
return ctx.JSON(http.StatusOK, map[string]string{"status": "export triggered"})
}
+60
View File
@@ -0,0 +1,60 @@
package queryservice_test
import (
"net/http"
"net/http/httptest"
queryservice "queryorchestration/api/queryService"
"strings"
"testing"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/stretchr/testify/assert"
)
func TestCreateJobCollector(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{})
err := cons.CreateJobCollector(ctx)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.NotEmpty(t, rec.Body.String())
}
func TestUpdateJobCollector(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{})
id := uuid.New()
err := cons.UpdateJobCollector(ctx, id.String())
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.NotEmpty(t, rec.Body.String())
}
func TestGetJobCollector(t *testing.T) {
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{})
id := uuid.New()
err := cons.GetJobCollectorById(ctx, id.String())
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.NotEmpty(t, rec.Body.String())
}
+72
View File
@@ -0,0 +1,72 @@
package queryservice
import (
"errors"
"queryorchestration/internal/query"
queryprocessor "queryorchestration/internal/queryProcessor"
)
func parseQueries(queries []*query.Query) ([]Query, error) {
outQueries := make([]Query, len(queries))
for index, query := range queries {
qt, err := parseQuery(query)
if err != nil {
return nil, err
}
outQueries[index] = *qt
}
return outQueries, nil
}
func parseQuery(query *query.Query) (*Query, error) {
requiredQueries := make([]string, len(query.RequiredQueryIDs))
for index, id := range query.RequiredQueryIDs {
requiredQueries[index] = id.String()
}
qt, err := parseQueryType(query.Type)
if err != nil {
return nil, err
}
q := &Query{
Id: query.ID.String(),
Type: qt,
ActiveVersion: query.ActiveVersion,
LatestVersion: query.LatestVersion,
}
if query.Config != "" {
q.Config = &query.Config
}
if len(query.RequiredQueryIDs) > 0 {
q.RequiredQueries = requiredQueries
}
return q, nil
}
func parseQueryType(qType queryprocessor.Type) (QueryType, error) {
switch qType {
case queryprocessor.TypeJsonExtractor:
return JSONEXTRACTOR, nil
case queryprocessor.TypeContextFull:
return CONTEXTFULL, nil
default:
return "", errors.New("invalid query type")
}
}
func parseSpecQueryType(qType QueryType) (queryprocessor.Type, error) {
switch qType {
case JSONEXTRACTOR:
return queryprocessor.TypeJsonExtractor, nil
case CONTEXTFULL:
return queryprocessor.TypeContextFull, nil
default:
return queryprocessor.Type(-1), errors.New("invalid query type")
}
}
+92
View File
@@ -0,0 +1,92 @@
package queryservice
import (
"queryorchestration/internal/query"
queryprocessor "queryorchestration/internal/queryProcessor"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/assert"
)
func TestParseQueries(t *testing.T) {
in := []*query.Query{
{
ID: uuid.New(),
Type: queryprocessor.TypeContextFull,
ActiveVersion: 1,
LatestVersion: 2,
RequiredQueryIDs: []uuid.UUID{
uuid.New(),
},
Config: "hey",
},
}
out, err := parseQueries(in)
assert.Nil(t, err)
assert.Len(t, out, len(in))
assert.ElementsMatch(t, []Query{
{
Id: in[0].ID.String(),
Type: CONTEXTFULL,
ActiveVersion: 1,
LatestVersion: 2,
RequiredQueries: []string{
in[0].RequiredQueryIDs[0].String(),
},
Config: &in[0].Config,
},
}, out)
}
func TestParseQuery(t *testing.T) {
in := &query.Query{
ID: uuid.New(),
Type: queryprocessor.TypeContextFull,
ActiveVersion: 1,
LatestVersion: 2,
RequiredQueryIDs: []uuid.UUID{
uuid.New(),
},
Config: "hey",
}
out, err := parseQuery(in)
assert.Nil(t, err)
assert.EqualExportedValues(t, Query{
Id: in.ID.String(),
Type: CONTEXTFULL,
ActiveVersion: 1,
LatestVersion: 2,
RequiredQueries: []string{
in.RequiredQueryIDs[0].String(),
},
Config: &in.Config,
},
*out)
}
func TestParseQueryType(t *testing.T) {
qt, err := parseQueryType(queryprocessor.TypeContextFull)
assert.Nil(t, err)
assert.Equal(t, CONTEXTFULL, qt)
qt, err = parseQueryType(queryprocessor.TypeJsonExtractor)
assert.Nil(t, err)
assert.Equal(t, JSONEXTRACTOR, qt)
_, err = parseQueryType(queryprocessor.Type(-1))
assert.NotNil(t, err)
}
func TestParseSpecQueryType(t *testing.T) {
qt, err := parseSpecQueryType(CONTEXTFULL)
assert.Nil(t, err)
assert.Equal(t, queryprocessor.Type(queryprocessor.TypeContextFull), qt)
qt, err = parseSpecQueryType(JSONEXTRACTOR)
assert.Nil(t, err)
assert.Equal(t, queryprocessor.Type(queryprocessor.TypeJsonExtractor), qt)
_, err = parseSpecQueryType("invalid")
assert.NotNil(t, err)
}
+143
View File
@@ -0,0 +1,143 @@
package queryservice
import (
"fmt"
"net/http"
"queryorchestration/internal/query"
queryprocessor "queryorchestration/internal/queryProcessor"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
)
func (s *Controllers) ListQueries(ctx echo.Context) error {
filters := query.ListFilters{}
queries, err := s.svc.Query.List(ctx.Request().Context(), filters)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to list query: %s", err))
}
outQueries, err := parseQueries(queries)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to list query: %s", err))
}
return ctx.JSON(http.StatusOK, ListQueries{
Queries: outQueries,
})
}
func (s *Controllers) GetQueryById(ctx echo.Context, id string) error {
uid, err := uuid.Parse(id)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
query, err := s.svc.Query.Get(ctx.Request().Context(), uid)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to get query: %s", err))
}
qt, err := parseQuery(query)
if err != nil {
return echo.NewHTTPError(http.StatusNotFound, fmt.Sprintf("Unable to get query: %s", err))
}
return ctx.JSON(http.StatusOK, qt)
}
func (s *Controllers) CreateQuery(ctx echo.Context) error {
req := QueryCreate{}
if err := ctx.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
requiredQueryIDs := []uuid.UUID{}
if req.RequiredQueries != nil {
requiredQueryIDs := make([]uuid.UUID, len(*req.RequiredQueries))
for index, id := range *req.RequiredQueries {
parsedID, err := uuid.Parse(id)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Required ID")
}
requiredQueryIDs[index] = parsedID
}
}
qt, err := parseSpecQueryType(req.Type)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Query Type")
}
id, err := s.svc.Query.Create(ctx.Request().Context(), &queryprocessor.Create{
Type: qt,
RequiredQueryIDs: requiredQueryIDs,
})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to create query: %s", err))
}
return ctx.JSON(http.StatusCreated, IdMessage{
Id: id.String(),
})
}
func (s *Controllers) UpdateQuery(ctx echo.Context, id string) error {
uid, err := uuid.Parse(id)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
err = s.svc.Query.Update(ctx.Request().Context(), &queryprocessor.Update{
ID: uid,
})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to update query: %s", err))
}
return ctx.NoContent(http.StatusOK)
}
func (s *Controllers) DeprecateQuery(ctx echo.Context, id string) error {
uid, err := uuid.Parse(id)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid ID")
}
err = s.svc.Query.Deprecate(ctx.Request().Context(), uid)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to deprecate query: %s", err))
}
return ctx.NoContent(http.StatusOK)
}
func (s *Controllers) TestQuery(ctx echo.Context, id string) error {
queryId, err := uuid.Parse(id)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Query ID")
}
req := QueryTestRequest{}
if err := ctx.Bind(&req); err != nil {
return echo.NewHTTPError(http.StatusBadRequest, err)
}
docId, err := uuid.Parse(req.DocumentId)
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, "Invalid Doc ID")
}
value, err := s.svc.Query.Test(ctx.Request().Context(), query.Test{
QueryID: queryId,
QueryVersion: req.QueryVersion,
DocumentID: docId,
})
if err != nil {
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Unable to test query: %s", err))
}
return ctx.JSON(http.StatusOK, QueryTestResponse{
Value: value,
})
}
+267
View File
@@ -0,0 +1,267 @@
package queryservice_test
import (
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
queryservice "queryorchestration/api/queryService"
"queryorchestration/internal/database"
"queryorchestration/internal/database/repository"
"queryorchestration/internal/query"
"strings"
"testing"
"github.com/go-playground/validator/v10"
"github.com/google/uuid"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgtype"
"github.com/labstack/echo/v4"
"github.com/pashagolub/pgxmock/v3"
"github.com/stretchr/testify/assert"
)
func TestDeprecateQuery(t *testing.T) {
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
Query: query.New(db),
})
id := uuid.New()
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
pool.ExpectQuery("-- name: IsQueryDeprecated :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
pgxmock.NewRows([]string{"isdeprecated"}).
AddRow(true),
)
err = cons.DeprecateQuery(ctx, id.String())
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Empty(t, rec.Body.String())
}
func TestCreateQuery(t *testing.T) {
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
Query: query.New(db),
})
body := queryservice.QueryCreate{
Type: queryservice.CONTEXTFULL,
}
bodyBytes, err := json.Marshal(body)
assert.Nil(t, err)
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
id := uuid.New()
pool.ExpectBeginTx(pgx.TxOptions{})
pool.ExpectQuery("-- name: CreateQuery :one").WithArgs(repository.QuerytypeContextFull).WillReturnRows(
pgxmock.NewRows([]string{"id"}).
AddRow(database.MustToDBUUID(id)),
)
pool.ExpectCommit()
err = cons.CreateQuery(ctx)
assert.Nil(t, err)
assert.Equal(t, http.StatusCreated, rec.Code)
assert.Equal(t, fmt.Sprintf("{\"id\":\"%s\"}\n", id), rec.Body.String())
}
func TestListQueries(t *testing.T) {
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
Query: query.New(db),
})
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
id := uuid.New()
pool.ExpectQuery("-- name: ListQueries :many").WithArgs().WillReturnRows(
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
AddRow(database.MustToDBUUID(id), repository.QuerytypeContextFull, int32(1), int32(2), []byte(""), []pgtype.UUID{}),
)
err = cons.ListQueries(ctx)
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryservice.ListQueries
err = json.Unmarshal(rec.Body.Bytes(), &res)
assert.Nil(t, err)
assert.NotNil(t, res.Queries)
assert.ElementsMatch(t, res.Queries, []queryservice.Query{
{
Id: id.String(),
Type: queryservice.CONTEXTFULL,
ActiveVersion: 1,
LatestVersion: 2,
},
})
}
func TestGetQueryById(t *testing.T) {
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
Query: query.New(db),
})
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(""))
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
id := uuid.New()
ctx.Set("id", id)
pool.ExpectQuery("-- name: GetQuery :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
AddRow(database.MustToDBUUID(id), repository.QuerytypeContextFull, int32(1), int32(2), []byte(""), []pgtype.UUID{}),
)
err = cons.GetQueryById(ctx, id.String())
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryservice.Query
err = json.Unmarshal(rec.Body.Bytes(), &res)
assert.Nil(t, err)
assert.EqualExportedValues(t, queryservice.Query{
Id: id.String(),
Type: queryservice.CONTEXTFULL,
ActiveVersion: 1,
LatestVersion: 2,
}, res)
}
func TestUpdateQuery(t *testing.T) {
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
Query: query.New(db),
})
body := queryservice.QueryUpdate{}
bodyBytes, err := json.Marshal(body)
assert.Nil(t, err)
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
id := uuid.New()
ctx.Set("id", id)
pool.ExpectQuery("-- name: GetQuery :one").WithArgs(database.MustToDBUUID(id)).WillReturnRows(
pgxmock.NewRows([]string{"id", "type", "activeVersion", "latestVersion", "config", "requiredIds"}).
AddRow(database.MustToDBUUID(id), repository.QuerytypeContextFull, int32(1), int32(2), []byte(""), []pgtype.UUID{}),
)
err = cons.UpdateQuery(ctx, id.String())
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
assert.Empty(t, rec.Body.String())
}
func TestTestQuery(t *testing.T) {
pool, err := pgxmock.NewPool()
if err != nil {
t.Fatalf("failed to open pgxmock database: %v", err)
}
queries := repository.New(pool)
db := &database.Connection{
Queries: queries,
Pool: pool,
}
cons := queryservice.NewControllers(validator.New(), &queryservice.Services{
Query: query.New(db),
})
body := queryservice.QueryTestRequest{
DocumentId: uuid.NewString(),
}
bodyBytes, err := json.Marshal(body)
assert.Nil(t, err)
e := echo.New()
req := httptest.NewRequest(http.MethodPost, "/", strings.NewReader(string(bodyBytes)))
req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
rec := httptest.NewRecorder()
ctx := e.NewContext(req, rec)
id := uuid.New()
err = cons.TestQuery(ctx, id.String())
assert.Nil(t, err)
assert.Equal(t, http.StatusOK, rec.Code)
var res queryservice.QueryTestResponse
err = json.Unmarshal(rec.Body.Bytes(), &res)
assert.Nil(t, err)
assert.EqualExportedValues(t, queryservice.QueryTestResponse{
Value: "",
}, res)
}