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 providermanagement
29 "github.com/labstack/echo/v4"
31 "oransc.org/nonrtric/capifcore/internal/common29122"
32 provapi "oransc.org/nonrtric/capifcore/internal/providermanagementapi"
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"
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"
52 func TestRegisterValidProvider(t *testing.T) {
53 managerUnderTest, requestHandler := getEcho()
55 newProvider := getProvider()
57 // Register a valid provider
58 result := testutil.NewRequest().Post("/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
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"))
73 func TestUpdateValidProvider(t *testing.T) {
74 managerUnderTest, requestHandler := getEcho()
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
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,
99 updatedProvider.ApiProvFuncs = &testFuncs
101 result := testutil.NewRequest().Put("/registrations/"+domainID).WithJsonBody(updatedProvider).Go(t, requestHandler)
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"))
114 func TestUpdateMissingFunction(t *testing.T) {
115 managerUnderTest, requestHandler := getEcho()
117 provider := getProvider()
118 provider.ApiProvDomId = &domainID
120 (*provider.ApiProvFuncs)[0].ApiProvFuncId = &otherId
121 (*provider.ApiProvFuncs)[1].ApiProvFuncId = &funcIdAMF
122 (*provider.ApiProvFuncs)[2].ApiProvFuncId = &funcIdAEF
123 managerUnderTest.onboardedProviders[domainID] = provider
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
132 result := testutil.NewRequest().Put("/registrations/"+domainID).WithJsonBody(updatedProvider).Go(t, requestHandler)
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")
142 func TestDeleteProvider(t *testing.T) {
143 managerUnderTest, requestHandler := getEcho()
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))
151 result := testutil.NewRequest().Delete("/registrations/"+domainID).Go(t, requestHandler)
153 assert.Equal(t, http.StatusNoContent, result.Code())
154 assert.False(t, managerUnderTest.IsFunctionRegistered(funcIdAPF))
156 func TestProviderHandlingValidation(t *testing.T) {
157 _, requestHandler := getEcho()
159 newProvider := provapi.APIProviderEnrolmentDetails{}
161 // Register a valid provider
162 result := testutil.NewRequest().Post("/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
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)
174 func TestGetExposedFunctionsForPublishingFunction(t *testing.T) {
175 managerUnderTest := NewProviderManager()
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()
185 exposedFuncs := managerUnderTest.GetAefsForPublisher(funcIdAPF)
186 assert.Equal(t, 1, len(exposedFuncs))
187 assert.Equal(t, funcIdAEF, exposedFuncs[0])
190 func getProvider() provapi.APIProviderEnrolmentDetails {
191 testFuncs := []provapi.APIProviderFunctionDetails{
193 ApiProvFuncInfo: &funcInfoAPF,
194 ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
197 ApiProvFuncInfo: &funcInfoAMF,
198 ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
201 ApiProvFuncInfo: &funcInfoAEF,
202 ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
205 return provapi.APIProviderEnrolmentDetails{
206 ApiProvDomInfo: &domainInfo,
207 ApiProvFuncs: &testFuncs,
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{
222 ApiProvFuncId: &otherApfId,
223 ApiProvFuncInfo: &otherFuncInfoAPF,
224 ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
227 ApiProvFuncId: &otherAmfId,
228 ApiProvFuncInfo: &otherFuncInfoAMF,
229 ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
232 ApiProvFuncId: &otherAefId,
233 ApiProvFuncInfo: &otherFuncInfoAEF,
234 ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
237 return provapi.APIProviderEnrolmentDetails{
238 ApiProvDomId: &otherDomainID,
239 ApiProvDomInfo: &otherDomainInfo,
240 ApiProvFuncs: &testFuncs,
245 func getEcho() (*ProviderManager, *echo.Echo) {
246 swagger, err := provapi.GetSwagger()
248 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
252 swagger.Servers = nil
254 pm := NewProviderManager()
257 e.Use(echomiddleware.Logger())
258 e.Use(middleware.OapiRequestValidator(swagger))
260 provapi.RegisterHandlers(e, pm)