fb5ad81a0e5707e00c895d97e8655af415dad377
[nonrtric/plt/sme.git] / capifcore / internal / providermanagementapi / typevalidation_test.go
1 // -
2 //   ========================LICENSE_START=================================
3 //   O-RAN-SC
4 //   %%
5 //   Copyright (C) 2023: 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 providermanagementapi
22
23 import (
24         "testing"
25
26         "github.com/stretchr/testify/assert"
27 )
28
29 var (
30         domainID      = "domain_id_rApp_domain"
31         otherDomainID = "domain_id_other_domain"
32         domainInfo    = "rApp domain"
33         funcInfoAPF   = "rApp as APF"
34         funcIdAPF     = "APF_id_rApp_as_APF"
35         funcInfoAMF   = "rApp as AMF"
36         funcIdAMF     = "AMF_id_rApp_as_AMF"
37         funcInfoAEF   = "rApp as AEF"
38         funcIdAEF     = "AEF_id_rApp_as_AEF"
39 )
40
41 func TestValidateRegistrationInformation(t *testing.T) {
42         regInfoUnderTest := RegistrationInformation{}
43         err := regInfoUnderTest.Validate()
44         if assert.Error(t, err) {
45                 assert.Contains(t, err.Error(), "missing")
46                 assert.Contains(t, err.Error(), "apiProvPubKey")
47         }
48
49         regInfoUnderTest.ApiProvPubKey = "key"
50         err = regInfoUnderTest.Validate()
51         assert.Nil(t, err)
52 }
53
54 func TestValidateAPIProviderFunctionDetails(t *testing.T) {
55         funcDetailsUnderTest := APIProviderFunctionDetails{}
56         err := funcDetailsUnderTest.Validate()
57         if assert.Error(t, err) {
58                 assert.Contains(t, err.Error(), "missing")
59                 assert.Contains(t, err.Error(), "apiProvFuncRole")
60         }
61
62         funcDetailsUnderTest.ApiProvFuncRole = ApiProviderFuncRoleAEF
63         err = funcDetailsUnderTest.Validate()
64         if assert.Error(t, err) {
65                 assert.Contains(t, err.Error(), "missing")
66                 assert.Contains(t, err.Error(), "apiProvPubKey")
67         }
68
69         funcDetailsUnderTest.RegInfo = RegistrationInformation{
70                 ApiProvPubKey: "key",
71         }
72         assert.Nil(t, funcDetailsUnderTest.Validate())
73 }
74
75 func TestValidateAPIProviderEnrolmentDetails(t *testing.T) {
76         providerDetailsUnderTest := APIProviderEnrolmentDetails{}
77         err := providerDetailsUnderTest.Validate()
78         if assert.Error(t, err) {
79                 assert.Contains(t, err.Error(), "missing")
80                 assert.Contains(t, err.Error(), "regSec")
81         }
82
83         providerDetailsUnderTest.RegSec = "sec"
84         funcs := []APIProviderFunctionDetails{{}}
85         providerDetailsUnderTest.ApiProvFuncs = &funcs
86         err = providerDetailsUnderTest.Validate()
87         if assert.Error(t, err) {
88                 assert.Contains(t, err.Error(), "apiProvFuncs")
89                 assert.Contains(t, err.Error(), "contains invalid")
90         }
91 }
92
93 func TestUpdateFuncs_addNewFunction(t *testing.T) {
94         providerUnderTest := getProvider()
95
96         newFuncInfoAEF := "new func as AEF"
97         newFuncs := append(*providerUnderTest.ApiProvFuncs, APIProviderFunctionDetails{
98                 ApiProvFuncInfo: &newFuncInfoAEF,
99                 ApiProvFuncRole: ApiProviderFuncRoleAEF,
100         })
101         providerUnderTest.ApiProvFuncs = &newFuncs
102
103         err := providerUnderTest.UpdateFuncs(getProvider())
104
105         assert.Nil(t, err)
106         assert.Len(t, *providerUnderTest.ApiProvFuncs, 4)
107         assert.True(t, providerUnderTest.IsFunctionRegistered("AEF_id_new_func_as_AEF"))
108 }
109
110 func TestUpdateFuncs_deleteFunction(t *testing.T) {
111         providerUnderTest := getProvider()
112
113         modFuncs := []APIProviderFunctionDetails{(*providerUnderTest.ApiProvFuncs)[0], (*providerUnderTest.ApiProvFuncs)[1]}
114         providerUnderTest.ApiProvFuncs = &modFuncs
115
116         err := providerUnderTest.UpdateFuncs(getProvider())
117
118         assert.Nil(t, err)
119         assert.Len(t, *providerUnderTest.ApiProvFuncs, 2)
120         assert.True(t, providerUnderTest.IsFunctionRegistered(funcIdAPF))
121         assert.True(t, providerUnderTest.IsFunctionRegistered(funcIdAMF))
122 }
123
124 func TestUpdateFuncs_unregisteredFunction(t *testing.T) {
125         providerUnderTest := getProvider()
126
127         unRegId := "unRegId"
128         modFuncs := []APIProviderFunctionDetails{
129                 {
130                         ApiProvFuncId: &unRegId,
131                 },
132         }
133         providerUnderTest.ApiProvFuncs = &modFuncs
134
135         err := providerUnderTest.UpdateFuncs(getProvider())
136         if assert.Error(t, err) {
137                 assert.Contains(t, err.Error(), unRegId)
138                 assert.Contains(t, err.Error(), "not registered")
139         }
140 }
141
142 func getProvider() APIProviderEnrolmentDetails {
143         testFuncs := []APIProviderFunctionDetails{
144                 {
145                         ApiProvFuncId:   &funcIdAPF,
146                         ApiProvFuncInfo: &funcInfoAPF,
147                         ApiProvFuncRole: ApiProviderFuncRoleAPF,
148                 },
149                 {
150                         ApiProvFuncId:   &funcIdAMF,
151                         ApiProvFuncInfo: &funcInfoAMF,
152                         ApiProvFuncRole: ApiProviderFuncRoleAMF,
153                 },
154                 {
155                         ApiProvFuncId:   &funcIdAEF,
156                         ApiProvFuncInfo: &funcInfoAEF,
157                         ApiProvFuncRole: ApiProviderFuncRoleAEF,
158                 },
159         }
160         return APIProviderEnrolmentDetails{
161                 ApiProvDomId:   &domainID,
162                 ApiProvDomInfo: &domainInfo,
163                 ApiProvFuncs:   &testFuncs,
164         }
165
166 }