2 // ========================LICENSE_START=================================
5 // Copyright (C) 2024: OpenInfra Foundation Europe
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
30 echo "github.com/labstack/echo/v4"
32 "oransc.org/nonrtric/servicemanager/internal/common29122"
33 "oransc.org/nonrtric/servicemanager/internal/envreader"
34 "oransc.org/nonrtric/servicemanager/internal/kongclear"
35 provapi "oransc.org/nonrtric/servicemanager/internal/providermanagementapi"
36 "oransc.org/nonrtric/servicemanager/internal/publishservice"
37 publishapi "oransc.org/nonrtric/servicemanager/internal/publishserviceapi"
39 "github.com/deepmap/oapi-codegen/pkg/middleware"
40 "github.com/deepmap/oapi-codegen/pkg/testutil"
41 echomiddleware "github.com/labstack/echo/v4/middleware"
42 log "github.com/sirupsen/logrus"
43 "github.com/stretchr/testify/assert"
47 domainID = "domain_id_Kong"
49 funcInfoAPF = "rApp Kong as APF"
50 funcIdAPF = "APF_id_rApp_Kong_as_APF"
51 funcInfoAEF = "rApp Kong as AEF"
52 funcIdAEF = "AEF_id_rApp_Kong_as_AEF"
55 var requestHandler *echo.Echo
57 func TestMain(m *testing.M) {
70 func setupTest() error {
71 myEnv, myPorts, err := envreader.ReadDotEnv()
73 log.Fatal("error loading environment file on setupTest")
77 requestHandler, err = getEcho(myEnv, myPorts)
79 log.Fatal("getEcho fatal error on setupTest")
85 func teardown() error {
86 log.Trace("entering teardown")
88 t := new(testing.T) // Create a new testing.T instance for teardown
91 invokerInfo := "invoker a"
92 invokerId := "api_invoker_id_" + strings.Replace(invokerInfo, " ", "_", 1)
94 result := testutil.NewRequest().Delete("/api-invoker-management/v1/onboardedInvokers/"+invokerId).Go(t, requestHandler)
95 assert.Equal(t, http.StatusNoContent, result.Code())
97 // Delete the original published service
98 apfId := "APF_id_rApp_Kong_as_APF"
100 apiId := "api_id_" + apiName
102 result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler)
103 assert.Equal(t, http.StatusNoContent, result.Code())
105 // Delete the first published service
106 apfId = "APF_id_rApp_Kong_as_APF"
108 apiId = "api_id_" + apiName
110 result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler)
111 assert.Equal(t, http.StatusNoContent, result.Code())
113 // Delete the second published service
115 apiId = "api_id_" + apiName
117 result = testutil.NewRequest().Delete("/published-apis/v1/"+apfId+"/service-apis/"+apiId).Go(t, requestHandler)
118 assert.Equal(t, http.StatusNoContent, result.Code())
120 // Delete the provider
121 result = testutil.NewRequest().Delete("/api-provider-management/v1/registrations/"+domainID).Go(t, requestHandler)
122 assert.Equal(t, http.StatusNoContent, result.Code())
124 myEnv, myPorts, err := envreader.ReadDotEnv()
126 log.Fatal("error loading environment file")
130 err = kongclear.KongClear(myEnv, myPorts)
132 log.Fatal("error clearing Kong on teardown")
137 func TestRegisterValidProvider(t *testing.T) {
140 // Register a valid provider
141 newProvider := getProvider()
142 result := testutil.NewRequest().Post("/api-provider-management/v1/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
143 assert.Equal(t, http.StatusCreated, result.Code())
145 var resultProvider provapi.APIProviderEnrolmentDetails
146 err := result.UnmarshalBodyToObject(&resultProvider)
147 assert.NoError(t, err, "error unmarshaling response")
149 assert.NotNil(t, resultProvider.ApiProvDomId, "error reading resultProvider")
151 if resultProvider.ApiProvDomId != nil {
152 assert.Equal(t, *resultProvider.ApiProvDomId, domainID)
154 apiProvFuncAPF := (*resultProvider.ApiProvFuncs)[0]
155 apiProvFuncIdAPF := *apiProvFuncAPF.ApiProvFuncId
156 assert.Equal(t, apiProvFuncIdAPF, funcIdAPF)
158 // We don't handle AMF
159 apiProvFuncAEF := (*resultProvider.ApiProvFuncs)[1]
160 apiProvFuncIdAEF := *apiProvFuncAEF.ApiProvFuncId
161 assert.Equal(t, apiProvFuncIdAEF, funcIdAEF)
163 assert.Empty(t, resultProvider.FailReason)
164 assert.Equal(t, "http://example.com/api-provider-management/v1/registrations/"+*resultProvider.ApiProvDomId, result.Recorder.Header().Get(echo.HeaderLocation))
166 // Register same provider again should result in Forbidden
167 result = testutil.NewRequest().Post("/api-provider-management/v1/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
169 var errorObj common29122.ProblemDetails
170 assert.Equal(t, http.StatusForbidden, result.Code())
172 err = result.UnmarshalBodyToObject(&errorObj)
173 assert.NoError(t, err, "error unmarshaling response")
174 assert.Equal(t, http.StatusForbidden, *errorObj.Status)
175 assert.Contains(t, *errorObj.Cause, "already registered")
179 func TestUpdateValidProviderWithNewFunction(t *testing.T) {
180 // Modify the provider
181 updatedProvider := getProvider()
182 updatedProvider.ApiProvDomId = &domainID
184 newDomainInfo := "New domain info"
185 updatedProvider.ApiProvDomInfo = &newDomainInfo
186 newFunctionInfo := "New function info"
187 (*updatedProvider.ApiProvFuncs)[0].ApiProvFuncInfo = &newFunctionInfo
188 newFuncInfoAEF := "new func as AEF"
190 testFuncs := append(*updatedProvider.ApiProvFuncs, provapi.APIProviderFunctionDetails{
191 ApiProvFuncInfo: &newFuncInfoAEF,
192 ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
193 RegInfo: provapi.RegistrationInformation{
194 ApiProvPubKey: "key",
197 updatedProvider.ApiProvFuncs = &testFuncs
199 result := testutil.NewRequest().Put("/api-provider-management/v1/registrations/"+domainID).WithJsonBody(updatedProvider).Go(t, requestHandler)
201 var resultProvider provapi.APIProviderEnrolmentDetails
202 assert.Equal(t, http.StatusOK, result.Code())
204 err := result.UnmarshalBodyToObject(&resultProvider)
205 assert.NoError(t, err, "error unmarshaling response")
207 assert.Equal(t, newDomainInfo, *resultProvider.ApiProvDomInfo)
208 assert.Equal(t, newFunctionInfo, *(*resultProvider.ApiProvFuncs)[0].ApiProvFuncInfo)
209 assert.Equal(t, *(*resultProvider.ApiProvFuncs)[2].ApiProvFuncId, "AEF_id_new_func_as_AEF")
210 assert.Empty(t, resultProvider.FailReason)
213 func TestDeleteProvider(t *testing.T) {
214 provider := getProvider()
215 provider.ApiProvDomId = &domainID
216 (*provider.ApiProvFuncs)[0].ApiProvFuncId = &funcIdAPF
217 result := testutil.NewRequest().Delete("/api-provider-management/v1/registrations/"+domainID).Go(t, requestHandler)
218 assert.Equal(t, http.StatusNoContent, result.Code())
222 func getProvider() provapi.APIProviderEnrolmentDetails {
223 testFuncs := []provapi.APIProviderFunctionDetails{
225 ApiProvFuncInfo: &funcInfoAPF,
226 ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
227 RegInfo: provapi.RegistrationInformation{
228 ApiProvPubKey: "APF-PublicKey",
232 ApiProvFuncInfo: &funcInfoAEF,
233 ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
234 RegInfo: provapi.RegistrationInformation{
235 ApiProvPubKey: "AEF-PublicKey",
239 return provapi.APIProviderEnrolmentDetails{
241 ApiProvDomInfo: &domainInfo,
242 ApiProvFuncs: &testFuncs,
246 func getEcho(myEnv map[string]string, myPorts map[string]int) (*echo.Echo, error) {
247 capifProtocol := myEnv["CAPIF_PROTOCOL"]
248 capifIPv4 := common29122.Ipv4Addr(myEnv["CAPIF_IPV4"])
249 capifPort := common29122.Port(myPorts["CAPIF_PORT"])
250 kongDomain := myEnv["KONG_DOMAIN"]
251 kongProtocol := myEnv["KONG_PROTOCOL"]
252 kongIPv4 := common29122.Ipv4Addr(myEnv["KONG_IPV4"])
253 kongDataPlanePort := common29122.Port(myPorts["KONG_DATA_PLANE_PORT"])
254 kongControlPlanePort := common29122.Port(myPorts["KONG_CONTROL_PLANE_PORT"])
258 // Register ProviderManagement
259 providerManagerSwagger, err := provapi.GetSwagger()
261 log.Fatalf("Error loading ProviderManagement swagger spec\n: %s", err)
264 providerManagerSwagger.Servers = nil
265 providerManager := NewProviderManager(capifProtocol, capifIPv4, capifPort)
267 var group *echo.Group
269 group = e.Group("/api-provider-management/v1")
270 group.Use(middleware.OapiRequestValidator(providerManagerSwagger))
271 provapi.RegisterHandlersWithBaseURL(e, providerManager, "/api-provider-management/v1")
273 publishServiceSwagger, err := publishapi.GetSwagger()
275 fmt.Fprintf(os.Stderr, "Error loading PublishService swagger spec\n: %s", err)
279 publishServiceSwagger.Servers = nil
281 ps := publishservice.NewPublishService(kongDomain, kongProtocol, kongIPv4, kongDataPlanePort, kongControlPlanePort, capifProtocol, capifIPv4, capifPort)
283 group = e.Group("/published-apis/v1")
284 group.Use(echomiddleware.Logger())
285 group.Use(middleware.OapiRequestValidator(publishServiceSwagger))
286 publishapi.RegisterHandlersWithBaseURL(e, ps, "/published-apis/v1")
288 // return ps, eventChannel, e