59467dcdff92b57c6c7f8078692d87d3c2975cd4
[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         "gerrit.o-ran-sc.org/r/ric-plt/submgr/pkg/rtmgr_models"
25         "gerrit.o-ran-sc.org/r/ric-plt/xapp-frame/pkg/xapp"
26         "net/http"
27         "sync"
28         "testing"
29         "time"
30 )
31
32 //-----------------------------------------------------------------------------
33 //
34 //-----------------------------------------------------------------------------
35
36 type httpEventWaiter struct {
37         resultChan   chan bool
38         nextActionOk bool
39 }
40
41 func (msg *httpEventWaiter) SetResult(res bool) {
42         msg.resultChan <- res
43 }
44
45 func (msg *httpEventWaiter) WaitResult(t *testing.T) bool {
46         select {
47         case result := <-msg.resultChan:
48                 return result
49         case <-time.After(15 * time.Second):
50                 testError(t, "Waiter not received result status from case within 15 secs")
51                 return false
52         }
53         testError(t, "Waiter error in default branch")
54         return false
55 }
56
57 //-----------------------------------------------------------------------------
58 //
59 //-----------------------------------------------------------------------------
60 type testingHttpRtmgrStub struct {
61         sync.Mutex
62         desc        string
63         port        string
64         eventWaiter *httpEventWaiter
65 }
66
67 func (hc *testingHttpRtmgrStub) NextEvent(eventWaiter *httpEventWaiter) {
68         hc.Lock()
69         defer hc.Unlock()
70         hc.eventWaiter = eventWaiter
71 }
72
73 func (hc *testingHttpRtmgrStub) AllocNextEvent(nextAction bool) *httpEventWaiter {
74         eventWaiter := &httpEventWaiter{
75                 resultChan:   make(chan bool),
76                 nextActionOk: nextAction,
77         }
78         hc.NextEvent(eventWaiter)
79         return eventWaiter
80 }
81
82 func (hc *testingHttpRtmgrStub) http_handler(w http.ResponseWriter, r *http.Request) {
83
84         hc.Lock()
85         defer hc.Unlock()
86
87         if r.Method == http.MethodPost || r.Method == http.MethodDelete {
88                 var req rtmgr_models.XappSubscriptionData
89                 err := json.NewDecoder(r.Body).Decode(&req)
90                 if err != nil {
91                         xapp.Logger.Error("%s", err.Error())
92                 }
93                 xapp.Logger.Info("(%s) handling SubscriptionID=%d Address=%s Port=%d", hc.desc, *req.SubscriptionID, *req.Address, *req.Port)
94         }
95         if r.Method == http.MethodPut {
96                 var req rtmgr_models.XappList
97                 err := json.NewDecoder(r.Body).Decode(&req)
98                 if err != nil {
99                         xapp.Logger.Error("%s", err.Error())
100                 }
101                 xapp.Logger.Info("(%s) handling put", hc.desc)
102         }
103
104         var code int = 0
105         switch r.Method {
106         case http.MethodPost:
107                 code = 201
108                 if hc.eventWaiter != nil {
109                         if hc.eventWaiter.nextActionOk == false {
110                                 code = 400
111                         }
112                 }
113         case http.MethodDelete:
114                 code = 200
115                 if hc.eventWaiter != nil {
116                         if hc.eventWaiter.nextActionOk == false {
117                                 code = 400
118                         }
119                 }
120         case http.MethodPut:
121                 code = 201
122                 if hc.eventWaiter != nil {
123                         if hc.eventWaiter.nextActionOk == false {
124                                 code = 400
125                         }
126                 }
127         default:
128                 code = 200
129         }
130
131         waiter := hc.eventWaiter
132         hc.eventWaiter = nil
133         if waiter != nil {
134                 waiter.SetResult(true)
135         }
136         xapp.Logger.Info("(%s) Method=%s Reply with code %d", hc.desc, r.Method, code)
137         w.WriteHeader(code)
138
139 }
140
141 func (hc *testingHttpRtmgrStub) run() {
142         http.HandleFunc("/", hc.http_handler)
143         http.ListenAndServe("localhost:"+hc.port, nil)
144 }
145
146 func createNewHttpRtmgrStub(desc string, port string) *testingHttpRtmgrStub {
147         hc := &testingHttpRtmgrStub{}
148         hc.desc = desc
149         hc.port = port
150         return hc
151 }