From b4da1f981ba6717f5ec52e15ad1db257b5d6b7f3 Mon Sep 17 00:00:00 2001 From: elinuxhenrik Date: Wed, 23 Nov 2022 08:31:43 +0100 Subject: [PATCH] Add get swagger path for APIs Issue-ID: NONRTRIC-814 Signed-off-by: elinuxhenrik Change-Id: I2ee14eb538adc7663f1200c003757d42de2eef28 --- capifcore/main.go | 36 +++++++++++++++++++++++++- capifcore/main_test.go | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+), 1 deletion(-) diff --git a/capifcore/main.go b/capifcore/main.go index 37137d7..ddba0dd 100644 --- a/capifcore/main.go +++ b/capifcore/main.go @@ -25,8 +25,10 @@ import ( "fmt" "net/http" + "github.com/getkin/kin-openapi/openapi3" "github.com/labstack/echo/v4" "helm.sh/helm/v3/pkg/cli" + "oransc.org/nonrtric/capifcore/internal/common29122" "oransc.org/nonrtric/capifcore/internal/discoverserviceapi" "oransc.org/nonrtric/capifcore/internal/invokermanagementapi" "oransc.org/nonrtric/capifcore/internal/providermanagementapi" @@ -125,7 +127,7 @@ func getEcho() *echo.Echo { discoverserviceapi.RegisterHandlersWithBaseURL(e, discoverService, "/service-apis/v1") // Register Security - securitySwagger, err := publishserviceapi.GetSwagger() + securitySwagger, err := securityapi.GetSwagger() if err != nil { log.Fatalf("Error loading Security swagger spec\n: %s", err) } @@ -137,6 +139,8 @@ func getEcho() *echo.Echo { e.GET("/", hello) + e.GET("/swagger/:apiName", getSwagger) + return e } @@ -152,3 +156,33 @@ func keepServerAlive() { func hello(c echo.Context) error { return c.String(http.StatusOK, "Hello, World!\n") } + +func getSwagger(c echo.Context) error { + var swagger *openapi3.T + var err error + switch api := c.Param("apiName"); api { + case "provider": + swagger, err = providermanagementapi.GetSwagger() + case "publish": + swagger, err = publishserviceapi.GetSwagger() + case "invoker": + swagger, err = invokermanagementapi.GetSwagger() + case "discover": + swagger, err = discoverserviceapi.GetSwagger() + case "security": + swagger, err = securityapi.GetSwagger() + default: + return c.JSON(http.StatusBadRequest, getProblemDetails("Invalid API name "+api, http.StatusBadRequest)) + } + if err != nil { + return c.JSON(http.StatusInternalServerError, getProblemDetails("Unable to get swagger for API", http.StatusInternalServerError)) + } + return c.JSON(http.StatusOK, swagger) +} + +func getProblemDetails(cause string, status int) common29122.ProblemDetails { + return common29122.ProblemDetails{ + Cause: &cause, + Status: &status, + } +} diff --git a/capifcore/main_test.go b/capifcore/main_test.go index 1894516..12092c1 100644 --- a/capifcore/main_test.go +++ b/capifcore/main_test.go @@ -25,8 +25,10 @@ import ( "testing" "github.com/deepmap/oapi-codegen/pkg/testutil" + "github.com/getkin/kin-openapi/openapi3" "github.com/labstack/echo/v4" "github.com/stretchr/testify/assert" + "oransc.org/nonrtric/capifcore/internal/common29122" ) var e *echo.Echo @@ -105,3 +107,70 @@ func Test_routing(t *testing.T) { }) } } + +func TestGetSwagger(t *testing.T) { + e = getEcho() + + type args struct { + apiPath string + apiName string + } + tests := []struct { + name string + args args + }{ + { + name: "Provider api", + args: args{ + apiPath: "provider", + apiName: "Provider", + }, + }, + { + name: "Publish api", + args: args{ + apiPath: "publish", + apiName: "Publish", + }, + }, + { + name: "Invoker api", + args: args{ + apiPath: "invoker", + apiName: "Invoker", + }, + }, + { + name: "Discover api", + args: args{ + apiPath: "discover", + apiName: "Discover", + }, + }, + { + name: "Security api", + args: args{ + apiPath: "security", + apiName: "Security", + }, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + result := testutil.NewRequest().Get("/swagger/"+tt.args.apiPath).Go(t, e) + assert.Equal(t, http.StatusOK, result.Code()) + var swaggerResponse openapi3.T + err := result.UnmarshalJsonToObject(&swaggerResponse) + assert.Nil(t, err) + assert.Contains(t, swaggerResponse.Info.Title, tt.args.apiName) + }) + } + invalidApi := "foobar" + result := testutil.NewRequest().Get("/swagger/"+invalidApi).Go(t, e) + assert.Equal(t, http.StatusBadRequest, result.Code()) + var errorResponse common29122.ProblemDetails + err := result.UnmarshalJsonToObject(&errorResponse) + assert.Nil(t, err) + assert.Contains(t, *errorResponse.Cause, "Invalid API") + assert.Contains(t, *errorResponse.Cause, invalidApi) +} -- 2.16.6