X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;f=capifcore%2Finternal%2Finvokermanagement%2Finvokermanagement_test.go;h=0b90de870bf72505ba39604fca94208b65c48359;hb=0b4c4ecb52b1c04037a65644dc8c6c29981d9736;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..0b90de8 100644 --- a/capifcore/internal/invokermanagement/invokermanagement_test.go +++ b/capifcore/internal/invokermanagement/invokermanagement_test.go @@ -23,6 +23,8 @@ import ( "fmt" "net/http" "os" + "path" + "strings" "testing" "oransc.org/nonrtric/capifcore/internal/invokermanagementapi" @@ -39,74 +41,44 @@ import ( "github.com/deepmap/oapi-codegen/pkg/testutil" echomiddleware "github.com/labstack/echo/v4/middleware" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" ) func TestOnboardInvoker(t *testing.T) { - var err error + 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{ + publishedServices := []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", - }, - }, - }, - }, - }, - }, - }, + AefProfiles: &aefProfiles, }, } + publishRegisterMock := publishmocks.PublishRegister{} + publishRegisterMock.On("GetAllPublishedServices").Return(publishedServices) + invokerUnderTest, requestHandler := getEcho(&publishRegisterMock) + invokerInfo := "invoker a" - newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ - ApiInvokerInformation: &invokerInfo, - NotificationDestination: "url", - OnboardingInformation: invokermanagementapi.OnboardingInformation{ - ApiInvokerPublicKey: "key", - }, - ApiList: &apiList, - } + newInvoker := getInvoker(invokerInfo) // 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) + wantedInvokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1) + assert.Equal(t, wantedInvokerId, *resultInvoker.ApiInvokerId) assert.Equal(t, newInvoker.NotificationDestination, resultInvoker.NotificationDestination) assert.Equal(t, newInvoker.OnboardingInformation.ApiInvokerPublicKey, resultInvoker.OnboardingInformation.ApiInvokerPublicKey) - assert.Equal(t, "onboarding_secret_invoker_a", *resultInvoker.OnboardingInformation.OnboardingSecret) + wantedInvokerSecret := "onboarding_secret_" + strings.Replace(invokerInfo, " ", "_", 1) + assert.Equal(t, wantedInvokerSecret, *resultInvoker.OnboardingInformation.OnboardingSecret) 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) + assert.True(t, invokerUnderTest.IsInvokerRegistered(wantedInvokerId)) + assert.True(t, invokerUnderTest.VerifyInvokerSecret(wantedInvokerId, wantedInvokerSecret)) + publishRegisterMock.AssertCalled(t, "GetAllPublishedServices") + assert.Equal(t, invokermanagementapi.APIList(publishedServices), *resultInvoker.ApiList) // Onboard an invoker missing required NotificationDestination, should get 400 with problem details invalidInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ @@ -122,8 +94,8 @@ func TestOnboardInvoker(t *testing.T) { assert.NoError(t, err, "error unmarshaling response") badRequest := http.StatusBadRequest assert.Equal(t, &badRequest, problemDetails.Status) - errMsg := "Invoker missing required NotificationDestination" - assert.Equal(t, &errMsg, problemDetails.Cause) + assert.Contains(t, *problemDetails.Cause, "missing") + assert.Contains(t, *problemDetails.Cause, "NotificationDestination") // Onboard an invoker missing required OnboardingInformation.ApiInvokerPublicKey, should get 400 with problem details invalidInvoker = invokermanagementapi.APIInvokerEnrolmentDetails{ @@ -136,12 +108,14 @@ func TestOnboardInvoker(t *testing.T) { err = result.UnmarshalBodyToObject(&problemDetails) assert.NoError(t, err, "error unmarshaling response") assert.Equal(t, &badRequest, problemDetails.Status) - errMsg = "Invoker missing required OnboardingInformation.ApiInvokerPublicKey" - assert.Equal(t, &errMsg, problemDetails.Cause) + assert.Contains(t, *problemDetails.Cause, "missing") + assert.Contains(t, *problemDetails.Cause, "OnboardingInformation.ApiInvokerPublicKey") } func TestDeleteInvoker(t *testing.T) { - _, requestHandler := getEcho(nil) + publishRegisterMock := publishmocks.PublishRegister{} + publishRegisterMock.On("GetAllPublishedServices").Return([]publishserviceapi.ServiceAPIDescription{}) + invokerUnderTest, requestHandler := getEcho(&publishRegisterMock) newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ NotificationDestination: "url", @@ -152,19 +126,21 @@ 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) { - _, requestHandler := getEcho(nil) + publishRegisterMock := publishmocks.PublishRegister{} + publishRegisterMock.On("GetAllPublishedServices").Return([]publishserviceapi.ServiceAPIDescription{}) + _, requestHandler := getEcho(&publishRegisterMock) newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ NotificationDestination: "url", @@ -178,18 +154,22 @@ func TestUpdateInvoker(t *testing.T) { var resultInvoker invokermanagementapi.APIInvokerEnrolmentDetails result.UnmarshalBodyToObject(&resultInvoker) + // Update the invoker with valid invoker, should return 200 with updated invoker details invokerId := resultInvoker.ApiInvokerId invokerUrl := result.Recorder.Header().Get(echo.HeaderLocation) - - // Update the invoker with valid invoker, should return 200 with invoker details + resultInvoker.ApiList = nil + newNotifURL := "newUrl" + resultInvoker.NotificationDestination = common29122.Uri(newNotifURL) + newPublicKey := "newPublicKey" + resultInvoker.OnboardingInformation.ApiInvokerPublicKey = newPublicKey result = testutil.NewRequest().Put(invokerUrl).WithJsonBody(resultInvoker).Go(t, requestHandler) 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, newInvoker.NotificationDestination, resultInvoker.NotificationDestination) - assert.Equal(t, newInvoker.OnboardingInformation.ApiInvokerPublicKey, resultInvoker.OnboardingInformation.ApiInvokerPublicKey) + assert.Equal(t, newNotifURL, string(resultInvoker.NotificationDestination)) + assert.Equal(t, newPublicKey, resultInvoker.OnboardingInformation.ApiInvokerPublicKey) // Update with an invoker missing required NotificationDestination, should get 400 with problem details validOnboardingInfo := invokermanagementapi.OnboardingInformation{ @@ -207,8 +187,8 @@ func TestUpdateInvoker(t *testing.T) { assert.NoError(t, err, "error unmarshaling response") badRequest := http.StatusBadRequest assert.Equal(t, &badRequest, problemDetails.Status) - errMsg := "Invoker missing required NotificationDestination" - assert.Equal(t, &errMsg, problemDetails.Cause) + assert.Contains(t, *problemDetails.Cause, "missing") + assert.Contains(t, *problemDetails.Cause, "NotificationDestination") // Update with an invoker missing required OnboardingInformation.ApiInvokerPublicKey, should get 400 with problem details invalidInvoker.NotificationDestination = "url" @@ -219,8 +199,8 @@ func TestUpdateInvoker(t *testing.T) { err = result.UnmarshalBodyToObject(&problemDetails) assert.NoError(t, err, "error unmarshaling response") assert.Equal(t, &badRequest, problemDetails.Status) - errMsg = "Invoker missing required OnboardingInformation.ApiInvokerPublicKey" - assert.Equal(t, &errMsg, problemDetails.Cause) + assert.Contains(t, *problemDetails.Cause, "missing") + assert.Contains(t, *problemDetails.Cause, "OnboardingInformation.ApiInvokerPublicKey") // Update with an invoker with other ApiInvokerId than the one provided in the URL, should get 400 with problem details invalidId := "1" @@ -232,8 +212,8 @@ func TestUpdateInvoker(t *testing.T) { err = result.UnmarshalBodyToObject(&problemDetails) assert.NoError(t, err, "error unmarshaling response") assert.Equal(t, &badRequest, problemDetails.Status) - errMsg = "Invoker ApiInvokerId not matching" - assert.Equal(t, &errMsg, problemDetails.Cause) + 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 missingId := "1" @@ -245,12 +225,45 @@ func TestUpdateInvoker(t *testing.T) { assert.NoError(t, err, "error unmarshaling response") notFound := http.StatusNotFound assert.Equal(t, ¬Found, problemDetails.Status) - errMsg = "The invoker to update has not been onboarded" - assert.Equal(t, &errMsg, problemDetails.Cause) + assert.Contains(t, *problemDetails.Cause, "not been onboarded") + assert.Contains(t, *problemDetails.Cause, "invoker") +} + +func TestGetInvokerApiList(t *testing.T) { + aefProfiles1 := []publishserviceapi.AefProfile{ + getAefProfile("aefId"), + } + apiId := "apiId" + apiList := []publishserviceapi.ServiceAPIDescription{ + { + ApiId: &apiId, + AefProfiles: &aefProfiles1, + }, + } + aefProfiles2 := []publishserviceapi.AefProfile{ + getAefProfile("aefId2"), + } + apiId2 := "apiId2" + apiList = append(apiList, publishserviceapi.ServiceAPIDescription{ + ApiId: &apiId2, + AefProfiles: &aefProfiles2, + }) + publishRegisterMock := publishmocks.PublishRegister{} + publishRegisterMock.On("GetAllPublishedServices").Return(apiList) + invokerUnderTest, requestHandler := 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)) + 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 +272,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 +281,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) invokermanagementapi.APIInvokerEnrolmentDetails { + newInvoker := invokermanagementapi.APIInvokerEnrolmentDetails{ + ApiInvokerInformation: &invokerInfo, + NotificationDestination: "url", + OnboardingInformation: invokermanagementapi.OnboardingInformation{ + ApiInvokerPublicKey: "key", + }, + ApiList: nil, + } + return newInvoker +}