3 // ========================LICENSE_START=================================
6 // Copyright (C) 2023-2024: OpenInfra Foundation Europe
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
12 // http://www.apache.org/licenses/LICENSE-2.0
14 // Unless required by applicable law or agreed to in writing, software
15 // distributed under the License is distributed on an "AS IS" BASIS,
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 // See the License for the specific language governing permissions and
18 // limitations under the License.
19 // ========================LICENSE_END===================================
20 package invokermanagement
29 "oransc.org/nonrtric/servicemanager/internal/common29122"
30 "oransc.org/nonrtric/servicemanager/internal/envreader"
31 "oransc.org/nonrtric/servicemanager/internal/invokermanagementapi"
32 "oransc.org/nonrtric/servicemanager/internal/kongclear"
33 "oransc.org/nonrtric/servicemanager/internal/providermanagement"
34 provapi "oransc.org/nonrtric/servicemanager/internal/providermanagementapi"
35 "oransc.org/nonrtric/servicemanager/internal/publishservice"
36 publishapi "oransc.org/nonrtric/servicemanager/internal/publishserviceapi"
38 "github.com/deepmap/oapi-codegen/pkg/middleware"
39 "github.com/deepmap/oapi-codegen/pkg/testutil"
40 "github.com/labstack/echo/v4"
41 echomiddleware "github.com/labstack/echo/v4/middleware"
42 log "github.com/sirupsen/logrus"
43 "github.com/stretchr/testify/assert"
46 var requestHandler *echo.Echo
48 func TestMain(m *testing.M) {
61 func setupTest() error {
62 myEnv, myPorts, err := envreader.ReadDotEnv()
64 log.Fatal("error loading environment file on setupTest")
68 requestHandler, err = getEcho(myEnv, myPorts)
70 log.Fatal("getEcho fatal error on setupTest")
75 log.Fatal("getEcho fatal error on teardown")
82 func getProvider() provapi.APIProviderEnrolmentDetails {
85 funcInfoAPF = "rApp Kong as APF"
86 funcInfoAEF = "rApp Kong as AEF"
89 testFuncs := []provapi.APIProviderFunctionDetails{
91 ApiProvFuncInfo: &funcInfoAPF,
92 ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
93 RegInfo: provapi.RegistrationInformation{
94 ApiProvPubKey: "APF-PublicKey",
98 ApiProvFuncInfo: &funcInfoAEF,
99 ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
100 RegInfo: provapi.RegistrationInformation{
101 ApiProvPubKey: "AEF-PublicKey",
105 return provapi.APIProviderEnrolmentDetails{
107 ApiProvDomInfo: &domainInfo,
108 ApiProvFuncs: &testFuncs,
112 func teardown() error {
113 log.Trace("entering teardown")
115 t := new(testing.T) // Create a new testing.T instance for teardown
117 // Delete the invoker
118 invokerInfo := "invoker a"
119 invokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1)
121 result := testutil.NewRequest().Delete("/api-invoker-management/v1/onboardedInvokers/"+invokerId).Go(t, requestHandler)
122 assert.Equal(t, http.StatusNoContent, result.Code())
124 // Delete the original published service
125 apfId := "APF_id_rApp_Kong_as_APF"
127 apiId := "api_id_" + apiName
129 result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler)
130 assert.Equal(t, http.StatusNoContent, result.Code())
132 // Delete the first published service
133 apfId = "APF_id_rApp_Kong_as_APF"
135 apiId = "api_id_" + apiName
137 result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler)
138 assert.Equal(t, http.StatusNoContent, result.Code())
140 // Delete the second published service
142 apiId = "api_id_" + apiName
144 result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler)
145 assert.Equal(t, http.StatusNoContent, result.Code())
147 // Delete the provider
148 domainID := "domain_id_Kong"
149 result = testutil.NewRequest().Delete("/api-provider-management/v1/registrations/"+domainID).Go(t, requestHandler)
150 assert.Equal(t, http.StatusNoContent, result.Code())
152 myEnv, myPorts, err := envreader.ReadDotEnv()
154 log.Fatal("error loading environment file")
158 err = kongclear.KongClear(myEnv, myPorts)
160 log.Fatal("error clearing Kong on teardown")
165 func TestRegisterValidProvider(t *testing.T) {
168 newProvider := getProvider()
170 // Register a valid provider
171 result := testutil.NewRequest().Post("/api-provider-management/v1/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
172 assert.Equal(t, http.StatusCreated, result.Code())
174 var resultProvider provapi.APIProviderEnrolmentDetails
175 err := result.UnmarshalBodyToObject(&resultProvider)
176 assert.NoError(t, err, "error unmarshaling response")
179 func TestPublishUnpublishService(t *testing.T) {
180 apfId := "APF_id_rApp_Kong_as_APF"
182 newApiId := "api_id_" + apiName
184 myEnv, myPorts, err := envreader.ReadDotEnv()
185 assert.Nil(t, err, "error reading env file")
187 testServiceIpv4 := common29122.Ipv4Addr(myEnv["TEST_SERVICE_IPV4"])
188 testServicePort := common29122.Port(myPorts["TEST_SERVICE_PORT"])
190 assert.NotEmpty(t, testServiceIpv4, "TEST_SERVICE_IPV4 is required in .env file for unit testing")
191 assert.NotZero(t, testServicePort, "TEST_SERVICE_PORT is required in .env file for unit testing")
193 // Check no services published
194 result := testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis").Go(t, requestHandler)
195 assert.Equal(t, http.StatusOK, result.Code())
197 // Parse JSON from the response body
198 var resultServices []publishapi.ServiceAPIDescription
199 err = result.UnmarshalJsonToObject(&resultServices)
200 assert.NoError(t, err, "error unmarshaling response")
202 // Check if the parsed array is empty
203 assert.Zero(t, len(resultServices))
205 aefId := "AEF_id_rApp_Kong_as_AEF"
206 namespace := "namespace"
207 repoName := "repoName"
208 chartName := "chartName"
209 releaseName := "releaseName"
210 description := fmt.Sprintf("Description,%s,%s,%s,%s", namespace, repoName, chartName, releaseName)
212 newServiceDescription := getServiceAPIDescription(aefId, apiName, description, testServiceIpv4, testServicePort)
214 // Publish a service for provider
215 result = testutil.NewRequest().Post("/published-apis/v1/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler)
216 assert.Equal(t, http.StatusCreated, result.Code())
218 if result.Code() != http.StatusCreated {
219 log.Fatalf("failed to publish the service with HTTP result code %d", result.Code())
223 var resultService publishapi.ServiceAPIDescription
224 err = result.UnmarshalJsonToObject(&resultService)
225 assert.NoError(t, err, "error unmarshaling response")
226 assert.Equal(t, newApiId, *resultService.ApiId)
228 assert.Equal(t, "http://example.com/published-apis/v1/"+apfId+"/service-apis/"+*resultService.ApiId, result.Recorder.Header().Get(echo.HeaderLocation))
230 // Check that the service is published for the provider
231 result = testutil.NewRequest().Get("/published-apis/v1/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
232 assert.Equal(t, http.StatusOK, result.Code())
234 err = result.UnmarshalJsonToObject(&resultService)
235 assert.NoError(t, err, "error unmarshaling response")
236 assert.Equal(t, newApiId, *resultService.ApiId)
238 aefProfile := (*resultService.AefProfiles)[0]
239 interfaceDescription := (*aefProfile.InterfaceDescriptions)[0]
241 resultServiceIpv4 := *interfaceDescription.Ipv4Addr
242 resultServicePort := *interfaceDescription.Port
244 kongIPv4 := common29122.Ipv4Addr(myEnv["KONG_IPV4"])
245 kongDataPlanePort := common29122.Port(myPorts["KONG_DATA_PLANE_PORT"])
247 assert.NotEmpty(t, kongIPv4, "KONG_IPV4 is required in .env file for unit testing")
248 assert.NotZero(t, kongDataPlanePort, "KONG_DATA_PLANE_PORT is required in .env file for unit testing")
250 assert.Equal(t, kongIPv4, resultServiceIpv4)
251 assert.Equal(t, kongDataPlanePort, resultServicePort)
254 func TestOnboardInvoker(t *testing.T) {
255 invokerInfo := "invoker a"
256 newInvoker := getInvoker(invokerInfo)
258 // Onboard a valid invoker
259 result := testutil.NewRequest().Post("/api-invoker-management/v1/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler)
260 assert.Equal(t, http.StatusCreated, result.Code())
262 var resultInvoker invokermanagementapi.APIInvokerEnrolmentDetails
264 err := result.UnmarshalBodyToObject(&resultInvoker)
265 assert.NoError(t, err, "error unmarshaling response")
267 wantedInvokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1)
269 assert.Equal(t, wantedInvokerId, *resultInvoker.ApiInvokerId)
270 assert.Equal(t, newInvoker.NotificationDestination, resultInvoker.NotificationDestination)
271 assert.Equal(t, newInvoker.OnboardingInformation.ApiInvokerPublicKey, resultInvoker.OnboardingInformation.ApiInvokerPublicKey)
273 assert.Equal(t, "http://example.com/api-invoker-management/v1/onboardedInvokers/"+*resultInvoker.ApiInvokerId, result.Recorder.Header().Get(echo.HeaderLocation))
275 // Onboarding the same invoker should result in Forbidden
276 result = testutil.NewRequest().Post("/api-invoker-management/v1/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler)
278 assert.Equal(t, http.StatusForbidden, result.Code())
280 var problemDetails common29122.ProblemDetails
281 err = result.UnmarshalBodyToObject(&problemDetails)
282 assert.NoError(t, err, "error unmarshaling response")
284 assert.Equal(t, http.StatusForbidden, *problemDetails.Status)
285 assert.Contains(t, *problemDetails.Cause, "already onboarded")
287 // Onboard an invoker missing required NotificationDestination, should get 400 with problem details
288 invalidInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{
289 OnboardingInformation: invokermanagementapi.OnboardingInformation{
290 ApiInvokerPublicKey: "newKey",
293 result = testutil.NewRequest().Post("/api-invoker-management/v1/onboardedInvokers").WithJsonBody(invalidInvoker).Go(t, requestHandler)
294 assert.Equal(t, http.StatusBadRequest, result.Code())
296 err = result.UnmarshalBodyToObject(&problemDetails)
297 assert.NoError(t, err, "error unmarshaling response")
299 assert.Equal(t, http.StatusBadRequest, *problemDetails.Status)
300 assert.Contains(t, *problemDetails.Cause, "missing")
301 assert.Contains(t, *problemDetails.Cause, "NotificationDestination")
303 // Onboard an invoker missing required OnboardingInformation.ApiInvokerPublicKey, should get 400 with problem details
304 invalidInvoker = invokermanagementapi.APIInvokerEnrolmentDetails{
305 NotificationDestination: "http://golang.cafe/",
308 result = testutil.NewRequest().Post("/api-invoker-management/v1/onboardedInvokers").WithJsonBody(invalidInvoker).Go(t, requestHandler)
309 assert.Equal(t, http.StatusBadRequest, result.Code())
311 err = result.UnmarshalBodyToObject(&problemDetails)
312 assert.NoError(t, err, "error unmarshaling response")
314 assert.Equal(t, http.StatusBadRequest, *problemDetails.Status)
315 assert.Contains(t, *problemDetails.Cause, "missing")
316 assert.Contains(t, *problemDetails.Cause, "OnboardingInformation.ApiInvokerPublicKey")
319 func TestDeleteInvoker(t *testing.T) {
320 invokerInfo := "invoker a"
321 invokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1)
323 // Delete the invoker
324 result := testutil.NewRequest().Delete("/api-invoker-management/v1/onboardedInvokers/"+invokerId).Go(t, requestHandler)
325 assert.Equal(t, http.StatusNoContent, result.Code())
328 func TestUpdateInvoker(t *testing.T) {
329 invokerInfo := "invoker a"
330 invoker := getInvoker(invokerInfo)
331 invokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1)
333 // Onboard a valid invoker
334 result := testutil.NewRequest().Post("/api-invoker-management/v1/onboardedInvokers").WithJsonBody(invoker).Go(t, requestHandler)
335 assert.Equal(t, http.StatusCreated, result.Code())
337 // Update the invoker with valid invoker, should return 200 with updated invoker details
338 newNotifURL := "http://golang.org/"
339 invoker.NotificationDestination = common29122.Uri(newNotifURL)
340 newPublicKey := "newPublicKey"
341 invoker.OnboardingInformation.ApiInvokerPublicKey = newPublicKey
343 invoker.ApiInvokerId = &invokerId
345 result = testutil.NewRequest().Put("/api-invoker-management/v1/onboardedInvokers/"+invokerId).WithJsonBody(invoker).Go(t, requestHandler)
346 assert.Equal(t, http.StatusOK, result.Code())
348 var resultInvoker invokermanagementapi.APIInvokerEnrolmentDetails
350 err := result.UnmarshalBodyToObject(&resultInvoker)
351 assert.NoError(t, err, "error unmarshaling response")
353 assert.Equal(t, invokerId, *resultInvoker.ApiInvokerId)
354 assert.Equal(t, newNotifURL, string(resultInvoker.NotificationDestination))
355 assert.Equal(t, newPublicKey, resultInvoker.OnboardingInformation.ApiInvokerPublicKey)
357 // Update with an invoker missing required NotificationDestination, should get 400 with problem details
358 validOnboardingInfo := invokermanagementapi.OnboardingInformation{
359 ApiInvokerPublicKey: "key",
361 invalidInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{
362 ApiInvokerId: &invokerId,
363 OnboardingInformation: validOnboardingInfo,
365 result = testutil.NewRequest().Put("/api-invoker-management/v1/onboardedInvokers/"+invokerId).WithJsonBody(invalidInvoker).Go(t, requestHandler)
366 assert.Equal(t, http.StatusBadRequest, result.Code())
368 var problemDetails common29122.ProblemDetails
369 err = result.UnmarshalBodyToObject(&problemDetails)
370 assert.NoError(t, err, "error unmarshaling response")
372 assert.Equal(t, http.StatusBadRequest, *problemDetails.Status)
373 assert.Contains(t, *problemDetails.Cause, "missing")
374 assert.Contains(t, *problemDetails.Cause, "NotificationDestination")
376 // Update with an invoker missing required OnboardingInformation.ApiInvokerPublicKey, should get 400 with problem details
377 invalidInvoker.NotificationDestination = "http://golang.org/"
378 invalidInvoker.OnboardingInformation = invokermanagementapi.OnboardingInformation{}
379 result = testutil.NewRequest().Put("/api-invoker-management/v1/onboardedInvokers/"+invokerId).WithJsonBody(invalidInvoker).Go(t, requestHandler)
380 assert.Equal(t, http.StatusBadRequest, result.Code())
382 err = result.UnmarshalBodyToObject(&problemDetails)
383 assert.NoError(t, err, "error unmarshaling response")
385 assert.Equal(t, http.StatusBadRequest, *problemDetails.Status)
386 assert.Contains(t, *problemDetails.Cause, "missing")
387 assert.Contains(t, *problemDetails.Cause, "OnboardingInformation.ApiInvokerPublicKey")
389 // Update with an invoker with other ApiInvokerId than the one provided in the URL, should get 400 with problem details
391 invalidInvoker.ApiInvokerId = &invalidId
392 invalidInvoker.OnboardingInformation = validOnboardingInfo
393 result = testutil.NewRequest().Put("/api-invoker-management/v1/onboardedInvokers/"+invokerId).WithJsonBody(invalidInvoker).Go(t, requestHandler)
395 assert.Equal(t, http.StatusBadRequest, result.Code())
397 err = result.UnmarshalBodyToObject(&problemDetails)
398 assert.NoError(t, err, "error unmarshaling response")
400 assert.Equal(t, http.StatusBadRequest, *problemDetails.Status)
401 assert.Contains(t, *problemDetails.Cause, "APIInvokerEnrolmentDetails ApiInvokerId doesn't match path parameter")
403 // Update an invoker that has not been onboarded, should get 404 with problem details
405 invoker.ApiInvokerId = &missingId
406 result = testutil.NewRequest().Put("/api-invoker-management/v1/onboardedInvokers/"+missingId).WithJsonBody(invoker).Go(t, requestHandler)
407 assert.Equal(t, http.StatusNotFound, result.Code())
409 err = result.UnmarshalBodyToObject(&problemDetails)
410 assert.NoError(t, err, "error unmarshaling response")
412 assert.Equal(t, http.StatusNotFound, *problemDetails.Status)
413 assert.Contains(t, *problemDetails.Cause, "not been onboarded")
414 assert.Contains(t, *problemDetails.Cause, "invoker")
418 func getEcho(myEnv map[string]string, myPorts map[string]int) (*echo.Echo, error) {
419 capifProtocol := myEnv["CAPIF_PROTOCOL"]
420 capifIPv4 := common29122.Ipv4Addr(myEnv["CAPIF_IPV4"])
421 capifPort := common29122.Port(myPorts["CAPIF_PORT"])
422 kongDomain := myEnv["KONG_DOMAIN"]
423 kongProtocol := myEnv["KONG_PROTOCOL"]
424 kongIPv4 := common29122.Ipv4Addr(myEnv["KONG_IPV4"])
425 kongDataPlanePort := common29122.Port(myPorts["KONG_DATA_PLANE_PORT"])
426 kongControlPlanePort := common29122.Port(myPorts["KONG_CONTROL_PLANE_PORT"])
430 // Register ProviderManagement
431 providerManagerSwagger, err := provapi.GetSwagger()
433 log.Fatalf("error loading ProviderManagement swagger spec\n: %s", err)
436 providerManagerSwagger.Servers = nil
437 providerManager := providermanagement.NewProviderManager(capifProtocol, capifIPv4, capifPort)
439 var group *echo.Group
441 group = e.Group("/api-provider-management/v1")
442 group.Use(middleware.OapiRequestValidator(providerManagerSwagger))
443 provapi.RegisterHandlersWithBaseURL(e, providerManager, "/api-provider-management/v1")
445 publishServiceSwagger, err := publishapi.GetSwagger()
447 fmt.Fprintf(os.Stderr, "Error loading PublishService swagger spec\n: %s", err)
451 publishServiceSwagger.Servers = nil
453 ps := publishservice.NewPublishService(kongDomain, kongProtocol, kongIPv4, kongDataPlanePort, kongControlPlanePort, capifProtocol, capifIPv4, capifPort)
455 group = e.Group("/published-apis/v1")
456 group.Use(echomiddleware.Logger())
457 group.Use(middleware.OapiRequestValidator(publishServiceSwagger))
458 publishapi.RegisterHandlersWithBaseURL(e, ps, "/published-apis/v1")
460 invokerServiceSwagger, err := invokermanagementapi.GetSwagger()
462 fmt.Fprintf(os.Stderr, "Error loading InvokerManagement swagger spec\n: %s", err)
466 invokerServiceSwagger.Servers = nil
468 im := NewInvokerManager(capifProtocol, capifIPv4, capifPort)
470 group = e.Group("/api-invoker-management/v1")
471 group.Use(echomiddleware.Logger())
472 group.Use(middleware.OapiRequestValidator(invokerServiceSwagger))
473 invokermanagementapi.RegisterHandlersWithBaseURL(e, im, "api-invoker-management/v1")
478 func getServiceAPIDescription(aefId, apiName, description string, testServiceIpv4 common29122.Ipv4Addr, testServicePort common29122.Port) publishapi.ServiceAPIDescription {
480 var protocol publishapi.Protocol = "HTTP_1_1"
482 return publishapi.ServiceAPIDescription{
483 AefProfiles: &[]publishapi.AefProfile{
486 InterfaceDescriptions: &[]publishapi.InterfaceDescription{
488 Ipv4Addr: &testServiceIpv4,
489 Port: &testServicePort,
490 SecurityMethods: &[]publishapi.SecurityMethod{
495 DomainName: &domainName,
497 Versions: []publishapi.Version{
500 Resources: &[]publishapi.Resource{
502 CommType: "REQUEST_RESPONSE",
503 Operations: &[]publishapi.Operation{
506 ResourceName: "helloworld",
515 Description: &description,
519 func getInvoker(invokerInfo string) invokermanagementapi.APIInvokerEnrolmentDetails {
520 newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{
521 ApiInvokerInformation: &invokerInfo,
522 NotificationDestination: "http://golang.cafe/",
523 OnboardingInformation: invokermanagementapi.OnboardingInformation{
524 ApiInvokerPublicKey: "key",