Subscription REST interface update
[ric-plt/submgr.git] / pkg / control / ut_stub_rtmgr_test.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 package control
21
22 import (
23         "encoding/json"
24         "net/http"
25         "sync"
26         "testing"
27         "time"
28
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"
31 )
32
33 //-----------------------------------------------------------------------------
34 //
35 //-----------------------------------------------------------------------------
36
37 type HttpEventWaiter struct {
38         teststub.TestWrapper
39         resultChan   chan bool
40         nextActionOk bool
41         sleep        int
42 }
43
44 func (msg *HttpEventWaiter) SetResult(res bool) {
45         msg.resultChan <- res
46 }
47
48 func (msg *HttpEventWaiter) WaitResult(t *testing.T) bool {
49         select {
50         case result := <-msg.resultChan:
51                 return result
52         case <-time.After(15 * time.Second):
53                 msg.TestError(t, "Waiter not received result status from case within 15 secs")
54                 return false
55         }
56         msg.TestError(t, "Waiter error in default branch")
57         return false
58 }
59
60 //-----------------------------------------------------------------------------
61 //
62 //-----------------------------------------------------------------------------
63 type testingHttpRtmgrStub struct {
64         sync.Mutex
65         teststub.TestWrapper
66         port        string
67         eventWaiter *HttpEventWaiter
68 }
69
70 func (tc *testingHttpRtmgrStub) NextEvent(eventWaiter *HttpEventWaiter) {
71         tc.Lock()
72         defer tc.Unlock()
73         tc.eventWaiter = eventWaiter
74 }
75
76 func (tc *testingHttpRtmgrStub) AllocNextEvent(nextAction bool) *HttpEventWaiter {
77         eventWaiter := &HttpEventWaiter{
78                 resultChan:   make(chan bool),
79                 nextActionOk: nextAction,
80         }
81         eventWaiter.TestWrapper.Init("localhost:" + tc.port)
82         tc.NextEvent(eventWaiter)
83         return eventWaiter
84 }
85
86 func (tc *testingHttpRtmgrStub) AllocNextSleep(sleep int, nextAction bool) *HttpEventWaiter {
87         eventWaiter := &HttpEventWaiter{
88                 resultChan:   make(chan bool),
89                 nextActionOk: nextAction,
90                 sleep:        sleep,
91         }
92         eventWaiter.TestWrapper.Init("localhost:" + tc.port)
93         tc.NextEvent(eventWaiter)
94         return eventWaiter
95 }
96
97 func (tc *testingHttpRtmgrStub) http_handler(w http.ResponseWriter, r *http.Request) {
98
99         tc.Lock()
100         defer tc.Unlock()
101         var id int32 = -1
102
103         if r.Method == http.MethodPost || r.Method == http.MethodDelete {
104                 var req rtmgr_models.XappSubscriptionData
105                 err := json.NewDecoder(r.Body).Decode(&req)
106                 if err != nil {
107                         tc.Error("%s", err.Error())
108                 }
109                 tc.Debug("handling SubscriptionID=%d Address=%s Port=%d", *req.SubscriptionID, *req.Address, *req.Port)
110                 id = *req.SubscriptionID
111         }
112         if r.Method == http.MethodPut {
113                 var req rtmgr_models.XappList
114                 err := json.NewDecoder(r.Body).Decode(&req)
115                 if err != nil {
116                         tc.Error("%s", err.Error())
117                 }
118                 tc.Debug("handling put")
119         }
120
121         var code int = 0
122         switch r.Method {
123         case http.MethodPost:
124                 code = 201
125                 if tc.eventWaiter != nil {
126                         if tc.eventWaiter.nextActionOk == false {
127                                 code = 400
128                         }
129                         if tc.eventWaiter.sleep != 0 {
130                                 <-time.After(time.Duration(tc.eventWaiter.sleep) * time.Millisecond)
131                                 tc.Debug("sleeping done, %v", id)
132                         }
133                 }
134         case http.MethodDelete:
135                 code = 200
136                 if tc.eventWaiter != nil {
137                         if tc.eventWaiter.nextActionOk == false {
138                                 code = 400
139                         }
140                 }
141         case http.MethodPut:
142                 code = 201
143                 if tc.eventWaiter != nil {
144                         if tc.eventWaiter.nextActionOk == false {
145                                 code = 400
146                         }
147                 }
148         default:
149                 code = 200
150         }
151
152         waiter := tc.eventWaiter
153         tc.eventWaiter = nil
154         if waiter != nil {
155                 waiter.SetResult(true)
156         }
157         tc.Debug("Method=%s Reply with code %d", r.Method, code)
158         w.WriteHeader(code)
159
160 }
161
162 func (tc *testingHttpRtmgrStub) run() {
163         http.HandleFunc("/", tc.http_handler)
164         http.ListenAndServe("localhost:"+tc.port, nil)
165 }
166
167 func createNewHttpRtmgrStub(desc string, port string) *testingHttpRtmgrStub {
168         tc := &testingHttpRtmgrStub{}
169         tc.port = port
170         tc.TestWrapper.Init(desc)
171         return tc
172 }