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"
40 func TestProviderHandlingSuccessfully(t *testing.T) {
41 managerUnderTest, requestHandler := getEcho()
43 domainInfo := "rApp domain"
44 funcInfoAPF := "rApp as APF"
45 funcInfoAMF := "rApp as AMF"
46 funcInfoAEF := "rApp as AEF"
47 testFuncs := []provapi.APIProviderFunctionDetails{
49 ApiProvFuncInfo: &funcInfoAPF,
50 ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
53 ApiProvFuncInfo: &funcInfoAMF,
54 ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
57 ApiProvFuncInfo: &funcInfoAEF,
58 ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
61 newProvider := provapi.APIProviderEnrolmentDetails{
62 ApiProvDomInfo: &domainInfo,
63 ApiProvFuncs: &testFuncs,
66 // Register a valid provider
67 result := testutil.NewRequest().Post("/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
69 domainID := "domain_id_rApp_domain"
70 funcIdAPF := "APF_id_rApp_as_APF"
71 funcIdAMF := "AMF_id_rApp_as_AMF"
72 funcIdAEF := "AEF_id_rApp_as_AEF"
73 assert.Equal(t, http.StatusCreated, result.Code())
74 var resultProvider provapi.APIProviderEnrolmentDetails
75 err := result.UnmarshalBodyToObject(&resultProvider)
76 assert.NoError(t, err, "error unmarshaling response")
77 assert.Equal(t, *resultProvider.ApiProvDomId, domainID)
78 assert.Equal(t, *(*resultProvider.ApiProvFuncs)[0].ApiProvFuncId, funcIdAPF)
79 assert.Equal(t, *(*resultProvider.ApiProvFuncs)[1].ApiProvFuncId, funcIdAMF)
80 assert.Equal(t, *(*resultProvider.ApiProvFuncs)[2].ApiProvFuncId, funcIdAEF)
81 assert.Empty(t, resultProvider.FailReason)
82 assert.Equal(t, "http://example.com/registrations/"+*resultProvider.ApiProvDomId, result.Recorder.Header().Get(echo.HeaderLocation))
83 assert.True(t, managerUnderTest.IsFunctionRegistered("APF_id_rApp_as_APF"))
85 // Update the provider
86 newProvider.ApiProvDomId = &domainID
87 (*newProvider.ApiProvFuncs)[0].ApiProvFuncId = &funcIdAPF
88 (*newProvider.ApiProvFuncs)[1].ApiProvFuncId = &funcIdAMF
89 (*newProvider.ApiProvFuncs)[2].ApiProvFuncId = &funcIdAEF
90 newFuncInfoAEF := "new func as AEF"
91 testFuncs = append(testFuncs, provapi.APIProviderFunctionDetails{
92 ApiProvFuncInfo: &newFuncInfoAEF,
93 ApiProvFuncRole: "AEF",
96 result = testutil.NewRequest().Put("/registrations/"+domainID).WithJsonBody(newProvider).Go(t, requestHandler)
98 assert.Equal(t, http.StatusOK, result.Code())
99 err = result.UnmarshalBodyToObject(&resultProvider)
100 assert.NoError(t, err, "error unmarshaling response")
101 assert.Equal(t, *(*resultProvider.ApiProvFuncs)[3].ApiProvFuncId, "AEF_id_new_func_as_AEF")
102 assert.Empty(t, resultProvider.FailReason)
103 assert.True(t, managerUnderTest.IsFunctionRegistered("AEF_id_new_func_as_AEF"))
105 // Delete the provider
106 result = testutil.NewRequest().Delete("/registrations/"+*resultProvider.ApiProvDomId).Go(t, requestHandler)
108 assert.Equal(t, http.StatusNoContent, result.Code())
109 assert.False(t, managerUnderTest.IsFunctionRegistered("APF_id_rApp_as_APF"))
112 func TestProviderHandlingValidation(t *testing.T) {
113 _, requestHandler := getEcho()
115 newProvider := provapi.APIProviderEnrolmentDetails{}
117 // Register a valid provider
118 result := testutil.NewRequest().Post("/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
120 assert.Equal(t, http.StatusBadRequest, result.Code())
121 var problemDetails common29122.ProblemDetails
122 err := result.UnmarshalBodyToObject(&problemDetails)
123 assert.NoError(t, err, "error unmarshaling response")
125 assert.Equal(t, &badRequest, problemDetails.Status)
126 errMsg := "Provider missing required ApiProvDomInfo"
127 assert.Equal(t, &errMsg, problemDetails.Cause)
130 func getEcho() (*ProviderManager, *echo.Echo) {
131 swagger, err := provapi.GetSwagger()
133 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
137 swagger.Servers = nil
139 pm := NewProviderManager()
142 e.Use(echomiddleware.Logger())
143 e.Use(middleware.OapiRequestValidator(swagger))
145 provapi.RegisterHandlers(e, pm)