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