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 ==================================================================================
29 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
30 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models"
31 "github.com/stretchr/testify/assert"
32 "github.com/stretchr/testify/mock"
38 func TestMain(m *testing.M) {
39 sdlInst = new(SdlMock)
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",
48 rh = createResthook(sdlInst)
53 func TestGetAllPolicyType(t *testing.T) {
54 resp := rh.GetAllPolicyType()
55 assert.Equal(t, 2, len(resp))
58 func TestGetPolicyType(t *testing.T) {
60 policyTypeId := models.PolicyTypeID(20001)
62 resp := rh.GetPolicyType(policyTypeId)
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)
77 sdlInst.On("Get", a1MediatorNs, policyTypeId).Return(map[string]interface{}{key: policyTypeSchema}, nil)
79 assert.NotNil(t, resp)
81 sdlInst.AssertExpectations(t)
85 func TestCreatePolicyType(t *testing.T) {
86 var policyTypeId models.PolicyTypeID
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,},}`
98 data, err := policyTypeSchema.MarshalBinary()
99 a1.Logger.Debug("error : %+v ", err)
100 a1.Logger.Debug("data : %+v ", data)
101 key := a1PolicyPrefix + strconv.FormatInt(20001, 10)
102 a1.Logger.Debug("key : %+v ", key)
104 sdlInst.On("SetIfNotExists", a1MediatorNs, key, string(data)).Return(true, nil)
106 errresp := rh.CreatePolicyType(policyTypeId, policyTypeSchema)
108 assert.Nil(t, errresp)
109 //Mock Assertion :Behavioral
110 sdlInst.AssertExpectations(t)
113 func TestCreatePolicyTypeInstance(t *testing.T) {
114 var policyTypeId models.PolicyTypeID
116 var policyInstanceID models.PolicyInstanceID
117 policyInstanceID = "123456"
122 "trigger_threshold":10
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)
129 instancearr := []interface{}{instancekey, string(data)}
130 sdlInst.On("Set", "A1m_ns", instancearr).Return("CREATE", nil)
131 errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, httpBody)
133 assert.Nil(t, errresp)
134 sdlInst.AssertExpectations(t)
137 type SdlMock struct {
141 func (s *SdlMock) GetAll(ns string) ([]string, error) {
142 args := s.MethodCalled("GetAll", ns)
143 return args.Get(0).([]string), nil
146 func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) {
147 a1.Logger.Debug("Get Called ")
148 args := s.MethodCalled("Get", ns, keys)
149 a1.Logger.Debug("keys :%+v", args.Get(1))
150 policytypeid := int64(20001)
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}}}`
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)
161 key := a1PolicyPrefix + strconv.FormatInt((policytypeid), 10)
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)
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)
173 func (s *SdlMock) Set(ns string, pairs ...interface{}) error {
174 args := s.MethodCalled("Set", ns, pairs)
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)