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/capifcore/internal/common29122"
30 "oransc.org/nonrtric/capifcore/internal/providermanagement"
32 "github.com/labstack/echo/v4"
34 publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
36 "oransc.org/nonrtric/capifcore/internal/helmmanagement"
37 helmMocks "oransc.org/nonrtric/capifcore/internal/helmmanagement/mocks"
38 serviceMocks "oransc.org/nonrtric/capifcore/internal/providermanagement/mocks"
40 "github.com/deepmap/oapi-codegen/pkg/middleware"
41 "github.com/deepmap/oapi-codegen/pkg/testutil"
42 echomiddleware "github.com/labstack/echo/v4/middleware"
43 "github.com/stretchr/testify/assert"
44 "github.com/stretchr/testify/mock"
47 func TestPublishUnpublishService(t *testing.T) {
50 serviceRegisterMock := serviceMocks.ServiceRegister{}
51 serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{aefId, "otherAefId"})
52 helmManagerMock := helmMocks.HelmManager{}
53 helmManagerMock.On("InstallHelmChart", mock.Anything, mock.Anything, mock.Anything, mock.Anything).Return(nil)
54 serviceUnderTest, requestHandler := getEcho(&serviceRegisterMock, &helmManagerMock)
56 // Check no services published for provider
57 result := testutil.NewRequest().Get("/"+apfId+"/service-apis").Go(t, requestHandler)
59 assert.Equal(t, http.StatusNotFound, result.Code())
61 apiName := "app-management"
62 description := "Description,namespace,repoName,chartName,releaseName"
63 newServiceDescription := getServiceAPIDescription(aefId, apiName, description)
65 // Publish a service for provider
66 result = testutil.NewRequest().Post("/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler)
68 assert.Equal(t, http.StatusCreated, result.Code())
69 var resultService publishapi.ServiceAPIDescription
70 err := result.UnmarshalBodyToObject(&resultService)
71 assert.NoError(t, err, "error unmarshaling response")
72 newApiId := "api_id_app-management"
73 assert.Equal(t, *resultService.ApiId, newApiId)
74 assert.Equal(t, "http://example.com/"+apfId+"/service-apis/"+*resultService.ApiId, result.Recorder.Header().Get(echo.HeaderLocation))
75 newServiceDescription.ApiId = &newApiId
76 wantedAPILIst := []publishapi.ServiceAPIDescription{newServiceDescription}
77 assert.True(t, serviceUnderTest.AreAPIsPublished(&wantedAPILIst))
78 assert.True(t, serviceUnderTest.IsAPIPublished("aefId", "app-management"))
79 serviceRegisterMock.AssertCalled(t, "GetAefsForPublisher", apfId)
80 helmManagerMock.AssertCalled(t, "InstallHelmChart", "namespace", "repoName", "chartName", "releaseName")
81 assert.ElementsMatch(t, []string{aefId}, serviceUnderTest.getAllAefIds())
83 // Check that the service is published for the provider
84 result = testutil.NewRequest().Get("/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
86 assert.Equal(t, http.StatusOK, result.Code())
87 err = result.UnmarshalBodyToObject(&resultService)
88 assert.NoError(t, err, "error unmarshaling response")
89 assert.Equal(t, *resultService.ApiId, newApiId)
92 helmManagerMock.On("UninstallHelmChart", mock.Anything, mock.Anything).Return(nil)
93 result = testutil.NewRequest().Delete("/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
95 assert.Equal(t, http.StatusNoContent, result.Code())
96 helmManagerMock.AssertCalled(t, "UninstallHelmChart", "namespace", "chartName")
97 assert.Empty(t, serviceUnderTest.getAllAefIds())
99 // Check no services published
100 result = testutil.NewRequest().Get("/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
102 assert.Equal(t, http.StatusNotFound, result.Code())
105 func TestPostUnpublishedServiceWithUnregisteredFunction(t *testing.T) {
108 serviceRegisterMock := serviceMocks.ServiceRegister{}
109 serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{"otherAefId"})
110 _, requestHandler := getEcho(&serviceRegisterMock, nil)
112 newServiceDescription := getServiceAPIDescription(aefId, "apiname", "description")
115 result := testutil.NewRequest().Post("/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler)
117 assert.Equal(t, http.StatusNotFound, result.Code())
118 var resultError common29122.ProblemDetails
119 err := result.UnmarshalBodyToObject(&resultError)
120 assert.NoError(t, err, "error unmarshaling response")
121 errMsg := "Function not registered, aefId"
122 assert.Equal(t, &errMsg, resultError.Cause)
123 notFound := http.StatusNotFound
124 assert.Equal(t, ¬Found, resultError.Status)
127 func TestGetServices(t *testing.T) {
130 serviceRegisterMock := serviceMocks.ServiceRegister{}
131 serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{aefId})
132 _, requestHandler := getEcho(&serviceRegisterMock, nil)
134 // Check no services published for provider
135 result := testutil.NewRequest().Get("/"+apfId+"/service-apis").Go(t, requestHandler)
137 assert.Equal(t, http.StatusNotFound, result.Code())
139 serviceDescription1 := getServiceAPIDescription(aefId, "api1", "Description")
140 serviceDescription2 := getServiceAPIDescription(aefId, "api2", "Description")
142 // Publish a service for provider
143 testutil.NewRequest().Post("/"+apfId+"/service-apis").WithJsonBody(serviceDescription1).Go(t, requestHandler)
144 testutil.NewRequest().Post("/"+apfId+"/service-apis").WithJsonBody(serviceDescription2).Go(t, requestHandler)
146 // Get all services for provider
147 result = testutil.NewRequest().Get("/"+apfId+"/service-apis").Go(t, requestHandler)
148 assert.Equal(t, http.StatusOK, result.Code())
149 var resultServices []publishapi.ServiceAPIDescription
150 err := result.UnmarshalBodyToObject(&resultServices)
151 assert.NoError(t, err, "error unmarshaling response")
152 assert.Len(t, resultServices, 2)
153 apiId1 := "api_id_api1"
154 serviceDescription1.ApiId = &apiId1
155 apiId2 := "api_id_api2"
156 serviceDescription2.ApiId = &apiId2
157 assert.Contains(t, resultServices, serviceDescription1)
158 assert.Contains(t, resultServices, serviceDescription2)
161 func getEcho(serviceRegister providermanagement.ServiceRegister, helmManager helmmanagement.HelmManager) (*PublishService, *echo.Echo) {
162 swagger, err := publishapi.GetSwagger()
164 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
168 swagger.Servers = nil
170 ps := NewPublishService(serviceRegister, helmManager)
173 e.Use(echomiddleware.Logger())
174 e.Use(middleware.OapiRequestValidator(swagger))
176 publishapi.RegisterHandlers(e, ps)
180 func getServiceAPIDescription(aefId, apiName, description string) publishapi.ServiceAPIDescription {
181 domainName := "domainName"
182 var protocol publishapi.Protocol = "HTTP_1_1"
183 return publishapi.ServiceAPIDescription{
184 AefProfiles: &[]publishapi.AefProfile{
187 DomainName: &domainName,
189 Versions: []publishapi.Version{
192 Resources: &[]publishapi.Resource{
194 CommType: "REQUEST_RESPONSE",
195 Operations: &[]publishapi.Operation{
207 Description: &description,