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===================================
30 "github.com/deepmap/oapi-codegen/pkg/testutil"
31 "github.com/getkin/kin-openapi/openapi3"
32 "github.com/labstack/echo/v4"
33 "github.com/stretchr/testify/assert"
34 log "github.com/sirupsen/logrus"
36 "oransc.org/nonrtric/servicemanager/internal/common29122"
37 "oransc.org/nonrtric/servicemanager/internal/envreader"
38 "oransc.org/nonrtric/servicemanager/internal/kongclear"
40 "oransc.org/nonrtric/capifcore"
41 "oransc.org/nonrtric/servicemanager/mockkong"
45 eServiceManager *echo.Echo
48 mockConfigReader *envreader.MockConfigReader
49 serviceManagerServer *httptest.Server
50 capifServer *httptest.Server
51 mockKongServer *httptest.Server
54 // Init code to run before tests
55 func TestMain(m *testing.M) {
68 func setupTest() error {
69 // Start the mock Kong server
71 mockKong.RegisterHandlers(eKong)
72 mockKongServer = httptest.NewServer(eKong)
74 // Parse the server URL
75 parsedMockKongURL, err := url.Parse(mockKongServer.URL)
77 log.Fatalf("error parsing mock Kong URL: %v", err)
81 // Extract the host and port
82 mockKongHost := parsedMockKongURL.Hostname()
83 mockKongControlPlanePort := parsedMockKongURL.Port()
85 eCapifWeb = echo.New()
86 capifcore.RegisterHandlers(eCapifWeb, nil, nil)
87 capifServer = httptest.NewServer(eCapifWeb)
89 // Parse the server URL
90 parsedCapifURL, err := url.Parse(capifServer.URL)
92 log.Fatalf("error parsing mock Kong URL: %v", err)
96 // Extract the host and port
97 capifHost := parsedCapifURL.Hostname()
98 capifPort := parsedCapifURL.Port()
100 // Set up the mock config reader with the desired configuration for testing
101 mockConfigReader = &envreader.MockConfigReader{
102 MockedConfig: map[string]string{
103 "KONG_DOMAIN": "kong",
104 "KONG_PROTOCOL": "http",
105 "KONG_IPV4": mockKongHost,
106 "KONG_DATA_PLANE_PORT": "32080",
107 "KONG_CONTROL_PLANE_PORT": mockKongControlPlanePort,
108 "CAPIF_PROTOCOL": "http",
109 "CAPIF_IPV4": capifHost,
110 "CAPIF_PORT": capifPort,
112 "SERVICE_MANAGER_PORT": "8095",
113 "TEST_SERVICE_IPV4": "10.101.1.101",
114 "TEST_SERVICE_PORT": "30951",
118 myEnv, myPorts, err := mockConfigReader.ReadDotEnv()
120 log.Fatal("error loading environment file on setupTest")
124 eServiceManager = echo.New()
125 err = registerHandlers(eServiceManager, myEnv, myPorts)
127 log.Fatal("registerHandlers fatal error on setupTest")
130 serviceManagerServer = httptest.NewServer(eServiceManager)
136 func teardown() error {
137 log.Trace("entering teardown")
139 myEnv, myPorts, err := mockConfigReader.ReadDotEnv()
141 log.Fatal("error loading environment file")
145 err = kongclear.KongClear(myEnv, myPorts)
147 log.Fatal("error clearing Kong on teardown")
150 mockKongServer.Close()
152 serviceManagerServer.Close()
158 func Test_routing(t *testing.T) {
169 name: "Default path",
172 returnStatus: http.StatusOK,
177 name: "Provider path",
179 url: "/api-provider-management/v1/registrations/provider",
180 returnStatus: http.StatusNoContent,
185 name: "Publish path",
187 url: "/published-apis/v1/apfId/service-apis/serviceId",
188 returnStatus: http.StatusNotFound,
193 name: "Discover path",
195 url: "/service-apis/v1/allServiceAPIs?api-invoker-id=api_invoker_id",
196 returnStatus: http.StatusNotFound,
201 name: "Invoker path",
203 url: "/api-invoker-management/v1/onboardedInvokers/invoker",
204 returnStatus: http.StatusNoContent,
209 for _, tt := range tests {
210 t.Run(tt.name, func(t *testing.T) {
211 var result *testutil.CompletedRequest
212 if tt.args.method == "GET" {
213 result = testutil.NewRequest().Get(tt.args.url).Go(t, eServiceManager)
214 } else if tt.args.method == "DELETE" {
215 result = testutil.NewRequest().Delete(tt.args.url).Go(t, eServiceManager)
218 assert.Equal(t, tt.args.returnStatus, result.Code(), tt.name)
223 func TestGetSwagger(t *testing.T) {
233 name: "Provider api",
254 name: "Discover api",
261 for _, tt := range tests {
262 t.Run(tt.name, func(t *testing.T) {
263 result := testutil.NewRequest().Get("/swagger/"+tt.args.apiPath).Go(t, eServiceManager)
264 assert.Equal(t, http.StatusOK, result.Code())
265 var swaggerResponse openapi3.T
266 err := result.UnmarshalJsonToObject(&swaggerResponse)
268 assert.Contains(t, swaggerResponse.Info.Title, tt.args.apiName)
271 invalidApi := "foobar"
272 result := testutil.NewRequest().Get("/swagger/"+invalidApi).Go(t, eServiceManager)
273 assert.Equal(t, http.StatusBadRequest, result.Code())
274 var errorResponse common29122.ProblemDetails
275 err := result.UnmarshalJsonToObject(&errorResponse)
277 assert.Contains(t, *errorResponse.Cause, "Invalid API")
278 assert.Contains(t, *errorResponse.Cause, invalidApi)