2 ==================================================================================
3 Copyright (c) 2022 Samsung
5 Licensed under the Apache License, Version 2.0 (the "License");
6 you may not use this file except in compliance with the License.
7 You may obtain a copy of the License at
9 http://www.apache.org/licenses/LICENSE-2.0
11 Unless required by applicable law or agreed to in writing, software
12 distributed under the License is distributed on an "AS IS" BASIS,
13 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 See the License for the specific language governing permissions and
15 limitations under the License.
17 This source code is part of the near-RT RIC (RAN Intelligent Controller)
18 platform project (RICP).
19 ==================================================================================
30 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
31 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models"
32 "github.com/stretchr/testify/assert"
33 "github.com/stretchr/testify/mock"
37 a1InstanceMetadataPrefix = "a1.policy_inst_metadata."
48 func TestMain(m *testing.M) {
49 sdlInst = new(SdlMock)
51 pm = createPolicyManager(sdlInst)
55 func TestSetPolicyInstance(t *testing.T) {
58 policyInstanceID := "123456"
61 instancehandlerKey := a1HandlerPrefix + strconv.FormatInt(20001, 10) + "." + policyInstanceID
62 instancearr := []interface{}{instancehandlerKey, status}
63 sdlInst.On("Set", "A1m_ns", instancearr).Return(nil)
64 errresp := pm.SetPolicyInstanceStatus(policyTypeId, policyInstanceID, status)
65 assert.NoError(t, errresp)
66 sdlInst.AssertExpectations(t)
69 func TestSetPolicyInstanceFail(t *testing.T) {
72 policyInstanceID := ""
75 sdlInst.On("Set", "A1m_ns", mock.Anything).Return(errors.New("Some Error"))
76 errresp := pm.SetPolicyInstanceStatus(policyTypeId, policyInstanceID, status)
77 a1.Logger.Debug("err from set test : %+v", errresp)
78 assert.Error(t, errresp)
79 sdlInst.AssertExpectations(t)
82 func TestGetPolicyInstance(t *testing.T) {
84 var policyTypeId models.PolicyTypeID
86 var policyInstanceID models.PolicyInstanceID
87 policyInstanceID = "123456"
88 policyString := "testval"
89 typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
90 a1.Logger.Debug("policyString String : %+v", policyString)
91 a1.Logger.Debug("typekey from get test : %+v", typekey)
94 testmp1 := map[string]interface{}{typekey: string(policyString)}
96 sdlInst.On("Get", "A1m_ns", keys[:]).Return(testmp1, nil).Once()
97 instancekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID)
98 a1.Logger.Debug("policyString String : %+v", policyString)
99 a1.Logger.Debug("instancekey from get test : %+v", instancekey)
100 var instancekeys [1]string
101 instancekeys[0] = instancekey
102 testmp2 := map[string]interface{}{instancekey: string(policyString)}
104 sdlInst.On("Get", "A1m_ns", instancekeys[:]).Return(testmp2, nil).Once()
105 resp, err := pm.GetPolicyInstance(policyTypeId, policyInstanceID)
106 a1.Logger.Debug("resp from get test : %+v", resp)
107 a1.Logger.Debug("err from get test : %+v", err)
108 //sdlInst.AssertExpectations(t)
109 assert.NoError(t, err)
110 assert.NotNil(t, resp)
111 sdlInst.AssertExpectations(t)
114 func TestGetPolicyInstanceFail1(t *testing.T) {
115 var policyTypeId models.PolicyTypeID
117 var policyInstanceID models.PolicyInstanceID
118 policyInstanceID = "123"
119 policyString := "testval"
120 typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
121 a1.Logger.Debug("policyString String : %+v", policyString)
122 a1.Logger.Debug("typekey from get test : %+v", typekey)
125 testmp1 := map[string]interface{}{typekey: string(policyString)}
127 sdlInst.On("Get", "A1m_ns", keys[:]).Return(testmp1,errors.New("Some Error")).Once()
128 resp, err := pm.GetPolicyInstance(policyTypeId, policyInstanceID)
129 a1.Logger.Debug("resp from get test : %+v", resp)
130 a1.Logger.Debug("err from get test : %+v", err)
131 //sdlInst.AssertExpectations(t)
133 assert.NotNil(t, err)
134 sdlInst.AssertExpectations(t)
137 func TestGetPolicyInstanceFail2(t *testing.T) {
138 var policyTypeId models.PolicyTypeID
140 var policyInstanceID models.PolicyInstanceID
141 policyInstanceID = "123"
142 policyString := "testval"
143 typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
144 a1.Logger.Debug("policyString String : %+v", policyString)
145 a1.Logger.Debug("typekey from get test : %+v", typekey)
148 testmp1 := map[string]interface{}{typekey: string(policyString)}
150 sdlInst.On("Get", "A1m_ns", keys[:]).Return(testmp1,nil).Once()
151 resp, err := pm.GetPolicyInstance(policyTypeId, policyInstanceID)
152 a1.Logger.Debug("resp from get test : %+v", resp)
153 a1.Logger.Debug("err from get test : %+v", err)
154 //sdlInst.AssertExpectations(t)
156 assert.NotNil(t, err)
157 sdlInst.AssertExpectations(t)
160 func TestGetPolicyInstanceFail3(t *testing.T) {
162 var policyTypeId models.PolicyTypeID
164 var policyInstanceID models.PolicyInstanceID
165 policyInstanceID = "123456"
166 policyString := "testval"
167 typekey := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
168 a1.Logger.Debug("policyString String : %+v", policyString)
169 a1.Logger.Debug("typekey from get test : %+v", typekey)
172 testmp1 := map[string]interface{}{typekey: string(policyString)}
174 sdlInst.On("Get", "A1m_ns", keys[:]).Return(testmp1, nil).Once()
175 instancekey := a1InstancePrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID)
176 a1.Logger.Debug("policyString String : %+v", policyString)
177 a1.Logger.Debug("instancekey from get test : %+v", instancekey)
178 var instancekeys [1]string
179 instancekeys[0] = instancekey
180 testmp2 := map[string]interface{}{instancekey: string(policyString)}
182 sdlInst.On("Get", "A1m_ns", instancekeys[:]).Return(testmp2,errors.New("Some Error")).Once()
183 resp, err := pm.GetPolicyInstance(policyTypeId, policyInstanceID)
184 a1.Logger.Debug("resp from get test : %+v", resp)
185 a1.Logger.Debug("err from get test : %+v", err)
186 //sdlInst.AssertExpectations(t)
187 assert.NotNil(t, err)
188 sdlInst.AssertExpectations(t)
191 func TestGetPolicyInstanceStatus(t *testing.T) {
194 policyInstanceID := "123456"
195 policyString := "testval"
196 instancekey := a1HandlerPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + policyInstanceID
197 a1.Logger.Debug("policyString String : %+v", policyString)
198 a1.Logger.Debug("key from get test : %+v", instancekey)
200 keys[0] = instancekey
201 instancearr := []interface{}{instancekey, "OK"}
203 sdlInst.On("Get", "A1m_ns", keys[:]).Return(instancearr, nil).Once()
204 resp, errresp := pm.GetPolicyInstanceStatus(policyTypeId, policyInstanceID)
205 a1.Logger.Debug("resp from get test : %+v", resp)
206 a1.Logger.Debug("errresp from get test : %+v", errresp)
207 assert.Nil(t, errresp)
208 assert.NotNil(t, resp)
209 sdlInst.AssertExpectations(t)
212 func TestGetPolicyInstanceStatusFail(t *testing.T) {
215 policyInstanceID := "123"
216 policyString := "testval"
217 instancekey := a1HandlerPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + policyInstanceID
218 a1.Logger.Debug("policyString String : %+v", policyString)
219 a1.Logger.Debug("key from get test : %+v", instancekey)
221 keys[0] = instancekey
222 instancearr := []interface{}{instancekey, "NOK"}
224 sdlInst.On("Get", "A1m_ns", keys[:]).Return(instancearr, errors.New("Some Error")).Once()
225 resp, errresp := pm.GetPolicyInstanceStatus(policyTypeId, policyInstanceID)
226 a1.Logger.Debug("resp from get test : %+v", resp)
227 a1.Logger.Debug("errresp from get test : %+v", errresp)
228 assert.NotNil(t, errresp)
229 sdlInst.AssertExpectations(t)
232 func TestSendPolicyStatusNotification(t *testing.T) {
235 policyInstanceID := "123456"
236 notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + fmt.Sprint(policyInstanceID)
237 urlString := "http://www.abc.com"
238 a1.Logger.Debug("Test: Calling key from get test : %+v", notificationDestinationkey)
239 keys := [1]string{notificationDestinationkey}
242 notDes := map[string]interface{}{notificationDestinationkey: string(urlString)}
244 sdlInst.On("Get", "A1m_ns", keys[:]).Return(notDes, nil).Once()
245 instancekey := a1HandlerPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + policyInstanceID
246 var instancekeys [1]string
247 instancekeys[0] = instancekey
248 instancearr := []interface{}{instancekey, "OK"}
250 sdlInst.On("Get", "A1m_ns", instancekeys[:]).Return(instancearr, nil).Once()
251 err := pm.SendPolicyStatusNotification(policyTypeId,policyInstanceID,notificationDestinationkey,status)
253 sdlInst.AssertExpectations(t)
256 func TestSendPolicyStatusNotificationFail1(t *testing.T) {
259 policyInstanceID := "12345"
260 notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + fmt.Sprint(policyInstanceID)
261 urlString := "www.abc.com"
262 a1.Logger.Debug("Test: Calling key from get test : %+v", notificationDestinationkey)
263 keys := [1]string{notificationDestinationkey}
266 notDes := map[string]interface{}{notificationDestinationkey: string(urlString)}
268 sdlInst.On("Get", "A1m_ns", keys[:]).Return(notDes, errors.New("Some Error")).Once()
269 err := pm.SendPolicyStatusNotification(policyTypeId,policyInstanceID,notificationDestinationkey,status)
270 assert.NotNil(t, err)
271 sdlInst.AssertExpectations(t)
274 func TestSendPolicyStatusNotificationFail2(t *testing.T) {
277 policyInstanceID := "123456"
278 notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + fmt.Sprint(policyInstanceID)
279 urlString := "http://www.abc.com"
280 a1.Logger.Debug("Test: Calling key from get test : %+v", notificationDestinationkey)
281 keys := [1]string{notificationDestinationkey}
284 notDes := map[string]interface{}{notificationDestinationkey: string(urlString)}
286 sdlInst.On("Get", "A1m_ns", keys[:]).Return(notDes,nil).Once()
287 instancekey := a1HandlerPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + policyInstanceID
288 var instancekeys [1]string
289 instancekeys[0] = instancekey
290 instancearr := []interface{}{instancekey, "NOK"}
292 sdlInst.On("Get", "A1m_ns", instancekeys[:]).Return(instancearr, errors.New("Some Error")).Once()
293 err := pm.SendPolicyStatusNotification(policyTypeId,policyInstanceID,notificationDestinationkey,status)
294 assert.NotNil(t, err)
295 sdlInst.AssertExpectations(t)
298 func TestGetAllPolicyIntances(t *testing.T) {
301 keys := []string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",}
302 sdlInst.On("GetAll", "A1m_ns").Return(keys,nil).Once()
303 resp, err := pm.GetAllPolicyInstance(policyTypeId)
304 fmt.Println("GETALL is ",resp)
305 assert.NoError(t, err)
306 assert.Equal(t, 2, len(resp))
309 func TestGetAllPolicyIntancesFail(t *testing.T) {
312 keys := []string{"a1.policy_instance.1006001.qos","a1.policy_instance.20005.123456","a1.policy_instance.20005.234567","a1.policy_type.1006001","a1.policy_type.20000","a1.policy_inst_metadata.1006001.qos",}
313 sdlInst.On("GetAll", "A1m_ns").Return(keys,nil).Once()
314 resp, err := pm.GetAllPolicyInstance(policyTypeId)
315 fmt.Println("GETALL is ",resp)
317 assert.Equal(t, 0, len(resp))
321 func TestGetAllPolicyIntancesFail2(t *testing.T) {
322 keys := []string{"",}
323 sdlInst.On("GetAll", "A1m_ns").Return(keys, errors.New("Some Error")).Once()
324 resp, err := pm.GetAllPolicyInstance(0)
325 fmt.Println("GETALL is ",resp)
326 fmt.Println("GETALL is ",err)
328 assert.Equal(t, 0, len(resp))
332 func (s *SdlMock) Set(ns string, pairs ...interface{}) error {
333 args := s.MethodCalled("Set", ns, pairs)
338 func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) {
339 a1.Logger.Debug("Mock:Get Called ")
340 args := s.MethodCalled("Get", ns, keys)
341 a1.Logger.Debug("Mock: ns :%+v", args.Get(0))
342 var policySchemaString string
344 a1.Logger.Debug("Mock: Key[0] is:%+v", keys[0])
345 if keys[0] == "a1.policy_instance.20001.123456" {
346 policySchemaString = "testval"
347 key = a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + "123456"
348 } else if keys[0] == "a1.policy_type.20001" {
349 policySchemaString = "testval"
350 key = a1PolicyPrefix + strconv.FormatInt((int64(20001)), 10)
351 } else if keys[0] == "a1.policy_notification_destination.20001.123456" {
352 policySchemaString = "http://www.xyz.com"
353 key = a1NotificationDestinationPrefix + strconv.FormatInt((int64(20001)), 10) + "." + "123456"
354 } else if keys[0] == "a1.policy_handler.20001.123456" {
355 policySchemaString = "testval"
356 key = a1HandlerPrefix + strconv.FormatInt(20001, 10) + "." + "123456"
357 } else if keys[0] == "a1.policy_inst_metadata.20001.123456" {
358 policySchemaString = "testval"
359 key = a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + "123456"
360 } else if keys[0] == "a1.policy_notification_destination.20000.12345" {
361 policySchemaString = "www.xyz.com"
362 key = a1NotificationDestinationPrefix + strconv.FormatInt((int64(20000)), 10) + "." + "12345"
364 a1.Logger.Debug(" Mock: policy SchemaString %+v", policySchemaString)
365 a1.Logger.Debug(" Mock: key for policy type %+v", key)
366 mp := map[string]interface{}{key: string(policySchemaString)}
367 a1.Logger.Debug("Mock: Get Called and mp return %+v ", mp)
368 return mp, args.Error(1)
371 func (s *SdlMock) GetAll(ns string) ([]string, error) {
372 args := s.MethodCalled("GetAll", ns)
373 return args.Get(0).([]string), args.Error(1)
376 func (s *SdlMock) RemoveAll(ns string) error {
377 //args := s.MethodCalled("RemoveAll", ns)