From d172c2eb8e9072f16caa9d0c3c7b990b3503f879 Mon Sep 17 00:00:00 2001 From: shikha0203 Date: Tue, 29 Nov 2022 16:14:41 +0000 Subject: [PATCH] Add update functionality to publish service Updated the comment in publish service Issue-ID: NONRTRIC-814 Signed-off-by: shikha0203 Change-Id: I9cf6690fb950e4e027e76427151de0bcf8b709d2 --- .../internal/publishservice/publishservice.go | 54 +++++++++++++++++++++- .../internal/publishservice/publishservice_test.go | 31 +++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) diff --git a/capifcore/internal/publishservice/publishservice.go b/capifcore/internal/publishservice/publishservice.go index 2314039..a4b84f1 100644 --- a/capifcore/internal/publishservice/publishservice.go +++ b/capifcore/internal/publishservice/publishservice.go @@ -272,7 +272,59 @@ func (ps *PublishService) ModifyIndAPFPubAPI(ctx echo.Context, apfId string, ser // Update a published service API. func (ps *PublishService) PutApfIdServiceApisServiceApiId(ctx echo.Context, apfId string, serviceApiId string) error { - return ctx.NoContent(http.StatusNotImplemented) + ps.lock.Lock() + defer ps.lock.Unlock() + + pos, publishedService, shouldReturn, returnValue := ps.checkIfServiceIsPublished(apfId, serviceApiId, ctx) + if shouldReturn { + return returnValue + } + + updatedServiceDescription, shouldReturn, returnValue := getServiceFromRequest(ctx) + if shouldReturn { + return returnValue + } + + if updatedServiceDescription.Description != nil { + publishedService.Description = updatedServiceDescription.Description + ps.publishedServices[apfId][pos] = publishedService + } + + err := ctx.JSON(http.StatusOK, ps.publishedServices[apfId][pos]) + if err != nil { + // Something really bad happened, tell Echo that our handler failed + return err + } + + return nil +} + +func (ps *PublishService) checkIfServiceIsPublished(apfId string, serviceApiId string, ctx echo.Context) (int, publishapi.ServiceAPIDescription, bool, error) { + + publishedServices, ok := ps.publishedServices[apfId] + if !ok { + return 0, publishapi.ServiceAPIDescription{}, true, sendCoreError(ctx, http.StatusBadRequest, "Service must be published before updating it") + } else { + for pos, description := range publishedServices { + if *description.ApiId == serviceApiId { + return pos, description, false, nil + + } + + } + + } + return 0, publishapi.ServiceAPIDescription{}, true, sendCoreError(ctx, http.StatusBadRequest, "Service must be published before updating it") + +} + +func getServiceFromRequest(ctx echo.Context) (publishapi.ServiceAPIDescription, bool, error) { + var updatedServiceDescription publishapi.ServiceAPIDescription + err := ctx.Bind(&updatedServiceDescription) + if err != nil { + return publishapi.ServiceAPIDescription{}, true, sendCoreError(ctx, http.StatusBadRequest, "Invalid format for service") + } + return updatedServiceDescription, false, 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 ab92363..7bb0152 100644 --- a/capifcore/internal/publishservice/publishservice_test.go +++ b/capifcore/internal/publishservice/publishservice_test.go @@ -179,6 +179,37 @@ func TestGetPublishedServices(t *testing.T) { assert.Len(t, result, 2) } +func TestUpdateDescription(t *testing.T) { + apfId := "apfId" + serviceApiId := "serviceApiId" + aefId := "aefId" + apiName := "apiName" + description := "description" + serviceRegisterMock := serviceMocks.ServiceRegister{} + serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{aefId, "otherAefId"}) + helmManagerMock := helmMocks.HelmManager{} + helmManagerMock.On("InstallHelmChart", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil) + serviceUnderTest, requestHandler := getEcho(&serviceRegisterMock, &helmManagerMock) + + serviceDescription := getServiceAPIDescription(aefId, apiName, description) + serviceDescription.ApiId = &serviceApiId + serviceUnderTest.publishedServices[apfId] = []publishapi.ServiceAPIDescription{serviceDescription} + + //Modify the service + updatedServiceDescription := getServiceAPIDescription(aefId, apiName, description) + updatedServiceDescription.ApiId = &description + newDescription := "new description" + updatedServiceDescription.Description = &newDescription + result := testutil.NewRequest().Put("/"+apfId+"/service-apis/"+serviceApiId).WithJsonBody(updatedServiceDescription).Go(t, requestHandler) + + var resultService publishapi.ServiceAPIDescription + assert.Equal(t, http.StatusOK, result.Code()) + err := result.UnmarshalBodyToObject(&resultService) + assert.NoError(t, err, "error unmarshaling response") + assert.Equal(t, resultService.Description, &newDescription) + +} + func getEcho(serviceRegister providermanagement.ServiceRegister, helmManager helmmanagement.HelmManager) (*PublishService, *echo.Echo) { swagger, err := publishapi.GetSwagger() if err != nil { -- 2.16.6