2 // ========================LICENSE_START=================================
5 // Copyright (C) 2023-2024: OpenInfra Foundation Europe
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
28 "github.com/deepmap/oapi-codegen/pkg/testutil"
29 "github.com/getkin/kin-openapi/openapi3"
30 "github.com/labstack/echo/v4"
31 "github.com/stretchr/testify/assert"
32 log "github.com/sirupsen/logrus"
34 "oransc.org/nonrtric/servicemanager/internal/common29122"
35 "oransc.org/nonrtric/servicemanager/internal/envreader"
39 var myPorts map [string]int
41 func TestMain(m *testing.M) {
42 // Init code to run before tests
43 myEnv, myPorts, err := envreader.ReadDotEnv()
45 log.Fatal("error loading environment file")
49 e, err = getEcho(myEnv, myPorts)
51 log.Fatal("getEcho fatal error")
58 // Finalization code to run after tests
60 // Exit with exit value from tests
65 func Test_routing(t *testing.T) {
79 returnStatus: http.StatusOK,
84 name: "Provider path",
86 url: "/api-provider-management/v1/registrations/provider",
87 returnStatus: http.StatusNoContent,
94 url: "/published-apis/v1/apfId/service-apis/serviceId",
95 returnStatus: http.StatusNotFound,
100 name: "Discover path",
102 url: "/service-apis/v1/allServiceAPIs?api-invoker-id=api_invoker_id",
103 returnStatus: http.StatusNotFound,
108 name: "Invoker path",
110 url: "/api-invoker-management/v1/onboardedInvokers/invoker",
111 returnStatus: http.StatusNoContent,
116 for _, tt := range tests {
117 t.Run(tt.name, func(t *testing.T) {
118 var result *testutil.CompletedRequest
119 if tt.args.method == "GET" {
120 result = testutil.NewRequest().Get(tt.args.url).Go(t, e)
121 } else if tt.args.method == "DELETE" {
122 result = testutil.NewRequest().Delete(tt.args.url).Go(t, e)
125 assert.Equal(t, tt.args.returnStatus, result.Code(), tt.name)
130 func TestGetSwagger(t *testing.T) {
140 name: "Provider api",
161 name: "Discover api",
168 for _, tt := range tests {
169 t.Run(tt.name, func(t *testing.T) {
170 result := testutil.NewRequest().Get("/swagger/"+tt.args.apiPath).Go(t, e)
171 assert.Equal(t, http.StatusOK, result.Code())
172 var swaggerResponse openapi3.T
173 err := result.UnmarshalJsonToObject(&swaggerResponse)
175 assert.Contains(t, swaggerResponse.Info.Title, tt.args.apiName)
178 invalidApi := "foobar"
179 result := testutil.NewRequest().Get("/swagger/"+invalidApi).Go(t, e)
180 assert.Equal(t, http.StatusBadRequest, result.Code())
181 var errorResponse common29122.ProblemDetails
182 err := result.UnmarshalJsonToObject(&errorResponse)
184 assert.Contains(t, *errorResponse.Cause, "Invalid API")
185 assert.Contains(t, *errorResponse.Cause, invalidApi)