2 ==================================================================================
4 Copyright (c) 2021 Samsung
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
18 This source code is part of the near-RT RIC (RAN Intelligent Controller)
19 platform project (RICP).
21 ==================================================================================
32 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
33 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models"
34 "github.com/stretchr/testify/assert"
35 "github.com/stretchr/testify/mock"
38 type RmrSenderMock struct {
44 var rmrSenderInst *RmrSenderMock
46 func TestMain(m *testing.M) {
47 sdlInst = new(SdlMock)
49 sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos",
50 "a1.policy_instance.20005.123456",
51 "a1.policy_instance.20005.234567",
52 "a1.policy_type.1006001",
53 "a1.policy_type.20000",
54 "a1.policy_inst_metadata.1006001.qos",
56 rmrSenderInst = new(RmrSenderMock)
58 rh = createResthook(sdlInst, rmrSenderInst)
63 func TestHealth(t *testing.T) {
64 resp := rh.GetA1Health()
66 a1.Logger.Debug("A1 is healthy ")
67 assert.Equal(t, true, resp)
69 a1.Logger.Debug("A1 is unhealthy")
70 assert.Equal(t, false, resp)
74 func TestGetAllPolicyType(t *testing.T) {
75 resp := rh.GetAllPolicyType()
76 assert.Equal(t, 2, len(resp))
79 func TestGetPolicyType(t *testing.T) {
81 policyTypeId := models.PolicyTypeID(20001)
83 var policyTypeSchema models.PolicyTypeSchema
84 name := "admission_control_policy_mine"
85 policyTypeSchema.Name = &name
86 policytypeid := int64(20001)
87 policyTypeSchema.PolicyTypeID = &policytypeid
88 description := "various parameters to control admission of dual connection"
89 policyTypeSchema.Description = &description
90 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)",},
91 "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}`
92 policyTypeSchema.CreateSchema = schema
93 key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
97 sdlInst.On("Get", a1MediatorNs, keys[:]).Return(map[string]interface{}{key: policyTypeSchema}, nil)
98 resp := rh.GetPolicyType(policyTypeId)
99 assert.NotNil(t, resp)
101 sdlInst.AssertExpectations(t)
105 func TestCreatePolicyType(t *testing.T) {
106 var policyTypeId models.PolicyTypeID
108 var policyTypeSchema models.PolicyTypeSchema
109 name := "admission_control_policy_mine"
110 policyTypeSchema.Name = &name
111 policytypeid := int64(20001)
112 policyTypeSchema.PolicyTypeID = &policytypeid
113 description := "various parameters to control admission of dual connection"
114 policyTypeSchema.Description = &description
115 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)",},
116 "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}`
118 data, err := policyTypeSchema.MarshalBinary()
119 a1.Logger.Debug("error : %+v ", err)
120 a1.Logger.Debug("data : %+v ", data)
121 key := a1PolicyPrefix + strconv.FormatInt(20001, 10)
122 a1.Logger.Debug("key : %+v ", key)
124 sdlInst.On("SetIfNotExists", a1MediatorNs, key, string(data)).Return(true, nil)
126 errresp := rh.CreatePolicyType(policyTypeId, policyTypeSchema)
128 assert.Nil(t, errresp)
129 //Mock Assertion :Behavioral
130 sdlInst.AssertExpectations(t)
133 func TestCreatePolicyTypeInstance(t *testing.T) {
134 var policyInstanceID models.PolicyInstanceID
135 policyInstanceID = "123456"
136 var httpBody = `{"enforce":true,"window_length":20,"blocking_rate":20,"trigger_threshold":10}`
137 instancekey := a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
138 var policyTypeId models.PolicyTypeID
141 var instancedata map[string]interface{}
143 json.Unmarshal([]byte(httpBody), &instancedata)
145 data, _ := json.Marshal(instancedata)
146 a1.Logger.Debug("Marshaled data : %+v", string(data))
147 a1.Logger.Debug("instancekey : %+v", instancekey)
148 instancearr := []interface{}{instancekey, string(data)}
149 sdlInst.On("Set", "A1m_ns", instancearr).Return(nil)
151 metadatainstancekey := a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
152 creation_timestamp := time.Now()
153 var metadatajson []interface{}
154 metadatajson = append(metadatajson, map[string]string{"created_at": creation_timestamp.Format("2006-01-02 15:04:05"), "has_been_deleted": "False"})
155 metadata, _ := json.Marshal(metadatajson)
156 a1.Logger.Debug("Marshaled Metadata : %+v", string(metadata))
157 a1.Logger.Debug("metadatainstancekey : %+v", metadatainstancekey)
158 metadatainstancearr := []interface{}{metadatainstancekey, string(metadata)}
159 sdlInst.On("Set", "A1m_ns", metadatainstancearr).Return(nil)
160 rmrSenderInst.On("RmrSendToXapp", "httpBodyString", 20010, int(policyTypeId)).Return(true)
162 errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, instancedata, "")
164 assert.Nil(t, errresp)
165 sdlInst.AssertExpectations(t)
168 func TestGetPolicyInstance(t *testing.T) {
170 var policyTypeId models.PolicyTypeID
172 var policyInstanceID models.PolicyInstanceID
173 policyInstanceID = "123456"
178 "trigger_threshold":10
180 instancekey := a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
181 a1.Logger.Debug("httpBody String : %+v", httpBody)
182 a1.Logger.Debug("key : %+v", instancekey)
184 keys[0] = instancekey
186 sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody, nil)
188 resp, err := rh.GetPolicyInstance(policyTypeId, policyInstanceID)
189 a1.Logger.Error("err : %+v", err)
190 assert.NotNil(t, resp)
192 sdlInst.AssertExpectations(t)
195 func TestGetAllPolicyIntances(t *testing.T) {
196 var policyTypeId models.PolicyTypeID
198 resp, err := rh.GetAllPolicyInstance(policyTypeId)
199 a1.Logger.Error("err : %+v", err)
200 assert.Equal(t, 2, len(resp))
203 func TestDeletePolicyType(t *testing.T) {
205 policyTypeId := models.PolicyTypeID(20001)
206 key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
211 sdlInst.On("Remove", a1MediatorNs, keys[:]).Return(nil)
213 errresp := rh.DeletePolicyType(policyTypeId)
215 assert.Nil(t, errresp)
216 sdlInst.AssertExpectations(t)
219 func TestGetPolicyInstanceStatus(t *testing.T) {
220 var policyTypeId models.PolicyTypeID
222 var policyInstanceID models.PolicyInstanceID
223 policyInstanceID = "123456"
225 "created_at":"0001-01-01T00:00:00.000Z",
226 "instance_status":"NOT IN EFFECT"
228 instancekey := a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
229 a1.Logger.Debug("httpBody String : %+v", httpBody)
230 a1.Logger.Debug("key : %+v", instancekey)
232 keys[0] = instancekey
233 sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody)
234 instancekey = a1HandlerPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
235 var instancekeys [1]string
236 instancekeys[0] = instancekey
237 instancearr := []interface{}{instancekey, "OK"}
238 sdlInst.On("Get", a1MediatorNs, instancekeys[:]).Return(instancearr, nil)
239 resp, errresp := rh.GetPolicyInstanceStatus(policyTypeId, policyInstanceID)
241 assert.Nil(t, errresp)
242 assert.NotNil(t, resp)
243 sdlInst.AssertExpectations(t)
246 func TestDeletePolicyInstance(t *testing.T) {
247 var policyTypeId models.PolicyTypeID
249 var policyInstanceID models.PolicyInstanceID
250 policyInstanceID = "123456"
251 var policyTypeSchema models.PolicyTypeSchema
252 name := "admission_control_policy_mine"
253 policyTypeSchema.Name = &name
254 policytypeid := int64(20001)
255 policyTypeSchema.PolicyTypeID = &policytypeid
256 description := "various parameters to control admission of dual connection"
257 policyTypeSchema.Description = &description
258 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)",},
260 "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}`
262 policyTypeSchema.CreateSchema = schema
264 key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
265 var policytypekeys [1]string
266 policytypekeys[0] = key
268 sdlInst.On("Get", a1MediatorNs, policytypekeys[:]).Return(map[string]interface{}{key: policyTypeSchema}, nil)
274 "trigger_threshold":10
276 instancekey := a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
277 var instancekeys [1]string
278 instancekeys[0] = instancekey
280 sdlInst.On("Get", a1MediatorNs, instancekeys[:]).Return(httpBody, nil)
282 var instanceMetadataKeys [1]string
283 instanceMetadataKey := a1InstanceMetadataPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID)
284 instanceMetadataKeys[0] = instanceMetadataKey
286 "created_at":"2022-11-02 10:30:20",
287 "instance_status":"NOT IN EFFECT"
290 sdlInst.On("Get", a1MediatorNs, instanceMetadataKeys[:]).Return(httpBody, nil)
292 sdlInst.On("Remove", a1MediatorNs, instanceMetadataKeys[:]).Return(nil)
294 var metadatainstancekeys [1]string
295 metadatainstancekeys[0] = instancekey
297 sdlInst.On("Remove", a1MediatorNs, metadatainstancekeys[:]).Return(nil)
299 metadatainstancekey := a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
300 deleted_timestamp := time.Now()
301 var metadatajson interface{}
302 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"}
303 metadata, _ := json.Marshal(metadatajson)
304 metadatainstancearr := []interface{}{metadatainstancekey, string(metadata)}
306 sdlInst.On("Set", "A1m_ns", metadatainstancearr).Return(nil)
308 httpBodyString := `{"operation":"DELETE","payload":"","policy_instance_id":"123456","policy_type_id":"20001"}`
310 rmrSenderInst.On("RmrSendToXapp", httpBodyString, 20010, int(policyTypeId)).Return(true)
311 notificationDestinationkey := a1NotificationDestinationPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID)
312 var notificationDestinationkeys [1]string
313 notificationDestinationkeys[0] = notificationDestinationkey
314 sdlInst.On("Remove", a1MediatorNs, notificationDestinationkeys[:]).Return(nil)
315 errresp := rh.DeletePolicyInstance(policyTypeId, policyInstanceID)
317 assert.Nil(t, errresp)
318 sdlInst.AssertExpectations(t)
320 func TestDataDelivery(t *testing.T) {
327 var instancedata interface{}
329 json.Unmarshal([]byte(httpBody), &instancedata)
330 a1.Logger.Debug("Marshaled data : %+v", (instancedata))
331 httpBodyString := `{"ei_job_id":"1","payload":"payload"}`
332 rmrSenderInst.On("RmrSendToXapp", httpBodyString, 20017, -1).Return(true)
333 errresp := rh.DataDelivery(instancedata)
335 assert.Nil(t, errresp)
336 sdlInst.AssertExpectations(t)
339 func TestGetMetaData(t *testing.T) {
340 var policyTypeId models.PolicyTypeID
342 var policyInstanceID models.PolicyInstanceID
343 policyInstanceID = "123456"
344 instanceMetadataKey := a1InstanceMetadataPrefix + strconv.FormatInt((int64(policyTypeId)), 10) + "." + string(policyInstanceID)
345 a1.Logger.Debug("key : %+v", instanceMetadataKey)
348 keys[0] = instanceMetadataKey
350 policySchemaString := `{
351 "created_at":"2022-11-02 10:30:20",
352 "instance_status":"NOT IN EFFECT"
355 sdlInst.On("Get", a1MediatorNs, keys[:]).Return(map[string]interface{}{instanceMetadataKey: policySchemaString}, nil)
357 resp, errresp := rh.getMetaData(policyTypeId, policyInstanceID)
359 assert.Nil(t, errresp)
360 assert.NotNil(t, resp)
361 sdlInst.AssertExpectations(t)
364 type SdlMock struct {
368 func (s *SdlMock) GetAll(ns string) ([]string, error) {
369 args := s.MethodCalled("GetAll", ns)
370 return args.Get(0).([]string), nil
373 func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) {
374 a1.Logger.Debug("Get Called ")
375 args := s.MethodCalled("Get", ns, keys)
376 a1.Logger.Debug("ns :%+v", args.Get(0))
377 policytypeid := int64(20001)
378 policyInstanceID := "123456"
379 var policySchemaString string
381 if keys[0] == "a1.policy_instance.20001.123456" {
382 policySchemaString = `{
386 "trigger_threshold":10
388 key = a1InstancePrefix + strconv.FormatInt(policytypeid, 10) + "." + string(policyInstanceID)
389 } else if keys[0] == "a1.policy_type.20001" {
390 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}`
391 key = a1PolicyPrefix + strconv.FormatInt((policytypeid), 10)
392 } else if keys[0] == "a1.policy_inst_metadata.20001.123456" {
393 policySchemaString = `{
394 "created_at":"2022-11-02 10:30:20",
395 "instance_status":"NOT IN EFFECT"
397 key = a1InstanceMetadataPrefix + strconv.FormatInt(policytypeid, 10) + "." + string(policyInstanceID)
399 a1.Logger.Debug(" policy SchemaString %+v", policySchemaString)
400 policyTypeSchema, _ := json.Marshal((policySchemaString))
401 a1.Logger.Debug(" policyTypeSchema %+v", string(policyTypeSchema))
403 a1.Logger.Debug(" key for policy type %+v", key)
404 mp := map[string]interface{}{key: string(policySchemaString)}
405 a1.Logger.Debug("Get Called and mp return %+v ", mp)
408 func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, error) {
409 args := s.MethodCalled("SetIfNotExists", ns, key, data)
410 return args.Bool(0), args.Error(1)
413 func (s *SdlMock) Set(ns string, pairs ...interface{}) error {
414 args := s.MethodCalled("Set", ns, pairs)
417 func (s *SdlMock) SetIf(ns string, key string, oldData, newData interface{}) (bool, error) {
418 args := s.MethodCalled("SetIfNotExists", ns, key, oldData, newData)
419 return args.Bool(0), args.Error(1)
422 func (rmr *RmrSenderMock) RmrSendToXapp(httpBodyString string, mtype int, subid int) bool {
423 if httpBodyString == `{"blocking_rate":20,"enforce":true,"trigger_threshold":10,"window_length":20}` {
424 args := rmr.MethodCalled("RmrSendToXapp", httpBodyString, mtype, subid)
426 } else if httpBodyString == `{"ei_job_id":"1","payload":"payload"}` {
427 args := rmr.MethodCalled("RmrSendToXapp", httpBodyString, mtype, subid)
433 func (s *SdlMock) Remove(ns string, keys []string) error {
434 args := s.MethodCalled("Remove", ns, keys)