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