From c21e101767fbde3a091a0ee7fef7782fedbe5b37 Mon Sep 17 00:00:00 2001 From: elinuxhenrik Date: Mon, 6 Feb 2023 09:59:28 +0100 Subject: [PATCH] Refactor check earlier registration Issue-ID: NONRTRIC-814 Signed-off-by: elinuxhenrik Change-Id: Ia1a921c13234f848138607edd4c294a322663d80 --- .../internal/invokermanagement/invokermanagement.go | 2 +- .../internal/invokermanagementapi/typevalidation.go | 4 ++++ .../invokermanagementapi/typevalidation_test.go | 19 +++++++++++++++++++ .../internal/providermanagement/providermanagement.go | 2 +- .../internal/providermanagementapi/typevalidation.go | 4 ++++ .../providermanagementapi/typevalidation_test.go | 15 +++++++++++++++ capifcore/internal/publishservice/publishservice.go | 2 +- .../internal/publishserviceapi/typevalidation.go | 4 ++++ .../internal/publishserviceapi/typevalidation_test.go | 15 +++++++++++++++ 9 files changed, 64 insertions(+), 3 deletions(-) diff --git a/capifcore/internal/invokermanagement/invokermanagement.go b/capifcore/internal/invokermanagement/invokermanagement.go index 5d4ba25..c6f2db3 100644 --- a/capifcore/internal/invokermanagement/invokermanagement.go +++ b/capifcore/internal/invokermanagement/invokermanagement.go @@ -133,7 +133,7 @@ func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error { func (im *InvokerManager) isInvokerOnboarded(newInvoker invokerapi.APIInvokerEnrolmentDetails) bool { for _, invoker := range im.onboardedInvokers { - if newInvoker.OnboardingInformation.ApiInvokerPublicKey == invoker.OnboardingInformation.ApiInvokerPublicKey { + if invoker.IsOnboarded(newInvoker) { return true } } diff --git a/capifcore/internal/invokermanagementapi/typevalidation.go b/capifcore/internal/invokermanagementapi/typevalidation.go index be29481..8bfb784 100644 --- a/capifcore/internal/invokermanagementapi/typevalidation.go +++ b/capifcore/internal/invokermanagementapi/typevalidation.go @@ -41,3 +41,7 @@ func (ied *APIInvokerEnrolmentDetails) Validate() error { return nil } + +func (ied *APIInvokerEnrolmentDetails) IsOnboarded(otherInvoker APIInvokerEnrolmentDetails) bool { + return ied.OnboardingInformation.ApiInvokerPublicKey == otherInvoker.OnboardingInformation.ApiInvokerPublicKey +} diff --git a/capifcore/internal/invokermanagementapi/typevalidation_test.go b/capifcore/internal/invokermanagementapi/typevalidation_test.go index 58cd655..499b0a1 100644 --- a/capifcore/internal/invokermanagementapi/typevalidation_test.go +++ b/capifcore/internal/invokermanagementapi/typevalidation_test.go @@ -53,3 +53,22 @@ func TestValidateInvoker(t *testing.T) { err = invokerUnderTest.Validate() assert.Nil(t, err) } + +func TestIsOnboarded(t *testing.T) { + publicKey := "publicKey" + invokerUnderTest := APIInvokerEnrolmentDetails{ + OnboardingInformation: OnboardingInformation{ + ApiInvokerPublicKey: publicKey, + }, + } + + otherInvoker := APIInvokerEnrolmentDetails{ + OnboardingInformation: OnboardingInformation{ + ApiInvokerPublicKey: "otherPublicKey", + }, + } + assert.False(t, invokerUnderTest.IsOnboarded(otherInvoker)) + + otherInvoker.OnboardingInformation.ApiInvokerPublicKey = publicKey + assert.True(t, invokerUnderTest.IsOnboarded(otherInvoker)) +} diff --git a/capifcore/internal/providermanagement/providermanagement.go b/capifcore/internal/providermanagement/providermanagement.go index 6ca4a7c..5e9211e 100644 --- a/capifcore/internal/providermanagement/providermanagement.go +++ b/capifcore/internal/providermanagement/providermanagement.go @@ -97,7 +97,7 @@ func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error { func (pm *ProviderManager) isProviderRegistered(newProvider provapi.APIProviderEnrolmentDetails) bool { for _, prov := range pm.registeredProviders { - if newProvider.RegSec == prov.RegSec { + if prov.IsRegistered(newProvider) { return true } } diff --git a/capifcore/internal/providermanagementapi/typevalidation.go b/capifcore/internal/providermanagementapi/typevalidation.go index c873f30..3c514a8 100644 --- a/capifcore/internal/providermanagementapi/typevalidation.go +++ b/capifcore/internal/providermanagementapi/typevalidation.go @@ -67,3 +67,7 @@ func (pd APIProviderEnrolmentDetails) validateFunctions() error { } return nil } + +func (pd APIProviderEnrolmentDetails) IsRegistered(otherProvider APIProviderEnrolmentDetails) bool { + return pd.RegSec == otherProvider.RegSec +} diff --git a/capifcore/internal/providermanagementapi/typevalidation_test.go b/capifcore/internal/providermanagementapi/typevalidation_test.go index 7698aab..b79a13b 100644 --- a/capifcore/internal/providermanagementapi/typevalidation_test.go +++ b/capifcore/internal/providermanagementapi/typevalidation_test.go @@ -106,6 +106,21 @@ func TestValidateAPIProviderEnrolmentDetails(t *testing.T) { assert.Nil(t, providerDetailsUnderTest.Validate()) } +func TestIsRegistered(t *testing.T) { + regSec := "regSec" + providerUnderTest := APIProviderEnrolmentDetails{ + RegSec: regSec, + } + + otherProvider := APIProviderEnrolmentDetails{ + RegSec: "otherRegSec", + } + assert.False(t, providerUnderTest.IsRegistered(otherProvider)) + + otherProvider.RegSec = regSec + assert.True(t, providerUnderTest.IsRegistered(otherProvider)) +} + func getProvider() APIProviderEnrolmentDetails { testFuncs := []APIProviderFunctionDetails{ { diff --git a/capifcore/internal/publishservice/publishservice.go b/capifcore/internal/publishservice/publishservice.go index 5cea538..ee3efef 100644 --- a/capifcore/internal/publishservice/publishservice.go +++ b/capifcore/internal/publishservice/publishservice.go @@ -211,7 +211,7 @@ func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) e func (ps *PublishService) isServicePublished(newService publishapi.ServiceAPIDescription) bool { for _, services := range ps.publishedServices { for _, service := range services { - if newService.ApiName == service.ApiName { + if service.IsPublished(newService) { return true } } diff --git a/capifcore/internal/publishserviceapi/typevalidation.go b/capifcore/internal/publishserviceapi/typevalidation.go index ee331ad..ad19324 100644 --- a/capifcore/internal/publishserviceapi/typevalidation.go +++ b/capifcore/internal/publishserviceapi/typevalidation.go @@ -31,3 +31,7 @@ func (sd ServiceAPIDescription) Validate() error { } return nil } + +func (sd ServiceAPIDescription) IsPublished(otherService ServiceAPIDescription) bool { + return sd.ApiName == otherService.ApiName +} diff --git a/capifcore/internal/publishserviceapi/typevalidation_test.go b/capifcore/internal/publishserviceapi/typevalidation_test.go index ef4e35b..39e3619 100644 --- a/capifcore/internal/publishserviceapi/typevalidation_test.go +++ b/capifcore/internal/publishserviceapi/typevalidation_test.go @@ -38,3 +38,18 @@ func TestValidate(t *testing.T) { assert.Nil(t, err) } + +func TestIsServicePublished(t *testing.T) { + apiName := "apiName" + serviceUnderTest := ServiceAPIDescription{ + ApiName: apiName, + } + + otherService := ServiceAPIDescription{ + ApiName: "otherApiName", + } + assert.False(t, serviceUnderTest.IsPublished(otherService)) + + otherService.ApiName = apiName + assert.True(t, serviceUnderTest.IsPublished(otherService)) +} -- 2.16.6