Add update functionality to publish service 77/9877/2
authorshikha0203 <shivani.khare@est.tech>
Tue, 29 Nov 2022 16:14:41 +0000 (16:14 +0000)
committerShivani Khare <shivani.khare@est.tech>
Fri, 2 Dec 2022 10:00:46 +0000 (10:00 +0000)
Updated the comment in publish service

Issue-ID: NONRTRIC-814
Signed-off-by: shikha0203 <shivani.khare@est.tech>
Change-Id: I9cf6690fb950e4e027e76427151de0bcf8b709d2

capifcore/internal/publishservice/publishservice.go
capifcore/internal/publishservice/publishservice_test.go

index 2314039..a4b84f1 100644 (file)
@@ -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
index ab92363..7bb0152 100644 (file)
@@ -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 {