+ serviceUnderTest, eventChannel, requestHandler := getEcho(&serviceRegisterMock, &helmManagerMock)
+
+ // Check no services published
+ var resultServices []publishapi.ServiceAPIDescription
+ result := testutil.NewRequest().Get("/"+apfId+"/service-apis").Go(t, requestHandler)
+ assert.Equal(t, http.StatusOK, result.Code())
+
+ // Parse JSON from the response body
+ err := result.UnmarshalJsonToObject(&resultServices)
+ assert.NoError(t, err, "error unmarshaling response")
+
+ // Check if the parsed array is empty
+ assert.Zero(t, len(resultServices))
+
+ apiName := "app-management"
+ namespace := "namespace"
+ repoName := "repoName"
+ chartName := "chartName"
+ releaseName := "releaseName"
+ description := fmt.Sprintf("Description,%s,%s,%s,%s", namespace, repoName, chartName, releaseName)
+ newServiceDescription := getServiceAPIDescription(aefId, apiName, description)
+
+ // Publish a service for provider
+ result = testutil.NewRequest().Post("/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler)
+ assert.Equal(t, http.StatusCreated, result.Code())
+
+ var resultService publishapi.ServiceAPIDescription
+
+ err = result.UnmarshalJsonToObject(&resultService)
+ assert.NoError(t, err, "error unmarshaling response")
+ newApiId := "api_id_" + apiName
+ 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
+ assert.True(t, serviceUnderTest.IsAPIPublished(aefId, apiName))
+ serviceRegisterMock.AssertCalled(t, "GetAefsForPublisher", apfId)
+ helmManagerMock.AssertCalled(t, "InstallHelmChart", namespace, repoName, chartName, releaseName)
+ assert.ElementsMatch(t, []string{aefId}, serviceUnderTest.getAllAefIds())
+ if publishEvent, ok := waitForEvent(eventChannel, 1*time.Second); ok {
+ assert.Fail(t, "No event sent")
+ } else {
+ assert.Equal(t, *resultService.ApiId, (*publishEvent.EventDetail.ApiIds)[0])
+ assert.Equal(t, eventsapi.CAPIFEventSERVICEAPIAVAILABLE, publishEvent.Events)
+ }
+
+ // Check that the service is published for the provider
+ result = testutil.NewRequest().Get("/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
+
+ assert.Equal(t, http.StatusOK, result.Code())
+ err = result.UnmarshalJsonToObject(&resultService)
+ 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.UnmarshalJsonToObject(&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)
+
+ result = testutil.NewRequest().Delete("/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
+
+ assert.Equal(t, http.StatusNoContent, result.Code())
+ helmManagerMock.AssertCalled(t, "UninstallHelmChart", namespace, chartName)
+ assert.Empty(t, serviceUnderTest.getAllAefIds())
+
+ // Check no services published for a provider
+ result = testutil.NewRequest().Get("/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
+
+ if publishEvent, ok := waitForEvent(eventChannel, 1*time.Second); ok {
+ assert.Fail(t, "No event sent")
+ } else {
+ assert.Equal(t, *resultService.ApiId, (*publishEvent.EventDetail.ApiIds)[0])
+ assert.Equal(t, eventsapi.CAPIFEventSERVICEAPIUNAVAILABLE, publishEvent.Events)
+ }
+
+ assert.Equal(t, http.StatusNotFound, result.Code())