Improvement: get realm from config file
[nonrtric/plt/sme.git] / capifcore / internal / publishservice / publishservice_test.go
index ffbfba3..8de1e9f 100644 (file)
@@ -52,6 +52,7 @@ func TestPublishUnpublishService(t *testing.T) {
        aefId := "aefId"
        serviceRegisterMock := serviceMocks.ServiceRegister{}
        serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{aefId, "otherAefId"})
+       serviceRegisterMock.On("IsPublishingFunctionRegistered", apfId).Return(true)
        helmManagerMock := helmMocks.HelmManager{}
        helmManagerMock.On("InstallHelmChart", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
        serviceUnderTest, eventChannel, requestHandler := getEcho(&serviceRegisterMock, &helmManagerMock)
@@ -71,7 +72,6 @@ func TestPublishUnpublishService(t *testing.T) {
 
        // 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.UnmarshalBodyToObject(&resultService)
@@ -80,8 +80,6 @@ func TestPublishUnpublishService(t *testing.T) {
        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}
-       assert.True(t, serviceUnderTest.AreAPIsPublished(&wantedAPILIst))
        assert.True(t, serviceUnderTest.IsAPIPublished(aefId, apiName))
        serviceRegisterMock.AssertCalled(t, "GetAefsForPublisher", apfId)
        helmManagerMock.AssertCalled(t, "InstallHelmChart", namespace, repoName, chartName, releaseName)
@@ -101,6 +99,16 @@ func TestPublishUnpublishService(t *testing.T) {
        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)
 
@@ -128,6 +136,7 @@ func TestPostUnpublishedServiceWithUnregisteredFunction(t *testing.T) {
        aefId := "aefId"
        serviceRegisterMock := serviceMocks.ServiceRegister{}
        serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{"otherAefId"})
+       serviceRegisterMock.On("IsPublishingFunctionRegistered", apfId).Return(true)
        _, _, requestHandler := getEcho(&serviceRegisterMock, nil)
 
        newServiceDescription := getServiceAPIDescription(aefId, "apiName", "description")
@@ -141,8 +150,7 @@ func TestPostUnpublishedServiceWithUnregisteredFunction(t *testing.T) {
        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) {
@@ -150,6 +158,7 @@ func TestGetServices(t *testing.T) {
        aefId := "aefId"
        serviceRegisterMock := serviceMocks.ServiceRegister{}
        serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{aefId})
+       serviceRegisterMock.On("IsPublishingFunctionRegistered", apfId).Return(true)
        _, _, requestHandler := getEcho(&serviceRegisterMock, nil)
 
        // Check no services published for provider
@@ -205,6 +214,7 @@ func TestUpdateDescription(t *testing.T) {
 
        serviceRegisterMock := serviceMocks.ServiceRegister{}
        serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{aefId, "otherAefId", "aefIdNew"})
+       serviceRegisterMock.On("IsPublishingFunctionRegistered", apfId).Return(true)
        helmManagerMock := helmMocks.HelmManager{}
        helmManagerMock.On("InstallHelmChart", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
        serviceUnderTest, eventChannel, requestHandler := getEcho(&serviceRegisterMock, &helmManagerMock)
@@ -278,6 +288,7 @@ func TestUpdateValidServiceWithDeletedFunction(t *testing.T) {
        description := "description"
 
        serviceRegisterMock := serviceMocks.ServiceRegister{}
+       serviceRegisterMock.On("IsPublishingFunctionRegistered", apfId).Return(true)
        serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{aefId, "otherAefId", "aefIdNew"})
        helmManagerMock := helmMocks.HelmManager{}
        helmManagerMock.On("InstallHelmChart", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
@@ -360,7 +371,11 @@ func TestUpdateValidServiceWithDeletedFunction(t *testing.T) {
 }
 
 func TestPublishInvalidService(t *testing.T) {
-       _, _, requestHandler := getEcho(nil, nil)
+       apfId := "apfId"
+       serviceRegisterMock := serviceMocks.ServiceRegister{}
+       serviceRegisterMock.On("IsPublishingFunctionRegistered", apfId).Return(true)
+
+       _, _, requestHandler := getEcho(&serviceRegisterMock, nil)
        newServiceDescription := getServiceAPIDescription("aefId", " ", "description")
 
        // Publish a service