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