Comment RMR unit tests
[ric-plt/resource-status-manager.git] / RSM / rmrcgo / rmr_c_go_api_test.go
1 //
2 // Copyright 2019 AT&T Intellectual Property
3 // Copyright 2019 Nokia
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 rmrcgo
22
23 import (
24         "bytes"
25         "encoding/json"
26         "github.com/stretchr/testify/assert"
27         "io/ioutil"
28         "rsm/logger"
29         "rsm/tests"
30         "testing"
31 )
32
33 var (
34         msgr RmrMessenger
35 )
36
37 func TestLogger(t *testing.T) {
38         log := initLog(t)
39         data := map[string]interface{}{"messageType": 1001, "ranIp": "10.0.0.3", "ranPort": 879, "ranName": "test1"}
40         b := new(bytes.Buffer)
41         _ = json.NewEncoder(b).Encode(data)
42         req := tests.GetHttpRequest()
43         boo, _ := ioutil.ReadAll(req.Body)
44         log.Debugf("#rmr_c_go_api_test.TestLogger - request header: %v\n; request body: %s\n", req.Header, string(boo))
45 }
46
47 func TestNewMBufSuccess(t *testing.T) {
48         msg := NewMBuf(tests.MessageType, len(tests.DummyPayload), "RanName", &tests.DummyPayload, &tests.DummyXAction)
49         assert.NotNil(t, msg)
50         assert.NotEmpty(t, msg.Payload)
51         assert.NotEmpty(t, msg.XAction)
52         assert.Equal(t, msg.MType, tests.MessageType)
53         assert.Equal(t, msg.Meid, "RanName")
54         assert.Equal(t, msg.Len, len(tests.DummyPayload))
55 }
56
57 /*func TestSendRecvMsgSuccess(t *testing.T) {
58         log := initLog(t)
59
60         initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
61         if msgr == nil || !msgr.IsReady() {
62                 t.Errorf("#rmr_c_go_api_test.TestSendRecvMsgSuccess - The rmr router is not ready")
63         }
64
65         msg := NewMBuf(1, tests.MaxMsgSize, "test 1", &tests.DummyPayload, &tests.DummyXAction)
66         log.Debugf("#rmr_c_go_api_test.TestSendRecvMsgSuccess - Going to send the message: %#v\n", msg)
67         result, err := msgr.SendMsg(msg)
68
69         assert.Nil(t, err)
70         assert.NotNil(t, result)
71
72         msgR, err := msgr.RecvMsg()
73
74         assert.Nil(t, err)
75         assert.NotNil(t, msgR)
76         msgr.Close()
77 }
78
79 func TestSendMsgRmrInvalidPortError(t *testing.T) {
80         log := initLog(t)
81
82         initRmr(tests.ReadyIntervalSec, "tcp:"+strconv.Itoa(5555), tests.MaxMsgSize, tests.Flags, log)
83         if msgr == nil || !msgr.IsReady() {
84                 t.Errorf("#rmr_c_go_api_test.TestSendMsgRmrInvalidPortError - The rmr router is not ready")
85         }
86
87         msg := NewMBuf(1, tests.MaxMsgSize, "test 1", &tests.DummyPayload, &tests.DummyXAction)
88         log.Debugf("#rmr_c_go_api_test.TestSendMsgRmrInvalidPortError - Going to send the message: %#v\n", msg)
89         result, err := msgr.SendMsg(msg)
90
91         assert.NotNil(t, err)
92         assert.Nil(t, result)
93
94         msgr.Close()
95 }
96
97 func TestSendMsgRmrInvalidMsgNumError(t *testing.T) {
98         log := initLog(t)
99
100         initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
101         if msgr == nil || !msgr.IsReady() {
102                 t.Errorf("#rmr_c_go_api_test.TestSendMsgRmrInvalidMsgNumError - The rmr router is not ready")
103         }
104
105         msg := NewMBuf(10, tests.MaxMsgSize, "test 1", &tests.DummyPayload, &tests.DummyXAction)
106         log.Debugf("#rmr_c_go_api_test.TestSendMsgRmrInvalidMsgNumError - Going to send the message: %#v\n", msg)
107         result, err := msgr.SendMsg(msg)
108
109         assert.NotNil(t, err)
110         assert.Nil(t, result)
111
112         msgr.Close()
113 }
114
115 func TestIsReadySuccess(t *testing.T) {
116         log := initLog(t)
117
118         initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
119         if msgr == nil || !msgr.IsReady() {
120                 t.Errorf("#rmr_c_go_api_test.TestIsReadySuccess - The rmr router is not ready")
121         }
122
123         msgr.Close()
124 }
125
126 func initRmr(readyIntervalSec int, port string, maxMsgSize int, flags int, log *logger.Logger) {
127         var ctx *Context
128         msgr = ctx.Init(readyIntervalSec, port, maxMsgSize, flags, log)
129 }
130 */
131
132 func initLog(t *testing.T) *logger.Logger {
133         log, err := logger.InitLogger(logger.DebugLevel)
134         if err != nil {
135                 t.Errorf("#rmr_c_go_api_test.initLog - failed to initialize logger, error: %s", err)
136         }
137         return log
138 }