Add generated code
[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/providermanagement"
30
31         "github.com/labstack/echo/v4"
32
33         publishapi "oransc.org/nonrtric/capifcore/internal/publishserviceapi"
34
35         "oransc.org/nonrtric/capifcore/internal/helmmanagement"
36         helmMocks "oransc.org/nonrtric/capifcore/internal/helmmanagement/mocks"
37         serviceMocks "oransc.org/nonrtric/capifcore/internal/providermanagement/mocks"
38
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"
44 )
45
46 func TestPublishUnpublishService(t *testing.T) {
47         aefId := "aefId"
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)
54
55         // Check no services published
56         result := testutil.NewRequest().Get("/aefId/service-apis/"+newApiId).Go(t, requestHandler)
57
58         assert.Equal(t, http.StatusNotFound, result.Code())
59
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{
65                         {
66                                 AefId:      aefId,
67                                 DomainName: &domainName,
68                                 Protocol:   &protocol,
69                                 Versions: []publishapi.Version{
70                                         {
71                                                 ApiVersion: "v1",
72                                                 Resources: &[]publishapi.Resource{
73                                                         {
74                                                                 CommType: "REQUEST_RESPONSE",
75                                                                 Operations: &[]publishapi.Operation{
76                                                                         "POST",
77                                                                 },
78                                                                 ResourceName: "app",
79                                                                 Uri:          "app",
80                                                         },
81                                                 },
82                                         },
83                                 },
84                         },
85                 },
86                 ApiName:     "app-management",
87                 Description: &description,
88         }
89
90         // Publish a service
91         result = testutil.NewRequest().Post("/aefId/service-apis").WithJsonBody(newServiceDescription).Go(t, requestHandler)
92
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())
106
107         // Check that service is published
108         result = testutil.NewRequest().Get("/aefId/service-apis/"+newApiId).Go(t, requestHandler)
109
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)
114
115         // Delete a service
116         helmManagerMock.On("UninstallHelmChart", mock.Anything, mock.Anything).Return(nil)
117         result = testutil.NewRequest().Delete("/aefId/service-apis/"+newApiId).Go(t, requestHandler)
118
119         assert.Equal(t, http.StatusNoContent, result.Code())
120         helmManagerMock.AssertCalled(t, "UninstallHelmChart", "namespace", "chartName")
121         assert.Empty(t, *serviceUnderTest.GetAPIs())
122
123         // Check no services published
124         result = testutil.NewRequest().Get("/aefId/service-apis/"+newApiId).Go(t, requestHandler)
125
126         assert.Equal(t, http.StatusNotFound, result.Code())
127 }
128
129 func getEcho(serviceRegister providermanagement.ServiceRegister, helmManager helmmanagement.HelmManager) (*PublishService, *echo.Echo) {
130         swagger, err := publishapi.GetSwagger()
131         if err != nil {
132                 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
133                 os.Exit(1)
134         }
135
136         swagger.Servers = nil
137
138         ps := NewPublishService(serviceRegister, helmManager)
139
140         e := echo.New()
141         e.Use(echomiddleware.Logger())
142         e.Use(middleware.OapiRequestValidator(swagger))
143
144         publishapi.RegisterHandlers(e, ps)
145         return ps, e
146 }