Generalized unittest stubs so frame can be used also in other apps
[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, rtfile string, port string, stat string, mtypeseed int) *RmrDummyStub {
46         dummyStub := &RmrDummyStub{}
47         dummyStub.RmrStubControl.Init(desc, rtfile, port, 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) {
58         tc.Logger.Info("SendReq")
59         var dummyBuf []byte = make([]byte, 100)
60         params := xapptweaks.NewParams(nil)
61         params.Mtype = tc.reqMsg
62         params.SubId = -1
63         params.Payload = dummyBuf
64         params.PayloadLen = 100
65         params.Meid = &xapp.RMRMeid{RanName: "TEST"}
66         params.Xid = "TEST"
67         params.Mbuf = nil
68
69         snderr := tc.RmrSend(params)
70         if snderr != nil {
71                 tc.TestError(t, "%s", snderr.Error())
72         }
73         return
74 }
75
76 func (tc *RmrDummyStub) RecvResp(t *testing.T) bool {
77         tc.Logger.Info("RecvResp")
78
79         msg := tc.WaitMsg(15)
80         if msg != nil {
81                 if msg.Mtype != tc.respMsg {
82                         tc.TestError(t, "Received wrong mtype expected %d got %d, error", tc.respMsg, msg.Mtype)
83                         return false
84                 }
85                 return true
86         } else {
87                 tc.TestError(t, "Not Received msg within %d secs", 15)
88         }
89         return false
90 }
91
92 //-----------------------------------------------------------------------------
93 //
94 //-----------------------------------------------------------------------------
95 func RmrDummyHandleMessage(msg *xapptweaks.RMRParams, mtypeseed int, rmr xapptweaks.XAppWrapperIf) (bool, error) {
96         if msg.Mtype == mtypeseed+1 {
97                 var dummyBuf []byte = make([]byte, 100)
98                 params := xapptweaks.NewParams(nil)
99                 params.Mtype = mtypeseed + 2
100                 params.SubId = msg.SubId
101                 params.Payload = dummyBuf
102                 params.PayloadLen = 100
103                 params.Meid = msg.Meid
104                 params.Xid = msg.Xid
105                 params.Mbuf = nil
106                 rmr.GetLogger().Info("SEND DUMMY RESP: %s", params.String())
107                 err := rmr.RmrSend(params)
108                 if err != nil {
109                         rmr.GetLogger().Error("RmrDummyHandleMessage: err(%s)", err.Error())
110                 }
111                 return true, err
112         }
113         return false, nil
114 }