X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=capifcore%2Finternal%2Finvokermanagement%2Finvokermanagement_test.go;h=507a69b7d5a4e5f0170e9bbe91f619c8cfced70c;hb=2ba4580c67fbe7994141e4cd2701f7bd22b69ebf;hp=2c95658b978a0ba77896fb87089705c56f8968e3;hpb=c956436e747ab021a2593aae4f46fdbc66ece7fb;p=nonrtric%2Fplt%2Fsme.git diff --git a/capifcore/internal/invokermanagement/invokermanagement_test.go b/capifcore/internal/invokermanagement/invokermanagement_test.go index 2c95658..507a69b 100644 --- a/capifcore/internal/invokermanagement/invokermanagement_test.go +++ b/capifcore/internal/invokermanagement/invokermanagement_test.go @@ -23,6 +23,7 @@ import ( "fmt" "net/http" "os" + "path" "testing" "oransc.org/nonrtric/capifcore/internal/invokermanagementapi" @@ -43,61 +44,28 @@ import ( ) func TestOnboardInvoker(t *testing.T) { - var err error + publishRegisterMock := publishmocks.PublishRegister{} + publishRegisterMock.On("AreAPIsPublished", mock.Anything).Return(true) + invokerUnderTest, requestHandler := getEcho(&publishRegisterMock) + + aefProfiles := []publishserviceapi.AefProfile{ + getAefProfile("aefId"), + } apiId := "apiId" - aefId := "aefId" - apiRegisterMock := publishmocks.APIRegister{} - apiRegisterMock.On("AreAPIsRegistered", mock.Anything).Return(true) - invokerUnderTest, requestHandler := getEcho(&apiRegisterMock) - - description := "description" - domainName := "domain" - var protocol publishserviceapi.Protocol = "HTTP_1_1" var apiList invokermanagementapi.APIList = []publishserviceapi.ServiceAPIDescription{ { ApiId: &apiId, - ApiName: "api", - Description: &description, - AefProfiles: &[]publishserviceapi.AefProfile{ - { - AefId: aefId, - DomainName: &domainName, - Protocol: &protocol, - Versions: []publishserviceapi.Version{ - { - ApiVersion: "v1", - Resources: &[]publishserviceapi.Resource{ - { - ResourceName: "app", - CommType: "REQUEST_RESPONSE", - Uri: "uri", - Operations: &[]publishserviceapi.Operation{ - "POST", - }, - }, - }, - }, - }, - }, - }, - }, - } - invokerInfo := "invoker a" - newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ - ApiInvokerInformation: &invokerInfo, - NotificationDestination: "url", - OnboardingInformation: invokermanagementapi.OnboardingInformation{ - ApiInvokerPublicKey: "key", + AefProfiles: &aefProfiles, }, - ApiList: &apiList, } + newInvoker := getInvoker("invoker a", apiList) // Onboard a valid invoker result := testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler) assert.Equal(t, http.StatusCreated, result.Code()) var resultInvoker invokermanagementapi.APIInvokerEnrolmentDetails - err = result.UnmarshalBodyToObject(&resultInvoker) + err := result.UnmarshalBodyToObject(&resultInvoker) assert.NoError(t, err, "error unmarshaling response") assert.Equal(t, "api_invoker_id_invoker_a", *resultInvoker.ApiInvokerId) assert.Equal(t, newInvoker.NotificationDestination, resultInvoker.NotificationDestination) @@ -106,7 +74,7 @@ func TestOnboardInvoker(t *testing.T) { assert.Equal(t, "http://example.com/onboardedInvokers/"+*resultInvoker.ApiInvokerId, result.Recorder.Header().Get(echo.HeaderLocation)) assert.True(t, invokerUnderTest.IsInvokerRegistered("api_invoker_id_invoker_a")) assert.True(t, invokerUnderTest.VerifyInvokerSecret("api_invoker_id_invoker_a", "onboarding_secret_invoker_a")) - apiRegisterMock.AssertCalled(t, "AreAPIsRegistered", mock.Anything) + publishRegisterMock.AssertCalled(t, "AreAPIsPublished", mock.Anything) // Onboard an invoker missing required NotificationDestination, should get 400 with problem details invalidInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ @@ -141,7 +109,7 @@ func TestOnboardInvoker(t *testing.T) { } func TestDeleteInvoker(t *testing.T) { - _, requestHandler := getEcho(nil) + invokerUnderTest, requestHandler := getEcho(nil) newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ NotificationDestination: "url", @@ -152,15 +120,15 @@ func TestDeleteInvoker(t *testing.T) { // Onboard an invoker result := testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler) - var resultInvoker invokermanagementapi.APIInvokerEnrolmentDetails - result.UnmarshalBodyToObject(&resultInvoker) invokerUrl := result.Recorder.Header().Get(echo.HeaderLocation) + assert.True(t, invokerUnderTest.IsInvokerRegistered(path.Base(invokerUrl))) // Delete the invoker result = testutil.NewRequest().Delete(invokerUrl).Go(t, requestHandler) assert.Equal(t, http.StatusNoContent, result.Code()) + assert.False(t, invokerUnderTest.IsInvokerRegistered(path.Base(invokerUrl))) } func TestUpdateInvoker(t *testing.T) { @@ -247,10 +215,45 @@ func TestUpdateInvoker(t *testing.T) { assert.Equal(t, ¬Found, problemDetails.Status) errMsg = "The invoker to update has not been onboarded" assert.Equal(t, &errMsg, problemDetails.Cause) +} + +func TestGetInvokerApiList(t *testing.T) { + publishRegisterMock := publishmocks.PublishRegister{} + publishRegisterMock.On("AreAPIsPublished", mock.Anything).Return(true) + invokerUnderTest, requestHandler := getEcho(&publishRegisterMock) + + // Onboard two invokers + aefProfiles := []publishserviceapi.AefProfile{ + getAefProfile("aefId"), + } + apiId := "apiId" + var apiList invokermanagementapi.APIList = []publishserviceapi.ServiceAPIDescription{ + { + ApiId: &apiId, + AefProfiles: &aefProfiles, + }, + } + newInvoker := getInvoker("invoker a", apiList) + testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler) + aefProfiles = []publishserviceapi.AefProfile{ + getAefProfile("aefId2"), + } + apiId2 := "apiId2" + apiList = []publishserviceapi.ServiceAPIDescription{ + { + ApiId: &apiId2, + AefProfiles: &aefProfiles, + }, + } + newInvoker = getInvoker("invoker b", apiList) + testutil.NewRequest().Post("/onboardedInvokers").WithJsonBody(newInvoker).Go(t, requestHandler) + wantedApiList := invokerUnderTest.GetInvokerApiList("api_invoker_id_invoker_a") + assert.NotNil(t, wantedApiList) + assert.Equal(t, apiId, *(*wantedApiList)[0].ApiId) } -func getEcho(apiRegister publishservice.APIRegister) (*InvokerManager, *echo.Echo) { +func getEcho(publishRegister publishservice.PublishRegister) (*InvokerManager, *echo.Echo) { swagger, err := invokermanagementapi.GetSwagger() if err != nil { fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err) @@ -259,7 +262,7 @@ func getEcho(apiRegister publishservice.APIRegister) (*InvokerManager, *echo.Ech swagger.Servers = nil - im := NewInvokerManager(apiRegister) + im := NewInvokerManager(publishRegister) e := echo.New() e.Use(echomiddleware.Logger()) @@ -268,3 +271,30 @@ func getEcho(apiRegister publishservice.APIRegister) (*InvokerManager, *echo.Ech invokermanagementapi.RegisterHandlers(e, im) return im, e } + +func getAefProfile(aefId string) publishserviceapi.AefProfile { + return publishserviceapi.AefProfile{ + AefId: aefId, + Versions: []publishserviceapi.Version{ + { + Resources: &[]publishserviceapi.Resource{ + { + CommType: "REQUEST_RESPONSE", + }, + }, + }, + }, + } +} + +func getInvoker(invokerInfo string, apiList invokermanagementapi.APIList) invokermanagementapi.APIInvokerEnrolmentDetails { + newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ + ApiInvokerInformation: &invokerInfo, + NotificationDestination: "url", + OnboardingInformation: invokermanagementapi.OnboardingInformation{ + ApiInvokerPublicKey: "key", + }, + ApiList: &apiList, + } + return newInvoker +}