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 ==================================================================================
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"
36 type RmrSenderMock struct {
42 var rmrSenderInst *RmrSenderMock
44 func TestMain(m *testing.M) {
45 sdlInst = new(SdlMock)
47 sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos",
48 "a1.policy_instance.20005.123456",
49 "a1.policy_instance.20005.234567",
50 "a1.policy_type.1006001",
51 "a1.policy_type.20000",
52 "a1.policy_inst_metadata.1006001.qos",
54 RMRclient = new(RMRClientMock)
56 rh = createResthook(sdlInst, RMRclient)
61 func TestHealth(t *testing.T) {
62 resp := rh.GetA1Health()
64 a1.Logger.Debug("A1 is healthy ")
65 assert.Equal(t, true, resp)
67 a1.Logger.Debug("A1 is unhealthy")
68 assert.Equal(t, false, resp)
72 func TestGetAllPolicyType(t *testing.T) {
73 resp := rh.GetAllPolicyType()
74 assert.Equal(t, 2, len(resp))
77 func TestGetPolicyType(t *testing.T) {
79 policyTypeId := models.PolicyTypeID(20001)
81 var policyTypeSchema models.PolicyTypeSchema
82 name := "admission_control_policy_mine"
83 policyTypeSchema.Name = &name
84 policytypeid := int64(20001)
85 policyTypeSchema.PolicyTypeID = &policytypeid
86 description := "various parameters to control admission of dual connection"
87 policyTypeSchema.Description = &description
88 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)",},
89 "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}`
90 policyTypeSchema.CreateSchema = schema
91 key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
95 sdlInst.On("Get", a1MediatorNs, keys[:]).Return(map[string]interface{}{key: policyTypeSchema}, nil)
96 resp := rh.GetPolicyType(policyTypeId)
97 assert.NotNil(t, resp)
99 sdlInst.AssertExpectations(t)
103 func TestCreatePolicyType(t *testing.T) {
104 var policyTypeId models.PolicyTypeID
106 var policyTypeSchema models.PolicyTypeSchema
107 name := "admission_control_policy_mine"
108 policyTypeSchema.Name = &name
109 policytypeid := int64(20001)
110 policyTypeSchema.PolicyTypeID = &policytypeid
111 description := "various parameters to control admission of dual connection"
112 policyTypeSchema.Description = &description
113 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)",},
114 "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}`
116 data, err := policyTypeSchema.MarshalBinary()
117 a1.Logger.Debug("error : %+v ", err)
118 a1.Logger.Debug("data : %+v ", data)
119 key := a1PolicyPrefix + strconv.FormatInt(20001, 10)
120 a1.Logger.Debug("key : %+v ", key)
122 sdlInst.On("SetIfNotExists", a1MediatorNs, key, string(data)).Return(true, nil)
124 errresp := rh.CreatePolicyType(policyTypeId, policyTypeSchema)
126 assert.Nil(t, errresp)
127 //Mock Assertion :Behavioral
128 sdlInst.AssertExpectations(t)
131 func TestCreatePolicyTypeInstance(t *testing.T) {
132 var policyInstanceID models.PolicyInstanceID
133 policyInstanceID = "123456"
134 var httpBody = `{"enforce":true,"window_length":20,"blocking_rate":20,"trigger_threshold":10}`
135 instancekey := a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
136 var policyTypeId models.PolicyTypeID
139 var instancedata map[string]interface{}
141 json.Unmarshal([]byte(httpBody), &instancedata)
143 data, _ := json.Marshal(instancedata)
144 a1.Logger.Debug("Marshaled data : %+v", string(data))
145 a1.Logger.Debug("instancekey : %+v", instancekey)
146 instancearr := []interface{}{instancekey, string(data)}
147 sdlInst.On("Set", "A1m_ns", instancearr).Return(nil)
149 metadatainstancekey := a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
150 creation_timestamp := time.Now()
151 var metadatajson []interface{}
152 metadatajson = append(metadatajson, map[string]string{"created_at": creation_timestamp.Format("2006-01-02 15:04:05"), "has_been_deleted": "False"})
153 metadata, _ := json.Marshal(metadatajson)
154 a1.Logger.Debug("Marshaled Metadata : %+v", string(metadata))
155 a1.Logger.Debug("metadatainstancekey : %+v", metadatainstancekey)
156 metadatainstancearr := []interface{}{metadatainstancekey, string(metadata)}
157 sdlInst.On("Set", "A1m_ns", metadatainstancearr).Return(nil)
158 rmrSenderInst.On("RmrSendToXapp", "httpBodyString", 20010).Return(true)
160 errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, instancedata)
162 assert.Nil(t, errresp)
163 sdlInst.AssertExpectations(t)
166 func TestGetPolicyInstance(t *testing.T) {
168 var policyTypeId models.PolicyTypeID
170 var policyInstanceID models.PolicyInstanceID
171 policyInstanceID = "123456"
176 "trigger_threshold":10
178 instancekey := a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
179 a1.Logger.Debug("httpBody String : %+v", httpBody)
180 a1.Logger.Debug("key : %+v", instancekey)
182 keys[0] = instancekey
184 sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody, nil)
186 resp, err := rh.GetPolicyInstance(policyTypeId, policyInstanceID)
187 a1.Logger.Error("err : %+v", err)
188 assert.NotNil(t, resp)
190 sdlInst.AssertExpectations(t)
193 func TestGetAllPolicyIntances(t *testing.T) {
194 var policyTypeId models.PolicyTypeID
196 resp, err := rh.GetAllPolicyInstance(policyTypeId)
197 a1.Logger.Error("err : %+v", err)
198 assert.Equal(t, 2, len(resp))
201 func TestDeletePolicyType(t *testing.T) {
203 policyTypeId := models.PolicyTypeID(20001)
204 key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
209 sdlInst.On("Remove", a1MediatorNs, keys[:]).Return(nil)
211 errresp := rh.DeletePolicyType(policyTypeId)
213 assert.Nil(t, errresp)
214 sdlInst.AssertExpectations(t)
217 func TestGetPolicyInstanceStatus(t *testing.T) {
218 var policyTypeId models.PolicyTypeID
220 var policyInstanceID models.PolicyInstanceID
221 policyInstanceID = "123456"
223 "created_at":"0001-01-01T00:00:00.000Z",
224 "instance_status":"NOT IN EFFECT"
226 instancekey := a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
227 a1.Logger.Debug("httpBody String : %+v", httpBody)
228 a1.Logger.Debug("key : %+v", instancekey)
230 keys[0] = instancekey
231 sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody)
233 resp := rh.GetPolicyInstanceStatus(policyTypeId, policyInstanceID)
235 assert.NotNil(t, resp)
236 sdlInst.AssertExpectations(t)
239 func TestDeletePolicyInstance(t *testing.T) {
240 var policyTypeId models.PolicyTypeID
242 var policyInstanceID models.PolicyInstanceID
243 policyInstanceID = "123456"
244 var policyTypeSchema models.PolicyTypeSchema
245 name := "admission_control_policy_mine"
246 policyTypeSchema.Name = &name
247 policytypeid := int64(20001)
248 policyTypeSchema.PolicyTypeID = &policytypeid
249 description := "various parameters to control admission of dual connection"
250 policyTypeSchema.Description = &description
251 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)",},
252 "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}`
253 policyTypeSchema.CreateSchema = schema
255 key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
256 var policytypekeys [1]string
257 policytypekeys[0] = key
259 sdlInst.On("Get", a1MediatorNs, policytypekeys[:]).Return(map[string]interface{}{key: policyTypeSchema}, nil)
265 "trigger_threshold":10
267 instancekey := a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
268 var instancekeys [1]string
269 instancekeys[0] = instancekey
271 sdlInst.On("Get", a1MediatorNs, instancekeys[:]).Return(httpBody, nil)
273 var instanceMetadataKeys [1]string
274 instanceMetadataKey := a1InstanceMetadataPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID)
275 instanceMetadataKeys[0] = instanceMetadataKey
277 "created_at":"2022-11-02 10:30:20",
278 "instance_status":"NOT IN EFFECT"
281 sdlInst.On("Get", a1MediatorNs, instanceMetadataKeys[:]).Return(httpBody, nil)
283 sdlInst.On("Remove", a1MediatorNs, instanceMetadataKeys[:]).Return(nil)
285 var metadatainstancekeys [1]string
286 metadatainstancekeys[0] = instancekey
288 sdlInst.On("Remove", a1MediatorNs, metadatainstancekeys[:]).Return(nil)
290 metadatainstancekey := a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
291 deleted_timestamp := time.Now()
292 var metadatajson interface{}
293 metadatajson = map[string]string{"created_at": "2022-11-02 10:30:20", "deleted_at": deleted_timestamp.Format("2006-01-02 15:04:05"), "has_been_deleted": "True"}
294 metadata, _ := json.Marshal(metadatajson)
295 metadatainstancearr := []interface{}{metadatainstancekey, string(metadata)}
297 sdlInst.On("Set", "A1m_ns", metadatainstancearr).Return(nil)
299 httpBodyString := `{"operation":"DELETE","payload":"","policy_instance_id":"123456","policy_type_id":"20001"}`
301 rmrSenderInst.On("RmrSendToXapp", httpBodyString, 20010).Return(true)
303 errresp := rh.DeletePolicyInstance(policyTypeId, policyInstanceID)
305 assert.Nil(t, errresp)
306 sdlInst.AssertExpectations(t)
308 func TestDataDelivery(t *testing.T) {
315 var instancedata interface{}
317 json.Unmarshal([]byte(httpBody), &instancedata)
318 a1.Logger.Debug("Marshaled data : %+v", (instancedata))
319 httpBodyString := `{"ei_job_id":"1","payload":"payload"}`
320 rmrSenderInst.On("RmrSendToXapp", httpBodyString, 20017).Return(true)
321 errresp := rh.DataDelivery(instancedata)
323 assert.Nil(t, errresp)
324 sdlInst.AssertExpectations(t)
327 type SdlMock struct {
331 func (s *SdlMock) GetAll(ns string) ([]string, error) {
332 args := s.MethodCalled("GetAll", ns)
333 return args.Get(0).([]string), nil
336 func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) {
337 a1.Logger.Debug("Get Called ")
338 args := s.MethodCalled("Get", ns, keys)
339 a1.Logger.Debug("ns :%+v", args.Get(0))
340 policytypeid := int64(20001)
341 policyInstanceID := "123456"
342 var policySchemaString string
344 if keys[0] == "a1.policy_instance.20001.123456" {
345 policySchemaString = `{
349 "trigger_threshold":10
351 key = a1InstancePrefix + strconv.FormatInt(policytypeid, 10) + "." + string(policyInstanceID)
352 } else if keys[0] == "a1.policy_type.20001" {
353 policySchemaString = `{"create_schema":{"$schema":"http://json-schema.org/draft-07/schema#","properties":{"additionalProperties":false,"blocking_rate":{"default":10,"description":"% Connections to block","maximum":1001,"minimum":1,"type":"number"},"enforce":{"default":"true","type":"boolean"},"window_length":{"default":1,"description":"Sliding window length (in minutes)","maximum":60,"minimum":1,"type":"integer"}},"type":"object"},"description":"various parameters to control admission of dual connection","name":"admission_control_policy_mine","policy_type_id":20001}`
354 key = a1PolicyPrefix + strconv.FormatInt((policytypeid), 10)
355 } else if keys[0] == "a1.policy_inst_metadata.20001.123456" {
356 policySchemaString = `{
357 "created_at":"2022-11-02 10:30:20",
358 "instance_status":"NOT IN EFFECT"
360 key = a1InstanceMetadataPrefix + strconv.FormatInt(policytypeid, 10) + "." + string(policyInstanceID)
362 a1.Logger.Debug(" policy SchemaString %+v", policySchemaString)
363 policyTypeSchema, _ := json.Marshal((policySchemaString))
364 a1.Logger.Debug(" policyTypeSchema %+v", string(policyTypeSchema))
366 a1.Logger.Debug(" key for policy type %+v", key)
367 mp := map[string]interface{}{key: string(policySchemaString)}
368 a1.Logger.Debug("Get Called and mp return %+v ", mp)
371 func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, error) {
372 args := s.MethodCalled("SetIfNotExists", ns, key, data)
373 return args.Bool(0), args.Error(1)
376 func (s *SdlMock) Set(ns string, pairs ...interface{}) error {
377 args := s.MethodCalled("Set", ns, pairs)
380 func (s *SdlMock) SetIf(ns string, key string, oldData, newData interface{}) (bool, error) {
381 args := s.MethodCalled("SetIfNotExists", ns, key, oldData, newData)
382 return args.Bool(0), args.Error(1)
385 func (rmr *RmrSenderMock) RmrSendToXapp(httpBodyString string, mtype int) bool {
386 if httpBodyString == `{"blocking_rate":20,"enforce":true,"trigger_threshold":10,"window_length":20}` {
387 args := rmr.MethodCalled("RmrSendToXapp", httpBodyString, mtype)
389 } else if httpBodyString == `{"ei_job_id":"1","payload":"payload"}` {
390 args := rmr.MethodCalled("RmrSendToXapp", httpBodyString, mtype)
396 func (s *SdlMock) Remove(ns string, keys []string) error {
397 args := s.MethodCalled("Remove", ns, keys)