e88eeb91129aff643637dde1f31cf0a77885d182
[nonrtric/plt/sme.git] / 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/sme/internal/common29122"
32         provapi "oransc.org/nonrtric/sme/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 func TestProviderHandlingSuccessfully(t *testing.T) {
41         managerUnderTest, requestHandler := getEcho()
42
43         domainInfo := "rApp domain"
44         funcInfoAPF := "rApp as APF"
45         funcInfoAMF := "rApp as AMF"
46         funcInfoAEF := "rApp as AEF"
47         testFuncs := []provapi.APIProviderFunctionDetails{
48                 {
49                         ApiProvFuncInfo: &funcInfoAPF,
50                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAPF,
51                 },
52                 {
53                         ApiProvFuncInfo: &funcInfoAMF,
54                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAMF,
55                 },
56                 {
57                         ApiProvFuncInfo: &funcInfoAEF,
58                         ApiProvFuncRole: provapi.ApiProviderFuncRoleAEF,
59                 },
60         }
61         newProvider := provapi.APIProviderEnrolmentDetails{
62                 ApiProvDomInfo: &domainInfo,
63                 ApiProvFuncs:   &testFuncs,
64         }
65
66         // Register a valid provider
67         result := testutil.NewRequest().Post("/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
68
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"))
84
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",
94         })
95
96         result = testutil.NewRequest().Put("/registrations/"+domainID).WithJsonBody(newProvider).Go(t, requestHandler)
97
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"))
104
105         // Delete the provider
106         result = testutil.NewRequest().Delete("/registrations/"+*resultProvider.ApiProvDomId).Go(t, requestHandler)
107
108         assert.Equal(t, http.StatusNoContent, result.Code())
109         assert.False(t, managerUnderTest.IsFunctionRegistered("APF_id_rApp_as_APF"))
110 }
111
112 func TestProviderHandlingValidation(t *testing.T) {
113         _, requestHandler := getEcho()
114
115         newProvider := provapi.APIProviderEnrolmentDetails{}
116
117         // Register a valid provider
118         result := testutil.NewRequest().Post("/registrations").WithJsonBody(newProvider).Go(t, requestHandler)
119
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")
124         badRequest := 400
125         assert.Equal(t, &badRequest, problemDetails.Status)
126         errMsg := "Provider missing required ApiProvDomInfo"
127         assert.Equal(t, &errMsg, problemDetails.Cause)
128 }
129
130 func getEcho() (*ProviderManager, *echo.Echo) {
131         swagger, err := provapi.GetSwagger()
132         if err != nil {
133                 fmt.Fprintf(os.Stderr, "Error loading swagger spec\n: %s", err)
134                 os.Exit(1)
135         }
136
137         swagger.Servers = nil
138
139         pm := NewProviderManager()
140
141         e := echo.New()
142         e.Use(echomiddleware.Logger())
143         e.Use(middleware.OapiRequestValidator(swagger))
144
145         provapi.RegisterHandlers(e, pm)
146         return pm, e
147 }