Add check for same service publication

Issue-ID: NONRTRIC-814
Signed-off-by: elinuxhenrik <henrik.b.andersson@est.tech>
Change-Id: I9ed7c24cbe39e42a8b1d5beac3d79d558d1c558b
diff --git a/capifcore/internal/publishservice/publishservice.go b/capifcore/internal/publishservice/publishservice.go
index e3a2314..5cea538 100644
--- a/capifcore/internal/publishservice/publishservice.go
+++ b/capifcore/internal/publishservice/publishservice.go
@@ -158,12 +158,16 @@
 // 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 "
+	errorMsg := "Unable to publish the service due to %s "
 	err := ctx.Bind(&newServiceAPIDescription)
 	if err != nil {
 		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 := newServiceAPIDescription.Validate(); err != nil {
 		return sendCoreError(ctx, http.StatusBadRequest, fmt.Sprintf(errorMsg, err))
 	}
@@ -204,6 +208,17 @@
 	return nil
 }
 
+func (ps *PublishService) isServicePublished(newService publishapi.ServiceAPIDescription) bool {
+	for _, services := range ps.publishedServices {
+		for _, service := range services {
+			if newService.ApiName == service.ApiName {
+				return true
+			}
+		}
+	}
+	return false
+}
+
 func (ps *PublishService) installHelmChart(newServiceAPIDescription publishapi.ServiceAPIDescription, ctx echo.Context) (bool, error) {
 	info := strings.Split(*newServiceAPIDescription.Description, ",")
 	if len(info) == 5 {
diff --git a/capifcore/internal/publishservice/publishservice_test.go b/capifcore/internal/publishservice/publishservice_test.go
index ffbfba3..3c19204 100644
--- a/capifcore/internal/publishservice/publishservice_test.go
+++ b/capifcore/internal/publishservice/publishservice_test.go
@@ -101,6 +101,16 @@
 	assert.NoError(t, err, "error unmarshaling response")
 	assert.Equal(t, *resultService.ApiId, newApiId)
 
+	// Publish the same service again should result in Forbidden
+	result = testutil.NewRequest().Post("/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler)
+
+	assert.Equal(t, http.StatusForbidden, result.Code())
+	var resultError common29122.ProblemDetails
+	err = result.UnmarshalBodyToObject(&resultError)
+	assert.NoError(t, err, "error unmarshaling response")
+	assert.Contains(t, *resultError.Cause, "already published")
+	assert.Equal(t, http.StatusForbidden, *resultError.Status)
+
 	// Delete the service
 	helmManagerMock.On("UninstallHelmChart", mock.Anything, mock.Anything).Return(nil)
 
@@ -141,8 +151,7 @@
 	assert.NoError(t, err, "error unmarshaling response")
 	assert.Contains(t, *resultError.Cause, aefId)
 	assert.Contains(t, *resultError.Cause, "not registered")
-	notFound := http.StatusNotFound
-	assert.Equal(t, &notFound, resultError.Status)
+	assert.Equal(t, http.StatusNotFound, *resultError.Status)
 }
 
 func TestGetServices(t *testing.T) {