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" |
| 28 | |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 29 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" |
naman.gupta | d5aa5be | 2022-02-23 11:44:43 +0530 | [diff] [blame] | 30 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models" |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 31 | "github.com/stretchr/testify/assert" |
| 32 | "github.com/stretchr/testify/mock" |
| 33 | ) |
| 34 | |
| 35 | var rh *Resthook |
| 36 | var sdlInst *SdlMock |
| 37 | |
| 38 | func TestMain(m *testing.M) { |
| 39 | sdlInst = new(SdlMock) |
subhash kumar singh | 6017d6a | 2021-10-26 12:14:26 +0000 | [diff] [blame] | 40 | |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 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", |
| 45 | }, nil) |
subhash kumar singh | 6017d6a | 2021-10-26 12:14:26 +0000 | [diff] [blame] | 46 | |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 47 | a1.Init() |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 48 | rh = createResthook(sdlInst) |
| 49 | code := m.Run() |
| 50 | os.Exit(code) |
| 51 | } |
| 52 | |
| 53 | func TestGetAllPolicyType(t *testing.T) { |
| 54 | resp := rh.GetAllPolicyType() |
| 55 | assert.Equal(t, 2, len(resp)) |
| 56 | } |
| 57 | |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 58 | func TestGetPolicyType(t *testing.T) { |
| 59 | |
| 60 | policyTypeId := models.PolicyTypeID(20001) |
| 61 | |
| 62 | resp := rh.GetPolicyType(policyTypeId) |
| 63 | |
| 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) |
| 75 | |
| 76 | //Setup Expectations |
| 77 | sdlInst.On("Get", a1MediatorNs, policyTypeId).Return(map[string]interface{}{key: policyTypeSchema}, nil) |
| 78 | |
| 79 | assert.NotNil(t, resp) |
| 80 | |
| 81 | sdlInst.AssertExpectations(t) |
| 82 | |
| 83 | } |
| 84 | |
naman.gupta | d5aa5be | 2022-02-23 11:44:43 +0530 | [diff] [blame] | 85 | func TestCreatePolicyType(t *testing.T) { |
| 86 | var policyTypeId models.PolicyTypeID |
| 87 | policyTypeId = 20001 |
| 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,},}` |
| 97 | |
| 98 | data, err := policyTypeSchema.MarshalBinary() |
| 99 | a1.Logger.Debug("error : %+v ", err) |
| 100 | a1.Logger.Debug("data : %+v ", data) |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 101 | key := a1PolicyPrefix + strconv.FormatInt(20001, 10) |
naman.gupta | d5aa5be | 2022-02-23 11:44:43 +0530 | [diff] [blame] | 102 | a1.Logger.Debug("key : %+v ", key) |
| 103 | //Setup Expectations |
| 104 | sdlInst.On("SetIfNotExists", a1MediatorNs, key, string(data)).Return(true, nil) |
| 105 | |
| 106 | errresp := rh.CreatePolicyType(policyTypeId, policyTypeSchema) |
| 107 | //Data Assertion |
| 108 | assert.Nil(t, errresp) |
| 109 | //Mock Assertion :Behavioral |
| 110 | sdlInst.AssertExpectations(t) |
| 111 | } |
| 112 | |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame^] | 113 | func TestCreatePolicyTypeInstance(t *testing.T) { |
| 114 | var policyTypeId models.PolicyTypeID |
| 115 | policyTypeId = 20001 |
| 116 | var policyInstanceID models.PolicyInstanceID |
| 117 | policyInstanceID = "123456" |
| 118 | httpBody := `{ |
| 119 | "enforce":true, |
| 120 | "window_length":20, |
| 121 | "blocking_rate":20, |
| 122 | "trigger_threshold":10 |
| 123 | }` |
| 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) |
| 128 | |
| 129 | instancearr := []interface{}{instancekey, string(data)} |
| 130 | sdlInst.On("Set", "A1m_ns", instancearr).Return("CREATE", nil) |
| 131 | errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, httpBody) |
| 132 | |
| 133 | assert.Nil(t, errresp) |
| 134 | sdlInst.AssertExpectations(t) |
| 135 | } |
| 136 | |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 137 | type SdlMock struct { |
| 138 | mock.Mock |
| 139 | } |
| 140 | |
| 141 | func (s *SdlMock) GetAll(ns string) ([]string, error) { |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 142 | args := s.MethodCalled("GetAll", ns) |
| 143 | return args.Get(0).([]string), nil |
| 144 | } |
| 145 | |
naman.gupta | 6864473 | 2022-02-24 14:41:55 +0530 | [diff] [blame] | 146 | func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) { |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 147 | a1.Logger.Debug("Get Called ") |
| 148 | args := s.MethodCalled("Get", ns, keys) |
| 149 | a1.Logger.Debug("keys :%+v", args.Get(1)) |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 150 | policytypeid := int64(20001) |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame^] | 151 | |
| 152 | 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}}}` |
| 153 | |
| 154 | a1.Logger.Error(" policyTypeSchemaString %+v", policyTypeSchemaString) |
| 155 | policyTypeSchema, _ := json.Marshal((policyTypeSchemaString)) |
| 156 | // a1.Logger.Error(" policyTypeSchema error %+v", err) |
| 157 | a1.Logger.Error(" policyTypeSchema %+v", string(policyTypeSchema)) |
| 158 | var p models.PolicyTypeSchema |
| 159 | _ = json.Unmarshal([]byte(string(policyTypeSchemaString)), &p) |
| 160 | a1.Logger.Error("unmarshalled policyTypeSchema %+v", p.CreateSchema) |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 161 | key := a1PolicyPrefix + strconv.FormatInt((policytypeid), 10) |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame^] | 162 | a1.Logger.Error(" key for policy type %+v", key) |
| 163 | mp := map[string]interface{}{key: string(policyTypeSchema)} |
| 164 | a1.Logger.Error("Get Called and mp return %+v ", mp) |
naman.gupta | b5c9dd5 | 2022-03-10 15:12:05 +0530 | [diff] [blame] | 165 | return mp, nil |
naman.gupta | 6864473 | 2022-02-24 14:41:55 +0530 | [diff] [blame] | 166 | } |
naman.gupta | 3ad6de4 | 2022-04-26 21:59:05 +0530 | [diff] [blame^] | 167 | |
| 168 | func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, error) { |
| 169 | args := s.MethodCalled("SetIfNotExists", ns, key, data) |
| 170 | return args.Bool(0), args.Error(1) |
| 171 | } |
| 172 | |
| 173 | func (s *SdlMock) Set(ns string, pairs ...interface{}) error { |
| 174 | args := s.MethodCalled("Set", ns, pairs) |
| 175 | return args.Error(1) |
| 176 | } |
| 177 | func (s *SdlMock) SetIf(ns string, key string, oldData, newData interface{}) (bool, error) { |
| 178 | args := s.MethodCalled("SetIfNotExists", ns, key, oldData, newData) |
| 179 | return args.Bool(0), args.Error(1) |
| 180 | } |