Add more provider update functionality
[nonrtric/plt/sme.git] / capifcore / internal / providermanagement / providermanagement_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 providermanagement
22
23 import (
24         "fmt"
25         "net/http"
26         "os"
27         "testing"
28
29         "github.com/labstack/echo/v4"
30
31         "oransc.org/nonrtric/capifcore/internal/common29122"
32         provapi "oransc.org/nonrtric/capifcore/internal/providermanagementapi"
33
34         "github.com/deepmap/oapi-codegen/pkg/middleware"
35         "github.com/deepmap/oapi-codegen/pkg/testutil"
36         echomiddleware "github.com/labstack/echo/v4/middleware"
37         "github.com/stretchr/testify/assert"
38 )
39
40 var (
41         domainID      = "domain_id_rApp_domain"
42         otherDomainID = "domain_id_other_domain"
43         domainInfo    = "rApp domain"
44         funcInfoAPF   = "rApp as APF"
45         funcIdAPF     = "APF_id_rApp_as_APF"
46         funcInfoAMF   = "rApp as AMF"
47         funcIdAMF     = "AMF_id_rApp_as_AMF"
48         funcInfoAEF   = "rApp as AEF"
49         funcIdAEF     = "AEF_id_rApp_as_AEF"
50 )
51
52 func TestRegisterValidProvider(t *testing.T) {
53         managerUnderTest, requestHandler := getEcho()
54
55         newProvider := getProvider()
56
57         // Register a valid provider
58         result := testutil.NewRequest().Post("/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
59
60         assert.Equal(t, http.StatusCreated, result.Code())
61         var resultProvider provapi.APIProviderEnrolmentDetails
62         err := result.UnmarshalBodyToObject(&resultProvider)
63         assert.NoError(t, err, "error unmarshaling response")
64         assert.Equal(t, *resultProvider.ApiProvDomId, domainID)
65         assert.Equal(t, *(*resultProvider.ApiProvFuncs)[0].ApiProvFuncId, funcIdAPF)
66         assert.Equal(t, *(*resultProvider.ApiProvFuncs)[1].ApiProvFuncId, funcIdAMF)
67         assert.Equal(t, *(*resultProvider.ApiProvFuncs)[2].ApiProvFuncId, funcIdAEF)
68         assert.Empty(t, resultProvider.FailReason)
69         assert.Equal(t, "http://example.com/registrations/"+*resultProvider.ApiProvDomId, result.Recorder.Header().Get(echo.HeaderLocation))
70         assert.True(t, managerUnderTest.IsFunctionRegistered("APF_id_rApp_as_APF"))
71 }
72
73 func TestUpdateValidProvider(t *testing.T) {
74         managerUnderTest, requestHandler := getEcho()
75
76         provider := getProvider()
77         provider.ApiProvDomId = &domainID
78         (*provider.ApiProvFuncs)[0].ApiProvFuncId = &funcIdAPF
79         (*provider.ApiProvFuncs)[1].ApiProvFuncId = &funcIdAMF
80         (*provider.ApiProvFuncs)[2].ApiProvFuncId = &funcIdAEF
81         managerUnderTest.onboardedProviders[domainID] = provider
82
83         // Modify the provider
84         updatedProvider := getProvider()
85         updatedProvider.ApiProvDomId = &domainID
86         (*updatedProvider.ApiProvFuncs)[0].ApiProvFuncId = &funcIdAPF
87         (*updatedProvider.ApiProvFuncs)[1].ApiProvFuncId = &funcIdAMF
88         (*updatedProvider.ApiProvFuncs)[2].ApiProvFuncId = &funcIdAEF
89         newDomainInfo := "New domain info"
90         updatedProvider.ApiProvDomInfo = &newDomainInfo
91         newFunctionInfo := "New function info"
92         (*updatedProvider.ApiProvFuncs)[0].ApiProvFuncInfo = &newFunctionInfo
93         newFuncInfoAEF := "new func as AEF"
94         testFuncs := *updatedProvider.ApiProvFuncs
95         testFuncs = append(testFuncs, provapi.APIProviderFunctionDetails{
96                 ApiProvFuncInfo: &newFuncInfoAEF,
97                 ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
98         })
99         updatedProvider.ApiProvFuncs = &testFuncs
100
101         result := testutil.NewRequest().Put("/registrations/"+domainID).WithJsonBody(updatedProvider).Go(t, requestHandler)
102
103         var resultProvider provapi.APIProviderEnrolmentDetails
104         assert.Equal(t, http.StatusOK, result.Code())
105         err := result.UnmarshalBodyToObject(&resultProvider)
106         assert.NoError(t, err, "error unmarshaling response")
107         assert.Equal(t, newDomainInfo, *resultProvider.ApiProvDomInfo)
108         assert.Equal(t, newFunctionInfo, *(*resultProvider.ApiProvFuncs)[0].ApiProvFuncInfo)
109         assert.Equal(t, *(*resultProvider.ApiProvFuncs)[3].ApiProvFuncId, "AEF_id_new_func_as_AEF")
110         assert.Empty(t, resultProvider.FailReason)
111         assert.True(t, managerUnderTest.IsFunctionRegistered("AEF_id_new_func_as_AEF"))
112 }
113
114 func TestUpdateMissingFunction(t *testing.T) {
115         managerUnderTest, requestHandler := getEcho()
116
117         provider := getProvider()
118         provider.ApiProvDomId = &domainID
119         otherId := "otherId"
120         (*provider.ApiProvFuncs)[0].ApiProvFuncId = &otherId
121         (*provider.ApiProvFuncs)[1].ApiProvFuncId = &funcIdAMF
122         (*provider.ApiProvFuncs)[2].ApiProvFuncId = &funcIdAEF
123         managerUnderTest.onboardedProviders[domainID] = provider
124
125         // Modify the provider
126         updatedProvider := getProvider()
127         updatedProvider.ApiProvDomId = &domainID
128         (*updatedProvider.ApiProvFuncs)[0].ApiProvFuncId = &funcIdAPF
129         newFunctionInfo := "New function info"
130         (*updatedProvider.ApiProvFuncs)[0].ApiProvFuncInfo = &newFunctionInfo
131
132         result := testutil.NewRequest().Put("/registrations/"+domainID).WithJsonBody(updatedProvider).Go(t, requestHandler)
133
134         var errorObj common29122.ProblemDetails
135         assert.Equal(t, http.StatusBadRequest, result.Code())
136         err := result.UnmarshalBodyToObject(&errorObj)
137         assert.NoError(t, err, "error unmarshaling response")
138         assert.Contains(t, *errorObj.Cause, funcIdAPF)
139         assert.Contains(t, *errorObj.Cause, "not registered")
140 }
141
142 func TestDeleteProvider(t *testing.T) {
143         managerUnderTest, requestHandler := getEcho()
144
145         provider := getProvider()
146         provider.ApiProvDomId = &domainID
147         (*provider.ApiProvFuncs)[0].ApiProvFuncId = &funcIdAPF
148         managerUnderTest.onboardedProviders[domainID] = provider
149         assert.True(t, managerUnderTest.IsFunctionRegistered(funcIdAPF))
150
151         result := testutil.NewRequest().Delete("/registrations/"+domainID).Go(t, requestHandler)
152
153         assert.Equal(t, http.StatusNoContent, result.Code())
154         assert.False(t, managerUnderTest.IsFunctionRegistered(funcIdAPF))
155 }
156 func TestProviderHandlingValidation(t *testing.T) {
157         _, requestHandler := getEcho()
158
159         newProvider := provapi.APIProviderEnrolmentDetails{}
160
161         // Register a valid provider
162         result := testutil.NewRequest().Post("/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
163
164         assert.Equal(t, http.StatusBadRequest, result.Code())
165         var problemDetails common29122.ProblemDetails
166         err := result.UnmarshalBodyToObject(&problemDetails)
167         assert.NoError(t, err, "error unmarshaling response")
168         badRequest := http.StatusBadRequest
169         assert.Equal(t, &badRequest, problemDetails.Status)
170         errMsg := "Provider missing required ApiProvDomInfo"
171         assert.Equal(t, &errMsg, problemDetails.Cause)
172 }
173
174 func TestGetExposedFunctionsForPublishingFunction(t *testing.T) {
175         managerUnderTest := NewProviderManager()
176
177         provider := getProvider()
178         provider.ApiProvDomId = &domainID
179         (*provider.ApiProvFuncs)[0].ApiProvFuncId = &funcIdAPF
180         (*provider.ApiProvFuncs)[1].ApiProvFuncId = &funcIdAMF
181         (*provider.ApiProvFuncs)[2].ApiProvFuncId = &funcIdAEF
182         managerUnderTest.onboardedProviders[domainID] = provider
183         managerUnderTest.onboardedProviders[otherDomainID] = getOtherProvider()
184
185         exposedFuncs := managerUnderTest.GetAefsForPublisher(funcIdAPF)
186         assert.Equal(t, 1, len(exposedFuncs))
187         assert.Equal(t, funcIdAEF, exposedFuncs[0])
188 }
189
190 func getProvider() provapi.APIProviderEnrolmentDetails {
191         testFuncs := []provapi.APIProviderFunctionDetails{
192                 {
193                         ApiProvFuncInfo: &funcInfoAPF,
194                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
195                 },
196                 {
197                         ApiProvFuncInfo: &funcInfoAMF,
198                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
199                 },
200                 {
201                         ApiProvFuncInfo: &funcInfoAEF,
202                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
203                 },
204         }
205         return provapi.APIProviderEnrolmentDetails{
206                 ApiProvDomInfo: &domainInfo,
207                 ApiProvFuncs:   &testFuncs,
208         }
209
210 }
211
212 func getOtherProvider() provapi.APIProviderEnrolmentDetails {
213         otherDomainInfo := "other domain"
214         otherFuncInfoAPF := "other as APF"
215         otherApfId := "APF_id_other_as_APF"
216         otherFuncInfoAMF := "other as AMF"
217         otherAmfId := "AMF_id_other_as_AMF"
218         otherFuncInfoAEF := "other as AEF"
219         otherAefId := "AEF_id_other_as_AEF"
220         testFuncs := []provapi.APIProviderFunctionDetails{
221                 {
222                         ApiProvFuncId:   &otherApfId,
223                         ApiProvFuncInfo: &otherFuncInfoAPF,
224                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
225                 },
226                 {
227                         ApiProvFuncId:   &otherAmfId,
228                         ApiProvFuncInfo: &otherFuncInfoAMF,
229                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
230                 },
231                 {
232                         ApiProvFuncId:   &otherAefId,
233                         ApiProvFuncInfo: &otherFuncInfoAEF,
234                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
235                 },
236         }
237         return provapi.APIProviderEnrolmentDetails{
238                 ApiProvDomId:   &otherDomainID,
239                 ApiProvDomInfo: &otherDomainInfo,
240                 ApiProvFuncs:   &testFuncs,
241         }
242
243 }
244
245 func getEcho() (*ProviderManager, *echo.Echo) {
246         swagger, err := provapi.GetSwagger()
247         if err != nil {
248                 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
249                 os.Exit(1)
250         }
251
252         swagger.Servers = nil
253
254         pm := NewProviderManager()
255
256         e := echo.New()
257         e.Use(echomiddleware.Logger())
258         e.Use(middleware.OapiRequestValidator(swagger))
259
260         provapi.RegisterHandlers(e, pm)
261         return pm, e
262 }