Refactor check earlier registration 04/10404/1
authorelinuxhenrik <henrik.b.andersson@est.tech>
Wed, 8 Feb 2023 13:26:24 +0000 (14:26 +0100)
committerelinuxhenrik <henrik.b.andersson@est.tech>
Wed, 8 Feb 2023 13:26:30 +0000 (14:26 +0100)
Issue-ID: NONRTRIC-814
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: Ic9f6038faa123325a0c8a8be7a7f5cc5f7a586a0

capifcore/internal/invokermanagement/invokermanagement.go
capifcore/internal/invokermanagementapi/typevalidation.go
capifcore/internal/invokermanagementapi/typevalidation_test.go
capifcore/internal/providermanagement/providermanagement.go
capifcore/internal/providermanagementapi/typevalidation.go
capifcore/internal/providermanagementapi/typevalidation_test.go
capifcore/internal/publishservice/publishservice.go
capifcore/internal/publishserviceapi/typevalidation.go
capifcore/internal/publishserviceapi/typevalidation_test.go

index 1fbe2f0..43bdc02 100644 (file)
@@ -106,8 +106,8 @@ func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
                return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for invoker"))
        }
 
-       if im.isInvokerOnboarded(newInvoker) {
-               return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, "invoker already onboarded"))
+       if err := im.isInvokerOnboarded(newInvoker); err != nil {
+               return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, err))
        }
 
        if err := im.validateInvoker(newInvoker, ctx); err != nil {
@@ -129,13 +129,13 @@ func (im *InvokerManager) PostOnboardedInvokers(ctx echo.Context) error {
        return nil
 }
 
-func (im *InvokerManager) isInvokerOnboarded(newInvoker invokerapi.APIInvokerEnrolmentDetails) bool {
+func (im *InvokerManager) isInvokerOnboarded(newInvoker invokerapi.APIInvokerEnrolmentDetails) error {
        for _, invoker := range im.onboardedInvokers {
-               if invoker.IsOnboarded(newInvoker) {
-                       return true
+               if err := invoker.ValidateAlreadyOnboarded(newInvoker); err != nil {
+                       return err
                }
        }
-       return false
+       return nil
 }
 
 func (im *InvokerManager) prepareNewInvoker(newInvoker *invokerapi.APIInvokerEnrolmentDetails) {
index 8bfb784..10db338 100644 (file)
@@ -42,6 +42,9 @@ func (ied *APIInvokerEnrolmentDetails) Validate() error {
        return nil
 }
 
-func (ied *APIInvokerEnrolmentDetails) IsOnboarded(otherInvoker APIInvokerEnrolmentDetails) bool {
-       return ied.OnboardingInformation.ApiInvokerPublicKey == otherInvoker.OnboardingInformation.ApiInvokerPublicKey
+func (ied *APIInvokerEnrolmentDetails) ValidateAlreadyOnboarded(otherInvoker APIInvokerEnrolmentDetails) error {
+       if ied.OnboardingInformation.ApiInvokerPublicKey == otherInvoker.OnboardingInformation.ApiInvokerPublicKey {
+               return errors.New("invoker with identical public key already onboarded")
+       }
+       return nil
 }
index 499b0a1..b6b491d 100644 (file)
@@ -54,7 +54,7 @@ func TestValidateInvoker(t *testing.T) {
        assert.Nil(t, err)
 }
 
-func TestIsOnboarded(t *testing.T) {
+func TestValidateAlreadyOnboarded(t *testing.T) {
        publicKey := "publicKey"
        invokerUnderTest := APIInvokerEnrolmentDetails{
                OnboardingInformation: OnboardingInformation{
@@ -67,8 +67,8 @@ func TestIsOnboarded(t *testing.T) {
                        ApiInvokerPublicKey: "otherPublicKey",
                },
        }
-       assert.False(t, invokerUnderTest.IsOnboarded(otherInvoker))
+       assert.Nil(t, invokerUnderTest.ValidateAlreadyOnboarded(otherInvoker))
 
        otherInvoker.OnboardingInformation.ApiInvokerPublicKey = publicKey
-       assert.True(t, invokerUnderTest.IsOnboarded(otherInvoker))
+       assert.NotNil(t, invokerUnderTest.ValidateAlreadyOnboarded(otherInvoker))
 }
index 5e9211e..d5e7a63 100644 (file)
@@ -76,8 +76,8 @@ func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error {
                return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errMsg, "invalid format for provider"))
        }
 
-       if pm.isProviderRegistered(newProvider) {
-               return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, "provider already registered"))
+       if err := pm.isProviderRegistered(newProvider); err != nil {
+               return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errMsg, err))
        }
 
        if err := newProvider.Validate(); err != nil {
@@ -95,13 +95,13 @@ func (pm *ProviderManager) PostRegistrations(ctx echo.Context) error {
        return nil
 }
 
-func (pm *ProviderManager) isProviderRegistered(newProvider provapi.APIProviderEnrolmentDetails) bool {
+func (pm *ProviderManager) isProviderRegistered(newProvider provapi.APIProviderEnrolmentDetails) error {
        for _, prov := range pm.registeredProviders {
-               if prov.IsRegistered(newProvider) {
-                       return true
+               if err := prov.ValidateAlreadyRegistered(newProvider); err != nil {
+                       return err
                }
        }
-       return false
+       return nil
 }
 
 func (pm *ProviderManager) prepareNewProvider(newProvider *provapi.APIProviderEnrolmentDetails) {
index 3c514a8..bb32991 100644 (file)
@@ -68,6 +68,9 @@ func (pd APIProviderEnrolmentDetails) validateFunctions() error {
        return nil
 }
 
-func (pd APIProviderEnrolmentDetails) IsRegistered(otherProvider APIProviderEnrolmentDetails) bool {
-       return pd.RegSec == otherProvider.RegSec
+func (pd APIProviderEnrolmentDetails) ValidateAlreadyRegistered(otherProvider APIProviderEnrolmentDetails) error {
+       if pd.RegSec == otherProvider.RegSec {
+               return errors.New("provider with identical regSec already registered")
+       }
+       return nil
 }
index b79a13b..a5a4e3c 100644 (file)
@@ -106,7 +106,7 @@ func TestValidateAPIProviderEnrolmentDetails(t *testing.T) {
        assert.Nil(t, providerDetailsUnderTest.Validate())
 }
 
-func TestIsRegistered(t *testing.T) {
+func TestValidateAlreadyRegistered(t *testing.T) {
        regSec := "regSec"
        providerUnderTest := APIProviderEnrolmentDetails{
                RegSec: regSec,
@@ -115,10 +115,10 @@ func TestIsRegistered(t *testing.T) {
        otherProvider := APIProviderEnrolmentDetails{
                RegSec: "otherRegSec",
        }
-       assert.False(t, providerUnderTest.IsRegistered(otherProvider))
+       assert.Nil(t, providerUnderTest.ValidateAlreadyRegistered(otherProvider))
 
        otherProvider.RegSec = regSec
-       assert.True(t, providerUnderTest.IsRegistered(otherProvider))
+       assert.NotNil(t, providerUnderTest.ValidateAlreadyRegistered(otherProvider))
 }
 
 func getProvider() APIProviderEnrolmentDetails {
index bf79899..7960f12 100644 (file)
@@ -118,8 +118,8 @@ func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) e
                return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, "invalid format for service "+apfId))
        }
 
-       if ps.isServicePublished(newServiceAPIDescription) {
-               return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errorMsg, "service already published"))
+       if err := ps.isServicePublished(newServiceAPIDescription); err != nil {
+               return sendCoreError(ctx, http.StatusForbidden, fmt.Sprintf(errorMsg, err))
        }
 
        if err := newServiceAPIDescription.Validate(); err != nil {
@@ -161,15 +161,15 @@ func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) e
        return nil
 }
 
-func (ps *PublishService) isServicePublished(newService publishapi.ServiceAPIDescription) bool {
+func (ps *PublishService) isServicePublished(newService publishapi.ServiceAPIDescription) error {
        for _, services := range ps.publishedServices {
                for _, service := range services {
-                       if service.IsPublished(newService) {
-                               return true
+                       if err := service.ValidateAlreadyPublished(newService); err != nil {
+                               return err
                        }
                }
        }
-       return false
+       return nil
 }
 
 func (ps *PublishService) installHelmChart(newServiceAPIDescription publishapi.ServiceAPIDescription, ctx echo.Context) (bool, error) {
index ad19324..287a07d 100644 (file)
@@ -32,6 +32,9 @@ func (sd ServiceAPIDescription) Validate() error {
        return nil
 }
 
-func (sd ServiceAPIDescription) IsPublished(otherService ServiceAPIDescription) bool {
-       return sd.ApiName == otherService.ApiName
+func (sd ServiceAPIDescription) ValidateAlreadyPublished(otherService ServiceAPIDescription) error {
+       if sd.ApiName == otherService.ApiName {
+               return errors.New("service with identical apiName is already published")
+       }
+       return nil
 }
index 39e3619..d7b59d5 100644 (file)
@@ -39,7 +39,7 @@ func TestValidate(t *testing.T) {
 
 }
 
-func TestIsServicePublished(t *testing.T) {
+func TestValidateAlreadyPublished(t *testing.T) {
        apiName := "apiName"
        serviceUnderTest := ServiceAPIDescription{
                ApiName: apiName,
@@ -48,8 +48,8 @@ func TestIsServicePublished(t *testing.T) {
        otherService := ServiceAPIDescription{
                ApiName: "otherApiName",
        }
-       assert.False(t, serviceUnderTest.IsPublished(otherService))
+       assert.Nil(t, serviceUnderTest.ValidateAlreadyPublished(otherService))
 
        otherService.ApiName = apiName
-       assert.True(t, serviceUnderTest.IsPublished(otherService))
+       assert.NotNil(t, serviceUnderTest.ValidateAlreadyPublished(otherService))
 }