X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=sidebyside;f=capifcore%2Finternal%2Finvokermanagement%2Finvokermanagement_test.go;h=d3c386c6361b2b264f69f21a345aefbd6ea0d193;hb=b8e717a8c264a8b3f73626fc28c81ae65283ae80;hp=0b90de870bf72505ba39604fca94208b65c48359;hpb=0b4c4ecb52b1c04037a65644dc8c6c29981d9736;p=nonrtric%2Fplt%2Fsme.git diff --git a/capifcore/internal/invokermanagement/invokermanagement_test.go b/capifcore/internal/invokermanagement/invokermanagement_test.go index 0b90de8..d3c386c 100644 --- a/capifcore/internal/invokermanagement/invokermanagement_test.go +++ b/capifcore/internal/invokermanagement/invokermanagement_test.go @@ -23,10 +23,11 @@ import ( "fmt" "net/http" "os" - "path" "strings" "testing" + "time" + "oransc.org/nonrtric/capifcore/internal/eventsapi" "oransc.org/nonrtric/capifcore/internal/invokermanagementapi" "github.com/labstack/echo/v4" @@ -56,7 +57,7 @@ func TestOnboardInvoker(t *testing.T) { } publishRegisterMock := publishmocks.PublishRegister{} publishRegisterMock.On("GetAllPublishedServices").Return(publishedServices) - invokerUnderTest, requestHandler := getEcho(&publishRegisterMock) + invokerUnderTest, eventChannel, requestHandler := getEcho(&publishRegisterMock) invokerInfo := "invoker a" newInvoker := getInvoker(invokerInfo) @@ -79,6 +80,12 @@ func TestOnboardInvoker(t *testing.T) { assert.True(t, invokerUnderTest.VerifyInvokerSecret(wantedInvokerId, wantedInvokerSecret)) publishRegisterMock.AssertCalled(t, "GetAllPublishedServices") assert.Equal(t, invokermanagementapi.APIList(publishedServices), *resultInvoker.ApiList) + if invokerEvent, timeout := waitForEvent(eventChannel, 1*time.Second); timeout { + assert.Fail(t, "No event sent") + } else { + assert.Equal(t, *resultInvoker.ApiInvokerId, (*invokerEvent.EventDetail.ApiInvokerIds)[0]) + assert.Equal(t, eventsapi.CAPIFEventAPIINVOKERONBOARDED, invokerEvent.Events) + } // Onboard an invoker missing required NotificationDestination, should get 400 with problem details invalidInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ @@ -113,61 +120,59 @@ func TestOnboardInvoker(t *testing.T) { } func TestDeleteInvoker(t *testing.T) { - publishRegisterMock := publishmocks.PublishRegister{} - publishRegisterMock.On("GetAllPublishedServices").Return([]publishserviceapi.ServiceAPIDescription{}) - invokerUnderTest, requestHandler := getEcho(&publishRegisterMock) + invokerUnderTest, eventChannel, requestHandler := getEcho(nil) + invokerId := "invokerId" newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ + ApiInvokerId: &invokerId, NotificationDestination: "url", OnboardingInformation: invokermanagementapi.OnboardingInformation{ ApiInvokerPublicKey: "key", }, } - - // Onboard an invoker - result := testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler) - - invokerUrl := result.Recorder.Header().Get(echo.HeaderLocation) - assert.True(t, invokerUnderTest.IsInvokerRegistered(path.Base(invokerUrl))) + invokerUnderTest.onboardedInvokers[invokerId] = newInvoker + assert.True(t, invokerUnderTest.IsInvokerRegistered(invokerId)) // Delete the invoker - result = testutil.NewRequest().Delete(invokerUrl).Go(t, requestHandler) + result := testutil.NewRequest().Delete("/onboardedInvokers/"+invokerId).Go(t, requestHandler) assert.Equal(t, http.StatusNoContent, result.Code()) - assert.False(t, invokerUnderTest.IsInvokerRegistered(path.Base(invokerUrl))) + assert.False(t, invokerUnderTest.IsInvokerRegistered(invokerId)) + if invokerEvent, timeout := waitForEvent(eventChannel, 1*time.Second); timeout { + assert.Fail(t, "No event sent") + } else { + assert.Equal(t, invokerId, (*invokerEvent.EventDetail.ApiInvokerIds)[0]) + assert.Equal(t, eventsapi.CAPIFEventAPIINVOKEROFFBOARDED, invokerEvent.Events) + } } func TestUpdateInvoker(t *testing.T) { publishRegisterMock := publishmocks.PublishRegister{} publishRegisterMock.On("GetAllPublishedServices").Return([]publishserviceapi.ServiceAPIDescription{}) - _, requestHandler := getEcho(&publishRegisterMock) + serviceUnderTest, _, requestHandler := getEcho(&publishRegisterMock) - newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ + invokerId := "invokerId" + invoker := invokermanagementapi.APIInvokerEnrolmentDetails{ + ApiInvokerId: &invokerId, NotificationDestination: "url", OnboardingInformation: invokermanagementapi.OnboardingInformation{ ApiInvokerPublicKey: "key", }, } - - // Onboard an invoker - result := testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler) - var resultInvoker invokermanagementapi.APIInvokerEnrolmentDetails - result.UnmarshalBodyToObject(&resultInvoker) + serviceUnderTest.onboardedInvokers[invokerId] = invoker // Update the invoker with valid invoker, should return 200 with updated invoker details - invokerId := resultInvoker.ApiInvokerId - invokerUrl := result.Recorder.Header().Get(echo.HeaderLocation) - resultInvoker.ApiList = nil newNotifURL := "newUrl" - resultInvoker.NotificationDestination = common29122.Uri(newNotifURL) + invoker.NotificationDestination = common29122.Uri(newNotifURL) newPublicKey := "newPublicKey" - resultInvoker.OnboardingInformation.ApiInvokerPublicKey = newPublicKey - result = testutil.NewRequest().Put(invokerUrl).WithJsonBody(resultInvoker).Go(t, requestHandler) + invoker.OnboardingInformation.ApiInvokerPublicKey = newPublicKey + result := testutil.NewRequest().Put("/onboardedInvokers/"+invokerId).WithJsonBody(invoker).Go(t, requestHandler) + var resultInvoker invokermanagementapi.APIInvokerEnrolmentDetails assert.Equal(t, http.StatusOK, result.Code()) err := result.UnmarshalBodyToObject(&resultInvoker) assert.NoError(t, err, "error unmarshaling response") - assert.Equal(t, invokerId, resultInvoker.ApiInvokerId) + assert.Equal(t, invokerId, *resultInvoker.ApiInvokerId) assert.Equal(t, newNotifURL, string(resultInvoker.NotificationDestination)) assert.Equal(t, newPublicKey, resultInvoker.OnboardingInformation.ApiInvokerPublicKey) @@ -176,10 +181,10 @@ func TestUpdateInvoker(t *testing.T) { ApiInvokerPublicKey: "key", } invalidInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ - ApiInvokerId: invokerId, + ApiInvokerId: &invokerId, OnboardingInformation: validOnboardingInfo, } - result = testutil.NewRequest().Put(invokerUrl).WithJsonBody(invalidInvoker).Go(t, requestHandler) + result = testutil.NewRequest().Put("/onboardedInvokers/"+invokerId).WithJsonBody(invalidInvoker).Go(t, requestHandler) assert.Equal(t, http.StatusBadRequest, result.Code()) var problemDetails common29122.ProblemDetails @@ -193,7 +198,7 @@ func TestUpdateInvoker(t *testing.T) { // Update with an invoker missing required OnboardingInformation.ApiInvokerPublicKey, should get 400 with problem details invalidInvoker.NotificationDestination = "url" invalidInvoker.OnboardingInformation = invokermanagementapi.OnboardingInformation{} - result = testutil.NewRequest().Put(invokerUrl).WithJsonBody(invalidInvoker).Go(t, requestHandler) + result = testutil.NewRequest().Put("/onboardedInvokers/"+invokerId).WithJsonBody(invalidInvoker).Go(t, requestHandler) assert.Equal(t, http.StatusBadRequest, result.Code()) err = result.UnmarshalBodyToObject(&problemDetails) @@ -206,7 +211,7 @@ func TestUpdateInvoker(t *testing.T) { invalidId := "1" invalidInvoker.ApiInvokerId = &invalidId invalidInvoker.OnboardingInformation = validOnboardingInfo - result = testutil.NewRequest().Put(invokerUrl).WithJsonBody(invalidInvoker).Go(t, requestHandler) + result = testutil.NewRequest().Put("/onboardedInvokers/"+invokerId).WithJsonBody(invalidInvoker).Go(t, requestHandler) assert.Equal(t, http.StatusBadRequest, result.Code()) err = result.UnmarshalBodyToObject(&problemDetails) @@ -215,10 +220,10 @@ func TestUpdateInvoker(t *testing.T) { assert.Contains(t, *problemDetails.Cause, "not matching") assert.Contains(t, *problemDetails.Cause, "ApiInvokerId") - // Update an invoker that has not been onboarded, shold get 404 with problem details + // Update an invoker that has not been onboarded, should get 404 with problem details missingId := "1" - newInvoker.ApiInvokerId = &missingId - result = testutil.NewRequest().Put("/onboardedInvokers/"+missingId).WithJsonBody(newInvoker).Go(t, requestHandler) + invoker.ApiInvokerId = &missingId + result = testutil.NewRequest().Put("/onboardedInvokers/"+missingId).WithJsonBody(invoker).Go(t, requestHandler) assert.Equal(t, http.StatusNotFound, result.Code()) err = result.UnmarshalBodyToObject(&problemDetails) @@ -250,20 +255,26 @@ func TestGetInvokerApiList(t *testing.T) { }) publishRegisterMock := publishmocks.PublishRegister{} publishRegisterMock.On("GetAllPublishedServices").Return(apiList) - invokerUnderTest, requestHandler := getEcho(&publishRegisterMock) + invokerUnderTest, _, _ := getEcho(&publishRegisterMock) invokerInfo := "invoker a" newInvoker := getInvoker(invokerInfo) - testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler) - newInvoker = getInvoker("invoker b") - testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler) - - wantedApiList := invokerUnderTest.GetInvokerApiList("api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1)) + invokerAId := "api_invoker_id_" + strings.ReplaceAll(invokerInfo, " ", "_") + newInvoker.ApiInvokerId = &invokerAId + invokerUnderTest.onboardedInvokers[invokerAId] = newInvoker + invokerInfo = "invoker b" + newInvoker = getInvoker(invokerInfo) + invokerId := "api_invoker_id_" + strings.ReplaceAll(invokerInfo, " ", "_") + newInvoker.ApiInvokerId = &invokerId + invokerUnderTest.onboardedInvokers[invokerId] = newInvoker + + wantedApiList := invokerUnderTest.GetInvokerApiList(invokerAId) assert.NotNil(t, wantedApiList) + assert.Len(t, *wantedApiList, 2) assert.Equal(t, apiId, *(*wantedApiList)[0].ApiId) } -func getEcho(publishRegister publishservice.PublishRegister) (*InvokerManager, *echo.Echo) { +func getEcho(publishRegister publishservice.PublishRegister) (*InvokerManager, chan eventsapi.EventNotification, *echo.Echo) { swagger, err := invokermanagementapi.GetSwagger() if err != nil { fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err) @@ -272,14 +283,15 @@ func getEcho(publishRegister publishservice.PublishRegister) (*InvokerManager, * swagger.Servers = nil - im := NewInvokerManager(publishRegister) + eventChannel := make(chan eventsapi.EventNotification) + im := NewInvokerManager(publishRegister, eventChannel) e := echo.New() e.Use(echomiddleware.Logger()) e.Use(middleware.OapiRequestValidator(swagger)) invokermanagementapi.RegisterHandlers(e, im) - return im, e + return im, eventChannel, e } func getAefProfile(aefId string) publishserviceapi.AefProfile { @@ -308,3 +320,14 @@ func getInvoker(invokerInfo string) invokermanagementapi.APIInvokerEnrolmentDeta } return newInvoker } + +// waitForEvent waits for the channel to receive an event for the specified max timeout. +// Returns true if waiting timed out. +func waitForEvent(ch chan eventsapi.EventNotification, timeout time.Duration) (*eventsapi.EventNotification, bool) { + select { + case event := <-ch: + return &event, false // completed normally + case <-time.After(timeout): + return nil, true // timed out + } +}