b3b4352f766bef4380e14e6ba0de3ca3a7c8a691
[ric-plt/submgr.git] / pkg / teststubdummy / stubRmrDummy.go
1 /*
2 ==================================================================================
3   Copyright (c) 2019 AT&T Intellectual Property.
4   Copyright (c) 2019 Nokia
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10        http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20 //
21 // EXAMPLE HOW TO HAVE RMR STUB
22 //
23
24 package teststubdummy
25
26 import (
27         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
28         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/xapptweaks"
29         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
30         "testing"
31 )
32
33 //-----------------------------------------------------------------------------
34 //
35 //-----------------------------------------------------------------------------
36 type RmrDummyStub struct {
37         teststub.RmrStubControl
38         reqMsg  int
39         respMsg int
40 }
41
42 //-----------------------------------------------------------------------------
43 //
44 //-----------------------------------------------------------------------------
45 func CreateNewRmrDummyStub(desc string, srcId teststub.RmrSrcId, rtgSvc teststub.RmrRtgSvc, stat string, mtypeseed int) *RmrDummyStub {
46         dummyStub := &RmrDummyStub{}
47         dummyStub.RmrStubControl.Init(desc, srcId, rtgSvc, stat, mtypeseed)
48         dummyStub.reqMsg = mtypeseed + 1
49         dummyStub.respMsg = mtypeseed + 2
50         return dummyStub
51 }
52
53 //-----------------------------------------------------------------------------
54 //
55 //-----------------------------------------------------------------------------
56
57 func (tc *RmrDummyStub) SendReq(t *testing.T, plen int) {
58         tc.Logger.Info("SendReq")
59         len := plen
60         if len == 0 {
61                 len = 100
62         }
63         params := xapptweaks.NewParams(nil)
64         params.Mtype = tc.reqMsg
65         params.SubId = -1
66
67         params.Payload = make([]byte, len)
68         params.PayloadLen = 0
69         params.Meid = &xapp.RMRMeid{RanName: "TEST"}
70         params.Xid = "TEST"
71         params.Mbuf = nil
72
73         snderr := tc.RmrSend(params, 5)
74         if snderr != nil {
75                 tc.TestError(t, "%s", snderr.Error())
76         }
77         return
78 }
79
80 func (tc *RmrDummyStub) SendResp(t *testing.T, plen int) {
81         tc.Logger.Info("SendReq")
82         len := plen
83         if len == 0 {
84                 len = 100
85         }
86         params := xapptweaks.NewParams(nil)
87         params.Mtype = tc.respMsg
88         params.SubId = -1
89         params.Payload = make([]byte, len)
90         params.PayloadLen = 0
91         params.Meid = &xapp.RMRMeid{RanName: "TEST"}
92         params.Xid = "TEST"
93         params.Mbuf = nil
94
95         snderr := tc.RmrSend(params, 5)
96         if snderr != nil {
97                 tc.TestError(t, "%s", snderr.Error())
98         }
99         return
100 }
101
102 func (tc *RmrDummyStub) RecvReq(t *testing.T) bool {
103         tc.Logger.Info("RecvReq")
104
105         msg := tc.WaitMsg(15)
106         if msg != nil {
107                 if msg.Mtype != tc.reqMsg {
108                         tc.TestError(t, "Received wrong mtype expected %d got %d, error", tc.reqMsg, msg.Mtype)
109                         return false
110                 }
111                 return true
112         } else {
113                 tc.TestError(t, "Not Received msg within %d secs", 15)
114         }
115         return false
116 }
117
118 func (tc *RmrDummyStub) RecvResp(t *testing.T) bool {
119         tc.Logger.Info("RecvResp")
120
121         msg := tc.WaitMsg(15)
122         if msg != nil {
123                 if msg.Mtype != tc.respMsg {
124                         tc.TestError(t, "Received wrong mtype expected %d got %d, error", tc.respMsg, msg.Mtype)
125                         return false
126                 }
127                 return true
128         } else {
129                 tc.TestError(t, "Not Received msg within %d secs", 15)
130         }
131         return false
132 }