55e22c392c3fe5ce4bea816c836ac5b2f7dcce76
[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         "os"
25         "strconv"
26         "testing"
27
28         "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/a1"
29         "gerrit.o-ran-sc.org/r/ric-plt/a1/pkg/models"
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)
39
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)
45
46         a1.Init()
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
57 func TestGetPolicyType(t *testing.T) {
58
59         policyTypeId := models.PolicyTypeID(20001)
60
61         resp := rh.GetPolicyType(policyTypeId)
62
63         var policyTypeSchema models.PolicyTypeSchema
64         name := "admission_control_policy_mine"
65         policyTypeSchema.Name = &name
66         policytypeid := int64(20001)
67         policyTypeSchema.PolicyTypeID = &policytypeid
68         description := "various parameters to control admission of dual connection"
69         policyTypeSchema.Description = &description
70         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)",},
71 "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}`
72         policyTypeSchema.CreateSchema = schema
73         key := a1PolicyPrefix + strconv.FormatInt((int64(policyTypeId)), 10)
74
75         //Setup Expectations
76         sdlInst.On("Get", a1MediatorNs, policyTypeId).Return(map[string]interface{}{key: policyTypeSchema}, nil)
77
78         assert.NotNil(t, resp)
79
80         sdlInst.AssertExpectations(t)
81
82 }
83
84 func TestCreatePolicyType(t *testing.T) {
85         var policyTypeId models.PolicyTypeID
86         policyTypeId = 20001
87         var policyTypeSchema models.PolicyTypeSchema
88         name := "admission_control_policy_mine"
89         policyTypeSchema.Name = &name
90         policytypeid := int64(20001)
91         policyTypeSchema.PolicyTypeID = &policytypeid
92         description := "various parameters to control admission of dual connection"
93         policyTypeSchema.Description = &description
94         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)",},
95 "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}`
96
97         data, err := policyTypeSchema.MarshalBinary()
98         a1.Logger.Debug("error : %+v ", err)
99         a1.Logger.Debug("data : %+v ", data)
100         key := a1PolicyPrefix + strconv.FormatInt(20001, 10)
101         a1.Logger.Debug("key : %+v ", key)
102         //Setup Expectations
103         sdlInst.On("SetIfNotExists", a1MediatorNs, key, string(data)).Return(true, nil)
104
105         errresp := rh.CreatePolicyType(policyTypeId, policyTypeSchema)
106         //Data Assertion
107         assert.Nil(t, errresp)
108         //Mock Assertion :Behavioral
109         sdlInst.AssertExpectations(t)
110 }
111
112 type SdlMock struct {
113         mock.Mock
114 }
115
116 func (s *SdlMock) GetAll(ns string) ([]string, error) {
117         args := s.MethodCalled("GetAll", ns)
118         return args.Get(0).([]string), nil
119 }
120
121 func (s *SdlMock) SetIfNotExists(ns string, key string, data interface{}) (bool, error) {
122         a1.Logger.Debug("SetIfNotExists mock called")
123         args := s.MethodCalled("SetIfNotExists", ns, key, data)
124         return args.Bool(0), nil
125 }
126
127 func (s *SdlMock) Get(ns string, keys []string) (map[string]interface{}, error) {
128         a1.Logger.Debug("Get Called ")
129         args := s.MethodCalled("Get", ns, keys)
130         a1.Logger.Debug("keys :%+v", args.Get(1))
131         var policyTypeSchema models.PolicyTypeSchema
132         name := "admission_control_policy_mine"
133         policyTypeSchema.Name = &name
134         policytypeid := int64(20001)
135         policyTypeSchema.PolicyTypeID = &policytypeid
136         description := "various parameters to control admission of dual connection"
137         policyTypeSchema.Description = &description
138         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)",},
139 "blocking_rate": {"type":"number","default":10,"minimum":1,"maximum":100,"description": "% Connections to block",},"additionalProperties": false,},}`
140         key := a1PolicyPrefix + strconv.FormatInt((policytypeid), 10)
141         mp := map[string]interface{}{key: policyTypeSchema}
142         a1.Logger.Debug("Get Called and mp return %+v ", mp)
143         return mp, nil
144 }