Add get method for ApiList for invoker 38/9538/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Wed, 9 Nov 2022 08:29:58 +0000 (09:29 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Wed, 9 Nov 2022 08:31:54 +0000 (09:31 +0100)
Issue-ID: NONRTRIC-814
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: I4b64198e0508f8985bddc91c074b4cb870069a53

capifcore/internal/invokermanagement/invokermanagement.go
capifcore/internal/invokermanagement/invokermanagement_test.go
capifcore/internal/invokermanagement/mocks/InvokerRegister.go

index 1d8582d..2de6ca6 100644 (file)
@@ -41,6 +41,7 @@ import (
 type InvokerRegister interface {
        IsInvokerRegistered(invokerId string) bool
        VerifyInvokerSecret(invokerId, secret string) bool
+       GetInvokerApiList(invokerId string) *invokerapi.APIList
 }
 
 type InvokerManager struct {
@@ -77,6 +78,14 @@ func (im *InvokerManager) VerifyInvokerSecret(invokerId, secret string) bool {
        return verified
 }
 
+func (im *InvokerManager) GetInvokerApiList(invokerId string) *invokerapi.APIList {
+       invoker, ok := im.onboardedInvokers[invokerId]
+       if ok {
+               return invoker.ApiList
+       }
+       return nil
+}
+
 func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
        var newInvoker invokerapi.APIInvokerEnrolmentDetails
        err := ctx.Bind(&newInvoker)
@@ -84,8 +93,8 @@ func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
                return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for invoker")
        }
 
-       shouldReturn, coreError := im.validateInvoker(newInvoker, ctx)
-       if shouldReturn {
+       coreError := im.validateInvoker(newInvoker, ctx)
+       if coreError != nil {
                return coreError
        }
 
@@ -134,8 +143,8 @@ func (im *InvokerManager) PutOnboardedInvokersOnboardingId(ctx echo.Context, onb
                return sendCoreError(ctx, http.StatusBadRequest, "Invoker ApiInvokerId not matching")
        }
 
-       shouldReturn, coreError := im.validateInvoker(invoker, ctx)
-       if shouldReturn {
+       coreError := im.validateInvoker(invoker, ctx)
+       if coreError != nil {
                return coreError
        }
 
@@ -161,20 +170,20 @@ func (im *InvokerManager) ModifyIndApiInvokeEnrolment(ctx echo.Context, onboardi
        return ctx.NoContent(http.StatusNotImplemented)
 }
 
-func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) (bool, error) {
+func (im *InvokerManager) validateInvoker(invoker invokerapi.APIInvokerEnrolmentDetails, ctx echo.Context) error {
        if invoker.NotificationDestination == "" {
-               return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required NotificationDestination")
+               return sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required NotificationDestination")
        }
 
        if invoker.OnboardingInformation.ApiInvokerPublicKey == "" {
-               return true, sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required OnboardingInformation.ApiInvokerPublicKey")
+               return sendCoreError(ctx, http.StatusBadRequest, "Invoker missing required OnboardingInformation.ApiInvokerPublicKey")
        }
 
        if !im.areAPIsRegistered(invoker.ApiList) {
-               return true, sendCoreError(ctx, http.StatusBadRequest, "Some APIs needed by invoker are not registered")
+               return sendCoreError(ctx, http.StatusBadRequest, "Some APIs needed by invoker are not registered")
        }
 
-       return false, nil
+       return nil
 }
 
 func (im *InvokerManager) areAPIsRegistered(apis *invokerapi.APIList) bool {
index 2c95658..4aae429 100644 (file)
@@ -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
-       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"
+       aefProfiles := []publishserviceapi.AefProfile{
+               getAefProfile("aefId"),
+       }
+       apiId := "apiId"
        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)
@@ -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,7 +215,42 @@ func TestUpdateInvoker(t *testing.T) {
        assert.Equal(t, &notFound, problemDetails.Status)
        errMsg = "The invoker to update has not been onboarded"
        assert.Equal(t, &errMsg, problemDetails.Cause)
+}
+
+func TestGetInvokerApiList(t *testing.T) {
+       apiRegisterMock := publishmocks.APIRegister{}
+       apiRegisterMock.On("AreAPIsRegistered", mock.Anything).Return(true)
+       invokerUnderTest, requestHandler := getEcho(&apiRegisterMock)
+
+       // 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) {
@@ -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
+}
index 3eb317a..123dd9a 100644 (file)
@@ -2,13 +2,32 @@
 
 package mocks
 
-import mock "github.com/stretchr/testify/mock"
+import (
+       mock "github.com/stretchr/testify/mock"
+       invokermanagementapi "oransc.org/nonrtric/capifcore/internal/invokermanagementapi"
+)
 
 // InvokerRegister is an autogenerated mock type for the InvokerRegister type
 type InvokerRegister struct {
        mock.Mock
 }
 
+// GetInvokerApiList provides a mock function with given fields: invokerId
+func (_m *InvokerRegister) GetInvokerApiList(invokerId string) *invokermanagementapi.APIList {
+       ret := _m.Called(invokerId)
+
+       var r0 *invokermanagementapi.APIList
+       if rf, ok := ret.Get(0).(func(string) *invokermanagementapi.APIList); ok {
+               r0 = rf(invokerId)
+       } else {
+               if ret.Get(0) != nil {
+                       r0 = ret.Get(0).(*invokermanagementapi.APIList)
+               }
+       }
+
+       return r0
+}
+
 // IsInvokerRegistered provides a mock function with given fields: invokerId
 func (_m *InvokerRegister) IsInvokerRegistered(invokerId string) bool {
        ret := _m.Called(invokerId)