Updates for G Maintenance release
[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         var invalidFuncRole ApiProviderFuncRole = "invalid"
63         funcDetailsUnderTest.ApiProvFuncRole = invalidFuncRole
64         err = funcDetailsUnderTest.Validate()
65         if assert.Error(t, err) {
66                 assert.Contains(t, err.Error(), "invalid")
67                 assert.Contains(t, err.Error(), "apiProvFuncRole")
68         }
69
70         funcDetailsUnderTest.ApiProvFuncRole = ApiProviderFuncRoleAEF
71         err = funcDetailsUnderTest.Validate()
72         if assert.Error(t, err) {
73                 assert.Contains(t, err.Error(), "missing")
74                 assert.Contains(t, err.Error(), "apiProvPubKey")
75         }
76
77         funcDetailsUnderTest.RegInfo = RegistrationInformation{
78                 ApiProvPubKey: "key",
79         }
80         assert.Nil(t, funcDetailsUnderTest.Validate())
81 }
82
83 func TestValidateAPIProviderEnrolmentDetails(t *testing.T) {
84         providerDetailsUnderTest := APIProviderEnrolmentDetails{}
85         err := providerDetailsUnderTest.Validate()
86         if assert.Error(t, err) {
87                 assert.Contains(t, err.Error(), "missing")
88                 assert.Contains(t, err.Error(), "regSec")
89         }
90
91         providerDetailsUnderTest.RegSec = "sec"
92         funcs := []APIProviderFunctionDetails{{}}
93         providerDetailsUnderTest.ApiProvFuncs = &funcs
94         err = providerDetailsUnderTest.Validate()
95         if assert.Error(t, err) {
96                 assert.Contains(t, err.Error(), "apiProvFuncs")
97                 assert.Contains(t, err.Error(), "contains invalid")
98         }
99
100         (*providerDetailsUnderTest.ApiProvFuncs)[0] = APIProviderFunctionDetails{
101                 ApiProvFuncRole: ApiProviderFuncRoleAEF,
102                 RegInfo: RegistrationInformation{
103                         ApiProvPubKey: "key",
104                 },
105         }
106         assert.Nil(t, providerDetailsUnderTest.Validate())
107 }
108
109 func TestValidateAlreadyRegistered(t *testing.T) {
110         regSec := "regSec"
111         providerUnderTest := APIProviderEnrolmentDetails{
112                 RegSec: regSec,
113         }
114
115         otherProvider := APIProviderEnrolmentDetails{
116                 RegSec: "otherRegSec",
117         }
118         assert.Nil(t, providerUnderTest.ValidateAlreadyRegistered(otherProvider))
119
120         otherProvider.RegSec = regSec
121         assert.NotNil(t, providerUnderTest.ValidateAlreadyRegistered(otherProvider))
122 }
123
124 func getProvider() APIProviderEnrolmentDetails {
125         testFuncs := []APIProviderFunctionDetails{
126                 {
127                         ApiProvFuncId:   &funcIdAPF,
128                         ApiProvFuncInfo: &funcInfoAPF,
129                         ApiProvFuncRole: ApiProviderFuncRoleAPF,
130                 },
131                 {
132                         ApiProvFuncId:   &funcIdAMF,
133                         ApiProvFuncInfo: &funcInfoAMF,
134                         ApiProvFuncRole: ApiProviderFuncRoleAMF,
135                 },
136                 {
137                         ApiProvFuncId:   &funcIdAEF,
138                         ApiProvFuncInfo: &funcInfoAEF,
139                         ApiProvFuncRole: ApiProviderFuncRoleAEF,
140                 },
141         }
142         return APIProviderEnrolmentDetails{
143                 ApiProvDomId:   &domainID,
144                 ApiProvDomInfo: &domainInfo,
145                 ApiProvFuncs:   &testFuncs,
146         }
147
148 }