Type validation in publish service 23/10323/2
authorshikha0203 <shivani.khare@est.tech>
Wed, 1 Feb 2023 14:21:23 +0000 (14:21 +0000)
committerHenrik Andersson <henrik.b.andersson@est.tech>
Wed, 1 Feb 2023 15:00:28 +0000 (15:00 +0000)
Issue-ID: NONRTRIC-814
Signed-off-by: shikha0203 <shivani.khare@est.tech>
Change-Id: I646b268a250382ff3cc769254a3b9205acd308e0

capifcore/internal/publishservice/publishservice.go
capifcore/internal/publishservice/publishservice_test.go
capifcore/internal/publishserviceapi/typevalidation.go [new file with mode: 0644]
capifcore/internal/publishserviceapi/typevalidation_test.go [new file with mode: 0644]

index 3898a80..e3a2314 100644 (file)
@@ -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
index 8b35f55..ffbfba3 100644 (file)
@@ -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 (file)
index 0000000..375a9e3
--- /dev/null
@@ -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 (file)
index 0000000..fa5ccff
--- /dev/null
@@ -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)
+
+}