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 namespace := "namespace"
63 repoName := "repoName"
64 chartName := "chartName"
65 releaseName := "releaseName"
66 description := fmt.Sprintf("Description,%s,%s,%s,%s", namespace, repoName, chartName, releaseName)
67 newServiceDescription := getServiceAPIDescription(aefId, apiName, description)
69 // Publish a service for provider
70 result = testutil.NewRequest().Post("/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler)
72 assert.Equal(t, http.StatusCreated, result.Code())
73 var resultService publishapi.ServiceAPIDescription
74 err := result.UnmarshalBodyToObject(&resultService)
75 assert.NoError(t, err, "error unmarshaling response")
76 newApiId := "api_id_" + apiName
77 assert.Equal(t, *resultService.ApiId, newApiId)
78 assert.Equal(t, "http://example.com/"+apfId+"/service-apis/"+*resultService.ApiId, result.Recorder.Header().Get(echo.HeaderLocation))
79 newServiceDescription.ApiId = &newApiId
80 wantedAPILIst := []publishapi.ServiceAPIDescription{newServiceDescription}
81 assert.True(t, serviceUnderTest.AreAPIsPublished(&wantedAPILIst))
82 assert.True(t, serviceUnderTest.IsAPIPublished(aefId, apiName))
83 serviceRegisterMock.AssertCalled(t, "GetAefsForPublisher", apfId)
84 helmManagerMock.AssertCalled(t, "InstallHelmChart", namespace, repoName, chartName, releaseName)
85 assert.ElementsMatch(t, []string{aefId}, serviceUnderTest.getAllAefIds())
87 // Check that the service is published for the provider
88 result = testutil.NewRequest().Get("/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
90 assert.Equal(t, http.StatusOK, result.Code())
91 err = result.UnmarshalBodyToObject(&resultService)
92 assert.NoError(t, err, "error unmarshaling response")
93 assert.Equal(t, *resultService.ApiId, newApiId)
96 helmManagerMock.On("UninstallHelmChart", mock.Anything, mock.Anything).Return(nil)
97 result = testutil.NewRequest().Delete("/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
99 assert.Equal(t, http.StatusNoContent, result.Code())
100 helmManagerMock.AssertCalled(t, "UninstallHelmChart", namespace, chartName)
101 assert.Empty(t, serviceUnderTest.getAllAefIds())
103 // Check no services published
104 result = testutil.NewRequest().Get("/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
106 assert.Equal(t, http.StatusNotFound, result.Code())
109 func TestPostUnpublishedServiceWithUnregisteredFunction(t *testing.T) {
112 serviceRegisterMock := serviceMocks.ServiceRegister{}
113 serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{"otherAefId"})
114 _, requestHandler := getEcho(&serviceRegisterMock, nil)
116 newServiceDescription := getServiceAPIDescription(aefId, "apiname", "description")
119 result := testutil.NewRequest().Post("/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler)
121 assert.Equal(t, http.StatusNotFound, result.Code())
122 var resultError common29122.ProblemDetails
123 err := result.UnmarshalBodyToObject(&resultError)
124 assert.NoError(t, err, "error unmarshaling response")
125 assert.Contains(t, *resultError.Cause, aefId)
126 assert.Contains(t, *resultError.Cause, "not registered")
127 notFound := http.StatusNotFound
128 assert.Equal(t, ¬Found, resultError.Status)
131 func TestGetServices(t *testing.T) {
134 serviceRegisterMock := serviceMocks.ServiceRegister{}
135 serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{aefId})
136 _, requestHandler := getEcho(&serviceRegisterMock, nil)
138 // Check no services published for provider
139 result := testutil.NewRequest().Get("/"+apfId+"/service-apis").Go(t, requestHandler)
141 assert.Equal(t, http.StatusNotFound, result.Code())
143 serviceDescription1 := getServiceAPIDescription(aefId, "api1", "Description")
144 serviceDescription2 := getServiceAPIDescription(aefId, "api2", "Description")
146 // Publish a service for provider
147 testutil.NewRequest().Post("/"+apfId+"/service-apis").WithJsonBody(serviceDescription1).Go(t, requestHandler)
148 testutil.NewRequest().Post("/"+apfId+"/service-apis").WithJsonBody(serviceDescription2).Go(t, requestHandler)
150 // Get all services for provider
151 result = testutil.NewRequest().Get("/"+apfId+"/service-apis").Go(t, requestHandler)
152 assert.Equal(t, http.StatusOK, result.Code())
153 var resultServices []publishapi.ServiceAPIDescription
154 err := result.UnmarshalBodyToObject(&resultServices)
155 assert.NoError(t, err, "error unmarshaling response")
156 assert.Len(t, resultServices, 2)
157 apiId1 := "api_id_api1"
158 serviceDescription1.ApiId = &apiId1
159 apiId2 := "api_id_api2"
160 serviceDescription2.ApiId = &apiId2
161 assert.Contains(t, resultServices, serviceDescription1)
162 assert.Contains(t, resultServices, serviceDescription2)
165 func TestGetPublishedServices(t *testing.T) {
166 serviceUnderTest := NewPublishService(nil, nil)
168 profiles := make([]publishapi.AefProfile, 1)
169 serviceDescription := publishapi.ServiceAPIDescription{
170 AefProfiles: &profiles,
172 serviceUnderTest.publishedServices["publisher1"] = []publishapi.ServiceAPIDescription{
175 serviceUnderTest.publishedServices["publisher2"] = []publishapi.ServiceAPIDescription{
178 result := serviceUnderTest.GetAllPublishedServices()
179 assert.Len(t, result, 2)
182 func getEcho(serviceRegister providermanagement.ServiceRegister, helmManager helmmanagement.HelmManager) (*PublishService, *echo.Echo) {
183 swagger, err := publishapi.GetSwagger()
185 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
189 swagger.Servers = nil
191 ps := NewPublishService(serviceRegister, helmManager)
194 e.Use(echomiddleware.Logger())
195 e.Use(middleware.OapiRequestValidator(swagger))
197 publishapi.RegisterHandlers(e, ps)
201 func getServiceAPIDescription(aefId, apiName, description string) publishapi.ServiceAPIDescription {
202 domainName := "domainName"
203 var protocol publishapi.Protocol = "HTTP_1_1"
204 return publishapi.ServiceAPIDescription{
205 AefProfiles: &[]publishapi.AefProfile{
208 DomainName: &domainName,
210 Versions: []publishapi.Version{
213 Resources: &[]publishapi.Resource{
215 CommType: "REQUEST_RESPONSE",
216 Operations: &[]publishapi.Operation{
228 Description: &description,