Add GetAefsForPublisher to ServiceRegister 96/9596/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Fri, 11 Nov 2022 09:49:16 +0000 (10:49 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Fri, 11 Nov 2022 09:49:21 +0000 (10:49 +0100)
Issue-ID: NONRTRIC-814
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: I5df308b8fcb49b870ca743af2cea9ab22fd43318

capifcore/.gitignore
capifcore/internal/providermanagement/mocks/ServiceRegister.go
capifcore/internal/providermanagement/providermanagement.go
capifcore/internal/providermanagement/providermanagement_test.go

index db1fbad..84bc744 100644 (file)
@@ -4,4 +4,4 @@ commoncollector
 enumfixer
 specificationfixer
 capifcore
-coverage.txt
\ No newline at end of file
+coverage.*
index b97a0f4..c795ed1 100644 (file)
@@ -9,13 +9,29 @@ type ServiceRegister struct {
        mock.Mock
 }
 
-// IsFunctionRegistered provides a mock function with given fields: aefId
-func (_m *ServiceRegister) IsFunctionRegistered(aefId string) bool {
-       ret := _m.Called(aefId)
+// GetAefsForPublisher provides a mock function with given fields: apfId
+func (_m *ServiceRegister) GetAefsForPublisher(apfId string) []string {
+       ret := _m.Called(apfId)
+
+       var r0 []string
+       if rf, ok := ret.Get(0).(func(string) []string); ok {
+               r0 = rf(apfId)
+       } else {
+               if ret.Get(0) != nil {
+                       r0 = ret.Get(0).([]string)
+               }
+       }
+
+       return r0
+}
+
+// IsFunctionRegistered provides a mock function with given fields: functionId
+func (_m *ServiceRegister) IsFunctionRegistered(functionId string) bool {
+       ret := _m.Called(functionId)
 
        var r0 bool
        if rf, ok := ret.Get(0).(func(string) bool); ok {
-               r0 = rf(aefId)
+               r0 = rf(functionId)
        } else {
                r0 = ret.Get(0).(bool)
        }
index b957961..63ed4d8 100644 (file)
@@ -36,7 +36,8 @@ import (
 
 //go:generate mockery --name ServiceRegister
 type ServiceRegister interface {
-       IsFunctionRegistered(aefId string) bool
+       IsFunctionRegistered(functionId string) bool
+       GetAefsForPublisher(apfId string) []string
 }
 
 type ProviderManager struct {
@@ -50,12 +51,12 @@ func NewProviderManager() *ProviderManager {
        }
 }
 
-func (pm *ProviderManager) IsFunctionRegistered(aefId string) bool {
+func (pm *ProviderManager) IsFunctionRegistered(functionId string) bool {
        registered := false
 out:
        for _, provider := range pm.onboardedProviders {
                for _, registeredFunc := range *provider.ApiProvFuncs {
-                       if *registeredFunc.ApiProvFuncId == aefId {
+                       if *registeredFunc.ApiProvFuncId == functionId {
                                registered = true
                                break out
                        }
@@ -65,6 +66,27 @@ out:
        return registered
 }
 
+func (pm *ProviderManager) GetAefsForPublisher(apfId string) []string {
+       for _, provider := range pm.onboardedProviders {
+               for _, registeredFunc := range *provider.ApiProvFuncs {
+                       if *registeredFunc.ApiProvFuncId == apfId && registeredFunc.ApiProvFuncRole == provapi.ApiProviderFuncRoleAPF {
+                               return getExposedFuncs(provider.ApiProvFuncs)
+                       }
+               }
+       }
+       return nil
+}
+
+func getExposedFuncs(providerFuncs *[]provapi.APIProviderFunctionDetails) []string {
+       exposedFuncs := []string{}
+       for _, registeredFunc := range *providerFuncs {
+               if registeredFunc.ApiProvFuncRole == provapi.ApiProviderFuncRoleAEF {
+                       exposedFuncs = append(exposedFuncs, *registeredFunc.ApiProvFuncId)
+               }
+       }
+       return exposedFuncs
+}
+
 func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error {
        var newProvider provapi.APIProviderEnrolmentDetails
        err := ctx.Bind(&newProvider)
index 069bcc4..8d8c9b4 100644 (file)
@@ -37,39 +37,26 @@ import (
        "github.com/stretchr/testify/assert"
 )
 
+var (
+       domainID      = "domain_id_rApp_domain"
+       otherDomainID = "domain_id_other_domain"
+       domainInfo    = "rApp domain"
+       funcInfoAPF   = "rApp as APF"
+       funcIdAPF     = "APF_id_rApp_as_APF"
+       funcInfoAMF   = "rApp as AMF"
+       funcIdAMF     = "AMF_id_rApp_as_AMF"
+       funcInfoAEF   = "rApp as AEF"
+       funcIdAEF     = "AEF_id_rApp_as_AEF"
+)
+
 func TestProviderHandlingSuccessfully(t *testing.T) {
        managerUnderTest, requestHandler := getEcho()
 
-       domainInfo := "rApp domain"
-       funcInfoAPF := "rApp as APF"
-       funcInfoAMF := "rApp as AMF"
-       funcInfoAEF := "rApp as AEF"
-       testFuncs := []provapi.APIProviderFunctionDetails{
-               {
-                       ApiProvFuncInfo: &funcInfoAPF,
-                       ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
-               },
-               {
-                       ApiProvFuncInfo: &funcInfoAMF,
-                       ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
-               },
-               {
-                       ApiProvFuncInfo: &funcInfoAEF,
-                       ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
-               },
-       }
-       newProvider := provapi.APIProviderEnrolmentDetails{
-               ApiProvDomInfo: &domainInfo,
-               ApiProvFuncs:   &testFuncs,
-       }
+       newProvider := getProvider()
 
        // Register a valid provider
        result := testutil.NewRequest().Post("/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
 
-       domainID := "domain_id_rApp_domain"
-       funcIdAPF := "APF_id_rApp_as_APF"
-       funcIdAMF := "AMF_id_rApp_as_AMF"
-       funcIdAEF := "AEF_id_rApp_as_AEF"
        assert.Equal(t, http.StatusCreated, result.Code())
        var resultProvider provapi.APIProviderEnrolmentDetails
        err := result.UnmarshalBodyToObject(&resultProvider)
@@ -88,10 +75,12 @@ func TestProviderHandlingSuccessfully(t *testing.T) {
        (*newProvider.ApiProvFuncs)[1].ApiProvFuncId = &funcIdAMF
        (*newProvider.ApiProvFuncs)[2].ApiProvFuncId = &funcIdAEF
        newFuncInfoAEF := "new func as AEF"
+       testFuncs := *newProvider.ApiProvFuncs
        testFuncs = append(testFuncs, provapi.APIProviderFunctionDetails{
                ApiProvFuncInfo: &newFuncInfoAEF,
                ApiProvFuncRole: "AEF",
        })
+       newProvider.ApiProvFuncs = &testFuncs
 
        result = testutil.NewRequest().Put("/registrations/"+domainID).WithJsonBody(newProvider).Go(t, requestHandler)
 
@@ -127,6 +116,76 @@ func TestProviderHandlingValidation(t *testing.T) {
        assert.Equal(t, &errMsg, problemDetails.Cause)
 }
 
+func TestGetExposedFunctionsForPublishingFunction(t *testing.T) {
+       managerUnderTest := NewProviderManager()
+
+       managerUnderTest.onboardedProviders[domainID] = getProvider()
+       managerUnderTest.onboardedProviders[otherDomainID] = getOtherProvider()
+
+       exposedFuncs := managerUnderTest.GetAefsForPublisher(funcIdAPF)
+       assert.Equal(t, 1, len(exposedFuncs))
+       assert.Equal(t, funcIdAEF, exposedFuncs[0])
+}
+
+func getProvider() provapi.APIProviderEnrolmentDetails {
+       testFuncs := []provapi.APIProviderFunctionDetails{
+               {
+                       ApiProvFuncId:   &funcIdAPF,
+                       ApiProvFuncInfo: &funcInfoAPF,
+                       ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
+               },
+               {
+                       ApiProvFuncId:   &funcIdAMF,
+                       ApiProvFuncInfo: &funcInfoAMF,
+                       ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
+               },
+               {
+                       ApiProvFuncId:   &funcIdAEF,
+                       ApiProvFuncInfo: &funcInfoAEF,
+                       ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
+               },
+       }
+       return provapi.APIProviderEnrolmentDetails{
+               ApiProvDomId:   &domainID,
+               ApiProvDomInfo: &domainInfo,
+               ApiProvFuncs:   &testFuncs,
+       }
+
+}
+
+func getOtherProvider() provapi.APIProviderEnrolmentDetails {
+       otherDomainInfo := "other domain"
+       otherFuncInfoAPF := "other as APF"
+       otherApfId := "APF_id_other_as_APF"
+       otherFuncInfoAMF := "other as AMF"
+       otherAmfId := "AMF_id_other_as_AMF"
+       otherFuncInfoAEF := "other as AEF"
+       otherAefId := "AEF_id_other_as_AEF"
+       testFuncs := []provapi.APIProviderFunctionDetails{
+               {
+                       ApiProvFuncId:   &otherApfId,
+                       ApiProvFuncInfo: &otherFuncInfoAPF,
+                       ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
+               },
+               {
+                       ApiProvFuncId:   &otherAmfId,
+                       ApiProvFuncInfo: &otherFuncInfoAMF,
+                       ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
+               },
+               {
+                       ApiProvFuncId:   &otherAefId,
+                       ApiProvFuncInfo: &otherFuncInfoAEF,
+                       ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
+               },
+       }
+       return provapi.APIProviderEnrolmentDetails{
+               ApiProvDomId:   &otherDomainID,
+               ApiProvDomInfo: &otherDomainInfo,
+               ApiProvFuncs:   &testFuncs,
+       }
+
+}
+
 func getEcho() (*ProviderManager, *echo.Echo) {
        swagger, err := provapi.GetSwagger()
        if err != nil {