Restructured test files. stubs locates in own files etc.
[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         var req rtmgr_models.XappSubscriptionData
88         err := json.NewDecoder(r.Body).Decode(&req)
89         if err != nil {
90                 xapp.Logger.Error("%s", err.Error())
91         }
92         xapp.Logger.Info("(%s) handling Address=%s Port=%d SubscriptionID=%d", hc.desc, *req.Address, *req.Port, *req.SubscriptionID)
93
94         var code int = 0
95         switch r.Method {
96         case http.MethodPost:
97                 code = 201
98                 if hc.eventWaiter != nil {
99                         if hc.eventWaiter.nextActionOk == false {
100                                 code = 400
101                         }
102                 }
103         case http.MethodDelete:
104                 code = 200
105                 if hc.eventWaiter != nil {
106                         if hc.eventWaiter.nextActionOk == false {
107                                 code = 400
108                         }
109                 }
110         default:
111                 code = 200
112         }
113
114         waiter := hc.eventWaiter
115         hc.eventWaiter = nil
116         if waiter != nil {
117                 waiter.SetResult(true)
118         }
119         xapp.Logger.Info("(%s) Method=%s Reply with code %d", hc.desc, r.Method, code)
120         w.WriteHeader(code)
121
122 }
123
124 func (hc *testingHttpRtmgrStub) run() {
125         http.HandleFunc("/", hc.http_handler)
126         http.ListenAndServe("localhost:"+hc.port, nil)
127 }
128
129 func createNewHttpRtmgrStub(desc string, port string) *testingHttpRtmgrStub {
130         hc := &testingHttpRtmgrStub{}
131         hc.desc = desc
132         hc.port = port
133         return hc
134 }