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 ( |
| 24 | "os" |
naman.gupta | d5aa5be | 2022-02-23 11:44:43 +0530 | [diff] [blame] | 25 | "strconv" |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 26 | "testing" |
| 27 | |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 28 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1" |
naman.gupta | d5aa5be | 2022-02-23 11:44:43 +0530 | [diff] [blame] | 29 | "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models" |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 30 | "github.com/stretchr/testify/assert" |
| 31 | "github.com/stretchr/testify/mock" |
| 32 | ) |
| 33 | |
| 34 | var rh *Resthook |
| 35 | var sdlInst *SdlMock |
| 36 | |
| 37 | func TestMain(m *testing.M) { |
| 38 | sdlInst = new(SdlMock) |
subhash kumar singh | 6017d6a | 2021-10-26 12:14:26 +0000 | [diff] [blame] | 39 | |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 40 | sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos", |
| 41 | "a1.policy_type.1006001", |
| 42 | "a1.policy_type.20000", |
| 43 | "a1.policy_inst_metadata.1006001.qos", |
| 44 | }, nil) |
subhash kumar singh | 6017d6a | 2021-10-26 12:14:26 +0000 | [diff] [blame] | 45 | |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 46 | a1.Init() |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 47 | rh = createResthook(sdlInst) |
| 48 | code := m.Run() |
| 49 | os.Exit(code) |
| 50 | } |
| 51 | |
| 52 | func TestGetAllPolicyType(t *testing.T) { |
| 53 | resp := rh.GetAllPolicyType() |
| 54 | assert.Equal(t, 2, len(resp)) |
| 55 | } |
| 56 | |
naman.gupta | d5aa5be | 2022-02-23 11:44:43 +0530 | [diff] [blame] | 57 | func TestCreatePolicyType(t *testing.T) { |
| 58 | var policyTypeId models.PolicyTypeID |
| 59 | policyTypeId = 20001 |
| 60 | var policyTypeSchema models.PolicyTypeSchema |
| 61 | name := "admission_control_policy_mine" |
| 62 | policyTypeSchema.Name = &name |
| 63 | policytypeid := int64(20001) |
| 64 | policyTypeSchema.PolicyTypeID = &policytypeid |
| 65 | description := "various parameters to control admission of dual connection" |
| 66 | policyTypeSchema.Description = &description |
| 67 | 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)",}, |
| 68 | "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}` |
| 69 | |
| 70 | data, err := policyTypeSchema.MarshalBinary() |
| 71 | a1.Logger.Debug("error : %+v ", err) |
| 72 | a1.Logger.Debug("data : %+v ", data) |
| 73 | key := "a1.policy_type." + strconv.FormatInt(20001, 10) |
| 74 | a1.Logger.Debug("key : %+v ", key) |
| 75 | //Setup Expectations |
| 76 | sdlInst.On("SetIfNotExists", a1MediatorNs, key, string(data)).Return(true, nil) |
| 77 | |
| 78 | errresp := rh.CreatePolicyType(policyTypeId, policyTypeSchema) |
| 79 | //Data Assertion |
| 80 | assert.Nil(t, errresp) |
| 81 | //Mock Assertion :Behavioral |
| 82 | sdlInst.AssertExpectations(t) |
| 83 | } |
| 84 | |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 85 | type SdlMock struct { |
| 86 | mock.Mock |
| 87 | } |
| 88 | |
| 89 | func (s *SdlMock) GetAll(ns string) ([]string, error) { |
naman.gupta | ab3e9ee | 2022-02-18 18:03:36 +0530 | [diff] [blame] | 90 | args := s.MethodCalled("GetAll", ns) |
| 91 | return args.Get(0).([]string), nil |
| 92 | } |
| 93 | |
| 94 | func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, error) { |
naman.gupta | d5aa5be | 2022-02-23 11:44:43 +0530 | [diff] [blame] | 95 | a1.Logger.Debug("SetIfNotExists mock called") |
| 96 | args := s.MethodCalled("SetIfNotExists", ns, key, data) |
| 97 | return args.Bool(0), nil |
subhash kumar singh | 327d9db | 2021-09-30 19:07:18 +0000 | [diff] [blame] | 98 | } |
naman.gupta | 6864473 | 2022-02-24 14:41:55 +0530 | [diff] [blame^] | 99 | |
| 100 | func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) { |
| 101 | return make(map[string]interface{}), nil |
| 102 | } |