2 ==================================================================================
3 Copyright (c) 2021 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 ==================================================================================
29 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
30 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models"
31 "github.com/stretchr/testify/assert"
32 "github.com/stretchr/testify/mock"
38 func TestMain(m *testing.M) {
39 sdlInst = new(SdlMock)
41 sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos",
42 "a1.policy_type.1006001",
43 "a1.policy_type.20000",
44 "a1.policy_inst_metadata.1006001.qos",
48 rh = createResthook(sdlInst)
53 func TestGetAllPolicyType(t *testing.T) {
54 resp := rh.GetAllPolicyType()
55 assert.Equal(t, 2, len(resp))
58 func TestGetPolicyType(t *testing.T) {
60 policyTypeId := models.PolicyTypeID(20001)
62 resp := rh.GetPolicyType(policyTypeId)
64 var policyTypeSchema models.PolicyTypeSchema
65 name := "admission_control_policy_mine"
66 policyTypeSchema.Name = &name
67 policytypeid := int64(20001)
68 policyTypeSchema.PolicyTypeID = &policytypeid
69 description := "various parameters to control admission of dual connection"
70 policyTypeSchema.Description = &description
71 schema := `{"$schema": "http://json-schema.org/draft-07/schema#","type":"object","properties": {"enforce": {"type":"boolean","default":"true",},"window_length": {"type": "integer","default":1,"minimum":1,"maximum":60,"description": "Sliding window length (in minutes)",},
72 "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}`
73 policyTypeSchema.CreateSchema = schema
74 key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
77 sdlInst.On("Get", a1MediatorNs, policyTypeId).Return(map[string]interface{}{key: policyTypeSchema}, nil)
79 assert.NotNil(t, resp)
81 sdlInst.AssertExpectations(t)
85 func TestCreatePolicyType(t *testing.T) {
86 var policyTypeId models.PolicyTypeID
88 var policyTypeSchema models.PolicyTypeSchema
89 name := "admission_control_policy_mine"
90 policyTypeSchema.Name = &name
91 policytypeid := int64(20001)
92 policyTypeSchema.PolicyTypeID = &policytypeid
93 description := "various parameters to control admission of dual connection"
94 policyTypeSchema.Description = &description
95 policyTypeSchema.CreateSchema = `{"$schema": "http://json-schema.org/draft-07/schema#","type":"object","properties": {"enforce": {"type":"boolean","default":"true",},"window_length": {"type": "integer","default":1,"minimum":1,"maximum":60,"description": "Sliding window length (in minutes)",},
96 "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}`
98 data, err := policyTypeSchema.MarshalBinary()
99 a1.Logger.Debug("error : %+v ", err)
100 a1.Logger.Debug("data : %+v ", data)
101 key := a1PolicyPrefix + strconv.FormatInt(20001, 10)
102 a1.Logger.Debug("key : %+v ", key)
104 sdlInst.On("SetIfNotExists", a1MediatorNs, key, string(data)).Return(true, nil)
106 errresp := rh.CreatePolicyType(policyTypeId, policyTypeSchema)
108 assert.Nil(t, errresp)
109 //Mock Assertion :Behavioral
110 sdlInst.AssertExpectations(t)
113 func TestCreatePolicyTypeInstance(t *testing.T) {
114 var policyTypeId models.PolicyTypeID
116 var policyInstanceID models.PolicyInstanceID
117 policyInstanceID = "123456"
122 "trigger_threshold":10
124 instancekey := a1PolicyPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
125 data, _ := json.Marshal(httpBody)
126 a1.Logger.Debug("Marshaled String : %+v", string(data))
127 a1.Logger.Debug("key : %+v", instancekey)
129 instancearr := []interface{}{instancekey, string(data)}
130 sdlInst.On("Set", "A1m_ns", instancearr).Return("CREATE", nil)
131 errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, httpBody)
133 assert.Nil(t, errresp)
134 sdlInst.AssertExpectations(t)
137 func TestGetPolicyInstance(t *testing.T) {
139 var policyTypeId models.PolicyTypeID
141 var policyInstanceID models.PolicyInstanceID
142 policyInstanceID = "123456"
147 "trigger_threshold":10
149 instancekey := a1PolicyPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
150 a1.Logger.Debug("httpBody String : %+v", httpBody)
151 a1.Logger.Debug("key : %+v", instancekey)
153 keys[0] = instancekey
155 sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody, nil)
157 resp := rh.GetPolicyInstance(policyTypeId, policyInstanceID)
158 a1.Logger.Error("resp : %+v", resp)
159 assert.NotNil(t, resp)
161 sdlInst.AssertExpectations(t)
164 type SdlMock struct {
168 func (s *SdlMock) GetAll(ns string) ([]string, error) {
169 args := s.MethodCalled("GetAll", ns)
170 return args.Get(0).([]string), nil
173 func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) {
174 a1.Logger.Debug("Get Called ")
175 args := s.MethodCalled("Get", ns, keys)
176 a1.Logger.Debug("keys :%+v", args.Get(1))
177 policytypeid := int64(20001)
179 policyTypeSchemaString := `{"name":"admission_control_policy_mine","description":"various parameters to control admission of dual connection","policy_type_id": 20001,"create_schema":{"$schema": "http://json-schema.org/draft-07/schema#","type": "object","properties": {"enforce": {"type": "boolean","default": "true"},"window_length": {"type":"integer","default": 1,"minimum": 1,"maximum": 60,"description": "Sliding window length (in minutes)"},"blocking_rate": {"type": "number","default": 10,"minimum": 1,"maximum": 1001,"description": "% Connections to block"},"additionalProperties": false}}}`
181 a1.Logger.Error(" policyTypeSchemaString %+v", policyTypeSchemaString)
182 policyTypeSchema, _ := json.Marshal((policyTypeSchemaString))
183 // a1.Logger.Error(" policyTypeSchema error %+v", err)
184 a1.Logger.Error(" policyTypeSchema %+v", string(policyTypeSchema))
185 var p models.PolicyTypeSchema
186 _ = json.Unmarshal([]byte(string(policyTypeSchemaString)), &p)
187 a1.Logger.Error("unmarshalled policyTypeSchema %+v", p.CreateSchema)
188 key := a1PolicyPrefix + strconv.FormatInt((policytypeid), 10)
189 a1.Logger.Error(" key for policy type %+v", key)
190 mp := map[string]interface{}{key: string(policyTypeSchema)}
191 a1.Logger.Error("Get Called and mp return %+v ", mp)
195 func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, error) {
196 args := s.MethodCalled("SetIfNotExists", ns, key, data)
197 return args.Bool(0), args.Error(1)
200 func (s *SdlMock) Set(ns string, pairs ...interface{}) error {
201 args := s.MethodCalled("Set", ns, pairs)
204 func (s *SdlMock) SetIf(ns string, key string, oldData, newData interface{}) (bool, error) {
205 args := s.MethodCalled("SetIfNotExists", ns, key, oldData, newData)
206 return args.Bool(0), args.Error(1)