50a7fe27ebe5a7e443898c257ea52e5bbab15f5e
[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_type.1006001",
43                 "a1.policy_type.20000",
44                 "a1.policy_inst_metadata.1006001.qos",
45         }, nil)
46
47         a1.Init()
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
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
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)
101         key := a1PolicyPrefix + strconv.FormatInt(20001, 10)
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
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
137 func TestGetPolicyInstance(t *testing.T) {
138
139         var policyTypeId models.PolicyTypeID
140         policyTypeId = 20001
141         var policyInstanceID models.PolicyInstanceID
142         policyInstanceID = "123456"
143         httpBody := `{
144                 "enforce":true,
145                 "window_length":20,
146            "blocking_rate":20,
147                 "trigger_threshold":10
148                 }`
149         instancekey := a1PolicyPrefix + strconv.FormatInt(20001, 10) + "." + string(policyInstanceID)
150         a1.Logger.Debug("httpBody String : %+v", httpBody)
151         a1.Logger.Debug("key   : %+v", instancekey)
152         var keys [1]string
153         keys[0] = instancekey
154         //Setup Expectations
155         sdlInst.On("Get", a1MediatorNs, keys[:]).Return(httpBody, nil)
156
157         resp := rh.GetPolicyInstance(policyTypeId, policyInstanceID)
158         a1.Logger.Error("resp : %+v", resp)
159         assert.NotNil(t, resp)
160
161         sdlInst.AssertExpectations(t)
162 }
163
164 type SdlMock struct {
165         mock.Mock
166 }
167
168 func (s *SdlMock) GetAll(ns string) ([]string, error) {
169         args := s.MethodCalled("GetAll", ns)
170         return args.Get(0).([]string), nil
171 }
172
173 func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) {
174         a1.Logger.Debug("Get Called ")
175         args := s.MethodCalled("Get", ns, keys)
176         a1.Logger.Debug("keys :%+v", args.Get(1))
177         policytypeid := int64(20001)
178
179         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}}}`
180
181         a1.Logger.Error(" policyTypeSchemaString %+v", policyTypeSchemaString)
182         policyTypeSchema, _ := json.Marshal((policyTypeSchemaString))
183         // a1.Logger.Error(" policyTypeSchema error %+v",  err)
184         a1.Logger.Error(" policyTypeSchema %+v", string(policyTypeSchema))
185         var p models.PolicyTypeSchema
186         _ = json.Unmarshal([]byte(string(policyTypeSchemaString)), &p)
187         a1.Logger.Error("unmarshalled  policyTypeSchema %+v", p.CreateSchema)
188         key := a1PolicyPrefix + strconv.FormatInt((policytypeid), 10)
189         a1.Logger.Error(" key for policy type %+v", key)
190         mp := map[string]interface{}{key: string(policyTypeSchema)}
191         a1.Logger.Error("Get Called and mp return %+v ", mp)
192         return mp, nil
193 }
194
195 func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, error) {
196         args := s.MethodCalled("SetIfNotExists", ns, key, data)
197         return args.Bool(0), args.Error(1)
198 }
199
200 func (s *SdlMock) Set(ns string, pairs ...interface{}) error {
201         args := s.MethodCalled("Set", ns, pairs)
202         return args.Error(1)
203 }
204 func (s *SdlMock) SetIf(ns string, key string, oldData, newData interface{}) (bool, error) {
205         args := s.MethodCalled("SetIfNotExists", ns, key, oldData, newData)
206         return args.Bool(0), args.Error(1)
207 }