Restart/Start of RM, fetch E2T data from E2manager - implementation in RM for v1...
[ric-plt/rtmgr.git] / pkg / nbi / httpgetter_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    This source code is part of the near-RT RIC (RAN Intelligent Controller)
20    platform project (RICP).
21
22 ==================================================================================
23 */
24 /*
25   Mnemonic:     httpgetter.go
26   Abstract:     HTTPgetter unit tests
27   Date:         14 May 2019
28 */
29
30 package nbi
31
32 import (
33         "net"
34         "net/http"
35         "net/http/httptest"
36         "testing"
37 )
38
39 var (
40         XMURL = "http://127.0.0.1:3000/ric/v1/xapps"
41         E2MURL = "http://127.0.0.1:8080/ric/v1/e2t/list"
42 )
43
44 func TestFetchXappListInvalidData(t *testing.T) {
45         var httpGetter = NewHttpGetter()
46         _, err := httpGetter.FetchAllXApps(XMURL)
47         if err == nil {
48                 t.Error("No XApp data received: " + err.Error())
49         }
50 }
51
52 func TestFetchXappListWithInvalidData(t *testing.T) {
53         var expected = 0
54         b := []byte(`{"ID":"deadbeef1234567890", "Version":0, "EventType":"all"}`)
55         l, err := net.Listen("tcp", "127.0.0.1:3000")
56         if err != nil {
57                 t.Error("Failed to create listener: " + err.Error())
58         }
59         ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
60                 //t.Log(r.Method)
61                 //t.Log(r.URL)
62                 if r.Method == "GET" && r.URL.String() == "/ric/v1/xapps" {
63                         //t.Log("Sending reply")
64                         w.Header().Add("Content-Type", "application/json")
65                         w.WriteHeader(http.StatusOK)
66                         w.Write(b)
67                 }
68         }))
69         ts.Listener.Close()
70         ts.Listener = l
71
72         ts.Start()
73         defer ts.Close()
74         var httpGetter = NewHttpGetter()
75         xapplist, err := httpGetter.FetchAllXApps(XMURL)
76         if err == nil {
77                 t.Error("Error occured: " + err.Error())
78         } else {
79                 //t.Log(len(*xapplist))
80                 if len(*xapplist) != expected {
81                         t.Error("Invalid XApp data: got " + string(len(*xapplist)) + ", expected " + string(expected))
82                 }
83         }
84 }
85
86 func TestFetchAllXAppsWithValidData(t *testing.T) {
87         var expected = 1
88         b := []byte(`[
89  {
90  "name":"xapp-01","status":"unknown","version":"1.2.3",
91     "instances":[
92         {"name":"xapp-01-instance-01","status":"pending","ip":"172.16.1.103","port":4555,
93             "txMessages":["ControlIndication"],
94             "rxMessages":["LoadIndication","Reset"]
95         },
96         {"name":"xapp-01-instance-02","status":"pending","ip":"10.244.1.12","port":4561,
97             "txMessages":["ControlIndication","SNStatusTransfer"],
98             "rxMessages":["LoadIndication","HandoverPreparation"]
99         }
100     ]
101 }
102 ]`)
103         l, err := net.Listen("tcp", "127.0.0.1:3000")
104         if err != nil {
105                 t.Error("Failed to create listener: " + err.Error())
106         }
107         ts := httptest.NewUnstartedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
108                 //t.Log(r.Method)
109                 //t.Log(r.URL)
110                 if r.Method == "GET" && r.URL.String() == "/ric/v1/xapps" {
111                         //t.Log("Sending reply")
112                         w.Header().Add("Content-Type", "application/json")
113                         w.WriteHeader(http.StatusOK)
114                         w.Write(b)
115                 }
116         }))
117         ts.Listener.Close()
118         ts.Listener = l
119
120         ts.Start()
121         defer ts.Close()
122         var httpGetter = NewHttpGetter()
123         xapplist, err := httpGetter.FetchAllXApps(XMURL)
124         if err != nil {
125                 t.Error("Error occured: " + err.Error())
126         } else {
127                 if len(*xapplist) != expected {
128                         t.Error("Invalid XApp data: got " + string(len(*xapplist)) + ", expected " + string(expected))
129                 }
130         }
131 }