NONRTRIC-946: Servicemanager - mock kong and capif as library
[nonrtric/plt/sme.git] / servicemanager / internal / publishservice / publishservice_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2023-2024: OpenInfra Foundation Europe
6 //   %%
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
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
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===================================
19 //
20
21 package publishservice
22
23 import (
24         "fmt"
25         "net/http"
26         "net/http/httptest"
27         "net/url"
28         "os"
29         "strings"
30         "testing"
31
32         "github.com/deepmap/oapi-codegen/pkg/middleware"
33         "github.com/deepmap/oapi-codegen/pkg/testutil"
34         echo "github.com/labstack/echo/v4"
35         echomiddleware "github.com/labstack/echo/v4/middleware"
36         log "github.com/sirupsen/logrus"
37         "github.com/stretchr/testify/assert"
38
39         "oransc.org/nonrtric/servicemanager/internal/envreader"
40         "oransc.org/nonrtric/servicemanager/internal/kongclear"
41
42         "oransc.org/nonrtric/servicemanager/internal/common29122"
43         "oransc.org/nonrtric/servicemanager/internal/providermanagement"
44         provapi "oransc.org/nonrtric/servicemanager/internal/providermanagementapi"
45         publishapi "oransc.org/nonrtric/servicemanager/internal/publishserviceapi"
46
47         "oransc.org/nonrtric/capifcore"
48         "oransc.org/nonrtric/servicemanager/mockkong"
49 )
50
51 var (
52         eServiceManager          *echo.Echo
53         eCapifWeb                *echo.Echo
54         eKong                    *echo.Echo
55         mockConfigReader         *envreader.MockConfigReader
56         serviceManagerServer *httptest.Server
57         capifServer              *httptest.Server
58         mockKongServer           *httptest.Server
59 )
60
61 func TestMain(m *testing.M) {
62         err := setupTest()
63         if err != nil {
64                 return
65         }
66
67         ret := m.Run()
68         if ret == 0 {
69                 teardown()
70         }
71         os.Exit(ret)
72 }
73
74 func setupTest() error {
75         // Start the mock Kong server
76         eKong = echo.New()
77         mockKong.RegisterHandlers(eKong)
78         mockKongServer = httptest.NewServer(eKong)
79
80         // Parse the server URL
81         parsedMockKongURL, err := url.Parse(mockKongServer.URL)
82         if err != nil {
83                 log.Fatalf("error parsing mock Kong URL: %v", err)
84                 return err
85         }
86
87         // Extract the host and port
88         mockKongHost := parsedMockKongURL.Hostname()
89         mockKongControlPlanePort := parsedMockKongURL.Port()
90
91         eCapifWeb = echo.New()
92         capifcore.RegisterHandlers(eCapifWeb, nil, nil)
93         capifServer = httptest.NewServer(eCapifWeb)
94
95         // Parse the server URL
96         parsedCapifURL, err := url.Parse(capifServer.URL)
97         if err != nil {
98                 log.Fatalf("error parsing mock Kong URL: %v", err)
99                 return err
100         }
101
102         // Extract the host and port
103         capifHost := parsedCapifURL.Hostname()
104         capifPort := parsedCapifURL.Port()
105
106         // Set up the mock config reader with the desired configuration for testing
107         mockConfigReader = &envreader.MockConfigReader{
108                 MockedConfig: map[string]string{
109                         "KONG_DOMAIN":             "kong",
110                         "KONG_PROTOCOL":           "http",
111                         "KONG_IPV4":               mockKongHost,
112                         "KONG_DATA_PLANE_PORT":    "32080",
113                         "KONG_CONTROL_PLANE_PORT": mockKongControlPlanePort,
114                         "CAPIF_PROTOCOL":          "http",
115                         "CAPIF_IPV4":              capifHost,
116                         "CAPIF_PORT":              capifPort,
117                         "LOG_LEVEL":               "Info",
118                         "SERVICE_MANAGER_PORT":    "8095",
119                         "TEST_SERVICE_IPV4":       "10.101.1.101",
120                         "TEST_SERVICE_PORT":       "30951",
121                 },
122         }
123
124         // Use the mock implementation for testing
125         myEnv, myPorts, err := mockConfigReader.ReadDotEnv()
126         if err != nil {
127                 log.Fatalf("error reading mock config: %v", err)
128         }
129
130         eServiceManager = echo.New()
131         err = registerHandlers(eServiceManager, myEnv, myPorts)
132         if err != nil {
133                 log.Fatal("registerHandlers fatal error on setupTest")
134                 return err
135         }
136         serviceManagerServer = httptest.NewServer(eServiceManager)
137         capifCleanUp()
138         return err
139 }
140
141 func getProvider() provapi.APIProviderEnrolmentDetails {
142         var (
143                 domainInfo  = "Kong"
144                 funcInfoAPF = "rApp Kong as APF"
145                 funcInfoAEF = "rApp Kong as AEF"
146         )
147
148         testFuncs := []provapi.APIProviderFunctionDetails{
149                 {
150                         ApiProvFuncInfo: &funcInfoAPF,
151                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
152                         RegInfo: provapi.RegistrationInformation{
153                                 ApiProvPubKey: "APF-PublicKey",
154                         },
155                 },
156                 {
157                         ApiProvFuncInfo: &funcInfoAEF,
158                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
159                         RegInfo: provapi.RegistrationInformation{
160                                 ApiProvPubKey: "AEF-PublicKey",
161                         },
162                 },
163         }
164         return provapi.APIProviderEnrolmentDetails{
165                 RegSec:         "sec",
166                 ApiProvDomInfo: &domainInfo,
167                 ApiProvFuncs:   &testFuncs,
168         }
169 }
170
171 func capifCleanUp()  {
172         t := new(testing.T) // Create a new testing.T instance for capifCleanUp
173
174         // Delete the invoker
175         invokerInfo := "invoker a"
176         invokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1)
177
178         result := testutil.NewRequest().Delete("/api-invoker-management/v1/onboardedInvokers/"+invokerId).Go(t, eServiceManager)
179         assert.Equal(t, http.StatusNoContent, result.Code())
180
181         // Delete the original published service
182         apfId := "APF_id_rApp_Kong_as_APF"
183         apiName := "apiName"
184         apiId := "api_id_" + apiName
185
186         result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, eServiceManager)
187         assert.Equal(t, http.StatusNoContent, result.Code())
188
189         // Delete the first published service
190         apfId = "APF_id_rApp_Kong_as_APF"
191         apiName = "apiName1"
192         apiId = "api_id_" + apiName
193
194         result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, eServiceManager)
195         assert.Equal(t, http.StatusNoContent, result.Code())
196
197         // Delete the second published service
198         apiName = "apiName2"
199         apiId = "api_id_" + apiName
200
201         result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, eServiceManager)
202         assert.Equal(t, http.StatusNoContent, result.Code())
203
204         // Delete the provider
205         domainID := "domain_id_Kong"
206         result = testutil.NewRequest().Delete("/api-provider-management/v1/registrations/"+domainID).Go(t, eServiceManager)
207         assert.Equal(t, http.StatusNoContent, result.Code())
208 }
209
210 func teardown() error {
211         log.Trace("entering teardown")
212
213         myEnv, myPorts, err := mockConfigReader.ReadDotEnv()
214         if err != nil {
215                 log.Fatal("error loading environment file")
216                 return err
217         }
218
219         err = kongclear.KongClear(myEnv, myPorts)
220         if err != nil {
221                 log.Fatal("error clearing Kong on teardown")
222         }
223
224         mockKongServer.Close()
225         capifServer.Close()
226         serviceManagerServer.Close()
227
228         return nil
229 }
230
231 func TestPostUnpublishedServiceWithUnregisteredPublisher(t *testing.T) {
232         capifCleanUp()
233
234         apfId := "APF_id_rApp_Kong_as_APF"
235
236         // Check no services published
237         result := testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, eServiceManager)
238         assert.Equal(t, http.StatusNotFound, result.Code())
239
240         var resultError common29122.ProblemDetails
241         err := result.UnmarshalBodyToObject(&resultError)
242         assert.NoError(t, err, "error unmarshaling response")
243
244         assert.Contains(t, *resultError.Cause, apfId)
245         assert.Contains(t, *resultError.Cause, "api is only available for publishers")
246
247         aefId := "AEF_id_rApp_Kong_as_AEF"
248         namespace := "namespace"
249         repoName := "repoName"
250         chartName := "chartName"
251         releaseName := "releaseName"
252         description := fmt.Sprintf("Description,%s,%s,%s,%s", namespace, repoName, chartName, releaseName)
253
254         myEnv, myPorts, err := mockConfigReader.ReadDotEnv()
255         assert.Nil(t, err, "error reading env file")
256
257         testServiceIpv4 := common29122.Ipv4Addr(myEnv["TEST_SERVICE_IPV4"])
258         testServicePort := common29122.Port(myPorts["TEST_SERVICE_PORT"])
259
260         assert.NotEmpty(t, testServiceIpv4, "TEST_SERVICE_IPV4 is required in .env file for unit testing")
261         assert.NotZero(t, testServicePort, "TEST_SERVICE_PORT is required in .env file for unit testing")
262
263         apiName := "apiName"
264         newServiceDescription := getServiceAPIDescription(aefId, apiName, description, testServiceIpv4, testServicePort)
265
266         // Attempt to publish a service for provider
267         result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, eServiceManager)
268         assert.Equal(t, http.StatusForbidden, result.Code())
269
270         // var resultError common29122.ProblemDetails
271         err = result.UnmarshalBodyToObject(&resultError)
272         assert.NoError(t, err, "error unmarshaling response")
273
274         assert.Contains(t, *resultError.Cause, apfId)
275         assert.Contains(t, *resultError.Cause, "Unable to publish the service due to api is only available for publishers")
276 }
277
278 func TestRegisterValidProvider(t *testing.T) {
279         newProvider := getProvider()
280
281         // Register a valid provider
282         result := testutil.NewRequest().Post("/api-provider-management/v1/registrations").WithJsonBody(newProvider).Go(t, eServiceManager)
283         assert.Equal(t, http.StatusCreated, result.Code())
284
285         var resultProvider provapi.APIProviderEnrolmentDetails
286         err := result.UnmarshalBodyToObject(&resultProvider)
287         assert.NoError(t, err, "error unmarshaling response")
288 }
289
290 func TestPublishUnpublishService(t *testing.T) {
291         apfId := "APF_id_rApp_Kong_as_APF"
292         apiName := "apiName"
293
294         myEnv, myPorts, err := mockConfigReader.ReadDotEnv()
295         assert.Nil(t, err, "error reading env file")
296
297         testServiceIpv4 := common29122.Ipv4Addr(myEnv["TEST_SERVICE_IPV4"])
298         testServicePort := common29122.Port(myPorts["TEST_SERVICE_PORT"])
299
300         assert.NotEmpty(t, testServiceIpv4, "TEST_SERVICE_IPV4 is required in .env file for unit testing")
301         assert.NotZero(t, testServicePort, "TEST_SERVICE_PORT is required in .env file for unit testing")
302
303         // Check no services published
304         result := testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, eServiceManager)
305         assert.Equal(t, http.StatusOK, result.Code())
306
307         // Parse JSON from the response body
308         var resultServices []publishapi.ServiceAPIDescription
309         err = result.UnmarshalJsonToObject(&resultServices)
310         assert.NoError(t, err, "error unmarshaling response")
311
312         // Check if the parsed array is empty
313         assert.Zero(t, len(resultServices))
314
315         aefId := "AEF_id_rApp_Kong_as_AEF"
316         namespace := "namespace"
317         repoName := "repoName"
318         chartName := "chartName"
319         releaseName := "releaseName"
320         description := fmt.Sprintf("Description,%s,%s,%s,%s", namespace, repoName, chartName, releaseName)
321
322         newServiceDescription := getServiceAPIDescriptionMissingInterface(aefId, apiName, description)
323
324         // Publish a service for provider
325         result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, eServiceManager)
326         assert.Equal(t, http.StatusBadRequest, result.Code())
327
328         var resultError common29122.ProblemDetails
329         err = result.UnmarshalJsonToObject(&resultError)
330         assert.NoError(t, err, "error unmarshaling response")
331
332         assert.Contains(t, *resultError.Cause, "cannot read interfaceDescription")
333
334         newServiceDescription = getServiceAPIDescription(aefId, apiName, description, testServiceIpv4, testServicePort)
335
336         // Publish a service for provider
337         result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, eServiceManager)
338         assert.Equal(t, http.StatusCreated, result.Code())
339
340         if result.Code() != http.StatusCreated {
341                 log.Fatalf("failed to publish the service with HTTP result code %d", result.Code())
342                 return
343         }
344
345         var resultService publishapi.ServiceAPIDescription
346         err = result.UnmarshalJsonToObject(&resultService)
347         assert.NoError(t, err, "error unmarshaling response")
348         newApiId := "api_id_" + apiName
349         assert.Equal(t, newApiId, *resultService.ApiId)
350
351         assert.Equal(t, "http://example.com/published-apis/v1/"+apfId+"/service-apis/"+*resultService.ApiId, result.Recorder.Header().Get(echo.HeaderLocation))
352
353         // Check that the service is published for the provider
354         result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis/"+newApiId).Go(t, eServiceManager)
355         assert.Equal(t, http.StatusOK, result.Code())
356
357         err = result.UnmarshalJsonToObject(&resultService)
358         assert.NoError(t, err, "error unmarshaling response")
359         assert.Equal(t, newApiId, *resultService.ApiId)
360
361         aefProfile := (*resultService.AefProfiles)[0]
362         interfaceDescription := (*aefProfile.InterfaceDescriptions)[0]
363
364         resultServiceIpv4 := *interfaceDescription.Ipv4Addr
365         resultServicePort := *interfaceDescription.Port
366
367         kongIPv4 := common29122.Ipv4Addr(myEnv["KONG_IPV4"])
368         kongDataPlanePort := common29122.Port(myPorts["KONG_DATA_PLANE_PORT"])
369
370         assert.NotEmpty(t, kongIPv4, "KONG_IPV4 is required in .env file for unit testing")
371         assert.NotZero(t, kongDataPlanePort, "KONG_DATA_PLANE_PORT is required in .env file for unit testing")
372
373         assert.Equal(t, kongIPv4, resultServiceIpv4)
374         assert.Equal(t, kongDataPlanePort, resultServicePort)
375
376         // Publish the same service again should result in Forbidden
377         newServiceDescription.ApiId = &newApiId
378         result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, eServiceManager)
379         assert.Equal(t, http.StatusForbidden, result.Code())
380
381         err = result.UnmarshalBodyToObject(&resultError)
382         assert.NoError(t, err, "error unmarshaling response")
383         assert.Contains(t, *resultError.Cause, "already published")
384         assert.Equal(t, http.StatusForbidden, *resultError.Status)
385
386         // Delete the service
387         result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+newApiId).Go(t, eServiceManager)
388         assert.Equal(t, http.StatusNoContent, result.Code())
389
390         // Check no services published
391         result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, eServiceManager)
392         assert.Equal(t, http.StatusOK, result.Code())
393
394         // Parse JSON from the response body
395         err = result.UnmarshalJsonToObject(&resultServices)
396         assert.NoError(t, err, "error unmarshaling response")
397
398         // Check if the parsed array is empty
399         assert.Zero(t, len(resultServices))
400         capifCleanUp()
401 }
402
403 func registerHandlers(e *echo.Echo, myEnv map[string]string, myPorts map[string]int) (err error) {
404         capifProtocol := myEnv["CAPIF_PROTOCOL"]
405         capifIPv4 := common29122.Ipv4Addr(myEnv["CAPIF_IPV4"])
406         capifPort := common29122.Port(myPorts["CAPIF_PORT"])
407         kongDomain := myEnv["KONG_DOMAIN"]
408         kongProtocol := myEnv["KONG_PROTOCOL"]
409         kongIPv4 := common29122.Ipv4Addr(myEnv["KONG_IPV4"])
410         kongDataPlanePort := common29122.Port(myPorts["KONG_DATA_PLANE_PORT"])
411         kongControlPlanePort := common29122.Port(myPorts["KONG_CONTROL_PLANE_PORT"])
412
413         // Register ProviderManagement
414         providerManagerSwagger, err := provapi.GetSwagger()
415         if err != nil {
416                 log.Fatalf("error loading ProviderManagement swagger spec\n: %s", err)
417                 return err
418         }
419         providerManagerSwagger.Servers = nil
420         providerManager := providermanagement.NewProviderManager(capifProtocol, capifIPv4, capifPort)
421
422         var group *echo.Group
423
424         group = e.Group("/api-provider-management/v1")
425         group.Use(middleware.OapiRequestValidator(providerManagerSwagger))
426         provapi.RegisterHandlersWithBaseURL(e, providerManager, "/api-provider-management/v1")
427
428         publishServiceSwagger, err := publishapi.GetSwagger()
429         if err != nil {
430                 fmt.Fprintf(os.Stderr, "Error loading PublishService swagger spec\n: %s", err)
431                 return err
432         }
433
434         publishServiceSwagger.Servers = nil
435
436         ps := NewPublishService(kongDomain, kongProtocol, kongIPv4, kongDataPlanePort, kongControlPlanePort, capifProtocol, capifIPv4, capifPort)
437
438         group = e.Group("/published-apis/v1")
439         group.Use(echomiddleware.Logger())
440         group.Use(middleware.OapiRequestValidator(publishServiceSwagger))
441         publishapi.RegisterHandlersWithBaseURL(e, ps, "/published-apis/v1")
442
443         return err
444 }
445
446 func getServiceAPIDescription(aefId, apiName, description string, testServiceIpv4 common29122.Ipv4Addr, testServicePort common29122.Port) publishapi.ServiceAPIDescription {
447         domainName := "Kong"
448         var protocol publishapi.Protocol = "HTTP_1_1"
449
450         return publishapi.ServiceAPIDescription{
451                 AefProfiles: &[]publishapi.AefProfile{
452                         {
453                                 AefId: aefId,
454                                 InterfaceDescriptions: &[]publishapi.InterfaceDescription{
455                                         {
456                                                 Ipv4Addr: &testServiceIpv4,
457                                                 Port:     &testServicePort,
458                                                 SecurityMethods: &[]publishapi.SecurityMethod{
459                                                         "PKI",
460                                                 },
461                                         },
462                                 },
463                                 DomainName: &domainName,
464                                 Protocol:   &protocol,
465                                 Versions: []publishapi.Version{
466                                         {
467                                                 ApiVersion: "v1",
468                                                 Resources: &[]publishapi.Resource{
469                                                         {
470                                                                 CommType: "REQUEST_RESPONSE",
471                                                                 Operations: &[]publishapi.Operation{
472                                                                         "GET",
473                                                                 },
474                                                                 ResourceName: "helloworld",
475                                                                 Uri:          "/helloworld",
476                                                         },
477                                                 },
478                                         },
479                                 },
480                         },
481                 },
482                 ApiName:     apiName,
483                 Description: &description,
484         }
485 }
486
487 func getServiceAPIDescriptionMissingInterface(aefId, apiName, description string) publishapi.ServiceAPIDescription {
488         domainName := "Kong"
489         var protocol publishapi.Protocol = "HTTP_1_1"
490
491         return publishapi.ServiceAPIDescription{
492                 AefProfiles: &[]publishapi.AefProfile{
493                         {
494                                 AefId:      aefId,
495                                 DomainName: &domainName,
496                                 Protocol:   &protocol,
497                                 Versions: []publishapi.Version{
498                                         {
499                                                 ApiVersion: "v1",
500                                                 Resources: &[]publishapi.Resource{
501                                                         {
502                                                                 CommType: "REQUEST_RESPONSE",
503                                                                 Operations: &[]publishapi.Operation{
504                                                                         "GET",
505                                                                 },
506                                                                 ResourceName: "helloworld",
507                                                                 Uri:          "/helloworld",
508                                                         },
509                                                 },
510                                         },
511                                 },
512                         },
513                 },
514                 ApiName:     apiName,
515                 Description: &description,
516         }
517 }