subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
| 3 | Copyright (c) 2021 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 | package resthooks |
| 22 | |
| 23 | import ( |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 24 | "encoding/json" |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 25 | "os" |
naman.gupta | d5aa5be | 2022-02-23 11:44:43 +0530 | [diff] [blame] | 26 | "strconv" |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 27 | "testing" |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 28 | "time" |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 29 | |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 30 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" |
naman.gupta | d5aa5be | 2022-02-23 11:44:43 +0530 | [diff] [blame] | 31 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models" |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 32 | "github.com/stretchr/testify/assert" |
| 33 | "github.com/stretchr/testify/mock" |
| 34 | ) |
| 35 | |
| 36 | var rh *Resthook |
| 37 | var sdlInst *SdlMock |
| 38 | |
| 39 | func TestMain(m *testing.M) { |
| 40 | sdlInst = new(SdlMock) |
subhash kumar singh | 6017d6a | 2021-10-26 12:14:26 +0000 | [diff] [blame] | 41 | |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 42 | sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos", |
naman.gupta | 05a0f58 | 2022-05-05 15:59:51 +0530 | [diff] [blame] | 43 | "a1.policy_instance.20005.123456", |
| 44 | "a1.policy_instance.20005.234567", |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 45 | "a1.policy_type.1006001", |
| 46 | "a1.policy_type.20000", |
| 47 | "a1.policy_inst_metadata.1006001.qos", |
| 48 | }, nil) |
subhash kumar singh | 6017d6a | 2021-10-26 12:14:26 +0000 | [diff] [blame] | 49 | |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 50 | a1.Init() |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 51 | rh = createResthook(sdlInst) |
| 52 | code := m.Run() |
| 53 | os.Exit(code) |
| 54 | } |
| 55 | |
| 56 | func TestGetAllPolicyType(t *testing.T) { |
| 57 | resp := rh.GetAllPolicyType() |
| 58 | assert.Equal(t, 2, len(resp)) |
| 59 | } |
| 60 | |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 61 | func TestGetPolicyType(t *testing.T) { |
| 62 | |
| 63 | policyTypeId := models.PolicyTypeID(20001) |
| 64 | |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 65 | var policyTypeSchema models.PolicyTypeSchema |
| 66 | name := "admission_control_policy_mine" |
| 67 | policyTypeSchema.Name = &name |
| 68 | policytypeid := int64(20001) |
| 69 | policyTypeSchema.PolicyTypeID = &policytypeid |
| 70 | description := "various parameters to control admission of dual connection" |
| 71 | policyTypeSchema.Description = &description |
| 72 | 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)",}, |
| 73 | "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}` |
| 74 | policyTypeSchema.CreateSchema = schema |
| 75 | key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10) |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 76 | var keys [1]string |
| 77 | keys[0] = key |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 78 | //Setup Expectations |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 79 | sdlInst.On("Get", a1MediatorNs, keys[:]).Return(map[string]interface{}{key: policyTypeSchema}, nil) |
| 80 | resp := rh.GetPolicyType(policyTypeId) |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 81 | assert.NotNil(t, resp) |
| 82 | |
| 83 | sdlInst.AssertExpectations(t) |
| 84 | |
| 85 | } |
| 86 | |
naman.gupta | d5aa5be | 2022-02-23 11:44:43 +0530 | [diff] [blame] | 87 | func TestCreatePolicyType(t *testing.T) { |
| 88 | var policyTypeId models.PolicyTypeID |
| 89 | policyTypeId = 20001 |
| 90 | var policyTypeSchema models.PolicyTypeSchema |
| 91 | name := "admission_control_policy_mine" |
| 92 | policyTypeSchema.Name = &name |
| 93 | policytypeid := int64(20001) |
| 94 | policyTypeSchema.PolicyTypeID = &policytypeid |
| 95 | description := "various parameters to control admission of dual connection" |
| 96 | policyTypeSchema.Description = &description |
| 97 | 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)",}, |
| 98 | "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}` |
| 99 | |
| 100 | data, err := policyTypeSchema.MarshalBinary() |
| 101 | a1.Logger.Debug("error : %+v ", err) |
| 102 | a1.Logger.Debug("data : %+v ", data) |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 103 | key := a1PolicyPrefix + strconv.FormatInt(20001, 10) |
naman.gupta | d5aa5be | 2022-02-23 11:44:43 +0530 | [diff] [blame] | 104 | a1.Logger.Debug("key : %+v ", key) |
| 105 | //Setup Expectations |
| 106 | sdlInst.On("SetIfNotExists", a1MediatorNs, key, string(data)).Return(true, nil) |
| 107 | |
| 108 | errresp := rh.CreatePolicyType(policyTypeId, policyTypeSchema) |
| 109 | //Data Assertion |
| 110 | assert.Nil(t, errresp) |
| 111 | //Mock Assertion :Behavioral |
| 112 | sdlInst.AssertExpectations(t) |
| 113 | } |
| 114 | |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 115 | func TestCreatePolicyTypeInstance(t *testing.T) { |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 116 | var policyInstanceID models.PolicyInstanceID |
| 117 | policyInstanceID = "123456" |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 118 | var httpBody = `{"enforce":true,"window_length":20,"blocking_rate":20,"trigger_threshold":10}` |
| 119 | instancekey := a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) |
| 120 | var policyTypeId models.PolicyTypeID |
| 121 | policyTypeId = 20001 |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 122 | |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 123 | var instancedata map[string]interface{} |
| 124 | |
| 125 | json.Unmarshal([]byte(httpBody), &instancedata) |
| 126 | |
| 127 | data, _ := json.Marshal(instancedata) |
| 128 | a1.Logger.Debug("Marshaled data : %+v", string(data)) |
| 129 | a1.Logger.Debug("instancekey : %+v", instancekey) |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 130 | instancearr := []interface{}{instancekey, string(data)} |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 131 | sdlInst.On("Set", "A1m_ns", instancearr).Return(nil) |
| 132 | |
| 133 | metadatainstancekey := a1InstanceMetadataPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) |
| 134 | creation_timestamp := time.Now() |
| 135 | var metadatajson []interface{} |
| 136 | metadatajson = append(metadatajson, map[string]string{"created_at": creation_timestamp.Format("2006-01-02 15:04:05"), "has_been_deleted": "False"}) |
| 137 | metadata, _ := json.Marshal(metadatajson) |
| 138 | a1.Logger.Debug("Marshaled Metadata : %+v", string(metadata)) |
| 139 | a1.Logger.Debug("metadatainstancekey : %+v", metadatainstancekey) |
| 140 | metadatainstancearr := []interface{}{metadatainstancekey, string(metadata)} |
| 141 | sdlInst.On("Set", "A1m_ns", metadatainstancearr).Return(nil) |
| 142 | |
| 143 | errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, instancedata) |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 144 | |
| 145 | assert.Nil(t, errresp) |
| 146 | sdlInst.AssertExpectations(t) |
| 147 | } |
| 148 | |
naman.gupta | 6579f78 | 2022-05-02 15:34:31 +0530 | [diff] [blame] | 149 | func TestGetPolicyInstance(t *testing.T) { |
| 150 | |
| 151 | var policyTypeId models.PolicyTypeID |
| 152 | policyTypeId = 20001 |
| 153 | var policyInstanceID models.PolicyInstanceID |
| 154 | policyInstanceID = "123456" |
| 155 | httpBody := `{ |
| 156 | "enforce":true, |
| 157 | "window_length":20, |
| 158 | "blocking_rate":20, |
| 159 | "trigger_threshold":10 |
| 160 | }` |
naman.gupta | 6242a3f | 2022-10-03 16:05:10 +0530 | [diff] [blame^] | 161 | instancekey := a1InstancePrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID) |
naman.gupta | 6579f78 | 2022-05-02 15:34:31 +0530 | [diff] [blame] | 162 | a1.Logger.Debug("httpBody String : %+v", httpBody) |
| 163 | a1.Logger.Debug("key : %+v", instancekey) |
| 164 | var keys [1]string |
| 165 | keys[0] = instancekey |
| 166 | //Setup Expectations |
| 167 | sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody, nil) |
| 168 | |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 169 | resp, err := rh.GetPolicyInstance(policyTypeId, policyInstanceID) |
| 170 | a1.Logger.Error("err : %+v", err) |
naman.gupta | 6579f78 | 2022-05-02 15:34:31 +0530 | [diff] [blame] | 171 | assert.NotNil(t, resp) |
| 172 | |
| 173 | sdlInst.AssertExpectations(t) |
| 174 | } |
| 175 | |
naman.gupta | 05a0f58 | 2022-05-05 15:59:51 +0530 | [diff] [blame] | 176 | func TestGetAllPolicyIntances(t *testing.T) { |
| 177 | var policyTypeId models.PolicyTypeID |
| 178 | policyTypeId = 20005 |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 179 | resp, err := rh.GetAllPolicyInstance(policyTypeId) |
| 180 | a1.Logger.Error("err : %+v", err) |
naman.gupta | 05a0f58 | 2022-05-05 15:59:51 +0530 | [diff] [blame] | 181 | assert.Equal(t, 2, len(resp)) |
| 182 | } |
| 183 | |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 184 | type SdlMock struct { |
| 185 | mock.Mock |
| 186 | } |
| 187 | |
| 188 | func (s *SdlMock) GetAll(ns string) ([]string, error) { |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 189 | args := s.MethodCalled("GetAll", ns) |
| 190 | return args.Get(0).([]string), nil |
| 191 | } |
| 192 | |
naman.gupta | 6864473 | 2022-02-24 14:41:55 +0530 | [diff] [blame] | 193 | func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) { |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 194 | a1.Logger.Debug("Get Called ") |
| 195 | args := s.MethodCalled("Get", ns, keys) |
| 196 | a1.Logger.Debug("keys :%+v", args.Get(1)) |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 197 | policytypeid := int64(20001) |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 198 | policyInstanceID := "123456" |
| 199 | var policySchemaString string |
| 200 | var key string |
| 201 | if keys[0] == "a1.policy_instance.20001.123456" { |
| 202 | policySchemaString = `{ |
| 203 | "enforce":true, |
| 204 | "window_length":20, |
| 205 | "blocking_rate":20, |
| 206 | "trigger_threshold":10 |
| 207 | }` |
| 208 | key = a1InstancePrefix + strconv.FormatInt(policytypeid, 10) + "." + string(policyInstanceID) |
| 209 | } else if keys[0] == "a1.policy_type.20001" { |
naman.gupta | 6242a3f | 2022-10-03 16:05:10 +0530 | [diff] [blame^] | 210 | 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}` |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 211 | key = a1PolicyPrefix + strconv.FormatInt((policytypeid), 10) |
| 212 | } |
| 213 | a1.Logger.Error(" policy SchemaString %+v", policySchemaString) |
| 214 | policyTypeSchema, _ := json.Marshal((policySchemaString)) |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 215 | a1.Logger.Error(" policyTypeSchema %+v", string(policyTypeSchema)) |
naman.gupta | 6242a3f | 2022-10-03 16:05:10 +0530 | [diff] [blame^] | 216 | |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 217 | a1.Logger.Error(" key for policy type %+v", key) |
naman.gupta | 6242a3f | 2022-10-03 16:05:10 +0530 | [diff] [blame^] | 218 | mp := map[string]interface{}{key: string(policySchemaString)} |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 219 | a1.Logger.Error("Get Called and mp return %+v ", mp) |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 220 | return mp, nil |
naman.gupta | 6864473 | 2022-02-24 14:41:55 +0530 | [diff] [blame] | 221 | } |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 222 | |
| 223 | func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, error) { |
| 224 | args := s.MethodCalled("SetIfNotExists", ns, key, data) |
| 225 | return args.Bool(0), args.Error(1) |
| 226 | } |
| 227 | |
| 228 | func (s *SdlMock) Set(ns string, pairs ...interface{}) error { |
| 229 | args := s.MethodCalled("Set", ns, pairs) |
Naman Gupta/Single vRAN Call Control /SRI-Bangalore/Engineer/Samsung Electronics | 584d755 | 2022-09-27 10:32:15 +0530 | [diff] [blame] | 230 | return args.Error(0) |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame] | 231 | } |
| 232 | func (s *SdlMock) SetIf(ns string, key string, oldData, newData interface{}) (bool, error) { |
| 233 | args := s.MethodCalled("SetIfNotExists", ns, key, oldData, newData) |
| 234 | return args.Bool(0), args.Error(1) |
| 235 | } |