ef5938771cd2cd05b9396af884e1cb3b3fd8883d
[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         "strconv"
31         "testing"
32         "time"
33 )
34
35 var (
36         msgr RmrMessenger
37 )
38
39 func TestLogger(t *testing.T) {
40         log := initLog(t)
41         data := map[string]interface{}{"messageType": 1001, "ranIp": "10.0.0.3", "ranPort": 879, "ranName": "test1"}
42         b := new(bytes.Buffer)
43         _ = json.NewEncoder(b).Encode(data)
44         req := tests.GetHttpRequest()
45         boo, _ := ioutil.ReadAll(req.Body)
46         log.Debugf("#rmr_c_go_api_test.TestLogger - request header: %v\n; request body: %s\n", req.Header, string(boo))
47 }
48
49 func TestNewMBufSuccess(t *testing.T) {
50         msg := NewMBuf(tests.MessageType, len(tests.DummyPayload), "RanName", &tests.DummyPayload, &tests.DummyXAction)
51         assert.NotNil(t, msg)
52         assert.NotEmpty(t, msg.Payload)
53         assert.NotEmpty(t, msg.XAction)
54         assert.Equal(t, msg.MType, tests.MessageType)
55         assert.Equal(t, msg.Meid, "RanName")
56         assert.Equal(t, msg.Len, len(tests.DummyPayload))
57 }
58 //TODO check why test failure and TestIsReadySuccess success
59 func TestInitFailure(t *testing.T) {
60         log := initLog(t)
61         go initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
62         time.Sleep(time.Second)
63         if msgr != nil {
64                 t.Errorf("The rmr router is ready, should be not ready")
65         }
66 }
67
68 func TestIsReadyFailure(t *testing.T) {
69         log := initLog(t)
70         go initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
71         assert.True(t, msgr == nil || !msgr.IsReady())
72 }
73
74 func TestSendRecvMsgSuccess(t *testing.T) {
75         log := initLog(t)
76
77         go initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
78         time.Sleep(time.Duration(2) * time.Second)
79         if msgr == nil || !msgr.IsReady()  {
80                 t.Errorf("#rmr_c_go_api_test.TestSendRecvMsgSuccess - The rmr router is not ready")
81         }
82
83         msg := NewMBuf(1, tests.MaxMsgSize, "test 1", &tests.DummyPayload, &tests.DummyXAction)
84         log.Debugf("#rmr_c_go_api_test.TestSendRecvMsgSuccess - Going to send the message: %#v\n", msg)
85         result, err := msgr.SendMsg(msg)
86
87         assert.Nil(t, err)
88         assert.NotNil(t, result)
89
90         msgR, err := msgr.RecvMsg()
91
92         assert.Nil(t, err)
93         assert.NotNil(t, msgR)
94         msgr.Close()
95 }
96
97 func TestSendMsgRmrInvalidPortError(t *testing.T) {
98         log := initLog(t)
99
100         go initRmr(tests.ReadyIntervalSec, "tcp:" + strconv.Itoa(5555), tests.MaxMsgSize, tests.Flags, log)
101         time.Sleep(time.Duration(2) * time.Second)
102         if msgr == nil || !msgr.IsReady()  {
103                 t.Errorf("#rmr_c_go_api_test.TestSendMsgRmrInvalidPortError - The rmr router is not ready")
104         }
105
106         msg := NewMBuf(1, tests.MaxMsgSize, "test 1", &tests.DummyPayload, &tests.DummyXAction)
107         log.Debugf("#rmr_c_go_api_test.TestSendMsgRmrInvalidPortError - Going to send the message: %#v\n", msg)
108         result, err := msgr.SendMsg(msg)
109
110         assert.NotNil(t, err)
111         assert.Nil(t, result)
112
113         msgr.Close()
114 }
115
116 func TestSendMsgRmrInvalidMsgNumError(t *testing.T) {
117         log := initLog(t)
118
119         go initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
120         time.Sleep(time.Duration(2) * time.Second)
121         if msgr == nil || !msgr.IsReady()  {
122                 t.Errorf("#rmr_c_go_api_test.TestSendMsgRmrInvalidMsgNumError - The rmr router is not ready")
123         }
124
125         msg := NewMBuf(10, tests.MaxMsgSize, "test 1", &tests.DummyPayload, &tests.DummyXAction)
126         log.Debugf("#rmr_c_go_api_test.TestSendMsgRmrInvalidMsgNumError - Going to send the message: %#v\n", msg)
127         result, err := msgr.SendMsg(msg)
128
129         assert.NotNil(t, err)
130         assert.Nil(t, result)
131
132         msgr.Close()
133 }
134
135 func TestIsReadySuccess(t *testing.T) {
136         log := initLog(t)
137
138         go initRmr(tests.ReadyIntervalSec, tests.GetPort(), tests.MaxMsgSize, tests.Flags, log)
139         time.Sleep(time.Duration(tests.ReadyIntervalSec))
140         if msgr == nil || !msgr.IsReady()  {
141                 t.Errorf("#rmr_c_go_api_test.TestIsReadySuccess - The rmr router is not ready")
142         }
143 }
144
145 func initRmr(readyIntervalSec int, port string, maxMsgSize int, flags int, log *logger.Logger){
146         var ctx *Context
147         msgr = ctx.Init(readyIntervalSec, port, maxMsgSize, flags, log)
148 }
149
150 func initLog(t *testing.T) *logger.Logger {
151         log, err := logger.InitLogger(logger.DebugLevel)
152         if err != nil {
153                 t.Errorf("#rmr_c_go_api_test.initLog - failed to initialize logger, error: %s", err)
154         }
155         return log
156 }