Replacing a1-python with a1-go implementation
[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         "os"
26         "strconv"
27         "testing"
28
29         "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
30         "github.com/stretchr/testify/assert"
31         "github.com/stretchr/testify/mock"
32 )
33
34 type SdlMock struct {
35         mock.Mock
36 }
37
38 var sdlInst *SdlMock
39 var pm *PolicyManager
40
41 func TestMain(m *testing.M) {
42         sdlInst = new(SdlMock)
43         a1.Init()
44         pm = createPolicyManager(sdlInst)
45         code := m.Run()
46         os.Exit(code)
47 }
48 func TestSetPolicyInstance(t *testing.T) {
49         var policyTypeId int
50         policyTypeId = 20001
51         var policyInstanceID int
52         policyInstanceID = 123456
53         var status string
54         status = "OK"
55         instancehandlerKey := a1HandlerPrefix + strconv.FormatInt(20001, 10) + "." + strconv.FormatInt(int64(policyInstanceID), 10)
56         instancearr := []interface{}{instancehandlerKey, status}
57         sdlInst.On("Set", "A1m_ns", instancehandlerKey, instancearr).Return(nil)
58         errresp := pm.SetPolicyInstanceStatus(policyTypeId, policyInstanceID, status)
59         assert.NoError(t, errresp)
60         sdlInst.AssertExpectations(t)
61 }
62
63 func TestGetAllPolicyIntances(t *testing.T) {
64         var policyTypeId int
65         policyTypeId = 20005
66         sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos",
67                 "a1.policy_instance.20005.123456",
68                 "a1.policy_instance.20005.234567",
69                 "a1.policy_type.1006001",
70                 "a1.policy_type.20000",
71                 "a1.policy_inst_metadata.1006001.qos",
72         }, nil)
73         resp, err := pm.GetAllPolicyInstance(policyTypeId)
74         assert.NoError(t, err)
75         assert.Equal(t, 2, len(resp))
76 }
77
78 func (s *SdlMock) Set(ns string, pairs ...interface{}) error {
79         args := s.MethodCalled("Set", ns, pairs)
80         return args.Error(0)
81 }
82
83 func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) {
84         a1.Logger.Error("Get Called ")
85         return map[string]interface{}{}, nil
86 }
87
88 func (s *SdlMock) GetAll(ns string) ([]string, error) {
89         args := s.MethodCalled("GetAll", ns)
90         return args.Get(0).([]string), nil
91 }