Improve error messages and tests
[nonrtric/plt/sme.git] / capifcore / internal / publishservice / publishservice_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2022: Nordix Foundation
6 //   %%
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
10 //
11 //        http://www.apache.org/licenses/LICENSE-2.0
12 //
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===================================
19 //
20
21 package publishservice
22
23 import (
24         "fmt"
25         "net/http"
26         "os"
27         "testing"
28
29         "oransc.org/nonrtric/capifcore/internal/common29122"
30         "oransc.org/nonrtric/capifcore/internal/providermanagement"
31
32         "github.com/labstack/echo/v4"
33
34         publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
35
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"
39
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"
45 )
46
47 func TestPublishUnpublishService(t *testing.T) {
48         apfId := "apfId"
49         aefId := "aefId"
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)
55
56         // Check no services published for provider
57         result := testutil.NewRequest().Get("/"+apfId+"/service-apis").Go(t, requestHandler)
58
59         assert.Equal(t, http.StatusNotFound, result.Code())
60
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)
68
69         // Publish a service for provider
70         result = testutil.NewRequest().Post("/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler)
71
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())
86
87         // Check that the service is published for the provider
88         result = testutil.NewRequest().Get("/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
89
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)
94
95         // Delete the service
96         helmManagerMock.On("UninstallHelmChart", mock.Anything, mock.Anything).Return(nil)
97         result = testutil.NewRequest().Delete("/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
98
99         assert.Equal(t, http.StatusNoContent, result.Code())
100         helmManagerMock.AssertCalled(t, "UninstallHelmChart", namespace, chartName)
101         assert.Empty(t, serviceUnderTest.getAllAefIds())
102
103         // Check no services published
104         result = testutil.NewRequest().Get("/"+apfId+"/service-apis/"+newApiId).Go(t, requestHandler)
105
106         assert.Equal(t, http.StatusNotFound, result.Code())
107 }
108
109 func TestPostUnpublishedServiceWithUnregisteredFunction(t *testing.T) {
110         apfId := "apfId"
111         aefId := "aefId"
112         serviceRegisterMock := serviceMocks.ServiceRegister{}
113         serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{"otherAefId"})
114         _, requestHandler := getEcho(&serviceRegisterMock, nil)
115
116         newServiceDescription := getServiceAPIDescription(aefId, "apiname", "description")
117
118         // Publish a service
119         result := testutil.NewRequest().Post("/"+apfId+"/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler)
120
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, &notFound, resultError.Status)
129 }
130
131 func TestGetServices(t *testing.T) {
132         apfId := "apfId"
133         aefId := "aefId"
134         serviceRegisterMock := serviceMocks.ServiceRegister{}
135         serviceRegisterMock.On("GetAefsForPublisher", apfId).Return([]string{aefId})
136         _, requestHandler := getEcho(&serviceRegisterMock, nil)
137
138         // Check no services published for provider
139         result := testutil.NewRequest().Get("/"+apfId+"/service-apis").Go(t, requestHandler)
140
141         assert.Equal(t, http.StatusNotFound, result.Code())
142
143         serviceDescription1 := getServiceAPIDescription(aefId, "api1", "Description")
144         serviceDescription2 := getServiceAPIDescription(aefId, "api2", "Description")
145
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)
149
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)
163 }
164
165 func getEcho(serviceRegister providermanagement.ServiceRegister, helmManager helmmanagement.HelmManager) (*PublishService, *echo.Echo) {
166         swagger, err := publishapi.GetSwagger()
167         if err != nil {
168                 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
169                 os.Exit(1)
170         }
171
172         swagger.Servers = nil
173
174         ps := NewPublishService(serviceRegister, helmManager)
175
176         e := echo.New()
177         e.Use(echomiddleware.Logger())
178         e.Use(middleware.OapiRequestValidator(swagger))
179
180         publishapi.RegisterHandlers(e, ps)
181         return ps, e
182 }
183
184 func getServiceAPIDescription(aefId, apiName, description string) publishapi.ServiceAPIDescription {
185         domainName := "domainName"
186         var protocol publishapi.Protocol = "HTTP_1_1"
187         return publishapi.ServiceAPIDescription{
188                 AefProfiles: &[]publishapi.AefProfile{
189                         {
190                                 AefId:      aefId,
191                                 DomainName: &domainName,
192                                 Protocol:   &protocol,
193                                 Versions: []publishapi.Version{
194                                         {
195                                                 ApiVersion: "v1",
196                                                 Resources: &[]publishapi.Resource{
197                                                         {
198                                                                 CommType: "REQUEST_RESPONSE",
199                                                                 Operations: &[]publishapi.Operation{
200                                                                         "POST",
201                                                                 },
202                                                                 ResourceName: "app",
203                                                                 Uri:          "app",
204                                                         },
205                                                 },
206                                         },
207                                 },
208                         },
209                 },
210                 ApiName:     apiName,
211                 Description: &description,
212         }
213 }