Add GetAefsForPublisher to ServiceRegister

Issue-ID: NONRTRIC-814
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: I5df308b8fcb49b870ca743af2cea9ab22fd43318
diff --git a/capifcore/.gitignore b/capifcore/.gitignore
index db1fbad..84bc744 100644
--- a/capifcore/.gitignore
+++ b/capifcore/.gitignore
@@ -4,4 +4,4 @@
 enumfixer
 specificationfixer
 capifcore
-coverage.txt
\ No newline at end of file
+coverage.*
diff --git a/capifcore/internal/providermanagement/mocks/ServiceRegister.go b/capifcore/internal/providermanagement/mocks/ServiceRegister.go
index b97a0f4..c795ed1 100644
--- a/capifcore/internal/providermanagement/mocks/ServiceRegister.go
+++ b/capifcore/internal/providermanagement/mocks/ServiceRegister.go
@@ -9,13 +9,29 @@
 	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)
 	}
diff --git a/capifcore/internal/providermanagement/providermanagement.go b/capifcore/internal/providermanagement/providermanagement.go
index b957961..63ed4d8 100644
--- a/capifcore/internal/providermanagement/providermanagement.go
+++ b/capifcore/internal/providermanagement/providermanagement.go
@@ -36,7 +36,8 @@
 
 //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 (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 @@
 	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)
diff --git a/capifcore/internal/providermanagement/providermanagement_test.go b/capifcore/internal/providermanagement/providermanagement_test.go
index 069bcc4..8d8c9b4 100644
--- a/capifcore/internal/providermanagement/providermanagement_test.go
+++ b/capifcore/internal/providermanagement/providermanagement_test.go
@@ -37,39 +37,26 @@
 	"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 @@
 	(*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 @@
 	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 {