98d4f8c2e58f8da236c9b04d87662d1f3c8ad502
[ric-plt/a1.git] / a1-go / pkg / resthooks / resthooks_test.go
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         "encoding/json"
25         "os"
26         "strconv"
27         "testing"
28
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"
33 )
34
35 var rh *Resthook
36 var sdlInst *SdlMock
37
38 func TestMain(m *testing.M) {
39         sdlInst = new(SdlMock)
40
41         sdlInst.On("GetAll", "A1m_ns").Return([]string{"a1.policy_instance.1006001.qos",
42                 "a1.policy_instance.20005.123456",
43                 "a1.policy_instance.20005.234567",
44                 "a1.policy_type.1006001",
45                 "a1.policy_type.20000",
46                 "a1.policy_inst_metadata.1006001.qos",
47         }, nil)
48
49         a1.Init()
50         rh = createResthook(sdlInst)
51         code := m.Run()
52         os.Exit(code)
53 }
54
55 func TestGetAllPolicyType(t *testing.T) {
56         resp := rh.GetAllPolicyType()
57         assert.Equal(t, 2, len(resp))
58 }
59
60 func TestGetPolicyType(t *testing.T) {
61
62         policyTypeId := models.PolicyTypeID(20001)
63
64         resp := rh.GetPolicyType(policyTypeId)
65
66         var policyTypeSchema models.PolicyTypeSchema
67         name := "admission_control_policy_mine"
68         policyTypeSchema.Name = &name
69         policytypeid := int64(20001)
70         policyTypeSchema.PolicyTypeID = &policytypeid
71         description := "various parameters to control admission of dual connection"
72         policyTypeSchema.Description = &description
73         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)",},
74 "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}`
75         policyTypeSchema.CreateSchema = schema
76         key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
77
78         //Setup Expectations
79         sdlInst.On("Get", a1MediatorNs, policyTypeId).Return(map[string]interface{}{key: policyTypeSchema}, nil)
80
81         assert.NotNil(t, resp)
82
83         sdlInst.AssertExpectations(t)
84
85 }
86
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)
103         key := a1PolicyPrefix + strconv.FormatInt(20001, 10)
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
115 func TestCreatePolicyTypeInstance(t *testing.T) {
116         var policyTypeId models.PolicyTypeID
117         policyTypeId = 20001
118         var policyInstanceID models.PolicyInstanceID
119         policyInstanceID = "123456"
120         httpBody := `{
121                 "enforce":true,
122                 "window_length":20,
123            "blocking_rate":20,
124                 "trigger_threshold":10
125                 }`
126         instancekey := a1PolicyPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
127         data, _ := json.Marshal(httpBody)
128         a1.Logger.Debug("Marshaled String : %+v", string(data))
129         a1.Logger.Debug("key   : %+v", instancekey)
130
131         instancearr := []interface{}{instancekey, string(data)}
132         sdlInst.On("Set", "A1m_ns", instancearr).Return("CREATE", nil)
133         errresp := rh.CreatePolicyInstance(policyTypeId, policyInstanceID, httpBody)
134
135         assert.Nil(t, errresp)
136         sdlInst.AssertExpectations(t)
137 }
138
139 func TestGetPolicyInstance(t *testing.T) {
140
141         var policyTypeId models.PolicyTypeID
142         policyTypeId = 20001
143         var policyInstanceID models.PolicyInstanceID
144         policyInstanceID = "123456"
145         httpBody := `{
146                 "enforce":true,
147                 "window_length":20,
148            "blocking_rate":20,
149                 "trigger_threshold":10
150                 }`
151         instancekey := a1PolicyPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
152         a1.Logger.Debug("httpBody String : %+v", httpBody)
153         a1.Logger.Debug("key   : %+v", instancekey)
154         var keys [1]string
155         keys[0] = instancekey
156         //Setup Expectations
157         sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody, nil)
158
159         resp := rh.GetPolicyInstance(policyTypeId, policyInstanceID)
160         a1.Logger.Error("resp : %+v", resp)
161         assert.NotNil(t, resp)
162
163         sdlInst.AssertExpectations(t)
164 }
165
166 func TestGetAllPolicyIntances(t *testing.T) {
167         var policyTypeId models.PolicyTypeID
168         policyTypeId = 20005
169         resp := rh.GetAllPolicyInstance(policyTypeId)
170         assert.Equal(t, 2, len(resp))
171 }
172
173 type SdlMock struct {
174         mock.Mock
175 }
176
177 func (s *SdlMock) GetAll(ns string) ([]string, error) {
178         args := s.MethodCalled("GetAll", ns)
179         return args.Get(0).([]string), nil
180 }
181
182 func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) {
183         a1.Logger.Debug("Get Called ")
184         args := s.MethodCalled("Get", ns, keys)
185         a1.Logger.Debug("keys :%+v", args.Get(1))
186         policytypeid := int64(20001)
187
188         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}}}`
189
190         a1.Logger.Error(" policyTypeSchemaString %+v", policyTypeSchemaString)
191         policyTypeSchema, _ := json.Marshal((policyTypeSchemaString))
192         // a1.Logger.Error(" policyTypeSchema error %+v",  err)
193         a1.Logger.Error(" policyTypeSchema %+v", string(policyTypeSchema))
194         var p models.PolicyTypeSchema
195         _ = json.Unmarshal([]byte(string(policyTypeSchemaString)), &p)
196         a1.Logger.Error("unmarshalled  policyTypeSchema %+v", p.CreateSchema)
197         key := a1PolicyPrefix + strconv.FormatInt((policytypeid), 10)
198         a1.Logger.Error(" key for policy type %+v", key)
199         mp := map[string]interface{}{key: string(policyTypeSchema)}
200         a1.Logger.Error("Get Called and mp return %+v ", mp)
201         return mp, nil
202 }
203
204 func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, error) {
205         args := s.MethodCalled("SetIfNotExists", ns, key, data)
206         return args.Bool(0), args.Error(1)
207 }
208
209 func (s *SdlMock) Set(ns string, pairs ...interface{}) error {
210         args := s.MethodCalled("Set", ns, pairs)
211         return args.Error(1)
212 }
213 func (s *SdlMock) SetIf(ns string, key string, oldData, newData interface{}) (bool, error) {
214         args := s.MethodCalled("SetIfNotExists", ns, key, oldData, newData)
215         return args.Bool(0), args.Error(1)
216 }