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