blob: cef23ea4cabf908c1cfa7198b65ceae4b5f802db [file] [log] [blame]
subhash kumar singh327d9db2021-09-30 19:07:18 +00001/*
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*/
21package resthooks
22
23import (
naman.gupta3ad6de42022-04-26 21:59:05 +053024 "encoding/json"
subhash kumar singh327d9db2021-09-30 19:07:18 +000025 "os"
naman.guptad5aa5be2022-02-23 11:44:43 +053026 "strconv"
subhash kumar singh327d9db2021-09-30 19:07:18 +000027 "testing"
28
naman.guptaab3e9ee2022-02-18 18:03:36 +053029 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
naman.guptad5aa5be2022-02-23 11:44:43 +053030 "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models"
subhash kumar singh327d9db2021-09-30 19:07:18 +000031 "github.com/stretchr/testify/assert"
32 "github.com/stretchr/testify/mock"
33)
34
35var rh *Resthook
36var sdlInst *SdlMock
37
38func TestMain(m *testing.M) {
39 sdlInst = new(SdlMock)
subhash kumar singh6017d6a2021-10-26 12:14:26 +000040
naman.guptaab3e9ee2022-02-18 18:03:36 +053041 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 singh6017d6a2021-10-26 12:14:26 +000046
naman.guptaab3e9ee2022-02-18 18:03:36 +053047 a1.Init()
subhash kumar singh327d9db2021-09-30 19:07:18 +000048 rh = createResthook(sdlInst)
49 code := m.Run()
50 os.Exit(code)
51}
52
53func TestGetAllPolicyType(t *testing.T) {
54 resp := rh.GetAllPolicyType()
55 assert.Equal(t, 2, len(resp))
56}
57
naman.guptab5c9dd52022-03-10 15:12:05 +053058func 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.guptad5aa5be2022-02-23 11:44:43 +053085func 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.guptab5c9dd52022-03-10 15:12:05 +0530101 key := a1PolicyPrefix + strconv.FormatInt(20001, 10)
naman.guptad5aa5be2022-02-23 11:44:43 +0530102 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.gupta3ad6de42022-04-26 21:59:05 +0530113func 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 singh327d9db2021-09-30 19:07:18 +0000137type SdlMock struct {
138 mock.Mock
139}
140
141func (s *SdlMock) GetAll(ns string) ([]string, error) {
naman.guptaab3e9ee2022-02-18 18:03:36 +0530142 args := s.MethodCalled("GetAll", ns)
143 return args.Get(0).([]string), nil
144}
145
naman.gupta68644732022-02-24 14:41:55 +0530146func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) {
naman.guptab5c9dd52022-03-10 15:12:05 +0530147 a1.Logger.Debug("Get Called ")
148 args := s.MethodCalled("Get", ns, keys)
149 a1.Logger.Debug("keys :%+v", args.Get(1))
naman.guptab5c9dd52022-03-10 15:12:05 +0530150 policytypeid := int64(20001)
naman.gupta3ad6de42022-04-26 21:59:05 +0530151
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.guptab5c9dd52022-03-10 15:12:05 +0530161 key := a1PolicyPrefix + strconv.FormatInt((policytypeid), 10)
naman.gupta3ad6de42022-04-26 21:59:05 +0530162 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.guptab5c9dd52022-03-10 15:12:05 +0530165 return mp, nil
naman.gupta68644732022-02-24 14:41:55 +0530166}
naman.gupta3ad6de42022-04-26 21:59:05 +0530167
168func (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
173func (s *SdlMock) Set(ns string, pairs ...interface{}) error {
174 args := s.MethodCalled("Set", ns, pairs)
175 return args.Error(1)
176}
177func (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}