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_CONTROL_PLANE_IPV4": mockKongHost,
106 "KONG_CONTROL_PLANE_PORT": mockKongControlPlanePort,
107 "KONG_DATA_PLANE_IPV4": "10.101.1.101",
108 "KONG_DATA_PLANE_PORT": "32080",
109 "CAPIF_PROTOCOL": "http",
110 "CAPIF_IPV4": capifHost,
111 "CAPIF_PORT": capifPort,
113 "SERVICE_MANAGER_PORT": "8095",
114 "TEST_SERVICE_IPV4": "10.101.1.101",
115 "TEST_SERVICE_PORT": "30951",
119 myEnv, myPorts, err := mockConfigReader.ReadDotEnv()
121 log.Fatal("error loading environment file on setupTest")
125 eServiceManager = echo.New()
126 err = registerHandlers(eServiceManager, myEnv, myPorts)
128 log.Fatal("registerHandlers fatal error on setupTest")
131 serviceManagerServer = httptest.NewServer(eServiceManager)
137 func teardown() error {
138 log.Trace("entering teardown")
140 myEnv, myPorts, err := mockConfigReader.ReadDotEnv()
142 log.Fatal("error loading environment file")
146 err = kongclear.KongClear(myEnv, myPorts)
148 log.Fatal("error clearing Kong on teardown")
151 mockKongServer.Close()
153 serviceManagerServer.Close()
159 func Test_routing(t *testing.T) {
170 name: "Default path",
173 returnStatus: http.StatusOK,
178 name: "Provider path",
180 url: "/api-provider-management/v1/registrations/provider",
181 returnStatus: http.StatusNoContent,
186 name: "Publish path",
188 url: "/published-apis/v1/apfId/service-apis/serviceId",
189 returnStatus: http.StatusNotFound,
194 name: "Discover path",
196 url: "/service-apis/v1/allServiceAPIs?api-invoker-id=api_invoker_id",
197 returnStatus: http.StatusNotFound,
202 name: "Invoker path",
204 url: "/api-invoker-management/v1/onboardedInvokers/invoker",
205 returnStatus: http.StatusNoContent,
210 for _, tt := range tests {
211 t.Run(tt.name, func(t *testing.T) {
212 var result *testutil.CompletedRequest
213 if tt.args.method == "GET" {
214 result = testutil.NewRequest().Get(tt.args.url).Go(t, eServiceManager)
215 } else if tt.args.method == "DELETE" {
216 result = testutil.NewRequest().Delete(tt.args.url).Go(t, eServiceManager)
219 assert.Equal(t, tt.args.returnStatus, result.Code(), tt.name)
224 func TestGetSwagger(t *testing.T) {
234 name: "Provider api",
255 name: "Discover api",
262 for _, tt := range tests {
263 t.Run(tt.name, func(t *testing.T) {
264 result := testutil.NewRequest().Get("/swagger/"+tt.args.apiPath).Go(t, eServiceManager)
265 assert.Equal(t, http.StatusOK, result.Code())
266 var swaggerResponse openapi3.T
267 err := result.UnmarshalJsonToObject(&swaggerResponse)
269 assert.Contains(t, swaggerResponse.Info.Title, tt.args.apiName)
272 invalidApi := "foobar"
273 result := testutil.NewRequest().Get("/swagger/"+invalidApi).Go(t, eServiceManager)
274 assert.Equal(t, http.StatusBadRequest, result.Code())
275 var errorResponse common29122.ProblemDetails
276 err := result.UnmarshalJsonToObject(&errorResponse)
278 assert.Contains(t, *errorResponse.Cause, "Invalid API")
279 assert.Contains(t, *errorResponse.Cause, invalidApi)