8d8c9b4122304d64ccc29202f913e8fc1e427741
[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 TestProviderHandlingSuccessfully(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         // Update the provider
73         newProvider.ApiProvDomId = &domainID
74         (*newProvider.ApiProvFuncs)[0].ApiProvFuncId = &funcIdAPF
75         (*newProvider.ApiProvFuncs)[1].ApiProvFuncId = &funcIdAMF
76         (*newProvider.ApiProvFuncs)[2].ApiProvFuncId = &funcIdAEF
77         newFuncInfoAEF := "new func as AEF"
78         testFuncs := *newProvider.ApiProvFuncs
79         testFuncs = append(testFuncs, provapi.APIProviderFunctionDetails{
80                 ApiProvFuncInfo: &newFuncInfoAEF,
81                 ApiProvFuncRole: "AEF",
82         })
83         newProvider.ApiProvFuncs = &testFuncs
84
85         result = testutil.NewRequest().Put("/registrations/"+domainID).WithJsonBody(newProvider).Go(t, requestHandler)
86
87         assert.Equal(t, http.StatusOK, result.Code())
88         err = result.UnmarshalBodyToObject(&resultProvider)
89         assert.NoError(t, err, "error unmarshaling response")
90         assert.Equal(t, *(*resultProvider.ApiProvFuncs)[3].ApiProvFuncId, "AEF_id_new_func_as_AEF")
91         assert.Empty(t, resultProvider.FailReason)
92         assert.True(t, managerUnderTest.IsFunctionRegistered("AEF_id_new_func_as_AEF"))
93
94         // Delete the provider
95         result = testutil.NewRequest().Delete("/registrations/"+*resultProvider.ApiProvDomId).Go(t, requestHandler)
96
97         assert.Equal(t, http.StatusNoContent, result.Code())
98         assert.False(t, managerUnderTest.IsFunctionRegistered("APF_id_rApp_as_APF"))
99 }
100
101 func TestProviderHandlingValidation(t *testing.T) {
102         _, requestHandler := getEcho()
103
104         newProvider := provapi.APIProviderEnrolmentDetails{}
105
106         // Register a valid provider
107         result := testutil.NewRequest().Post("/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
108
109         assert.Equal(t, http.StatusBadRequest, result.Code())
110         var problemDetails common29122.ProblemDetails
111         err := result.UnmarshalBodyToObject(&problemDetails)
112         assert.NoError(t, err, "error unmarshaling response")
113         badRequest := http.StatusBadRequest
114         assert.Equal(t, &badRequest, problemDetails.Status)
115         errMsg := "Provider missing required ApiProvDomInfo"
116         assert.Equal(t, &errMsg, problemDetails.Cause)
117 }
118
119 func TestGetExposedFunctionsForPublishingFunction(t *testing.T) {
120         managerUnderTest := NewProviderManager()
121
122         managerUnderTest.onboardedProviders[domainID] = getProvider()
123         managerUnderTest.onboardedProviders[otherDomainID] = getOtherProvider()
124
125         exposedFuncs := managerUnderTest.GetAefsForPublisher(funcIdAPF)
126         assert.Equal(t, 1, len(exposedFuncs))
127         assert.Equal(t, funcIdAEF, exposedFuncs[0])
128 }
129
130 func getProvider() provapi.APIProviderEnrolmentDetails {
131         testFuncs := []provapi.APIProviderFunctionDetails{
132                 {
133                         ApiProvFuncId:   &funcIdAPF,
134                         ApiProvFuncInfo: &funcInfoAPF,
135                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
136                 },
137                 {
138                         ApiProvFuncId:   &funcIdAMF,
139                         ApiProvFuncInfo: &funcInfoAMF,
140                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
141                 },
142                 {
143                         ApiProvFuncId:   &funcIdAEF,
144                         ApiProvFuncInfo: &funcInfoAEF,
145                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
146                 },
147         }
148         return provapi.APIProviderEnrolmentDetails{
149                 ApiProvDomId:   &domainID,
150                 ApiProvDomInfo: &domainInfo,
151                 ApiProvFuncs:   &testFuncs,
152         }
153
154 }
155
156 func getOtherProvider() provapi.APIProviderEnrolmentDetails {
157         otherDomainInfo := "other domain"
158         otherFuncInfoAPF := "other as APF"
159         otherApfId := "APF_id_other_as_APF"
160         otherFuncInfoAMF := "other as AMF"
161         otherAmfId := "AMF_id_other_as_AMF"
162         otherFuncInfoAEF := "other as AEF"
163         otherAefId := "AEF_id_other_as_AEF"
164         testFuncs := []provapi.APIProviderFunctionDetails{
165                 {
166                         ApiProvFuncId:   &otherApfId,
167                         ApiProvFuncInfo: &otherFuncInfoAPF,
168                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
169                 },
170                 {
171                         ApiProvFuncId:   &otherAmfId,
172                         ApiProvFuncInfo: &otherFuncInfoAMF,
173                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
174                 },
175                 {
176                         ApiProvFuncId:   &otherAefId,
177                         ApiProvFuncInfo: &otherFuncInfoAEF,
178                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
179                 },
180         }
181         return provapi.APIProviderEnrolmentDetails{
182                 ApiProvDomId:   &otherDomainID,
183                 ApiProvDomInfo: &otherDomainInfo,
184                 ApiProvFuncs:   &testFuncs,
185         }
186
187 }
188
189 func getEcho() (*ProviderManager, *echo.Echo) {
190         swagger, err := provapi.GetSwagger()
191         if err != nil {
192                 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
193                 os.Exit(1)
194         }
195
196         swagger.Servers = nil
197
198         pm := NewProviderManager()
199
200         e := echo.New()
201         e.Use(echomiddleware.Logger())
202         e.Use(middleware.OapiRequestValidator(swagger))
203
204         provapi.RegisterHandlers(e, pm)
205         return pm, e
206 }