From 30931ba398fc7a3b9269af7120dce7bad027c79e Mon Sep 17 00:00:00 2001 From: shikha0203 Date: Wed, 1 Feb 2023 14:21:23 +0000 Subject: [PATCH] Type validation in publish service Issue-ID: NONRTRIC-814 Signed-off-by: shikha0203 Change-Id: I646b268a250382ff3cc769254a3b9205acd308e0 --- capifcore/internal/publishservice/publishservice.go | 10 ++++++---- .../internal/publishservice/publishservice_test.go | 19 ++++++++++++++++++- .../internal/publishserviceapi/typevalidation.go | 14 ++++++++++++++ .../publishserviceapi/typevalidation_test.go | 21 +++++++++++++++++++++ 4 files changed, 59 insertions(+), 5 deletions(-) create mode 100644 capifcore/internal/publishserviceapi/typevalidation.go create mode 100644 capifcore/internal/publishserviceapi/typevalidation_test.go diff --git a/capifcore/internal/publishservice/publishservice.go b/capifcore/internal/publishservice/publishservice.go index 3898a80..e3a2314 100644 --- a/capifcore/internal/publishservice/publishservice.go +++ b/capifcore/internal/publishservice/publishservice.go @@ -158,18 +158,22 @@ func (ps *PublishService) GetApfIdServiceApis(ctx echo.Context, apfId string) er // Publish a new API. func (ps *PublishService) PostApfIdServiceApis(ctx echo.Context, apfId string) error { var newServiceAPIDescription publishapi.ServiceAPIDescription + errorMsg := "Unable to register the service due to: %s " err := ctx.Bind(&newServiceAPIDescription) if err != nil { - return sendCoreError(ctx, http.StatusBadRequest, "Invalid format for service "+apfId) + return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, "invalid format for service "+apfId)) } + if err := newServiceAPIDescription.Validate(); err != nil { + return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, err)) + } ps.lock.Lock() defer ps.lock.Unlock() registeredFuncs := ps.serviceRegister.GetAefsForPublisher(apfId) for _, profile := range *newServiceAPIDescription.AefProfiles { if !slices.Contains(registeredFuncs, profile.AefId) { - return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf("Function %s not registered", profile.AefId)) + return sendCoreError(ctx, http.StatusNotFound, fmt.Sprintf(errorMsg, fmt.Sprintf("function %s not registered", profile.AefId))) } } @@ -344,7 +348,6 @@ func (ps *PublishService) sendEvent(service publishapi.ServiceAPIDescription, ev } func (ps *PublishService) checkProfilesRegistered(apfId string, updatedProfiles []publishapi.AefProfile) error { - registeredFuncs := ps.serviceRegister.GetAefsForPublisher(apfId) for _, profile := range updatedProfiles { if !slices.Contains(registeredFuncs, profile.AefId) { @@ -352,7 +355,6 @@ func (ps *PublishService) checkProfilesRegistered(apfId string, updatedProfiles } } return nil - } // This function wraps sending of an error in the Error format, and diff --git a/capifcore/internal/publishservice/publishservice_test.go b/capifcore/internal/publishservice/publishservice_test.go index 8b35f55..ffbfba3 100644 --- a/capifcore/internal/publishservice/publishservice_test.go +++ b/capifcore/internal/publishservice/publishservice_test.go @@ -77,7 +77,7 @@ func TestPublishUnpublishService(t *testing.T) { err := result.UnmarshalBodyToObject(&resultService) assert.NoError(t, err, "error unmarshaling response") newApiId := "api_id_" + apiName - assert.Equal(t, *resultService.ApiId, newApiId) + assert.Equal(t, newApiId, *resultService.ApiId) assert.Equal(t, "http://example.com/"+apfId+"/service-apis/"+*resultService.ApiId, result.Recorder.Header().Get(echo.HeaderLocation)) newServiceDescription.ApiId = &newApiId wantedAPILIst := []publishapi.ServiceAPIDescription{newServiceDescription} @@ -357,6 +357,23 @@ func TestUpdateValidServiceWithDeletedFunction(t *testing.T) { assert.Len(t, (*resultService.AefProfiles), 1) assert.False(t, serviceUnderTest.IsAPIPublished("aefId", "path")) +} + +func TestPublishInvalidService(t *testing.T) { + _, _, requestHandler := getEcho(nil, nil) + newServiceDescription := getServiceAPIDescription("aefId", " ", "description") + + // Publish a service + result := testutil.NewRequest().Post("/apfId/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler) + + assert.Equal(t, http.StatusBadRequest, result.Code()) + var resultError common29122.ProblemDetails + err := result.UnmarshalBodyToObject(&resultError) + assert.NoError(t, err, "error unmarshaling response") + assert.Contains(t, *resultError.Cause, "missing") + assert.Contains(t, *resultError.Cause, "apiName") + assert.Equal(t, http.StatusBadRequest, *resultError.Status) + } func getEcho(serviceRegister providermanagement.ServiceRegister, helmManager helmmanagement.HelmManager) (*PublishService, chan eventsapi.EventNotification, *echo.Echo) { swagger, err := publishapi.GetSwagger() diff --git a/capifcore/internal/publishserviceapi/typevalidation.go b/capifcore/internal/publishserviceapi/typevalidation.go new file mode 100644 index 0000000..375a9e3 --- /dev/null +++ b/capifcore/internal/publishserviceapi/typevalidation.go @@ -0,0 +1,14 @@ +package publishserviceapi + +import ( + "errors" + //"fmt" + "strings" +) + +func (sd ServiceAPIDescription) Validate() error { + if len(strings.TrimSpace(sd.ApiName)) == 0 { + return errors.New("ServiceAPIDescription missing required apiName") + } + return nil +} diff --git a/capifcore/internal/publishserviceapi/typevalidation_test.go b/capifcore/internal/publishserviceapi/typevalidation_test.go new file mode 100644 index 0000000..fa5ccff --- /dev/null +++ b/capifcore/internal/publishserviceapi/typevalidation_test.go @@ -0,0 +1,21 @@ +package publishserviceapi + +import ( + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestValidate(t *testing.T) { + serviceDescriptionUnderTest := ServiceAPIDescription{} + err := serviceDescriptionUnderTest.Validate() + if assert.Error(t, err) { + assert.Contains(t, err.Error(), "missing") + assert.Contains(t, err.Error(), "apiName") + } + + serviceDescriptionUnderTest.ApiName = "apiName" + err = serviceDescriptionUnderTest.Validate() + assert.Nil(t, err) + +} -- 2.16.6