2b2ac35bd6561fba2092172395b91c1e7655bd46
[ric-plt/a1.git] / pkg / policy / policyManager_test.go
1 /*
2 ==================================================================================
3   Copyright (c) 2022 Samsung
4
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
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
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.
16
17    This source code is part of the near-RT RIC (RAN Intelligent Controller)
18    platform project (RICP).
19 ==================================================================================
20 */
21
22 package policy
23
24 import (
25         "errors"
26         "os"
27         "strconv"
28         "testing"
29         "fmt"
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"
34 )
35
36 const (
37         a1InstanceMetadataPrefix        = "a1.policy_inst_metadata."
38 )
39
40
41 type SdlMock struct {
42         mock.Mock
43 }
44
45 var sdlInst *SdlMock
46 var pm *PolicyManager
47
48 func TestMain(m *testing.M) {
49         sdlInst = new(SdlMock)
50         a1.Init()
51         pm = createPolicyManager(sdlInst)
52         code := m.Run()
53         os.Exit(code)
54 }
55 func TestSetPolicyInstance(t *testing.T) {
56         var policyTypeId int
57         policyTypeId = 20001
58         policyInstanceID := "123456"
59         var status string
60         status = "OK"
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)
67 }
68
69 func TestSetPolicyInstanceFail(t *testing.T) {
70         var policyTypeId int
71         policyTypeId = 0
72         policyInstanceID := ""
73         var status string
74         status = "NOK"
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)
80 }
81
82 func TestGetPolicyInstance(t *testing.T) {
83
84         var policyTypeId models.PolicyTypeID
85         policyTypeId = 20001
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)
92         var keys [1]string
93         keys[0] = typekey
94         testmp1 := map[string]interface{}{typekey: string(policyString)}
95         //Setup Expectations
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)}
103         //Setup Expectations
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)
112 }
113
114 func TestGetPolicyInstanceFail1(t *testing.T) {
115         var policyTypeId models.PolicyTypeID
116         policyTypeId = 20009
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)
123         var keys [1]string
124         keys[0] = typekey
125         testmp1 := map[string]interface{}{typekey: string(policyString)}
126         //Setup Expectations
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)
132         assert.Nil(t, resp)
133         assert.NotNil(t, err)
134         sdlInst.AssertExpectations(t)
135
136 }
137 func TestGetPolicyInstanceFail2(t *testing.T) {
138         var policyTypeId models.PolicyTypeID
139         policyTypeId = 20009
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)
146         var keys [1]string
147         keys[0] = typekey
148         testmp1 := map[string]interface{}{typekey: string(policyString)}
149         //Setup Expectations
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)
155         assert.Nil(t, resp)
156         assert.NotNil(t, err)
157         sdlInst.AssertExpectations(t)
158 }
159
160 func TestGetPolicyInstanceFail3(t *testing.T) {
161
162         var policyTypeId models.PolicyTypeID
163         policyTypeId = 20001
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)
170         var keys [1]string
171         keys[0] = typekey
172         testmp1 := map[string]interface{}{typekey: string(policyString)}
173         //Setup Expectations
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)}
181         //Setup Expectations
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)
189 }
190
191 func TestGetPolicyInstanceStatus(t *testing.T) {
192         var policyTypeId int
193         policyTypeId = 20001
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)
199         var keys [1]string
200         keys[0] = instancekey
201         instancearr := []interface{}{instancekey, "OK"}
202         //Setup Expectations
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)
210 }
211
212 func TestGetPolicyInstanceStatusFail(t *testing.T) {
213         var policyTypeId int
214         policyTypeId = 200
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)
220         var keys [1]string
221         keys[0] = instancekey
222         instancearr := []interface{}{instancekey, "NOK"}
223         //Setup Expectations
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)
230 }
231
232 func TestSendPolicyStatusNotification(t *testing.T) {
233         var policyTypeId int
234         policyTypeId = 20001
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}
240         var status string
241         status = "OK"
242         notDes := map[string]interface{}{notificationDestinationkey: string(urlString)}
243         //Setup Expectations
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"}
249         //Setup Expectations
250         sdlInst.On("Get", "A1m_ns", instancekeys[:]).Return(instancearr, nil).Once()
251         err := pm.SendPolicyStatusNotification(policyTypeId,policyInstanceID,notificationDestinationkey,status)
252         assert.Nil(t, err)
253         sdlInst.AssertExpectations(t)
254 }
255
256 func TestSendPolicyStatusNotificationFail1(t *testing.T) {
257         var policyTypeId int
258         policyTypeId = 20000
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}
264         var status string
265         status = "OK"
266         notDes := map[string]interface{}{notificationDestinationkey: string(urlString)}
267         //Setup Expectations
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)
272 }
273
274 func TestSendPolicyStatusNotificationFail2(t *testing.T) {
275         var policyTypeId int
276         policyTypeId = 20001
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}
282         var status string
283         status = "OK"
284         notDes := map[string]interface{}{notificationDestinationkey: string(urlString)}
285         //Setup Expectations
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"}
291         //Setup Expectations
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)
296 }
297
298 func TestGetAllPolicyIntances(t *testing.T) {
299         var policyTypeId int
300         policyTypeId = 20005
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))
307 }
308
309 func TestGetAllPolicyIntancesFail(t *testing.T) {
310         var policyTypeId int
311         policyTypeId = 20009
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)
316         assert.Error(t, err)
317         assert.Equal(t, 0, len(resp))
318 }
319
320
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)
327         assert.Error(t, err)
328         assert.Equal(t, 0, len(resp))
329 }
330
331
332 func (s *SdlMock) Set(ns string, pairs ...interface{}) error {
333         args := s.MethodCalled("Set", ns, pairs)
334         return args.Error(0)
335 }
336
337
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
343         var key 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"
363         }
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)
369 }
370
371 func (s *SdlMock) GetAll(ns string) ([]string, error) {
372         args := s.MethodCalled("GetAll", ns)
373         return args.Get(0).([]string), args.Error(1)
374 }
375
376 func (s *SdlMock) RemoveAll(ns string) error {
377         //args := s.MethodCalled("RemoveAll", ns)
378         return nil
379 }
380