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 TestProviderHandlingSuccessfully(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"))
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",
83 newProvider.ApiProvFuncs = &testFuncs
85 result = testutil.NewRequest().Put("/registrations/"+domainID).WithJsonBody(newProvider).Go(t, requestHandler)
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"))
94 // Delete the provider
95 result = testutil.NewRequest().Delete("/registrations/"+*resultProvider.ApiProvDomId).Go(t, requestHandler)
97 assert.Equal(t, http.StatusNoContent, result.Code())
98 assert.False(t, managerUnderTest.IsFunctionRegistered("APF_id_rApp_as_APF"))
101 func TestProviderHandlingValidation(t *testing.T) {
102 _, requestHandler := getEcho()
104 newProvider := provapi.APIProviderEnrolmentDetails{}
106 // Register a valid provider
107 result := testutil.NewRequest().Post("/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
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)
119 func TestGetExposedFunctionsForPublishingFunction(t *testing.T) {
120 managerUnderTest := NewProviderManager()
122 managerUnderTest.onboardedProviders[domainID] = getProvider()
123 managerUnderTest.onboardedProviders[otherDomainID] = getOtherProvider()
125 exposedFuncs := managerUnderTest.GetAefsForPublisher(funcIdAPF)
126 assert.Equal(t, 1, len(exposedFuncs))
127 assert.Equal(t, funcIdAEF, exposedFuncs[0])
130 func getProvider() provapi.APIProviderEnrolmentDetails {
131 testFuncs := []provapi.APIProviderFunctionDetails{
133 ApiProvFuncId: &funcIdAPF,
134 ApiProvFuncInfo: &funcInfoAPF,
135 ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
138 ApiProvFuncId: &funcIdAMF,
139 ApiProvFuncInfo: &funcInfoAMF,
140 ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
143 ApiProvFuncId: &funcIdAEF,
144 ApiProvFuncInfo: &funcInfoAEF,
145 ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
148 return provapi.APIProviderEnrolmentDetails{
149 ApiProvDomId: &domainID,
150 ApiProvDomInfo: &domainInfo,
151 ApiProvFuncs: &testFuncs,
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{
166 ApiProvFuncId: &otherApfId,
167 ApiProvFuncInfo: &otherFuncInfoAPF,
168 ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
171 ApiProvFuncId: &otherAmfId,
172 ApiProvFuncInfo: &otherFuncInfoAMF,
173 ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
176 ApiProvFuncId: &otherAefId,
177 ApiProvFuncInfo: &otherFuncInfoAEF,
178 ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
181 return provapi.APIProviderEnrolmentDetails{
182 ApiProvDomId: &otherDomainID,
183 ApiProvDomInfo: &otherDomainInfo,
184 ApiProvFuncs: &testFuncs,
189 func getEcho() (*ProviderManager, *echo.Echo) {
190 swagger, err := provapi.GetSwagger()
192 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
196 swagger.Servers = nil
198 pm := NewProviderManager()
201 e.Use(echomiddleware.Logger())
202 e.Use(middleware.OapiRequestValidator(swagger))
204 provapi.RegisterHandlers(e, pm)