2 // ========================LICENSE_START=================================
5 // Copyright (C) 2022: Nordix Foundation
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
11 // http://www.apache.org/licenses/LICENSE-2.0
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 // ========================LICENSE_END===================================
21 package publishservice
29 "oransc.org/nonrtric/sme/internal/providermanagement"
31 "github.com/labstack/echo/v4"
33 publishapi "oransc.org/nonrtric/sme/internal/publishserviceapi"
35 "oransc.org/nonrtric/sme/internal/helmmanagement"
36 helmMocks "oransc.org/nonrtric/sme/internal/helmmanagement/mocks"
37 serviceMocks "oransc.org/nonrtric/sme/internal/providermanagement/mocks"
39 "github.com/deepmap/oapi-codegen/pkg/middleware"
40 "github.com/deepmap/oapi-codegen/pkg/testutil"
41 echomiddleware "github.com/labstack/echo/v4/middleware"
42 "github.com/stretchr/testify/assert"
43 "github.com/stretchr/testify/mock"
46 func TestPublishUnpublishService(t *testing.T) {
48 newApiId := "api_id_app-management"
49 serviceRegisterMock := serviceMocks.ServiceRegister{}
50 serviceRegisterMock.On("IsFunctionRegistered", aefId).Return(true)
51 helmManagerMock := helmMocks.HelmManager{}
52 helmManagerMock.On("InstallHelmChart", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
53 serviceUnderTest, requestHandler := getEcho(&serviceRegisterMock, &helmManagerMock)
55 // Check no services published
56 result := testutil.NewRequest().Get("/aefId/service-apis/"+newApiId).Go(t, requestHandler)
58 assert.Equal(t, http.StatusNotFound, result.Code())
60 domainName := "domain"
61 var protocol publishapi.Protocol = "HTTP_1_1"
62 description := "Description,namespace,repoName,chartName,releaseName"
63 newServiceDescription := publishapi.ServiceAPIDescription{
64 AefProfiles: &[]publishapi.AefProfile{
67 DomainName: &domainName,
69 Versions: []publishapi.Version{
72 Resources: &[]publishapi.Resource{
74 CommType: "REQUEST_RESPONSE",
75 Operations: &[]publishapi.Operation{
86 ApiName: "app-management",
87 Description: &description,
91 result = testutil.NewRequest().Post("/aefId/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler)
93 assert.Equal(t, http.StatusCreated, result.Code())
94 var resultService publishapi.ServiceAPIDescription
95 err := result.UnmarshalBodyToObject(&resultService)
96 assert.NoError(t, err, "error unmarshaling response")
97 assert.Equal(t, *resultService.ApiId, newApiId)
98 assert.Equal(t, "http://example.com/"+aefId+"/service-apis/"+*resultService.ApiId, result.Recorder.Header().Get(echo.HeaderLocation))
99 newServiceDescription.ApiId = &newApiId
100 wantedAPILIst := []publishapi.ServiceAPIDescription{newServiceDescription}
101 assert.True(t, serviceUnderTest.AreAPIsRegistered(&wantedAPILIst))
102 assert.True(t, serviceUnderTest.IsAPIRegistered("aefId", "app-management"))
103 serviceRegisterMock.AssertCalled(t, "IsFunctionRegistered", aefId)
104 helmManagerMock.AssertCalled(t, "InstallHelmChart", "namespace", "repoName", "chartName", "releaseName")
105 assert.ElementsMatch(t, wantedAPILIst, *serviceUnderTest.GetAPIs())
107 // Check that service is published
108 result = testutil.NewRequest().Get("/aefId/service-apis/"+newApiId).Go(t, requestHandler)
110 assert.Equal(t, http.StatusOK, result.Code())
111 err = result.UnmarshalBodyToObject(&resultService)
112 assert.NoError(t, err, "error unmarshaling response")
113 assert.Equal(t, *resultService.ApiId, newApiId)
116 helmManagerMock.On("UninstallHelmChart", mock.Anything, mock.Anything).Return(nil)
117 result = testutil.NewRequest().Delete("/aefId/service-apis/"+newApiId).Go(t, requestHandler)
119 assert.Equal(t, http.StatusNoContent, result.Code())
120 helmManagerMock.AssertCalled(t, "UninstallHelmChart", "namespace", "chartName")
121 assert.Empty(t, *serviceUnderTest.GetAPIs())
123 // Check no services published
124 result = testutil.NewRequest().Get("/aefId/service-apis/"+newApiId).Go(t, requestHandler)
126 assert.Equal(t, http.StatusNotFound, result.Code())
129 func getEcho(serviceRegister providermanagement.ServiceRegister, helmManager helmmanagement.HelmManager) (*PublishService, *echo.Echo) {
130 swagger, err := publishapi.GetSwagger()
132 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
136 swagger.Servers = nil
138 ps := NewPublishService(serviceRegister, helmManager)
141 e.Use(echomiddleware.Logger())
142 e.Use(middleware.OapiRequestValidator(swagger))
144 publishapi.RegisterHandlers(e, ps)