2 ==================================================================================
3 Copyright (c) 2019 AT&T Intellectual Property.
4 Copyright (c) 2019 Nokia
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
29 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_models"
30 "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/teststub"
33 //-----------------------------------------------------------------------------
35 //-----------------------------------------------------------------------------
37 type HttpEventWaiter struct {
44 func (msg *HttpEventWaiter) SetResult(res bool) {
48 func (msg *HttpEventWaiter) WaitResult(t *testing.T) bool {
50 case result := <-msg.resultChan:
52 case <-time.After(15 * time.Second):
53 msg.TestError(t, "Waiter not received result status from case within 15 secs")
56 msg.TestError(t, "Waiter error in default branch")
60 //-----------------------------------------------------------------------------
62 //-----------------------------------------------------------------------------
63 type testingHttpRtmgrStub struct {
67 eventWaiter *HttpEventWaiter
70 func (tc *testingHttpRtmgrStub) NextEvent(eventWaiter *HttpEventWaiter) {
73 tc.eventWaiter = eventWaiter
76 func (tc *testingHttpRtmgrStub) AllocNextEvent(nextAction bool) *HttpEventWaiter {
77 eventWaiter := &HttpEventWaiter{
78 resultChan: make(chan bool),
79 nextActionOk: nextAction,
81 eventWaiter.TestWrapper.Init("localhost:" + tc.port)
82 tc.NextEvent(eventWaiter)
86 func (tc *testingHttpRtmgrStub) AllocNextSleep(sleep int, nextAction bool) *HttpEventWaiter {
87 eventWaiter := &HttpEventWaiter{
88 resultChan: make(chan bool),
89 nextActionOk: nextAction,
92 eventWaiter.TestWrapper.Init("localhost:" + tc.port)
93 tc.NextEvent(eventWaiter)
97 func (tc *testingHttpRtmgrStub) http_handler(w http.ResponseWriter, r *http.Request) {
103 if r.Method == http.MethodPost || r.Method == http.MethodDelete {
104 var req rtmgr_models.XappSubscriptionData
105 err := json.NewDecoder(r.Body).Decode(&req)
107 tc.Error("%s", err.Error())
109 tc.Debug("handling SubscriptionID=%d Address=%s Port=%d", *req.SubscriptionID, *req.Address, *req.Port)
110 id = *req.SubscriptionID
112 if r.Method == http.MethodPut {
113 var req rtmgr_models.XappList
114 err := json.NewDecoder(r.Body).Decode(&req)
116 tc.Error("%s", err.Error())
118 tc.Debug("handling put")
123 case http.MethodPost:
125 if tc.eventWaiter != nil {
126 if tc.eventWaiter.nextActionOk == false {
129 if tc.eventWaiter.sleep != 0 {
130 <-time.After(time.Duration(tc.eventWaiter.sleep) * time.Millisecond)
131 tc.Debug("sleeping done, %v", id)
134 case http.MethodDelete:
136 if tc.eventWaiter != nil {
137 if tc.eventWaiter.nextActionOk == false {
143 if tc.eventWaiter != nil {
144 if tc.eventWaiter.nextActionOk == false {
152 waiter := tc.eventWaiter
155 waiter.SetResult(true)
157 tc.Debug("Method=%s Reply with code %d", r.Method, code)
162 func (tc *testingHttpRtmgrStub) run() {
163 http.HandleFunc("/", tc.http_handler)
164 http.ListenAndServe("localhost:"+tc.port, nil)
167 func createNewHttpRtmgrStub(desc string, port string) *testingHttpRtmgrStub {
168 tc := &testingHttpRtmgrStub{}
170 tc.TestWrapper.Init(desc)